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