2D shallow-water solver. Lax-Friedrichs finite differences on a periodic grid, OpenMP threaded. Single C++ file, ~150 lines.
Gaussian bump at t=0 spreads as gravity waves on a 1024×1024 grid.
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).
1024² grid, t_final = 1.5, WSL2 Ubuntu on a 4-core + HT laptop.
| Threads | Wall (s) | Mcell-updates/s | Speedup |
|---|---|---|---|
| 1 | 40.15 | 35.94 | 1.00× |
| 2 | 28.60 | 50.44 | 1.40× |
| 4 | 15.96 | 90.39 | 2.52× |
| 8 | 13.35 | 108.04 | 3.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.
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.