Flow Lab

Syllabus / Module 11: Fluid Dynamics: Navier–Stokes and Beyond

Lesson 11.11

Stam's Stable Fluids, Step by Step

Assembling the full solver: add sources, diffuse (implicit), advect (semi-Lagrangian), project. Density and velocity, boundaries, vorticity confinement.

Written byClaude Opus

🌊
Why this matters for simulation: The solver from The Art of Fluid Animation, understood line by line.

By the end of this lesson you can…

  • Assemble a Stable Fluids step (add forces, diffuse, advect, project) for velocity and smoke, and explain why the order matters.
  • Show why explicit diffusion blows up once $\nu\Delta t/h^2 \gt 1/4$ and implicit diffusion never does.
  • Implement semi-Lagrangian advection, prove it creates no new extremes, and derive its numerical viscosity $f(1-f)h^2/2\Delta t$.
  • Set wall, inflow, outflow and obstacle boundaries with ghost cells.
  • Add vorticity confinement and Boussinesq-style buoyancy, and say what each corrects or simplifies.
  • Measure dissipation against resolution and pressure-solve quality, and weigh the remedies.
On this page
·
  1. The four steps
  2. Step 1: add forces
  3. Step 2: diffusion, and why it must be implicit
  4. Step 3: semi-Lagrangian advection
  5. Step 4: projection, and why the order matters
  6. Density, temperature and boundaries
  7. Vorticity confinement
  8. Buoyant smoke
  9. Computing it
  10. Concept checks
  11. Problems
  12. Key Takeaways

Last lesson ended with a promise. The projection keeps the air incompressible: whatever the other steps do, one Poisson solve removes the part of the velocity that would squeeze air into a cell or pull it out. But a projection on its own moves nothing. Put three steps in front of it (forces that push the air, diffusion that smooths it, advection that carries it) and you have a complete fluid solver: Jos Stam’s Stable Fluids (1999, from memory), the algorithm behind The Art of Fluid Animation and the one this module has been building towards. We take it apart one line at a time, in our own code, and measure what each line costs.

Tools: 11.10 The Projection Method for Incompressible Flow, 11.9 Turbulence: An Introduction (numerical viscosity), 11.6 Vorticity Dynamics & Circulation, 4.2 The Euler Family: Forward, Backward, Heun & Midpoint (backward Euler) and 4.5 Stability Regions & Stiffness.

