// Simulation parameters (strike, listener, numerics), the grid with its mask, // the plate as evaluated on the grid (material, thickness field, stress-free // shape, mount), and the derived quantities the kernel needs. All values are // SI. The cymbal design itself is produced by the script in design.ts. export interface SimParams { // Strike strikeFracX: number; // strike point as a fraction of the bounding-box width from the left edge strikeFracY: number; // strike point as a fraction of the bounding-box height from the bottom edge strikeVelocity: number; // stick tip speed at impact [m/s] stickMass: number; // effective mass of the stick [kg] contactStiffness: number; // Hertz contact stiffness K [N/m^alpha] contactExponent: number; // Hertz exponent alpha (1.5 for a rounded tip) contactRadius: number; // radius of the contact patch [m] // Listener listenerDist: number; // [m] listenerAzimuth: number; // [deg], rotation about the plate's vertical axis (0 = on the normal) listenerElevation: number; // [deg] // Numerics and output spacing: number; // requested grid spacing [m] inPlaneMass: number; // factor by which the in-plane inertia is scaled; 1 is physical, larger lengthens the time step rigidProjection: boolean; // remove the six rigid-body modes every RB_INTERVAL steps (free plates only) fs: number; // audio sample rate [Hz] fps: number; // movie frame rate, also the batch rate [Hz] duration: number; // recorded duration after the impact [s] } export const defaultParams: SimParams = { strikeFracX: 0.7, strikeFracY: 0.5, strikeVelocity: 3, stickMass: 0.03, contactStiffness: 5e8, contactExponent: 1.5, contactRadius: 5e-3, listenerDist: 1.0, listenerAzimuth: 0, listenerElevation: 0, spacing: 0.008, inPlaneMass: 1, rigidProjection: false, fs: 48000, fps: 120, duration: 4, }; export const SPEED_OF_SOUND = 343; // [m/s] export const RHO_AIR = 1.2; // [kg/m^3] export const CFL = 0.9; // fraction of the stability limit used for the time step // The ring buffer of the retarded-time listener sum holds one batch of samples // plus this margin, which bounds the spread of the delays across the plate. export const HIST_MARGIN = 128; // [samples] export const RB_INTERVAL = 32; // steps between rigid-body projections // ---------- Grid ---------- export interface Grid { nx: number; ny: number; hx: number; hy: number; n: number; /** 1 for nodes that belong to the plate, 0 otherwise; absent for a full rectangle. */ mask?: Uint8Array; } /** True when the point (x, y), relative to the center of the bounding box, lies in the plate. */ export type InsideFn = (x: number, y: number) => boolean; /** * The grid covering the bounding box Lx by Ly with about the requested * spacing, restricted to the outline `inside` when one is given. */ export function makeGrid(Lx: number, Ly: number, spacing: number, inside?: InsideFn): Grid { const nx = Math.max(6, Math.round(Lx / spacing) + 1); const ny = Math.max(6, Math.round(Ly / spacing) + 1); const g: Grid = { nx, ny, hx: Lx / (nx - 1), hy: Ly / (ny - 1), n: nx * ny }; return inside ? withMask(g, inside) : g; } /** * Restricts a grid to an outline. The edge is a staircase of whole cells: a * cell belongs to the plate when its center lies inside the outline, and a * node when it is a corner of such a cell. Deciding by cell centers rather * than by nodes keeps the covered area unbiased; a node-based mask loses * about half a cell all around the edge, which raises every frequency by a * few percent. A node with no cell of its own would carry no mass, and the * trailing loop removes any such node. A hole in the outline (a cymbal's * center hole) is handled by the same rule. */ export function withMask(g: Grid, inside: InsideFn): Grid { return withCellMask(g, (ci, cj) => inside(nodeX(g, ci) + g.hx / 2, nodeY(g, cj) + g.hy / 2)); } /** The same restriction with the membership of each cell (ci, cj) given directly. */ export function withCellMask(g: Grid, cellInside: (ci: number, cj: number) => boolean): Grid { const mask = new Uint8Array(g.n); for (let cj = 0; cj < g.ny - 1; cj++) { for (let ci = 0; ci < g.nx - 1; ci++) { if (cellInside(ci, cj)) { mask[cj * g.nx + ci] = 1; mask[cj * g.nx + ci + 1] = 1; mask[(cj + 1) * g.nx + ci] = 1; mask[(cj + 1) * g.nx + ci + 1] = 1; } } } const gm: Grid = { ...g, mask }; for (let changed = true; changed; ) { changed = false; for (let j = 0; j < g.ny; j++) { for (let i = 0; i < g.nx; i++) { if (mask[j * g.nx + i] && nodeWeight(gm, i, j) === 0) { mask[j * g.nx + i] = 0; changed = true; } } } } return gm; } /** The ellipse inscribed in the bounding box (kept for the tests of the discretization). */ export function withEllipseMask(g: Grid): Grid { const a = ((g.nx - 1) * g.hx) / 2, b = ((g.ny - 1) * g.hy) / 2; return withMask(g, (x, y) => (x / a) ** 2 + (y / b) ** 2 <= 1); } /** Node coordinates relative to the center of the bounding box. */ export function nodeX(g: Grid, i: number): number { return (i - (g.nx - 1) / 2) * g.hx; } export function nodeY(g: Grid, j: number): number { return (j - (g.ny - 1) / 2) * g.hy; } /** True when node (i, j) exists and belongs to the plate. */ export function insideNode(g: Grid, i: number, j: number): boolean { if (i < 0 || i >= g.nx || j < 0 || j >= g.ny) return false; return g.mask ? g.mask[j * g.nx + i] !== 0 : true; } /** True when cell (ci, cj), with nodes (ci..ci+1, cj..cj+1), lies within the plate. */ export function insideCell(g: Grid, ci: number, cj: number): boolean { return insideNode(g, ci, cj) && insideNode(g, ci + 1, cj) && insideNode(g, ci + 1, cj + 1) && insideNode(g, ci, cj + 1); } /** * Quadrature and lumped-mass weight of a node: the fraction of its four * surrounding cells that belong to the plate. On a full rectangle this is the * trapezoidal rule (1 inside, 1/2 on an edge, 1/4 at a corner). Zero outside. */ export function nodeWeight(g: Grid, i: number, j: number): number { if (!insideNode(g, i, j)) return 0; let c = 0; if (insideCell(g, i, j)) c++; if (insideCell(g, i - 1, j)) c++; if (insideCell(g, i, j - 1)) c++; if (insideCell(g, i - 1, j - 1)) c++; return c / 4; } /** Per-node weights as a flat array. */ export function nodeWeights(g: Grid): Float32Array { const w = new Float32Array(g.n); 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); return w; } /** Per-cell membership (1 or 0) as a flat array of (nx - 1) * (ny - 1). */ export function cellMask(g: Grid): Float32Array { const c = new Float32Array((g.nx - 1) * (g.ny - 1)); 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; return c; } /** Number of nodes in the plate. */ export function countInside(g: Grid): number { if (!g.mask) return g.n; let c = 0; for (let k = 0; k < g.n; k++) c += g.mask[k]; return c; } // ---------- Material and plate ---------- export interface Material { E: number; // Young's modulus [Pa] nu: number; // Poisson ratio rho: number; // density [kg/m^3] } /** Losses of the plate (see THEORY.md). */ export interface Loss { sigma0: number; // frequency-independent loss of all motion [1/s] sigma1: number; // loss proportional to the squared wavenumber, transverse motion [m^2/s] sigmaIn: number; // additional frequency-independent loss of the in-plane motion [1/s] } /** * The stand: the plate nodes within `radius` of (x, y) are tied to the ground * by a spring of total stiffness `stiffness` and a dashpot of total rate * `damping`, both spread over the nodes in proportion to their weight and * acting on all three displacement components. */ export interface Mount { x: number; // [m] from the center of the bounding box y: number; radius: number; // [m] stiffness: number; // [N/m] damping: number; // [N s/m] } /** A design evaluated on a grid: everything the simulation needs to know about the plate. */ export interface Plate { name: string; Lx: number; // bounding box [m] Ly: number; grid: Grid; mat: Material; /** Thickness at every node [m]; zero outside the plate. */ h: Float64Array; /** Stress-free shape at every node [m], mean plane removed; zero outside the plate. */ w0: Float64Array; mount: Mount | null; loss: Loss; } /** Bending stiffness [N m] of a plate of thickness h. */ export function bendingStiffness(m: Material, h: number): number { return (m.E * h ** 3) / (12 * (1 - m.nu * m.nu)); } /** Membrane stiffness E h / (1 - nu^2) [N/m]. */ export function membraneStiffness(m: Material, h: number): number { return (m.E * h) / (1 - m.nu * m.nu); } /** Membrane shear stiffness E h / (2 (1 + nu)) [N/m]. */ export function shearStiffness(m: Material, h: number): number { return (m.E * h) / (2 * (1 + m.nu)); } /** Mass of node (i, j) [kg]: density, local thickness, cell area, and the node's weight. */ export function nodeMass(pl: Plate, i: number, j: number): number { const g = pl.grid; return pl.mat.rho * pl.h[j * g.nx + i] * g.hx * g.hy * nodeWeight(g, i, j); } /** Thickness of a cell: the mean over its four corners (zero for a cell outside the plate). */ export function cellThickness(pl: Plate, ci: number, cj: number): number { const g = pl.grid; if (!insideCell(g, ci, cj)) return 0; const k = cj * g.nx + ci; return 0.25 * (pl.h[k] + pl.h[k + 1] + pl.h[k + g.nx] + pl.h[k + g.nx + 1]); } /** Smallest, mean, and largest thickness over the plate nodes. */ export function thicknessRange(pl: Plate): { min: number; mean: number; max: number } { const g = pl.grid; let lo = Infinity, hi = 0, sum = 0, sw = 0; for (let j = 0; j < g.ny; j++) { for (let i = 0; i < g.nx; i++) { const wgt = nodeWeight(g, i, j); if (wgt === 0) continue; const h = pl.h[j * g.nx + i]; lo = Math.min(lo, h); hi = Math.max(hi, h); sum += wgt * h; sw += wgt; } } return { min: isFinite(lo) ? lo : 0, mean: sw > 0 ? sum / sw : 0, max: hi }; } /** Total mass [kg] and the moments of inertia about the in-plane axes through the center of the bounding box. */ export function massProperties(pl: Plate): { mass: number; Ix: number; Iy: number } { const g = pl.grid; let mass = 0, Ix = 0, Iy = 0; for (let j = 0; j < g.ny; j++) { for (let i = 0; i < g.nx; i++) { const mk = nodeMass(pl, i, j); mass += mk; Ix += mk * nodeY(g, j) ** 2; Iy += mk * nodeX(g, i) ** 2; } } return { mass, Ix, Iy }; } /** * 60 dB decay time [s] of a bending mode of angular frequency omega under the * loss model sigma0 + sigma1 k^2, using the flat-plate dispersion k^2 = omega / kappa * for a plate of thickness h. */ export function t60(m: Material, h: number, loss: Loss, omega: number): number { const kappa = Math.sqrt(bendingStiffness(m, h) / (m.rho * h)); const sigma = loss.sigma0 + loss.sigma1 * (omega / kappa); return (3 * Math.LN10) / sigma; } /** * Frequency [Hz] at which the three-point second difference lowers a bending * frequency by the relative amount `err` on a grid of spacing hmax, for a * plate of thickness h. A plane bending wave along a grid axis with * wavenumber k has the discrete frequency ratio (2 - 2 cos kh) / (kh)^2. */ export function dispersionFrequency(m: Material, h: number, g: Grid, err: number): number { const hg = Math.max(g.hx, g.hy); let lo = 0, hi = Math.PI; for (let it = 0; it < 60; it++) { const x = (lo + hi) / 2; if ((2 - 2 * Math.cos(x)) / (x * x) > 1 - err) lo = x; else hi = x; } const k = lo / hg; const kappa = Math.sqrt(bendingStiffness(m, h) / (m.rho * h)); return (kappa * k * k) / (2 * Math.PI); } // ---------- Geometry of the excitation and the mount ---------- /** Strike point in plate coordinates. */ export function strikePoint(p: SimParams, pl: Plate): [number, number] { return [(p.strikeFracX - 0.5) * pl.Lx, (p.strikeFracY - 0.5) * pl.Ly]; } /** The plate node nearest to a point, by index; -1 when the plate has no nodes. */ export function nearestNode(g: Grid, x: number, y: number): number { let nearest = -1, best = Infinity; for (let j = 0; j < g.ny; j++) { for (let i = 0; i < g.nx; i++) { if (nodeWeight(g, i, j) === 0) continue; const d2 = (nodeX(g, i) - x) ** 2 + (nodeY(g, j) - y) ** 2; if (d2 < best) { best = d2; nearest = j * g.nx + i; } } } return nearest; } export interface Patch { nodes: number[]; /** The plate node nearest the center; always a member. */ nearest: number; /** Total mass of the member nodes [kg]. */ mass: number; /** Total node weight of the members. */ weight: number; /** Mass-weighted center [m]. */ center: [number, number]; /** Index box enclosing the members (i0, i1, j0, j1, inclusive). */ box: [number, number, number, number]; } /** * The plate nodes within `radius` of (x, y), plus the nearest one. Used for * the drumstick contact patch and for the mount. The contact force is spread * over the patch in proportion to the node masses, and the contact coordinate * is their mass-weighted mean displacement, so the two are conjugate and the * contact has a potential energy. The mass-weighted center of the patch is * where the inertia-relief reaction to the force is taken. */ export function patchAround(pl: Plate, x: number, y: number, radius: number): Patch { const g = pl.grid; const nearest = nearestNode(g, x, y); const nodes: number[] = []; let mass = 0, weight = 0, cx = 0, cy = 0; const box: [number, number, number, number] = [g.nx, -1, g.ny, -1]; for (let j = 0; j < g.ny; j++) { for (let i = 0; i < g.nx; i++) { const k = j * g.nx + i; const wgt = nodeWeight(g, i, j); if (wgt === 0) continue; const d2 = (nodeX(g, i) - x) ** 2 + (nodeY(g, j) - y) ** 2; if (d2 <= radius * radius || k === nearest) { nodes.push(k); const mk = nodeMass(pl, i, j); mass += mk; weight += wgt; cx += mk * nodeX(g, i); cy += mk * nodeY(g, j); box[0] = Math.min(box[0], i); box[1] = Math.max(box[1], i); box[2] = Math.min(box[2], j); box[3] = Math.max(box[3], j); } } } return { nodes, nearest, mass, weight, center: mass > 0 ? [cx / mass, cy / mass] : [x, y], box }; } export function contactPatch(p: SimParams, pl: Plate): Patch { const [sx, sy] = strikePoint(p, pl); return patchAround(pl, sx, sy, p.contactRadius); } /** The mount patch, or null when the plate is free. */ export function mountPatch(pl: Plate): Patch | null { if (!pl.mount) return null; return patchAround(pl, pl.mount.x, pl.mount.y, pl.mount.radius); }