087c169Physically simulated pizzicato double bass and an SF2 built from itJeremy Magland 1// Verification of the FE body building blocks against analytic results.
2// node scripts/test_fe.ts
3import { Terms, Skyline } from "../src/fe/sparse.ts";
4import { eigsSliced, symEig } from "../src/fe/eigen.ts";
5import { addBending, addMembrane, ortho, type Ortho } from "../src/fe/plate.ts";
6import { IN, type Grid } from "../src/fe/geometry.ts";
7import { buildCavity, cavityModes, makePlug } from "../src/fe/air.ts";
8import { solveCoupled } from "../src/fe/coupling.ts";
10let failures = 0;
11function check(name: string, ok: boolean, detail: string) {
12 console.log(`${ok ? "PASS" : "FAIL"} ${name}: ${detail}`);
13 if (!ok) failures++;
14}
16/** Simply-supported rectangle Lx × Ly on the FD grid (boundary nodes constrained). */
17function rectPlate(Lx: number, Ly: number, h: number, P: Ortho, shell: boolean, fmax: number): number[] {
18 const nx = Math.round(Lx / h) + 1, ns = Math.round(Ly / h) + 1;
19 const type = new Int8Array(nx * ns);
20 for (let i = 1; i < nx - 1; i++) for (let j = 1; j < ns - 1; j++) type[i * ns + j] = IN;
21 const g: Grid = { nx, ns, h, x0: 0, s0: 0, type, z0: new Float64Array(nx * ns) };
22 const wd = new Int32Array(nx * ns).fill(-1), ud = new Int32Array(nx * ns).fill(-1), vd = new Int32Array(nx * ns).fill(-1);
23 let n = 0;
24 for (let k = 0; k < nx * ns; k++)
25 if (type[k] === IN) {
26 wd[k] = n++;
27 if (shell) {
28 ud[k] = n++;
29 vd[k] = n++;
30 }
31 }
32 const T = new Terms();
33 addBending(T, g, wd, P, 0, 0);
34 if (shell) addMembrane(T, g, wd, ud, vd, P, 0);
35 const K = Skyline.fromTerms(n, T.pack());
36 const M = new Float64Array(n).fill(P.rhoh * h * h);
37 const r = eigsSliced(K, M, -1, (2 * Math.PI * fmax) ** 2, { perSlice: 40 });
38 if (r.warnings.length) console.log(" warnings:", r.warnings);
39 return r.lam.map((l) => Math.sqrt(l) / (2 * Math.PI));
40}
42function ssAnalytic(Lx: number, Ly: number, P: Ortho, count: number): { f: number; m: number; n: number }[] {
43 const out: { f: number; m: number; n: number }[] = [];
44 for (let m = 1; m <= 12; m++)
45 for (let n = 1; n <= 12; n++) {
46 const a = (m * Math.PI) / Lx, b = (n * Math.PI) / Ly;
47 const w2 = (P.D11 * a ** 4 + 2 * (P.D12 + 2 * P.D66) * a * a * b * b + P.D22 * b ** 4) / P.rhoh;
48 out.push({ f: Math.sqrt(w2) / (2 * Math.PI), m, n });
49 }
50 out.sort((x, y) => x.f - y.f);
51 return out.slice(0, count);
52}
54// ------------------------------------------------------------------ 0. eigensolver vs dense
55{
56 // random sparse SPD banded matrix + diagonal mass
57 const n = 300;
58 const T = new Terms();
59 let s = 1;
60 const rnd = () => ((s = (s * 16807) % 2147483647) / 2147483647);
61 for (let i = 0; i < n; i++) {
62 T.add([i], [1], 1 + 3 * rnd(), 0);
63 for (let d = 1; d <= 4; d++) if (i + d < n) T.add([i, i + d], [1, -rnd()], 2 * rnd(), 0);
64 }
65 const K = Skyline.fromTerms(n, T.pack());
66 const M = Float64Array.from({ length: n }, () => 0.5 + rnd());
67 const A = new Float64Array(n * n);
68 for (let i = 0; i < n; i++) for (let j = 0; j < n; j++) A[i * n + j] = K.get(i, j) / Math.sqrt(M[i] * M[j]);
69 const { vals } = symEig(A, n);
70 const lamHi = vals[Math.floor(n * 0.6)] * 1.0000001;
71 const r = eigsSliced(K, M, -1, lamHi, { perSlice: 30 });
72 let maxErr = 0;
73 for (let k = 0; k < r.lam.length; k++) maxErr = Math.max(maxErr, Math.abs(r.lam[k] - vals[k]) / vals[k]);
74 check("0. sliced Lanczos vs dense eigensolver", r.lam.length === Math.floor(n * 0.6) + 1 && maxErr < 1e-9, `${r.lam.length} eigenvalues, max rel. error ${maxErr.toExponential(2)}, ${r.slices} slices`);
75}
77// ------------------------------------------------------------------ 1. SS isotropic plate
78{
79 const Lx = 0.6, Ly = 0.4, h = 0.01;
80 const P = ortho(70e9, 70e9, 70e9 / (2 * 1.3), 0.3, 2700, 0.003);
81 const an = ssAnalytic(Lx, Ly, P, 6);
82 const fe = rectPlate(Lx, Ly, h, P, false, an[5].f * 1.05);
83 const errs = an.map((a, k) => (fe[k] - a.f) / a.f);
84 const maxE = Math.max(...errs.map(Math.abs));
85 console.log(" mode analytic FD err");
86 an.forEach((a, k) => console.log(` (${a.m},${a.n}) ${a.f.toFixed(2).padStart(8)} ${fe[k].toFixed(2).padStart(8)} ${(100 * errs[k]).toFixed(3)} %`));
87 check("1a. SS isotropic rectangle 0.6×0.4 m, 3 mm Al, h = 1 cm (bending only)", maxE < 0.02, `max |err| ${(100 * maxE).toFixed(3)} % over the first 6 modes`);
88 const fs = rectPlate(Lx, Ly, h, P, true, an[5].f * 1.05);
89 const maxS = Math.max(...an.map((a, k) => Math.abs(fs[k] - a.f) / a.f));
90 const nAn = ssAnalytic(Lx, Ly, P, 40).filter((a) => a.f < an[5].f * 1.05).length;
91 check(
92 "1b. same, flat shell assembly (w, u, v; membrane Q4 cells)",
93 maxS < 0.02 && fs.length === nAn && fe.length === nAn,
94 `${fs.length} modes below 1.05 f6 = analytic count ${nAn} (no spurious in-plane modes), max |err| ${(100 * maxS).toFixed(3)} %`,
95 );
96}
98// ------------------------------------------------------------------ 2. rigid box cavity
99{
100 const Lx = 0.9, Ly = 0.55, Lz = 0.23, ha = 0.025, c = 343;
101 const cv = buildCavity(0, Lx, Ly / 2, ha, () => true, () => Lz, Math.round(Lz / ha));
102 const fmax = 700;
103 const am = cavityModes(cv, c, fmax);
104 const an: { f: number; l: number; m: number; n: number }[] = [];
105 for (let l = 0; l < 10; l++) for (let m = 0; m < 10; m++) for (let n = 0; n < 6; n++) an.push({ f: (c / 2) * Math.hypot(l / Lx, m / Ly, n / Lz), l, m, n });
106 an.sort((a, b) => a.f - b.f);
107 const fe = am.omega.map((w) => w / (2 * Math.PI));
108 let maxE = 0;
109 console.log(" mode analytic FV err");
110 for (let k = 1; k < Math.min(12, fe.length); k++) {
111 const e = (fe[k] - an[k].f) / an[k].f;
112 maxE = Math.max(maxE, Math.abs(e));
113 console.log(` (${an[k].l},${an[k].m},${an[k].n}) ${an[k].f.toFixed(2).padStart(8)} ${fe[k].toFixed(2).padStart(8)} ${(100 * e).toFixed(3)} %`);
114 }
115 const nAn = an.filter((a) => a.f < fmax).length;
116 check(
117 "2. rigid box 0.9×0.55×0.23 m, 2.5 cm cells",
118 maxE < 0.01 && fe.length === nAn && Math.abs(fe[0]) < 1e-3,
119 `mode 0 at ${fe[0].toExponential(1)} Hz, ${fe.length}/${nAn} modes below ${fmax} Hz, max |err| ${(100 * maxE).toFixed(3)} % (first 11)`,
120 );
121}
123// ------------------------------------------------------------------ 3. Helmholtz resonator
124{
125 const Lx = 0.6, Ly = 0.4, Lz = 0.3, ha = 0.025, c = 343, rho0 = 1.2;
126 const V = Lx * Ly * Lz;
127 const cv = buildCavity(0, Lx, Ly / 2, ha, () => true, () => Lz, Math.round(Lz / ha));
128 const am = cavityModes(cv, c, 2500);
129 const A = 0.003, lEff = 0.04;
130 const run = (corr: boolean) => {
131 const pl = makePlug(cv, am, A, lEff, 0.004, [{ x: 0.21, s: 0.07 }], rho0, c);
132 const mp = corr ? pl.mp : rho0 * A * lEff;
133 const na = am.omega.length;
134 const S = new Float64Array(na);
135 for (let j = 0; j < na; j++) S[j] = (A / Math.sqrt(mp)) * pl.psiBar[j];
136 const cr = solveCoupled({ omega2: [0], S, airOmega: am.omega, D: new Float64Array(na).fill(rho0 * c * c) });
137 return { f: Math.sqrt(cr.omega2[0]) / (2 * Math.PI), pl };
138 };
139 const fH = (c / (2 * Math.PI)) * Math.sqrt(A / (V * lEff));
140 const r1 = run(true), r0 = run(false);
141 const e = (r1.f - fH) / fH;
142 check(
143 "3. rigid box 72 L + one hole (A = 30 cm², l_eff = 4 cm)",
144 Math.abs(e) < 0.03,
145 `coupled ${r1.f.toFixed(2)} Hz vs c/2π·sqrt(A/(V l)) = ${fH.toFixed(2)} Hz, err ${(100 * e).toFixed(2)} % ` +
146 `(modal near-field Δl = ${(1000 * r1.pl.dl).toFixed(1)} mm removed; without that correction ${r0.f.toFixed(2)} Hz)`,
147 );
148}
150// ------------------------------------------------------------------ 4. orthotropic plate, E_L <-> E_R
151{
152 const Lx = 0.6, Ly = 0.4, h = 0.01, t = 0.0045, rho = 430;
153 const EL = 11e9, ER = 0.75e9, G = 0.7e9, nu = 0.37;
154 const P1 = ortho(EL, ER, G, nu, rho, t);
155 const P2 = ortho(ER, EL, G, (nu * ER) / EL, rho, t); // grain across: same material rotated 90°
156 const an1 = ssAnalytic(Lx, Ly, P1, 6), an2 = ssAnalytic(Lx, Ly, P2, 6);
157 const fmax = Math.max(an1[5].f, an2[5].f) * 1.05;
158 const f1 = rectPlate(Lx, Ly, h, P1, false, fmax), f2 = rectPlate(Lx, Ly, h, P2, false, fmax);
159 const f2rot = rectPlate(Ly, Lx, h, P1, false, fmax); // grain along the short side
160 const e1 = Math.max(...an1.map((a, k) => Math.abs(f1[k] - a.f) / a.f));
161 const e2 = Math.max(...an2.map((a, k) => Math.abs(f2[k] - a.f) / a.f));
162 let eRot = 0;
163 for (let k = 0; k < 6; k++) eRot = Math.max(eRot, Math.abs(f2[k] - f2rot[k]) / f2[k]);
164 console.log(" grain along x: " + an1.map((a, k) => `(${a.m},${a.n}) ${f1[k].toFixed(1)}/${a.f.toFixed(1)}`).join(" "));
165 console.log(" grain along s: " + an2.map((a, k) => `(${a.m},${a.n}) ${f2[k].toFixed(1)}/${a.f.toFixed(1)}`).join(" "));
166 check("4a. orthotropic spruce 0.6×0.4, grain along x vs analytic", e1 < 0.02, `max |err| ${(100 * e1).toFixed(3)} %`);
167 check("4b. E_L <-> E_R swapped (grain along s) vs analytic", e2 < 0.02, `max |err| ${(100 * e2).toFixed(3)} %`);
168 check("4c. swapped = original material on the rotated (0.4×0.6) plate", eRot < 1e-6, `max rel. diff ${eRot.toExponential(2)}`);
169 check(
170 "4d. grain across the short span raises the fundamental (D11 acts over 0.4 m)",
171 f1[0] < f2[0] && f2[0] / f1[0] > 1.5,
172 `f11 ${f1[0].toFixed(2)} Hz (grain along 0.6 m) vs ${f2[0].toFixed(2)} Hz (grain along 0.4 m)`,
173 );
174}
176console.log(failures === 0 ? "\nall checks passed" : `\n${failures} check(s) FAILED`);
177if (failures) process.exitCode = 1;