// One explicit time step (symplectic Euler) of the von Karman plate. // // State layout (structure of arrays, n = nx * ny nodes): // pos: [u | v | w | wLo] wLo carries the rounding error of the w update // vel: [du | dv | dw] // A workgroup of 16x16 threads advances a 12x12 tile of nodes in three // phases separated by barriers. (1) Every thread loads one entry of the tile // plus its 2-node halo (16x16 values of w, u, v, w0, w_t and the node // weights) into shared memory. (2) The threads compute, once each and again // into shared memory, the membrane forces of the 13x13 cells around the tile // and the weighted bending moments and mixed differences that the bending // stencil gathers. (3) Each of the 144 tile nodes evaluates its update from // shared memory alone. On grids of a few thousand nodes the kernel is bound // by the latency of one workgroup rather than by throughput, so the shared // phases, which replace a tenfold per-node recomputation of the stencil // ingredients, are what make a step cheap. // // Nodes outside the sheet (weight 0) are skipped: a second difference exists // only where the node and both neighbors belong to the sheet, and a cell // contributes only when its four corners do. That reproduces the free edge of // the rectangle and gives a staircase free edge for an ellipse. @group(0) @binding(0) var P: Params; @group(0) @binding(1) var SP: StepParams; @group(0) @binding(2) var posIn: array; @group(0) @binding(3) var velIn: array; @group(0) @binding(4) var posOut: array; @group(0) @binding(5) var velOut: array; @group(0) @binding(6) var hist: array; // [slot * n + node]: transverse velocity at the audio sample instants // Constraint reaction (acceleration of w) per node, followed by two 4-float // slots of drumstick state (position, velocity, force, max force). @group(0) @binding(7) var react: array; @group(0) @binding(8) var w0: array; // stress-free initial shape const TILE = 12; // nodes per workgroup along each axis (must match simulation.ts) const HALO = 2; const TW = 16; // TILE + 2 * HALO: the loaded tile, one thread per entry const CT = 13; // TILE + 1: cells adjacent to the tile's nodes const MT = 14; // TILE + 2: nodes at which the bending moments are needed // Tile with halo, local index (j - oy) * TW + (i - ox). var tw: array; // w var tu: array; // u var tv: array; // v var tw0: array; // stress-free shape var tdw: array; // w_t var tn: array; // node weights in quarters (0..4), four to a word // Membrane force contributions of the CT x CT cells, 4 nodes x 3 components each. var cellF: array; // Weighted bending moments at the MT x MT inner nodes and mixed differences at the CT x CT cells. var ma: array; var mb: array; var cc: array; // Tile origin in global node coordinates (set once per invocation). var ox: i32; var oy: i32; var ihx2: f32; var ihy2: f32; var ihxy: f32; fn id(i: i32, j: i32) -> u32 { return u32(j) * P.nx + u32(i); } fn lidx(i: i32, j: i32) -> u32 { return u32(j - oy) * u32(TW) + u32(i - ox); } fn midx(i: i32, j: i32) -> u32 { return u32(j - oy - 1) * u32(MT) + u32(i - ox - 1); } fn cidx(ci: i32, cj: i32) -> u32 { return u32(cj - oy - 1) * u32(CT) + u32(ci - ox - 1); } // Tile lookups with global indices; the tile covers [ox, ox + TW) x [oy, oy + TW). fn W(i: i32, j: i32) -> f32 { return tw[lidx(i, j)]; } fn U(i: i32, j: i32) -> f32 { return tu[lidx(i, j)]; } fn V(i: i32, j: i32) -> f32 { return tv[lidx(i, j)]; } fn W0(i: i32, j: i32) -> f32 { return tw0[lidx(i, j)]; } fn DW(i: i32, j: i32) -> f32 { return tdw[lidx(i, j)]; } fn NWq(i: i32, j: i32) -> u32 { let l = lidx(i, j); return (tn[l >> 2u] >> ((l & 3u) * 8u)) & 0xFFu; } fn NW(i: i32, j: i32) -> f32 { return 0.25 * f32(NWq(i, j)); } fn INS(i: i32, j: i32) -> bool { return NWq(i, j) != 0u; } fn insCell(ci: i32, cj: i32) -> bool { return INS(ci, cj) && INS(ci + 1, cj) && INS(ci + 1, cj + 1) && INS(ci, cj + 1); } // Graph Laplacian of the transverse velocity at node (i, j), over neighbors in the sheet. fn lapDW(i: i32, j: i32) -> f32 { let c = DW(i, j); var lap = 0.0; if (INS(i - 1, j)) { lap += (DW(i - 1, j) - c) * ihx2; } if (INS(i + 1, j)) { lap += (DW(i + 1, j) - c) * ihx2; } if (INS(i, j - 1)) { lap += (DW(i, j - 1) - c) * ihy2; } if (INS(i, j + 1)) { lap += (DW(i, j + 1) - c) * ihy2; } return lap; } // Weighted bending moments at nodes; zero where the second difference is undefined. fn Ma(i: i32, j: i32) -> f32 { if (!(INS(i - 1, j) && INS(i, j) && INS(i + 1, j))) { return 0.0; } let a = (W(i + 1, j) - 2.0 * W(i, j) + W(i - 1, j)) * ihx2; var b = 0.0; if (INS(i, j - 1) && INS(i, j + 1)) { b = (W(i, j + 1) - 2.0 * W(i, j) + W(i, j - 1)) * ihy2; } return NW(i, j) * (a + P.nu * b); } fn Mb(i: i32, j: i32) -> f32 { if (!(INS(i, j - 1) && INS(i, j) && INS(i, j + 1))) { return 0.0; } let b = (W(i, j + 1) - 2.0 * W(i, j) + W(i, j - 1)) * ihy2; var a = 0.0; if (INS(i - 1, j) && INS(i + 1, j)) { a = (W(i + 1, j) - 2.0 * W(i, j) + W(i - 1, j)) * ihx2; } return NW(i, j) * (b + P.nu * a); } // Mixed second difference at a cell center; zero outside the sheet. fn Cc(ci: i32, cj: i32) -> f32 { if (!insCell(ci, cj)) { return 0.0; } return (W(ci + 1, cj + 1) - W(ci + 1, cj) - W(ci, cj + 1) + W(ci, cj)) * ihxy; } // Membrane forces of the bilinear cell (ci, cj) on its four nodes (2x2 Gauss), // written to cellF at local cell index lc. fn cellForces(ci: i32, cj: i32, lc: u32) { let base = lc * 12u; if (!insCell(ci, cj)) { for (var q = 0u; q < 12u; q++) { cellF[base + q] = 0.0; } return; } let Un = vec4f(U(ci, cj), U(ci + 1, cj), U(ci + 1, cj + 1), U(ci, cj + 1)); let Vn = vec4f(V(ci, cj), V(ci + 1, cj), V(ci + 1, cj + 1), V(ci, cj + 1)); let W0n = vec4f(W0(ci, cj), W0(ci + 1, cj), W0(ci + 1, cj + 1), W0(ci, cj + 1)); let Wn = vec4f(W(ci, cj), W(ci + 1, cj), W(ci + 1, cj + 1), W(ci, cj + 1)) + W0n; // total shape let q = 0.25 * P.hx * P.hy; let ihx = 1.0 / P.hx; let ihy = 1.0 / P.hy; let g3 = 0.57735026918962576; var Fu = vec4f(0.0); var Fv = vec4f(0.0); var Fw = vec4f(0.0); for (var gp = 0; gp < 4; gp++) { var xi = -g3; if ((gp & 1) != 0) { xi = g3; } var eta = -g3; if ((gp & 2) != 0) { eta = g3; } let dNdx = vec4f(-(1.0 - eta), 1.0 - eta, 1.0 + eta, -(1.0 + eta)) * (0.5 * ihx); let dNdy = vec4f(-(1.0 - xi), -(1.0 + xi), 1.0 + xi, 1.0 - xi) * (0.5 * ihy); let ux = dot(dNdx, Un); let uy = dot(dNdy, Un); let vx = dot(dNdx, Vn); let vy = dot(dNdy, Vn); let wx = dot(dNdx, Wn); let wy = dot(dNdy, Wn); let w0x = dot(dNdx, W0n); let w0y = dot(dNdy, W0n); // Marguerre strains: total slope minus the stress-free slope. let exx = ux + 0.5 * (wx * wx - w0x * w0x); let eyy = vy + 0.5 * (wy * wy - w0y * w0y); let gxy = uy + vx + wx * wy - w0x * w0y; let Nxx = P.C * (exx + P.nu * eyy); let Nyy = P.C * (eyy + P.nu * exx); let Nxy = P.G * gxy; Fu -= q * (Nxx * dNdx + Nxy * dNdy); Fv -= q * (Nyy * dNdy + Nxy * dNdx); Fw -= q * ((Nxx * wx + Nxy * wy) * dNdx + (Nxy * wx + Nyy * wy) * dNdy); } for (var a = 0u; a < 4u; a++) { cellF[base + a * 3u] = Fu[a]; cellF[base + a * 3u + 1u] = Fv[a]; cellF[base + a * 3u + 2u] = Fw[a]; } } // True for the sheet nodes of the drumstick contact patch (same rule as contactPatch in params.ts). fn inContactPatch(i: i32, j: i32) -> bool { if (nodeWeight(i, j) == 0.0) { return false; } let dxs = nodeX(i) - P.strikeX; let dys = nodeY(j) - P.strikeY; return dxs * dxs + dys * dys <= P.strikeR2 || id(i, j) == P.strikeNode; } // Stored membrane force on local node `a` of cell (ci, cj). fn cellForce(ci: i32, cj: i32, a: u32) -> vec3f { let base = cidx(ci, cj) * 12u + a * 3u; return vec3f(cellF[base], cellF[base + 1u], cellF[base + 2u]); } @compute @workgroup_size(16, 16) fn main(@builtin(local_invocation_id) lid: vec3u, @builtin(workgroup_id) wid: vec3u) { let n = P.n; ox = i32(wid.x) * TILE - HALO; oy = i32(wid.y) * TILE - HALO; ihx2 = 1.0 / (P.hx * P.hx); ihy2 = 1.0 / (P.hy * P.hy); ihxy = 1.0 / (P.hx * P.hy); let lt = lid.y * 16u + lid.x; // Phase 1: one entry of the tile with halo per thread. { let gi = ox + i32(lid.x); let gj = oy + i32(lid.y); var ww = 0.0; var uu = 0.0; var vv = 0.0; var z0 = 0.0; var dw = 0.0; if (gi >= 0 && gi < i32(P.nx) && gj >= 0 && gj < i32(P.ny)) { let k = id(gi, gj); ww = posIn[2u * n + k]; uu = posIn[k]; vv = posIn[n + k]; z0 = w0[k]; dw = velIn[2u * n + k]; } tw[lt] = ww; tu[lt] = uu; tv[lt] = vv; tw0[lt] = z0; tdw[lt] = dw; if (lt < 64u) { var word = 0u; for (var q = 0u; q < 4u; q++) { let e = lt * 4u + q; let ei = ox + i32(e % u32(TW)); let ej = oy + i32(e / u32(TW)); var wq = 0u; if (ei >= 0 && ei < i32(P.nx) && ej >= 0 && ej < i32(P.ny)) { wq = u32(round(4.0 * nw[id(ei, ej)])); } word |= wq << (q * 8u); } tn[lt] = word; } } workgroupBarrier(); // Phase 2: membrane forces of the CT x CT cells, bending moments at the MT x MT // inner nodes, mixed differences at the cells; one item per thread each. if (lt < 169u) { let ci = ox + 1 + i32(lt % u32(CT)); let cj = oy + 1 + i32(lt / u32(CT)); cellForces(ci, cj, lt); cc[lt] = Cc(ci, cj); } if (lt < 196u) { let mi = ox + 1 + i32(lt % u32(MT)); let mj = oy + 1 + i32(lt / u32(MT)); ma[lt] = Ma(mi, mj); mb[lt] = Mb(mi, mj); } workgroupBarrier(); // Phase 3: the update of the TILE x TILE nodes (no barriers past this point). if (lt >= 144u) { return; } let i = ox + HALO + i32(lt % u32(TILE)); let j = oy + HALO + i32(lt / u32(TILE)); if (i >= i32(P.nx) || j >= i32(P.ny)) { return; } let k = id(i, j); let weight = NW(i, j); if (weight == 0.0) { // Outside the sheet: the node stays at rest. posOut[k] = 0.0; posOut[n + k] = 0.0; posOut[2u * n + k] = 0.0; posOut[3u * n + k] = 0.0; velOut[k] = 0.0; velOut[n + k] = 0.0; velOut[2u * n + k] = 0.0; if ((SP.flags & 2u) != 0u) { react[k] = 0.0; } return; } // Elastic forces: bending from the discrete Kirchhoff energy, membrane from the cells. let bend = (ma[midx(i - 1, j)] - 2.0 * ma[midx(i, j)] + ma[midx(i + 1, j)]) * ihx2 + (mb[midx(i, j - 1)] - 2.0 * mb[midx(i, j)] + mb[midx(i, j + 1)]) * ihy2 + 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; var F = vec3f(0.0, 0.0, -P.D * P.hx * P.hy * bend); F += cellForce(i, j, 0u); F += cellForce(i - 1, j, 1u); F += cellForce(i - 1, j - 1, 2u); F += cellForce(i, j - 1, 3u); let mass = P.rhoh * P.hx * P.hy * weight; var acc = F * (1.0 / mass); // Optional scaling of the in-plane inertia (lengthens the stable time step). acc.x *= P.invInPlaneMass; acc.y *= P.invInPlaneMass; // Losses: frequency-independent on all components, the graph Laplacian of the // transverse velocity (~k^2), and an extra frequency-independent in-plane loss. let vel = vec3f(velIn[k], velIn[n + k], velIn[2u * n + k]); acc -= 2.0 * SP.sigma0 * vel; acc.z += 2.0 * SP.sigma1 * lapDW(i, j); acc.x -= 2.0 * SP.sigmaIn * vel.x; acc.y -= 2.0 * SP.sigmaIn * vel.y; // Finger letting go: the reaction recorded at the end of the hold, ramped to zero. if ((SP.flags & 4u) != 0u) { acc.z += react[k] * SP.releaseGain; } // Drumstick: Hertz contact between a free mass and the contact patch (the // sheet nodes within the tip radius, plus the nearest one). The contact // coordinate is the mass-weighted mean displacement of the patch, and the // force is spread over the patch in proportion to the node masses, so the // pair is conjugate and the contact has a potential energy. if ((SP.flags & 8u) != 0u) { let sIn = n + 4u * SP.parity; // Inertia relief: the force of the previous step, applied to the whole sheet // as the rigid acceleration field that balances the patch load and its // moments about the center, so the sheet gains no net momentum (the thread // would take it up in reality). The one-step lag integrates to zero. let Fprev = react[sIn + 2u]; acc.z += Fprev * (1.0 / P.mass + nodeY(j) * P.patchCY / P.Ix + nodeX(i) * P.patchCX / P.Iy); if (inContactPatch(i, j)) { let sx = react[sIn]; let sv = react[sIn + 1u]; let sMax = react[sIn + 3u]; var wc = 0.0; for (var pj = i32(P.patchJ0); pj <= i32(P.patchJ1); pj++) { for (var pi = i32(P.patchI0); pi <= i32(P.patchI1); pi++) { if (inContactPatch(pi, pj)) { wc += P.rhoh * P.hx * P.hy * nodeWeight(pi, pj) * posIn[2u * n + id(pi, pj)]; } } } wc /= P.patchMass; let pen = wc - sx; // the stick sits on the +z side and moves toward -z var Fc = 0.0; if (pen > 0.0) { Fc = P.K * pow(pen, P.alpha); } acc.z -= Fc / P.patchMass; if (k == P.strikeNode) { let sOut = n + 4u * (1u - SP.parity); if ((SP.flags & 16u) != 0u) { // Next hit of a series: a fresh stick arrives at the patch, wherever // it is at this instant, with the scheduled speed; the previous // stick's rebound is discarded. react[sOut] = wc; react[sOut + 1u] = -SP.strikeV; react[sOut + 2u] = 0.0; react[sOut + 3u] = sMax; } else { let svn = sv + P.dt * Fc / P.stickMass; react[sOut] = sx + P.dt * svn; react[sOut + 1u] = svn; react[sOut + 2u] = Fc; react[sOut + 3u] = max(sMax, Fc); } } } } var vNew = vel + P.dt * acc; if (SP.audioSlot >= 0) { hist[u32(SP.audioSlot) * n + k] = vNew.z; } let pOld = vec3f(posIn[k], posIn[n + k], posIn[2u * n + k]); var pNew = pOld + P.dt * vNew; // Compensated update of w: the increment dt * v can be far below the // float32 resolution of w, so the rounding error is carried separately. let y = P.dt * vNew.z + posIn[3u * n + k]; let t = pOld.z + y; var lo = (pOld.z - t) + y; pNew.z = t; if ((SP.flags & 1u) != 0u) { let dxp = nodeX(i) - P.pushX; let dyp = nodeY(j) - P.pushY; let inPatch = dxp * dxp + dyp * dyp <= P.pushR2; if ((SP.flags & 2u) != 0u) { // Acceleration the constraint has to supply to hold this node. var r = 0.0; if (inPatch) { r = (SP.pushV - vNew.z) / P.dt; } react[k] = r; } if (inPatch) { pNew.z = SP.pushW; vNew.z = SP.pushV; lo = 0.0; } } posOut[k] = pNew.x; posOut[n + k] = pNew.y; posOut[2u * n + k] = pNew.z; posOut[3u * n + k] = lo; velOut[k] = vNew.x; velOut[n + k] = vNew.y; velOut[2u * n + k] = vNew.z; }