▶ Video One time step taken apart: a force pushes smoke, diffusion smooths it in one implicit solve, every cell traces its air backwards and interpolates, the projection removes the squeeze, and a plate started from rest leaves its starting vortex behindcaptions only
Transcript
  1. Last lesson built the projection. On its own it moves nothing. Put three steps in front of it: forces that push the air, diffusion that smooths it, and advection that carries it, and you have Jos Stam's Stable Fluids, 1999, a complete fluid solver. We take it apart, and measure what each line costs.
  2. The velocity step has five lines. Add forces. Diffuse, implicitly. Project. Advect. Project again: the first spares the air a compressing field, the last cleans up what advection creates. Smoke follows the same recipe, with no projections: it is passive. And every sub-step is stable for any time step. That is the stable in the name.
  3. Diffusion first. Explicitly, each cell moves toward its neighbours by r, nu delta t over h squared. On the sim's finest grid, r is 0.2731. Take a checkerboard, the roughest field a grid holds: each step multiplies it by one minus eight r, which is minus 1.1845. It flips and grows, 2.59 times ten to the fourth in 60 steps, one second. The step limit is 0.0153 seconds. Implicitly, the factor is 0.3140: damped, whatever the step.
  4. Advection. Every cell traces its air backwards for one time step, to a departure point between four cell centres, and takes the bilinear interpolation of them: weights that are never negative and sum to one. A weighted average can never leave the range of what it averages. So no new extremes appear, at any time step. The wing preset runs at a Courant number of 3.16 without trouble.
  5. But averaging smears. Advect a sine by c cells per step: interpolation adds a numerical viscosity, f times one minus f, h squared over two delta t. At c equal 0.5, the sine decays at 2.4834 against 2.4674 predicted. A whole-cell shift, c equal 1, has none. The smearing is first order in h: the price of stability.
  6. Now skip the projection. One step leaves a divergence of 0.0980. By time 2.013 it has reached 12.4955, and the Taylor–Green vortex keeps only 57.04 percent of its energy. With the projection it keeps 0.8527. That vortex is steady only because pressure bends each parcel around its cell. Without it, parcels coast straight on, and streams collide.
  7. Numerical viscosity hits small scales hardest, draining the curls that make smoke look like smoke. Vorticity confinement spins them back up, with a force along the flow, epsilon h times N cross omega, where N points toward the nearest core. In the vortex test the peak vorticity kept is 0.7153 with no confinement, then 1.1659, 1.5953 and 2.3913 for epsilon 0.5, 1 and 2. It overshoots: a tuned correction.
  8. The projection's quality matters too. Stam's solver does 20 Gauss-Seidel sweeps from zero. In the vortex test, finer grids do worse: 0.5435 of the peak vorticity kept at 48 cells, 0.1203 at 256. Our explanation is an inference: 20 sweeps spread information about 20 cells, so on a fine grid they build less of the pressure holding the core. A warm start with over-relaxation fixes it: 0.5881 rising to 0.7928.
  9. The test that matters for aviation: a plate, 12 degrees nose up, started from rest in a wind tunnel. A vortex rolls off the trailing edge. Around the plate the circulation is minus 0.1864, clockwise, lift up. Behind it, plus 0.1802. The sum is minus 0.0062, within 4 percent. Kelvin survives even a dissipative solver.
  10. Everything here leaned on the density never changing, which made pressure an instant multiplier and the speed of sound infinite. Next, 11.12: let the density vary. The air gets a finite speed of sound, a Mach number, and shock waves: the other half of aviation.

The four steps

The unknowns are the velocity u=(u,v)\mathbf u = (u, v) and a scalar the flow carries, such as smoke density dd:

∂u∂t=−(u⋅∇)u+ν∇2u+f−1ρ∇p,∇⋅u=0,∂d∂t=−(u⋅∇)d+κ∇2d+S.\frac{\partial\mathbf u}{\partial t} = -(\mathbf u\cdot\nabla)\mathbf u + \nu\nabla^2\mathbf u + \mathbf f - \frac1\rho\nabla p, \quad \nabla\cdot\mathbf u = 0, \qquad \frac{\partial d}{\partial t} = -(\mathbf u\cdot\nabla)d + \kappa\nabla^2 d + S .

Operator splitting (11.10) gives each term its own sub-step. For the velocity:

  1. Add forces: u←u+Δt f\mathbf u \leftarrow \mathbf u + \Delta t\,\mathbf f.
  2. Diffuse: solve (I−νΔt∇2)unew=u(I - \nu\Delta t\nabla^2)\mathbf u^{\text{new}} = \mathbf u, implicitly.
  3. Project, so the air is carried by a divergence-free field.
  4. Advect: every cell traces its air backwards for Δt\Delta t and takes the value it finds there.
  5. Project again.

Smoke follows the same recipe without projections: sources, diffuse, advect with the new velocity. It is passive: it rides the flow and pushes back only through buoyancy. Each sub-step is stable for any Δt\Delta t, the “stable” in the name. Accuracy has a cost, measured below.

◆ Interactive A real-time smoke sandbox: drag to push air and inject hot smoke; switch each step off; choose the resolution, viscosity, diffusion and confinement

Loading simulation…

Step 1: add forces

Forward Euler, u←u+Δt f\mathbf u \leftarrow \mathbf u + \Delta t\,\mathbf f, is safe here because these forces either ignore u\mathbf u (the mouse, buoyancy) or are kept small on purpose (confinement). The mouse adds a Gaussian puff of velocity, smoke and heat.

Step 2: diffusion, and why it must be implicit

On 11.10’s 5-point grid the explicit diffusion step is

