Task 2 of 4
Now the chemistry. Gray–Scott tracks two chemicals on the same grid:
U (food, fed in everywhere) and V (the eater —
U + 2V → 3V, so V converts U into more V, and is itself slowly removed).
Per cell, per step:
u' = u + (Du·∇²u − u·v² + F·(1 − u))·dt
v' = v + (Dv·∇²v + u·v² − (F + K)·v)·dt
Each equation is your task-1 Laplacian plus three pointwise terms — diffusion, reaction, feed/kill. One kernel per chemical: both are gathers over the old grids, so all 1,024 cells of a step can run in parallel.
stepU and
stepV each return their chemical's next value. The Laplacians are already
gathered for you.stepU first, then stepVu·v·v — U loses it, V gains itstepU returns uc + (du·lap − uc·vc·vc + f·(1 − uc))·dtstepV returns vc + (dv·lap + uc·vc·vc − (f + k)·vc)·dtlap, uc and vc are computed for you;
the parameters live in this.constants (du, f,
dt in stepU; dv, f, k,
dt in stepV). The TODO is one return per kernel.
return uc + (this.constants.du * lap - uc * vc * vc
+ this.constants.f * (1 - uc)) * this.constants.dt;
stepV is the same shape
with + uc·vc·vc and − (f + k)·vc.
This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.