182eaafAdd a Physics 3D tab: slow-motion 3D replay of the simulationJeremy Magland 1// Compute the finite-element body from params and save it as JSON, and its mode shapes for the
2// 3D view next to it (<out>_shapes.bin).
087c169Physically simulated pizzicato double bass and an SF2 built from itJeremy Magland 3// node scripts/fe_body.ts [params.json] [out.json] (default out: bodies/fe_default.json)
4import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
5import { dirname } from "node:path";
6import { defaultParams, mergeParams } from "../src/core/params.ts";
7import { bridgeAdmittance, STRING_NAMES } from "../src/core/types.ts";
8import { computeFEBody } from "../src/fe/index.ts";
182eaafAdd a Physics 3D tab: slow-motion 3D replay of the simulationJeremy Magland 9import { encodeShapes, type FEShapes } from "../src/fe/shapes.ts";
11const args = process.argv.slice(2);
12let params = defaultParams();
13let outPath = "bodies/fe_default.json";
14if (args.length >= 1) {
15 params = mergeParams(JSON.parse(readFileSync(args[0], "utf8")));
16 outPath = args[1] ?? outPath;
17}
19let lastMsg = "";
20const t0 = performance.now();
182eaafAdd a Physics 3D tab: slow-motion 3D replay of the simulationJeremy Magland 21let shapes: FEShapes | null = null;
087c169Physically simulated pizzicato double bass and an SF2 built from itJeremy Magland 22const body = computeFEBody(params, (msg, frac) => {
23 if (msg !== lastMsg) {
24 process.stdout.write(`\r[${(frac * 100).toFixed(0).padStart(3)}%] ${msg}`.padEnd(100));
25 lastMsg = msg;
26 }
182eaafAdd a Physics 3D tab: slow-motion 3D replay of the simulationJeremy Magland 27}, { shapes: (s) => { shapes = s; } });
087c169Physically simulated pizzicato double bass and an SF2 built from itJeremy Magland 28process.stdout.write("\n");
29const elapsed = (performance.now() - t0) / 1000;
30mkdirSync(dirname(outPath), { recursive: true });
31writeFileSync(outPath, JSON.stringify(body));
182eaafAdd a Physics 3D tab: slow-motion 3D replay of the simulationJeremy Magland 32const shapesPath = outPath.replace(/\.json$/, "") + "_shapes.bin";
33writeFileSync(shapesPath, encodeShapes(shapes!));
34console.log(`wrote ${outPath} and ${shapesPath}`);
36const meta = body.meta as Record<string, any>;
37const fe = body.modes.filter((m) => m.label !== "stat");
38console.log(`\nFE body: ${fe.length} FE modes <= ${params.fe.fmax} Hz, ${meta.statModes} statistical modes above, total ${body.modes.length}`);
39console.log(`A0 = ${meta.a0Hz?.toFixed(1)} Hz (rigid-wall Helmholtz estimate ${meta.helmholtzEstimateHz?.toFixed(1)} Hz)`);
40console.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)`);
41console.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`);
42console.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}`);
43console.log("\n # f Hz zeta label |by| E A D G |bz| |rad| ke(top,back,bridge,plug,air)");
44const diagByF = new Map<number, any>(meta.diag.map((d: any) => [d.f, d]));
45fe.slice(0, 25).forEach((m, i) => {
46 const d = diagByF.get(m.f);
47 console.log(
48 `${String(i + 1).padStart(4)} ${m.f.toFixed(1).padStart(9)} ${m.zeta.toFixed(4).padStart(8)} ${(m.label ?? "").padEnd(8)} ` +
49 m.by.map((v) => Math.abs(v).toFixed(3).padStart(6)).join(" ") +
50 ` ${Math.abs(m.bz[0]).toFixed(3).padStart(6)} ${Math.hypot(m.radRe, m.radIm).toExponential(2).padStart(9)} ` +
51 (d ? d.ke.map((v: number) => v.toFixed(2)).join(" ") : ""),
52 );
53});
54// bridge admittance summary
55const freqs: number[] = [];
56for (let f = 30; f <= 5000; f *= 1.01) freqs.push(f);
57for (const dir of ["y", "z"] as const) {
58 for (const si of [0, 3]) {
59 const Y = bridgeAdmittance(body, freqs, si, dir);
60 const mag = freqs.map((_, k) => Math.hypot(Y.re[k], Y.im[k]));
61 const bandMax = (lo: number, hi: number) => {
62 let mx = 0, fm = 0;
63 freqs.forEach((f, k) => {
64 if (f >= lo && f < hi && mag[k] > mx) {
65 mx = mag[k];
66 fm = f;
67 }
68 });
69 return `${mx.toExponential(2)}@${fm.toFixed(0)}`;
70 };
71 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`);
72 }
73}
74// 1/3-octave rms admittance (G string notch) and rms radiation transfer, to see the bridge hill and the
75// continuity at fmax
76{
77 const Yy = bridgeAdmittance(body, freqs, 3, "y"), Yz = bridgeAdmittance(body, freqs, 3, "z");
78 const rows: string[] = [];
79 for (let fc = 50; fc <= 5000; fc *= Math.pow(2, 1 / 3)) {
80 let sy = 0, sz = 0, n = 0;
81 freqs.forEach((f, k) => {
82 if (f >= fc / Math.pow(2, 1 / 6) && f < fc * Math.pow(2, 1 / 6)) {
83 sy += Yy.re[k] ** 2 + Yy.im[k] ** 2;
84 sz += Yz.re[k] ** 2 + Yz.im[k] ** 2;
85 n++;
86 }
87 });
88 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)}`);
89 }
90 console.log("\n1/3-octave rms bridge admittance at the G notch (s/kg):\n" + rows.join("\n"));
91}
92console.log(`\ntimings (s): ${JSON.stringify(meta.timings)} (wall ${elapsed.toFixed(1)} s)`);
93if (meta.warnings.length) console.log("warnings:\n " + meta.warnings.join("\n "));
94console.log(`wrote ${outPath}`);