1% Schnakenberg reaction-diffusion on a closed surface.
2%
3% du/dt = D1*lap_g(u) + a - u + u^2*v
4% dv/dt = D2*lap_g(v) + b - u^2*v
5%
6% Explicit reaction, implicit diffusion (IMEX Euler). The implicit solve
7% splits lap_g = lap_s + dlap: the round-sphere part lap_s is diagonal in
8% spherical-harmonic space (eigenvalues -lam), and the loop iterates the
9% geometric correction dlap from that exact solve. Grid fields are npts x 1;
10% spectral fields are real 2 x nlm. See docs/richardson-iteration.md.
11%
12% The correction evaluates lap_g in flux form -- 6 transforms per species
13% per iteration where the Cartesian-gradient form (Algorithm 4 of
14% evolving_surface/notes/algos.tex) needs 12. See
15% docs/reduced-transforms.md, and models/schnakenberg_alg4.m
16% for the original form kept as a live reference.
18function [U, V, u, v] = init(noise, a, b)
19 us = a + b;
20 vs = b / (us * us);
21 [U, V] = analys(us + noise, vs * ones(numel(noise), 1));
22 [u, v] = synth(U, V);
23end
25function [Un, Vn, u, v] = step(U, V, lam, filt, gx, gy, gz, p1, p2, q2, r, a, b, D1, D2, dt, niter)
26 % Grouped transforms -- [a, b] = synth(x, y) -- are explicit batching:
27 % output k is the transform of input k, and the whole group runs as one
28 % batched Legendre dispatch, or as many as the device's lane width allows
29 % (src/mgpu/plan.ts, materializeTransforms). The grouping is a promise of
30 % independence, never of a lane width, so the same source runs anywhere.
31 [u, v] = synth(U, V);
32 uuv = u .* u .* v;
34 % Right-hand side of the implicit solve (I - dt*D*lap_g) Unew = B.
35 ru = a - u + uuv;
36 rv = b - uuv;
37 [Ru, Rv] = analys(ru, rv);
38 Bu = U + dt * Ru;
39 Bv = V + dt * Rv;
41 % Round-sphere solve, then iterate the geometric correction.
42 Un = Bu ./ (1 + (dt * D1) * lam);
43 Vn = Bv ./ (1 + (dt * D2) * lam);
45 for k = 1:niter
46 % dlap = lap_g - lap_s at the current iterate, in flux form
47 % (docs/reduced-transforms.md Sec 4). The sin-weighted derivatives
48 % sin(theta)*dtheta(u) and dphi(u) -- both smooth on the sphere,
49 % synthesized straight from the dthetac/dphic coefficient shuffles --
50 % are combined pointwise through the precomputed weights p1,p2,q2 into
51 % two fluxes P,Q, also smooth. Their coefficients are then pushed
52 % through the *same* shuffles again and summed before the one synthesis
53 % of the divergence, which r scales into lap_g(u). The only division by
54 % sin(theta) anywhere is folded into p1,p2,q2,r at precompute time.
55 % lam.*Un adds back -lap_s(Un), since lam holds +l(l+1). filt zeroes the
56 % top two degrees, where the derivative recurrences cannot exactly
57 % represent a derivative.
58 %
59 % The two species share each grouped call: the four gradient
60 % syntheses, the four flux analyses, the two divergence syntheses and
61 % the two final analyses each run as one batched dispatch.
62 Fu = Un .* filt;
63 Fv = Vn .* filt;
64 vtu = dthetac(Fu);
65 vpu = dphic(Fu);
66 vtv = dthetac(Fv);
67 vpv = dphic(Fv);
68 [Ftu, Fpu, Ftv, Fpv] = synth(vtu, vpu, vtv, vpv);
69 Pu = p1 .* Ftu + p2 .* Fpu;
70 Qu = p2 .* Ftu + q2 .* Fpu;
71 Pv = p1 .* Ftv + p2 .* Fpv;
72 Qv = p2 .* Ftv + q2 .* Fpv;
73 [PAu, QAu, PAv, QAv] = analys(Pu, Qu, Pv, Qv);
74 Pcu = PAu .* filt;
75 Qcu = QAu .* filt;
76 Pcv = PAv .* filt;
77 Qcv = QAv .* filt;
78 scu = dthetac(Pcu) + dphic(Qcu);
79 scv = dthetac(Pcv) + dphic(Qcv);
80 [Lu, Lv] = synth(scu, scv);
81 lapu = r .* Lu;
82 lapv = r .* Lv;
83 [LAu, LAv] = analys(lapu, lapv);
84 dLu = LAu + lam .* Un;
85 dLv = LAv + lam .* Vn;
87 Un = (Bu + (dt * D1) * dLu) ./ (1 + (dt * D1) * lam);
88 Vn = (Bv + (dt * D2) * dLv) ./ (1 + (dt * D2) * lam);
89 end
90end