Skip to content

This project implements a real-time fluid simulation using Navier-Stokes equations. The simulation models fluid behavior, including density diffusion, advection, and velocity updates, creating realistic smoke or liquid-like effects.

License

Notifications You must be signed in to change notification settings

Jalpan04/Fluid_simulator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🌊 Fluid Dynamics Simulation

Python Version Pygame NumPy License

A real-time, interactive 2D fluid simulation that visualizes complex fluid dynamics using the Navier-Stokes equations. This physics-based simulation demonstrates computational fluid dynamics principles with an intuitive, visually engaging interface.

Fluid Simulation Demo

Fluid Density Visualization

Velocity Field Visualization

Velocity Vector Field Visualization

✨ Features

  • Interactive Fluid Dynamics: Click and drag to introduce forces and dye into the simulation
  • Real-time Physics: Accurate numerical approximation of the Navier-Stokes equations
  • Dual Visualization Modes: View both the fluid density and underlying velocity field
  • Customizable Parameters: Adjust diffusion, viscosity, and other physical properties
  • Optimized Performance: Efficiently implemented algorithms for smooth real-time interaction

🧪 Mathematical Foundation

The simulation solves the incompressible Navier-Stokes equations:

Where:

  • u: Velocity field
  • p: Pressure
  • ρ: Density
  • ν: Viscosity coefficient
  • f: External forces

With the incompressibility constraint:

Navier-Stokes Equations

The simulation solves the incompressible Navier-Stokes equations:

∂u/∂t + (u ⋅ ∇)u = -(1/ρ)∇p + ν∇²u + f

Where:

  • u: Velocity field
  • p: Pressure
  • ρ: Density
  • ν: Viscosity coefficient
  • f: External forces

With the incompressibility constraint:

∇ ⋅ u = 0

Prerequisites

# Install required dependencies
pip install numpy pygame

Running the Simulation

# Launch the simulation
python fluidsim.py

🎮 Controls

Key/Action Description
Mouse Drag Add fluid density and velocity
Space Pause/Resume simulation
V Toggle velocity visualization
C Clear the simulation
R Add random forces
+/- Increase/decrease simulation speed
ESC Exit application

🏗️ Code Architecture

fluid-simulation/
├── fluidsim.py         # Main application entry point
├── fluid_simulator.py  # Core fluid dynamics solver
├── visualization.py    # Rendering and visualization components
├── utils/
│   ├── vector_field.py # Vector field operations
│   └── math_utils.py   # Math helper functions
└── tests/              # Test suite

Key Classes

  • FluidSimulator: Core physics engine implementing Navier-Stokes solver
  • FluidRenderer: Handles visualization of density and velocity fields
  • SimulationController: Manages user input and simulation parameters

💡 Implementation Details

The implementation follows Jos Stam's "Real-Time Fluid Dynamics for Games" approach with several optimizations:

  1. Velocity Field Update:

    • Advection → Diffusion → External Forces → Projection
  2. Density Field Update:

    • Advection → Diffusion → Source Addition
  3. Projection Method:

    • Computes pressure to ensure mass conservation
    • Uses multi-grid or conjugate gradient methods for efficiency

🔧 Customization

The simulation offers various parameters for experimentation:

# Example configuration
config = {
    'grid_size': 128,
    'diffusion_rate': 0.0001,
    'viscosity': 0.00001,
    'dt': 0.1,
    'iterations': 20
}

🔮 Future Enhancements

  • GPU acceleration using OpenCL/CUDA
  • 3D fluid simulation
  • Interaction with rigid bodies
  • Advanced rendering with particle systems
  • Web-based version using WebGL

📚 Resources

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.


About

This project implements a real-time fluid simulation using Navier-Stokes equations. The simulation models fluid behavior, including density diffusion, advection, and velocity updates, creating realistic smoke or liquid-like effects.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages