/ proof-of-concept / steelsim41
Sign in
proof-of-concept / steelsim41
steelsim41 / src / params.ts
383 lines · 14.3 KBBlameHistoryRaw
1// Simulation parameters (strike, listener, numerics), the grid with its mask,
2// the plate as evaluated on the grid (material, thickness field, stress-free
3// shape, mount), and the derived quantities the kernel needs. All values are
4// SI. The cymbal design itself is produced by the script in design.ts.
6export interface SimParams {
7 // Strike
8 strikeFracX: number; // strike point as a fraction of the bounding-box width from the left edge
9 strikeFracY: number; // strike point as a fraction of the bounding-box height from the bottom edge
10 strikeVelocity: number; // stick tip speed at impact [m/s]
11 stickMass: number; // effective mass of the stick [kg]
12 contactStiffness: number; // Hertz contact stiffness K [N/m^alpha]
13 contactExponent: number; // Hertz exponent alpha (1.5 for a rounded tip)
14 contactRadius: number; // radius of the contact patch [m]
15 // Listener
16 listenerDist: number; // [m]
17 listenerAzimuth: number; // [deg], rotation about the plate's vertical axis (0 = on the normal)
18 listenerElevation: number; // [deg]
19 // Numerics and output
20 spacing: number; // requested grid spacing [m]
21 inPlaneMass: number; // factor by which the in-plane inertia is scaled; 1 is physical, larger lengthens the time step
22 rigidProjection: boolean; // remove the six rigid-body modes every RB_INTERVAL steps (free plates only)
23 fs: number; // audio sample rate [Hz]
24 fps: number; // movie frame rate, also the batch rate [Hz]
25 duration: number; // recorded duration after the impact [s]
28export const defaultParams: SimParams = {
29 strikeFracX: 0.7,
30 strikeFracY: 0.5,
31 strikeVelocity: 3,
32 stickMass: 0.03,
33 contactStiffness: 5e8,
34 contactExponent: 1.5,
35 contactRadius: 5e-3,
36 listenerDist: 1.0,
37 listenerAzimuth: 0,
38 listenerElevation: 0,
39 spacing: 0.008,
40 inPlaneMass: 1,
41 rigidProjection: false,
42 fs: 48000,
43 fps: 120,
44 duration: 4,
45};
47export const SPEED_OF_SOUND = 343; // [m/s]
48export const RHO_AIR = 1.2; // [kg/m^3]
49export const CFL = 0.9; // fraction of the stability limit used for the time step
50// The ring buffer of the retarded-time listener sum holds one batch of samples
51// plus this margin, which bounds the spread of the delays across the plate.
52export const HIST_MARGIN = 128; // [samples]
53export const RB_INTERVAL = 32; // steps between rigid-body projections
55// ---------- Grid ----------
57export interface Grid {
58 nx: number;
59 ny: number;
60 hx: number;
61 hy: number;
62 n: number;
63 /** 1 for nodes that belong to the plate, 0 otherwise; absent for a full rectangle. */
64 mask?: Uint8Array;
67/** True when the point (x, y), relative to the center of the bounding box, lies in the plate. */
68export type InsideFn = (x: number, y: number) => boolean;
70/**
71 * The grid covering the bounding box Lx by Ly with about the requested
72 * spacing, restricted to the outline `inside` when one is given.
73 */
74export function makeGrid(Lx: number, Ly: number, spacing: number, inside?: InsideFn): Grid {
75 const nx = Math.max(6, Math.round(Lx / spacing) + 1);
76 const ny = Math.max(6, Math.round(Ly / spacing) + 1);
77 const g: Grid = { nx, ny, hx: Lx / (nx - 1), hy: Ly / (ny - 1), n: nx * ny };
78 return inside ? withMask(g, inside) : g;
81/**
82 * Restricts a grid to an outline. The edge is a staircase of whole cells: a
83 * cell belongs to the plate when its center lies inside the outline, and a
84 * node when it is a corner of such a cell. Deciding by cell centers rather
85 * than by nodes keeps the covered area unbiased; a node-based mask loses
86 * about half a cell all around the edge, which raises every frequency by a
87 * few percent. A node with no cell of its own would carry no mass, and the
88 * trailing loop removes any such node. A hole in the outline (a cymbal's
89 * center hole) is handled by the same rule.
90 */
91export function withMask(g: Grid, inside: InsideFn): Grid {
92 const mask = new Uint8Array(g.n);
93 for (let cj = 0; cj < g.ny - 1; cj++) {
94 for (let ci = 0; ci < g.nx - 1; ci++) {
95 if (inside(nodeX(g, ci) + g.hx / 2, nodeY(g, cj) + g.hy / 2)) {
96 mask[cj * g.nx + ci] = 1; mask[cj * g.nx + ci + 1] = 1;
97 mask[(cj + 1) * g.nx + ci] = 1; mask[(cj + 1) * g.nx + ci + 1] = 1;
98 }
99 }
100 }
101 const gm: Grid = { ...g, mask };
102 for (let changed = true; changed; ) {
103 changed = false;
104 for (let j = 0; j < g.ny; j++) {
105 for (let i = 0; i < g.nx; i++) {
106 if (mask[j * g.nx + i] && nodeWeight(gm, i, j) === 0) {
107 mask[j * g.nx + i] = 0;
108 changed = true;
109 }
110 }
111 }
112 }
113 return gm;
116/** The ellipse inscribed in the bounding box (kept for the tests of the discretization). */
117export function withEllipseMask(g: Grid): Grid {
118 const a = ((g.nx - 1) * g.hx) / 2, b = ((g.ny - 1) * g.hy) / 2;
119 return withMask(g, (x, y) => (x / a) ** 2 + (y / b) ** 2 <= 1);
122/** Node coordinates relative to the center of the bounding box. */
123export function nodeX(g: Grid, i: number): number {
124 return (i - (g.nx - 1) / 2) * g.hx;
126export function nodeY(g: Grid, j: number): number {
127 return (j - (g.ny - 1) / 2) * g.hy;
130/** True when node (i, j) exists and belongs to the plate. */
131export function insideNode(g: Grid, i: number, j: number): boolean {
132 if (i < 0 || i >= g.nx || j < 0 || j >= g.ny) return false;
133 return g.mask ? g.mask[j * g.nx + i] !== 0 : true;
136/** True when cell (ci, cj), with nodes (ci..ci+1, cj..cj+1), lies within the plate. */
137export function insideCell(g: Grid, ci: number, cj: number): boolean {
138 return insideNode(g, ci, cj) && insideNode(g, ci + 1, cj) && insideNode(g, ci + 1, cj + 1) && insideNode(g, ci, cj + 1);
141/**
142 * Quadrature and lumped-mass weight of a node: the fraction of its four
143 * surrounding cells that belong to the plate. On a full rectangle this is the
144 * trapezoidal rule (1 inside, 1/2 on an edge, 1/4 at a corner). Zero outside.
145 */
146export function nodeWeight(g: Grid, i: number, j: number): number {
147 if (!insideNode(g, i, j)) return 0;
148 let c = 0;
149 if (insideCell(g, i, j)) c++;
150 if (insideCell(g, i - 1, j)) c++;
151 if (insideCell(g, i, j - 1)) c++;
152 if (insideCell(g, i - 1, j - 1)) c++;
153 return c / 4;
156/** Per-node weights as a flat array. */
157export function nodeWeights(g: Grid): Float32Array<ArrayBuffer> {
158 const w = new Float32Array(g.n);
159 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);
160 return w;
163/** Per-cell membership (1 or 0) as a flat array of (nx - 1) * (ny - 1). */
164export function cellMask(g: Grid): Float32Array<ArrayBuffer> {
165 const c = new Float32Array((g.nx - 1) * (g.ny - 1));
166 for (let cj = 0; cj < g.ny - 1; cj++) for (let ci = 0; ci < g.nx - 1; ci++) c[cj * (g.nx - 1) + ci] = insideCell(g, ci, cj) ? 1 : 0;
167 return c;
170/** Number of nodes in the plate. */
171export function countInside(g: Grid): number {
172 if (!g.mask) return g.n;
173 let c = 0;
174 for (let k = 0; k < g.n; k++) c += g.mask[k];
175 return c;
178// ---------- Material and plate ----------
180export interface Material {
181 E: number; // Young's modulus [Pa]
182 nu: number; // Poisson ratio
183 rho: number; // density [kg/m^3]
186/** Losses of the plate (see THEORY.md). */
187export interface Loss {
188 sigma0: number; // frequency-independent loss of all motion [1/s]
189 sigma1: number; // loss proportional to the squared wavenumber, transverse motion [m^2/s]
190 sigmaIn: number; // additional frequency-independent loss of the in-plane motion [1/s]
193/**
194 * The stand: the plate nodes within `radius` of (x, y) are tied to the ground
195 * by a spring of total stiffness `stiffness` and a dashpot of total rate
196 * `damping`, both spread over the nodes in proportion to their weight and
197 * acting on all three displacement components.
198 */
199export interface Mount {
200 x: number; // [m] from the center of the bounding box
201 y: number;
202 radius: number; // [m]
203 stiffness: number; // [N/m]
204 damping: number; // [N s/m]
207/** A design evaluated on a grid: everything the simulation needs to know about the plate. */
208export interface Plate {
209 name: string;
210 Lx: number; // bounding box [m]
211 Ly: number;
212 grid: Grid;
213 mat: Material;
214 /** Thickness at every node [m]; zero outside the plate. */
215 h: Float64Array;
216 /** Stress-free shape at every node [m], mean plane removed; zero outside the plate. */
217 w0: Float64Array;
218 mount: Mount | null;
219 loss: Loss;
222/** Bending stiffness [N m] of a plate of thickness h. */
223export function bendingStiffness(m: Material, h: number): number {
224 return (m.E * h ** 3) / (12 * (1 - m.nu * m.nu));
226/** Membrane stiffness E h / (1 - nu^2) [N/m]. */
227export function membraneStiffness(m: Material, h: number): number {
228 return (m.E * h) / (1 - m.nu * m.nu);
230/** Membrane shear stiffness E h / (2 (1 + nu)) [N/m]. */
231export function shearStiffness(m: Material, h: number): number {
232 return (m.E * h) / (2 * (1 + m.nu));
235/** Mass of node (i, j) [kg]: density, local thickness, cell area, and the node's weight. */
236export function nodeMass(pl: Plate, i: number, j: number): number {
237 const g = pl.grid;
238 return pl.mat.rho * pl.h[j * g.nx + i] * g.hx * g.hy * nodeWeight(g, i, j);
241/** Thickness of a cell: the mean over its four corners (zero for a cell outside the plate). */
242export function cellThickness(pl: Plate, ci: number, cj: number): number {
243 const g = pl.grid;
244 if (!insideCell(g, ci, cj)) return 0;
245 const k = cj * g.nx + ci;
246 return 0.25 * (pl.h[k] + pl.h[k + 1] + pl.h[k + g.nx] + pl.h[k + g.nx + 1]);
249/** Smallest, mean, and largest thickness over the plate nodes. */
250export function thicknessRange(pl: Plate): { min: number; mean: number; max: number } {
251 const g = pl.grid;
252 let lo = Infinity, hi = 0, sum = 0, sw = 0;
253 for (let j = 0; j < g.ny; j++) {
254 for (let i = 0; i < g.nx; i++) {
255 const wgt = nodeWeight(g, i, j);
256 if (wgt === 0) continue;
257 const h = pl.h[j * g.nx + i];
258 lo = Math.min(lo, h); hi = Math.max(hi, h); sum += wgt * h; sw += wgt;
259 }
260 }
261 return { min: isFinite(lo) ? lo : 0, mean: sw > 0 ? sum / sw : 0, max: hi };
264/** Total mass [kg] and the moments of inertia about the in-plane axes through the center of the bounding box. */
265export function massProperties(pl: Plate): { mass: number; Ix: number; Iy: number } {
266 const g = pl.grid;
267 let mass = 0, Ix = 0, Iy = 0;
268 for (let j = 0; j < g.ny; j++) {
269 for (let i = 0; i < g.nx; i++) {
270 const mk = nodeMass(pl, i, j);
271 mass += mk;
272 Ix += mk * nodeY(g, j) ** 2;
273 Iy += mk * nodeX(g, i) ** 2;
274 }
275 }
276 return { mass, Ix, Iy };
279/**
280 * 60 dB decay time [s] of a bending mode of angular frequency omega under the
281 * loss model sigma0 + sigma1 k^2, using the flat-plate dispersion k^2 = omega / kappa
282 * for a plate of thickness h.
283 */
284export function t60(m: Material, h: number, loss: Loss, omega: number): number {
285 const kappa = Math.sqrt(bendingStiffness(m, h) / (m.rho * h));
286 const sigma = loss.sigma0 + loss.sigma1 * (omega / kappa);
287 return (3 * Math.LN10) / sigma;
290/**
291 * Frequency [Hz] at which the three-point second difference lowers a bending
292 * frequency by the relative amount `err` on a grid of spacing hmax, for a
293 * plate of thickness h. A plane bending wave along a grid axis with
294 * wavenumber k has the discrete frequency ratio (2 - 2 cos kh) / (kh)^2.
295 */
296export function dispersionFrequency(m: Material, h: number, g: Grid, err: number): number {
297 const hg = Math.max(g.hx, g.hy);
298 let lo = 0, hi = Math.PI;
299 for (let it = 0; it < 60; it++) {
300 const x = (lo + hi) / 2;
301 if ((2 - 2 * Math.cos(x)) / (x * x) > 1 - err) lo = x; else hi = x;
302 }
303 const k = lo / hg;
304 const kappa = Math.sqrt(bendingStiffness(m, h) / (m.rho * h));
305 return (kappa * k * k) / (2 * Math.PI);
308// ---------- Geometry of the excitation and the mount ----------
310/** Strike point in plate coordinates. */
311export function strikePoint(p: SimParams, pl: Plate): [number, number] {
312 return [(p.strikeFracX - 0.5) * pl.Lx, (p.strikeFracY - 0.5) * pl.Ly];
315/** The plate node nearest to a point, by index; -1 when the plate has no nodes. */
316export function nearestNode(g: Grid, x: number, y: number): number {
317 let nearest = -1, best = Infinity;
318 for (let j = 0; j < g.ny; j++) {
319 for (let i = 0; i < g.nx; i++) {
320 if (nodeWeight(g, i, j) === 0) continue;
321 const d2 = (nodeX(g, i) - x) ** 2 + (nodeY(g, j) - y) ** 2;
322 if (d2 < best) { best = d2; nearest = j * g.nx + i; }
323 }
324 }
325 return nearest;
328export interface Patch {
329 nodes: number[];
330 /** The plate node nearest the center; always a member. */
331 nearest: number;
332 /** Total mass of the member nodes [kg]. */
333 mass: number;
334 /** Total node weight of the members. */
335 weight: number;
336 /** Mass-weighted center [m]. */
337 center: [number, number];
338 /** Index box enclosing the members (i0, i1, j0, j1, inclusive). */
339 box: [number, number, number, number];
342/**
343 * The plate nodes within `radius` of (x, y), plus the nearest one. Used for
344 * the drumstick contact patch and for the mount. The contact force is spread
345 * over the patch in proportion to the node masses, and the contact coordinate
346 * is their mass-weighted mean displacement, so the two are conjugate and the
347 * contact has a potential energy. The mass-weighted center of the patch is
348 * where the inertia-relief reaction to the force is taken.
349 */
350export function patchAround(pl: Plate, x: number, y: number, radius: number): Patch {
351 const g = pl.grid;
352 const nearest = nearestNode(g, x, y);
353 const nodes: number[] = [];
354 let mass = 0, weight = 0, cx = 0, cy = 0;
355 const box: [number, number, number, number] = [g.nx, -1, g.ny, -1];
356 for (let j = 0; j < g.ny; j++) {
357 for (let i = 0; i < g.nx; i++) {
358 const k = j * g.nx + i;
359 const wgt = nodeWeight(g, i, j);
360 if (wgt === 0) continue;
361 const d2 = (nodeX(g, i) - x) ** 2 + (nodeY(g, j) - y) ** 2;
362 if (d2 <= radius * radius || k === nearest) {
363 nodes.push(k);
364 const mk = nodeMass(pl, i, j);
365 mass += mk; weight += wgt; cx += mk * nodeX(g, i); cy += mk * nodeY(g, j);
366 box[0] = Math.min(box[0], i); box[1] = Math.max(box[1], i);
367 box[2] = Math.min(box[2], j); box[3] = Math.max(box[3], j);
368 }
369 }
370 }
371 return { nodes, nearest, mass, weight, center: mass > 0 ? [cx / mass, cy / mass] : [x, y], box };
374export function contactPatch(p: SimParams, pl: Plate): Patch {
375 const [sx, sy] = strikePoint(p, pl);
376 return patchAround(pl, sx, sy, p.contactRadius);
379/** The mount patch, or null when the plate is free. */
380export function mountPatch(pl: Plate): Patch | null {
381 if (!pl.mount) return null;
382 return patchAround(pl, pl.mount.x, pl.mount.y, pl.mount.radius);
↑↓ move↵openescclose