uC←uC+r (uE+uW+uN+uS−4uC),r=νΔth2.u_C \leftarrow u_C + r\,(u_E + u_W + u_N + u_S - 4u_C), \qquad r = \frac{\nu\Delta t}{h^2} .

Try the worst field a grid holds, the checkerboard u=A(−1)i+ju = A(-1)^{i+j}. All four neighbours carry −uC-u_C, so the bracket is −8uC-8u_C and each step multiplies the amplitude by 1−8r1 - 8r. For r>1/4r \gt 1/4 that is below −1-1: the checkerboard flips and grows. Explicit diffusion needs Δt≤h2/4ν\Delta t \le h^2/4\nu, and the h2h^2 hurts: halve the cells, quarter the step. On the sim’s 256×128256\times128 grid (h=1/128h = 1/128 m) with Δt=1/60\Delta t = 1/60 s and ν=10−3\nu = 10^{-3} m2^2/s, r=0.2731r = 0.2731, the limit is 0.01530.0153 s, and the factor −1.1845-1.1845 turns checkerboard noise into 2.59×1042.59\times10^4 times itself in one second (lab block A).

The implicit (backward Euler) step takes the neighbours at the new time:

(1+4r) uCnew−r (uEnew+uWnew+uNnew+uSnew)=uC.(1 + 4r)\,u_C^{\text{new}} - r\,(u_E^{\text{new}} + u_W^{\text{new}} + u_N^{\text{new}} + u_S^{\text{new}}) = u_C .

For the checkerboard the left side is (1+8r)Anew(1 + 8r)A^{\text{new}}: factor 1/(1+8r)=0.31401/(1 + 8r) = 0.3140, damped as diffusion should be. Every grid Fourier mode gets 1/(1+rμ)1/(1 + r\mu) with 0≤μ≤80 \le \mu \le 8 (11.10’s Laplacian eigenvalues times −h2-h^2), inside (0,1](0, 1] for every rr: unconditionally stable. This is stiffness (4.5 Stability Regions & Stiffness): the grid’s fastest mode decays at rate 8ν/h28\nu/h^2, far faster than anything we watch, and only an implicit method steps over it.

The price is one easy linear solve. Each row says ”uCnewu_C^{\text{new}} is a weighted average of its old value and its four new neighbours”. The diagonal 1+4r1 + 4r beats the off-diagonal sum 4r4r, so Gauss–Seidel converges for every rr (diagonal dominance, from memory). The sim does 20 sweeps, as Stam does.

Step 3: semi-Lagrangian advection

Advection says a parcel keeps its value as it moves: the material derivative Dq/DtDq/Dt is zero (11.1 Continuum Hypothesis & Flow Kinematics). So the new value at a cell centre x\mathbf x is the old value at the parcel’s departure point, traced backwards:

xd=x−Δt u(x)(Euler),xd=x−Δt u(x−12Δt u(x))(midpoint, RK2),\mathbf x_d = \mathbf x - \Delta t\,\mathbf u(\mathbf x) \quad\text{(Euler)}, \qquad \mathbf x_d = \mathbf x - \Delta t\,\mathbf u\big(\mathbf x - \tfrac12\Delta t\,\mathbf u(\mathbf x)\big) \quad\text{(midpoint, RK2)},

then the old field is interpolated there, bilinearly from the four surrounding centres: the semi-Lagrangian method. The sim traces with RK2.

Stable for any Δt\Delta t. Bilinear interpolation returns w1q1+w2q2+w3q3+w4q4w_1q_1 + w_2q_2 + w_3q_3 + w_4q_4 with wk≥0w_k \ge 0 summing to 11: a weighted average, which can never leave the range of what it averages:

min⁡oldq  ≤  qnew  ≤  max⁡oldq,\min_{\text{old}} q \;\le\; q^{\text{new}} \;\le\; \max_{\text{old}} q ,

whatever the velocity and however far back the trace reaches: a maximum principle, and a one-line stability proof. An explicit finite-difference advection step needs the Courant number C=∣u∣Δt/h≤1C = |u|\Delta t/h \le 1 (the CFL condition); the sim’s wing preset reads a peak Courant number of 3.163.16 at t=0.50t = 0.50 s without trouble.

