miniSWE

2D shallow-water solver. Lax-Friedrichs finite differences on a periodic grid, OpenMP threaded. Single C++ file, ~150 lines.

Animation

Gaussian bump at t=0 spreads as gravity waves on a 1024×1024 grid.

shallow-water height field animation

Equations

State U = [h, hu, hv], gravity g = 9.81.

∂h /∂t + ∂(hu)/∂x         + ∂(hv)/∂y         = 0
∂hu/∂t + ∂(hu² + ½gh²)/∂x + ∂(huv)/∂y        = 0
∂hv/∂t + ∂(huv)/∂x        + ∂(hv² + ½gh²)/∂y = 0

Lax-Friedrichs update with CFL-adaptive timestep Δt = 0.4 · min(Δx,Δy) / max(|u|+c, |v|+c), c=√(gh).

Strong scaling

1024² grid, t_final = 1.5, WSL2 Ubuntu on a 4-core + HT laptop.

ThreadsWall (s)Mcell-updates/sSpeedup
140.1535.941.00×
228.6050.441.40×
415.9690.392.52×
813.35108.043.01×

Memory-bandwidth bound, as expected for a stencil code. Speedup plateaus at physical-core count; HT threads share cache and bus so the 4→8 gain is small.

Build & run

make
OMP_NUM_THREADS=8 ./swe 1024 1024 2.0 8   # nx ny t_final n_frames
./scale.sh                                # strong-scaling sweep
python3 make_gif.py                       # stitch frames into wave.gif

Source: repository · MIT-style, do whatever.