{
 "id": "stable-fluids",
 "title": "Stable Fluids",
 "aliases": [],
 "domain": "simulation",
 "subdomain": "grid fluids",
 "stage": "simulation",
 "realtime": "realtime",
 "platforms": [
  "webgl2",
  "webgpu",
  "desktop-gpu",
  "cpu"
 ],
 "year_introduced": 1999,
 "inputs": [
  "voxels"
 ],
 "outputs": [
  "volume-density",
  "particles"
 ],
 "style": "both",
 "dynamic": "dynamic",
 "summary": "Unconditionally stable grid solver for incompressible smoke and gas: advect semi-Lagrangian, diffuse, project to divergence-free.",
 "how_it_works": "Each step applies external forces, then advects the velocity field by tracing each cell backwards through the flow and sampling the old field with interpolation (semi-Lagrangian), which is stable for any step size. Viscosity is an implicit diffusion solve. A projection step solves a Poisson equation for pressure and subtracts its gradient so that divergence is zero. Smoke density or temperature are advected as passive scalars through the same velocity field. On GPUs each stage is a fragment pass with ping-pong textures, and the Jacobi pressure solve typically takes 20-40 iterations. Vorticity confinement is commonly added to recover small swirls lost to numerical diffusion.",
 "when_to_use": "Choose it when you want cheap, robust, art-directable 2D or 3D smoke, fire and ink on a regular grid. Prefer `flip-pic-fluids` when you need liquid with a free surface, `lattice-boltzmann-fluids` for easily parallel grid flow with simple boundaries, and `sph-fluids` for meshless splashing fluids.",
 "cost": {
  "class": "medium",
  "notes": "Cheap in 2D, medium in 3D (pressure Jacobi iterations dominate)."
 },
 "failure_modes": [
  "Semi-Lagrangian advection smears detail (numerical diffusion) and kills small vortices",
  "Fixed grid: no free surface, large domains cost cubically",
  "Jacobi pressure solve with few iterations leaves residual divergence and visible compressibility",
  "Boundary conditions on moving obstacles need care or smoke leaks"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1145/311535.311548",
   "type": "paper",
   "title": "Stable fluids",
   "authors": [
    "Jos Stam"
   ],
   "year": 1999,
   "venue": "Proceedings of the 26th annual conference on Computer graphics and interactive techniques  - SIGGRAPH '99",
   "url": "https://doi.org/10.1145/311535.311548",
   "ids": {
    "doi": "10.1145/311535.311548"
   },
   "license": null,
   "extra": {
    "csl_type": "proceedings-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/311535.311548",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/311535.311548",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/311535.311548",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/311535.311548",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/311535.311548",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "stam1999stable"
  },
  {
   "role": "tutorial",
   "key": "url:https://www.dgp.toronto.edu/public_user/stam/reality/Research/pdf/GDC03.pdf",
   "type": "tutorial",
   "title": "Real-Time Fluid Dynamics for Games",
   "authors": [
    "Jos Stam"
   ],
   "year": null,
   "venue": "dgp.toronto.edu",
   "url": "https://www.dgp.toronto.edu/public_user/stam/reality/Research/pdf/GDC03.pdf",
   "ids": {},
   "license": null,
   "extra": {
    "http_status": 200,
    "live": true,
    "archive": {
     "url": "http://web.archive.org/web/20260719111721/https://www.dgp.toronto.edu/public_user/stam/reality/Research/pdf/GDC03.pdf",
     "timestamp": "20260719111721"
    },
    "page_title": null,
    "host": "dgp.toronto.edu",
    "title_confirmed_by_page": false,
    "title_unconfirmed": false
   },
   "provenance": {
    "title": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "http",
     "endpoint": "GET https://www.dgp.toronto.edu/public_user/stam/reality/Research/pdf/GDC03.pdf → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "http",
     "endpoint": "GET https://www.dgp.toronto.edu/public_user/stam/reality/Research/pdf/GDC03.pdf → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "stamfluid"
  },
  {
   "role": "tutorial",
   "key": "url:https://developer.nvidia.com/gpugems/gpugems/part-vi-beyond-triangles/chapter-38-fast-fluid-dynamics-simulation-gpu",
   "type": "tutorial",
   "title": "GPU Gems Chapter 38: Fast Fluid Dynamics Simulation on the GPU",
   "authors": [
    "Mark J. Harris"
   ],
   "year": null,
   "venue": "developer.nvidia.com",
   "url": "https://developer.nvidia.com/gpugems/gpugems/part-vi-beyond-triangles/chapter-38-fast-fluid-dynamics-simulation-gpu",
   "ids": {},
   "license": null,
   "extra": {
    "http_status": 200,
    "live": true,
    "archive": {
     "url": "http://web.archive.org/web/20261015131355/https://developer.nvidia.com/gpugems/gpugems/part-vi-beyond-triangles/chapter-38-fast-fluid-dynamics-simulation-gpu",
     "timestamp": "20261015131355"
    },
    "page_title": "Chapter 38. Fast Fluid Dynamics Simulation on the GPU",
    "host": "developer.nvidia.com",
    "title_confirmed_by_page": true,
    "title_unconfirmed": false
   },
   "provenance": {
    "title": {
     "authority": "http",
     "endpoint": "GET https://developer.nvidia.com/gpugems/gpugems/part-vi-beyond-triangles/chapter-38-fast-fluid-dynamics-simulation-gpu → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "http",
     "endpoint": "GET https://developer.nvidia.com/gpugems/gpugems/part-vi-beyond-triangles/chapter-38-fast-fluid-dynamics-simulation-gpu → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "http",
     "endpoint": "GET https://developer.nvidia.com/gpugems/gpugems/part-vi-beyond-triangles/chapter-38-fast-fluid-dynamics-simulation-gpu → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 0.95,
   "__bib": "harrisgems"
  }
 ],
 "presets": [
  {
   "id": "stable-fluids-live",
   "title": "Stable fluids — semi-Lagrangian divergence-free advection & vorticity dye plumes",
   "runtime": "glsl-fragment",
   "params": {
    "P_VISCOSITY": 0.35,
    "P_VORTICITY": 1.35,
    "P_ADVECTION": 1.1,
    "P_SPEED": 1
   },
   "basis": "original",
   "license_note": "Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).",
   "code": "const float P_VISCOSITY = 0.35; // P Viscosity [0.05, 1.2, 0.02]\nconst float P_VORTICITY = 1.35; // P Vorticity [0.2, 2.5, 0.05]\nconst float P_ADVECTION = 1.1; // P Advect Step [0.2, 2.2, 0.05]\nconst float P_SPEED = 1.0; // P Flow Speed [0.0, 2.5, 0.05]\n\n// Analytical streamfunction psi(p,t) -> curl(psi) is strictly divergence-free (div u = 0)\nfloat streamFunc(vec2 p, float t, float visc) {\n  float damp = exp(-visc * 0.45);\n  float psi = 0.0;\n  // Multi-scale Karman vortex street & dipole eddies\n  psi += 0.48 * sin(p.x * 2.1 - t * 0.65) * cos(p.y * 2.3 + t * 0.42);\n  psi += 0.28 * damp * cos(p.x * 4.3 + p.y * 3.1 + t * 0.92);\n  psi += 0.16 * damp * damp * sin(p.x * 7.8 - p.y * 6.4 - t * 1.35);\n  // Central swirling vortex dipole\n  vec2 c1 = vec2(0.28 * cos(t * 0.55), 0.24 * sin(t * 0.68));\n  vec2 c2 = -c1;\n  psi += 0.36 * exp(-dot(p - c1, p - c1) * 7.5);\n  psi -= 0.36 * exp(-dot(p - c2, p - c2) * 7.5);\n  return psi;\n}\n\n// Divergence-free velocity u = (dPsi/dy, -dPsi/dx)\nvec2 evalVelocity(vec2 p, float t, float visc, float vort) {\n  float e = 0.008;\n  float dPsiDx = (streamFunc(p + vec2(e, 0.0), t, visc) - streamFunc(p - vec2(e, 0.0), t, visc)) / (2.0 * e);\n  float dPsiDy = (streamFunc(p + vec2(0.0, e), t, visc) - streamFunc(p - vec2(0.0, e), t, visc)) / (2.0 * e);\n  return vec2(dPsiDy, -dPsiDx) * vort;\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  float t = iTime * max(P_SPEED, 0.0);\n  float visc = clamp(P_VISCOSITY, 0.02, 1.5);\n  float vort = clamp(P_VORTICITY, 0.1, 2.8);\n  float adv = clamp(P_ADVECTION, 0.1, 2.4);\n\n  // Semi-Lagrangian backtracing: unconditionally stable RK2 advection through 7 substeps\n  vec2 pos = uv;\n  float dt = 0.055 * adv;\n  float curlAccum = 0.0;\n  for (int i = 0; i < 7; i++) {\n    float ti = t - float(i) * dt * 0.85;\n    vec2 v1 = evalVelocity(pos, ti, visc, vort);\n    vec2 mid = pos - 0.5 * dt * v1;\n    vec2 v2 = evalVelocity(mid, ti - 0.5 * dt, visc, vort);\n    pos -= dt * v2;\n    curlAccum += length(v2);\n  }\n  curlAccum /= 7.0;\n\n  // Source dye jets injected at advected coordinates (cyan, magenta, amber-gold)\n  float band1 = exp(-pow(sin(pos.y * 3.8 + pos.x * 1.4) * 1.8, 2.0));\n  float band2 = exp(-pow(cos(pos.x * 4.1 - pos.y * 1.8) * 1.9, 2.0));\n  float band3 = exp(-pow(sin((pos.x + pos.y) * 5.2) * 2.1, 2.0));\n\n  // Viscous diffusion softens filament contrast at higher P_VISCOSITY\n  float crisp = mix(1.45, 0.65, clamp(visc, 0.0, 1.0));\n  float d1 = pow(clamp(band1, 0.0, 1.0), crisp);\n  float d2 = pow(clamp(band2, 0.0, 1.0), crisp);\n  float d3 = pow(clamp(band3, 0.0, 1.0), crisp);\n\n  vec3 col = vec3(0.018, 0.024, 0.042);\n  col += vec3(0.08, 0.82, 0.96) * d1 * 1.05;\n  col += vec3(0.96, 0.22, 0.58) * d2 * 0.95;\n  col += vec3(1.05, 0.72, 0.18) * d3 * 0.78;\n\n  // Specular-like vorticity highlight along high-shear ribbons\n  vec2 vNow = evalVelocity(uv, t, visc, vort);\n  float speedMag = length(vNow);\n  float streamIso = smoothstep(0.92, 0.99, 0.5 + 0.5 * cos(streamFunc(uv, t, visc) * 32.0));\n  col += vec3(0.75, 0.92, 1.0) * streamIso * 0.12 * clamp(speedMag, 0.0, 1.5);\n  col *= 0.65 + 0.55 * clamp(curlAccum * 0.75, 0.0, 1.4);\n\n  // Dark subtle frame vignette\n  col *= 1.0 - 0.28 * dot(uv, uv);\n  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);\n}",
   "execution_fidelity": "conceptual-diagram",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-30",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "stable-fluids",
     "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.7582,
      "std": 0.1662,
      "unique_colors": 1480
     },
     "image": "/specimens/stable-fluids-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "flip-pic-fluids",
  "lattice-boltzmann-fluids",
  "sph-fluids",
  "position-based-fluids"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/simulation/stable-fluids.json"
 },
 "solves": [
  "need smoke or ink that doesn't explode at large timesteps",
  "cheap 2d or 3d smoke on a grid",
  "art-directable gas and fire",
  "fluid solver must be unconditionally stable",
  "interactive swirling smoke in a game"
 ],
 "authority": "Stam, 1999",
 "url": "https://texelatlas.vercel.app/t/stable-fluids/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 44.53,
  "tier": "heavy",
  "p50_ms": 1.4652,
  "p95_ms": 1.5321,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "stable-fluids-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 39.78,
   "tier": "heavy",
   "p50_ms": 0.6325,
   "p95_ms": 0.634,
   "speedup_x": 2.32,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 26.9,
   "tier": "heavy",
   "p50_ms": 0.7747,
   "p95_ms": 0.7752,
   "speedup_x": 1.89,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}