// Verification of the FE body building blocks against analytic results. // node scripts/test_fe.ts import { Terms, Skyline } from "../src/fe/sparse.ts"; import { eigsSliced, symEig } from "../src/fe/eigen.ts"; import { addBending, addMembrane, ortho, type Ortho } from "../src/fe/plate.ts"; import { IN, type Grid } from "../src/fe/geometry.ts"; import { buildCavity, cavityModes, makePlug } from "../src/fe/air.ts"; import { solveCoupled } from "../src/fe/coupling.ts"; let failures = 0; function check(name: string, ok: boolean, detail: string) { console.log(`${ok ? "PASS" : "FAIL"} ${name}: ${detail}`); if (!ok) failures++; } /** Simply-supported rectangle Lx × Ly on the FD grid (boundary nodes constrained). */ function rectPlate(Lx: number, Ly: number, h: number, P: Ortho, shell: boolean, fmax: number): number[] { const nx = Math.round(Lx / h) + 1, ns = Math.round(Ly / h) + 1; const type = new Int8Array(nx * ns); for (let i = 1; i < nx - 1; i++) for (let j = 1; j < ns - 1; j++) type[i * ns + j] = IN; const g: Grid = { nx, ns, h, x0: 0, s0: 0, type, z0: new Float64Array(nx * ns) }; const wd = new Int32Array(nx * ns).fill(-1), ud = new Int32Array(nx * ns).fill(-1), vd = new Int32Array(nx * ns).fill(-1); let n = 0; for (let k = 0; k < nx * ns; k++) if (type[k] === IN) { wd[k] = n++; if (shell) { ud[k] = n++; vd[k] = n++; } } const T = new Terms(); addBending(T, g, wd, P, 0, 0); if (shell) addMembrane(T, g, wd, ud, vd, P, 0); const K = Skyline.fromTerms(n, T.pack()); const M = new Float64Array(n).fill(P.rhoh * h * h); const r = eigsSliced(K, M, -1, (2 * Math.PI * fmax) ** 2, { perSlice: 40 }); if (r.warnings.length) console.log(" warnings:", r.warnings); return r.lam.map((l) => Math.sqrt(l) / (2 * Math.PI)); } function ssAnalytic(Lx: number, Ly: number, P: Ortho, count: number): { f: number; m: number; n: number }[] { const out: { f: number; m: number; n: number }[] = []; for (let m = 1; m <= 12; m++) for (let n = 1; n <= 12; n++) { const a = (m * Math.PI) / Lx, b = (n * Math.PI) / Ly; const w2 = (P.D11 * a ** 4 + 2 * (P.D12 + 2 * P.D66) * a * a * b * b + P.D22 * b ** 4) / P.rhoh; out.push({ f: Math.sqrt(w2) / (2 * Math.PI), m, n }); } out.sort((x, y) => x.f - y.f); return out.slice(0, count); } // ------------------------------------------------------------------ 0. eigensolver vs dense { // random sparse SPD banded matrix + diagonal mass const n = 300; const T = new Terms(); let s = 1; const rnd = () => ((s = (s * 16807) % 2147483647) / 2147483647); for (let i = 0; i < n; i++) { T.add([i], [1], 1 + 3 * rnd(), 0); for (let d = 1; d <= 4; d++) if (i + d < n) T.add([i, i + d], [1, -rnd()], 2 * rnd(), 0); } const K = Skyline.fromTerms(n, T.pack()); const M = Float64Array.from({ length: n }, () => 0.5 + rnd()); const A = new Float64Array(n * n); 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]); const { vals } = symEig(A, n); const lamHi = vals[Math.floor(n * 0.6)] * 1.0000001; const r = eigsSliced(K, M, -1, lamHi, { perSlice: 30 }); let maxErr = 0; for (let k = 0; k < r.lam.length; k++) maxErr = Math.max(maxErr, Math.abs(r.lam[k] - vals[k]) / vals[k]); 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`); } // ------------------------------------------------------------------ 1. SS isotropic plate { const Lx = 0.6, Ly = 0.4, h = 0.01; const P = ortho(70e9, 70e9, 70e9 / (2 * 1.3), 0.3, 2700, 0.003); const an = ssAnalytic(Lx, Ly, P, 6); const fe = rectPlate(Lx, Ly, h, P, false, an[5].f * 1.05); const errs = an.map((a, k) => (fe[k] - a.f) / a.f); const maxE = Math.max(...errs.map(Math.abs)); console.log(" mode analytic FD err"); 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)} %`)); 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`); const fs = rectPlate(Lx, Ly, h, P, true, an[5].f * 1.05); const maxS = Math.max(...an.map((a, k) => Math.abs(fs[k] - a.f) / a.f)); const nAn = ssAnalytic(Lx, Ly, P, 40).filter((a) => a.f < an[5].f * 1.05).length; check( "1b. same, flat shell assembly (w, u, v; membrane Q4 cells)", maxS < 0.02 && fs.length === nAn && fe.length === nAn, `${fs.length} modes below 1.05 f6 = analytic count ${nAn} (no spurious in-plane modes), max |err| ${(100 * maxS).toFixed(3)} %`, ); } // ------------------------------------------------------------------ 2. rigid box cavity { const Lx = 0.9, Ly = 0.55, Lz = 0.23, ha = 0.025, c = 343; const cv = buildCavity(0, Lx, Ly / 2, ha, () => true, () => Lz, Math.round(Lz / ha)); const fmax = 700; const am = cavityModes(cv, c, fmax); const an: { f: number; l: number; m: number; n: number }[] = []; 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 }); an.sort((a, b) => a.f - b.f); const fe = am.omega.map((w) => w / (2 * Math.PI)); let maxE = 0; console.log(" mode analytic FV err"); for (let k = 1; k < Math.min(12, fe.length); k++) { const e = (fe[k] - an[k].f) / an[k].f; maxE = Math.max(maxE, Math.abs(e)); 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)} %`); } const nAn = an.filter((a) => a.f < fmax).length; check( "2. rigid box 0.9×0.55×0.23 m, 2.5 cm cells", maxE < 0.01 && fe.length === nAn && Math.abs(fe[0]) < 1e-3, `mode 0 at ${fe[0].toExponential(1)} Hz, ${fe.length}/${nAn} modes below ${fmax} Hz, max |err| ${(100 * maxE).toFixed(3)} % (first 11)`, ); } // ------------------------------------------------------------------ 3. Helmholtz resonator { const Lx = 0.6, Ly = 0.4, Lz = 0.3, ha = 0.025, c = 343, rho0 = 1.2; const V = Lx * Ly * Lz; const cv = buildCavity(0, Lx, Ly / 2, ha, () => true, () => Lz, Math.round(Lz / ha)); const am = cavityModes(cv, c, 2500); const A = 0.003, lEff = 0.04; const run = (corr: boolean) => { const pl = makePlug(cv, am, A, lEff, 0.004, [{ x: 0.21, s: 0.07 }], rho0, c); const mp = corr ? pl.mp : rho0 * A * lEff; const na = am.omega.length; const S = new Float64Array(na); for (let j = 0; j < na; j++) S[j] = (A / Math.sqrt(mp)) * pl.psiBar[j]; const cr = solveCoupled({ omega2: [0], S, airOmega: am.omega, D: new Float64Array(na).fill(rho0 * c * c) }); return { f: Math.sqrt(cr.omega2[0]) / (2 * Math.PI), pl }; }; const fH = (c / (2 * Math.PI)) * Math.sqrt(A / (V * lEff)); const r1 = run(true), r0 = run(false); const e = (r1.f - fH) / fH; check( "3. rigid box 72 L + one hole (A = 30 cm², l_eff = 4 cm)", Math.abs(e) < 0.03, `coupled ${r1.f.toFixed(2)} Hz vs c/2π·sqrt(A/(V l)) = ${fH.toFixed(2)} Hz, err ${(100 * e).toFixed(2)} % ` + `(modal near-field Δl = ${(1000 * r1.pl.dl).toFixed(1)} mm removed; without that correction ${r0.f.toFixed(2)} Hz)`, ); } // ------------------------------------------------------------------ 4. orthotropic plate, E_L <-> E_R { const Lx = 0.6, Ly = 0.4, h = 0.01, t = 0.0045, rho = 430; const EL = 11e9, ER = 0.75e9, G = 0.7e9, nu = 0.37; const P1 = ortho(EL, ER, G, nu, rho, t); const P2 = ortho(ER, EL, G, (nu * ER) / EL, rho, t); // grain across: same material rotated 90° const an1 = ssAnalytic(Lx, Ly, P1, 6), an2 = ssAnalytic(Lx, Ly, P2, 6); const fmax = Math.max(an1[5].f, an2[5].f) * 1.05; const f1 = rectPlate(Lx, Ly, h, P1, false, fmax), f2 = rectPlate(Lx, Ly, h, P2, false, fmax); const f2rot = rectPlate(Ly, Lx, h, P1, false, fmax); // grain along the short side const e1 = Math.max(...an1.map((a, k) => Math.abs(f1[k] - a.f) / a.f)); const e2 = Math.max(...an2.map((a, k) => Math.abs(f2[k] - a.f) / a.f)); let eRot = 0; for (let k = 0; k < 6; k++) eRot = Math.max(eRot, Math.abs(f2[k] - f2rot[k]) / f2[k]); console.log(" grain along x: " + an1.map((a, k) => `(${a.m},${a.n}) ${f1[k].toFixed(1)}/${a.f.toFixed(1)}`).join(" ")); console.log(" grain along s: " + an2.map((a, k) => `(${a.m},${a.n}) ${f2[k].toFixed(1)}/${a.f.toFixed(1)}`).join(" ")); check("4a. orthotropic spruce 0.6×0.4, grain along x vs analytic", e1 < 0.02, `max |err| ${(100 * e1).toFixed(3)} %`); check("4b. E_L <-> E_R swapped (grain along s) vs analytic", e2 < 0.02, `max |err| ${(100 * e2).toFixed(3)} %`); check("4c. swapped = original material on the rotated (0.4×0.6) plate", eRot < 1e-6, `max rel. diff ${eRot.toExponential(2)}`); check( "4d. grain across the short span raises the fundamental (D11 acts over 0.4 m)", f1[0] < f2[0] && f2[0] / f1[0] > 1.5, `f11 ${f1[0].toFixed(2)} Hz (grain along 0.6 m) vs ${f2[0].toFixed(2)} Hz (grain along 0.4 m)`, ); } console.log(failures === 0 ? "\nall checks passed" : `\n${failures} check(s) FAILED`); if (failures) process.exitCode = 1;