"""3D time-domain acoustic solver (staggered p/u leapfrog, CuPy RawKernel). See `kernels.py` for the update equations. All quantities SI: pressure Pa, velocity m/s, volume velocity m^3/s, lengths m. """ from __future__ import annotations import time from dataclasses import dataclass, field import numpy as np from .kernels import SRC from .wall import C_AIR, RHO_AIR, WallMaterial _mod = None def _kernels(): global _mod import cupy as cp if _mod is None: _mod = cp.RawModule(code=SRC, options=("-std=c++11",)) return {k: _mod.get_function(k) for k in ("step_u", "step_p", "step_wall", "inject", "probe", "kh_accum")} @dataclass class Scene: """Voxel scene. Arrays are (nz, ny, nx); cell (k, j, i) is centred at origin + ((i, j, k) + 0.5) * dx (origin = (x, y, z) of the corner of cell (0, 0, 0)). air bool, True where the cell is air (staircase description), or vol/ax/ay/az float32 fill fraction and +x/+y/+z face apertures in [0, 1] (conformal). wall_mat uint8 per cell: index (1-based) into `materials` of the wall touched by this air cell; 0 = rigid. pml cells of PML on (x-, x+, y-, y+, z-, z+); 0 = rigid boundary (baffle / symmetry). src (idx[n], w[n]) linear cell indices and weights (sum 1) of the volume-velocity source. probes list of (idx[m], w[m]) weighted pressure probes. kh_box (i0, i1, j0, j1, k0, k1) cell-index box whose surface is the Kirchhoff-Helmholtz surface (must enclose all solids/sources); kh_obs = observer points (x, y, z) in m. sym_y if not None, the y coordinate of a mirror plane (rigid domain face) used to mirror the KH surface. """ dx: float air: np.ndarray origin: tuple = (0.0, 0.0, 0.0) wall_mat: np.ndarray | None = None materials: list = field(default_factory=list) pml: tuple = (12, 12, 12, 12, 12, 12) src: tuple | None = None probes: list = field(default_factory=list) kh_box: tuple | None = None kh_obs: list = field(default_factory=list) sym_y: float | None = None kh_mirrors: list = field(default_factory=list) # [(axis 0|1|2, coordinate)] mirror planes vol: np.ndarray | None = None ax: np.ndarray | None = None ay: np.ndarray | None = None az: np.ndarray | None = None c: float = C_AIR rho: float = RHO_AIR area_mode: str = "smooth" # wall-area estimate for staircase walls: smooth|vector|faces meta: dict = field(default_factory=dict) @property def shape(self): return self.air.shape def lin(self, i, j, k): nz, ny, nx = self.shape return (np.asarray(k, dtype=np.int64) * ny + np.asarray(j)) * nx + np.asarray(i) def pos(self, i, j, k): o = self.origin return (o[0] + (np.asarray(i) + 0.5) * self.dx, o[1] + (np.asarray(j) + 0.5) * self.dx, o[2] + (np.asarray(k) + 0.5) * self.dx) def pick_dt(dx, c=C_AIR, fs_out=88200.0, cfl=0.99, lam2_max=1.0 / 3.0): """Largest dt = 1/(r fs_out), r integer, with (c dt/dx)^2 <= cfl^2 * lam2_max.""" dt_max = cfl * dx * np.sqrt(lam2_max) / c r = int(np.ceil(1.0 / (dt_max * fs_out))) return 1.0 / (r * fs_out), r class Solver: def __init__(self, scene: Scene, dt: float | None = None, nb: int | None = None, pml_R0: float = 1e-6, pml_order: float = 3.0, verbose: bool = False): import cupy as cp self.cp = cp self.sc = sc = scene self.k = _kernels() nz, ny, nx = sc.shape self.nx, self.ny, self.nz = nx, ny, nz self.N = nx * ny * nz dx = sc.dx if dt is None: dt, _ = pick_dt(dx, sc.c) self.dt = dt self.lam2 = (sc.c * dt / dx) ** 2 self.conformal = sc.vol is not None self.c1 = np.float32(dt / (sc.rho * dx)) self.c2 = np.float32(sc.rho * sc.c ** 2 * dt / dx) # ---- geometry: apertures, volumes ------------------------------------------------- air = cp.asarray(sc.air, dtype=cp.bool_) air[0], air[-1] = False, False air[:, 0], air[:, -1] = False, False air[:, :, 0], air[:, :, -1] = False, False if self.conformal: vol = cp.asarray(sc.vol, dtype=cp.float32) * air ax = cp.asarray(sc.ax, dtype=cp.float32) ay = cp.asarray(sc.ay, dtype=cp.float32) az = cp.asarray(sc.az, dtype=cp.float32) else: vol = air.astype(cp.float32) ax = cp.ones((nz, ny, nx), dtype=cp.float32) ay = cp.ones((nz, ny, nx), dtype=cp.float32) az = cp.ones((nz, ny, nx), dtype=cp.float32) # a face is open only between two air cells ax[:, :, :-1] *= air[:, :, :-1] & air[:, :, 1:] ay[:, :-1, :] *= air[:, :-1, :] & air[:, 1:, :] az[:-1, :, :] *= air[:-1, :, :] & air[1:, :, :] ax[:, :, -1] = 0 ay[:, -1, :] = 0 az[-1, :, :] = 0 # quantise apertures to 8 bit (as used by the kernel) qx = cp.rint(ax * 255).astype(cp.uint32) qy = cp.rint(ay * 255).astype(cp.uint32) qz = cp.rint(az * 255).astype(cp.uint32) ax, ay, az = (q.astype(cp.float32) / 255 for q in (qx, qy, qz)) # sum of apertures of the six faces of each cell ssum = ax + ay + az ssum[:, :, 1:] += ax[:, :, :-1] ssum[:, 1:, :] += ay[:, :-1, :] ssum[1:, :, :] += az[:-1, :, :] if self.conformal: # leapfrog stability of the cut-cell scheme: lam2 * sum_f s_f / v <= 2 vmin = (self.lam2 * ssum / 2.0) * 1.02 infl = cp.maximum(vmin - vol, 0) * air self.vol_inflation = float(infl.sum()) / max(float((vol * air).sum()), 1e-30) vol = cp.maximum(vol, vmin) * air invV = cp.where(air, 1.0 / cp.maximum(vol, 1e-6), 0).astype(cp.float32) else: self.vol_inflation = 0.0 invV = cp.zeros(1, dtype=cp.float32) self.geo = (qx | (qy << 8) | (qz << 16) | (air.astype(cp.uint32) << 24)).ravel() self.invV = invV.ravel() self.n_air = int(air.sum()) self._vol = vol if self.conformal else None self._air = air # ---- wall cells ------------------------------------------------------------------- self.nb = 0 self.nw = 0 if sc.wall_mat is not None and len(sc.materials) > 0: wm = cp.asarray(sc.wall_mat, dtype=cp.uint8) # area vector of the wall inside each cell (divergence theorem), in units of dx^2. # Faces on the solid domain rim are symmetry planes (mirror), not walls: they get # the aperture of the parallel face one cell in (conformal) or 1 (staircase). wx, wy, wz = ax.copy(), ay.copy(), az.copy() for wa, a_ in ((wx, 2), (wy, 1), (wz, 0)): for f_, g_, c_ in ((0, 1, 1), (-2, -3, -2)): sf = [slice(None)] * 3 sg = [slice(None)] * 3 sc_ = [slice(None)] * 3 sf[a_], sg[a_], sc_[a_] = f_, g_, c_ src_ap = wa[tuple(sg)] if self.conformal else 1.0 wa[tuple(sf)] = src_ap * air[tuple(sc_)] vx, vy, vz = wx.copy(), wy.copy(), wz.copy() vx[:, :, 1:] -= wx[:, :, :-1] vy[:, 1:, :] -= wy[:, :-1, :] vz[1:, :, :] -= wz[:-1, :, :] vnorm = cp.sqrt(vx * vx + vy * vy + vz * vz) if self.conformal: fac = vnorm else: wsum = wx + wy + wz wsum[:, :, 1:] += wx[:, :, :-1] wsum[:, 1:, :] += wy[:, :-1, :] wsum[1:, :, :] += wz[:-1, :, :] nclosed = 6.0 - wsum del wsum if sc.area_mode == "faces": fac = nclosed else: if sc.area_mode == "smooth": import cupyx.scipy.ndimage as ndi am = air.astype(cp.float32) # rim layers mirrored am[0], am[-1] = am[1], am[-2] am[:, 0], am[:, -1] = am[:, 1], am[:, -2] am[:, :, 0], am[:, :, -1] = am[:, :, 1], am[:, :, -2] sm = ndi.gaussian_filter(am, 1.5, mode="nearest") del am gz_, gy_, gx_ = cp.gradient(sm) gn = cp.sqrt(gx_ ** 2 + gy_ ** 2 + gz_ ** 2) l1 = (cp.abs(gx_) + cp.abs(gy_) + cp.abs(gz_)) / cp.maximum(gn, 1e-12) l1 = cp.where(gn > 1e-6, l1, 1.0) del sm, gx_, gy_, gz_, gn else: l1 = (cp.abs(vx) + cp.abs(vy) + cp.abs(vz)) / cp.maximum(vnorm, 1e-12) l1 = cp.where(vnorm > 1e-6, l1, 1.0) fac = nclosed / cp.clip(l1, 1.0, 1.7320508) sel = air & (wm > 0) & (fac > 1e-3) widx = cp.nonzero(sel.ravel())[0].astype(cp.int32) self.nw = int(widx.size) if self.nw > 0: facw = fac.ravel()[widx] self.wall_area = float(facw.sum()) * dx * dx beta = (sc.rho * sc.c ** 2 * dt / dx) * facw if self.conformal: beta = beta * self.invV[widx] self.widx = widx self.wbeta = beta.astype(cp.float32) self.wmat = (wm.ravel()[widx] - 1).astype(cp.uint8) self.nb = nb if nb is not None else max(len(m.branches) for m in sc.materials) self.nb = max(self.nb, 1) tab = np.stack([m.table(dt, self.nb) for m in sc.materials]) self.mat = cp.asarray(tab.ravel()) self.wv = cp.zeros(self.nb * self.nw, dtype=cp.float32) self.wxi = cp.zeros(self.nb * self.nw, dtype=cp.float32) del vx, vy, vz, wx, wy, wz, vnorm, fac self._ax, self._ay, self._az = ax, ay, az # ---- PML coefficients ------------------------------------------------------------- npml = sc.pml self.x0, self.x1 = 1 + npml[0], nx - 1 - npml[1] self.y0, self.y1 = 1 + npml[2], ny - 1 - npml[3] self.z0, self.z1 = 1 + npml[4], nz - 1 - npml[5] def prof(n, lo, hi, n0, n1): i = np.arange(n, dtype=np.float64) sig_p = np.zeros(n) sig_u = np.zeros(n) for (L, side) in ((lo, 0), (hi, 1)): if L <= 0: continue smax = -(pml_order + 1) * sc.c * np.log(pml_R0) / (2 * L * dx) if side == 0: dp = (n0 - (i + 0.5)) / L du = (n0 - (i + 1.0)) / L else: dp = ((i + 0.5) - n1) / L du = ((i + 1.0) - n1) / L sig_p += smax * np.clip(dp, 0, None) ** pml_order sig_u += smax * np.clip(du, 0, None) ** pml_order out = [] for s in (sig_p, sig_u): b = 1.0 / (1.0 + 0.5 * s * dt) a = (1.0 - 0.5 * s * dt) * b out += [a, b] return np.concatenate(out) coef = np.concatenate([prof(nx, npml[0], npml[1], self.x0, self.x1), prof(ny, npml[2], npml[3], self.y0, self.y1), prof(nz, npml[4], npml[5], self.z0, self.z1)]) self.coef = cp.asarray(coef, dtype=cp.float32) # ---- fields ----------------------------------------------------------------------- self.p = cp.zeros(self.N, dtype=cp.float32) self.ux = cp.zeros(self.N, dtype=cp.float32) self.uy = cp.zeros(self.N, dtype=cp.float32) self.uz = cp.zeros(self.N, dtype=cp.float32) has_pml = any(n > 0 for n in npml) self.px = cp.zeros(self.N if has_pml else 1, dtype=cp.float32) self.py = cp.zeros(self.N if has_pml else 1, dtype=cp.float32) if not has_pml: # interior covers everything self.x0 = self.y0 = self.z0 = 0 self.x1, self.y1, self.z1 = nx, ny, nz # ---- source ----------------------------------------------------------------------- self.ns = 0 if sc.src is not None: sidx = cp.asarray(sc.src[0], dtype=cp.int32) w = cp.asarray(sc.src[1], dtype=cp.float32) sw = w * np.float32(sc.rho * sc.c ** 2 * dt / dx ** 3) if self.conformal: sw = sw * self.invV[sidx] self.sidx, self.sw, self.ns = sidx, sw.astype(cp.float32), int(sidx.size) # ---- probes ----------------------------------------------------------------------- self.np_ = len(sc.probes) if self.np_: self.pidx = cp.asarray(np.concatenate([np.asarray(a).ravel() for a, _ in sc.probes]), dtype=cp.int32) self.pw = cp.asarray(np.concatenate([np.asarray(w).ravel() for _, w in sc.probes]), dtype=cp.float32) self.pid = cp.asarray(np.concatenate( [np.full(np.asarray(a).size, m) for m, (a, _) in enumerate(sc.probes)]), dtype=cp.int32) # ---- Kirchhoff-Helmholtz surface -------------------------------------------------- self.nkh = 0 self.nobs = len(sc.kh_obs) if sc.kh_box is not None and self.nobs: self._build_kh() self.nstep = 0 if verbose: print(f"[fdtd3d] grid {nx}x{ny}x{nz} = {self.N/1e6:.2f} M cells, air " f"{self.n_air/1e6:.2f} M, wall cells {self.nw}, dt {dt*1e6:.3f} us, " f"lam2 {self.lam2:.4f}, conformal {self.conformal}") # ------------------------------------------------------------------------------------ def _build_kh(self): cp = self.cp sc = self.sc nx, ny = self.nx, self.ny i0, i1, j0, j1, k0, k1 = sc.kh_box dx = sc.dx ox, oy, oz = sc.origin air = cp.asnumpy(self._air) faces = [] # (cin, cout, axis, x, y, z, nvec) ii, jj, kk = np.arange(i0, i1), np.arange(j0, j1), np.arange(k0, k1) def add(I, J, K, dI, dJ, dK, axis): I, J, K = (a.ravel() for a in np.meshgrid(I, J, K, indexing="ij")) cin = sc.lin(I, J, K) cout = sc.lin(I + dI, J + dJ, K + dK) ok = air[K, J, I] & air[K + dK, J + dJ, I + dI] x = ox + (I + 0.5 + 0.5 * dI) * dx y = oy + (J + 0.5 + 0.5 * dJ) * dx z = oz + (K + 0.5 + 0.5 * dK) * dx n = np.array([dI, dJ, dK], dtype=np.float64) faces.append((cin[ok], cout[ok], np.full(ok.sum(), axis, dtype=np.int8), x[ok], y[ok], z[ok], np.tile(n, (int(ok.sum()), 1)))) return int((~ok).sum()) # mirror planes (rigid symmetry planes / infinite baffles): the box side lying on the # plane is not part of the surface; image surfaces are added instead mirrors = list(sc.kh_mirrors) if sc.sym_y is not None: mirrors.append((1, sc.sym_y)) box_lo = (ox + i0 * dx, oy + j0 * dx, oz + k0 * dx) box_hi = (ox + i1 * dx, oy + j1 * dx, oz + k1 * dx) skip = set() for (a_, co) in mirrors: skip.add((a_, 0 if co <= 0.5 * (box_lo[a_] + box_hi[a_]) else 1)) nskip = 0 if (0, 1) not in skip: nskip += add(np.array([i1 - 1]), jj, kk, 1, 0, 0, 1) if (0, 0) not in skip: nskip += add(np.array([i0]), jj, kk, -1, 0, 0, -1) if (1, 1) not in skip: nskip += add(ii, np.array([j1 - 1]), kk, 0, 1, 0, 2) if (1, 0) not in skip: nskip += add(ii, np.array([j0]), kk, 0, -1, 0, -2) if (2, 1) not in skip: nskip += add(ii, jj, np.array([k1 - 1]), 0, 0, 1, 3) if (2, 0) not in skip: nskip += add(ii, jj, np.array([k0]), 0, 0, -1, -3) self.kh_skipped_faces = nskip cin = np.concatenate([f[0] for f in faces]) cout = np.concatenate([f[1] for f in faces]) axis = np.concatenate([f[2] for f in faces]) pos = np.stack([np.concatenate([f[3] for f in faces]), np.concatenate([f[4] for f in faces]), np.concatenate([f[5] for f in faces])], axis=1) nrm = np.concatenate([f[6] for f in faces]) sets = [(pos, nrm)] for (a_, co) in mirrors: new_sets = [] for (ps, ns_) in sets: pm = ps.copy() pm[:, a_] = 2 * co - pm[:, a_] nm = ns_.copy() nm[:, a_] *= -1 new_sets.append((pm, nm)) sets = sets + new_sets L = {k: [] for k in ("cin", "cout", "axis", "dly", "wA", "wB", "wC", "obs")} dS = dx * dx maxd = 0.0 for o, xo in enumerate(sc.kh_obs): xo = np.asarray(xo, dtype=np.float64) for (ps, ns_) in sets: rv = xo[None, :] - ps R = np.linalg.norm(rv, axis=1) cos = np.sum(rv * ns_, axis=1) / R L["cin"].append(cin); L["cout"].append(cout); L["axis"].append(axis) d = R / (sc.c * self.dt) maxd = max(maxd, float(d.max())) L["dly"].append(d) L["wA"].append(dS * cos / (4 * np.pi * R * sc.c)) L["wB"].append(sc.rho * dS / (4 * np.pi * R)) L["wC"].append(dS * cos / (4 * np.pi * R * R)) L["obs"].append(np.full(R.size, o)) self.kh = dict( cin=cp.asarray(np.concatenate(L["cin"]), dtype=cp.int32), cout=cp.asarray(np.concatenate(L["cout"]), dtype=cp.int32), axis=cp.asarray(np.concatenate(L["axis"]), dtype=cp.int8), dly=cp.asarray(np.concatenate(L["dly"]), dtype=cp.float32), wA=cp.asarray(np.concatenate(L["wA"]), dtype=cp.float32), wB=cp.asarray(np.concatenate(L["wB"]), dtype=cp.float32), wC=cp.asarray(np.concatenate(L["wC"]), dtype=cp.float32), obs=cp.asarray(np.concatenate(L["obs"]), dtype=cp.int32)) self.nkh = int(self.kh["cin"].size) self.kh_maxdelay = int(np.ceil(maxd)) + 4 # ------------------------------------------------------------------------------------ def energy(self) -> float: """Total acoustic energy in the air (J), excluding energy stored in wall branches.""" cp = self.cp sc = self.sc shp = (self.nz, self.ny, self.nx) p = self.p.reshape(shp) v = self._vol if self.conformal else self._air ep = float(cp.sum(v * p * p, dtype=cp.float64)) / (2 * sc.rho * sc.c ** 2) ek = 0.0 for u, a in ((self.ux, self._ax), (self.uy, self._ay), (self.uz, self._az)): u = u.reshape(shp) ek += float(cp.sum(a * u * u, dtype=cp.float64)) return (ep + 0.5 * sc.rho * ek) * sc.dx ** 3 def run(self, q, energy_every: int = 0, sync_every: int = 2000, kh_every: int = 1): """Advance len(q) steps. q[n] (m^3/s) is the source volume velocity at t=(n+1/2) dt. Returns dict: probes (nsteps, n_probes) at t=(n+1) dt; kh_D, kh_C (n_obs, len) with sample m at t = m dt, observer pressure = d(kh_D)/dt + kh_C; energy (list of (t, E)); wall time and throughput.""" cp = self.cp k = self.k q = np.asarray(q, dtype=np.float32) nsteps = q.size nx, ny, nz = (np.int32(v) for v in (self.nx, self.ny, self.nz)) B = 256 grid = ((self.N + B - 1) // B,) blk = (B,) rec = cp.zeros(max(nsteps * self.np_, 1), dtype=cp.float32) if self.nkh: klen = nsteps + self.kh_maxdelay accD = cp.zeros(self.nobs * klen, dtype=cp.float64) accC = cp.zeros(self.nobs * klen, dtype=cp.float64) kh = self.kh args_u = (self.p, self.ux, self.uy, self.uz, self.geo, self.coef, self.c1, nx, ny, nz) args_p = (self.p, self.px, self.py, self.ux, self.uy, self.uz, self.geo, self.invV, self.coef, self.c2, nx, ny, nz, np.int32(self.x0), np.int32(self.x1), np.int32(self.y0), np.int32(self.y1), np.int32(self.z0), np.int32(self.z1), np.int32(1 if self.conformal else 0)) if self.nw: gw = ((self.nw + B - 1) // B,) args_w = (self.p, self.widx, self.wbeta, self.wmat, self.wv, self.wxi, self.mat, np.float32(self.dt), np.int32(self.nw), np.int32(self.nb)) if self.ns: gs = ((self.ns + B - 1) // B,) if self.np_: mtot = int(self.pidx.size) gp = ((mtot + B - 1) // B,) if self.nkh: gk = ((self.nkh + B - 1) // B,) energies = [] cp.cuda.Stream.null.synchronize() t0 = time.perf_counter() for n in range(nsteps): k["step_u"](grid, blk, args_u) k["step_p"](grid, blk, args_p) if self.ns and q[n] != 0.0: k["inject"](gs, blk, (self.p, self.sidx, self.sw, q[n], np.int32(self.ns))) if self.nw: k["step_wall"](gw, blk, args_w) if self.np_: k["probe"](gp, blk, (self.p, self.pidx, self.pw, self.pid, rec, np.int32(n), np.int32(self.np_), np.int32(mtot))) if self.nkh: k["kh_accum"](gk, blk, (self.p, self.ux, self.uy, self.uz, kh["cin"], kh["cout"], kh["axis"], kh["dly"], kh["wA"], kh["wB"], kh["wC"], kh["obs"], accD, accC, np.int32(n), np.int32(klen), np.int32(self.nkh))) if energy_every and (n + 1) % energy_every == 0: energies.append(((self.nstep + n + 1) * self.dt, self.energy())) if sync_every and (n + 1) % sync_every == 0: cp.cuda.Stream.null.synchronize() cp.cuda.Stream.null.synchronize() wall = time.perf_counter() - t0 self.nstep += nsteps out = dict(dt=self.dt, nsteps=nsteps, wall_s=wall, cell_updates_per_s=self.N * nsteps / wall, air_cell_updates_per_s=self.n_air * nsteps / wall, energy=np.array(energies)) if self.np_: out["probes"] = cp.asnumpy(rec).reshape(nsteps, self.np_).astype(np.float64) if self.nkh: out["kh_D"] = cp.asnumpy(accD).reshape(self.nobs, klen).astype(np.float64) out["kh_C"] = cp.asnumpy(accC).reshape(self.nobs, klen).astype(np.float64) return out