Skip to content
Fluids
Back

Stable fluid, solved every frame

A semi-Lagrangian solver that keeps the velocity field incompressible.

How it works

This solves the Navier–Stokes equations, the same ones behind weather forecasts and aircraft design. The canvas holds two invisible grids: one of velocity, saying which way the fluid is moving at every point, and one of dye, which contributes nothing to the physics and exists only so that the motion is visible. Dragging injects a push into the first and a splash into the second.

Each frame the fluid is carried along by itself. To work out what a point holds now, the solver traces backwards along the velocity to where that stuff must have come from a moment ago, and samples there. Working backwards rather than forwards is what keeps it stable, because nothing can accumulate in a place that was never traced from.

Then comes the step that makes it read as liquid rather than smoke. Real fluid cannot be compressed, so anywhere the flow is piling up has to be cancelled out: the solver measures that pile-up, solves for a pressure that exactly opposes it over twenty passes, and subtracts it back out. All of it runs as one small program per pixel on the graphics card, thousands at a time.

All experiments →