---
id: "stable-fluids"
title: "Stable Fluids"
domain: "simulation"
stage: "simulation"
realtime: "realtime"
platforms: [webgl2, webgpu, desktop-gpu, cpu]
year_introduced: 1999
inputs: [voxels]
outputs: [volume-density, particles]
style: "both"
dynamic: "dynamic"
cost: "medium"
authority: "Stam, 1999"
url: "https://texelatlas.vercel.app/t/stable-fluids/"
sources_verified_at: "2026-11-29"
---
# Stable Fluids (Stam, 1999)

> Unconditionally stable grid solver for incompressible smoke and gas: advect semi-Lagrangian, diffuse, project to divergence-free.

## Reach for it when

- 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

## 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 it

Choose it when you want cheap, robust, art-directable 2D or 3D smoke, fire and ink on a regular grid. Prefer [FLIP/PIC fluids](https://texelatlas.vercel.app/t/flip-pic-fluids.md) when you need liquid with a free surface, [Lattice Boltzmann fluids](https://texelatlas.vercel.app/t/lattice-boltzmann-fluids.md) for easily parallel grid flow with simple boundaries, and [Smoothed Particle Hydrodynamics (fluids)](https://texelatlas.vercel.app/t/sph-fluids.md) for meshless splashing fluids.

## Cost

medium — Cheap in 2D, medium in 3D (pressure Jacobi iterations dominate).
Measured GPU Weight: 44.53× baseline (heavy) — 1.4652 ms median / 1.5321 ms p95 on Tesla T4/PCIe/SSE2 @ 1920x1080 (opengl-timer-query).
Ada L4 scaling: 39.78× baseline (heavy) — 0.6325 ms median (2.32× faster on L4).
Ampere A100 scaling: 26.9× baseline (heavy) — 0.7747 ms median (1.89× faster on A100).

## 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

## Engine support

- blender: builtin — `Physics > Fluid (Mantaflow grid/FLIP Navier-Stokes fluid solver)` (evidence: https://docs.blender.org/manual/en/5.2/physics/fluid/introduction.html, checked 2026-12-01)
- unreal: official — `Enable Niagara Fluids plugin > Grid 2D / Grid 3D Gas and Smoke Eulerian fluid solvers` (evidence: https://dev.epicgames.com/documentation/en-us/unreal-engine/niagara-fluids-reference-in-unreal-engine, checked 2026-12-01)

## Dataflow

- Inputs: voxels
- Outputs: volume-density, particles
- Platforms: webgl2, webgpu, desktop-gpu, cpu

## Presets

### Stable fluids — semi-Lagrangian divergence-free advection & vorticity dye plumes (glsl-fragment; rendered 2026-11-30)

```json
{
  "P_VISCOSITY": 0.35,
  "P_VORTICITY": 1.35,
  "P_ADVECTION": 1.1,
  "P_SPEED": 1
}
```

```glsl
const float P_VISCOSITY = 0.35; // P Viscosity [0.05, 1.2, 0.02]
const float P_VORTICITY = 1.35; // P Vorticity [0.2, 2.5, 0.05]
const float P_ADVECTION = 1.1; // P Advect Step [0.2, 2.2, 0.05]
const float P_SPEED = 1.0; // P Flow Speed [0.0, 2.5, 0.05]

// Analytical streamfunction psi(p,t) -> curl(psi) is strictly divergence-free (div u = 0)
float streamFunc(vec2 p, float t, float visc) {
  float damp = exp(-visc * 0.45);
  float psi = 0.0;
  // Multi-scale Karman vortex street & dipole eddies
  psi += 0.48 * sin(p.x * 2.1 - t * 0.65) * cos(p.y * 2.3 + t * 0.42);
  psi += 0.28 * damp * cos(p.x * 4.3 + p.y * 3.1 + t * 0.92);
  psi += 0.16 * damp * damp * sin(p.x * 7.8 - p.y * 6.4 - t * 1.35);
  // Central swirling vortex dipole
  vec2 c1 = vec2(0.28 * cos(t * 0.55), 0.24 * sin(t * 0.68));
  vec2 c2 = -c1;
  psi += 0.36 * exp(-dot(p - c1, p - c1) * 7.5);
  psi -= 0.36 * exp(-dot(p - c2, p - c2) * 7.5);
  return psi;
}

// Divergence-free velocity u = (dPsi/dy, -dPsi/dx)
vec2 evalVelocity(vec2 p, float t, float visc, float vort) {
  float e = 0.008;
  float dPsiDx = (streamFunc(p + vec2(e, 0.0), t, visc) - streamFunc(p - vec2(e, 0.0), t, visc)) / (2.0 * e);
  float dPsiDy = (streamFunc(p + vec2(0.0, e), t, visc) - streamFunc(p - vec2(0.0, e), t, visc)) / (2.0 * e);
  return vec2(dPsiDy, -dPsiDx) * vort;
}

vec3 aces(vec3 x) {
  x = max(x, 0.0);
  return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
  float t = iTime * max(P_SPEED, 0.0);
  float visc = clamp(P_VISCOSITY, 0.02, 1.5);
  float vort = clamp(P_VORTICITY, 0.1, 2.8);
  float adv = clamp(P_ADVECTION, 0.1, 2.4);

  // Semi-Lagrangian backtracing: unconditionally stable RK2 advection through 7 substeps
  vec2 pos = uv;
  float dt = 0.055 * adv;
  float curlAccum = 0.0;
  for (int i = 0; i < 7; i++) {
    float ti = t - float(i) * dt * 0.85;
    vec2 v1 = evalVelocity(pos, ti, visc, vort);
    vec2 mid = pos - 0.5 * dt * v1;
    vec2 v2 = evalVelocity(mid, ti - 0.5 * dt, visc, vort);
    pos -= dt * v2;
    curlAccum += length(v2);
  }
  curlAccum /= 7.0;

  // Source dye jets injected at advected coordinates (cyan, magenta, amber-gold)
  float band1 = exp(-pow(sin(pos.y * 3.8 + pos.x * 1.4) * 1.8, 2.0));
  float band2 = exp(-pow(cos(pos.x * 4.1 - pos.y * 1.8) * 1.9, 2.0));
  float band3 = exp(-pow(sin((pos.x + pos.y) * 5.2) * 2.1, 2.0));

  // Viscous diffusion softens filament contrast at higher P_VISCOSITY
  float crisp = mix(1.45, 0.65, clamp(visc, 0.0, 1.0));
  float d1 = pow(clamp(band1, 0.0, 1.0), crisp);
  float d2 = pow(clamp(band2, 0.0, 1.0), crisp);
  float d3 = pow(clamp(band3, 0.0, 1.0), crisp);

  vec3 col = vec3(0.018, 0.024, 0.042);
  col += vec3(0.08, 0.82, 0.96) * d1 * 1.05;
  col += vec3(0.96, 0.22, 0.58) * d2 * 0.95;
  col += vec3(1.05, 0.72, 0.18) * d3 * 0.78;

  // Specular-like vorticity highlight along high-shear ribbons
  vec2 vNow = evalVelocity(uv, t, visc, vort);
  float speedMag = length(vNow);
  float streamIso = smoothstep(0.92, 0.99, 0.5 + 0.5 * cos(streamFunc(uv, t, visc) * 32.0));
  col += vec3(0.75, 0.92, 1.0) * streamIso * 0.12 * clamp(speedMag, 0.0, 1.5);
  col *= 0.65 + 0.55 * clamp(curlAccum * 0.75, 0.0, 1.4);

  // Dark subtle frame vignette
  col *= 1.0 - 0.28 * dot(uv, uv);
  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);
}
```

Attribution: Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).

## Lineage

- extended by [MacCormack Predictor-Corrector Clamped Fluid Advection](https://texelatlas.vercel.app/t/maccormack-clamped-advection.md)

## Combination ideas

- [Chladni-vibrated iridescent soap film](https://texelatlas.vercel.app/ideas/chladni-driven-iridescent-soap-film/)
- [Storms that form and drift](https://texelatlas.vercel.app/ideas/simulated-weather-for-clouds/)

## Sources

1. [primary] Jos Stam (1999) Stable fluids. Proceedings of the 26th annual conference on Computer graphics and interactive techniques  - SIGGRAPH '99. https://doi.org/10.1145/311535.311548 (verified via crossref on 2026-11-29)
2. [tutorial] Jos Stam Real-Time Fluid Dynamics for Games. dgp.toronto.edu. https://www.dgp.toronto.edu/public_user/stam/reality/Research/pdf/GDC03.pdf (verified via http on 2026-11-29)
3. [tutorial] Mark J. Harris GPU Gems Chapter 38: Fast Fluid Dynamics Simulation on the GPU. developer.nvidia.com. https://developer.nvidia.com/gpugems/gpugems/part-vi-beyond-triangles/chapter-38-fast-fluid-dynamics-simulation-gpu (verified via http on 2026-11-29)

## Cite this entry

Texel. "Stable Fluids." https://texelatlas.vercel.app/t/stable-fluids/ (accessed 2026-12-01).
