/ interactive_tutorials / duffing_islands
Sign in
interactive_tutorials / duffing_islands
duffing_islands / app / js / duffing.js
166 lines · 6.4 KBBlameHistoryRaw
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 };
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 }
89 // Several oscillators coupled through their differences from the mean (of
90 // those not escaped): each gets r^2 (xbar - x_k) + 2 r (vbar - v_k), a
91 // spring and a damper that pull any mismatch in, critically damped, at
92 // rate r (s^-1), while the mean motion is left as it was. Above some r the
93 // oscillators fall into step and move as one. (A damper alone locks the
94 // velocities but not the positions: two oscillators can then glide along
95 // together in different wells.) The damper is solved exactly (vbar is
96 // kept, the differences decay by exp(-2 r t)), in half steps around the
97 // kicks and drift like the own damping; the spring joins the kicks.
98 // stepMany(states, r, outs) advances the states one drive period; outs
99 // (optional) as for step, one per state.
100 const cache = new Map();
101 const couplingDecay = (r) => {
102 if (!cache.has(r)) cache.set(r, COMPOSITION.map((w) => Math.exp(-r * w * h)));
103 return cache.get(r);
104 };
105 function stepMany(states, r, outs) {
106 const m = states.length;
107 const X = Float64Array.from(states, (s) => s.x);
108 const V = Float64Array.from(states, (s) => s.v);
109 const live = states.map((s) => !s.escaped);
110 const cd = couplingDecay(r);
111 const k2 = r * r;
112 const meanX = () => {
113 let sum = 0;
114 let n = 0;
115 for (let k = 0; k < m; k++) if (live[k]) { sum += X[k]; n++; }
116 return n ? sum / n : 0;
117 };
118 const couple = (f) => {
119 let sum = 0;
120 let n = 0;
121 for (let k = 0; k < m; k++) if (live[k]) { sum += V[k]; n++; }
122 if (n < 2) return;
123 const mean = sum / n;
124 for (let k = 0; k < m; k++) if (live[k]) V[k] = mean + (V[k] - mean) * f;
125 };
126 if (outs) for (let k = 0; k < m; k++) { outs[k][0] = X[k]; outs[k][1] = V[k]; }
127 for (let i = 0; i < N; i++) {
128 for (let j = 0; j < 3; j++) {
129 const hj = COMPOSITION[j] * h;
130 const d = decay[j];
131 couple(cd[j]);
132 // kick, drift, kick; the spring's pull on each uses the mean x at
133 // its kick
134 let xbar = meanX();
135 for (let k = 0; k < m; k++) {
136 if (!live[k]) continue;
137 const x = X[k];
138 V[k] = V[k] * d + 0.5 * hj * (drive[6 * i + 2 * j] - omega2 * x - alpha * x * x * x + k2 * (xbar - x));
139 X[k] = x + hj * V[k];
140 }
141 xbar = meanX();
142 for (let k = 0; k < m; k++) {
143 if (!live[k]) continue;
144 const x = X[k];
145 V[k] = (V[k] + 0.5 * hj * (drive[6 * i + 2 * j + 1] - omega2 * x - alpha * x * x * x + k2 * (xbar - x))) * d;
146 }
147 couple(cd[j]);
148 }
149 for (let k = 0; k < m; k++) {
150 if (live[k] && !(Math.abs(X[k]) <= X_MAX)) {
151 live[k] = false;
152 states[k].escaped = true;
153 X[k] = Math.sign(X[k]) * X_MAX;
154 V[k] = 0;
155 if (outs) for (let q = i + 1; q <= N; q++) { outs[k][2 * q] = X[k]; outs[k][2 * q + 1] = 0; }
156 }
157 if (live[k] && outs) { outs[k][2 * i + 2] = X[k]; outs[k][2 * i + 3] = V[k]; }
158 }
159 }
160 for (let k = 0; k < m; k++) {
161 states[k].x = X[k];
162 states[k].v = V[k];
163 }
164 }
165 return { step, stepMany, N, T };
↑↓ move↵openescclose