// 3D FDTD check of a bar: voxelize the bar in air, run two force-impulse simulations // (js/fdtd.js), and turn the recorded top-face velocities into a bar model in the same form // as the FE model: frequencies, radiation decay, and mass-normalized shapes on a grid. import { thickness } from './bar.js'; import { runFDTD, timeStep } from './fdtd.js'; import { fft, nextPow2, hann } from './fft.js'; const MARGIN = 20, SPONGE = 10; // mm of air around the bar, sponge cells export function isWood(b, x, y, z) { if (x < 0 || x > b.L || y < 0 || y > b.W || z > b.h0) return false; return z >= b.h0 - thickness(b, x); } /** Voxel grid (dx in mm). Top-face nodes are recorded on a coarser sampling (~6 mm). */ export function buildGrid(b, dx) { const pad = SPONGE + Math.ceil(MARGIN / dx); const nbx = Math.ceil(b.L / dx), nby = Math.ceil(b.W / dx), nbz = Math.ceil(b.h0 / dx); const nx = nbx + 2 * pad, ny = nby + 2 * pad, nz = nbz + 2 * pad; const o = pad * dx; const N = nx * ny * nz; const mat = new Uint32Array(N); const id = (i, j, k) => i + nx * (j + ny * k); for (let k = 0; k < nz; k++) { const z = (k + 0.5) * dx - o; for (let j = 0; j < ny; j++) { const y = (j + 0.5) * dx - o; for (let i = 0; i < nx; i++) { if (isWood(b, (i + 0.5) * dx - o, y, z)) mat[id(i, j, k)] = 1; } } } // top-face vz nodes (node above the topmost wood cell of each column) const kTop = new Int32Array(nbx * nby).fill(-1); for (let jb = 0; jb < nby; jb++) { for (let ib = 0; ib < nbx; ib++) { for (let k = nz - 2; k > 0; k--) if (mat[id(ib + pad, jb + pad, k)] === 1) { kTop[ib + nbx * jb] = k; break; } } } const st = Math.max(1, Math.round(6 / dx)); const sIdx = [], sPos = []; const colSurf = new Int32Array(nbx * nby).fill(-1); for (let jb = Math.floor(st / 2); jb < nby; jb += st) { for (let ib = Math.floor(st / 2); ib < nbx; ib += st) { const k = kTop[ib + nbx * jb]; if (k < 0) continue; colSurf[ib + nbx * jb] = sIdx.length; sIdx.push(id(ib + pad, jb + pad, k)); sPos.push([(ib + 0.5) * dx, (jb + 0.5) * dx]); } } let nWood = 0; for (let n = 0; n < N; n++) nWood += mat[n]; return { nx, ny, nz, N, dx, ns: SPONGE, pad, o, nbx, nby, mat, kTop, surf: Uint32Array.from(sIdx), sPos, colSurf, micIdx: 0, nWood, id }; } // Gaussian force patch around a top-face point (mm), on all top-face columns. export function strikePatch(grid, sx, sy) { const ib0 = Math.round(sx / grid.dx - 0.5), jb0 = Math.round(sy / grid.dx - 0.5); const idx = [], w = []; for (let dj = -2; dj <= 2; dj++) { for (let di = -2; di <= 2; di++) { const ib = ib0 + di, jb = jb0 + dj; if (ib < 0 || jb < 0 || ib >= grid.nbx || jb >= grid.nby) continue; const k = grid.kTop[ib + grid.nbx * jb]; if (k < 0) continue; idx.push(grid.id(ib + grid.pad, jb + grid.pad, k)); w.push(Math.exp(-(di * di + dj * dj) / 2)); } } const sum = w.reduce((a, c) => a + c, 0); return { idx: Uint32Array.from(idx), w: Float32Array.from(w.map((v) => v / sum)) }; } // Two reference points away from the nodes of most bending and torsion modes. export function refPoints(b) { return [[0.06, 0.2], [0.37, 0.88]].map(([fx, fy]) => [fx * b.L, fy * b.W]); } export const analysisDuration = (f1) => Math.min(0.1, Math.max(0.03, 6 / f1)); // Estimated cost, for the UI. export function analysisCost(b, mat, dx, f1) { const pad = SPONGE + Math.ceil(MARGIN / dx); const cells = (Math.ceil(b.L / dx) + 2 * pad) * (Math.ceil(b.W / dx) + 2 * pad) * (Math.ceil(b.h0 / dx) + 2 * pad); const steps = 2 * Math.ceil(analysisDuration(f1) / timeStep(mat, dx)); return { cells, steps }; } /** * Run the analysis. Returns a bar model { source: 'FDTD', design, nx, ny, modes } or null * if cancelled. onProgress(label, frac, sec), isCancelled(). */ export async function analyzeBar(b, mat, dx, f1, { onProgress, isCancelled, nx = 120, ny = 8 } = {}) { const grid = buildGrid(b, dx); const runs = []; const refs = refPoints(b).map(([x, y]) => { const ib = Math.round(x / dx - 0.5), jb = Math.round(y / dx - 0.5); // nearest recorded sample to the reference point let best = 0, bd = Infinity; grid.sPos.forEach((p, s) => { const d = Math.hypot(p[0] - x, p[1] - y); if (d < bd) { bd = d; best = s; } }); return { x, y, ib, jb, surf: best, patch: strikePatch(grid, x, y) }; }); let seconds = 0; for (let k = 0; k < refs.length; k++) { const res = await runFDTD({ mat, grid, mode: 'impulse', patch: refs[k].patch, duration: analysisDuration(f1), stiffScale: 1, recRate: 48000, onProgress: (f, sec) => onProgress?.(`FDTD run ${k + 1}/${refs.length}`, (k + f) / refs.length, seconds + sec, f), isCancelled, }); if (!res) return null; seconds += res.seconds; runs.push(res); } onProgress?.('Extracting modes…', 1, seconds, 1); await new Promise((r) => setTimeout(r, 20)); const modes = extractModes(runs, refs, { fmin: 0.3 * f1 }); // resample each mode onto the regular top-face grid used by the FE model const st = Math.max(1, Math.round(6 / dx)); const sx = st * dx, x0 = (Math.floor(st / 2) + 0.5) * dx; const cols = Math.ceil((grid.nbx - Math.floor(st / 2)) / st), rows = Math.ceil((grid.nby - Math.floor(st / 2)) / st); const at = (i, j) => grid.colSurf[(Math.floor(st / 2) + i * st) + grid.nbx * (Math.floor(st / 2) + j * st)]; const out = modes.map((m) => { const phi = new Float32Array(nx * ny); for (let j = 0; j < ny; j++) { const v = Math.min(rows - 1.0001, Math.max(0, (((j + 0.5) / ny) * b.W - x0) / sx)); const j0 = Math.floor(v), fv = v - j0, j1 = Math.min(rows - 1, j0 + 1); for (let i = 0; i < nx; i++) { const u = Math.min(cols - 1.0001, Math.max(0, (((i + 0.5) / nx) * b.L - x0) / sx)); const i0 = Math.floor(u), fu = u - i0, i1 = Math.min(cols - 1, i0 + 1); const g = (ii, jj) => { const s = at(ii, jj); return s >= 0 ? m.phi[s] : 0; }; phi[j * nx + i] = g(i0, j0) * (1 - fu) * (1 - fv) + g(i1, j0) * fu * (1 - fv) + g(i0, j1) * (1 - fu) * fv + g(i1, j1) * fu * fv; } } return { f: m.f, sigRad: m.sigRad, phi, label: '' }; }); return { source: 'FDTD', design: { ...b }, nx, ny, modes: out, seconds, grid: { nx: grid.nx, ny: grid.ny, nz: grid.nz } }; } /** * Poles from the combined spectrum, radiation decay from two half windows, then a * least-squares fit of complex amplitudes with those poles. The mobility V(ref → P) of a * mass-normalized mode is -φ(ref)φ(P) (force down, velocity up), which gives φ(P). */ export function extractModes(runs, refs, { maxModes = 60, fmin = 50, fmax = 16000 } = {}) { const { nRec, nSurf, recRate } = runs[0]; const t = Float64Array.from({ length: nRec }, (_, r) => (r + 0.5) / recRate); const win = hann(nRec); const nfft = nextPow2(nRec) * 4; const Ptot = new Float64Array(nfft / 2); const stride = Math.max(1, Math.floor(nSurf / 300)); const re = new Float64Array(nfft), im = new Float64Array(nfft); for (const run of runs) { const P = new Float64Array(nfft / 2); for (let n = 0; n < nSurf; n += stride) { re.fill(0); im.fill(0); for (let r = 0; r < nRec; r++) re[r] = run.data[r * nSurf + n] * win[r]; fft(re, im); for (let k = 0; k < nfft / 2; k++) P[k] += re[k] * re[k] + im[k] * im[k]; } let m = 0; for (let k = 0; k < nfft / 2; k++) m = Math.max(m, P[k]); for (let k = 0; k < nfft / 2; k++) Ptot[k] += P[k] / m; } const df = recRate / nfft; const k0 = Math.max(2, Math.floor(fmin / df)); const k1 = Math.min(nfft / 2 - 2, Math.floor(Math.min(fmax, 0.45 * recRate) / df)); let pmax = 0; for (let k = k0; k <= k1; k++) pmax = Math.max(pmax, Ptot[k]); const span = Math.round(3 * (nfft / nRec)); const peaks = []; for (let k = k0; k <= k1; k++) { if (Ptot[k] < pmax * 1e-7) continue; let ok = true; for (let q = Math.max(0, k - span); q <= Math.min(nfft / 2 - 1, k + span); q++) { if (q !== k && Ptot[q] > Ptot[k]) { ok = false; break; } } if (!ok) continue; const a = Math.log(Ptot[k - 1]), bb = Math.log(Ptot[k]), c = Math.log(Ptot[k + 1]); const d = (0.5 * (a - c)) / (a - 2 * bb + c); peaks.push({ f: (k + (Number.isFinite(d) ? d : 0)) * df, p: Ptot[k] }); } peaks.sort((x, y) => y.p - x.p); const poles = peaks.slice(0, maxModes).sort((x, y) => x.f - y.f).map((pk) => ({ f: pk.f, w: 2 * Math.PI * pk.f })); // radiation decay from two half-window DFTs const half = Math.floor(nRec / 2); const w1 = hann(half); const Dt = half / recRate; const sstride = Math.max(1, Math.floor(nSurf / 150)); for (const pole of poles) { let P1 = 0, P2 = 0; for (const run of runs) { for (let n = 0; n < nSurf; n += sstride) { let ar = 0, ai = 0, br = 0, bi = 0; for (let r = 0; r < 2 * half; r++) { const c = Math.cos(pole.w * t[r]), s = Math.sin(pole.w * t[r]); const v = run.data[r * nSurf + n] * (r < half ? w1[r] : w1[r - half]); if (r < half) { ar += v * c; ai += v * s; } else { br += v * c; bi += v * s; } } P1 += ar * ar + ai * ai; P2 += br * br + bi * bi; } } const sig = P2 > 0 ? (0.5 * Math.log(P1 / P2)) / Dt : 0; pole.sig = Math.min(300, Math.max(0, Number.isFinite(sig) ? sig : 0)); } const r0 = Math.ceil(2 * runs[0].pulseT * recRate); const fits = runs.map((run) => lsFit(run.data, nRec, nSurf, t, r0, poles, recRate)); return poles.map((pole, n) => { let best = null; for (let k = 0; k < runs.length; k++) { const [fr, fi] = halfSineFT(runs[k].pulseA, runs[k].pulseT, pole.w); const f2 = fr * fr + fi * fi; const Vre = new Float64Array(nSurf), Vim = new Float64Array(nSurf); let vmax = 0; for (let s = 0; s < nSurf; s++) { const ar = fits[k].re[n * nSurf + s], ai = fits[k].im[n * nSurf + s]; Vre[s] = (ar * fr + ai * fi) / f2; Vim[s] = (ai * fr - ar * fi) / f2; vmax = Math.max(vmax, Math.hypot(Vre[s], Vim[s])); } const sr = refs[k].surf; const vr = Math.hypot(Vre[sr], Vim[sr]); const ratio = vr / (vmax || 1); if (!best || ratio > best.ratio) best = { ratio, Vre, Vim, sr, vr }; } // φ(P) = Re(V(P) e^{-i arg V(ref)}) / sqrt|V(ref)| const { Vre, Vim, sr, vr } = best; const cr = Vre[sr] / vr, ci = -Vim[sr] / vr, sc = 1 / Math.sqrt(vr || 1e-30); const phi = new Float64Array(nSurf); for (let s = 0; s < nSurf; s++) phi[s] = (Vre[s] * cr - Vim[s] * ci) * sc; return { f: pole.f, sigRad: pole.sig, phi, quality: best.ratio }; }).filter((m) => m.quality > 0.05); } // Fourier transform of a half-sine pulse A sin(πt/T), 0= 0; i--) { let v = col[i]; for (let j = i + 1; j < K; j++) v -= L[j * K + i] * col[j]; col[i] = v / L[i * K + i]; } for (let n = 0; n < M; n++) { out.re[n * nSurf + s] = col[2 * n]; out.im[n * nSurf + s] = col[2 * n + 1]; } } return out; } function cholesky(A, n) { const L = new Float64Array(n * n); for (let i = 0; i < n; i++) { for (let j = 0; j <= i; j++) { let s = A[i * n + j]; for (let k = 0; k < j; k++) s -= L[i * n + k] * L[j * n + k]; L[i * n + j] = i === j ? Math.sqrt(Math.max(s, 1e-300)) : s / L[j * n + j]; } } return L; } /** * Label FDTD modes by the FE mode they resemble most (shape correlation on the grid). * Returns rows [{ fe, fd, corr, cents }] for the FE modes that found a partner. */ export function matchModes(fe, fd) { const norm = (p) => Math.sqrt(p.reduce((a, v) => a + v * v, 0)) || 1; const rows = []; for (const m of fd.modes) { let best = null; for (const q of fe.modes) { let s = 0; for (let i = 0; i < m.phi.length; i++) s += m.phi[i] * q.phi[i]; const c = Math.abs(s) / (norm(m.phi) * norm(q.phi)); if (!best || c > best.c) best = { q, c }; } if (best && best.c > 0.7) { m.label = best.q.label; rows.push({ fe: best.q, fd: m, corr: best.c, cents: 1200 * Math.log2(m.f / best.q.f) }); } else m.label = '?'; } // keep the best match per FE mode; number the rest const seen = new Map(); for (const r of rows) if (!seen.has(r.fe.label) || seen.get(r.fe.label).corr < r.corr) seen.set(r.fe.label, r); for (const m of fd.modes) if (m.label !== '?' && seen.get(m.label)?.fd !== m) m.label += "'"; return [...seen.values()].sort((a, b) => a.fe.f - b.fe.f); }