This is the Bak–Tang–Wiesenfeld (BTW) sandpile automaton, the canonical toy model of self-organized criticality. Each cell (i,j) on the grid holds an integer grain count z(i,j). Grains are dropped one at a time; whenever a cell's count reaches the critical threshold Zc it topples:
if z(i,j) >= Zc:
z(i,j) -= Zc
z(i±1,j) += 1 (each in-bounds neighbor)
z(i,j±1) += 1
A single toppling can push a neighbor over threshold too, so one dropped grain can trigger a cascade — an avalanche. Grains that topple off the open boundary are lost, which is what keeps the pile statistically stationary instead of growing forever. Run it long enough and the pile self-tunes toward the critical slope: avalanche sizes stop clustering around one scale and instead follow a roughly power-law distribution P(s) ~ s^(−τ) — visible on the log-log histogram as a straight-ish downward line, with occasional huge cascades spanning much of the grid. That "no characteristic size" signature is exactly what self-organized criticality means, and it is the same mechanism invoked for real avalanches, earthquakes and forest fires.
- Threshold Zc — how many grains a cell tolerates before toppling; lower thresholds topple more readily and produce more frequent, smaller avalanches.
- Grid size — larger grids support larger maximum avalanches and a longer power-law tail.
- Perturbation strength — grains dropped at once when you click; large bursts are a good way to force a visible large cascade on demand.
- Auto-drop rate — background grains/second added at random cells, which is what drives the pile toward its critical state on its own, the way wind or vibration drives a real sandpile.