"""Locally reacting wall models (SI units). A wall material is a specific acoustic admittance (wall-normal velocity into the wall per unit pressure, m/(Pa s)): Y(w) = G + sum_b 1 / (j w M_b + R_b + K_b / (j w)) i.e. a real conductance plus parallel series mass-damper-spring branches (per unit area: M kg/m^2, R N s/m^3, K N/m^3). Two physical effects are mapped onto this form: * yielding soft-tissue wall: one branch (M, R, K) (Ishizaka et al. 1975; values as used by Story/Titze and VocalTractLab); * viscous + thermal boundary layer: equivalent admittance for grazing incidence Y_bl = sqrt(j w) (sqrt(nu) + (gamma - 1) sqrt(alpha)) / (rho c^2) approximated over [f_lo, f_hi] by G + sum of mass-less (R, K) branches (non-negative fit), so it is passive by construction. """ from __future__ import annotations from dataclasses import dataclass, field import numpy as np from scipy.optimize import nnls # air at body temperature, humid C_AIR = 350.0 # m/s RHO_AIR = 1.14 # kg/m^3 NU_AIR = 1.63e-5 # kinematic viscosity, m^2/s ALPHA_AIR = 2.36e-5 # thermal diffusivity, m^2/s GAMMA_AIR = 1.4 def boundary_layer_coefficient(c=C_AIR, rho=RHO_AIR, nu=NU_AIR, alpha=ALPHA_AIR, gamma=GAMMA_AIR) -> float: """C such that Y_bl(w) = C sqrt(j w) (grazing incidence, viscous + thermal).""" return (np.sqrt(nu) + (gamma - 1.0) * np.sqrt(alpha)) / (rho * c * c) def fit_sqrt_jw(n_branch=4, f_lo=80.0, f_hi=16000.0): """Fit sqrt(jw) ~ g0 + sum_k a_k jw/(jw + w_k), a_k, g0 >= 0 (relative LS). Returns (g0, a[n_branch], w[n_branch]) and the max relative error in the band.""" f = np.geomspace(f_lo, f_hi, 400) w = 2 * np.pi * f target = np.sqrt(1j * w) # pole frequencies: log-spaced, extended above the band (sqrt keeps growing) wk = 2 * np.pi * np.geomspace(f_lo * 1.5, f_hi * 4.0, n_branch) cols = [np.ones_like(w, dtype=complex)] + [1j * w / (1j * w + x) for x in wk] A = np.stack(cols, axis=1) / target[:, None] A_ri = np.concatenate([A.real, A.imag], axis=0) b_ri = np.concatenate([np.ones_like(w), np.zeros_like(w)]) best = None # small search over the pole-range scaling for a better fit for lo_s in (0.5, 1.0, 1.5, 2.5): for hi_s in (1.5, 3.0, 6.0, 12.0): wk = 2 * np.pi * np.geomspace(f_lo * lo_s, f_hi * hi_s, n_branch) cols = [np.ones_like(w, dtype=complex)] + [1j * w / (1j * w + x) for x in wk] A = np.stack(cols, axis=1) / target[:, None] A_ri = np.concatenate([A.real, A.imag], axis=0) x, _ = nnls(A_ri, b_ri) err = np.max(np.abs(A @ x - 1.0)) if best is None or err < best[0]: best = (err, x.copy(), wk.copy()) err, x, wk = best return x[0], x[1:], wk, err @dataclass class WallMaterial: """Y(w) = G + sum_b 1/(jw M_b + R_b + K_b/(jw)); branches = [(M, R, K), ...].""" G: float = 0.0 branches: list = field(default_factory=list) name: str = "wall" def admittance(self, f): f = np.asarray(f, dtype=float) w = 2 * np.pi * np.maximum(f, 1e-9) y = np.full(f.shape, self.G, dtype=complex) for (M, R, K) in self.branches: y = y + 1.0 / (1j * w * M + R + K / (1j * w)) return y def table(self, dt: float, nb: int) -> np.ndarray: """Row of the kernel material table: [G, (M/dt, D, K) * nb].""" if len(self.branches) > nb: raise ValueError("too many branches") row = np.zeros(1 + 3 * nb, dtype=np.float32) row[0] = self.G for b, (M, R, K) in enumerate(self.branches): row[1 + 3 * b] = M / dt row[2 + 3 * b] = 1.0 / (M / dt + R + K * dt) row[3 + 3 * b] = K return row def boundary_layer_branches(n_branch=4, scale=1.0, f_lo=80.0, f_hi=16000.0, **air): """(G, branches) approximating scale * Y_bl(w) = scale * C sqrt(jw).""" C = boundary_layer_coefficient(**air) * scale g0, a, wk, _ = fit_sqrt_jw(n_branch, f_lo, f_hi) branches = [] for ak, w in zip(a, wk): if ak <= 0: continue R = 1.0 / (ak * C) branches.append((0.0, R, w * R)) return g0 * C, branches def hard_wall(boundary_layer=True, n_bl=4, bl_scale=1.0, G=0.0) -> WallMaterial: """Rigid wall with viscothermal boundary-layer loss only (plus optional conductance G).""" g, br = (boundary_layer_branches(n_bl, bl_scale) if boundary_layer else (0.0, [])) return WallMaterial(G=G + g, branches=br, name="hard") def tissue_wall(M=21.0, R=8000.0, K=845000.0, boundary_layer=True, n_bl=4, bl_scale=1.0, G=0.0) -> WallMaterial: """Yielding soft-tissue wall (mass-damper-spring per unit area) + boundary-layer loss. Defaults: M = 2.1 g/cm^2, R = 800 g/(s cm^2), K = 84500 dyn/cm^3 (Ishizaka et al. 1975, relaxed cheek), i.e. SI 21 kg/m^2, 8000 N s/m^3, 8.45e5 N/m^3.""" g, br = (boundary_layer_branches(n_bl, bl_scale) if boundary_layer else (0.0, [])) return WallMaterial(G=G + g, branches=[(M, R, K)] + br, name="tissue") def conductance_wall(G: float) -> WallMaterial: """Frequency-independent real wall admittance G (m/(Pa s)); G = 1/(rho c) is matched.""" return WallMaterial(G=G, branches=[], name="conductance")