{
 "id": "shallow-water-equations",
 "title": "Shallow Water Equations (Height-field Water)",
 "aliases": [
  "SWE",
  "height-field water",
  "virtual pipes"
 ],
 "domain": "volumes",
 "subdomain": "water simulation",
 "stage": "volume",
 "realtime": "realtime",
 "platforms": [
  "webgl2",
  "webgpu",
  "desktop-gpu",
  "cuda",
  "cpu"
 ],
 "year_introduced": 1990,
 "inputs": [
  "heightfield"
 ],
 "outputs": [
  "heightfield",
  "normals"
 ],
 "style": "both",
 "dynamic": "dynamic",
 "summary": "Simulate water as a 2D height field plus a velocity field, so waves, ripples and shoreline flow update on a grid with one cheap pass per step.",
 "how_it_works": "The 3D flow is vertically averaged, which leaves a water height h and horizontal velocity per grid cell. Momentum is changed by the gradient of the water surface plus terrain height (gravity pushes water downhill) and mass moves as the divergence of the flux. A simple form stores a height and a vertical velocity per cell and sets acceleration proportional to the average of the four neighbour heights minus the cell height, then damps; a better form uses per-edge flux (virtual pipes) to stay mass-conserving over a terrain. Each step is explicit and needs a time step below the wave speed over cell size (CFL). Interaction is easy: add a bump to h for a drop or a moving object. The height field becomes a displaced grid with normals from central differences, and the same grid can store foam and flow for shading.",
 "when_to_use": "Choose it for interactive ripples, splashes, pools and river or flood flow where the surface is single-valued and interaction by objects is required, at a cost that fits a mobile or WebGL budget.\n\nPrefer `fft-ocean` for an open-sea surface, and `gerstner-waves` for analytic swells. Prefer a particle or grid-based 3D fluid solver when you need breaking waves, overturning or splashes that leave the surface. Use `water-caustics-realtime` to light the floor under it.",
 "cost": {
  "class": "cheap",
  "notes": "One or two grid passes per step; resolution and number of substeps are the cost control."
 },
 "failure_modes": [
  "Instability or explosion when the time step violates the CFL condition",
  "Energy and mass drift with the simple 4-neighbour wave equation, giving ever-growing or dying ripples",
  "Cannot represent overturning waves, splashes or vertical surfaces because the surface is a single-valued height",
  "Reflection from the grid boundary unless an absorbing sponge layer is added",
  "Shoreline wet/dry cells produce negative depths and flickering without careful handling"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1145/97879.97884",
   "type": "paper",
   "title": "Rapid, stable fluid dynamics for computer graphics",
   "authors": [
    "Michael Kass",
    "Gavin Miller"
   ],
   "year": 1990,
   "venue": "Proceedings of the 17th annual conference on Computer graphics and interactive techniques",
   "url": "https://doi.org/10.1145/97879.97884",
   "ids": {
    "doi": "10.1145/97879.97884"
   },
   "license": null,
   "extra": {
    "csl_type": "proceedings-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/97879.97884",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/97879.97884",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/97879.97884",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/97879.97884",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/97879.97884",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "kass1990rapid"
  },
  {
   "role": "implementation",
   "key": "gh:evanw/webgl-water",
   "type": "code",
   "title": "evanw/webgl-water",
   "description": "WebGL Water Demo",
   "authors": [
    "evanw"
   ],
   "year": 2015,
   "venue": "GitHub",
   "url": "https://github.com/evanw/webgl-water",
   "ids": {
    "repo": "evanw/webgl-water"
   },
   "license": null,
   "extra": {
    "stars": 1287,
    "stars_checked": "2026-11-29",
    "archived": false,
    "fork": false,
    "pushed_at": "2022-12-31T11:43:00Z",
    "language": "JavaScript",
    "homepage": null,
    "license_name": null
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/evanw/webgl-water",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/evanw/webgl-water",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/evanw/webgl-water",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/evanw/webgl-water",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/evanw/webgl-water",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/evanw/webgl-water",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "evanw2015evanw"
  },
  {
   "role": "implementation",
   "key": "gh:mrdoob/three.js",
   "type": "code",
   "title": "mrdoob/three.js",
   "description": "JavaScript 3D Library.",
   "authors": [
    "mrdoob"
   ],
   "year": 2010,
   "venue": "GitHub",
   "url": "https://github.com/mrdoob/three.js",
   "ids": {
    "repo": "mrdoob/three.js"
   },
   "license": "MIT",
   "extra": {
    "stars": 119718,
    "stars_checked": "2026-11-29",
    "archived": false,
    "fork": false,
    "pushed_at": "2026-11-29T11:39:51Z",
    "language": "JavaScript",
    "homepage": "https://threejs.org/",
    "license_name": "MIT License"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mrdoob/three.js",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mrdoob/three.js",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mrdoob/three.js",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mrdoob/three.js",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mrdoob/three.js",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mrdoob/three.js",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "mrdoob2010mrdoob"
  }
 ],
 "presets": [
  {
   "id": "shallow-water-equations-live",
   "title": "Shallow water equations — interactive rippling wave interference & pool caustics",
   "runtime": "glsl-fragment",
   "params": {
    "P_WAVE_AMP": 1,
    "P_DAMPING": 0.55,
    "P_CAUSTICS": 1.15,
    "P_SPEED": 1
   },
   "basis": "original",
   "license_note": "Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).",
   "code": "const float P_WAVE_AMP = 1.0; // P Wave Height [0.2, 2.2, 0.05]\nconst float P_DAMPING = 0.55; // P Spatial Damping [0.1, 1.4, 0.04]\nconst float P_CAUSTICS = 1.15; // P Floor Caustics [0.0, 2.2, 0.05]\nconst float P_SPEED = 1.0; // P Wave Speed c [0.0, 2.5, 0.05]\n\n// Evaluate shallow-water wave height h(x,z,t) solving d2h/dt2 = c^2 * laplacian(h) - damp * dh/dt\n// Superposition of periodic droplet impulses, orbiting wake source, and basin-wall boundary reflections\nfloat wavePacket(vec2 p, vec2 src, float t, float k, float c, float damp) {\n  float r = length(p - src);\n  float phase = k * (r - c * t);\n  float env = exp(-r * damp * 1.15) / (0.35 + r * 1.1);\n  return sin(phase) * env;\n}\n\nfloat evalHeight(vec2 p, float t, float amp, float damp, float c) {\n  float h = 0.0;\n  // 4 staggered droplet impulse centers with shallow-water phase speed c = sqrt(g * H)\n  vec2 s0 = vec2(-0.52, -0.26);\n  vec2 s1 = vec2(0.56, 0.34);\n  vec2 s2 = vec2(0.18 * cos(t * 0.85), 0.32 * sin(t * 0.65)); // moving wake emitter\n  vec2 s3 = vec2(-0.28, 0.44);\n\n  h += 0.34 * wavePacket(p, s0, t, 14.0, c * 0.42, damp);\n  h += 0.30 * wavePacket(p, s1, t + 1.3, 16.5, c * 0.46, damp);\n  h += 0.38 * wavePacket(p, s2, t * 1.15, 19.0, c * 0.38, damp * 1.1);\n  h += 0.24 * wavePacket(p, s3, t + 2.7, 12.5, c * 0.44, damp);\n\n  // Mirror-image boundary reflections from square basin walls at |x|=1.4, |z|=1.4\n  h += 0.16 * wavePacket(p, vec2(2.8 - s1.x, s1.y), t + 1.3, 16.5, c * 0.46, damp);\n  h += 0.16 * wavePacket(p, vec2(-2.8 - s0.x, s0.y), t, 14.0, c * 0.42, damp);\n\n  // Circular diffraction scatter off submerged limestone pillar at (-0.12, -0.05)\n  vec2 pil = vec2(-0.12, -0.05);\n  float rp = length(p - pil);\n  h += 0.22 * sin(18.0 * (rp - c * 0.4 * t)) * exp(-abs(rp - 0.24) * 3.8);\n\n  return h * amp * 0.11;\n}\n\nvec3 evalNormal(vec2 p, float t, float amp, float damp, float c) {\n  float e = 0.012;\n  float hx = evalHeight(p + vec2(e, 0.0), t, amp, damp, c) - evalHeight(p - vec2(e, 0.0), t, amp, damp, c);\n  float hz = evalHeight(p + vec2(0.0, e), t, amp, damp, c) - evalHeight(p - vec2(0.0, e), t, amp, damp, c);\n  return normalize(vec3(-hx, 2.0 * e, -hz));\n}\n\nvec3 aces(vec3 x) {\n  x = max(x, 0.0);\n  return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);\n}\n\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;\n\n  float amp = clamp(P_WAVE_AMP, 0.1, 2.5);\n  float damp = clamp(P_DAMPING, 0.08, 1.6);\n  float caustGain = clamp(P_CAUSTICS, 0.0, 2.4);\n  float spd = max(P_SPEED, 0.0);\n  float t = iTime * spd;\n\n  // Elevated isometric-perspective view looking down into a sunlit turquoise water basin\n  vec3 ro = vec3(0.0, 2.15, 2.05);\n  vec3 ta = vec3(0.0, -0.15, 0.0);\n  vec3 ww = normalize(ta - ro);\n  vec3 uu = normalize(cross(ww, vec3(0.0, 1.0, 0.0)));\n  vec3 vv = cross(uu, ww);\n  vec3 rd = normalize(uv.x * uu + uv.y * vv + 1.55 * ww);\n\n  vec3 sunDir = normalize(vec3(0.45, 0.85, -0.35));\n\n  // Intersect water plane y = 0\n  float tWater = -ro.y / min(rd.y, -0.001);\n  vec3 pW = ro + rd * tWater;\n  vec2 wp = pW.xz;\n\n  // Check raised cylindrical stone pillar piercing the water surface at (-0.12, -0.05)\n  vec2 pilC = vec2(-0.12, -0.05);\n  float pilRad = 0.24;\n  float hW = evalHeight(wp, t, amp, damp, 1.0);\n  vec3 nW = evalNormal(wp, t, amp, damp, 1.0);\n\n  // Refract view ray into shallow water depth H = 0.52 down to tiled seabed\n  vec3 refr = refract(rd, nW, 0.75);\n  float depth = 0.52 + hW;\n  vec2 floorP = wp + refr.xz * (depth / max(-refr.y, 0.2));\n\n  // Sunlit ceramic pool tiles on basin floor\n  vec2 tileGrid = abs(fract(floorP * 4.5 - 0.5) - 0.5);\n  float grout = smoothstep(0.06, 0.015, min(tileGrid.x, tileGrid.y));\n  vec2 tid = floor(floorP * 4.5);\n  float tileVar = 0.92 + 0.08 * sin(tid.x * 7.1 + tid.y * 11.3);\n  vec3 tileCol = mix(vec3(0.14, 0.62, 0.72), vec3(0.07, 0.36, 0.48), grout) * tileVar;\n\n  // Refractive caustics from surface Laplacian curvature (focusing of parallel sun rays!)\n  float eC = 0.024;\n  float hC = evalHeight(floorP, t, amp, damp, 1.0);\n  float lap = (\n    evalHeight(floorP + vec2(eC, 0.0), t, amp, damp, 1.0) +\n    evalHeight(floorP - vec2(eC, 0.0), t, amp, damp, 1.0) +\n    evalHeight(floorP + vec2(0.0, eC), t, amp, damp, 1.0) +\n    evalHeight(floorP - vec2(0.0, eC), t, amp, damp, 1.0) - 4.0 * hC\n  ) / (eC * eC);\n  float caustic = pow(clamp(-lap * 0.065 + 0.25, 0.0, 2.2), 2.2) * caustGain;\n  // Subtle chromatic dispersion on caustic edges\n  float causticB = pow(clamp(-lap * 0.072 + 0.20, 0.0, 2.2), 2.5) * caustGain;\n\n  vec3 floorLit = tileCol * 0.82 + vec3(0.95, 1.02, 0.88) * caustic * 0.62 + vec3(0.15, 0.65, 1.0) * causticB * 0.22;\n\n  // Beer-Lambert shallow water absorption\n  vec3 absorb = exp(-vec3(0.45, 0.14, 0.09) * depth * 1.5);\n  vec3 underWater = floorLit * absorb;\n\n  // Fresnel sky reflection & crisp sun sparkle on rippling surface\n  float ndv = max(dot(nW, -rd), 0.001);\n  float fres = 0.025 + 0.975 * pow(1.0 - ndv, 5.0);\n  vec3 refl = reflect(rd, nW);\n  vec3 skyRefl = mix(vec3(0.74, 0.86, 0.96), vec3(0.18, 0.45, 0.82), clamp(refl.y, 0.0, 1.0));\n  vec3 hVec = normalize(sunDir - rd);\n  float sunSpec = pow(max(dot(nW, hVec), 0.0), 160.0) * 4.2;\n\n  vec3 col = mix(underWater, skyRefl, clamp(fres * 1.15, 0.0, 0.92)) + vec3(1.0, 0.95, 0.84) * sunSpec;\n\n  // Subtle submerged pillar occlusion + emergent limestone column cylinder\n  float dPilFloor = length(floorP - pilC);\n  col *= mix(0.45, 1.0, smoothstep(pilRad * 0.8, pilRad * 1.85, dPilFloor));\n\n  // Ray-cylinder hit for stone pillar sticking out above water (y in [-0.5, 0.32])\n  vec2 oc = ro.xz - pilC;\n  float a = dot(rd.xz, rd.xz);\n  float b = dot(oc, rd.xz);\n  float cEq = dot(oc, oc) - pilRad * pilRad;\n  float disc = b * b - a * cEq;\n  if (disc > 0.0) {\n    float tp = (-b - sqrt(disc)) / a;\n    vec3 pp = ro + rd * tp;\n    if (tp > 0.0 && tp < tWater + 0.2 && pp.y > hW - 0.02 && pp.y < 0.28) {\n      vec3 pn = normalize(vec3(pp.x - pilC.x, 0.0, pp.z - pilC.y));\n      float ndl = max(dot(pn, sunDir), 0.0);\n      vec3 stone = vec3(0.76, 0.72, 0.65) * (0.28 + 0.95 * ndl);\n      // Wet darkening waterline + foam collar\n      float wet = smoothstep(hW + 0.08, hW, pp.y);\n      stone *= mix(1.0, 0.58, wet);\n      col = mix(stone, vec3(0.92, 0.97, 1.0), smoothstep(0.025, 0.0, abs(pp.y - hW)) * 0.7);\n    }\n  }\n  // Pillar cap at y = 0.28\n  float tCap = (0.28 - ro.y) / min(rd.y, -0.001);\n  vec3 pCap = ro + rd * tCap;\n  if (tCap > 0.0 && length(pCap.xz - pilC) < pilRad) {\n    float rRing = smoothstep(pilRad - 0.03, pilRad, length(pCap.xz - pilC));\n    col = mix(vec3(0.84, 0.80, 0.73), vec3(0.62, 0.58, 0.52), rRing);\n  }\n\n  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);\n}",
   "execution_fidelity": "reduced-sandbox",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-30",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "shallow-water-equations",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T16:50:13.852Z",
     "renderer": "Chromium 145.0.7632.6 headless, WebGL2 via ANGLE/SwiftShader, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.7551,
      "std": 0.1037,
      "unique_colors": 307
     },
     "image": "/specimens/shallow-water-equations-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "fft-ocean",
  "gerstner-waves",
  "water-caustics-realtime"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [
  {
   "id": "water-caustics-realtime",
   "why": "Provides the surface normals that the caustic pass refracts."
  },
  {
   "id": "fft-ocean",
   "why": "Interactive ripples from the SWE grid are added as local displacement over the FFT ocean."
  }
 ],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/volumes/shallow-water-equations.json"
 },
 "solves": [
  "interactive ripples when objects touch water",
  "pool or river flow that reacts to the player",
  "flood or shoreline movement on a grid",
  "cheap water simulation for mobile or webgl",
  "splashes and waves on a height field"
 ],
 "authority": "Kass & Miller, 1990",
 "url": "https://texelatlas.vercel.app/t/shallow-water-equations/",
 "execution_fidelity": "reduced-sandbox",
 "fidelity_label": "Analytic Sandbox",
 "benchmark_eligible": true,
 "gpu_weight": {
  "weight_x": 35.07,
  "tier": "heavy",
  "p50_ms": 1.1539,
  "p95_ms": 1.191,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "shallow-water-equations-live",
  "execution_fidelity": "reduced-sandbox",
  "fidelity_label": "Analytic Sandbox",
  "benchmark_eligible": true,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 33.77,
   "tier": "heavy",
   "p50_ms": 0.5369,
   "p95_ms": 0.5371,
   "speedup_x": 2.15,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 24.74,
   "tier": "heavy",
   "p50_ms": 0.7124,
   "p95_ms": 0.7125,
   "speedup_x": 1.62,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}