/ proof-of-concept / bead-percussion
Sign in
proof-of-concept / bead-percussion
bead-percussion / src / sim / solver.ts
610 lines · 20.6 KBBlameHistoryRaw
1// Time integration of the bead system.
2//
3// The conservative part (springs, external force pulses) is integrated with
4// velocity Verlet, optionally composed into Yoshida's fourth-order scheme.
5// Damping (friction and dashpots) is linear in the velocities, dv/dt = -A v,
6// and is applied as a separate exact step v <- exp(-tau A) v, once per audio
7// sample (Strang splitting over the sample interval Delta = 1/fs). The
8// splitting error is second order in Delta and scales with the damping rate,
9// which is small compared with the oscillation frequencies; the conservative
10// part is second order (Verlet) or fourth order (Yoshida) in the step h.
12import type { BeadSystem, Method, Modes, SimResult } from "./types";
14const YOSHIDA_W1 = 1 / (2 - Math.cbrt(2));
15const YOSHIDA_W0 = -Math.cbrt(2) * YOSHIDA_W1;
17export function stepCoefficients(method: Method): number[] {
18 return method === "yoshida4" ? [YOSHIDA_W1, YOSHIDA_W0, YOSHIDA_W1] : [1];
21// Largest h*omega for which the method is stable on x'' = -omega^2 x.
22export function stabilityLimit(method: Method): number {
23 if (method === "verlet") return 2;
24 const cs = stepCoefficients(method);
25 const stable = (h: number) => {
26 // transfer matrix of one full step for omega = 1
27 let m = [1, 0, 0, 1]; // [x<-x, x<-v, v<-x, v<-v]
28 const mul = (a: number[], b: number[]) => [
29 a[0] * b[0] + a[1] * b[2],
30 a[0] * b[1] + a[1] * b[3],
31 a[2] * b[0] + a[3] * b[2],
32 a[2] * b[1] + a[3] * b[3],
33 ];
34 for (const c of cs) {
35 const kick = [1, 0, (-c * h) / 2, 1];
36 const drift = [1, c * h, 0, 1];
37 m = mul(kick, mul(drift, mul(kick, m)));
38 }
39 return Math.abs(m[0] + m[3]) < 2;
40 };
41 let h = 1e-3;
42 while (stable(h + 1e-3)) h += 1e-3;
43 return h;
46// Error of the numerical oscillation frequency for x'' = -omega^2 x, in cents
47// (positive: the simulated tone is sharp).
48export function frequencyErrorCents(method: Method, omega: number, h: number): number {
49 let m = [1, 0, 0, 1];
50 const mul = (a: number[], b: number[]) => [
51 a[0] * b[0] + a[1] * b[2],
52 a[0] * b[1] + a[1] * b[3],
53 a[2] * b[0] + a[3] * b[2],
54 a[2] * b[1] + a[3] * b[3],
55 ];
56 for (const c of stepCoefficients(method)) {
57 const kick = [1, 0, (-c * h * omega * omega) / 2, 1];
58 const drift = [1, c * h, 0, 1];
59 m = mul(kick, mul(drift, mul(kick, m)));
60 }
61 const half = (m[0] + m[3]) / 2;
62 if (Math.abs(half) >= 1) return NaN;
63 const omegaNum = Math.acos(half) / h;
64 return 1200 * Math.log2(omegaNum / omega);
67export interface SimOptions {
68 modes?: Modes | null;
69 onProgress?: (t: number) => void;
70 // called at every frame record with the full-precision state (do not keep the arrays)
71 onFrame?: (frame: number, t: number, x: Float64Array, v: Float64Array) => void;
72 maxAnimBytes?: number;
75export function simulate(sys: BeadSystem, opts: SimOptions = {}): SimResult {
76 const t0 = performance.now();
77 const warnings: string[] = [];
78 const { n, mass, pinned } = sys;
79 const N3 = 3 * n;
80 const x = new Float64Array(sys.pos0);
81 const v = new Float64Array(N3);
82 const F = new Float64Array(N3);
83 const invm = new Float64Array(n);
84 for (let i = 0; i < n; i++) invm[i] = pinned[i] ? 0 : 1 / mass[i];
86 // ---- forces ------------------------------------------------------------
87 const { si, sj, sk, sL0, sk3, bi, bj, bk, bkb, bcb } = sys;
88 const ns = si.length;
89 const nb = bi.length;
90 const internalForces = (): number => {
91 F.fill(0);
92 let pe = 0;
93 for (let s = 0; s < ns; s++) {
94 const i3 = 3 * si[s];
95 const j3 = 3 * sj[s];
96 const dx = x[j3] - x[i3];
97 const dy = x[j3 + 1] - x[i3 + 1];
98 const dz = x[j3 + 2] - x[i3 + 2];
99 const L = Math.sqrt(dx * dx + dy * dy + dz * dz);
100 const e = L - sL0[s];
101 const k = sk[s];
102 const k3 = sk3[s];
103 const e2 = e * e;
104 pe += 0.5 * k * e2 + 0.25 * k3 * e2 * e2;
105 const g = (k * e + k3 * e2 * e) / L;
106 F[i3] += g * dx;
107 F[i3 + 1] += g * dy;
108 F[i3 + 2] += g * dz;
109 F[j3] -= g * dx;
110 F[j3 + 1] -= g * dy;
111 F[j3 + 2] -= g * dz;
112 }
113 for (let b = 0; b < nb; b++) {
114 const i3 = 3 * bi[b];
115 const j3 = 3 * bj[b];
116 const k3 = 3 * bk[b];
117 const kb = bkb[b];
118 const d0 = x[i3] - 2 * x[j3] + x[k3];
119 const d1 = x[i3 + 1] - 2 * x[j3 + 1] + x[k3 + 1];
120 const d2 = x[i3 + 2] - 2 * x[j3 + 2] + x[k3 + 2];
121 pe += 0.5 * kb * (d0 * d0 + d1 * d1 + d2 * d2);
122 F[i3] -= kb * d0;
123 F[i3 + 1] -= kb * d1;
124 F[i3 + 2] -= kb * d2;
125 F[j3] += 2 * kb * d0;
126 F[j3 + 1] += 2 * kb * d1;
127 F[j3 + 2] += 2 * kb * d2;
128 F[k3] -= kb * d0;
129 F[k3 + 1] -= kb * d1;
130 F[k3 + 2] -= kb * d2;
131 }
132 return pe;
133 };
134 const pulses = sys.hits.filter(h => h.duration > 0);
135 const externalForces = (t: number) => {
136 for (const p of pulses) {
137 const u = (t - p.t) / p.duration;
138 if (u <= 0 || u >= 1) continue;
139 const w = 0.5 * (1 - Math.cos(2 * Math.PI * u));
140 for (const b of p.beads) {
141 F[3 * b] += w * p.force[0];
142 F[3 * b + 1] += w * p.force[1];
143 F[3 * b + 2] += w * p.force[2];
144 }
145 }
146 };
148 // ---- rest state checks ---------------------------------------------------
149 let pe = internalForces();
150 const peRest = pe; // energy stored by pre-tension; energies are reported relative to rest
151 let residualForce = 0;
152 for (let i = 0; i < n; i++) {
153 if (pinned[i]) continue;
154 const f = Math.hypot(F[3 * i], F[3 * i + 1], F[3 * i + 2]);
155 if (f > residualForce) residualForce = f;
156 }
157 let maxTension = 0;
158 for (let s = 0; s < ns; s++) {
159 const i3 = 3 * si[s];
160 const j3 = 3 * sj[s];
161 const L = Math.hypot(x[j3] - x[i3], x[j3 + 1] - x[i3 + 1], x[j3 + 2] - x[i3 + 2]);
162 const e = L - sL0[s];
163 maxTension = Math.max(maxTension, Math.abs(sk[s] * e + sk3[s] * e * e * e));
164 }
165 // force scale: the larger of the rest tension and the force of a 1e-3 strain
166 // on any spring or bending triplet
167 let forceScale = maxTension;
168 for (let s = 0; s < ns; s++) forceScale = Math.max(forceScale, 1e-3 * sk[s] * sL0[s]);
169 for (let b = 0; b < nb; b++) {
170 const i3 = 3 * bi[b];
171 const j3 = 3 * bj[b];
172 const len = Math.hypot(x[j3] - x[i3], x[j3 + 1] - x[i3 + 1], x[j3 + 2] - x[i3 + 2]);
173 forceScale = Math.max(forceScale, 1e-3 * bkb[b] * len);
174 }
175 if (residualForce > 1e-6 * Math.max(forceScale, 1e-30)) {
176 warnings.push(
177 `The rest positions are not an equilibrium: the largest net force on a free bead is ${residualForce.toExponential(2)} N ` +
178 `(largest spring force ${maxTension.toExponential(2)} N). The beads will move before any strike.`
179 );
180 }
182 // ---- time step -----------------------------------------------------------
183 const omegaMax = estimateOmegaMax(sys);
184 const fs = sys.sampleRate;
185 const hLimit = stabilityLimit(sys.method) / omegaMax;
186 let substeps: number;
187 if (sys.dt > 0) {
188 substeps = Math.max(1, Math.round(1 / (fs * sys.dt)));
189 if (1 / (fs * substeps) > hLimit) {
190 warnings.push(
191 `dt = ${(1 / (fs * substeps)).toExponential(3)} s exceeds the stability limit ${hLimit.toExponential(3)} s.`
192 );
193 }
194 } else {
195 substeps = Math.max(1, Math.ceil(1 / (fs * sys.safety * hLimit)));
196 }
197 const h = 1 / (fs * substeps);
198 const nSamples = Math.max(1, Math.round(sys.duration * fs));
199 const nSteps = nSamples * substeps;
200 const cs = stepCoefficients(sys.method);
202 // ---- damping: v <- exp(-tau A) v ------------------------------------------
203 // dashpots with c = 0 (e.g. from bp_decay on untensioned springs) are dropped
204 const keepD = Array.from(sys.dc.keys()).filter(d => sys.dc[d] !== 0);
205 const di = Int32Array.from(keepD, d => sys.di[d]);
206 const dj = Int32Array.from(keepD, d => sys.dj[d]);
207 const dc = Float64Array.from(keepD, d => sys.dc[d]);
208 const { friction } = sys;
209 const nd = di.length;
210 let normA = 0; // infinity-norm bound of A
211 {
212 const row = new Float64Array(n);
213 for (let i = 0; i < n; i++) row[i] = friction[i];
214 for (let d = 0; d < nd; d++) {
215 row[di[d]] += 2 * dc[d];
216 row[dj[d]] += 2 * dc[d];
217 }
218 for (let b = 0; b < nb; b++) {
219 row[bi[b]] += 4 * bcb[b];
220 row[bj[b]] += 8 * bcb[b];
221 row[bk[b]] += 4 * bcb[b];
222 }
223 for (let i = 0; i < n; i++) normA = Math.max(normA, row[i] * invm[i]);
224 }
225 const hasDamping = normA > 0;
226 const frictionOnly = dc.every(c => c === 0) && bcb.every(c => c === 0); // then exp(-tau A) is diagonal
227 const term = new Float64Array(N3);
228 const tmp = new Float64Array(N3);
229 const fa = new Float64Array(n);
230 for (let i = 0; i < n; i++) fa[i] = friction[i] * invm[i];
231 const dai = new Float64Array(nd);
232 const daj = new Float64Array(nd);
233 for (let d = 0; d < nd; d++) {
234 dai[d] = dc[d] * invm[di[d]];
235 daj[d] = dc[d] * invm[dj[d]];
236 }
237 const applyA = (w: Float64Array, out: Float64Array) => {
238 for (let i = 0; i < n; i++) {
239 const a = fa[i];
240 out[3 * i] = a * w[3 * i];
241 out[3 * i + 1] = a * w[3 * i + 1];
242 out[3 * i + 2] = a * w[3 * i + 2];
243 }
244 for (let d = 0; d < nd; d++) {
245 const i3 = 3 * di[d];
246 const j3 = 3 * dj[d];
247 const ai = dai[d];
248 const aj = daj[d];
249 const r0 = w[i3] - w[j3];
250 const r1 = w[i3 + 1] - w[j3 + 1];
251 const r2 = w[i3 + 2] - w[j3 + 2];
252 out[i3] += ai * r0;
253 out[i3 + 1] += ai * r1;
254 out[i3 + 2] += ai * r2;
255 out[j3] -= aj * r0;
256 out[j3 + 1] -= aj * r1;
257 out[j3 + 2] -= aj * r2;
258 }
259 for (let b = 0; b < nb; b++) {
260 const cb = bcb[b];
261 if (cb === 0) continue;
262 const i3 = 3 * bi[b];
263 const j3 = 3 * bj[b];
264 const k3 = 3 * bk[b];
265 const ci = cb * invm[bi[b]];
266 const cj = -2 * cb * invm[bj[b]];
267 const ck = cb * invm[bk[b]];
268 const r0 = w[i3] - 2 * w[j3] + w[k3];
269 const r1 = w[i3 + 1] - 2 * w[j3 + 1] + w[k3 + 1];
270 const r2 = w[i3 + 2] - 2 * w[j3 + 2] + w[k3 + 2];
271 out[i3] += ci * r0;
272 out[i3 + 1] += ci * r1;
273 out[i3 + 2] += ci * r2;
274 out[j3] += cj * r0;
275 out[j3 + 1] += cj * r1;
276 out[j3 + 2] += cj * r2;
277 out[k3] += ck * r0;
278 out[k3 + 1] += ck * r1;
279 out[k3 + 2] += ck * r2;
280 }
281 };
282 const kinetic = () => {
283 let ke = 0;
284 for (let i = 0; i < n; i++) {
285 ke += 0.5 * mass[i] * (v[3 * i] ** 2 + v[3 * i + 1] ** 2 + v[3 * i + 2] ** 2);
286 }
287 return ke;
288 };
289 let dissipated = 0;
290 const applyDamping = (tau: number) => {
291 if (!hasDamping || tau === 0) return;
292 const keBefore = kinetic();
293 if (frictionOnly) {
294 for (let i = 0; i < n; i++) {
295 const f = Math.exp(-tau * friction[i] * invm[i]);
296 v[3 * i] *= f;
297 v[3 * i + 1] *= f;
298 v[3 * i + 2] *= f;
299 }
300 } else {
301 // Taylor series with scaling; |tau_s| * normA <= 0.5 so terms shrink fast.
302 const nsub = Math.max(1, Math.ceil((Math.abs(tau) * normA) / 0.5));
303 const ts = tau / nsub;
304 for (let r = 0; r < nsub; r++) {
305 let vmax = 0;
306 for (let q = 0; q < N3; q++) {
307 term[q] = v[q];
308 vmax = Math.max(vmax, Math.abs(v[q]));
309 }
310 if (vmax === 0) break;
311 for (let k = 1; k < 40; k++) {
312 applyA(term, tmp);
313 const c = -ts / k;
314 let tmax = 0;
315 for (let q = 0; q < N3; q++) {
316 const val = c * tmp[q];
317 term[q] = val;
318 v[q] += val;
319 const a = Math.abs(val);
320 if (a > tmax) tmax = a;
321 }
322 if (tmax <= 1e-17 * vmax) break;
323 }
324 }
325 }
326 dissipated += keBefore - kinetic();
327 };
329 // ---- recording -------------------------------------------------------------
330 const audio = new Float32Array(nSamples);
331 const lb = sys.listenBeads;
332 const [ldx, ldy, ldz] = sys.listenDir;
333 const pos0 = sys.pos0;
334 const listen = () => {
335 let s = 0;
336 for (const b of lb) {
337 s +=
338 (x[3 * b] - pos0[3 * b]) * ldx +
339 (x[3 * b + 1] - pos0[3 * b + 1]) * ldy +
340 (x[3 * b + 2] - pos0[3 * b + 2]) * ldz;
341 }
342 return s / lb.length;
343 };
344 const frameStride = Math.max(1, Math.round(fs / sys.frameRate));
345 const nFrames = Math.floor((nSamples - 1) / frameStride) + 1;
346 const maxAnimBytes = opts.maxAnimBytes ?? 150e6;
347 const animStride = frameStride * Math.max(1, Math.ceil((nFrames * N3 * 4) / maxAnimBytes));
348 const frameTimes = new Float64Array(nFrames);
349 const energy = new Float64Array(nFrames);
350 const dissipatedRec = new Float64Array(nFrames);
351 const animTimes: number[] = [];
352 const animFrames: Float32Array[] = [];
354 const modes = opts.modes ?? null;
355 const nModes = modes ? modes.freqs.length : 0;
356 const modeEnergy = modes ? new Float32Array(nFrames * nModes) : null;
357 let bz: Float64Array | null = null;
358 let bv: Float64Array | null = null;
359 let om2: Float64Array | null = null;
360 if (modes) {
361 bz = new Float64Array(modes.free.length);
362 bv = new Float64Array(modes.free.length);
363 om2 = new Float64Array(nModes);
364 for (let k = 0; k < nModes; k++) om2[k] = (2 * Math.PI * modes.freqs[k]) ** 2;
365 }
366 const recordModes = (f: number) => {
367 if (!modes || !modeEnergy || !bz || !bv || !om2) return;
368 const free = modes.free;
369 const nf = free.length;
370 for (let r = 0; r < nf; r++) {
371 const b = free[r];
372 const sm = Math.sqrt(mass[b]);
373 bz[r] = sm * (x[3 * b + 2] - pos0[3 * b + 2]);
374 bv[r] = sm * v[3 * b + 2];
375 }
376 const psi = modes.psi;
377 for (let k = 0; k < nModes; k++) {
378 let q = 0;
379 let qd = 0;
380 const off = k * nf;
381 for (let r = 0; r < nf; r++) {
382 q += psi[off + r] * bz[r];
383 qd += psi[off + r] * bv[r];
384 }
385 modeEnergy[f * nModes + k] = 0.5 * (qd * qd + om2[k] * q * q);
386 }
387 };
389 // ---- impulses, by audio sample ----------------------------------------------
390 const impulsesAt = new Map<number, typeof sys.hits>();
391 for (const hit of sys.hits) {
392 if (hit.duration > 0) continue;
393 const s = Math.round(hit.t * fs);
394 if (s >= nSamples) continue;
395 if (!impulsesAt.has(s)) impulsesAt.set(s, []);
396 impulsesAt.get(s)!.push(hit);
397 }
398 const tExcitationEnd = Math.max(0, ...sys.hits.map(hh => hh.t + hh.duration));
400 // ---- main loop ---------------------------------------------------------------
401 // Each audio sample interval Delta = substeps * h is one macro step:
402 // D(Delta/2) [substeps conservative steps] D(Delta/2). The damping halves of
403 // neighboring macro steps are merged unless something needs the velocities
404 // at the boundary (a frame record or an impulse).
405 const Delta = 1 / fs;
406 pe = internalForces();
407 externalForces(0);
408 let pendingD = 0;
409 let aborted: string | null = null;
410 let frame = 0;
411 let lastProgress = performance.now();
412 for (let s = 0; s <= nSteps; s++) {
413 const t = s * h;
414 const sample = s % substeps === 0 ? s / substeps : -1;
415 const isFrame = sample >= 0 && sample % frameStride === 0 && frame < nFrames;
416 const imps = sample >= 0 ? impulsesAt.get(sample) : undefined;
417 if ((isFrame || imps) && pendingD !== 0) {
418 applyDamping(pendingD);
419 pendingD = 0;
420 }
421 if (imps) {
422 for (const hit of imps) {
423 for (const b of hit.beads) {
424 if (pinned[b]) continue;
425 v[3 * b] += hit.velocity[0];
426 v[3 * b + 1] += hit.velocity[1];
427 v[3 * b + 2] += hit.velocity[2];
428 }
429 }
430 }
431 if (sample >= 0 && sample < nSamples) audio[sample] = listen();
432 if (isFrame) {
433 frameTimes[frame] = t;
434 energy[frame] = kinetic() + pe - peRest;
435 dissipatedRec[frame] = dissipated;
436 recordModes(frame);
437 opts.onFrame?.(frame, t, x, v);
438 if (sample % animStride === 0) {
439 const d = new Float32Array(N3);
440 for (let q = 0; q < N3; q++) d[q] = x[q] - pos0[q];
441 animTimes.push(t);
442 animFrames.push(d);
443 }
444 frame++;
445 }
446 if (s === nSteps) break;
447 if (sample >= 0) {
448 pendingD += Delta / 2;
449 applyDamping(pendingD);
450 pendingD = 0;
451 }
453 let tj = t;
454 for (const c of cs) {
455 const kh = (c * h) / 2;
456 for (let i = 0; i < n; i++) {
457 const im = invm[i] * kh;
458 v[3 * i] += im * F[3 * i];
459 v[3 * i + 1] += im * F[3 * i + 1];
460 v[3 * i + 2] += im * F[3 * i + 2];
461 }
462 const dh = c * h;
463 for (let q = 0; q < N3; q++) x[q] += dh * v[q];
464 tj += c * h;
465 pe = internalForces();
466 externalForces(tj);
467 for (let i = 0; i < n; i++) {
468 const im = invm[i] * kh;
469 v[3 * i] += im * F[3 * i];
470 v[3 * i + 1] += im * F[3 * i + 1];
471 v[3 * i + 2] += im * F[3 * i + 2];
472 }
473 }
474 if ((s + 1) % substeps === 0) pendingD = Delta / 2;
476 if ((s & 1023) === 0) {
477 if (!Number.isFinite(pe)) {
478 aborted = `The solution blew up at t = ${t.toFixed(4)} s. Reduce dt (or safety) or the strike strength.`;
479 break;
480 }
481 const now = performance.now();
482 if (opts.onProgress && now - lastProgress > 200) {
483 opts.onProgress(t);
484 lastProgress = now;
485 }
486 }
487 }
489 // energy drift after the last excitation
490 let energyDrift = 0;
491 let ref = -1;
492 for (let f = 0; f < frame; f++) {
493 if (frameTimes[f] < tExcitationEnd) continue;
494 const total = energy[f] + dissipatedRec[f];
495 if (ref < 0) ref = total;
496 else if (ref > 0) energyDrift = Math.max(energyDrift, Math.abs(total - ref) / ref);
497 }
499 return {
500 audio,
501 sampleRate: fs,
502 dt: h,
503 substeps,
504 omegaMax,
505 frameTimes: frameTimes.slice(0, frame),
506 energy: energy.slice(0, frame),
507 dissipated: dissipatedRec.slice(0, frame),
508 animTimes: Float64Array.from(animTimes),
509 animFrames,
510 modeEnergy: modeEnergy ? modeEnergy.slice(0, frame * nModes) : null,
511 residualForce,
512 maxTension,
513 energyDrift,
514 freqErrors: [1000, 2000, 5000, 10000, 20000]
515 .filter(hz => hz < fs / 2)
516 .map(hz => ({ hz, cents: frequencyErrorCents(sys.method, 2 * Math.PI * hz, h) })),
517 elapsedMs: performance.now() - t0,
518 aborted,
519 warnings,
520 };
523// Highest frequency (rad/s) of the system linearized about the rest positions,
524// by power iteration on M^{-1/2} H M^{-1/2}, with a small margin.
525export function estimateOmegaMax(sys: BeadSystem): number {
526 const { n, si, sj, sk, sL0, sk3, pos0, mass, pinned } = sys;
527 const ns = si.length;
528 // per-spring 3x3 blocks B = keff d d^T + (f/L)(I - d d^T)
529 const B = new Float64Array(ns * 6);
530 for (let s = 0; s < ns; s++) {
531 const i3 = 3 * si[s];
532 const j3 = 3 * sj[s];
533 const dx = pos0[j3] - pos0[i3];
534 const dy = pos0[j3 + 1] - pos0[i3 + 1];
535 const dz = pos0[j3 + 2] - pos0[i3 + 2];
536 const L = Math.sqrt(dx * dx + dy * dy + dz * dz);
537 const e = L - sL0[s];
538 const keff = sk[s] + 3 * sk3[s] * e * e;
539 const g = (sk[s] * e + sk3[s] * e * e * e) / L;
540 const u = [dx / L, dy / L, dz / L];
541 const a = keff - g;
542 B[6 * s] = a * u[0] * u[0] + g;
543 B[6 * s + 1] = a * u[1] * u[1] + g;
544 B[6 * s + 2] = a * u[2] * u[2] + g;
545 B[6 * s + 3] = a * u[0] * u[1];
546 B[6 * s + 4] = a * u[0] * u[2];
547 B[6 * s + 5] = a * u[1] * u[2];
548 }
549 const ism = new Float64Array(n);
550 for (let i = 0; i < n; i++) ism[i] = pinned[i] ? 0 : 1 / Math.sqrt(mass[i]);
551 let w = new Float64Array(3 * n);
552 let seed = 12345;
553 for (let q = 0; q < 3 * n; q++) {
554 seed = (seed * 1103515245 + 12345) & 0x7fffffff;
555 w[q] = ism[Math.floor(q / 3)] * (seed / 0x7fffffff - 0.5);
556 }
557 const u = new Float64Array(3 * n);
558 const y = new Float64Array(3 * n);
559 let lambda = 0;
560 for (let it = 0; it < 500; it++) {
561 let norm = 0;
562 for (let q = 0; q < 3 * n; q++) norm += w[q] * w[q];
563 norm = Math.sqrt(norm);
564 if (norm === 0) return 1;
565 for (let q = 0; q < 3 * n; q++) u[q] = (w[q] / norm) * ism[Math.floor(q / 3)];
566 y.fill(0);
567 for (let s = 0; s < ns; s++) {
568 const i3 = 3 * si[s];
569 const j3 = 3 * sj[s];
570 const r0 = u[i3] - u[j3];
571 const r1 = u[i3 + 1] - u[j3 + 1];
572 const r2 = u[i3 + 2] - u[j3 + 2];
573 const b = 6 * s;
574 const f0 = B[b] * r0 + B[b + 3] * r1 + B[b + 4] * r2;
575 const f1 = B[b + 3] * r0 + B[b + 1] * r1 + B[b + 5] * r2;
576 const f2 = B[b + 4] * r0 + B[b + 5] * r1 + B[b + 2] * r2;
577 y[i3] += f0;
578 y[i3 + 1] += f1;
579 y[i3 + 2] += f2;
580 y[j3] -= f0;
581 y[j3 + 1] -= f1;
582 y[j3 + 2] -= f2;
583 }
584 for (let b = 0; b < sys.bi.length; b++) {
585 const i3 = 3 * sys.bi[b];
586 const j3 = 3 * sys.bj[b];
587 const k3 = 3 * sys.bk[b];
588 const kb = sys.bkb[b];
589 for (let q = 0; q < 3; q++) {
590 const r = kb * (u[i3 + q] - 2 * u[j3 + q] + u[k3 + q]);
591 y[i3 + q] += r;
592 y[j3 + q] -= 2 * r;
593 y[k3 + q] += r;
594 }
595 }
596 let rq = 0;
597 const wNext = new Float64Array(3 * n);
598 for (let q = 0; q < 3 * n; q++) {
599 wNext[q] = y[q] * ism[Math.floor(q / 3)];
600 rq += (w[q] / norm) * wNext[q];
601 }
602 w = wNext;
603 if (it > 20 && Math.abs(rq - lambda) < 1e-8 * Math.abs(rq)) {
604 lambda = rq;
605 break;
606 }
607 lambda = rq;
608 }
609 return Math.sqrt(Math.max(lambda, 1e-300)) * 1.02;
↑↓ move↵openescclose