The cost: numerical viscosity. Advect a 1D periodic field by c=uΔt/hc = u\Delta t/h cells per step, c=n+fc = n + f with nn whole and 0≤f<10 \le f \lt 1. The departure point lies fhfh beyond cell i−ni - n and (1−f)h(1-f)h short of cell i−n−1i - n - 1, so qinew=(1−f) qi−n+f qi−n−1q_i^{\text{new}} = (1 - f)\,q_{i-n} + f\,q_{i-n-1}. The whole shift nn is exact. Taylor-expand about the departure point xdx_d:

(1−f) q(xd+fh)+f q(xd−(1−f)h)=q(xd)+0⋅q′(xd)+12f(1−f)h2 q′′(xd)+…(1 - f)\,q(x_d + fh) + f\,q(x_d - (1 - f)h) = q(x_d) + 0\cdot q'(x_d) + \tfrac12 f(1 - f)h^2\,q''(x_d) + \dots

The first derivatives cancel; what is left is the exact shift plus a diffusion step with

νnum=f(1−f) h22Δt.\nu_{\text{num}} = \frac{f(1 - f)\,h^2}{2\Delta t} .

For c<1c \lt 1, f=Cf = C and Δt=Ch/u\Delta t = Ch/u give νnum=uh(1−C)/2\nu_{\text{num}} = uh(1 - C)/2: 11.9’s formula, whose sine at N=64N = 64, C=0.5C = 0.5 decayed at 2.48342.4834 against νnumk2=2.4674\nu_{\text{num}}k^2 = 2.4674. The general form says more. A whole-cell shift (c=1c = 1) has no dissipation. At c=2.3c = 2.3 the exercise measures 0.45210.4521 against 0.45060.4506: bigger steps interpolate fewer times per second, so in 1D they smear less. The grid Reynolds number of a unit box, u/νnum=2NC/f(1−f)u/\nu_{\text{num}} = 2NC/f(1-f), is 11.9’s 2N/(1−C)2N/(1 - C) when c<1c \lt 1.

In 2D, the Taylor–Green vortex u=sin⁡xcos⁡yu = \sin x\cos y, v=−cos⁡xsin⁡yv = -\cos x\sin y on a periodic 2π2\pi box is a steady Euler solution, so with ν=0\nu = 0 every loss is numerical. Lab block B (semi-Lagrangian plus exact FFT projection, peak Courant number 0.50.5, to about t=2t = 2):

NNenergy keptνeff\nu_{\text{eff}}2π/νeff2\pi/\nu_{\text{eff}}2N/(1−C)2N/(1-C)
320.74070.74070.038230.03823164164128128
640.85270.85270.019800.01980317317256256
1280.92290.92290.010090.01009623623512512

The energy of this flow decays like e−4νte^{-4\nu t} (each component has k2=2k^2 = 2), which defines νeff\nu_{\text{eff}}. It halves with hh: first order, as νnum∝h\nu_{\text{num}} \propto h says. The effective Reynolds number beats 11.9’s estimate, which assumed every parcel moved at peak speed; most move slower, and νnum∝u\nu_{\text{num}} \propto u.

Step 4: projection, and why the order matters

The projection is 11.10’s. Stam projects twice: before advecting, so the air is never carried by a compressing field; and at the end, because a velocity field carried by itself does not stay divergence-free: ∇⋅[(u⋅∇)u]\nabla\cdot[(\mathbf u\cdot\nabla)\mathbf u] is not zero in general even when ∇⋅u=0\nabla\cdot\mathbf u = 0, and balancing it is the pressure’s job.

Skip it and the flow changes. Taylor–Green is steady only because a pressure gradient bends each parcel around its cell; without it parcels coast straight on and streams collide at the stagnation points. Lab block C, N=64N = 64, no projection: one step leaves max⁡∣∇⋅u∣=0.0980\max|\nabla\cdot\mathbf u| = 0.0980; by t=2.013t = 2.013 it is 12.495512.4955, with 57.04%57.04\% of the energy left. In the sim’s vortex test, untick project and press reset: after 22 s the rms divergence reads 0.92120.9212 s−1^{-1} instead of 0.00110.0011, and 0.36410.3641 of the peak vorticity remains.

