I chose a 2D incompressible fluid simulation where the liquid is
restricted to an initial waterdrop region. The simulation tracks
velocity fields (u, v) and a liquid fraction
field (liq in [0,1]) on a regular grid.
The drop falls by gravity and deforms while staying as a compact
liquid blob. The program renders a series of PPM images
(drop_0000.ppm, drop_0001.ppm, …) so I can see
time evolution, plus one final image.
This is interesting because it is a realistic PDE-based application, it gives visually meaningful output, and it has enough computation to study performance.
The code is in fluid2d.cc and is fully serial C++17 with
no external libraries.
Main loop per time step is:
liq) using the projected
velocity.The code prints:
All key parameters are configurable from command line
(nx, ny, steps, dt,
visc, jacobi, gravity,
frame-every, output names), so runtime can be scaled from
seconds to minutes.
Most heavy loops are per-cell stencil updates and are data-parallel:
These loops can later be parallelized with multicore CPU threads and GPU kernels. Likely speedup limits are memory bandwidth and the iterative pressure solve (many sweeps over the whole grid each step).