Task 5 of 6
Four kernels, three fields, one hundred steps. The loop lives in JavaScript, the grids live on the GPU, and the discipline is exactly the one Reaction–Diffusion and Cellular Automata already drilled — with one extra wrinkle worth naming.
Each step is ordered: dropTotal runs first, because the other
three read the grid it produces. Then moveWater, moveSediment
and erode all read the same snapshot of height, water and
sediment — plus that fresh drop grid — and only when all three have returned do you swap.
Overwrite height early and moveWater starts routing over
terrain that has already been cut, which is a different simulation and a worse one.
Nothing here is pipelined: each kernel hands JavaScript an ordinary array and gets one
back. That works, and it costs a round trip per pass — Pipelines & Textures shows how
to keep the whole thing resident on the card with pipeline: true and
immutable: true, which is the production answer once the grid stops being
64×64.
STEPS steps of the whole model, recording the
sediment in flight and the deepest water each step, then plot both.dropTotal first each step, then the other three with the same height / water / sedimenttotalOf(sediment) and maxOf(water) onto the traces each step and plot themconst drop = await dropTotal(height, water);
const nextWater = await moveWater(height, water, drop);
const nextSediment = await moveSediment(height, water, sediment, drop);
const nextHeight = await erode(height, water, sediment, drop);
height = nextHeight;
water = nextWater;
sediment = nextSediment;
Four awaits in order. Never Promise.all them: three of the
four need the first one's answer.
totalOf and maxOf are written for you. After the
swap:
carried.push(totalOf(sediment));
deepest.push(maxOf(water));
and after the loop,
plot({ 'sediment in flight': carried }, { title: '…' }).
Sediment climbs, bends over around step 60 as pick-up and settling come into balance — and then, around step 80, kicks upward again: that second climb is the two hollows turning into lakes, and it shows up in the other plot at the same moment as a step in the deepest water. From there the deepest cell keeps climbing, with a sawtooth on it, because a lake surface trades a little water back and forth with its rim every step. Drag the rainfall slider and watch both curves scale.
This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.