The projection’s quality matters too. Stam’s 20 Gauss–Seidel sweeps from p=0p = 0 spread information about 20 cells (11.10). The sim’s vortex decay test puts a Gaussian vortex (circulation 0.250.25 m2^2/s, core 0.10.1 m) in the closed box: axisymmetric, so steady for Euler apart from the walls’ small effect. With ν=0\nu = 0, after 22 s (the sim pauses there), the fraction of peak vorticity kept is

cells across486496128192256
Stam: 20 sweeps from zero0.54350.54350.53150.53150.40290.40290.27180.27180.15010.15010.12030.1203
SOR: 20 sweeps, warm start0.58810.58810.64680.64680.71530.71530.74990.74990.78040.78040.79280.7928

With Stam’s solve, finer grids do worse. The core spans more cells as hh shrinks, 20 sweeps build less of the low pressure that holds it in circular paths, and the underheld core flings outward (an inference that fits the trend and the leftover rms divergence, 0.05830.0583 at 96 cells against 0.00110.0011). The default fixes it: a warm start begins from the previous step’s pressure, so sweeps only fix the change, and over-relaxation (SOR) pushes each update further, by ω=1.8\omega = 1.8, picked by trial.

Density, temperature and boundaries

The sim carries smoke density dd and temperature TT, each with the scalar recipe and never projected. Stam’s collocated grid puts every unknown at a cell centre, ringed by ghost cells that each sub-step refills:

  • Wall: normal velocity copied with its sign flipped, so it averages to zero on the wall; tangential velocity and scalars copied (free slip, no flux).
  • Inflow (the tunnel’s left): u=Uu = U, v=0v = 0, dye streaks from a rake.
  • Outflow (right): everything copied, and p=0p = 0, which also fixes 11.10’s constant null space.
  • Obstacles: solid cells, a staircase. Velocity is zero there and the pressure equation drops solid neighbours (11.10’s Neumann rows).

As 11.10 showed, a collocated projection is not exact for the divergence the readout measures, and a staircase adds error. In the wing preset at 96 cells the rms divergence after projection reads 0.7110.711 s−1^{-1} at t=0.5t = 0.5 s, largest next to the plate. Production solvers use the staggered MAC grid and cut cells.

Vorticity confinement

Numerical viscosity hits small scales hardest, so it drains the tight curls that make smoke look like smoke. Fedkiw, Stam and Jensen (2001, from memory), previewed in 11.6 Vorticity Dynamics & Circulation, spin them back up. With ω=∂v/∂x−∂u/∂y\omega = \partial v/\partial x - \partial u/\partial y,

N=∇∣ω∣∣∇∣ω∣∣,fconf=ε h (N×ωz^)=ε h (Ny ω, −Nx ω).\mathbf N = \frac{\nabla|\omega|}{|\nabla|\omega||}, \qquad \mathbf f_{\text{conf}} = \varepsilon\,h\,(\mathbf N\times\omega\hat{\mathbf z}) = \varepsilon\,h\,(N_y\,\omega,\ -N_x\,\omega) .

N\mathbf N points towards the nearest core. On the right flank of a counterclockwise vortex N=(−1,0)\mathbf N = (-1, 0) and the force is εh(0,ω)\varepsilon h(0, \omega): upward, along the flow, adding spin where vorticity concentrates. The factor hh makes the force vanish under refinement, as the dissipation it corrects does; ε\varepsilon carries units of s−1^{-1}. In the vortex test at 96 cells:

ε\varepsilon00.512
peak vorticity kept0.71530.71531.16591.16591.59531.59532.39132.3913
energy kept0.80280.80280.83710.83710.87160.87160.93910.9391

