// 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; } return { step, N, T }; }