// Compute the finite-element body from params and save it as JSON, and its mode shapes for the // 3D view next to it (_shapes.bin). // node scripts/fe_body.ts [params.json] [out.json] (default out: bodies/fe_default.json) import { mkdirSync, readFileSync, writeFileSync } from "node:fs"; import { dirname } from "node:path"; import { defaultParams, mergeParams } from "../src/core/params.ts"; import { bridgeAdmittance, STRING_NAMES } from "../src/core/types.ts"; import { computeFEBody } from "../src/fe/index.ts"; import { encodeShapes, type FEShapes } from "../src/fe/shapes.ts"; const args = process.argv.slice(2); let params = defaultParams(); let outPath = "bodies/fe_default.json"; if (args.length >= 1) { params = mergeParams(JSON.parse(readFileSync(args[0], "utf8"))); outPath = args[1] ?? outPath; } let lastMsg = ""; const t0 = performance.now(); let shapes: FEShapes | null = null; const body = computeFEBody(params, (msg, frac) => { if (msg !== lastMsg) { process.stdout.write(`\r[${(frac * 100).toFixed(0).padStart(3)}%] ${msg}`.padEnd(100)); lastMsg = msg; } }, { shapes: (s) => { shapes = s; } }); process.stdout.write("\n"); const elapsed = (performance.now() - t0) / 1000; mkdirSync(dirname(outPath), { recursive: true }); writeFileSync(outPath, JSON.stringify(body)); const shapesPath = outPath.replace(/\.json$/, "") + "_shapes.bin"; writeFileSync(shapesPath, encodeShapes(shapes!)); console.log(`wrote ${outPath} and ${shapesPath}`); const meta = body.meta as Record; const fe = body.modes.filter((m) => m.label !== "stat"); console.log(`\nFE body: ${fe.length} FE modes <= ${params.fe.fmax} Hz, ${meta.statModes} statistical modes above, total ${body.modes.length}`); console.log(`A0 = ${meta.a0Hz?.toFixed(1)} Hz (rigid-wall Helmholtz estimate ${meta.helmholtzEstimateHz?.toFixed(1)} Hz)`); console.log(`top area ${meta.topArea.toFixed(3)} m², top plate ${meta.structure.topPlateMass.toFixed(3)} kg (+ bass bar ${meta.structure.bassBarMass.toFixed(3)} kg), back ${meta.structure.backPlateMass.toFixed(3)} kg (+ bars ${meta.structure.backBarsMass.toFixed(3)} kg)`); console.log(`cavity volume ${(meta.air.volume * 1000).toFixed(1)} L, first rigid-wall air modes: ${meta.air.firstHz.map((f: number) => f.toFixed(0)).join(", ")} Hz`); console.log(`grid: ${meta.grid.nx}x${meta.grid.ns}, h=${meta.grid.h}, ${meta.structure.dofs} structural DOFs, ${meta.structure.modes} structural modes; air ${meta.air.cells} cells, ${meta.air.modes} modes; coupled ${meta.coupled.dofs}`); console.log("\n # f Hz zeta label |by| E A D G |bz| |rad| ke(top,back,bridge,plug,air)"); const diagByF = new Map(meta.diag.map((d: any) => [d.f, d])); fe.slice(0, 25).forEach((m, i) => { const d = diagByF.get(m.f); console.log( `${String(i + 1).padStart(4)} ${m.f.toFixed(1).padStart(9)} ${m.zeta.toFixed(4).padStart(8)} ${(m.label ?? "").padEnd(8)} ` + m.by.map((v) => Math.abs(v).toFixed(3).padStart(6)).join(" ") + ` ${Math.abs(m.bz[0]).toFixed(3).padStart(6)} ${Math.hypot(m.radRe, m.radIm).toExponential(2).padStart(9)} ` + (d ? d.ke.map((v: number) => v.toFixed(2)).join(" ") : ""), ); }); // bridge admittance summary const freqs: number[] = []; for (let f = 30; f <= 5000; f *= 1.01) freqs.push(f); for (const dir of ["y", "z"] as const) { for (const si of [0, 3]) { const Y = bridgeAdmittance(body, freqs, si, dir); const mag = freqs.map((_, k) => Math.hypot(Y.re[k], Y.im[k])); const bandMax = (lo: number, hi: number) => { let mx = 0, fm = 0; freqs.forEach((f, k) => { if (f >= lo && f < hi && mag[k] > mx) { mx = mag[k]; fm = f; } }); return `${mx.toExponential(2)}@${fm.toFixed(0)}`; }; console.log(`|Y_${dir}| ${STRING_NAMES[si]}: max 50-400 Hz ${bandMax(50, 400)}, 400-800 ${bandMax(400, 800)}, 800-2000 ${bandMax(800, 2000)}, 2-5 kHz ${bandMax(2000, 5000)} s/kg`); } } // 1/3-octave rms admittance (G string notch) and rms radiation transfer, to see the bridge hill and the // continuity at fmax { const Yy = bridgeAdmittance(body, freqs, 3, "y"), Yz = bridgeAdmittance(body, freqs, 3, "z"); const rows: string[] = []; for (let fc = 50; fc <= 5000; fc *= Math.pow(2, 1 / 3)) { let sy = 0, sz = 0, n = 0; freqs.forEach((f, k) => { if (f >= fc / Math.pow(2, 1 / 6) && f < fc * Math.pow(2, 1 / 6)) { sy += Yy.re[k] ** 2 + Yy.im[k] ** 2; sz += Yz.re[k] ** 2 + Yz.im[k] ** 2; n++; } }); if (n) rows.push(`${fc.toFixed(0).padStart(5)} Hz |Y_y| ${Math.sqrt(sy / n).toExponential(2)} |Y_z| ${Math.sqrt(sz / n).toExponential(2)}`); } console.log("\n1/3-octave rms bridge admittance at the G notch (s/kg):\n" + rows.join("\n")); } console.log(`\ntimings (s): ${JSON.stringify(meta.timings)} (wall ${elapsed.toFixed(1)} s)`); if (meta.warnings.length) console.log("warnings:\n " + meta.warnings.join("\n ")); console.log(`wrote ${outPath}`);