// The driven Duffing oscillator, in the units of the hardening and softening // tutorial: // x'' + delta x' + omega2 x + alpha x^3 = F cos(Omega t) // with x the displacement as a fraction of the spring's rest length, t in // seconds, omega2 = sgn(f0) (2 pi f0)^2 (negative: a double well). // // The phase-space picture is the stroboscopic Poincaré section: (x, x') once // per drive period, at drive phase 0. A Stepper advances a state by exactly // one drive period in N equal steps, so each call lands on the section. // // Each step is a fourth-order Yoshida composition of a leapfrog, itself // split as damping (exact) / kick / drift / kick / damping, with time // advancing in the drift. Undamped, it is symplectic in the extended phase // space (x, x', t), so the islands keep their shape over long runs. // Past |x| = X_MAX (the spring fully compressed, or stretched to twice its // length) the motion stops: the orbit has escaped. export const X_MAX = 1; // Slider values (as in the tutorial) to the physics. export function physicsOf({ freq, alpha, delta, F, drive }) { return { omega2: Math.sign(freq) * (2 * Math.PI * freq) ** 2, alpha, delta, F, Omega: 2 * Math.PI * drive, }; } const CBRT2 = Math.cbrt(2); const W1 = 1 / (2 - CBRT2); const W0 = -CBRT2 * W1; const COMPOSITION = [W1, W0, W1]; const OMEGA_H = 0.08; // largest omega h per step // The stepper for parameters p. step(s, out) advances s = { x, v, escaped } // by one drive period; if out (a Float32Array of 2 (N + 1) values) is given, // it receives (x, x') at each of the N + 1 step boundaries, for drawing the // trail. export function createStepper(p) { const T = (2 * Math.PI) / p.Omega; // the fastest local frequency for |x| up to X_MAX const omegaMax = Math.max(Math.sqrt(Math.abs(p.omega2) + 3 * Math.abs(p.alpha) * X_MAX * X_MAX), p.Omega); const N = Math.max(16, Math.ceil((omegaMax * T) / OMEGA_H)); const h = T / N; // The kicks happen at the same times in every period, so tabulate the // driving force there: per step, per Yoshida stage, the two half kicks. const drive = new Float64Array(N * 6); const decay = COMPOSITION.map((w) => Math.exp(-0.5 * p.delta * w * h)); let t = 0; for (let i = 0; i < N; i++) { for (let j = 0; j < 3; j++) { const hj = COMPOSITION[j] * h; drive[6 * i + 2 * j] = p.F * Math.cos(p.Omega * t); t += hj; drive[6 * i + 2 * j + 1] = p.F * Math.cos(p.Omega * t); } } const { omega2, alpha } = p; function step(s, out) { if (s.escaped) return; let { x, v } = s; if (out) { out[0] = x; out[1] = v; } for (let i = 0; i < N; i++) { for (let j = 0; j < 3; j++) { const hj = COMPOSITION[j] * h; const d = decay[j]; v *= d; v += 0.5 * hj * (drive[6 * i + 2 * j] - omega2 * x - alpha * x * x * x); x += hj * v; v += 0.5 * hj * (drive[6 * i + 2 * j + 1] - omega2 * x - alpha * x * x * x); v *= d; } if (!(Math.abs(x) <= X_MAX)) { s.escaped = true; s.x = Math.sign(x) * X_MAX; s.v = 0; // the rest of the path stays where it escaped if (out) for (let k = i + 1; k <= N; k++) { out[2 * k] = s.x; out[2 * k + 1] = 0; } return; } if (out) { out[2 * i + 2] = x; out[2 * i + 3] = v; } } s.x = x; s.v = v; } // Several oscillators coupled through their differences from the mean (of // those not escaped): each gets r^2 (xbar - x_k) + 2 r (vbar - v_k), a // spring and a damper that pull any mismatch in, critically damped, at // rate r (s^-1), while the mean motion is left as it was. Above some r the // oscillators fall into step and move as one. (A damper alone locks the // velocities but not the positions: two oscillators can then glide along // together in different wells.) The damper is solved exactly (vbar is // kept, the differences decay by exp(-2 r t)), in half steps around the // kicks and drift like the own damping; the spring joins the kicks. // stepMany(states, r, outs) advances the states one drive period; outs // (optional) as for step, one per state. const cache = new Map(); const couplingDecay = (r) => { if (!cache.has(r)) cache.set(r, COMPOSITION.map((w) => Math.exp(-r * w * h))); return cache.get(r); }; function stepMany(states, r, outs) { const m = states.length; const X = Float64Array.from(states, (s) => s.x); const V = Float64Array.from(states, (s) => s.v); const live = states.map((s) => !s.escaped); const cd = couplingDecay(r); const k2 = r * r; const meanX = () => { let sum = 0; let n = 0; for (let k = 0; k < m; k++) if (live[k]) { sum += X[k]; n++; } return n ? sum / n : 0; }; const couple = (f) => { let sum = 0; let n = 0; for (let k = 0; k < m; k++) if (live[k]) { sum += V[k]; n++; } if (n < 2) return; const mean = sum / n; for (let k = 0; k < m; k++) if (live[k]) V[k] = mean + (V[k] - mean) * f; }; if (outs) for (let k = 0; k < m; k++) { outs[k][0] = X[k]; outs[k][1] = V[k]; } for (let i = 0; i < N; i++) { for (let j = 0; j < 3; j++) { const hj = COMPOSITION[j] * h; const d = decay[j]; couple(cd[j]); // kick, drift, kick; the spring's pull on each uses the mean x at // its kick let xbar = meanX(); for (let k = 0; k < m; k++) { if (!live[k]) continue; const x = X[k]; V[k] = V[k] * d + 0.5 * hj * (drive[6 * i + 2 * j] - omega2 * x - alpha * x * x * x + k2 * (xbar - x)); X[k] = x + hj * V[k]; } xbar = meanX(); for (let k = 0; k < m; k++) { if (!live[k]) continue; const x = X[k]; V[k] = (V[k] + 0.5 * hj * (drive[6 * i + 2 * j + 1] - omega2 * x - alpha * x * x * x + k2 * (xbar - x))) * d; } couple(cd[j]); } for (let k = 0; k < m; k++) { if (live[k] && !(Math.abs(X[k]) <= X_MAX)) { live[k] = false; states[k].escaped = true; X[k] = Math.sign(X[k]) * X_MAX; V[k] = 0; if (outs) for (let q = i + 1; q <= N; q++) { outs[k][2 * q] = X[k]; outs[k][2 * q + 1] = 0; } } if (live[k] && outs) { outs[k][2 * i + 2] = X[k]; outs[k][2 * i + 3] = V[k]; } } } for (let k = 0; k < m; k++) { states[k].x = X[k]; states[k].v = V[k]; } } return { step, stepMany, N, T }; }