Smoke in a simulation is a number in every cell of a grid, carried along by a velocity field. Two things must go right: the velocity must not create or destroy fluid, and the smoke must be moved without blowing up. This lesson pairs with Coding Adventure: Simulating Smoke by Sebastian Lague and 17 - How to write an Eulerian fluid simulator with 200 lines of code. by Ten Minute Physics, linked under Watch & read. The videos cover the equations and a working simulator; here you build the projection step, the advection step and a buoyancy force in NumPy, and test them against an independent solve.
Use a staggered grid. The grid has ny rows and nx columns of cells with side dx, and row 0 is the bottom. The horizontal velocity u lives on the vertical faces, so it has shape (ny, nx + 1). The vertical velocity v lives on the horizontal faces, with shape (ny + 1, nx). Smoke lives at the cell centres. With this layout the net outflow of a cell is exact, with no averaging: (u right - u left + v top - v bottom) / dx. That number is the divergence. Fluid that cannot be squeezed needs it to be zero in every fluid cell.
Forces and advection leave a little divergence behind, so each frame ends with a projection that removes it. Gauss-Seidel does this one cell at a time. Take a fluid cell with divergence d and share d equally among its faces: the right and top faces lose d / n, the left and bottom faces gain d / n. The cell is now clean, but its neighbours, which own the same faces, are slightly off. Sweep every cell, then sweep again, and the errors shrink.
The number n is the number of open faces. A face is open when the cell on its other side exists and is not solid. A face against an obstacle or the edge of the grid is a wall: its velocity is never changed, and it is not counted. Beside a wall n is 3, 2 or 1, and only in the middle of open fluid is it 4.
Here is a row of three cells with closed ends. The four u faces are [0, 0, 3, 0], so cell 1 has divergence 3 and cell 2 has divergence -3. The run below applies the update to each cell in order, twice.
import numpy as np
u = np.array([0.0, 0.0, 3.0, 0.0])
for sweep in (1, 2):
for c in range(3):
d = u[c + 1] - u[c]
open_faces = [f for f, ok in ((c, c > 0), (c + 1, c < 2)) if ok]
for f in open_faces:
u[f] += d / len(open_faces) if f == c else -d / len(open_faces)
print(sweep, u.tolist(), (u[1:] - u[:-1]).tolist())After the first sweep the divergences are [1.5, -1.5, 0]: the visit to cell 2 repaired it and disturbed cell 1. The second sweep removes the rest. Real grids need many sweeps, but the pattern is the same. Over-relaxation multiplies d by a number between 1 and 2 before sharing it, which often converges in fewer sweeps.
Advection moves the smoke. Pushing each value forward leaves holes and overlaps, so do the opposite: for every cell, ask where the smoke now here came from. Read the velocity at the cell centre by interpolating u and v, step backward by dt times that velocity, and interpolate the old smoke at that point. Positions between cells are blended bilinearly and clamped at the grid edge, so smoke never leaves the range of values it already had. The cost is that sharp edges get blurred a little at every step.
import numpy as np
s = np.array([0.0, 0.0, 10.0, 0.0, 0.0, 0.0])
i = np.arange(6)
print("backward", np.interp(i - 0.5, i, s).tolist())
print("forward ", np.interp(i + 0.5, i, s).tolist())The wind carries half a cell per step to the right. Tracing backward spreads the lump over cells 2 and 3, which is right. Tracing forward spreads it over cells 1 and 2, which moves it against the wind.
Buoyancy is the third piece. A v face between two cell rows gains an upward push of dt times a strength times the mean smoke of the two cells. A full frame is buoyancy, projection, advection, in that order, so the pushed field is cleaned before it carries anything.
Two mistakes are common. The first is dividing by 4 everywhere. It looks natural because most cells have four faces, but a cell beside a wall can change only three, so it keeps a quarter of its divergence on every visit and converges slowly. The second is the wrong direction of the trace, which makes the smoke travel against the flow. A third trap sits in the offsets: u and v sit half a cell away from the cell centre, so index coordinates are shifted by 0.5 in one direction for each.
The tests check the projection against a direct solve of the pressure equations on a small grid, which uses a different algorithm and must agree once Gauss-Seidel has converged. They also check that a uniform stream is unchanged, that wall faces never move, and that a uniform wind shifts a field by whole cells. What this does not show is that the result looks like real smoke: a grid this coarse, with blurring advection, only agrees with the equations in the limit of small cells and time steps.