Skip to content
Adrien Hubert

Stable fluids, with the projection on a slider.

Drag across the box to push ink around. Underneath is the solver Jos Stam published in 1999: a 128 by 128 grid of velocities, moved by tracing backwards and then corrected so that no cell gains or loses fluid. That correction is the expensive part. The slider below decides how much work it gets.

Show
pressure equation left unsolved
—
ink in the box
—
step time
—

Drag to stir. Touch works too. Space pauses.

Note

Every frame does four things to the velocity field. It adds whatever force your pointer or the jets apply. It diffuses the field if viscosity is above zero. It advects the field through itself. Then it projects the result onto the nearest field with zero divergence. The ink rides on the final velocities and does nothing back to them.

The advection step is why the paper says "stable". Instead of pushing each cell forward, which blows up once a velocity carries something further than one cell per step, it asks where the fluid now in this cell came from. It walks back along the velocity, lands between four cells, and interpolates. The result is always an average of existing values, so it can never grow. The price is numerical smoothing. Small swirls blur out faster than they would in water.

Advection on its own does not conserve volume. Some cells end up with more flow coming in than going out. The Helmholtz-Hodge decomposition says any vector field is a divergence-free part plus the gradient of a scalar, here the pressure. Find that pressure by solving a Poisson equation, subtract its gradient, and what remains is incompressible. This page solves the Poisson equation with Gauss-Seidel sweeps, the method in Stam's 2003 games paper, and the slider sets how many.

At zero sweeps the pressure stays zero and nothing is corrected. Switch to the divergence view and each jet becomes a red source wrapped in a blue sink. In the ink view the jets stall into two blobs, because the fluid they push has nowhere to go and nothing moves the surrounding fluid out of the way. With a few sweeps the plumes rise but sag. Gauss-Seidel removes local error fast and large-scale error slowly, so the first sweeps fix the small ripples and leave the slow, box-sized ones. The readout gives the root-mean-square residual of the pressure equation as a share of its right-hand side. With only the smooth jets running it sits near half at twenty sweeps, since smooth error is the kind Gauss-Seidel is slowest on. Turbulent flow drops it much lower. At 160 sweeps the step costs more than a frame. On a single test blob, outside this page, getting the residual under 0.1 percent took 10,000 sweeps. That is why production solvers use multigrid or conjugate gradients.

Vorticity confinement is a cheat from Fedkiw, Stam and Jensen in 2001. It measures where the field is spinning, then adds a small force that tightens each spin, giving back some of the detail the backwards tracing smoothed away. It adds energy that physics did not put there, and at high resolution you would turn it down. At 128 cells it is the difference between smoke and paint.

One more honest caveat. Here velocity and pressure share the same cell centres, as in Stam's games paper, so even an exact pressure solve leaves some divergence in the central-difference sense: a checkerboard pattern of pressure is invisible to that gradient. A staggered grid, with velocities on cell faces, removes it. That is why the divergence view never goes fully black.

Walls reflect: the velocity normal to an edge is mirrored to zero at the boundary. Ink is not quite conserved even with fading off, since bilinear interpolation leaks a little at every step. The "ink in the box" counter shows that drift.

Sources

  • Stam, J. (1999). Stable Fluids. Proceedings of SIGGRAPH 99, pages 121-128.
  • Stam, J. (2003). Real-Time Fluid Dynamics for Games. Proceedings of the Game Developer Conference.
  • Fedkiw, R., Stam, J., Jensen, H. W. (2001). Visual Simulation of Smoke. Proceedings of SIGGRAPH 2001, pages 15-22.
  • Bridson, R. (2015). Fluid Simulation for Computer Graphics, second edition. CRC Press.