1// Physical and numerical parameters of the simulation, and the derived
2// quantities (grid, material constants, geometry of the excitation).
3// All values are SI.
5export type PlateShape = "rectangle" | "ellipse";
6export type BowShape = "none" | "width" | "height" | "dish";
7export type Excitation = "push" | "strike";
8export type HitSequence = "single" | "steady" | "crescendo" | "decrescendo";
10export interface PhysParams {
11 // Sheet
12 shape: PlateShape; // outline of the sheet within its bounding box
13 Lx: number; // width along x [m] (bounding box for an ellipse)
14 Ly: number; // height along y [m]
15 h: number; // thickness [m]
16 E: number; // Young's modulus [Pa]
17 nu: number; // Poisson ratio
18 rho: number; // density [kg/m^3]
19 holeInset: number; // distance of the thread hole from the edges near the top-left corner [m]
20 // Stress-free initial shape
21 bowShape: BowShape;
22 bowAmplitude: number; // sag of the initial shape at the center [m]
23 // Losses
24 sigma0: number; // frequency-independent loss of all motion [1/s]
25 sigma1: number; // loss proportional to the squared wavenumber, transverse motion [m^2/s]
26 sigmaIn: number; // additional frequency-independent loss of the in-plane motion [1/s]
27 // Excitation
28 excitation: Excitation;
29 pushAmount: number; // corner displacement [m]; negative pushes against the bow
30 fingerRadius: number; // radius of the pushed patch [m]
31 rampTime: number; // duration of the push ramp [s]
32 holdMax: number; // maximum settle time before the release [s]
33 releaseTime: number; // time over which the finger force falls to zero [s]
34 settleSigma0: number; // artificial damping during push and settle [1/s]
35 settleKE: number; // non-rigid kinetic energy below which the sheet counts as settled [J]
36 strikeFracX: number; // strike point as a fraction of the width from the left edge
37 strikeFracY: number; // strike point as a fraction of the height from the bottom edge
38 strikeVelocity: number; // stick tip speed at impact [m/s]; the loudest hit of a sequence
39 hitSequence: HitSequence; // one hit, or a series of hits every hitInterval seconds
40 hitCount: number; // number of hits in a series
41 hitInterval: number; // time between the hits of a series [s]
42 strikeVelocityMin: number; // softest stick speed of a crescendo or decrescendo [m/s]
43 stickMass: number; // effective mass of the stick [kg]
44 contactStiffness: number; // Hertz contact stiffness K [N/m^alpha]
45 contactExponent: number; // Hertz exponent alpha (1.5 for a rounded tip)
46 contactRadius: number; // radius of the contact patch [m]
47 // Listener
48 listenerDist: number; // [m]
49 listenerAzimuth: number; // [deg], rotation about the sheet's vertical axis (0 = on the normal)
50 listenerElevation: number; // [deg]
51 // Numerics and output
52 spacing: number; // requested grid spacing [m]
53 inPlaneMass: number; // factor by which the in-plane inertia is scaled; 1 is physical, larger lengthens the time step
54 rigidProjection: boolean; // remove the six rigid-body modes every RB_INTERVAL steps while ringing
55 fs: number; // audio sample rate [Hz]
56 fps: number; // movie frame rate, also the batch rate [Hz]
57 duration: number; // recorded duration after the release or the impact [s]
58}
60export const defaultParams: PhysParams = {
61 shape: "rectangle",
62 Lx: 12 * 0.0254,
63 Ly: 18 * 0.0254,
64 h: 0.79e-3,
65 E: 170e9,
66 nu: 0.29,
67 rho: 7900,
68 holeInset: 0.012,
69 bowShape: "width",
70 bowAmplitude: 3e-3,
71 sigma0: 1.15,
72 sigma1: 5e-4,
73 sigmaIn: 20,
74 excitation: "strike",
75 pushAmount: 0.01,
76 fingerRadius: 0.01,
77 rampTime: 0.1,
78 holdMax: 0.5,
79 releaseTime: 0.02,
80 settleSigma0: 300,
81 settleKE: 1e-8,
82 strikeFracX: 0.7,
83 strikeFracY: 0.35,
84 strikeVelocity: 3,
85 hitSequence: "single",
86 hitCount: 6,
87 hitInterval: 0.5,
88 strikeVelocityMin: 0.5,
89 stickMass: 0.03,
90 contactStiffness: 5e8,
91 contactExponent: 1.5,
92 contactRadius: 5e-3,
93 listenerDist: 1.0,
94 listenerAzimuth: 0,
95 listenerElevation: 0,
96 spacing: 0.008,
97 inPlaneMass: 1,
98 rigidProjection: false,
99 fs: 48000,
100 fps: 120,
101 duration: 4,
102};
104export const SPEED_OF_SOUND = 343; // [m/s]
105export const RHO_AIR = 1.2; // [kg/m^3]
106export const CFL = 0.9; // fraction of the stability limit used for the time step
107// The ring buffer of the retarded-time listener sum holds one batch of samples
108// plus this margin, which bounds the spread of the delays across the sheet.
109export const HIST_MARGIN = 128; // [samples]
110export const RB_INTERVAL = 32; // steps between rigid-body projections while ringing
111export const RB_INTERVAL_HELD = 16; // while the finger constraint is active
112// The held sheet counts as settled when its non-rigid kinetic energy drops
113// below settleKE, or when it has stopped falling: it has not halved over the
114// last SETTLE_WINDOW seconds (or 3 / settleSigma0, whichever is longer).
115export const SETTLE_WINDOW = 0.05; // [s]
116export const SETTLE_RATIO = 0.5;
118// ---------- Grid ----------
120export interface Grid {
121 nx: number;
122 ny: number;
123 hx: number;
124 hy: number;
125 n: number;
126 /** 1 for nodes that belong to the sheet, 0 otherwise; absent for a full rectangle. */
127 mask?: Uint8Array;
128}
130export function makeGrid(p: PhysParams): Grid {
131 const nx = Math.max(6, Math.round(p.Lx / p.spacing) + 1);
132 const ny = Math.max(6, Math.round(p.Ly / p.spacing) + 1);
133 const g: Grid = { nx, ny, hx: p.Lx / (nx - 1), hy: p.Ly / (ny - 1), n: nx * ny };
134 return p.shape === "ellipse" ? withEllipseMask(g) : g;
135}
137/**
138 * Restricts a grid to the ellipse inscribed in its bounding box. The edge is a
139 * staircase of whole cells: a cell belongs to the sheet when its center lies
140 * inside the ellipse, and a node when it is a corner of such a cell. Deciding
141 * by cell centers rather than by nodes keeps the covered area unbiased; a
142 * node-based mask loses about half a cell all around the edge, which raises
143 * every frequency by a few percent. A node with no cell of its own would carry
144 * no mass, and the trailing loop removes any such node.
145 */
146export function withEllipseMask(g: Grid): Grid {
147 const mask = new Uint8Array(g.n);
148 const a = ((g.nx - 1) * g.hx) / 2, b = ((g.ny - 1) * g.hy) / 2;
149 for (let cj = 0; cj < g.ny - 1; cj++) {
150 for (let ci = 0; ci < g.nx - 1; ci++) {
151 const X = (nodeX(g, ci) + g.hx / 2) / a, Y = (nodeY(g, cj) + g.hy / 2) / b;
152 if (X * X + Y * Y <= 1) {
153 mask[cj * g.nx + ci] = 1; mask[cj * g.nx + ci + 1] = 1;
154 mask[(cj + 1) * g.nx + ci] = 1; mask[(cj + 1) * g.nx + ci + 1] = 1;
155 }
156 }
157 }
158 const gm: Grid = { ...g, mask };
159 for (let changed = true; changed; ) {
160 changed = false;
161 for (let j = 0; j < g.ny; j++) {
162 for (let i = 0; i < g.nx; i++) {
163 if (mask[j * g.nx + i] && nodeWeight(gm, i, j) === 0) {
164 mask[j * g.nx + i] = 0;
165 changed = true;
166 }
167 }
168 }
169 }
170 return gm;
171}
173/** Node coordinates relative to the center of the bounding box. */
174export function nodeX(g: Grid, i: number): number {
175 return (i - (g.nx - 1) / 2) * g.hx;
176}
177export function nodeY(g: Grid, j: number): number {
178 return (j - (g.ny - 1) / 2) * g.hy;
179}
181/** True when node (i, j) exists and belongs to the sheet. */
182export function insideNode(g: Grid, i: number, j: number): boolean {
183 if (i < 0 || i >= g.nx || j < 0 || j >= g.ny) return false;
184 return g.mask ? g.mask[j * g.nx + i] !== 0 : true;
185}
187/** True when cell (ci, cj), with nodes (ci..ci+1, cj..cj+1), lies within the sheet. */
188export function insideCell(g: Grid, ci: number, cj: number): boolean {
189 return insideNode(g, ci, cj) && insideNode(g, ci + 1, cj) && insideNode(g, ci + 1, cj + 1) && insideNode(g, ci, cj + 1);
190}
192/**
193 * Quadrature and lumped-mass weight of a node: the fraction of its four
194 * surrounding cells that belong to the sheet. On a full rectangle this is the
195 * trapezoidal rule (1 inside, 1/2 on an edge, 1/4 at a corner). Zero outside.
196 */
197export function nodeWeight(g: Grid, i: number, j: number): number {
198 if (!insideNode(g, i, j)) return 0;
199 let c = 0;
200 if (insideCell(g, i, j)) c++;
201 if (insideCell(g, i - 1, j)) c++;
202 if (insideCell(g, i, j - 1)) c++;
203 if (insideCell(g, i - 1, j - 1)) c++;
204 return c / 4;
205}
207/** Per-node weights as a flat array. */
208export function nodeWeights(g: Grid): Float32Array<ArrayBuffer> {
209 const w = new Float32Array(g.n);
210 for (let j = 0; j < g.ny; j++) for (let i = 0; i < g.nx; i++) w[j * g.nx + i] = nodeWeight(g, i, j);
211 return w;
212}
214/** Number of nodes in the sheet. */
215export function countInside(g: Grid): number {
216 if (!g.mask) return g.n;
217 let c = 0;
218 for (let k = 0; k < g.n; k++) c += g.mask[k];
219 return c;
220}
222// ---------- Material ----------
224export interface Material {
225 D: number; // bending stiffness E h^3 / (12 (1 - nu^2)) [N m]
226 C: number; // membrane stiffness E h / (1 - nu^2) [N/m]
227 G: number; // membrane shear stiffness E h / (2 (1 + nu)) [N/m]
228 nu: number;
229 rhoh: number; // mass per unit area [kg/m^2]
230}
232export function makeMaterial(p: PhysParams): Material {
233 return {
234 D: (p.E * p.h ** 3) / (12 * (1 - p.nu * p.nu)),
235 C: (p.E * p.h) / (1 - p.nu * p.nu),
236 G: (p.E * p.h) / (2 * (1 + p.nu)),
237 nu: p.nu,
238 rhoh: p.rho * p.h,
239 };
240}
242/**
243 * 60 dB decay time [s] of a bending mode of angular frequency omega under the
244 * loss model sigma0 + sigma1 k^2, using the flat-plate dispersion k^2 = omega / kappa.
245 */
246export function t60(m: Material, p: PhysParams, omega: number): number {
247 const kappa = Math.sqrt(m.D / m.rhoh);
248 const sigma = p.sigma0 + p.sigma1 * (omega / kappa);
249 return (3 * Math.LN10) / sigma;
250}
252/**
253 * Frequency [Hz] at which the three-point second difference lowers a bending
254 * frequency by the relative amount `err` on a grid of spacing hmax. A plane
255 * bending wave along a grid axis with wavenumber k has the discrete frequency
256 * ratio (2 - 2 cos kh) / (kh)^2.
257 */
258export function dispersionFrequency(m: Material, g: Grid, err: number): number {
259 const h = Math.max(g.hx, g.hy);
260 let lo = 0, hi = Math.PI;
261 for (let it = 0; it < 60; it++) {
262 const x = (lo + hi) / 2;
263 if ((2 - 2 * Math.cos(x)) / (x * x) > 1 - err) lo = x; else hi = x;
264 }
265 const k = lo / h;
266 const kappa = Math.sqrt(m.D / m.rhoh);
267 return (kappa * k * k) / (2 * Math.PI);
268}
270// ---------- Geometry of the excitation ----------
272/** One drumstick hit: time after the first impact [s] and stick speed [m/s]. */
273export interface Hit {
274 t: number;
275 v: number;
276}
278/**
279 * Schedule of the drumstick hits. The first hit is always at t = 0. A series
280 * has hitCount hits every hitInterval seconds, all at strikeVelocity for
281 * "steady", rising from strikeVelocityMin to strikeVelocity for "crescendo",
282 * and falling for "decrescendo".
283 */
284export function hitSchedule(p: PhysParams): Hit[] {
285 if (p.hitSequence === "single") return [{ t: 0, v: p.strikeVelocity }];
286 const count = Math.max(1, Math.round(p.hitCount));
287 const dt = Math.max(0, p.hitInterval);
288 const lo = Math.min(p.strikeVelocityMin, p.strikeVelocity), hi = p.strikeVelocity;
289 const hits: Hit[] = [];
290 for (let k = 0; k < count; k++) {
291 const f = count > 1 ? k / (count - 1) : 1;
292 let v = hi;
293 if (p.hitSequence === "crescendo") v = lo + (hi - lo) * f;
294 else if (p.hitSequence === "decrescendo") v = hi - (hi - lo) * f;
295 hits.push({ t: k * dt, v });
296 }
297 return hits;
298}
300/** Strike point in sheet coordinates. */
301export function strikePoint(p: PhysParams): [number, number] {
302 return [(p.strikeFracX - 0.5) * p.Lx, (p.strikeFracY - 0.5) * p.Ly];
303}
305/**
306 * Center of the thread hole. Rectangle: inset from the two edges of the
307 * top-left corner. Ellipse: inset along the radius from the rim point in the
308 * top-left direction (parametric angle 135 degrees).
309 */
310export function holePoint(p: PhysParams): [number, number] {
311 if (p.shape === "ellipse") {
312 const c = Math.SQRT1_2;
313 return [-(p.Lx / 2 - p.holeInset) * c, (p.Ly / 2 - p.holeInset) * c];
314 }
315 return [-p.Lx / 2 + p.holeInset, p.Ly / 2 - p.holeInset];
316}
318/** Center of the pushed patch: the bottom-right corner, or the rim point opposite the hole. */
319export function pushPoint(p: PhysParams): [number, number] {
320 if (p.shape === "ellipse") {
321 const c = Math.SQRT1_2;
322 return [(p.Lx / 2) * c, -(p.Ly / 2) * c];
323 }
324 return [p.Lx / 2, -p.Ly / 2];
325}
327/**
328 * Stress-free initial shape w0 at every node. All three shapes are parabolic
329 * with the given sag at the center of the bounding box: "width" and "height"
330 * are cylindrical with zero sag along the two opposite edges, and "dish" has
331 * the same curvature in both directions, so its sag is measured relative to
332 * the corners (the edge midpoints sit at half the sag).
333 */
334export function initialShape(p: PhysParams, g: Grid): Float32Array<ArrayBuffer> {
335 const w0 = new Float32Array(g.n);
336 if (p.bowShape === "none" || p.bowAmplitude === 0) return w0;
337 for (let j = 0; j < g.ny; j++) {
338 for (let i = 0; i < g.nx; i++) {
339 const X = (2 * nodeX(g, i)) / p.Lx; // -1..1
340 const Y = (2 * nodeY(g, j)) / p.Ly;
341 let f = 0;
342 if (p.bowShape === "width") f = 1 - X * X;
343 else if (p.bowShape === "height") f = 1 - Y * Y;
344 else f = 1 - 0.5 * (X * X + Y * Y);
345 w0[j * g.nx + i] = p.bowAmplitude * f;
346 }
347 }
348 return w0;
349}
351/**
352 * Nodes of the drumstick contact patch: the sheet node nearest the strike
353 * point, plus every sheet node within the contact radius. The contact force
354 * is spread over these nodes in proportion to their mass, and the contact
355 * coordinate is their mass-weighted mean displacement, so the two are
356 * conjugate and the contact has a potential energy. The mass-weighted center
357 * of the patch is where the inertia-relief reaction to the force is taken.
358 */
359export function contactPatch(p: PhysParams, g: Grid): { nodes: number[]; nearest: number; mass: number; center: [number, number]; box: [number, number, number, number] } {
360 const m = makeMaterial(p);
361 const [sx, sy] = strikePoint(p);
362 let nearest = -1, best = Infinity;
363 for (let j = 0; j < g.ny; j++) {
364 for (let i = 0; i < g.nx; i++) {
365 if (nodeWeight(g, i, j) === 0) continue;
366 const d2 = (nodeX(g, i) - sx) ** 2 + (nodeY(g, j) - sy) ** 2;
367 if (d2 < best) { best = d2; nearest = j * g.nx + i; }
368 }
369 }
370 const nodes: number[] = [];
371 let mass = 0, cx = 0, cy = 0;
372 const box: [number, number, number, number] = [g.nx, -1, g.ny, -1]; // i0, i1, j0, j1 (inclusive)
373 for (let j = 0; j < g.ny; j++) {
374 for (let i = 0; i < g.nx; i++) {
375 const k = j * g.nx + i;
376 const wgt = nodeWeight(g, i, j);
377 if (wgt === 0) continue;
378 const d2 = (nodeX(g, i) - sx) ** 2 + (nodeY(g, j) - sy) ** 2;
379 if (d2 <= p.contactRadius ** 2 || k === nearest) {
380 nodes.push(k);
381 const mk = m.rhoh * g.hx * g.hy * wgt;
382 mass += mk; cx += mk * nodeX(g, i); cy += mk * nodeY(g, j);
383 box[0] = Math.min(box[0], i); box[1] = Math.max(box[1], i);
384 box[2] = Math.min(box[2], j); box[3] = Math.max(box[3], j);
385 }
386 }
387 }
388 return { nodes, nearest, mass, center: [cx / mass, cy / mass], box };
389}