// Float64 reference time loop with exactly the structure of the GPU kernels: an element pass // (forces, sigma1 term, radiation accumulation), a hit pass (contact forces, release), a node // pass (gather, contact, lagged rigid-mode projection, leapfrog update, partial sums for the // next projection), and the exciter update. See THEORY.md. import { elementForces, strainEnergies } from "../model/forces.ts"; import { HOLD_HEIGHT } from "../model/exciter.ts"; import { DECIMATION_SPAN } from "../model/radiation.ts"; import type { Setup } from "../model/setup.ts"; import type { ChunkResult, Diagnostics, SimulatorBackend } from "../model/simulator.ts"; export class CpuSimulator implements SimulatorBackend { readonly kind = "cpu" as const; private n = 0; // sub-step counter private readonly q: Float64Array; private readonly v: Float64Array; private readonly nodeAcc: Float64Array; private readonly vwPrev: Float64Array; // projected transverse velocity of the previous step (for the radiated acceleration) private readonly ef: Float64Array; private readonly fc: Float64Array; private readonly zs: Float64Array; private readonly vs: Float64Array; private readonly Fk: Float64Array; private readonly F: Float64Array; private readonly Fmax: Float64Array; private readonly ncont: Uint32Array; private partialsPrev = new Float64Array(12); private partialsNew = new Float64Array(12); // [0,6): Rm.(M v), [6,12): Rm.(M q) private readonly acc: Float64Array; // (E, DECIMATION_SPAN) decimation accumulators private readonly history: Float64Array; private pending: { j: number; front: number; back: number }[] = []; samplesDone = 0; readonly maxChunk = 8192; readonly setup: Setup; readonly movieEvery: number; /** ``movieEvery`` > 0 captures w every that many audio samples. */ constructor(setup: Setup, movieEvery = 0) { this.setup = setup; this.movieEvery = movieEvery; const N = setup.mesh.n, E = setup.mesh.e, K = setup.schedule.K, P = setup.schedule.P; this.q = new Float64Array(5 * N); this.v = new Float64Array(5 * N); this.nodeAcc = new Float64Array(N); this.vwPrev = new Float64Array(N); this.ef = new Float64Array(15 * E); this.fc = new Float64Array(N); this.zs = new Float64Array(K).fill(HOLD_HEIGHT); this.vs = new Float64Array(K); this.Fk = new Float64Array(K * P); this.F = new Float64Array(K); this.Fmax = new Float64Array(K); this.ncont = new Uint32Array(K); this.acc = new Float64Array(E * DECIMATION_SPAN); this.history = new Float64Array(E * setup.radiation.ringLength); } private step() { const { shell: sh, mesh, schedule: sc, radiation: rad, dt, substeps: m } = this.setup; const { tri } = mesh, N = mesh.n, E = mesh.e, n = this.n; const sigma0 = this.setup.config.material.sigma0; // ---- element pass elementForces(sh, this.q, this.v, this.ef); if (n >= 1) { const s = n - 1, j0 = Math.floor(s / m), kk = s - j0 * m, W = DECIMATION_SPAN, taps = rad.taps; if (kk === 0 && j0 > 0) this.finalizeSample(j0 - W / 2); // slot 0 = sample j0 - (W/2 - 1) of the previous j0 for (let e = 0; e < E; e++) { const a = (this.nodeAcc[tri[3 * e]] + this.nodeAcc[tri[3 * e + 1]] + this.nodeAcc[tri[3 * e + 2]]) / 3; for (let w = 0; w < W; w++) this.acc[e * W + w] += a * taps[w * m + kk]; } } // ---- hit pass this.fc.fill(0); for (let k = 0; k < sc.K; k++) { let wmax = -Infinity; const wp = new Float64Array(sc.P); for (let p = 0; p < sc.P; p++) { const b = (k * sc.P + p) * 3; let w = sc.offsets[k * sc.P + p]; for (let i = 0; i < 3; i++) w += sc.weights[b + i] * this.q[5 * sc.nodes[b + i] + 2]; wp[p] = w; wmax = Math.max(wmax, w); } if (n === sc.releaseStep[k]) { this.zs[k] = wmax + sc.gap[k]; this.vs[k] = -sc.velocity[k]; } const active = n >= sc.releaseStep[k]; let Ftot = 0; for (let p = 0; p < sc.P; p++) { const delta = Math.max(wp[p] - this.zs[k], 0); const f = active ? sc.kPoint[k] * Math.pow(delta, sc.alpha[k]) : 0; this.Fk[k * sc.P + p] = f; Ftot += f; if (f > 0) { const b = (k * sc.P + p) * 3; for (let i = 0; i < 3; i++) this.fc[sc.nodes[b + i]] -= f * sc.weights[b + i]; } } this.F[k] = Ftot; } // ---- node pass const { M, Minv, Rm, adjOffsets, adjEntries } = sh, L = 5 * N, c = this.partialsPrev; this.partialsNew.fill(0); const f = new Float64Array(5); for (let nd = 0; nd < N; nd++) { f.fill(0); for (let a = adjOffsets[nd]; a < adjOffsets[nd + 1]; a++) { const ent = adjEntries[a], e = (ent / 3) | 0, i = ent % 3; for (let d = 0; d < 5; d++) f[d] += this.ef[15 * e + 5 * i + d]; } f[2] += this.fc[nd]; for (let d = 0; d < 5; d++) { const idx = 5 * nd + d; // lagged projection: remove the rigid content that the previous step's update introduced let vd = this.v[idx], qd = this.q[idx]; for (let k = 0; k < 6; k++) { const r = Rm[k * L + idx]; vd -= c[k] * r; qd -= c[6 + k] * r; } const v1 = (vd + dt * f[d] * Minv[idx]) / (1 + 2 * sigma0 * dt); // radiated acceleration from consecutive projected velocities, so that the rigid increment // that the lagged projection removes one step later never radiates if (d === 2) { this.nodeAcc[nd] = (vd - this.vwPrev[nd]) / dt; this.vwPrev[nd] = vd; } const q1 = qd + dt * v1; this.q[idx] = q1; this.v[idx] = v1; for (let k = 0; k < 6; k++) { const r = Rm[k * L + idx] * M[idx]; this.partialsNew[k] += r * v1; this.partialsNew[6 + k] += r * q1; } } } [this.partialsPrev, this.partialsNew] = [this.partialsNew, this.partialsPrev]; // ---- exciters for (let k = 0; k < sc.K; k++) { this.vs[k] += (dt * this.F[k]) / sc.mass[k]; this.zs[k] += dt * this.vs[k]; this.Fmax[k] = Math.max(this.Fmax[k], this.F[k]); if (this.F[k] > 0) this.ncont[k]++; } this.n++; } /** Slot 0 (audio sample j) of the accumulators is complete: store it, shift the slots, evaluate the listener pressure. */ private finalizeSample(j: number) { const { radiation: rad, mesh } = this.setup, E = mesh.e, D = rad.ringLength, W = DECIMATION_SPAN; for (let e = 0; e < E; e++) { if (j >= 0) this.history[e * D + (j % D)] = this.acc[e * W]; for (let w = 0; w < W - 1; w++) this.acc[e * W + w] = this.acc[e * W + w + 1]; this.acc[e * W + W - 1] = 0; } if (j < 0) return; let front = 0, back = 0; for (let e = 0; e < E; e++) { const jf = j - rad.i0F[e], jb = j - rad.i0B[e]; if (jf >= 1) front += rad.coefF[e] * (this.history[e * D + (jf % D)] * (1 - rad.frF[e]) + this.history[e * D + ((jf - 1) % D)] * rad.frF[e]); if (jb >= 1) back += rad.coefB[e] * (this.history[e * D + (jb % D)] * (1 - rad.frB[e]) + this.history[e * D + ((jb - 1) % D)] * rad.frB[e]); } this.pending.push({ j, front, back }); } async runChunk(nSamples: number): Promise { const m = this.setup.substeps, N = this.setup.mesh.n, frames: Float32Array[] = [], frameSamples: number[] = []; for (let s = 0; s < nSamples * m; s++) { if (this.movieEvery > 0 && this.n % (m * this.movieEvery) === 0) { const w = new Float32Array(N); for (let nd = 0; nd < N; nd++) w[nd] = this.q[5 * nd + 2]; frames.push(w); frameSamples.push(this.n / m); } this.step(); } this.samplesDone += nSamples; const out = this.pending; this.pending = []; const firstSample = out.length ? out[0].j : this.samplesDone; return { firstSample, pressureFront: Float32Array.from(out, (o) => o.front), pressureBack: Float32Array.from(out, (o) => o.back), frames, frameSamples }; } async diagnostics(): Promise { const { shell: sh, schedule: sc, dt } = this.setup, N = sh.mesh.n; let ke = 0, woh = 0; for (let i = 0; i < 5 * N; i++) ke += 0.5 * sh.M[i] * this.v[i] * this.v[i]; for (let nd = 0; nd < N; nd++) woh = Math.max(woh, Math.abs(this.q[5 * nd + 2] / sh.hNode[nd])); const { membrane, bending } = strainEnergies(sh, this.q); let ex = 0; for (let k = 0; k < sc.K; k++) ex += 0.5 * sc.mass[k] * this.vs[k] * this.vs[k]; return { time: this.n * dt, kinetic: ke, membrane, bending, exciters: ex, maxWoverH: woh, hits: Array.from({ length: sc.K }, (_, k) => ({ peakForce: this.Fmax[k], contactTime: this.ncont[k] * dt, velocity: this.vs[k], height: this.zs[k] })) }; } /** M-weighted rigid-mode content of the current velocity (the lagged projection leaves one step's increment). */ rigidContent(): number { const { shell: sh } = this.setup, L = 5 * sh.mesh.n; let worst = 0; for (let k = 0; k < 6; k++) { let c = 0; for (let i = 0; i < L; i++) c += sh.Rm[k * L + i] * sh.M[i] * this.v[i]; worst = Math.max(worst, Math.abs(c)); } return worst; } async displacement(): Promise { const N = this.setup.mesh.n, w = new Float32Array(N); for (let nd = 0; nd < N; nd++) w[nd] = this.q[5 * nd + 2]; return w; } dispose() {} }