/ music / acoustic-base
Sign in
music / acoustic-base
acoustic-base / src / fe / air.ts
205 lines · 7.0 KBBlameHistoryRaw
1// The air cavity: cell-centred finite-volume Laplacian with rigid (Neumann) walls on a
2// column grid. Each (x, s) column inside the outline spans the cavity from the back (z = 0) to the
3// top (z = H(x, s) = ribDepth + arching) and is divided into nz equal layers (terrain-following
4// "sigma" layers; the small slope terms of the arching are neglected).
5// ∫|∇p|² ≈ Σ_faces (area / distance) (p_a − p_b)², ∫p² ≈ Σ_cells vol p²,
6// so K ψ = k² M ψ gives the rigid-wall modes, ω = c k. ψ is M-normalised: ∫ψ² dV = 1.
8import { Terms, Skyline } from "./sparse.ts";
9import { eigsSliced } from "./eigen.ts";
11export interface Cavity {
12 ha: number;
13 nxA: number;
14 nsA: number;
15 x0: number;
16 s0: number;
17 nz: number;
18 /** horizontal cell (i*nsA + j) -> column index, or −1 */
19 col: Int32Array;
20 colX: Float64Array;
21 colS: Float64Array;
22 colH: Float64Array;
23 nCols: number;
24 /** number of cells (DOFs): column c, layer k -> c*nz + k */
25 n: number;
26 volume: number;
29export function buildCavity(
30 xlo: number,
31 xhi: number,
32 smax: number,
33 ha: number,
34 inside: (x: number, s: number) => boolean,
35 height: (x: number, s: number) => number,
36 nzOverride?: number,
37): Cavity {
38 const nxA = Math.max(1, Math.round((xhi - xlo) / ha));
39 const hx = (xhi - xlo) / nxA;
40 if (Math.abs(hx - ha) > 1e-9 * ha) ha = hx;
41 const nsA = Math.max(1, Math.round((2 * smax) / ha));
42 const x0 = xlo, s0 = -(nsA * ha) / 2;
43 const col = new Int32Array(nxA * nsA).fill(-1);
44 const cx: number[] = [], cs: number[] = [], ch: number[] = [];
45 for (let i = 0; i < nxA; i++)
46 for (let j = 0; j < nsA; j++) {
47 const x = x0 + (i + 0.5) * ha, s = s0 + (j + 0.5) * ha;
48 if (!inside(x, s)) continue;
49 col[i * nsA + j] = cx.length;
50 cx.push(x);
51 cs.push(s);
52 ch.push(height(x, s));
53 }
54 const hmax = Math.max(...ch);
55 const nz = nzOverride ?? Math.max(2, Math.round(hmax / ha));
56 let volume = 0;
57 for (const H of ch) volume += ha * ha * H;
58 return {
59 ha,
60 nxA,
61 nsA,
62 x0,
63 s0,
64 nz,
65 col,
66 colX: Float64Array.from(cx),
67 colS: Float64Array.from(cs),
68 colH: Float64Array.from(ch),
69 nCols: cx.length,
70 n: cx.length * nz,
71 volume,
72 };
75export function cavityMatrices(cv: Cavity): { K: Skyline; M: Float64Array } {
76 const { nxA, nsA, nz, ha, col, colH } = cv;
77 const T = new Terms();
78 const M = new Float64Array(cv.n);
79 for (let c = 0; c < cv.nCols; c++) {
80 const dz = colH[c] / nz;
81 for (let k = 0; k < nz; k++) M[c * nz + k] = ha * ha * dz;
82 for (let k = 0; k + 1 < nz; k++) T.add([c * nz + k, c * nz + k + 1], [1, -1], (ha * ha) / dz, 0);
83 }
84 for (let i = 0; i < nxA; i++)
85 for (let j = 0; j < nsA; j++) {
86 const c = col[i * nsA + j];
87 if (c < 0) continue;
88 const nb = [i + 1 < nxA ? col[(i + 1) * nsA + j] : -1, j + 1 < nsA ? col[i * nsA + j + 1] : -1];
89 for (const c2 of nb) {
90 if (c2 < 0) continue;
91 const g = 0.5 * (colH[c] + colH[c2]) / nz; // face area / distance = ha * dz_mean / ha
92 for (let k = 0; k < nz; k++) T.add([c * nz + k, c2 * nz + k], [1, -1], g, 0);
93 }
94 }
95 return { K: Skyline.fromTerms(cv.n, T.pack()), M };
98export interface CavityModes {
99 /** angular frequencies (rad/s); index 0 is the uniform compression mode (ω = 0) */
100 omega: number[];
101 psi: Float64Array[];
102 warnings: string[];
103 steps: number;
106export function cavityModes(cv: Cavity, c: number, fHi: number, onProgress?: (msg: string, frac: number) => void): CavityModes {
107 const { K, M } = cavityMatrices(cv);
108 const kHi = (2 * Math.PI * fHi) / c;
109 const r = eigsSliced(K, M, -1, kHi * kHi, {
110 countExp: 3,
111 perSlice: 60,
112 label: "air",
113 toHz: (lam) => (c * Math.sqrt(Math.max(lam, 0))) / (2 * Math.PI),
114 onProgress,
115 seed: 7,
116 });
117 const omega = r.lam.map((l) => c * Math.sqrt(Math.max(l, 0)));
118 // the compression mode: make it exactly uniform (it is, to round-off)
119 if (r.vec.length > 0 && omega[0] < 1e-3 * (omega[1] ?? 1)) {
120 omega[0] = 0;
121 const v = r.vec[0];
122 const val = 1 / Math.sqrt(cv.volume);
123 const sgn = v[0] >= 0 ? 1 : -1;
124 v.fill(sgn * val);
125 }
126 return { omega, psi: r.vec, warnings: r.warnings, steps: r.steps };
129/** Interpolation of the top-layer (or bottom-layer) cell values at (x, s): cell indices and weights. */
130export function surfaceStencil(cv: Cavity, x: number, s: number, which: "top" | "bottom"): { cells: number[]; wts: number[] } {
131 const { ha, nsA, nxA, col, nz } = cv;
132 const k = which === "top" ? nz - 1 : 0;
133 const fi = (x - cv.x0) / ha - 0.5, fj = (s - cv.s0) / ha - 0.5;
134 const i = Math.floor(fi), j = Math.floor(fj);
135 const a = fi - i, b = fj - j;
136 const cells: number[] = [], wts: number[] = [];
137 const cand: [number, number, number][] = [
138 [i, j, (1 - a) * (1 - b)],
139 [i + 1, j, a * (1 - b)],
140 [i, j + 1, (1 - a) * b],
141 [i + 1, j + 1, a * b],
142 ];
143 let ws = 0;
144 for (const [ii, jj, w] of cand) {
145 if (ii < 0 || jj < 0 || ii >= nxA || jj >= nsA || w <= 0) continue;
146 const c = col[ii * nsA + jj];
147 if (c < 0) continue;
148 cells.push(c * nz + k);
149 wts.push(w);
150 ws += w;
151 }
152 if (ws > 1e-6) {
153 for (let q = 0; q < wts.length; q++) wts[q] /= ws;
154 return { cells, wts };
155 }
156 // nearest column
157 let best = -1, bd = Infinity;
158 for (let c = 0; c < cv.nCols; c++) {
159 const d = (cv.colX[c] - x) ** 2 + (cv.colS[c] - s) ** 2;
160 if (d < bd) {
161 bd = d;
162 best = c;
163 }
164 }
165 return { cells: [best * nz + k], wts: [1] };
168export interface Plug {
169 /** opening area, m² */
170 A: number;
171 /** analytic effective neck length (both end corrections + wall thickness), m */
172 lEff: number;
173 /** inner near-field length already represented by the truncated cavity-mode sum, m */
174 dl: number;
175 /** neck length used for the plug mass, m */
176 lUsed: number;
177 /** plug mass ρ0 A lUsed, kg */
178 mp: number;
179 /** cavity mode shapes averaged over the opening */
180 psiBar: Float64Array;
183/**
184 * An opening in the top as an air plug (mass ρ0 A l, outward displacement ξ, zero stiffness to the
185 * outside, p_out ≈ 0), coupled to the cavity modes through ψ averaged over its sample points.
186 * The truncated modal sum adds an inertance ρ0 A² Σ_j ψ̄_j²/k_j² (the cavity-side near field
187 * resolved by the air grid); it is removed from the analytic end correction to avoid counting it
188 * twice (but at most half of the end correction is removed).
189 */
190export function makePlug(cv: Cavity, am: CavityModes, A: number, lEff: number, thickness: number, pts: { x: number; s: number }[], rho0: number, c: number): Plug {
191 const na = am.omega.length;
192 const psiBar = new Float64Array(na);
193 for (const pt of pts) {
194 const st = surfaceStencil(cv, pt.x, pt.s, "top");
195 for (let j = 0; j < na; j++) {
196 let v = 0;
197 for (let q = 0; q < st.cells.length; q++) v += st.wts[q] * am.psi[j][st.cells[q]];
198 psiBar[j] += v / pts.length;
199 }
200 }
201 let dl = 0;
202 for (let j = 0; j < na; j++) if (am.omega[j] > 0) dl += (A * psiBar[j] ** 2 * c * c) / am.omega[j] ** 2;
203 const lUsed = Math.max(lEff - dl, thickness + 0.5 * (lEff - thickness));
204 return { A, lEff, dl, lUsed, mp: rho0 * A * lUsed, psiBar };
↑↓ move↵openescclose