At ε=0.5\varepsilon = 0.5 the peak overshoots its start by 17%17\% while 16%16\% of the energy is still gone. Confinement is a tuned correction, sized by eye, not physics.

Buoyant smoke

The Boussinesq approximation (from memory) keeps density constant except in the gravity term, where a small change ρ′\rho' gives a force per unit mass −gρ′/ρ0-g\rho'/\rho_0. For an ideal gas at constant pressure ρ′/ρ0≈−(T−T0)/T0\rho'/\rho_0 \approx -(T - T_0)/T_0, so air 3030 K above a 300300 K room feels 0.980.98 m/s2^2 upward; smoke particles add weight. The sim uses fbuoy=(β(T−T0)−αd)y^\mathbf f_{\text{buoy}} = \big(\beta(T - T_0) - \alpha d\big)\hat{\mathbf y} with T0=0T_0 = 0 and knobs β\beta, α\alpha instead of g/T0g/T_0. Deliberate simplifications: the smoke never cools, and density changes appear nowhere else.

◆ Interactive The wing preset: a plate started from rest sheds its starting vortex, and the two loop circulations cancel

Loading simulation…

Computing it

🐍 PythonLab: explicit vs implicit diffusion (A), Taylor–Green energy loss against resolution (B), the same flow without projection (C)
⌘/Ctrl + Enter

The exercise. Write the 1D semi-Lagrangian step on a periodic grid for a constant shift of cc cells per step, any real cc. The tests check what we derived: whole-cell shifts are exact, no new extremes appear, and a sine at c=2.3c = 2.3 decays at νnumk2\nu_{\text{num}}k^2 within 2%2\%.

🧪 Coding exerciseExercise: semi-Lagrangian advection on a periodic grid: exact shifts, the maximum principle, numerical viscosity
⌘/Ctrl + Enter

The solution reproduces 11.9’s 2.48342.4834 against 2.46742.4674 at c=0.5c = 0.5, zero at c=1c = 1, and 0.45210.4521 against 0.45060.4506 at c=2.3c = 2.3; the small excess is the Taylor series’ next term. BFECC decays at 0.070450.07045, but the square wave shows the price: plain advection stays within 0.04640.0464 to 0.95360.9536, BFECC reaches −0.0516-0.0516 to 1.05161.0516.

Concept checks

✅ Concept check

On the sim's 256 by 128 grid (h = 1/128 m) with $\nu = 10^{-3}$ m$^2$/s, what is the largest time step, in seconds, for which explicit diffusion is stable?

✅ Concept check

Why can semi-Lagrangian advection with linear interpolation never blow up, at any time step?

✅ Concept check

A 1D periodic field is advected by exactly 2 cells per step with linear interpolation. What is its numerical viscosity?

✅ Concept check

In the sim's vortex test with Stam's 20 Gauss–Seidel sweeps from zero, the peak vorticity kept after 2 s falls from 0.5435 at 48 cells to 0.1203 at 256. Why does refining make it worse?

Problems

✏️ Pen & paperImplicit diffusion of one Fourier mode●○○

On a periodic 1D grid, implicit diffusion reads (1+2r)uinew−r(ui+1new+ui−1new)=ui(1 + 2r)u_i^{\text{new}} - r(u_{i+1}^{\text{new}} + u_{i-1}^{\text{new}}) = u_i. Put ui=Aeikxiu_i = Ae^{ikx_i}, find the amplification factor, and compare with the explicit one.

Show worked solution

ui+1+ui−1=2cos⁡(kh) uiu_{i+1} + u_{i-1} = 2\cos(kh)\,u_i, so Anew(1+2r−2rcos⁡kh)=AA^{\text{new}}(1 + 2r - 2r\cos kh) = A. With 1−cos⁡kh=2sin⁡2(kh/2)1 - \cos kh = 2\sin^2(kh/2), Gimp=1/(1+4rsin⁡2(kh/2))G_{\text{imp}} = 1/(1 + 4r\sin^2(kh/2)), in (0,1](0, 1] for every rr. Explicit: Gexp=1−4rsin⁡2(kh/2)G_{\text{exp}} = 1 - 4r\sin^2(kh/2), below −1-1 at kh=πkh = \pi once r>1/2r \gt 1/2 (in 2D, 1/41/4). For small khkh both are 1−νk2Δt1 - \nu k^2\Delta t, the start of the exact e−νk2Δte^{-\nu k^2\Delta t}.

