1// One explicit time step (symplectic Euler) of the von Karman plate with a
2// thickness field, a stress-free profile, and the drumstick.
3//
4// State layout (structure of arrays, n = nx * ny nodes):
5// pos: [u | v | w | wLo] wLo carries the rounding error of the w update
6// vel: [du | dv | dw]
7// A workgroup of 16x16 threads advances a 12x12 tile of nodes in three
8// phases separated by barriers. (1) Every thread loads one entry of the tile
9// plus its 2-node halo (w, u, v, w0, thickness, node weights) into shared
10// memory. (2) The threads compute, once each and again into shared memory,
11// the membrane forces of the 13x13 cells around the tile and the weighted
12// bending moments and mixed differences that the bending stencil gathers.
13// (3) Each of the 144 tile nodes evaluates its update from shared memory.
14// On grids of a few thousand nodes the kernel is bound by the latency of one
15// workgroup rather than by throughput, so the shared phases, which replace a
16// tenfold per-node recomputation of the stencil ingredients, are what make a
17// step cheap.
18//
19// Nodes outside the plate (weight 0) are skipped: a second difference exists
20// only where the node and both neighbors belong to the plate, and a cell
21// contributes only when its four corners do. That gives the free edge of the
22// rectangle and a staircase free edge for any other outline, hole included.
24@group(0) @binding(0) var<uniform> P: Params;
25@group(0) @binding(1) var<uniform> SP: StepParams;
26@group(0) @binding(2) var<storage, read> posIn: array<f32>;
27@group(0) @binding(3) var<storage, read> velIn: array<f32>;
28@group(0) @binding(4) var<storage, read_write> posOut: array<f32>;
29@group(0) @binding(5) var<storage, read_write> velOut: array<f32>;
30@group(0) @binding(6) var<storage, read_write> hist: array<f32>; // [slot * n + node]: transverse velocity at the audio sample instants
31// Two 8-float slots of drumstick state: position, velocity, force, max force,
32// time since the contact released, withdrawn flag, and two spare.
33const STICK_RELEASE: f32 = 5e-3; // [s] a release longer than this ends the hit
34@group(0) @binding(7) var<storage, read_write> stick: array<f32>;
36const TILE = 12; // nodes per workgroup along each axis (must match simulation.ts)
37const HALO = 2;
38const TW = 16; // TILE + 2 * HALO: the loaded tile, one thread per entry
39const CT = 13; // TILE + 1: cells adjacent to the tile's nodes
40const MT = 14; // TILE + 2: nodes at which the bending moments are needed
41var<workgroup> tw: array<f32, 256>; // w
42var<workgroup> tu: array<f32, 256>; // u
43var<workgroup> tv: array<f32, 256>; // v
44var<workgroup> tw0: array<f32, 256>; // stress-free shape
45var<workgroup> th: array<f32, 256>; // thickness
46var<workgroup> tn: array<u32, 64>; // node weights in quarters (0..4), four to a word
47var<workgroup> cellF: array<f32, 2028>; // membrane forces of the CT x CT cells, 4 nodes x 3 components
48var<workgroup> ma: array<f32, 196>; // weighted bending moments at the MT x MT inner nodes
49var<workgroup> mb: array<f32, 196>;
50var<workgroup> cc: array<f32, 169>; // weighted mixed differences at the CT x CT cells
52var<private> ox: i32;
53var<private> oy: i32;
54var<private> ihx2: f32;
55var<private> ihy2: f32;
56var<private> ihxy: f32;
58fn id(i: i32, j: i32) -> u32 { return u32(j) * P.nx + u32(i); }
59fn lidx(i: i32, j: i32) -> u32 { return u32(j - oy) * u32(TW) + u32(i - ox); }
60fn midx(i: i32, j: i32) -> u32 { return u32(j - oy - 1) * u32(MT) + u32(i - ox - 1); }
61fn cidx(ci: i32, cj: i32) -> u32 { return u32(cj - oy - 1) * u32(CT) + u32(ci - ox - 1); }
62fn W(i: i32, j: i32) -> f32 { return tw[lidx(i, j)]; }
63fn U(i: i32, j: i32) -> f32 { return tu[lidx(i, j)]; }
64fn V(i: i32, j: i32) -> f32 { return tv[lidx(i, j)]; }
65fn W0(i: i32, j: i32) -> f32 { return tw0[lidx(i, j)]; }
66fn DW(i: i32, j: i32) -> f32 { return velIn[2u * P.n + id(i, j)]; }
67fn TH(i: i32, j: i32) -> f32 { return th[lidx(i, j)]; }
68fn NWq(i: i32, j: i32) -> u32 { let l = lidx(i, j); return (tn[l >> 2u] >> ((l & 3u) * 8u)) & 0xFFu; }
69fn NW(i: i32, j: i32) -> f32 { return 0.25 * f32(NWq(i, j)); }
70fn INS(i: i32, j: i32) -> bool { return NWq(i, j) != 0u; }
71fn insCell(ci: i32, cj: i32) -> bool { return INS(ci, cj) && INS(ci + 1, cj) && INS(ci + 1, cj + 1) && INS(ci, cj + 1); }
72fn Dn(i: i32, j: i32) -> f32 { let h = TH(i, j); return P.Db * h * h * h; }
73fn cellH(ci: i32, cj: i32) -> f32 { return 0.25 * (TH(ci, cj) + TH(ci + 1, cj) + TH(ci + 1, cj + 1) + TH(ci, cj + 1)); }
75// Graph Laplacian of the transverse velocity at node (i, j), over neighbors in the plate.
76fn lapDW(i: i32, j: i32) -> f32 {
77 let c = DW(i, j);
78 var lap = 0.0;
79 if (INS(i - 1, j)) { lap += (DW(i - 1, j) - c) * ihx2; }
80 if (INS(i + 1, j)) { lap += (DW(i + 1, j) - c) * ihx2; }
81 if (INS(i, j - 1)) { lap += (DW(i, j - 1) - c) * ihy2; }
82 if (INS(i, j + 1)) { lap += (DW(i, j + 1) - c) * ihy2; }
83 return lap;
84}
86// Weighted bending moments at nodes; zero where the second difference is undefined.
87fn Ma(i: i32, j: i32) -> f32 {
88 if (!(INS(i - 1, j) && INS(i, j) && INS(i + 1, j))) { return 0.0; }
89 let a = (W(i + 1, j) - 2.0 * W(i, j) + W(i - 1, j)) * ihx2;
90 var b = 0.0;
91 if (INS(i, j - 1) && INS(i, j + 1)) { b = (W(i, j + 1) - 2.0 * W(i, j) + W(i, j - 1)) * ihy2; }
92 return NW(i, j) * Dn(i, j) * (a + P.nu * b);
93}
94fn Mb(i: i32, j: i32) -> f32 {
95 if (!(INS(i, j - 1) && INS(i, j) && INS(i, j + 1))) { return 0.0; }
96 let b = (W(i, j + 1) - 2.0 * W(i, j) + W(i, j - 1)) * ihy2;
97 var a = 0.0;
98 if (INS(i - 1, j) && INS(i + 1, j)) { a = (W(i + 1, j) - 2.0 * W(i, j) + W(i - 1, j)) * ihx2; }
99 return NW(i, j) * Dn(i, j) * (b + P.nu * a);
100}
101fn Cc(ci: i32, cj: i32) -> f32 {
102 if (!insCell(ci, cj)) { return 0.0; }
103 let h = cellH(ci, cj);
104 return P.Db * h * h * h * (W(ci + 1, cj + 1) - W(ci + 1, cj) - W(ci, cj + 1) + W(ci, cj)) * ihxy;
105}
107// Membrane forces of the bilinear cell (ci, cj) on its four nodes (2x2 Gauss), written to cellF at local cell index lc.
108fn cellForces(ci: i32, cj: i32, lc: u32) {
109 let base = lc * 12u;
110 if (!insCell(ci, cj)) {
111 for (var q = 0u; q < 12u; q++) { cellF[base + q] = 0.0; }
112 return;
113 }
114 let hc = cellH(ci, cj);
115 let C = P.Cm * hc;
116 let G = P.Gm * hc;
117 let Un = vec4f(U(ci, cj), U(ci + 1, cj), U(ci + 1, cj + 1), U(ci, cj + 1));
118 let Vn = vec4f(V(ci, cj), V(ci + 1, cj), V(ci + 1, cj + 1), V(ci, cj + 1));
119 let W0n = vec4f(W0(ci, cj), W0(ci + 1, cj), W0(ci + 1, cj + 1), W0(ci, cj + 1));
120 let Wdn = vec4f(W(ci, cj), W(ci + 1, cj), W(ci + 1, cj + 1), W(ci, cj + 1)); // deviation from the stress-free shape
121 let q = 0.25 * P.hx * P.hy;
122 let ihx = 1.0 / P.hx; let ihy = 1.0 / P.hy;
123 let g3 = 0.57735026918962576;
124 var Fu = vec4f(0.0); var Fv = vec4f(0.0); var Fw = vec4f(0.0);
125 for (var gp = 0; gp < 4; gp++) {
126 var xi = -g3; if ((gp & 1) != 0) { xi = g3; }
127 var eta = -g3; if ((gp & 2) != 0) { eta = g3; }
128 let dNdx = vec4f(-(1.0 - eta), 1.0 - eta, 1.0 + eta, -(1.0 + eta)) * (0.5 * ihx);
129 let dNdy = vec4f(-(1.0 - xi), -(1.0 + xi), 1.0 + xi, 1.0 - xi) * (0.5 * ihy);
130 let ux = dot(dNdx, Un); let uy = dot(dNdy, Un);
131 let vx = dot(dNdx, Vn); let vy = dot(dNdy, Vn);
132 let dwx = dot(dNdx, Wdn); let dwy = dot(dNdy, Wdn);
133 let w0x = dot(dNdx, W0n); let w0y = dot(dNdy, W0n);
134 let wx = w0x + dwx; let wy = w0y + dwy; // total slope
135 // Marguerre strains: the total slope minus the stress-free slope, written
136 // so that every term carries the deviation. The direct form
137 // 0.5 (wx^2 - w0x^2) subtracts two nearly equal numbers, and in single
138 // precision its rounding error is proportional to w0x^2 and so does not
139 // fall with the vibration: on a curved design it acts as a constant
140 // broadband forcing that leaves a noise floor which never decays.
141 let exx = ux + dwx * (w0x + 0.5 * dwx);
142 let eyy = vy + dwy * (w0y + 0.5 * dwy);
143 let gxy = uy + vx + w0x * dwy + w0y * dwx + dwx * dwy;
144 let Nxx = C * (exx + P.nu * eyy);
145 let Nyy = C * (eyy + P.nu * exx);
146 let Nxy = G * gxy;
147 Fu -= q * (Nxx * dNdx + Nxy * dNdy);
148 Fv -= q * (Nyy * dNdy + Nxy * dNdx);
149 Fw -= q * ((Nxx * wx + Nxy * wy) * dNdx + (Nxy * wx + Nyy * wy) * dNdy);
150 }
151 for (var a = 0u; a < 4u; a++) {
152 cellF[base + a * 3u] = Fu[a];
153 cellF[base + a * 3u + 1u] = Fv[a];
154 cellF[base + a * 3u + 2u] = Fw[a];
155 }
156}
158// True for the plate nodes of the drumstick contact patch (same rule as patchAround in params.ts).
159fn inContactPatch(i: i32, j: i32) -> bool {
160 if (nodeWeight(i, j) == 0.0) { return false; }
161 let dxs = nodeX(i) - P.strikeX; let dys = nodeY(j) - P.strikeY;
162 return dxs * dxs + dys * dys <= P.strikeR2 || id(i, j) == P.strikeNode;
163}
165fn cellForce(ci: i32, cj: i32, a: u32) -> vec3f {
166 let base = cidx(ci, cj) * 12u + a * 3u;
167 return vec3f(cellF[base], cellF[base + 1u], cellF[base + 2u]);
168}
170@compute @workgroup_size(16, 16)
171fn main(@builtin(local_invocation_id) lid: vec3u, @builtin(workgroup_id) wid: vec3u) {
172 let n = P.n;
173 ox = i32(wid.x) * TILE - HALO;
174 oy = i32(wid.y) * TILE - HALO;
175 ihx2 = 1.0 / (P.hx * P.hx); ihy2 = 1.0 / (P.hy * P.hy); ihxy = 1.0 / (P.hx * P.hy);
176 let lt = lid.y * 16u + lid.x;
178 // Phase 1: one entry of the tile with halo per thread.
179 {
180 let gi = ox + i32(lid.x); let gj = oy + i32(lid.y);
181 var ww = 0.0; var uu = 0.0; var vv = 0.0; var z0 = 0.0; var hh = 0.0;
182 if (gi >= 0 && gi < i32(P.nx) && gj >= 0 && gj < i32(P.ny)) {
183 let k = id(gi, gj);
184 let st = nd[k];
185 ww = posIn[2u * n + k]; uu = posIn[k]; vv = posIn[n + k]; z0 = st.x; hh = st.z;
186 }
187 tw[lt] = ww; tu[lt] = uu; tv[lt] = vv; tw0[lt] = z0; th[lt] = hh;
188 if (lt < 64u) {
189 var word = 0u;
190 for (var q = 0u; q < 4u; q++) {
191 let e = lt * 4u + q;
192 let ei = ox + i32(e % u32(TW)); let ej = oy + i32(e / u32(TW));
193 var wq = 0u;
194 if (ei >= 0 && ei < i32(P.nx) && ej >= 0 && ej < i32(P.ny)) { wq = u32(round(4.0 * nd[id(ei, ej)].y)); }
195 word |= wq << (q * 8u);
196 }
197 tn[lt] = word;
198 }
199 }
200 workgroupBarrier();
202 // Phase 2: membrane forces of the cells, bending moments and mixed differences.
203 if (lt < 169u) {
204 let ci = ox + 1 + i32(lt % u32(CT)); let cj = oy + 1 + i32(lt / u32(CT));
205 cellForces(ci, cj, lt);
206 cc[lt] = Cc(ci, cj);
207 }
208 if (lt < 196u) {
209 let mi = ox + 1 + i32(lt % u32(MT)); let mj = oy + 1 + i32(lt / u32(MT));
210 ma[lt] = Ma(mi, mj);
211 mb[lt] = Mb(mi, mj);
212 }
213 workgroupBarrier();
215 // Phase 3: the update of the TILE x TILE nodes (no barriers past this point).
216 if (lt >= 144u) { return; }
217 let i = ox + HALO + i32(lt % u32(TILE)); let j = oy + HALO + i32(lt / u32(TILE));
218 if (i >= i32(P.nx) || j >= i32(P.ny)) { return; }
219 let k = id(i, j);
220 let weight = NW(i, j);
221 if (weight == 0.0) {
222 posOut[k] = 0.0; posOut[n + k] = 0.0; posOut[2u * n + k] = 0.0; posOut[3u * n + k] = 0.0;
223 velOut[k] = 0.0; velOut[n + k] = 0.0; velOut[2u * n + k] = 0.0;
224 return;
225 }
227 // Elastic forces: bending from the discrete Kirchhoff energy, membrane from the cells.
228 let bend = (ma[midx(i - 1, j)] - 2.0 * ma[midx(i, j)] + ma[midx(i + 1, j)]) * ihx2
229 + (mb[midx(i, j - 1)] - 2.0 * mb[midx(i, j)] + mb[midx(i, j + 1)]) * ihy2
230 + 2.0 * (1.0 - P.nu) * (cc[cidx(i, j)] - cc[cidx(i - 1, j)] - cc[cidx(i, j - 1)] + cc[cidx(i - 1, j - 1)]) * ihxy;
231 var F = vec3f(0.0, 0.0, -P.hx * P.hy * bend);
232 F += cellForce(i, j, 0u);
233 F += cellForce(i - 1, j, 1u);
234 F += cellForce(i - 1, j - 1, 2u);
235 F += cellForce(i, j - 1, 3u);
236 let pOld = vec3f(posIn[k], posIn[n + k], posIn[2u * n + k]);
237 let vel = vec3f(velIn[k], velIn[n + k], velIn[2u * n + k]);
238 let mass = P.rho * TH(i, j) * P.hx * P.hy * weight;
239 var acc = F * (1.0 / mass);
241 // Losses: frequency-independent on all components, the graph Laplacian of the
242 // transverse velocity (~k^2), and an extra frequency-independent in-plane loss.
243 acc -= 2.0 * SP.sigma0 * vel;
244 acc.z += 2.0 * SP.sigma1 * lapDW(i, j);
245 acc.x -= 2.0 * SP.sigmaIn * vel.x;
246 acc.y -= 2.0 * SP.sigmaIn * vel.y;
248 // Drumstick: Hertz contact between a free mass and the contact patch (the
249 // plate nodes within the tip radius, plus the nearest one). The contact
250 // coordinate is the mass-weighted mean displacement of the patch, and the
251 // force is spread over the patch in proportion to the node masses, so the
252 // pair is conjugate and the contact has a potential energy. Once the
253 // contact has been released for STICK_RELEASE the stick is withdrawn.
254 if ((SP.flags & 1u) != 0u) {
255 let sIn = 8u * SP.parity;
256 // Inertia relief of the free plate. The force of the previous step is
257 // applied to the whole plate as the rigid acceleration field that balances
258 // the patch load and its moments about the center, so the plate gains no
259 // net momentum (the thread takes it up in reality).
260 let Fprev = stick[sIn + 2u];
261 acc.z += Fprev * (1.0 / P.mass + nodeY(j) * P.patchCY / P.Ix + nodeX(i) * P.patchCX / P.Iy);
262 if (inContactPatch(i, j)) {
263 let sx = stick[sIn]; let sv = stick[sIn + 1u]; let sMax = stick[sIn + 3u];
264 var released = stick[sIn + 4u];
265 var withdrawn = stick[sIn + 5u];
266 var Fc = 0.0;
267 if (withdrawn == 0.0) {
268 var wc = 0.0;
269 for (var pj = i32(P.patchJ0); pj <= i32(P.patchJ1); pj++) {
270 for (var pi = i32(P.patchI0); pi <= i32(P.patchI1); pi++) {
271 if (inContactPatch(pi, pj)) { wc += nodeMass(pi, pj) * posIn[2u * n + id(pi, pj)]; }
272 }
273 }
274 wc /= P.patchMass;
275 let pen = wc - sx; // the stick sits on the +z side and moves toward -z
276 if (pen > 0.0) { Fc = P.K * pow(pen, P.alpha); released = 0.0; }
277 else if (sMax > 0.0) {
278 released += P.dt;
279 if (released > STICK_RELEASE) { withdrawn = 1.0; }
280 }
281 }
282 acc.z -= Fc / P.patchMass;
283 if (k == P.strikeNode) {
284 let sOut = 8u * (1u - SP.parity);
285 let svn = sv + P.dt * Fc / P.stickMass;
286 stick[sOut] = sx + P.dt * svn;
287 stick[sOut + 1u] = svn;
288 stick[sOut + 2u] = Fc;
289 stick[sOut + 3u] = max(sMax, Fc);
290 stick[sOut + 4u] = released;
291 stick[sOut + 5u] = withdrawn;
292 }
293 }
294 }
296 let vNew = vel + P.dt * acc;
297 if (SP.audioSlot >= 0) { hist[u32(SP.audioSlot) * n + k] = vNew.z; }
298 var pNew = pOld + P.dt * vNew;
299 // Compensated update of w: the increment dt * v can be far below the
300 // float32 resolution of w, so the rounding error is carried separately.
301 let y = P.dt * vNew.z + posIn[3u * n + k];
302 let t = pOld.z + y;
303 let lo = (pOld.z - t) + y;
304 pNew.z = t;
305 posOut[k] = pNew.x; posOut[n + k] = pNew.y; posOut[2u * n + k] = pNew.z; posOut[3u * n + k] = lo;
306 velOut[k] = vNew.x; velOut[n + k] = vNew.y; velOut[2u * n + k] = vNew.z;
307}