Reaction-diffusion systems make spots, stripes and labyrinths out of two chemicals that react and spread at different speeds. The Gray-Scott model is the version that is easiest to put on a GPU: two fields, one Laplacian each, a few multiplications per pixel per step. Changing two numbers moves you between coral, spots that divide like cells, and moving worms.

Two pretend chemicals spread and react, and spots and stripes appear by themselves. Image: Jonathan Lidbeck from Eugene, USA, CC BY 2.0, via Wikimedia Commons.

Mental Model

  • Two fields and on a grid. is fed in from outside and eaten by ; reproduces by eating and is removed at a slow rate.
  • Both spread by diffusion, but spreads slower than . That speed difference is what lets patterns form (a Turing instability).
  • Pattern type is set almost entirely by the feed rate and kill rate . Diffusion constants stay fixed.
  • The simulation is a stencil: each cell reads its 3 by 3 neighborhood. Two textures swapped every step (“ping-pong”) implement it on a GPU.

1. The Equations

The reaction is (V catalyzes its own production from U) plus (V decays to an inert product). With concentrations and :

The term is the reaction rate: it removes and adds the same amount to . pulls back toward 1 at rate . removes : because the whole reactor is flushed at that rate, plus for the decay. Without the system sits at , , which is why the standard start is a field of with a small seed of .

2. Parameters

Same two pretend chemicals, two different feed and kill rates: one grows like coral, the other splits into dots like dividing cells. Rendered from the equations on this page.

Common choices on a unit grid: , , . The feed and kill rates then select the regime. These are typical values from the usual parameter maps, and the exact boundaries between regimes are fuzzy:

RegimeFk
Coral-like, branching labyrinth0.0550.062
Mitosis-like, spots that split0.03670.0649

Only a narrow band of gives patterns at all; outside it the seed either dies or fills the whole domain. Move by 0.002 or so at a time when exploring.

3. Discretization

Replace by a 3 by 3 weighted sum. A common stencil, with weights summing to zero so a constant field has no diffusion:

The explicit (forward Euler) update is

Update and from the old values, not one after the other.

Stability. For explicit diffusion each Fourier mode is multiplied by , where is an eigenvalue of the stencil. The most negative eigenvalue of this is at the checkerboard mode: . Stability needs , i.e. . With this allows . The plain 5-point Laplacian (weights 1, 1, 1, 1, -4) has most negative eigenvalue , which would force for the same . The reaction terms add their own limit, but at these parameters they are slow compared with diffusion.

4. A Short Simulation

import numpy as np
 
def lap(A):
    r = lambda dy, dx: np.roll(np.roll(A, dy, 0), dx, 1)   # periodic edges
    return (0.2 * (r(1,0) + r(-1,0) + r(0,1) + r(0,-1))
            + 0.05 * (r(1,1) + r(1,-1) + r(-1,1) + r(-1,-1)) - A)
 
def run(F, k, steps=8000, n=128, Du=1.0, Dv=0.5):
    U = np.ones((n, n)); V = np.zeros((n, n))
    U[54:74, 54:74] = 0.5; V[54:74, 54:74] = 0.25          # seed square
    V += 0.02 * np.random.default_rng(0).random((n, n)) * (V > 0)
    for _ in range(steps):
        uvv = U * V * V
        U, V = (U + Du * lap(U) - uvv + F * (1 - U),
                V + Dv * lap(V) + uvv - (F + k) * V)
    return U, V
 
U, V = run(0.0367, 0.0649)     # mitosis-like

I ran this on a 128 by 128 grid for 8000 steps. With the result stayed finite and covered about half the grid ( on 52% of cells) in 3 connected regions (a labyrinth). With it settled into 49 separate spots covering 20% of the grid. The values of stayed between 0 and about 0.4. Counts of spots depend on the seed and the number of steps; the regime is what’s reproducible.

5. Ping-Pong on the GPU

The update only reads the previous state, so each step is a fragment shader that reads texture A and writes texture B; the next step swaps them. Pack in the red channel and in green. Three requirements: a 32-bit float pixel format (8-bit textures cannot hold the small increments), repeat or wrap addressing for periodic edges, and several steps per displayed frame, since the pattern evolves over thousands of steps. In TouchDesigner that is a GLSL TOP whose input is a Feedback TOP pointing back at the same GLSL TOP, with an initial seed injected on the first frame, and several such stages chained to get more substeps per frame. Native Metal or WebGL does the same with two textures and a swap per pass. Draw the result by mapping through a color ramp, or use the gradient of as a normal map for lighting.

Intuition

A spot of keeps itself alive by consuming around it. Because spreads faster than , the supply of reaches the spot from a wide area, while stays local. The balance between the two spreading lengths sets the spacing of spots and stripes.