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.
- 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
The simulation solves the incompressible Navier-Stokes equations:
Where:
- u: Velocity field
- p: Pressure
- ρ: Density
- ν: Viscosity coefficient
- f: External forces
With the incompressibility constraint:
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
# Install required dependencies
pip install numpy pygame# Launch the simulation
python fluidsim.py| 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 |
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
FluidSimulator: Core physics engine implementing Navier-Stokes solverFluidRenderer: Handles visualization of density and velocity fieldsSimulationController: Manages user input and simulation parameters
The implementation follows Jos Stam's "Real-Time Fluid Dynamics for Games" approach with several optimizations:
-
Velocity Field Update:
- Advection → Diffusion → External Forces → Projection
-
Density Field Update:
- Advection → Diffusion → Source Addition
-
Projection Method:
- Computes pressure to ensure mass conservation
- Uses multi-grid or conjugate gradient methods for efficiency
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
}- GPU acceleration using OpenCL/CUDA
- 3D fluid simulation
- Interaction with rigid bodies
- Advanced rendering with particle systems
- Web-based version using WebGL
- Real-Time Fluid Dynamics for Games by Jos Stam
- Fluid Simulation for Computer Graphics by Robert Bridson
- Physically Based Modeling: Principles and Practice by Andrew Witkin and David Baraff
This project is licensed under the MIT License - see the LICENSE file for details.

