46256e7Marimba lab: per-key tuned bars, coupled resonator tubes, SoundFont exportJeremy Magland 1// Fast finite-element model of a free bar with a varying thickness profile:
2// - vertical bending: Timoshenko beam (2-node elements, w and rotation ψ, reduced shear
3// integration), shear modulus G_LR, rotary inertia included;
4// - torsion: Saint-Venant, with the torsion constant of an orthotropic rectangle.
5// Eigenvalues come from Sturm-sequence bisection on banded LDLᵀ factors and eigenvectors
6// from inverse iteration, so asking for a few modes costs well under a millisecond.
8import { thickness, woodStiffness, AIR } from './bar.js';
10const KAPPA = 5 / 6;
11export const NE = 160; // elements along the length
13// ---------- banded symmetric matrices (upper band, row-major, width b+1) ----------
15function band(n, b) { return { n, b, A: new Float64Array(n * (b + 1)) }; }
16function addSym(m, i, j, v) { // add v to (i,j) and (j,i)
17 if (j < i) [i, j] = [j, i];
18 m.A[i * (m.b + 1) + (j - i)] += v;
19}
21// LDLᵀ of K - lam M. Fills L (band by column) and d; returns the number of negative pivots.
22function ldl(K, M, lam, L, d) {
23 const { n, b } = K, w = b + 1;
24 let neg = 0;
25 for (let j = 0; j < n; j++) {
26 let s = K.A[j * w] - lam * M.A[j * w];
27 for (let k = Math.max(0, j - b); k < j; k++) { const l = L[k * w + (j - k)]; s -= l * l * d[k]; }
28 if (s === 0) s = 1e-300;
29 d[j] = s;
30 if (s < 0) neg++;
31 const iMax = Math.min(n - 1, j + b);
32 for (let i = j + 1; i <= iMax; i++) {
33 let t = K.A[j * w + (i - j)] - lam * M.A[j * w + (i - j)];
34 for (let k = Math.max(0, i - b); k < j; k++) t -= L[k * w + (i - k)] * L[k * w + (j - k)] * d[k];
35 L[j * w + (i - j)] = t / s;
36 }
37 }
38 return neg;
39}
41function ldlSolve(L, d, n, b, r) {
42 const w = b + 1, x = Float64Array.from(r);
43 for (let i = 0; i < n; i++) for (let k = Math.max(0, i - b); k < i; k++) x[i] -= L[k * w + (i - k)] * x[k];
44 for (let i = 0; i < n; i++) x[i] /= d[i];
45 for (let i = n - 1; i >= 0; i--) for (let k = i + 1; k <= Math.min(n - 1, i + b); k++) x[i] -= L[i * w + (k - i)] * x[k];
46 return x;
47}
49function bandMul(m, x) {
50 const { n, b } = m, w = b + 1, y = new Float64Array(n);
51 for (let i = 0; i < n; i++) {
52 y[i] += m.A[i * w] * x[i];
53 for (let k = 1; k <= b && i + k < n; k++) {
54 const v = m.A[i * w + k];
55 y[i] += v * x[i + k];
56 y[i + k] += v * x[i];
57 }
58 }
59 return y;
60}
62class Eig {
63 constructor(K, M, nRigid) {
64 this.K = K; this.M = M; this.nRigid = nRigid;
65 this.L = new Float64Array(K.n * (K.b + 1));
66 this.d = new Float64Array(K.n);
67 }
68 count(lam) { return ldl(this.K, this.M, lam, this.L, this.d); }
69 // m-th flexible eigenvalue (m = 1, 2, ...), by bisection
70 value(m, guess) {
71 const want = m + this.nRigid;
72 let lo = 1, hi = guess > 0 ? guess * 1.5 : 1e6;
73 while (this.count(hi) < want) { lo = hi; hi *= 4; }
74 let cLo = this.count(lo);
75 while (cLo >= want) { hi = lo; lo /= 4; cLo = this.count(lo); }
76 for (let it = 0; it < 80 && hi / lo - 1 > 1e-11; it++) {
77 const mid = Math.sqrt(lo * hi);
78 if (this.count(mid) >= want) hi = mid; else lo = mid;
79 }
80 return Math.sqrt(lo * hi);
81 }
82 // mass-normalized eigenvector by inverse iteration
83 vector(lam) {
84 const { K, M } = this, n = K.n;
85 let x = Float64Array.from({ length: n }, (_, i) => Math.sin(1.3 + 0.37 * i) + 0.5 * Math.cos(0.11 * i));
86 this.count(lam * (1 - 1e-9));
87 for (let it = 0; it < 4; it++) {
88 x = ldlSolve(this.L, this.d, n, K.b, bandMul(M, x));
89 const mx = bandMul(M, x);
90 let s = 0;
91 for (let i = 0; i < n; i++) s += x[i] * mx[i];
92 s = 1 / Math.sqrt(s);
93 for (let i = 0; i < n; i++) x[i] *= s;
94 }
95 return x;
96 }
97}
99// ---------- assembly ----------
101function sections(b, ne) {
102 const le = (b.L * 1e-3) / ne, W = b.W * 1e-3;
103 const out = [];
104 for (let e = 0; e < ne; e++) {
105 const h = thickness(b, ((e + 0.5) * b.L) / ne) * 1e-3;
106 out.push({ le, A: W * h, I: (W * h ** 3) / 12, h, W });
107 }
108 return out;
109}
111function bendingSystem(b, mat, ne) {
112 const C = woodStiffness(mat.EL * 1e9);
113 const n = 2 * (ne + 1);
114 const K = band(n, 3), M = band(n, 3);
115 for (const [e, s] of sections(b, ne).entries()) {
116 const o = 2 * e, { le, A, I } = s;
117 // bending on ψ
118 const kb = (C.E * I) / le;
119 addSym(K, o + 1, o + 1, kb); addSym(K, o + 3, o + 3, kb); addSym(K, o + 1, o + 3, -kb);
120 // shear, one-point integration: γ = (w2-w1)/le - (ψ1+ψ2)/2
121 const B = [-1 / le, -0.5, 1 / le, -0.5], ks = KAPPA * C.gxz * A * le;
122 for (let i = 0; i < 4; i++) for (let j = i; j < 4; j++) addSym(K, o + i, o + j, ks * B[i] * B[j]);
123 // consistent mass: translation and rotary inertia
124 const mt = (mat.rho * A * le) / 6, mr = (mat.rho * I * le) / 6;
125 addSym(M, o, o, 2 * mt); addSym(M, o + 2, o + 2, 2 * mt); addSym(M, o, o + 2, mt);
126 addSym(M, o + 1, o + 1, 2 * mr); addSym(M, o + 3, o + 3, 2 * mr); addSym(M, o + 1, o + 3, mr);
127 }
128 return new Eig(K, M, 2);
129}
131// Torsion constant of a b × t rectangle (isotropic), b ≥ t not required.
132function rectJ(b, t) {
133 if (b < t) [b, t] = [t, b];
134 const r = t / b;
135 return b * t ** 3 * (1 / 3 - 0.21 * r * (1 - r ** 4 / 12));
136}
138function torsionSystem(b, mat, ne) {
139 const C = woodStiffness(mat.EL * 1e9);
140 const n = ne + 1;
141 const K = band(n, 1), M = band(n, 1);
142 // orthotropic: stretch the width by alpha = sqrt(G_LT / G_LR), isotropic with G_LT
143 const alpha = Math.sqrt(C.gxy / C.gxz);
144 for (const [e, s] of sections(b, ne).entries()) {
145 const { le, h, W } = s;
146 const GJ = alpha * C.gxy * rectJ(W / alpha, h);
147 const Ip = (W * h * (W * W + h * h)) / 12;
148 const k = GJ / le, m = (mat.rho * Ip * le) / 6;
149 addSym(K, e, e, k); addSym(K, e + 1, e + 1, k); addSym(K, e, e + 1, -k);
150 addSym(M, e, e, 2 * m); addSym(M, e + 1, e + 1, 2 * m); addSym(M, e, e + 1, m);
151 }
152 return new Eig(K, M, 1);
153}
155// First `count` vertical bending frequencies (Hz). Used by the tuner.
156export function bendFreqs(b, mat, count = 3, ne = NE) {
157 const eig = bendingSystem(b, mat, ne);
158 const out = [];
159 let guess = 0;
160 for (let m = 1; m <= count; m++) {
161 const lam = eig.value(m, guess);
162 out.push(Math.sqrt(lam) / (2 * Math.PI));
163 guess = lam * 4;
164 }
165 return out;
166}
168/**
169 * All bending and torsion modes below fmax (Hz).
170 * Returns [{ kind: 'B'|'T', idx, f, shape: Float64Array (nodal w (m/√kg) or twist (rad/√(kg·m²))) }]
171 */
172export function feModes(b, mat, { fmax = 24000, ne = NE } = {}) {
173 const modes = [];
174 for (const [kind, sys, stride] of [['B', bendingSystem(b, mat, ne), 2], ['T', torsionSystem(b, mat, ne), 1]]) {
175 const lamMax = (2 * Math.PI * fmax) ** 2;
176 const nFlex = sys.count(lamMax) - sys.nRigid;
177 let guess = 0;
178 for (let m = 1; m <= nFlex; m++) {
179 const lam = sys.value(m, guess);
180 guess = lam * 1.2;
181 const v = sys.vector(lam);
182 const shape = new Float64Array(ne + 1);
183 for (let i = 0; i <= ne; i++) shape[i] = v[i * stride];
184 // sign: positive at the left end
185 if (shape[0] < 0) for (let i = 0; i <= ne; i++) shape[i] = -shape[i];
186 modes.push({ kind, idx: m, f: Math.sqrt(lam) / (2 * Math.PI), shape });
187 }
188 }
189 modes.sort((p, q) => p.f - q.f);
190 return modes;
191}
193// ---------- top-surface grid model, shared with the FDTD analysis ----------
195/**
196 * Sample FE modes on a grid of cells over the top face. Vertical displacement per unit
197 * modal coordinate: bending w(x); torsion θ(x)·(y - W/2).
198 * Returns a bar model: { source, design, nx, ny, modes: [{ f, label, sigRad, phi }] }.
199 */
200export function feBarModel(b, mat, opts = {}) {
201 const ne = opts.ne ?? NE;
202 const fe = feModes(b, mat, { fmax: opts.fmax ?? 24000, ne });
203 const nx = opts.nx ?? 120, ny = opts.ny ?? 8;
204 const modes = fe.map((m) => {
205 const phi = new Float32Array(nx * ny);
206 for (let i = 0; i < nx; i++) {
207 const u = ((i + 0.5) / nx) * ne, i0 = Math.min(ne - 1, Math.floor(u)), fu = u - i0;
208 const s = m.shape[i0] * (1 - fu) + m.shape[i0 + 1] * fu;
209 for (let j = 0; j < ny; j++) {
210 const y = ((j + 0.5) / ny - 0.5) * b.W * 1e-3;
211 phi[j * nx + i] = m.kind === 'B' ? s : s * y;
212 }
213 }
214 return { f: m.f, label: `${m.kind}${m.idx}`, kind: m.kind, phi, sigRad: 0 };
215 });
216 const model = { source: 'FE', design: { ...b }, nx, ny, modes };
217 for (const m of model.modes) m.sigRad = dipoleRadDecay(model, m);
218 // the f1 nodal lines (where the support cords go)
219 const s = fe.find((m) => m.kind === 'B')?.shape;
220 model.nodes = [];
221 if (s) for (let i = 0; i < ne; i++) if (s[i] * s[i + 1] < 0) model.nodes.push(((i + s[i] / (s[i] - s[i + 1])) / ne) * b.L);
222 return model;
223}
225// Cell geometry of a grid model (m).
226export function cellGeom(model) {
227 const b = model.design;
228 const dx = (b.L * 1e-3) / model.nx, dy = (b.W * 1e-3) / model.ny;
229 return { dx, dy, dA: dx * dy, x: (i) => (i + 0.5) * dx, y: (j) => (j + 0.5) * dy };
230}
232// Effective dipole volume per unit top area (m): added mass of a strip of width W plus the
233// bar's own volume. A bar slice moving with velocity v acts as a dipole of moment v·V.
234export const dipoleDepth = (b) => ((Math.PI * b.W) / 4 + b.h0) * 1e-3;
236/**
237 * Radiation decay rate (1/s, amplitude) of one mode, treating every top-face cell as a
238 * vertical point dipole in free space. Power of a set of z-dipoles in a plane:
239 * P = ρω⁴/(24πc³) ΣΣ DᵢDⱼ C(k rᵢⱼ), C(u) = 1.5 (j0(u) - j1(u)/u).
240 */
241export function dipoleRadDecay(model, m) {
242 const b = model.design;
243 const { dx, dy } = cellGeom(model);
244 // coarse cells (about 40 x 4) for the double sum
245 const sx = Math.max(1, Math.round(model.nx / 40)), sy = Math.max(1, Math.round(model.ny / 4));
246 const V = dipoleDepth(b) * dx * dy;
247 const pts = [];
248 for (let i0 = 0; i0 < model.nx; i0 += sx) {
249 for (let j0 = 0; j0 < model.ny; j0 += sy) {
250 let D = 0, cx = 0, cy = 0, n = 0;
251 for (let i = i0; i < Math.min(model.nx, i0 + sx); i++) {
252 for (let j = j0; j < Math.min(model.ny, j0 + sy); j++) {
253 D += m.phi[j * model.nx + i] * V; cx += (i + 0.5) * dx; cy += (j + 0.5) * dy; n++;
254 }
255 }
256 pts.push([cx / n, cy / n, D]);
257 }
258 }
259 const w = 2 * Math.PI * m.f, k = w / AIR.c;
260 let S = 0;
261 for (let p = 0; p < pts.length; p++) {
262 for (let q = 0; q < pts.length; q++) {
263 const u = k * Math.hypot(pts[p][0] - pts[q][0], pts[p][1] - pts[q][1]);
264 let C = 1;
265 if (u > 1e-4) {
266 const j0 = Math.sin(u) / u, j1 = Math.sin(u) / (u * u) - Math.cos(u) / u;
267 C = 1.5 * (j0 - j1 / u);
268 }
269 S += pts[p][2] * pts[q][2] * C;
270 }
271 }
272 // energy of a unit-amplitude mass-normalized mode is ω²/2; σ = P / (2E)
273 return (AIR.rho * w ** 4 * Math.max(0, S)) / (24 * Math.PI * AIR.c ** 3);
274}