Task 2 of 5
Nothing about the kernel you just wrote is wrong. Hand it a step size that is slightly too big and it will still compute exactly what you asked for — and the field will be at 10²⁴ in eighty steps.
Look again at that centre weight, 1 − 4α. At α = 0.1 it is
+0.6, the new value really is an average of five old ones, and an average
can never leave the range of the things it averaged. At α = 0.4 it is
−0.6, and "average" has become a lie: a cell that sits above its
neighbours is now pushed further above them every step. In
dims dimensions the centre weight is 1 − 2·dims·α, so the
tipping point is α = 1/(2·dims) — which, written in the quantities you
actually control, is the limit every explicit solver lives under:
dt ≤ dx² / (2 · D · dims)
dims = 2 on a 2D grid
Past it, the fastest pattern the grid can hold — a checkerboard of alternating hot
and cold cells — is multiplied by |1 − 8α| every step instead of damped.
At α = 0.4 that is 2.2× per step, and there is always
some checkerboard in there, if only from rounding. Ten steps: ×2,700. Eighty steps:
the run below.
dtMax, then run the same eighty-step
simulation twice — once safely inside the limit, once past it — and watch the console.
The stepping loop is written for you: N-Body already made a meal of how to
advance a simulation, and the only thing that matters here is how far each step goes.dtMax = dx * dx / (2 * D * DIMS) — it is 0.5 for this gridrun once at 0.4 * dtMax and once at 1.6 * dtMaxSTEPS at 80 — the trace prints the hottest cell every 20 stepsEach of the 2 · dims neighbours takes an α-sized bite
out of the centre, so the centre keeps 1 − 2·dims·α. Set that to zero,
substitute α = D·dt/dx², and solve for dt.
const dtMax = dx * dx / (2 * D * DIMS);
await run('SAFE', 0.4 * dtMax);
await run('PAST THE LINE', 1.6 * dtMax);dx to sharpen a picture
quarters the legal dt, so the same second of simulated time costs four
times as many kernel launches.
This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.