This project implements a real-time 2D fluid flow simulation using a well-known method developed by Jos Stam. The simulation and visualization are written in C++, with SDL2 for graphical output.
The simulation showcases how an incompressible fluid, governed by the Navier-Stokes equations, develops instability at the interface of regions with differing velocities. The dye-based visualization allows direct observation of vortex formation and mixing.
Kelvin–Helmholtz instability occurs when there is velocity shear in a single continuous fluid or between two fluids. Small disturbances at the interface grow, leading to turbulence and vortices. Real-time simulation of such flows is computationally intensive, but Stam's method provides a stable, efficient approach suitable for visualization.
The simulation is based on the discretized incompressible Navier-Stokes equations:
- Projection Method: The velocity field is projected onto a divergence-free field to enforce incompressibility.
- Periodic Boundary Conditions: The fluid grid wraps in both x and y directions (torus topology).
- Simulation Steps:
- Force Addition: External forces perturb the fluid.
- Advection: Fluid properties are advected using the velocity field.
- Diffusion: Viscosity is applied using Fast Fourier Transform (FFT) due to periodic boundaries.
- Projection: The velocity field is made divergence-free in Fourier space.
A dye field is evolved alongside the velocity field to visualize the flow.
- Language: C++
- Graphics: SDL2 library for real-time rendering.
- Main code: Located in
src/fluid_flow.cpp. - Build system: Use the
Makefilein thesrc/directory. - Visualization: The dye concentration is mapped to pixel colors; users can interactively inject dye with the left mouse button.
- Four
N x (N+2)arrays are used for velocity components (with extra columns for FFT). - Force fields are stored separately and can be toggled during the simulation.
- Interactive dye injection (left mouse button).
- Real-time rendering of vortex evolution.
- Simple mechanism for applying and removing forces.
- C++ compiler
- SDL2 development libraries
- Navigate to the
src/directory:cd src - Run
maketo build the executable:make
- After building, run the simulation from the
run/directory:./fluid_flow
- The GUI window will display the evolving fluid flow.
- Use the left mouse button to add a circular blob of dye for visual study.
The simulation produces evolving patterns that visualize the KH instability. Below are sample outputs at various simulation steps:
| 20 steps | 100 steps | 300 steps | 500 steps |
|---|---|---|---|
![]() |
![]() |
![]() |
![]() |
Another scenario applies a leftward force to a central region:
| 20 steps | 100 steps | 300 steps | 500 steps |
|---|---|---|---|
![]() |
![]() |
![]() |
![]() |
- Increasing viscosity causes the fluid to stabilize quickly.
- Decreasing viscosity enhances instability and vortex formation.
- Jos Stam. 2001. "A Simple Fluid Solver Based on the FFT." Journal of Graphics Tools, 6(2), 43–52. doi
- Jos Stam. 1999. "Stable fluids." SIGGRAPH '99. ACM Press/Addison-Wesley Publishing Co., USA, 121–128.
- SDL2 Library GitHub
- Kelvin–Helmholtz instability (Wikipedia)
For further details, see the full report.pdf.







