// The air cavity: cell-centred finite-volume Laplacian with rigid (Neumann) walls on a // column grid. Each (x, s) column inside the outline spans the cavity from the back (z = 0) to the // top (z = H(x, s) = ribDepth + arching) and is divided into nz equal layers (terrain-following // "sigma" layers; the small slope terms of the arching are neglected). // ∫|∇p|² ≈ Σ_faces (area / distance) (p_a − p_b)², ∫p² ≈ Σ_cells vol p², // so K ψ = k² M ψ gives the rigid-wall modes, ω = c k. ψ is M-normalised: ∫ψ² dV = 1. import { Terms, Skyline } from "./sparse.ts"; import { eigsSliced } from "./eigen.ts"; export interface Cavity { ha: number; nxA: number; nsA: number; x0: number; s0: number; nz: number; /** horizontal cell (i*nsA + j) -> column index, or −1 */ col: Int32Array; colX: Float64Array; colS: Float64Array; colH: Float64Array; nCols: number; /** number of cells (DOFs): column c, layer k -> c*nz + k */ n: number; volume: number; } export function buildCavity( xlo: number, xhi: number, smax: number, ha: number, inside: (x: number, s: number) => boolean, height: (x: number, s: number) => number, nzOverride?: number, ): Cavity { const nxA = Math.max(1, Math.round((xhi - xlo) / ha)); const hx = (xhi - xlo) / nxA; if (Math.abs(hx - ha) > 1e-9 * ha) ha = hx; const nsA = Math.max(1, Math.round((2 * smax) / ha)); const x0 = xlo, s0 = -(nsA * ha) / 2; const col = new Int32Array(nxA * nsA).fill(-1); const cx: number[] = [], cs: number[] = [], ch: number[] = []; for (let i = 0; i < nxA; i++) for (let j = 0; j < nsA; j++) { const x = x0 + (i + 0.5) * ha, s = s0 + (j + 0.5) * ha; if (!inside(x, s)) continue; col[i * nsA + j] = cx.length; cx.push(x); cs.push(s); ch.push(height(x, s)); } const hmax = Math.max(...ch); const nz = nzOverride ?? Math.max(2, Math.round(hmax / ha)); let volume = 0; for (const H of ch) volume += ha * ha * H; return { ha, nxA, nsA, x0, s0, nz, col, colX: Float64Array.from(cx), colS: Float64Array.from(cs), colH: Float64Array.from(ch), nCols: cx.length, n: cx.length * nz, volume, }; } export function cavityMatrices(cv: Cavity): { K: Skyline; M: Float64Array } { const { nxA, nsA, nz, ha, col, colH } = cv; const T = new Terms(); const M = new Float64Array(cv.n); for (let c = 0; c < cv.nCols; c++) { const dz = colH[c] / nz; for (let k = 0; k < nz; k++) M[c * nz + k] = ha * ha * dz; for (let k = 0; k + 1 < nz; k++) T.add([c * nz + k, c * nz + k + 1], [1, -1], (ha * ha) / dz, 0); } for (let i = 0; i < nxA; i++) for (let j = 0; j < nsA; j++) { const c = col[i * nsA + j]; if (c < 0) continue; const nb = [i + 1 < nxA ? col[(i + 1) * nsA + j] : -1, j + 1 < nsA ? col[i * nsA + j + 1] : -1]; for (const c2 of nb) { if (c2 < 0) continue; const g = 0.5 * (colH[c] + colH[c2]) / nz; // face area / distance = ha * dz_mean / ha for (let k = 0; k < nz; k++) T.add([c * nz + k, c2 * nz + k], [1, -1], g, 0); } } return { K: Skyline.fromTerms(cv.n, T.pack()), M }; } export interface CavityModes { /** angular frequencies (rad/s); index 0 is the uniform compression mode (ω = 0) */ omega: number[]; psi: Float64Array[]; warnings: string[]; steps: number; } export function cavityModes(cv: Cavity, c: number, fHi: number, onProgress?: (msg: string, frac: number) => void): CavityModes { const { K, M } = cavityMatrices(cv); const kHi = (2 * Math.PI * fHi) / c; const r = eigsSliced(K, M, -1, kHi * kHi, { countExp: 3, perSlice: 60, label: "air", toHz: (lam) => (c * Math.sqrt(Math.max(lam, 0))) / (2 * Math.PI), onProgress, seed: 7, }); const omega = r.lam.map((l) => c * Math.sqrt(Math.max(l, 0))); // the compression mode: make it exactly uniform (it is, to round-off) if (r.vec.length > 0 && omega[0] < 1e-3 * (omega[1] ?? 1)) { omega[0] = 0; const v = r.vec[0]; const val = 1 / Math.sqrt(cv.volume); const sgn = v[0] >= 0 ? 1 : -1; v.fill(sgn * val); } return { omega, psi: r.vec, warnings: r.warnings, steps: r.steps }; } /** Interpolation of the top-layer (or bottom-layer) cell values at (x, s): cell indices and weights. */ export function surfaceStencil(cv: Cavity, x: number, s: number, which: "top" | "bottom"): { cells: number[]; wts: number[] } { const { ha, nsA, nxA, col, nz } = cv; const k = which === "top" ? nz - 1 : 0; const fi = (x - cv.x0) / ha - 0.5, fj = (s - cv.s0) / ha - 0.5; const i = Math.floor(fi), j = Math.floor(fj); const a = fi - i, b = fj - j; const cells: number[] = [], wts: number[] = []; const cand: [number, number, number][] = [ [i, j, (1 - a) * (1 - b)], [i + 1, j, a * (1 - b)], [i, j + 1, (1 - a) * b], [i + 1, j + 1, a * b], ]; let ws = 0; for (const [ii, jj, w] of cand) { if (ii < 0 || jj < 0 || ii >= nxA || jj >= nsA || w <= 0) continue; const c = col[ii * nsA + jj]; if (c < 0) continue; cells.push(c * nz + k); wts.push(w); ws += w; } if (ws > 1e-6) { for (let q = 0; q < wts.length; q++) wts[q] /= ws; return { cells, wts }; } // nearest column let best = -1, bd = Infinity; for (let c = 0; c < cv.nCols; c++) { const d = (cv.colX[c] - x) ** 2 + (cv.colS[c] - s) ** 2; if (d < bd) { bd = d; best = c; } } return { cells: [best * nz + k], wts: [1] }; } export interface Plug { /** opening area, m² */ A: number; /** analytic effective neck length (both end corrections + wall thickness), m */ lEff: number; /** inner near-field length already represented by the truncated cavity-mode sum, m */ dl: number; /** neck length used for the plug mass, m */ lUsed: number; /** plug mass ρ0 A lUsed, kg */ mp: number; /** cavity mode shapes averaged over the opening */ psiBar: Float64Array; } /** * An opening in the top as an air plug (mass ρ0 A l, outward displacement ξ, zero stiffness to the * outside, p_out ≈ 0), coupled to the cavity modes through ψ averaged over its sample points. * The truncated modal sum adds an inertance ρ0 A² Σ_j ψ̄_j²/k_j² (the cavity-side near field * resolved by the air grid); it is removed from the analytic end correction to avoid counting it * twice (but at most half of the end correction is removed). */ export function makePlug(cv: Cavity, am: CavityModes, A: number, lEff: number, thickness: number, pts: { x: number; s: number }[], rho0: number, c: number): Plug { const na = am.omega.length; const psiBar = new Float64Array(na); for (const pt of pts) { const st = surfaceStencil(cv, pt.x, pt.s, "top"); for (let j = 0; j < na; j++) { let v = 0; for (let q = 0; q < st.cells.length; q++) v += st.wts[q] * am.psi[j][st.cells[q]]; psiBar[j] += v / pts.length; } } let dl = 0; for (let j = 0; j < na; j++) if (am.omega[j] > 0) dl += (A * psiBar[j] ** 2 * c * c) / am.omega[j] ** 2; const lUsed = Math.max(lEff - dl, thickness + 0.5 * (lEff - thickness)); return { A, lEff, dl, lUsed, mp: rho0 * A * lUsed, psiBar }; }