/ music / acoustic-base
Sign in
music / acoustic-base
acoustic-base / src / fe / index.ts
513 lines · 20.8 KBBlameHistoryRaw
1// Finite-element body of the double bass: plates + bars + post + bridge (structural modes by
2// shift-invert Lanczos), air cavity (finite-volume modes), f-hole plugs, symmetric modal
3// structural–acoustic coupling, energy-weighted damping, and radiation to a listener by a sum of
4// simple sources. Pure TypeScript; runs in Node and in a browser worker.
5//
6// Damping of a coupled mode k (ω_k, unit modal mass, potential energy ω_k² η̂²/2 at amplitude η̂):
7// ζ = ½ [Σ_g η_g U_g + U_air / cavityQ] / U_tot (material + cavity-wall losses)
8// + Σ_p κ_p R_p / (2 m_p ω) (viscous f-hole wall losses)
9// + ρ0 ω (1 + g(ka)) Σ_ab q_a q_b sinc(k R_ab) / (8π c) (radiation)
10// with U_g the strain energy in top (plate + bass bar + post), back (plate + bars), bridge springs,
11// U_air the air-spring energy; κ_p the kinetic-energy fraction of plug p, R_p = P l sqrt(ωρ0μ/2)
12// its wall resistance; q_a the outward volume displacement per unit η of source patch a (top,
13// back, plugs), R_ab their distances. The radiated power is that of free-field simple sources;
14// g(ka) = (ka)²/(1+(ka)²) with a the body's equivalent radius blends in the doubling of a closed
15// body face (baffle effect) at high frequency. The listener pressure uses the same sources with
16// an obliquity factor 1 + g cos θ.
18import { feInputsKey, type Params } from "../core/params.ts";
19import type { BodyMode, BodyModel } from "../core/types.ts";
20import { statisticalModes } from "../core/body.ts";
21import { eigsSliced } from "./eigen.ts";
22import { HOLE, makeArch, makeBodyGrid, makeFHoles, makeOutline, markFHoles, nodeS, nodeX, slotEffectiveLength } from "./geometry.ts";
23import { buildStructure, modeInfo, GROUP_NAMES } from "./structure.ts";
24import { buildCavity, cavityModes, makePlug, surfaceStencil } from "./air.ts";
25import { solveCoupled } from "./coupling.ts";
26import type { FEShapes } from "./shapes.ts";
28export type { FEExtra } from "./structure.ts";
30const AIR_MU = 1.81e-5;
32const now = () => (typeof performance !== "undefined" ? performance.now() : Date.now());
34/** opts.shapes: also compute the mode shapes for the 3D view (FEShapes) and hand them over. */
35export function computeFEBody(p: Params, onProgress?: (msg: string, frac: number) => void, opts: { shapes?: (s: FEShapes) => void } = {}): BodyModel {
36 const fe = p.fe;
37 const T0 = now();
38 const timings: Record<string, number> = {};
39 let tMark = T0;
40 const lap = (name: string) => {
41 const t = now();
42 timings[name] = Math.round(t - tMark) / 1000;
43 tMark = t;
44 };
45 const prog = (msg: string, frac: number) => onProgress?.(msg, Math.min(1, Math.max(0, frac)));
46 const warnings: string[] = [];
47 const rho0 = fe.airRho, c0 = fe.airC;
48 const L = fe.bodyLength;
49 const xb = fe.bridgePos * L;
50 const fHi = fe.fmax * 1.15; // subsystem modes are computed with a margin above fmax
52 // ---------------------------------------------------------------- geometry
53 prog("geometry", 0);
54 const outline = makeOutline(fe);
55 const arch = makeArch(fe, outline);
56 const h = fe.gridH;
57 const g = makeBodyGrid(fe, outline, arch, h);
58 const holes = makeFHoles(fe);
59 markFHoles(g, holes);
60 lap("geometry");
62 // ---------------------------------------------------------------- structure
63 prog("assembling structure", 0.02);
64 const S = buildStructure(fe, g);
65 if (Number(S.info.zeroStiffnessRows) > 0) warnings.push(`structure: ${S.info.zeroStiffnessRows} DOFs without stiffness`);
66 lap("assembly");
67 const sr = eigsSliced(S.K, S.M, -1, (2 * Math.PI * fHi) ** 2, {
68 countExp: 2,
69 perSlice: 60,
70 label: "structure",
71 seed: 3,
72 onProgress: (m, f) => prog(m, 0.04 + 0.46 * f),
73 });
74 warnings.push(...sr.warnings);
75 const ns = sr.lam.length;
76 const sOmega2 = Float64Array.from(sr.lam, (l) => Math.max(l, 0));
77 lap("structureModes");
79 // per structural mode: bridge coupling, energy splits, surface displacements
80 const topNodes: number[] = [], backNodes: number[] = [];
81 for (let k = 0; k < g.nx * g.ns; k++) {
82 if (S.topW[k] >= 0) topNodes.push(k);
83 if (S.backW[k] >= 0) backNodes.push(k);
84 }
85 const nTop = topNodes.length, nBack = backNodes.length;
86 const sBy: number[][] = [], sBz: number[][] = [], sPe: number[][] = [], sKe: number[][] = [];
87 const Wt = new Float64Array(ns * nTop), Wb = new Float64Array(ns * nBack);
88 for (let m = 0; m < ns; m++) {
89 const x = sr.vec[m];
90 const mi = modeInfo(S, x, sOmega2[m]);
91 sBy.push(mi.by);
92 sBz.push(mi.bz);
93 sPe.push(mi.pe);
94 sKe.push(mi.ke);
95 for (let a = 0; a < nTop; a++) Wt[m * nTop + a] = x[S.topW[topNodes[a]]];
96 for (let a = 0; a < nBack; a++) Wb[m * nBack + a] = x[S.backW[backNodes[a]]];
97 }
98 sr.vec.length = 0; // free
99 lap("structureInfo");
101 // ---------------------------------------------------------------- air cavity
102 prog("air cavity", 0.5);
103 const ha = Math.min(0.04, Math.max(0.022, 1.4 * h)); // air cells: ~6 per wavelength at 2.5 kHz
104 const cav = buildCavity(
105 0,
106 L,
107 outline.maxHalf,
108 ha,
109 (x, s) => Math.abs(s) < outline.halfWidth(x),
110 (x, s) => fe.ribDepth + arch(x, s),
111 );
112 const am = cavityModes(cav, c0, fHi, (m, f) => prog(m, 0.5 + 0.2 * f));
113 warnings.push(...am.warnings);
114 const na = am.omega.length;
115 lap("airModes");
117 // ψ on the plate surfaces
118 const topZ = (x: number, s: number) => fe.ribDepth + arch(x, s);
119 const psiTop = new Float64Array(na * nTop), psiBack = new Float64Array(na * nBack);
120 {
121 const stT = topNodes.map((k) => surfaceStencil(cav, nodeX(g, Math.floor(k / g.ns)), nodeS(g, k % g.ns), "top"));
122 const stB = backNodes.map((k) => surfaceStencil(cav, nodeX(g, Math.floor(k / g.ns)), nodeS(g, k % g.ns), "bottom"));
123 for (let j = 0; j < na; j++) {
124 const psi = am.psi[j];
125 for (let a = 0; a < nTop; a++) {
126 const st = stT[a];
127 let v = 0;
128 for (let q = 0; q < st.cells.length; q++) v += st.wts[q] * psi[st.cells[q]];
129 psiTop[j * nTop + a] = v;
130 }
131 for (let a = 0; a < nBack; a++) {
132 const st = stB[a];
133 let v = 0;
134 for (let q = 0; q < st.cells.length; q++) v += st.wts[q] * psi[st.cells[q]];
135 psiBack[j * nBack + a] = v;
136 }
137 }
138 }
140 // f-hole plugs
141 const plugs = holes.map((fh) => {
142 const lEff = slotEffectiveLength(fh.length, fh.width, fe.topThickness);
143 const pl = makePlug(cav, am, fh.area, lEff, fe.topThickness, fh.pts, rho0, c0);
144 return { fh, ...pl, x: fh.xc, s: fh.sc, z: topZ(fh.xc, fh.sc) };
145 });
146 const np = plugs.length;
147 const nm = ns + np;
148 const area = h * h;
149 const Smat = new Float64Array(nm * na);
150 for (let m = 0; m < ns; m++)
151 for (let j = 0; j < na; j++) {
152 let s = 0;
153 const ot = m * nTop, pt = j * nTop;
154 for (let a = 0; a < nTop; a++) s += Wt[ot + a] * psiTop[pt + a];
155 let sb = 0;
156 const ob = m * nBack, pb = j * nBack;
157 for (let a = 0; a < nBack; a++) sb += Wb[ob + a] * psiBack[pb + a];
158 Smat[m * na + j] = area * (s - sb); // back outward normal is −z
159 }
160 for (let q = 0; q < np; q++) {
161 const pl = plugs[q];
162 const f = pl.A / Math.sqrt(pl.mp);
163 for (let j = 0; j < na; j++) Smat[(ns + q) * na + j] = f * pl.psiBar[j];
164 }
165 const omega2m = new Float64Array(nm);
166 omega2m.set(sOmega2);
167 const Dj = new Float64Array(na).fill(rho0 * c0 * c0);
168 lap("couplingSetup");
170 // ---------------------------------------------------------------- coupled problem
171 prog(`coupled problem (${nm} + ${na} DOFs)`, 0.72);
172 const cr = solveCoupled({ omega2: omega2m, S: Smat, airOmega: am.omega, D: Dj });
173 lap("coupledEig");
175 // ---------------------------------------------------------------- radiation setup (patches)
176 prog("radiation", 0.9);
177 const topArea = outline.area;
178 const aEq = 0.5 * Math.sqrt(topArea / Math.PI);
179 const mic = { x: xb, s: 0, z: topZ(xb, 0) + fe.micDistance };
180 interface Level {
181 L: number;
182 nP: number;
183 px: Float64Array;
184 py: Float64Array;
185 pz: Float64Array;
186 nz: Float64Array; // outward normal z component (±1)
187 Q: Float64Array; // ns × (nP - np) patch volumes per structural mode
188 R: Float64Array; // nP × nP distances
189 }
190 const levels: Level[] = [];
191 for (let Lv = 1; Lv <= 4; Lv++) {
192 const ids = new Map<number, number>();
193 const memb: { plate: number; a: number; patch: number }[] = [];
194 const px: number[] = [], py: number[] = [], pz: number[] = [], pn: number[] = [], cnt: number[] = [];
195 const addNode = (plate: number, a: number, k: number) => {
196 const i = Math.floor(k / g.ns), j = k % g.ns;
197 const key = (plate * 100000 + (i >> Lv)) * 100000 + (j >> Lv);
198 let id = ids.get(key);
199 if (id === undefined) {
200 id = px.length;
201 ids.set(key, id);
202 px.push(0);
203 py.push(0);
204 pz.push(0);
205 pn.push(plate === 0 ? 1 : -1);
206 cnt.push(0);
207 }
208 const x = nodeX(g, i), s = nodeS(g, j);
209 px[id] += x;
210 py[id] += s;
211 pz[id] += plate === 0 ? topZ(x, s) : 0;
212 cnt[id]++;
213 memb.push({ plate, a, patch: id });
214 };
215 topNodes.forEach((k, a) => addNode(0, a, k));
216 backNodes.forEach((k, a) => addNode(1, a, k));
217 const nS = px.length;
218 for (let id = 0; id < nS; id++) {
219 px[id] /= cnt[id];
220 py[id] /= cnt[id];
221 pz[id] /= cnt[id];
222 }
223 for (const pl of plugs) {
224 px.push(pl.x);
225 py.push(pl.s);
226 pz.push(pl.z);
227 pn.push(1);
228 }
229 const nP = px.length;
230 const Q = new Float64Array(ns * nS);
231 for (let m = 0; m < ns; m++) {
232 const o = m * nS;
233 for (const mb of memb) {
234 const v = mb.plate === 0 ? Wt[m * nTop + mb.a] : -Wb[m * nBack + mb.a];
235 Q[o + mb.patch] += v * area;
236 }
237 }
238 const R = new Float64Array(nP * nP);
239 for (let a = 0; a < nP; a++)
240 for (let b = 0; b < nP; b++) R[a * nP + b] = Math.hypot(px[a] - px[b], py[a] - py[b], pz[a] - pz[b]);
241 levels.push({ L: Lv, nP, px: Float64Array.from(px), py: Float64Array.from(py), pz: Float64Array.from(pz), nz: Float64Array.from(pn), Q, R });
242 }
243 lap("radiationSetup");
245 // ---------------------------------------------------------------- coupled modes -> BodyModes
246 const losses = [fe.topLoss, fe.backLoss, 0.02, fe.topLoss]; // top, back, bridge, post
247 const modes: BodyMode[] = [];
248 const diag: { f: number; ke: number[]; pe: number[]; zParts: number[] }[] = [];
249 const nc = cr.nc;
250 const kept: number[] = [];
251 let dropped = 0;
252 for (let k = 0; k < nc; k++) {
253 const w2 = cr.omega2[k];
254 const f = Math.sqrt(Math.max(w2, 0)) / (2 * Math.PI);
255 if (f > fe.fmax) break;
256 if (f < 5) {
257 dropped++;
258 continue; // flow-through (one hole in, the other out) mode: no stiffness, no bridge coupling
259 }
260 const w = 2 * Math.PI * f;
261 const y = cr.Y.subarray(k * nc, (k + 1) * nc);
262 const by = [0, 0, 0, 0], bz = [0, 0, 0, 0];
263 const peG = [0, 0, 0, 0], keG = [0, 0, 0];
264 let pePlate = 0;
265 for (let m = 0; m < ns; m++) {
266 const e = y[m];
267 if (e === 0) continue;
268 for (let i = 0; i < 4; i++) {
269 by[i] += e * sBy[m][i];
270 bz[i] += e * sBz[m][i];
271 }
272 const e2 = e * e;
273 const pe = e2 * sOmega2[m];
274 pePlate += pe;
275 for (let gI = 0; gI < 4; gI++) peG[gI] += pe * sPe[m][gI];
276 for (let gI = 0; gI < 3; gI++) keG[gI] += e2 * sKe[m][gI];
277 }
278 let kePlug = 0, keAir = 0;
279 for (let q = 0; q < np; q++) kePlug += y[ns + q] ** 2;
280 for (let r = 0; r < nc - nm; r++) keAir += y[nm + r] ** 2;
281 const peAir = Math.max(0, w2 - pePlate);
282 // material + cavity losses
283 let zMat = 0;
284 for (let gI = 0; gI < 4; gI++) zMat += losses[gI] * peG[gI];
285 zMat = (0.5 * (zMat + peAir / fe.cavityQ)) / w2;
286 // viscous losses in the f-holes
287 let zVisc = 0;
288 for (let q = 0; q < np; q++) {
289 const pl = plugs[q];
290 const perim = 2 * (pl.fh.length + pl.fh.width);
291 const lv = fe.topThickness + 0.5 * (pl.lEff - fe.topThickness);
292 const Rm = perim * lv * Math.sqrt((w * rho0 * AIR_MU) / 2);
293 zVisc += (y[ns + q] ** 2 * Rm) / (2 * pl.mp * w);
294 }
295 // radiation: patch sources at the coarsest adequate level
296 const kw = w / c0;
297 const lam = (2 * Math.PI) / kw;
298 let lvl = levels[0];
299 for (const lv of levels) if (h * 2 ** lv.L <= lam / 3) lvl = lv;
300 const nP = lvl.nP, nS = nP - np;
301 const qv = new Float64Array(nP);
302 for (let m = 0; m < ns; m++) {
303 const e = y[m];
304 if (e === 0) continue;
305 const o = m * nS;
306 for (let a = 0; a < nS; a++) qv[a] += e * lvl.Q[o + a];
307 }
308 for (let q = 0; q < np; q++) qv[nS + q] = (plugs[q].A / Math.sqrt(plugs[q].mp)) * y[ns + q];
309 const ka = kw * aEq;
310 const gk = (ka * ka) / (1 + ka * ka);
311 let pRe = 0, pIm = 0;
312 for (let a = 0; a < nP; a++) {
313 const dx = mic.x - lvl.px[a], dy = mic.s - lvl.py[a], dz = mic.z - lvl.pz[a];
314 const R = Math.hypot(dx, dy, dz);
315 const cosT = (lvl.nz[a] * dz) / R;
316 const amp = (rho0 * qv[a] * (1 + gk * cosT)) / (4 * Math.PI * R);
317 const ph = -kw * (R - fe.micDistance);
318 pRe += amp * Math.cos(ph);
319 pIm += amp * Math.sin(ph);
320 }
321 let pw = 0;
322 for (let a = 0; a < nP; a++) {
323 const qa = qv[a];
324 if (qa === 0) continue;
325 let s = 0;
326 const ro = a * nP;
327 for (let b = a + 1; b < nP; b++) {
328 const x = kw * lvl.R[ro + b];
329 s += qv[b] * (x < 1e-8 ? 1 : Math.sin(x) / x);
330 }
331 pw += qa * (qa + 2 * s);
332 }
333 const zRad = Math.max(0, (rho0 * w * (1 + gk) * pw) / (8 * Math.PI * c0));
334 const zeta = zMat + zVisc + zRad;
335 modes.push({ f, zeta, by, bz, radRe: pRe, radIm: pIm });
336 kept.push(k);
337 diag.push({ f, ke: [keG[0], keG[1], keG[2], kePlug, keAir], pe: [...peG, peAir], zParts: [zMat, zVisc, zRad] });
338 }
339 // labels
340 let a0Found = false;
341 const counters: Record<string, number> = { top: 0, back: 0, bridge: 0, air: 0 };
342 let a0f = NaN;
343 for (let k = 0; k < modes.length; k++) {
344 const ke = diag[k].ke;
345 const airKe = ke[3] + ke[4];
346 if (!a0Found && ke[3] >= 0.25) {
347 modes[k].label = "A0";
348 a0Found = true;
349 a0f = modes[k].f;
350 continue;
351 }
352 const cand: [string, number][] = [
353 ["top", ke[0]],
354 ["back", ke[1]],
355 ["bridge", ke[2]],
356 ["air", airKe],
357 ];
358 cand.sort((a, b) => b[1] - a[1]);
359 const name = cand[0][0];
360 counters[name]++;
361 modes[k].label = `${name}${counters[name]}`;
362 }
363 lap("modes");
364 const nFE = modes.length;
366 // ---------------------------------------------------------------- mode shapes for the 3D view
367 if (opts.shapes) {
368 prog("mode shapes", 0.95);
369 const nCols = cav.nCols, kz = Math.floor(cav.nz / 2);
370 const top = new Float32Array(nFE * nTop), back = new Float32Array(nFE * nBack);
371 const air = new Float32Array(nFE * nCols), plug = new Float32Array(nFE * np);
372 const Pj = new Float64Array(na);
373 const tt = new Float64Array(nTop), bb = new Float64Array(nBack);
374 for (let q = 0; q < nFE; q++) {
375 const y = cr.Y.subarray(kept[q] * nc, (kept[q] + 1) * nc);
376 tt.fill(0); bb.fill(0);
377 for (let m = 0; m < ns; m++) {
378 const e = y[m];
379 if (e === 0) continue;
380 const ot = m * nTop, ob = m * nBack;
381 for (let a = 0; a < nTop; a++) tt[a] += e * Wt[ot + a];
382 for (let a = 0; a < nBack; a++) bb[a] += e * Wb[ob + a];
383 }
384 top.set(tt, q * nTop); back.set(bb, q * nBack);
385 // cavity pressure P_j = -D_j (S_j^T eta - r_j), r_j = Y_r omega_j / sqrt(D_j) (0 for the static mode)
386 for (let j = 0; j < na; j++) {
387 let sv = 0;
388 for (let m = 0; m < nm; m++) sv += Smat[m * na + j] * y[m];
389 Pj[j] = -Dj[j] * sv;
390 }
391 for (let r = 0; r < cr.rj.length; r++) { const j = cr.rj[r]; Pj[j] += Dj[j] * (y[nm + r] * am.omega[j]) / Math.sqrt(Dj[j]); }
392 for (let c = 0; c < nCols; c++) {
393 let v = 0;
394 const cell = c * cav.nz + kz;
395 for (let j = 0; j < na; j++) v += Pj[j] * am.psi[j][cell];
396 air[q * nCols + c] = v;
397 }
398 for (let qq = 0; qq < np; qq++) plug[q * np + qq] = y[ns + qq] / Math.sqrt(plugs[qq].mp);
399 }
400 opts.shapes({
401 feKey: feInputsKey(p),
402 nModes: nFE,
403 grid: { nx: g.nx, ns: g.ns, h: g.h, x0: g.x0, s0: g.s0 },
404 topNodes: Int32Array.from(topNodes),
405 backNodes: Int32Array.from(backNodes),
406 cav: { nxA: cav.nxA, nsA: cav.nsA, x0: cav.x0, s0: cav.s0, ha: cav.ha, col: Int32Array.from(cav.col), colH: Float32Array.from(cav.colH) },
407 plugs: plugs.map((pl) => ({ xc: pl.fh.xc, sc: pl.fh.sc, tilt: pl.fh.tilt, length: pl.fh.length, width: pl.fh.width })),
408 top, back, air, plug,
409 });
410 lap("shapes");
411 }
413 // ---------------------------------------------------------------- statistical continuation
414 let nStat = 0;
415 let statInfo: Record<string, number> = {};
416 if (fe.statAbove && nFE > 10) {
417 // continue the structural modes only (air-dominated cavity modes radiate and couple weakly)
418 const structural = modes.filter((_, k) => diag[k].ke[0] + diag[k].ke[1] + diag[k].ke[2] >= 0.5);
419 const band = structural.filter((m) => m.f >= 0.6 * fe.fmax && m.f <= fe.fmax);
420 const density = band.length / (0.4 * fe.fmax);
421 const near = structural.filter((m) => m.f >= 0.7 * fe.fmax);
422 const loss = (2 * near.reduce((s, m) => s + m.zeta, 0)) / Math.max(1, near.length);
423 const plateMass = Number(S.info.topPlateMass) + Number(S.info.backPlateMass);
424 const fHigh = Math.max(p.body.statFHigh, fe.fmax * 1.5);
425 const stat = statisticalModes({
426 fLow: fe.fmax,
427 fHigh,
428 density: Math.max(density, 1e-3),
429 mass: plateMass,
430 lossFactor: () => loss,
431 lateralRatio: p.body.lateralRatio,
432 // unfiltered here (bridge resonances far away); the FE bridge's own filters are applied below
433 bridgeF: 1e9,
434 bridgeQ: 1,
435 radCorner: p.body.radCorner,
436 radScale: p.body.radScale,
437 plateArea: topArea,
438 notchY: S.notchY,
439 micDistance: fe.micDistance,
440 airRho: rho0,
441 seed: p.body.seed + 101,
442 });
443 // the FE bridge above fmax: vertical/rotation through its stiff vertical resonance, lateral
444 // through the in-plane rocking resonance (rigid-feet values; lateral Q from the parametric
445 // body, vertical Q = 2: the bouncing bridge is heavily loaded by the plate)
446 const fV = Number(S.info.bridgeVerticalHz), fL = Number(S.info.bridgeLateralHz);
447 for (const m of stat) {
448 const gv = resGain(m.f, fV, 2), gl = resGain(m.f, fL, p.body.bridgeQ);
449 m.by = m.by.map((v) => v * gv);
450 m.bz = m.bz.map((v) => v * gl);
451 }
452 // match the rms levels of the FE modes just below fmax so the spectrum continues smoothly
453 const rms = (arr: BodyMode[], fn: (m: BodyMode) => number) => Math.sqrt(arr.reduce((s, m) => s + fn(m) ** 2, 0) / Math.max(1, arr.length));
454 const statNear = stat.filter((m) => m.f <= fe.fmax / 0.7);
455 if (statNear.length > 3 && near.length > 3) {
456 const ky = rms(near, (m) => Math.hypot(...m.by) / 2) / Math.max(1e-30, rms(statNear, (m) => Math.hypot(...m.by) / 2));
457 const kz = rms(near, (m) => m.bz[0]) / Math.max(1e-30, rms(statNear, (m) => m.bz[0]));
458 const kr = rms(near, (m) => Math.hypot(m.radRe, m.radIm)) / Math.max(1e-30, rms(statNear, (m) => Math.hypot(m.radRe, m.radIm)));
459 for (const m of stat) {
460 m.by = m.by.map((v) => v * ky);
461 m.bz = m.bz.map((v) => v * kz);
462 m.radRe *= kr;
463 m.radIm *= kr;
464 m.label = "stat";
465 }
466 statInfo = { density, loss, plateMass, scaleBy: ky, scaleBz: kz, scaleRad: kr, fHigh };
467 } else {
468 for (const m of stat) m.label = "stat";
469 statInfo = { density, loss, plateMass, fHigh };
470 }
471 modes.push(...stat);
472 nStat = stat.length;
473 lap("statistical");
474 }
476 const total = (now() - T0) / 1000;
477 prog("done", 1);
478 return {
479 name: "fe",
480 source: "fe",
481 modes,
482 meta: {
483 feKey: feInputsKey(p),
484 timings: { ...timings, total },
485 grid: { h, nx: g.nx, ns: g.ns, topNodes: nTop, backNodes: nBack, holeNodes: countType(g.type, HOLE) },
486 structure: { ...S.info, modes: ns, slices: sr.slices, lanczosSteps: sr.steps, factorizations: sr.factorizations, groups: GROUP_NAMES },
487 air: { h: cav.ha, nz: cav.nz, columns: cav.nCols, cells: cav.n, volume: cav.volume, modes: na, lanczosSteps: am.steps, firstHz: am.omega.slice(1, 6).map((w) => w / 2 / Math.PI) },
488 plugs: plugs.map((pl) => ({ area: pl.A, lEff: pl.lEff, nearFieldDl: pl.dl, lUsed: pl.lUsed, mass: pl.mp })),
489 helmholtzEstimateHz:
490 np > 0 ? (c0 / (2 * Math.PI)) * Math.sqrt(plugs.reduce((s, pl) => s + pl.A / pl.lEff, 0) / cav.volume) : null,
491 coupled: { dofs: nc, feModes: nFE, droppedLow: dropped },
492 a0Hz: a0f,
493 topArea,
494 statModes: nStat,
495 stat: statInfo,
496 diag: diag.map((d) => ({ f: d.f, ke: d.ke.map((v) => +v.toFixed(4)), pe: d.pe.map((v) => +(v / (2 * Math.PI * d.f) ** 2).toFixed(4)), z: d.zParts.map((v) => +v.toExponential(3)) })),
497 warnings,
498 },
499 };
502/** |1 / (1 - r² + i r/Q)|, r = f/f0 */
503function resGain(f: number, f0: number, q: number): number {
504 const r = f / f0;
505 return 1 / Math.hypot(1 - r * r, r / q);
508function countType(t: Int8Array, v: number): number {
509 let n = 0;
510 for (let k = 0; k < t.length; k++) if (t[k] === v) n++;
511 return n;
↑↓ move↵openescclose