Last lesson ended with a sobering count: no grid we can afford holds the wing’s eddies, and even Stam’s solver drains energy through the numerical viscosity of its interpolation. So our solver cannot be exact about turbulence. One thing it must get right anyway: at m/s the air is incompressible, so every grid cell must let out exactly as much air as it takes in. Break that and smoke inflates or vanishes, and a wing’s pressure field is nonsense. This lesson builds the step that enforces it, the pressure projection.
Tools: 11.4 Viscosity & the Navier–Stokes Equations, 2.11 The Laplacian & Vector Identities (Helmholtz–Hodge and Jacobi), 1.3 Linear Systems, Kernel & Column Space (null spaces) and 11.7 Potential Flow & Stream Functions.
Transcript
- Last lesson ended with a grim count: no grid we can afford holds the wing's eddies. So the solver cannot be exact about turbulence. What it must get right is incompressibility. At 50 metres per second the air cannot be squeezed, so every cell must let out exactly what it takes in. The step that enforces it is the pressure projection.
- No equation says how pressure changes in time: it is whatever keeps the divergence zero. So we split the step. Operator splitting advances each piece separately, and costs first order in time: the error is one half of B A minus A B, times delta t squared, unless the pieces commute. Stam's split: add forces, advect, diffuse, then project.
- Chorin's method predicts u star, ignoring pressure, then projects: u new is u star minus delta t over rho times the gradient of p. Take the divergence, demand zero, and the pressure Poisson equation falls out: one elliptic solve per step. In the worked example, u star is 0.3 cosine x plus sine y. The pressure is 36 sine x, its gradient is exactly the compression, and what is left is the shear, sine y. The projection removed 8.26 percent of the energy.
- Why does subtracting a gradient work? By Helmholtz and Hodge, every field splits uniquely and orthogonally into a divergence-free part plus a gradient. The projection computes the gradient part and throws it away, so it never adds energy. Equivalently, the pressure is a Lagrange multiplier, one per cell: the projected velocity is the nearest divergence-free field to u star.
- Walls. Dot the projection with the normal, and the wall's velocity condition becomes a Neumann condition on pressure. With all-Neumann boundaries, integrating gives a compatibility condition: the net inflow must be zero. Pump air into a closed box and no pressure can make it fit. And if p is a solution, so is p plus a constant: the null space. The solver sets the mean to zero.
- On a periodic grid each Fourier mode is an eigenvector of the Laplacian, so the solve is one division per mode, and exact. But exact for which divergence? Project three stirred jets four ways. Spectral, wide collocated and MAC all reach round-off, about ten to the minus fifteen. Stam's collocated compact stencil leaves 2.78 times ten to the minus two, because its Laplacian is not the divergence of its gradient.
- The collocated central difference skips the centre cell, so a checkerboard pressure has gradient zero. The projection cannot see it. On the staggered MAC grid, pressure at centres and velocities on faces, the same checkerboard has gradient 20.37. And the MAC gradient is minus the transpose of the divergence, so the Lagrange picture is exact.
- Now the real test: a uniform stream in a wind tunnel, around a cylinder, 7984 unknowns. The predictor ignores the body, so beside it the divergence is 8.000. Conjugate gradients take 319 iterations to a residual of 1.9 times ten to the minus thirteen. The speed just above the cylinder is 1.5044, against 1.4906 from channel theory and 1.4096 with no walls: the walls add 6 percent.
- Stam's solver uses Gauss-Seidel instead, and it is slow. A sweep carries news only a cell or so, and the smoothest error dies slowest, over a number of sweeps growing like N squared. After 20 sweeps the residual is 0.238, the divergence left is 12 percent, and the speed above the cylinder is 1.0425: the cylinder's influence has barely spread.
- So the projection keeps the air incompressible. Put semi-Lagrangian advection, an implicit diffusion solve and a force step in front of it, and you have Stam's complete Stable Fluids solver. Next, 11.11, we build it line by line.
Splitting the equations
The incompressible Navier–Stokes equations (11.4) are
No equation says how changes in time. The pressure has no dynamics of its own; it is whatever keeps , so a time stepper cannot march it.
Operator splitting advances each piece of a right-hand side in its own sub-step, with the method that suits it. For the exact step is (7.5 The Matrix Exponential & Fundamental Matrices) and “first , then ” is . Multiply out the series: both start , but at order the split step has where the exact one has . That is an error per step, over steps: splitting is first order unless the pieces commute. (Strang’s symmetric half-, , half- is second order; from memory.)
Stam splits each step into add forces, advect, diffuse, project. The first three make a predicted velocity with no regard for incompressibility; the last repairs it.
Chorin’s projection
Chorin’s method (1968, from memory; Temam found it independently) takes the step in two parts.
Predict. Advance everything except the pressure:
Even though , advection and forces leave .
Project. Add the pressure term on its own, , and choose so that . The divergence of both sides gives
the pressure Poisson equation: one elliptic solve per step. Adding the two parts gives a first-order time discretization of Navier–Stokes with the pressure at the new time, which is why these are called fractional-step methods. Second-order versions keep the old pressure gradient in the predictor and solve for the correction (from memory); the projection is unchanged.
Worked example: one projection by hand
On the periodic square , air ( kg/m), s, the predictor gave . Project it.
- Divergence: .
- Poisson: . Since , Pa, plus any constant.
- Correct: , so .
The compression wave is removed; the shear , already divergence-free, passes untouched. The pressure peaks at , where falls fastest along and air piles up. Energy: (the cross term integrates to zero), and the projection removes , or .
Halve and doubles to Pa for the same . A thermodynamic pressure is set by density and temperature; this one by how much compression must be removed, and how fast.
What the pressure really is
By Helmholtz–Hodge (2.11 The Laplacian & Vector Identities) every field splits uniquely and orthogonally as with . The projection is that split, with : compute the gradient part, throw it away. Orthogonality gives , so projection never adds energy.
A Lagrange multiplier. Picture the grid velocities as one long list. The projected list is the one nearest subject to one constraint per cell: its discrete divergence . Lagrange’s trick attaches a multiplier to each constraint and minimizes freely; setting the derivative with respect to each velocity to zero gives , because on the MAC grid below . So : the pressure is the multiplier enforcing incompressibility cell by cell, like the tension that holds a pendulum bob on its circle (11.3). It has no equation of state, is defined only up to a constant, and responds instantly everywhere: an incompressible fluid has an infinite speed of sound.
The pressure equation as a grid of unknowns
With the 5-point Laplacian the Poisson equation becomes one equation per cell:
with . In plain words: every unknown equals the average of its neighbours, minus a source. The cylinder sim below has such equations, each with at most five unknowns. Elimination would waste effort on a matrix that is almost all zeros, so we iterate:
- Jacobi: replace every by the right side computed from the old values (the Jacobi relaxation of 2.11 The Laplacian & Vector Identities, plus a source).
- Gauss–Seidel: the same, but each new value is used at once. Stam’s
lin_solvedoes 20 sweeps of this. - Conjugate gradients (CG): each step takes the best combination of the residuals so far; for a symmetric matrix like this it is far faster (stated, not derived). Multigrid is faster still (from memory).
Why are the first two slow? A Jacobi sweep moves information one cell; Gauss–Seidel carries it further in its sweep direction but one cell against it, weakening at each hop. The cylinder’s top is cells from the wall, and once the news arrives the smoothest error dies slowest, over a number of sweeps growing like .
Pressure at walls, and the all-Neumann problem
At a solid wall the air must not pass through: . Dot the projection with :
The wall condition on velocity becomes a Neumann condition on pressure. If the predictor already set the wall’s normal velocity (Stam does; on the MAC grid the wall face is simply not an unknown), , and the grid equation drops the missing neighbour: a cell beside a wall averages its three fluid neighbours. A prescribed inflow or outflow is Neumann too; at an outflow (Dirichlet) is a common alternative. Since is a numerical artifact rather than a law of the real pressure, the projected pressure is only first-order accurate in a thin layer at walls (from memory).
Compatibility. If every boundary is Neumann, integrate the Poisson equation and apply the divergence theorem to both sides: . With the wall condition the left side is , so a solution exists only if : the prescribed boundary velocities must add up to zero net inflow. Pump air into a closed incompressible box and no pressure can make it fit. On the grid, each interior face enters two cell equations with opposite signs, so the sum of all is the net boundary inflow.
The null space. If solves an all-Neumann problem, so does : the constant vector is in the matrix’s kernel (1.3 Linear Systems, Kernel & Column Space). Solvers pin one cell or, as the sim does, set the mean to zero.
The sim does this in a wind-tunnel channel long and high, , fluid cells around a staircase cylinder. Step 1 shows : a closed face beside one carrying gives . The net boundary inflow prints : solvable. Conjugate gradients take iterations to a relative residual of , and step 3 leaves near .
The numbers. At , just over half a radius above the cylinder, the grid speed is . Potential flow without walls gives ; with the channel walls (image doublets, our construction, checked numerically in the sim’s source), . The walls add : the blockage a wind-tunnel engineer corrects for. The remaining is the staircase, a body wrong by up to half a cell.
Stam’s 20 sweeps. With Gauss–Seidel at 20 sweeps the relative residual is , is of ‘s, and the speed above the cylinder is : the cylinder’s influence has barely spread. At 2000 sweeps: , residual . A running smoke solver removes only the divergence one step created, so 20 sweeps fare better there, but the leftover accumulates as volume loss.
The light wing. The button sets , 11.7’s light-aircraft analog. The clockwise loop integral around reads for (a vortex summed on a grid) and the same after projection, so the Kutta–Joukowski lift is kept. The air above is faster (; channel theory ), below slower (; ).
The painted in step 2 is not the pressure a wing feels. It is the pressure impulse, divided by , that would turn the bare stream into this flow in one step: high in front, where oncoming air must be stopped, equally low behind, where air must be pulled in, near zero on top. The steady surface pressure comes from Bernoulli on the projected velocity (11.3).
Periodic grids: the FFT solves it exactly
On a periodic grid each Fourier mode is an eigenvector of the discrete Laplacian: the 5-point stencil returns it multiplied by . So the FFT (11.9’s tool) turns the linear system into one division per mode: transform , divide by , transform back. It is exact and costs ; the null space is , the constant mode, and compatibility is automatic because a periodic divergence has zero mean. The price: a box with no walls and no wing.
Collocated grids and the checkerboard
The obvious grid, collocated, stores , and at cell centres with central differences, . That stencil skips the centre, so a checkerboard has central gradient zero: the projection cannot see it, nor a sawtooth velocity. Then:
- With Stam’s compact Laplacian, is a wide stencil, not the compact one: even an exact solve leaves divergence.
- With the wide Laplacian the divergence vanishes, but the grid splits into four uncoupled sub-grids, and the null space holds three checkerboards besides the constant (Problem 3).
The MAC grid (marker-and-cell; Harlow and Welch, 1965, from memory) staggers: at centres, on vertical faces, on horizontal faces. A cell’s divergence uses its own four faces, a face’s gradient its two cells, . Then is exactly the compact Laplacian, and the checkerboard has gradient . Summation by parts gives
the discrete , i.e. . This adjointness makes the Lagrange picture exact and the projection orthogonal on the grid.
Three Gaussian jets, each method judged by its own divergence (the sim and the lab below):
| method | after an exact solve | |
|---|---|---|
| spectral | about | |
| collocated, compact (Stam) | ||
| collocated, wide | about | |
| MAC | about |
The spectral result is divergence-free in its own derivative; measured with central differences (the sim’s readout), appears. “Divergence-free” always means “for this ”. Stam’s mismatch leaves after a perfect solve. Tick the sawtooth : spectral, compact and wide leave its amplitude at (their derivatives cannot see it); MAC removes it ().
Computing it
Each discretization is three Fourier symbols, , and a Laplacian, and a projection is exact for its own precisely when the Laplacian equals . The lab also prints the checkerboard’s gradient ( central, MAC) and both sides of the adjointness identity, equal to all ten decimals ().
The exercise. Build the MAC projection on a periodic grid, in physical units. The test field has a known answer: a discretely divergence-free part from a stream function at the cell corners, plus the gradient of a known . A correct projection returns the first part exactly, with pressure .
Graded: divergence at round-off, the divergence-free part returned unchanged and the pressure . The solution then projects pure noise: before, after, of the energy kept. About half is expected: each 2D Fourier mode has one direction along its wave vector (compression, removed) and one across it (swirl, kept).
Concept checks
Problems
Starting from , derive the pressure Poisson equation and the wall condition for . Then show that if is already divergence-free and satisfies the wall condition, is constant and the projection does nothing.
Show worked solution
Divergence: ; setting it to zero gives . Normal component at the wall: must equal , so . In the special case, with ; multiply by and integrate by parts: , so : is constant and .
A periodic 1D MAC grid has four cells (, and take , so ). Face velocities sit on the right face of cell : . The divergence of cell is (indices mod 4), the gradient on face is . Solve for with mean zero, project, and check the energy split. Why must the answer be a constant?
Show worked solution
Divergence: ; it sums to zero (compatibility). In 1D the only divergence-free periodic field is a constant, and the projection keeps the mean, , so we need . Then , , ; mean zero gives , so . Check cell 1: . ✓ Projected: . Energy: , with . ✓
On an periodic grid ( even), the collocated central-difference operators give the wide Laplacian . Find all in its null space, and compare with the compact Laplacian’s. What does this mean for a collocated pressure solve?
Show worked solution
Use Fourier modes: the wide stencil multiplies by , which vanishes when , i.e. (with , ). Four modes: , , , . The compact symbol vanishes only at : constants. So a collocated wide solve leaves three grid-scale oscillations undetermined; any can grow in the pressure without touching the velocity: the checkerboard. On the MAC grid has gradient , so the solve pins it.
Key Takeaways
- Incompressible pressure has no evolution equation. Split the step: predict ignoring pressure, then project; splitting is first order in unless the pieces commute.
- Chorin: with : Helmholtz–Hodge, discarding the gradient part, never adding energy.
- The pressure is a Lagrange multiplier, one per cell, scaling with , fixed up to a constant. Walls give ; an all-Neumann problem needs zero net inflow.
- Each unknown is its neighbours’ average minus a source. Twenty Gauss–Seidel sweeps leave of the cylinder’s divergence; CG reached in iterations; on a periodic grid the FFT is exact.
- “Divergence-free” means for a particular . Collocated grids hide checkerboards, and Stam’s stencil leaves after an exact solve; MAC has , = compact Laplacian, and round-off divergence.
- Projecting a uniform stream around a body gives potential flow; add a vortex and its circulation survives ( before and after): the lifting cylinder.
- Next, 11.11: the projection is the step that keeps the air incompressible; put semi-Lagrangian advection, an implicit diffusion solve and a force step in front of it, and you have Stam’s complete Stable Fluids solver, which we will build line by line.