1// The driven Duffing oscillator, in the units of the hardening and softening
2// tutorial:
3// x'' + delta x' + omega2 x + alpha x^3 = F cos(Omega t)
4// with x the displacement as a fraction of the spring's rest length, t in
5// seconds, omega2 = sgn(f0) (2 pi f0)^2 (negative: a double well).
6//
7// The phase-space picture is the stroboscopic Poincaré section: (x, x') once
8// per drive period, at drive phase 0. A Stepper advances a state by exactly
9// one drive period in N equal steps, so each call lands on the section.
10//
11// Each step is a fourth-order Yoshida composition of a leapfrog, itself
12// split as damping (exact) / kick / drift / kick / damping, with time
13// advancing in the drift. Undamped, it is symplectic in the extended phase
14// space (x, x', t), so the islands keep their shape over long runs.
16// Past |x| = X_MAX (the spring fully compressed, or stretched to twice its
17// length) the motion stops: the orbit has escaped.
18export const X_MAX = 1;
20// Slider values (as in the tutorial) to the physics.
21export function physicsOf({ freq, alpha, delta, F, drive }) {
22 return {
23 omega2: Math.sign(freq) * (2 * Math.PI * freq) ** 2,
24 alpha,
25 delta,
26 F,
27 Omega: 2 * Math.PI * drive,
28 };
29}
31const CBRT2 = Math.cbrt(2);
32const W1 = 1 / (2 - CBRT2);
33const W0 = -CBRT2 * W1;
34const COMPOSITION = [W1, W0, W1];
35const OMEGA_H = 0.08; // largest omega h per step
37// The stepper for parameters p. step(s, out) advances s = { x, v, escaped }
38// by one drive period; if out (a Float32Array of 2 (N + 1) values) is given,
39// it receives (x, x') at each of the N + 1 step boundaries, for drawing the
40// trail.
41export function createStepper(p) {
42 const T = (2 * Math.PI) / p.Omega;
43 // the fastest local frequency for |x| up to X_MAX
44 const omegaMax = Math.max(Math.sqrt(Math.abs(p.omega2) + 3 * Math.abs(p.alpha) * X_MAX * X_MAX), p.Omega);
45 const N = Math.max(16, Math.ceil((omegaMax * T) / OMEGA_H));
46 const h = T / N;
47 // The kicks happen at the same times in every period, so tabulate the
48 // driving force there: per step, per Yoshida stage, the two half kicks.
49 const drive = new Float64Array(N * 6);
50 const decay = COMPOSITION.map((w) => Math.exp(-0.5 * p.delta * w * h));
51 let t = 0;
52 for (let i = 0; i < N; i++) {
53 for (let j = 0; j < 3; j++) {
54 const hj = COMPOSITION[j] * h;
55 drive[6 * i + 2 * j] = p.F * Math.cos(p.Omega * t);
56 t += hj;
57 drive[6 * i + 2 * j + 1] = p.F * Math.cos(p.Omega * t);
58 }
59 }
60 const { omega2, alpha } = p;
61 function step(s, out) {
62 if (s.escaped) return;
63 let { x, v } = s;
64 if (out) { out[0] = x; out[1] = v; }
65 for (let i = 0; i < N; i++) {
66 for (let j = 0; j < 3; j++) {
67 const hj = COMPOSITION[j] * h;
68 const d = decay[j];
69 v *= d;
70 v += 0.5 * hj * (drive[6 * i + 2 * j] - omega2 * x - alpha * x * x * x);
71 x += hj * v;
72 v += 0.5 * hj * (drive[6 * i + 2 * j + 1] - omega2 * x - alpha * x * x * x);
73 v *= d;
74 }
75 if (!(Math.abs(x) <= X_MAX)) {
76 s.escaped = true;
77 s.x = Math.sign(x) * X_MAX;
78 s.v = 0;
79 // the rest of the path stays where it escaped
80 if (out) for (let k = i + 1; k <= N; k++) { out[2 * k] = s.x; out[2 * k + 1] = 0; }
81 return;
82 }
83 if (out) { out[2 * i + 2] = x; out[2 * i + 3] = v; }
84 }
85 s.x = x;
86 s.v = v;
87 }
88 return { step, N, T };
89}