1// One explicit time step (symplectic Euler) of the von Karman plate.
2//
3// State layout (structure of arrays, n = nx * ny nodes):
4// pos: [u | v | w | wLo] wLo carries the rounding error of the w update
5// vel: [du | dv | dw]
6// Each 16x16 workgroup loads its tile plus a 2-node halo of u, v, w, dw into
7// shared memory once, so the stencils below never touch global memory.
9@group(0) @binding(0) var<uniform> P: Params;
10@group(0) @binding(1) var<uniform> SP: StepParams;
11@group(0) @binding(2) var<storage, read> posIn: array<f32>;
12@group(0) @binding(3) var<storage, read> velIn: array<f32>;
13@group(0) @binding(4) var<storage, read_write> posOut: array<f32>;
14@group(0) @binding(5) var<storage, read_write> velOut: array<f32>;
15@group(0) @binding(6) var<storage, read_write> hist: array<f32>; // [slot * n + node] velocity of w at the audio sample instants
16@group(0) @binding(7) var<storage, read_write> react: array<f32>; // constraint reaction (acceleration of w) per node
17@group(0) @binding(8) var<storage, read> w0: array<f32>; // stress-free initial shape
19const TILE = 16;
20const HALO = 2;
21const TW = 20; // TILE + 2 * HALO
22var<workgroup> tw: array<f32, 400>;
23// Membrane force contributions of the 17x17 cells around the tile, 4 nodes x 3 components each.
24const CT = 17;
25var<workgroup> cellF: array<f32, 3468>;
27// Tile origin in global node coordinates (set once per invocation).
28var<private> ox: i32;
29var<private> oy: i32;
30var<private> ihx2: f32;
31var<private> ihy2: f32;
32var<private> ihxy: f32;
34fn id(i: i32, j: i32) -> u32 { return u32(j) * P.nx + u32(i); }
35fn lidx(i: i32, j: i32) -> u32 { return u32(j - oy) * u32(TW) + u32(i - ox); }
36// Global indices; the tile covers [ox, ox + TW) x [oy, oy + TW).
37fn W(i: i32, j: i32) -> f32 { return tw[lidx(i, j)]; }
38fn U(i: i32, j: i32) -> f32 { return posIn[id(i, j)]; }
39fn V(i: i32, j: i32) -> f32 { return posIn[P.n + id(i, j)]; }
40fn DW(i: i32, j: i32) -> f32 { return velIn[2u * P.n + id(i, j)]; }
41fn W0(i: i32, j: i32) -> f32 { return w0[id(i, j)]; }
43// Weighted bending "moments" at nodes; zero where the second difference is undefined.
44fn Ma(i: i32, j: i32) -> f32 {
45 let nx = i32(P.nx); let ny = i32(P.ny);
46 if (i < 1 || i > nx - 2 || j < 0 || j > ny - 1) { return 0.0; }
47 let a = (W(i + 1, j) - 2.0 * W(i, j) + W(i - 1, j)) * ihx2;
48 var b = 0.0;
49 if (j >= 1 && j <= ny - 2) { b = (W(i, j + 1) - 2.0 * W(i, j) + W(i, j - 1)) * ihy2; }
50 return trapWeight(i, j) * (a + P.nu * b);
51}
52fn Mb(i: i32, j: i32) -> f32 {
53 let nx = i32(P.nx); let ny = i32(P.ny);
54 if (j < 1 || j > ny - 2 || i < 0 || i > nx - 1) { return 0.0; }
55 let b = (W(i, j + 1) - 2.0 * W(i, j) + W(i, j - 1)) * ihy2;
56 var a = 0.0;
57 if (i >= 1 && i <= nx - 2) { a = (W(i + 1, j) - 2.0 * W(i, j) + W(i - 1, j)) * ihx2; }
58 return trapWeight(i, j) * (b + P.nu * a);
59}
60// Mixed second difference at a cell center; zero outside the plate.
61fn Cc(ci: i32, cj: i32) -> f32 {
62 if (ci < 0 || ci > i32(P.nx) - 2 || cj < 0 || cj > i32(P.ny) - 2) { return 0.0; }
63 return (W(ci + 1, cj + 1) - W(ci + 1, cj) - W(ci, cj + 1) + W(ci, cj)) * ihxy;
64}
66// Membrane forces of the bilinear cell (ci, cj) on its four nodes (2x2 Gauss),
67// written to cellF at local cell index lc.
68fn cellForces(ci: i32, cj: i32, lc: u32) {
69 let base = lc * 12u;
70 if (ci < 0 || ci > i32(P.nx) - 2 || cj < 0 || cj > i32(P.ny) - 2) {
71 for (var q = 0u; q < 12u; q++) { cellF[base + q] = 0.0; }
72 return;
73 }
74 let Un = vec4f(U(ci, cj), U(ci + 1, cj), U(ci + 1, cj + 1), U(ci, cj + 1));
75 let Vn = vec4f(V(ci, cj), V(ci + 1, cj), V(ci + 1, cj + 1), V(ci, cj + 1));
76 let W0n = vec4f(W0(ci, cj), W0(ci + 1, cj), W0(ci + 1, cj + 1), W0(ci, cj + 1));
77 let Wn = vec4f(W(ci, cj), W(ci + 1, cj), W(ci + 1, cj + 1), W(ci, cj + 1)) + W0n; // total shape
78 let q = 0.25 * P.hx * P.hy;
79 let ihx = 1.0 / P.hx; let ihy = 1.0 / P.hy;
80 let g3 = 0.57735026918962576;
81 var Fu = vec4f(0.0); var Fv = vec4f(0.0); var Fw = vec4f(0.0);
82 for (var gp = 0; gp < 4; gp++) {
83 var xi = -g3; if ((gp & 1) != 0) { xi = g3; }
84 var eta = -g3; if ((gp & 2) != 0) { eta = g3; }
85 let dNdx = vec4f(-(1.0 - eta), 1.0 - eta, 1.0 + eta, -(1.0 + eta)) * (0.5 * ihx);
86 let dNdy = vec4f(-(1.0 - xi), -(1.0 + xi), 1.0 + xi, 1.0 - xi) * (0.5 * ihy);
87 let ux = dot(dNdx, Un); let uy = dot(dNdy, Un);
88 let vx = dot(dNdx, Vn); let vy = dot(dNdy, Vn);
89 let wx = dot(dNdx, Wn); let wy = dot(dNdy, Wn);
90 let w0x = dot(dNdx, W0n); let w0y = dot(dNdy, W0n);
91 // Marguerre strains: total slope minus the stress-free slope.
92 let exx = ux + 0.5 * (wx * wx - w0x * w0x);
93 let eyy = vy + 0.5 * (wy * wy - w0y * w0y);
94 let gxy = uy + vx + wx * wy - w0x * w0y;
95 let Nxx = P.C * (exx + P.nu * eyy);
96 let Nyy = P.C * (eyy + P.nu * exx);
97 let Nxy = P.G * gxy;
98 Fu -= q * (Nxx * dNdx + Nxy * dNdy);
99 Fv -= q * (Nyy * dNdy + Nxy * dNdx);
100 Fw -= q * ((Nxx * wx + Nxy * wy) * dNdx + (Nxy * wx + Nyy * wy) * dNdy);
101 }
102 for (var a = 0u; a < 4u; a++) {
103 cellF[base + a * 3u] = Fu[a];
104 cellF[base + a * 3u + 1u] = Fv[a];
105 cellF[base + a * 3u + 2u] = Fw[a];
106 }
107}
109// Stored membrane force on local node `a` of cell (ci, cj).
110fn cellForce(ci: i32, cj: i32, a: u32) -> vec3f {
111 let lc = u32(cj - oy - 1) * u32(CT) + u32(ci - ox - 1);
112 let base = lc * 12u + a * 3u;
113 return vec3f(cellF[base], cellF[base + 1u], cellF[base + 2u]);
114}
116@compute @workgroup_size(16, 16)
117fn main(@builtin(global_invocation_id) gid: vec3u, @builtin(local_invocation_id) lid: vec3u, @builtin(workgroup_id) wid: vec3u) {
118 let n = P.n;
119 ox = i32(wid.x) * TILE - HALO;
120 oy = i32(wid.y) * TILE - HALO;
121 ihx2 = 1.0 / (P.hx * P.hx); ihy2 = 1.0 / (P.hy * P.hy); ihxy = 1.0 / (P.hx * P.hy);
122 // Cooperative load of the w tile (400 entries, 256 threads).
123 let lt = lid.y * 16u + lid.x;
124 for (var e = lt; e < 400u; e += 256u) {
125 let gi = ox + i32(e % u32(TW)); let gj = oy + i32(e / u32(TW));
126 var ww = 0.0;
127 if (gi >= 0 && gi < i32(P.nx) && gj >= 0 && gj < i32(P.ny)) { ww = posIn[2u * n + id(gi, gj)]; }
128 tw[e] = ww;
129 }
130 workgroupBarrier();
131 // Membrane forces of the 17x17 cells adjacent to the tile's nodes.
132 for (var e = lt; e < 289u; e += 256u) {
133 let lci = i32(e % u32(CT)); let lcj = i32(e / u32(CT));
134 cellForces(ox + 1 + lci, oy + 1 + lcj, e);
135 }
136 workgroupBarrier();
138 let i = i32(gid.x); let j = i32(gid.y);
139 if (i >= i32(P.nx) || j >= i32(P.ny)) { return; }
140 let k = id(i, j);
143 let bend = (Ma(i - 1, j) - 2.0 * Ma(i, j) + Ma(i + 1, j)) * ihx2
144 + (Mb(i, j - 1) - 2.0 * Mb(i, j) + Mb(i, j + 1)) * ihy2
145 + 2.0 * (1.0 - P.nu) * (Cc(i, j) - Cc(i - 1, j) - Cc(i, j - 1) + Cc(i - 1, j - 1)) * ihxy;
146 var F = vec3f(0.0, 0.0, -P.D * P.hx * P.hy * bend);
147 F += cellForce(i, j, 0u);
148 F += cellForce(i - 1, j, 1u);
149 F += cellForce(i - 1, j - 1, 2u);
150 F += cellForce(i, j - 1, 3u);
151 let mass = P.rhoh * P.hx * P.hy * trapWeight(i, j);
152 var acc = F * (1.0 / mass);
154 // Loss: frequency-independent on all components, frequency-dependent (graph Laplacian) on w.
155 let vel = vec3f(velIn[k], velIn[n + k], velIn[2u * n + k]);
156 var lap = 0.0;
157 if (i > 0) { lap += (DW(i - 1, j) - vel.z) * ihx2; }
158 if (i < i32(P.nx) - 1) { lap += (DW(i + 1, j) - vel.z) * ihx2; }
159 if (j > 0) { lap += (DW(i, j - 1) - vel.z) * ihy2; }
160 if (j < i32(P.ny) - 1) { lap += (DW(i, j + 1) - vel.z) * ihy2; }
161 acc -= 2.0 * SP.sigma0 * vel;
162 acc.z += 2.0 * SP.sigma1 * lap;
163 // In-plane (stretching) waves lie at several kHz; they get their own loss.
164 acc.x -= 2.0 * SP.sigmaIn * vel.x;
165 acc.y -= 2.0 * SP.sigmaIn * vel.y;
167 // Finger letting go: the reaction recorded at the end of the hold, ramped to zero.
168 if ((SP.flags & 4u) != 0u) { acc.z += react[k] * SP.releaseGain; }
170 var vNew = vel + P.dt * acc;
171 if (SP.audioSlot >= 0) { hist[u32(SP.audioSlot) * n + k] = vNew.z; }
172 let pOld = vec3f(posIn[k], posIn[n + k], posIn[2u * n + k]);
173 var pNew = pOld + P.dt * vNew;
174 // Compensated update of w: the increment dt * v can be far below the
175 // float32 resolution of w, so the rounding error is carried separately.
176 let y = P.dt * vNew.z + posIn[3u * n + k];
177 let t = pOld.z + y;
178 var lo = (pOld.z - t) + y;
179 pNew.z = t;
180 if ((SP.flags & 1u) != 0u) {
181 let dxp = nodeX(i) - P.pushX; let dyp = nodeY(j) - P.pushY;
182 let inPatch = dxp * dxp + dyp * dyp <= P.pushR2;
183 if ((SP.flags & 2u) != 0u) {
184 // Acceleration the constraint has to supply to hold this node.
185 var r = 0.0;
186 if (inPatch) { r = (SP.pushV - vNew.z) / P.dt; }
187 react[k] = r;
188 }
189 if (inPatch) { pNew.z = SP.pushW; vNew.z = SP.pushV; lo = 0.0; }
190 }
191 posOut[k] = pNew.x; posOut[n + k] = pNew.y; posOut[2u * n + k] = pNew.z; posOut[3u * n + k] = lo;
192 velOut[k] = vNew.x; velOut[n + k] = vNew.y; velOut[2u * n + k] = vNew.z;
193}