Diffusion is only nearest-neighbor exchange
Spread a single pulse across a periodic grid with a four-neighbor Laplacian, check that the total is conserved, and measure the spread against diffusion theory.
Until now every cell has stored a discrete state: empty or active, road or car, tree or fire. This chapter changes the cell to hold a continuous quantity and the transition to local exchange. The state is a concentration, and the update is the difference between a cell and its nearest neighbors.
The program below starts with all the concentration in one cell, spreads it with a four-neighbor Laplacian, checks that the total did not change, and measures how far it traveled.
Run it
Save this as diffusion.py and run it with Python 3.11 or later. It needs
NumPy only.
import numpy as np
size = 121
grid = np.zeros((size, size), dtype=np.float64)
grid[size // 2, size // 2] = 1.0
def laplacian(grid):
return (
np.roll(grid, 1, axis=0)
+ np.roll(grid, -1, axis=0)
+ np.roll(grid, 1, axis=1)
+ np.roll(grid, -1, axis=1)
- 4 * grid
)
def diffusion_step(
grid,
rate=0.15,
):
return (
grid
+ rate * laplacian(grid)
)
def spread_radius(grid):
y, x = np.indices(grid.shape)
total = grid.sum()
cx = (x * grid).sum() / total
cy = (y * grid).sum() / total
r2 = (
(x - cx) ** 2
+ (y - cy) ** 2
)
return np.sqrt(
(r2 * grid).sum()
/ total
)
initial_total = grid.sum()
for _ in range(120):
grid = diffusion_step(grid)
final_total = grid.sum()
total_in = round(float(initial_total), 12)
total_out = round(float(final_total), 12)
print("total concentration:", total_in, "->", total_out)
print("conserved (isclose):", bool(np.isclose(initial_total, final_total)))
radius = round(float(spread_radius(grid)), 2)
theory = round((4 * 0.15 * 120) ** 0.5, 2)
print("spread radius:", radius, "theory:", theory)The program is the chapter’s own code composed into one file: the single-cell pulse, the Laplacian, the explicit diffusion step, the second-moment radius, and a print block that adds the conservation check and the theory comparison.
Expected outcome
total concentration: 1.0 -> 1.0
conserved (isclose): True
spread radius: 8.49 theory: 8.49The total is unchanged and the measured spread matches the theoretical
√(4·rate·t) for this stencil. The two numbers agreeing to two decimals is
the chapter’s test: “When a metric and a theory agree to two decimals, the
implementation has earned trust.”
What the Laplacian actually does
The Laplacian is four neighbor lookups and a subtraction. Locally it says which way a difference should relax: a cell higher than its neighbors gets a negative value, a lower one a positive value.
def laplacian(grid):
return (
np.roll(grid, 1, axis=0)
+ np.roll(grid, -1, axis=0)
+ np.roll(grid, 1, axis=1)
+ np.roll(grid, -1, axis=1)
- 4 * grid
)Each part of that expression is a modeling decision, and the chapter lists them: the center cell carries weight minus 4, each of the four axial neighbors contributes plus 1, the diagonal cells are excluded, and anything outside the grid is wrapped. There is no global smoothing.
Two consequences follow. Conservation comes from the stencil, not a check: with
periodic boundaries the Laplacian contributions sum to zero, so the update
cannot create or destroy material. The program tests the total with
np.isclose, because bit-exact equality is the wrong test.
The rate is part of the model. The explicit scheme is stable only for
rate ≤ 1/4 on this stencil; at 0.26 the pulse erupts into enormous
oscillating negative values. The rate of 0.15 used here is well inside the
stable region, so the concentration stays positive and the spread matches
theory.
Mechanism and limitations
Documented: the chapter states that with periodic boundaries the
Laplacian contributions sum to zero, so the discrete update conserves total
concentration up to floating-point error; that isclose rather than exact
equality is the correct test, verified to six decimals across stable rates;
that the four-neighbor stencil is stable only for rate ≤ 1/4, with 0.24
spreading cleanly and 0.26 erupting; and that after 120 steps at rate 0.15 the
measured radius is 8.49 against a theoretical 8.49.
Observed: the run above. The total concentration stayed at 1.0 and
np.isclose confirmed it, the measured spread radius was 8.49, and the
theoretical value for the same rate and step count was also 8.49. The pulse
spread outward from a single cell with no cell knowing where the source was.
Not shown. The chapter’s stability boundary is asserted here, not measured: the program runs only at rate 0.15, so no unstable run is shown and no claim is made about where the scheme breaks. Nothing here verifies the claim that totals match to six decimals across all stable rates. The metric is a second moment, which describes spread but not shape, so the agreement with theory confirms the scale of the diffusion and not the isotropy of the stencil. Sources, sinks, obstacles and reflective boundaries are named in the chapter and not exercised, and the chapter’s guardrail stands: this is a lattice analogy to a physical process, not a validated physical model.
Understand this example
Copy this prompt into your AI tool. No code runs here.
Apply this example
Copy this prompt into your AI tool. No code runs here.