1// The oscillators on these slides are all of the form
2// x'' + delta x' + omega2 x + alpha x^3 = F cos(Omega t)
3// (harmonic: alpha = delta = F = 0), with x the displacement from equilibrium
4// as a fraction of the spring's rest length and t in seconds. p is
5// { omega2, alpha, delta, F, Omega } (F and Omega optional, default 0); a
6// state s is { x, v, phase, escaped }, where phase = Omega t (mod 2 pi) is
7// the driving force's phase, kept so that changing Omega does not jump it.
8// Shared by the animation (main thread) and the sound (oscillator-worklet.js).
10// A softening spring (alpha < 0) can let the block escape: past |x| = X_MAX
11// (the spring fully compressed, or stretched to twice its length) the motion
12// stops and s.escaped is set.
13export const X_MAX = 1;
15// The conservative part of x'', and its potential.
16export const force = (p, x) => -p.omega2 * x - p.alpha * x * x * x;
17export const potential = (p, x) => 0.5 * p.omega2 * x * x + 0.25 * p.alpha * x ** 4;
19// Amplitude of the undamped orbit through (x, v): the largest A > 0 with
20// V(A) = E, the farthest the orbit reaches. With b = omega2 / 2 and
21// s = sqrt(b^2 + alpha E), A^2 = (s - b) / (alpha / 2). For b > 0 (positive
22// linear stiffness) this is computed as 2 E / (b + s), which avoids
23// cancellation when alpha is small; for b < 0 (a double well) there is no
24// cancellation. Falls back to |x| when there is no such turning point (no
25// restoring force, or an orbit over a softening barrier).
26export function amplitude(p, x, v) {
27 const E = 0.5 * v * v + potential(p, x);
28 const b = 0.5 * p.omega2;
29 const disc = b * b + p.alpha * E;
30 let a2 = NaN;
31 if (disc >= 0) {
32 const s = Math.sqrt(disc);
33 a2 = b >= 0 ? (2 * E) / (b + s) : (s - b) / (0.5 * p.alpha);
34 }
35 return Number.isFinite(a2) && a2 > 0 ? Math.sqrt(a2) : Math.abs(x);
36}
38// A bound on the local angular frequency sqrt(|d force / dx|) for motion of
39// amplitude at most A, for choosing step sizes. With alpha < 0 the stiffness
40// can grow (negatively) up to |x| = X_MAX.
41export function omegaBound(p, A) {
42 const xm = p.alpha > 0 ? Math.min(Math.abs(A), X_MAX) : X_MAX;
43 return Math.sqrt(Math.abs(p.omega2) + 3 * Math.abs(p.alpha) * xm * xm);
44}
46// Advance s by dt, in substeps short enough to stay accurate at audio
47// frequencies (omega h <= 0.02, omega from omegaBound, and Omega h <= 0.02
48// when there is a driving force).
49// Each substep is velocity Verlet for the spring and driving forces, between
50// two half steps of the damping v' = -delta v solved exactly (Strang
51// splitting), so with delta = F = 0 it is symplectic.
52export function step(p, s, dt, omega) {
53 if (s.escaped) return;
54 const F = p.F ?? 0;
55 const W = p.Omega ?? 0;
56 const n = Math.max(1, Math.ceil((Math.max(omega, F ? W : 0) * Math.abs(dt)) / 0.02));
57 const h = dt / n;
58 const decay = Math.exp(-0.5 * p.delta * h);
59 let { x, v } = s;
60 let phase = s.phase ?? 0;
61 let a = force(p, x) + F * Math.cos(phase);
62 for (let i = 0; i < n; i++) {
63 v *= decay;
64 x += v * h + 0.5 * a * h * h;
65 if (Math.abs(x) > X_MAX) {
66 x = Math.sign(x) * X_MAX;
67 v = 0;
68 s.escaped = true;
69 break;
70 }
71 phase += W * h;
72 const a1 = force(p, x) + F * Math.cos(phase);
73 v += 0.5 * (a + a1) * h;
74 a = a1;
75 v *= decay;
76 }
77 s.x = x;
78 s.v = v;
79 s.phase = phase % (2 * Math.PI);
80}
82// Move s onto the undamped orbit with amplitude A under p, keeping x where
83// possible and setting v for that orbit's energy. Used when parameters change
84// during the motion, so the size of the swing carries over and the motion is
85// not knocked over a softening barrier by a sudden change. If the force at A
86// points outward (A past a softening barrier), that orbit escapes, so start
87// from rest at A.
88export function ontoOrbit(p, s, A) {
89 const outward = A !== 0 && force(p, A) * A >= 0;
90 const room = potential(p, A) - potential(p, s.x);
91 if (outward || Math.abs(s.x) >= Math.abs(A) || room < 0) {
92 s.x = Math.abs(s.x) >= Math.abs(A) && !outward ? Math.sign(s.x) * Math.abs(A) : A;
93 s.v = 0;
94 } else {
95 s.v = (s.v < 0 ? -1 : 1) * Math.sqrt(2 * room);
96 }
97}
99// Change the parameters of a moving state from p to q (see ontoOrbit).
100export function retune(p, q, s) {
101 ontoOrbit(q, s, amplitude(p, s.x, s.v));
102}