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.
Transcript
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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 and a scalar the flow carries, such as smoke density :
Operator splitting (11.10) gives each term its own sub-step. For the velocity:
- Add forces: .
- Diffuse: solve , implicitly.
- Project, so the air is carried by a divergence-free field.
- Advect: every cell traces its air backwards for and takes the value it finds there.
- 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 , the “stable” in the name. Accuracy has a cost, measured below.
Step 1: add forces
Forward Euler, , is safe here because these forces either ignore (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
Try the worst field a grid holds, the checkerboard . All four neighbours carry , so the bracket is and each step multiplies the amplitude by . For that is below : the checkerboard flips and grows. Explicit diffusion needs , and the hurts: halve the cells, quarter the step. On the sim’s grid ( m) with s and m/s, , the limit is s, and the factor turns checkerboard noise into times itself in one second (lab block A).
The implicit (backward Euler) step takes the neighbours at the new time:
For the checkerboard the left side is : factor , damped as diffusion should be. Every grid Fourier mode gets with (11.10’s Laplacian eigenvalues times ), inside for every : unconditionally stable. This is stiffness (4.5 Stability Regions & Stiffness): the grid’s fastest mode decays at rate , far faster than anything we watch, and only an implicit method steps over it.
The price is one easy linear solve. Each row says ” is a weighted average of its old value and its four new neighbours”. The diagonal beats the off-diagonal sum , so Gauss–Seidel converges for every (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 is zero (11.1 Continuum Hypothesis & Flow Kinematics). So the new value at a cell centre is the old value at the parcel’s departure point, traced backwards:
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 . Bilinear interpolation returns with summing to : a weighted average, which can never leave the range of what it averages:
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 (the CFL condition); the sim’s wing preset reads a peak Courant number of at s without trouble.
The cost: numerical viscosity. Advect a 1D periodic field by cells per step, with whole and . The departure point lies beyond cell and short of cell , so . The whole shift is exact. Taylor-expand about the departure point :
The first derivatives cancel; what is left is the exact shift plus a diffusion step with
For , and give : 11.9’s formula, whose sine at , decayed at against . The general form says more. A whole-cell shift () has no dissipation. At the exercise measures against : bigger steps interpolate fewer times per second, so in 1D they smear less. The grid Reynolds number of a unit box, , is 11.9’s when .
In 2D, the Taylor–Green vortex , on a periodic box is a steady Euler solution, so with every loss is numerical. Lab block B (semi-Lagrangian plus exact FFT projection, peak Courant number , to about ):
| energy kept | ||||
|---|---|---|---|---|
| 32 | ||||
| 64 | ||||
| 128 |
The energy of this flow decays like (each component has ), which defines . It halves with : first order, as says. The effective Reynolds number beats 11.9’s estimate, which assumed every parcel moved at peak speed; most move slower, and .
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: is not zero in general even when , 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, , no projection: one step leaves ; by it is , with of the energy left. In the sim’s vortex test, untick project and press reset: after s the rms divergence reads s instead of , and of the peak vorticity remains.
The projection’s quality matters too. Stam’s 20 Gauss–Seidel sweeps from spread information about 20 cells (11.10). The sim’s vortex decay test puts a Gaussian vortex (circulation m/s, core m) in the closed box: axisymmetric, so steady for Euler apart from the walls’ small effect. With , after s (the sim pauses there), the fraction of peak vorticity kept is
| cells across | 48 | 64 | 96 | 128 | 192 | 256 |
|---|---|---|---|---|---|---|
| Stam: 20 sweeps from zero | ||||||
| SOR: 20 sweeps, warm start |
With Stam’s solve, finer grids do worse. The core spans more cells as 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, at 96 cells against ). 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 , picked by trial.
Density, temperature and boundaries
The sim carries smoke density and temperature , 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): , , dye streaks from a rake.
- Outflow (right): everything copied, and , 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 s at 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 ,
points towards the nearest core. On the right flank of a counterclockwise vortex and the force is : upward, along the flow, adding spin where vorticity concentrates. The factor makes the force vanish under refinement, as the dissipation it corrects does; carries units of s. In the vortex test at 96 cells:
| 0 | 0.5 | 1 | 2 | |
|---|---|---|---|---|
| peak vorticity kept | ||||
| energy kept |
At the peak overshoots its start by while 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 gives a force per unit mass . For an ideal gas at constant pressure , so air K above a K room feels m/s upward; smoke particles add weight. The sim uses with and knobs , instead of . Deliberate simplifications: the smoke never cools, and density changes appear nowhere else.
Computing it
The exercise. Write the 1D semi-Lagrangian step on a periodic grid for a constant shift of cells per step, any real . The tests check what we derived: whole-cell shifts are exact, no new extremes appear, and a sine at decays at within .
The solution reproduces 11.9’s against at , zero at , and against at ; the small excess is the Taylor series’ next term. BFECC decays at , but the square wave shows the price: plain advection stays within to , BFECC reaches to .
Concept checks
Problems
On a periodic 1D grid, implicit diffusion reads . Put , find the amplification factor, and compare with the explicit one.
Show worked solution
, so . With , , in for every . Explicit: , below at once (in 2D, ). For small both are , the start of the exact .
Smoke moves at m/s on the sim’s default grid, m, with s. Find , , , the grid Reynolds number of a m box, and the decay rate of a wave m long.
Show worked solution
, so and . Then m/s, about times air’s (11.5), and . With m the decay rate is s, an e-folding time of s; real air would keep the wave about times longer.
Reorder the velocity step as advect, project, add forces, with no second projection, where buoyancy adds for a hot blob . 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 , 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, by 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 and needs ; implicit diffusion multiplies every mode by , between and , 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 , first order in .
- 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 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 : 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.