✏️ Pen & paperNumerical viscosity for a long step●●○

Smoke moves at 11 m/s on the sim’s default grid, h=1/96h = 1/96 m, with Δt=1/60\Delta t = 1/60 s. Find cc, ff, νnum\nu_{\text{num}}, the grid Reynolds number of a 11 m box, and the decay rate of a wave 0.10.1 m long.

Show worked solution

c=96/60=1.6c = 96/60 = 1.6, so f=0.6f = 0.6 and f(1−f)=0.24f(1 - f) = 0.24. Then νnum=0.24h2/2Δt=0.24×60/(2×962)=7.81×10−4\nu_{\text{num}} = 0.24h^2/2\Delta t = 0.24\times60/(2\times96^2) = 7.81\times10^{-4} m2^2/s, about 5353 times air’s 1.4607×10−51.4607\times10^{-5} (11.5), and Re=1/νnum=1280\mathrm{Re} = 1/\nu_{\text{num}} = 1280. With k=2π/0.1=62.83k = 2\pi/0.1 = 62.83 m−1^{-1} the decay rate is νnumk2=3.08\nu_{\text{num}}k^2 = 3.08 s−1^{-1}, an e-folding time of 0.320.32 s; real air would keep the wave about 5353 times longer.

✏️ Pen & paperThe order of the sub-steps●●●

Reorder the velocity step as advect, project, add forces, with no second projection, where buoyancy adds Δt βTy^\Delta t\,\beta T\hat{\mathbf y} for a hot blob TT. Is the field handed to the next advection divergence-free? What goes wrong, and how does Stam’s order avoid it?

Show worked solution

The force has divergence β ∂T/∂y\beta\,\partial T/\partial y, non-zero above and below the blob, so the next advection traces departure points with a compressing field: smoke bunches into sinks before any projection acts. In Stam’s order a projection follows the forces and precedes advection, so the advecting field is divergence-free to solver tolerance, and the final projection removes advection’s own divergence. Lab block C shows the extreme, max⁡∣∇⋅u∣=12.4955\max|\nabla\cdot\mathbf u| = 12.4955 by t=2.013t = 2.013 with no projection.

Key Takeaways

  • Stable Fluids splits a step into add forces, diffuse, project, advect, project; smoke follows without projection. Every piece is stable for any time step.
  • Explicit diffusion multiplies the checkerboard by 1−8r1 - 8r and needs Δt≤h2/4ν\Delta t \le h^2/4\nu; implicit diffusion multiplies every mode by 1/(1+rμ)1/(1 + r\mu), between 00 and 11, for one easy Gauss–Seidel solve.
  • Semi-Lagrangian advection traces back and interpolates; averaging creates no new extremes at any Courant number, but acts as a numerical viscosity f(1−f)h2/2Δtf(1-f)h^2/2\Delta t, first order in hh.
  • Skipping the projection, or doing it badly, changes the flow: with Stam’s cold 20 sweeps finer grids keep less vorticity, until a warm-started SOR solve restores the right trend.
  • Confinement εh(N×ω)\varepsilon h(\mathbf N\times\omega) restores spin but overshoots; BFECC sharpens but overshoots; buoyancy is a Boussinesq-style simplification.
  • A plate started from rest sheds a starting vortex whose circulation cancels the plate’s to within 4%4\%: Kelvin survives a dissipative solver.
  • Next, 11.12: everything here leaned on the air’s density never changing, which made the pressure an instant multiplier and the speed of sound infinite. Let the density vary and the air gets a finite speed of sound, a Mach number and shock waves: the other half of aviation.
Finished this lesson?

Mark it complete to track your progress (saved in this browser). Reading every section does this for you.

Where this leads

These later lessons build directly on this one: