/ music / double-pendulum
Sign in
music / double-pendulum
double-pendulum / src / physics / doublePendulum.ts
208 lines · 6.8 KBCodeBlameHistory
73fe08adouble pendulum: album app with composition 1 (poincaré)Jeremy Magland 1// Frictionless double pendulum in canonical (Hamiltonian) coordinates.
75ca48aOverhaul playback: background-computed, buffered, one clockJeremy Magland 2// Uses deterministic math (dmath.ts), so a simulation is bit-identical in every browser.
73fe08adouble pendulum: album app with composition 1 (poincaré)Jeremy Magland 3// State y = [θ1, θ2, p1, p2]; angles measured from straight down.
4//
5// Energy is steered toward a target with a generalized force Q = λ·p.
6// Because p = M·ω, that gives dE/dt = λ·pᵀω = 2λT, so choosing
7// λ = (E* − E) / (τ (2T + ε))
8// makes E relax toward E* with time constant ≈ τ while the motion stays smooth.
75ca48aOverhaul playback: background-computed, buffered, one clockJeremy Magland 10import { cos, sin, wrap } from "./dmath.ts";
73fe08adouble pendulum: album app with composition 1 (poincaré)Jeremy Magland 12export interface PendulumConfig {
13 m1: number;
14 m2: number;
15 l1: number;
16 l2: number;
17 g: number;
20export interface Derived {
21 w1: number;
22 w2: number;
23 T: number;
24 V: number;
27export class DoublePendulum {
28 cfg: PendulumConfig;
29 y = new Float64Array(4);
30 t = 0;
31 /** Target energy (absolute units) and relaxation time used by the controller. */
32 eTarget = 0;
33 tau = 2;
34 /** When false the energy controller is off and energy is conserved. */
35 controlled = true;
bc546ddAdd composition 3: divergenceJeremy Magland 36 /** Viscous damping rate (generalized force −damping·p, per sim second); 0 = none. */
37 damping = 0;
39 private k1 = new Float64Array(4);
40 private k2 = new Float64Array(4);
41 private k3 = new Float64Array(4);
42 private k4 = new Float64Array(4);
43 private tmp = new Float64Array(4);
45 constructor(cfg: PendulumConfig) {
46 this.cfg = { ...cfg };
47 }
49 /** Potential energy of both arms straight up (with V = 0 hanging down). Used as the energy unit. */
50 get vTop(): number {
51 const { m1, m2, l1, l2, g } = this.cfg;
52 return 2 * (m1 + m2) * g * l1 + 2 * m2 * g * l2;
53 }
55 /** Natural momentum scale for p2 (so plots and mappings don't depend on masses/lengths). */
56 get p2Scale(): number {
57 const { m2, l2 } = this.cfg;
58 return Math.sqrt(m2 * l2 * l2 * this.vTop);
59 }
61 derived(y: ArrayLike<number> = this.y): Derived {
62 const { m1, m2, l1, l2, g } = this.cfg;
63 const [th1, th2, p1, p2] = [y[0], y[1], y[2], y[3]];
64 const d = th1 - th2;
66 const sd = sin(d);
73fe08adouble pendulum: album app with composition 1 (poincaré)Jeremy Magland 67 const den = m1 + m2 * sd * sd;
68 const w1 = (l2 * p1 - l1 * p2 * cd) / (l1 * l1 * l2 * den);
69 const w2 = (l1 * (m1 + m2) * p2 - l2 * m2 * p1 * cd) / (l1 * l2 * l2 * m2 * den);
70 const T = 0.5 * (p1 * w1 + p2 * w2);
75ca48aOverhaul playback: background-computed, buffered, one clockJeremy Magland 71 const V = (m1 + m2) * g * l1 * (1 - cos(th1)) + m2 * g * l2 * (1 - cos(th2));
73fe08adouble pendulum: album app with composition 1 (poincaré)Jeremy Magland 72 return { w1, w2, T, V };
73 }
75 energy(y: ArrayLike<number> = this.y): number {
76 const d = this.derived(y);
77 return d.T + d.V;
78 }
80 /** Energy in units of vTop. */
81 energyNorm(): number {
82 return this.energy() / this.vTop;
83 }
85 private deriv(y: Float64Array, out: Float64Array) {
86 const { m1, m2, l1, l2, g } = this.cfg;
87 const th1 = y[0], th2 = y[1], p1 = y[2], p2 = y[3];
88 const d = th1 - th2;
90 const sd = sin(d);
73fe08adouble pendulum: album app with composition 1 (poincaré)Jeremy Magland 91 const den = m1 + m2 * sd * sd;
92 const w1 = (l2 * p1 - l1 * p2 * cd) / (l1 * l1 * l2 * den);
93 const w2 = (l1 * (m1 + m2) * p2 - l2 * m2 * p1 * cd) / (l1 * l2 * l2 * m2 * den);
94 const C1 = (p1 * p2 * sd) / (l1 * l2 * den);
95 const C2 =
96 ((l2 * l2 * m2 * p1 * p1 + l1 * l1 * (m1 + m2) * p2 * p2 - 2 * l1 * l2 * m2 * p1 * p2 * cd) *
73fe08adouble pendulum: album app with composition 1 (poincaré)Jeremy Magland 98 (2 * l1 * l1 * l2 * l2 * den * den);
100 // energy controller
101 const T = 0.5 * (p1 * w1 + p2 * w2);
75ca48aOverhaul playback: background-computed, buffered, one clockJeremy Magland 102 const V = (m1 + m2) * g * l1 * (1 - cos(th1)) + m2 * g * l2 * (1 - cos(th2));
104 if (this.controlled) {
105 const eps = 0.02 * this.vTop;
106 const lamMax = 4 / this.tau;
107 lam = (this.eTarget - (T + V)) / (this.tau * (2 * T + eps));
108 if (lam > lamMax) lam = lamMax;
109 else if (lam < -lamMax) lam = -lamMax;
110 }
bc546ddAdd composition 3: divergenceJeremy Magland 111 lam -= this.damping;
113 out[0] = w1;
114 out[1] = w2;
75ca48aOverhaul playback: background-computed, buffered, one clockJeremy Magland 115 out[2] = -(m1 + m2) * g * l1 * sin(th1) - C1 + C2 + lam * p1;
116 out[3] = -m2 * g * l2 * sin(th2) + C1 - C2 + lam * p2;
119 /** One RK4 step. */
120 step(dt: number) {
121 const { y, k1, k2, k3, k4, tmp } = this;
122 this.deriv(y, k1);
123 for (let i = 0; i < 4; i++) tmp[i] = y[i] + 0.5 * dt * k1[i];
124 this.deriv(tmp, k2);
125 for (let i = 0; i < 4; i++) tmp[i] = y[i] + 0.5 * dt * k2[i];
126 this.deriv(tmp, k3);
127 for (let i = 0; i < 4; i++) tmp[i] = y[i] + dt * k3[i];
128 this.deriv(tmp, k4);
129 for (let i = 0; i < 4; i++) y[i] += (dt / 6) * (k1[i] + 2 * k2[i] + 2 * k3[i] + k4[i]);
130 this.t += dt;
131 }
133 /**
134 * Random state with normalized energy e (in units of vTop).
135 * Angles are drawn so that V < E, the remaining energy goes into a random velocity direction.
136 */
137 randomize(e: number, rand: () => number) {
138 const { m1, m2, l1, l2 } = this.cfg;
139 const E = Math.max(e, 1e-6) * this.vTop;
140 const a = Math.min(Math.PI, 3 * Math.sqrt(E / (m2 * this.cfg.g * Math.min(l1, l2))));
141 let th1 = 0, th2 = 0, V = 0;
142 for (let tries = 0; tries < 1000; tries++) {
143 th1 = (2 * rand() - 1) * a;
144 th2 = (2 * rand() - 1) * a;
145 V = this.derived([th1, th2, 0, 0]).V;
146 if (V < 0.9 * E) break;
147 th1 = th2 = 0;
148 V = 0;
149 }
150 const K = E - V;
151 const phi = rand() * 2 * Math.PI;
75ca48aOverhaul playback: background-computed, buffered, one clockJeremy Magland 152 const u1 = cos(phi), u2 = sin(phi);
153 const cd = cos(th1 - th2);
155 const M11 = (m1 + m2) * l1 * l1;
156 const M12 = m2 * l1 * l2 * cd;
157 const M22 = m2 * l2 * l2;
158 const T1 = 0.5 * (M11 * u1 * u1 + 2 * M12 * u1 * u2 + M22 * u2 * u2);
159 const s = Math.sqrt(K / T1);
160 const w1 = s * u1, w2 = s * u2;
161 this.y[0] = th1;
162 this.y[1] = th2;
163 this.y[2] = M11 * w1 + M12 * w2;
164 this.y[3] = M12 * w1 + M22 * w2;
165 this.t = 0;
166 }
169/**
170 * Place the pendulum on the Poincaré section (θ1 = 0, dθ1/dt > 0) at normalized energy e,
171 * with (θ2, p2) drawn uniformly over the energetically allowed region. Returns false if
172 * no allowed point was found.
173 */
174export function seedOnSection(P: DoublePendulum, e: number, rand: () => number): boolean {
175 const { m1, m2, l1, l2, g } = P.cfg;
176 const E = e * P.vTop;
177 // p2 range: bound from the kinetic energy available with θ1 = 0
178 const pMax = Math.sqrt(2 * E * m2 * l2 * l2 * (1 + m2 / m1)) + 1e-12;
179 for (let tries = 0; tries < 400; tries++) {
180 const th2 = (2 * rand() - 1) * Math.PI;
181 const p2 = (2 * rand() - 1) * pMax;
75ca48aOverhaul playback: background-computed, buffered, one clockJeremy Magland 182 const V = m2 * g * l2 * (1 - cos(th2));
73fe08adouble pendulum: album app with composition 1 (poincaré)Jeremy Magland 183 if (V >= E) continue;
75ca48aOverhaul playback: background-computed, buffered, one clockJeremy Magland 184 const cd = cos(-th2);
185 const sd = sin(-th2);
73fe08adouble pendulum: album app with composition 1 (poincaré)Jeremy Magland 186 const den = m1 + m2 * sd * sd;
187 // T(p1) = E − V is a quadratic in p1
188 const a = m2 * l2 * l2;
189 const b = -2 * m2 * l1 * l2 * p2 * cd;
190 const c = (m1 + m2) * l1 * l1 * p2 * p2 - 2 * m2 * l1 * l1 * l2 * l2 * den * (E - V);
191 const disc = b * b - 4 * a * c;
192 if (disc < 0) continue;
193 // the larger root is the one with dθ1/dt > 0
194 const p1 = (-b + Math.sqrt(disc)) / (2 * a);
195 P.y[0] = 0;
196 P.y[1] = th2;
197 P.y[2] = p1;
198 P.y[3] = p2;
199 P.t = 0;
200 return true;
201 }
202 return false;
75ca48aOverhaul playback: background-computed, buffered, one clockJeremy Magland 205/** Angle wrapped to [−π, π) (deterministic). */
73fe08adouble pendulum: album app with composition 1 (poincaré)Jeremy Magland 206export function wrapAngle(a: number): number {
↑↓ move↵openescclose