// Frictionless double pendulum in canonical (Hamiltonian) coordinates. // State y = [θ1, θ2, p1, p2]; angles measured from straight down. // // Energy is steered toward a target with a generalized force Q = λ·p. // Because p = M·ω, that gives dE/dt = λ·pᵀω = 2λT, so choosing // λ = (E* − E) / (τ (2T + ε)) // makes E relax toward E* with time constant ≈ τ while the motion stays smooth. export interface PendulumConfig { m1: number; m2: number; l1: number; l2: number; g: number; } export interface Derived { w1: number; w2: number; T: number; V: number; } export class DoublePendulum { cfg: PendulumConfig; y = new Float64Array(4); t = 0; /** Target energy (absolute units) and relaxation time used by the controller. */ eTarget = 0; tau = 2; /** When false the energy controller is off and energy is conserved. */ controlled = true; private k1 = new Float64Array(4); private k2 = new Float64Array(4); private k3 = new Float64Array(4); private k4 = new Float64Array(4); private tmp = new Float64Array(4); constructor(cfg: PendulumConfig) { this.cfg = { ...cfg }; } /** Potential energy of both arms straight up (with V = 0 hanging down). Used as the energy unit. */ get vTop(): number { const { m1, m2, l1, l2, g } = this.cfg; return 2 * (m1 + m2) * g * l1 + 2 * m2 * g * l2; } /** Natural momentum scale for p2 (so plots and mappings don't depend on masses/lengths). */ get p2Scale(): number { const { m2, l2 } = this.cfg; return Math.sqrt(m2 * l2 * l2 * this.vTop); } derived(y: ArrayLike = this.y): Derived { const { m1, m2, l1, l2, g } = this.cfg; const [th1, th2, p1, p2] = [y[0], y[1], y[2], y[3]]; const d = th1 - th2; const cd = Math.cos(d); const sd = Math.sin(d); const den = m1 + m2 * sd * sd; const w1 = (l2 * p1 - l1 * p2 * cd) / (l1 * l1 * l2 * den); const w2 = (l1 * (m1 + m2) * p2 - l2 * m2 * p1 * cd) / (l1 * l2 * l2 * m2 * den); const T = 0.5 * (p1 * w1 + p2 * w2); const V = (m1 + m2) * g * l1 * (1 - Math.cos(th1)) + m2 * g * l2 * (1 - Math.cos(th2)); return { w1, w2, T, V }; } energy(y: ArrayLike = this.y): number { const d = this.derived(y); return d.T + d.V; } /** Energy in units of vTop. */ energyNorm(): number { return this.energy() / this.vTop; } private deriv(y: Float64Array, out: Float64Array) { const { m1, m2, l1, l2, g } = this.cfg; const th1 = y[0], th2 = y[1], p1 = y[2], p2 = y[3]; const d = th1 - th2; const cd = Math.cos(d); const sd = Math.sin(d); const den = m1 + m2 * sd * sd; const w1 = (l2 * p1 - l1 * p2 * cd) / (l1 * l1 * l2 * den); const w2 = (l1 * (m1 + m2) * p2 - l2 * m2 * p1 * cd) / (l1 * l2 * l2 * m2 * den); const C1 = (p1 * p2 * sd) / (l1 * l2 * den); const C2 = ((l2 * l2 * m2 * p1 * p1 + l1 * l1 * (m1 + m2) * p2 * p2 - 2 * l1 * l2 * m2 * p1 * p2 * cd) * Math.sin(2 * d)) / (2 * l1 * l1 * l2 * l2 * den * den); // energy controller const T = 0.5 * (p1 * w1 + p2 * w2); const V = (m1 + m2) * g * l1 * (1 - Math.cos(th1)) + m2 * g * l2 * (1 - Math.cos(th2)); let lam = 0; if (this.controlled) { const eps = 0.02 * this.vTop; const lamMax = 4 / this.tau; lam = (this.eTarget - (T + V)) / (this.tau * (2 * T + eps)); if (lam > lamMax) lam = lamMax; else if (lam < -lamMax) lam = -lamMax; } out[0] = w1; out[1] = w2; out[2] = -(m1 + m2) * g * l1 * Math.sin(th1) - C1 + C2 + lam * p1; out[3] = -m2 * g * l2 * Math.sin(th2) + C1 - C2 + lam * p2; } /** One RK4 step. */ step(dt: number) { const { y, k1, k2, k3, k4, tmp } = this; this.deriv(y, k1); for (let i = 0; i < 4; i++) tmp[i] = y[i] + 0.5 * dt * k1[i]; this.deriv(tmp, k2); for (let i = 0; i < 4; i++) tmp[i] = y[i] + 0.5 * dt * k2[i]; this.deriv(tmp, k3); for (let i = 0; i < 4; i++) tmp[i] = y[i] + dt * k3[i]; this.deriv(tmp, k4); for (let i = 0; i < 4; i++) y[i] += (dt / 6) * (k1[i] + 2 * k2[i] + 2 * k3[i] + k4[i]); this.t += dt; } /** * Random state with normalized energy e (in units of vTop). * Angles are drawn so that V < E, the remaining energy goes into a random velocity direction. */ randomize(e: number, rand: () => number) { const { m1, m2, l1, l2 } = this.cfg; const E = Math.max(e, 1e-6) * this.vTop; const a = Math.min(Math.PI, 3 * Math.sqrt(E / (m2 * this.cfg.g * Math.min(l1, l2)))); let th1 = 0, th2 = 0, V = 0; for (let tries = 0; tries < 1000; tries++) { th1 = (2 * rand() - 1) * a; th2 = (2 * rand() - 1) * a; V = this.derived([th1, th2, 0, 0]).V; if (V < 0.9 * E) break; th1 = th2 = 0; V = 0; } const K = E - V; const phi = rand() * 2 * Math.PI; const u1 = Math.cos(phi), u2 = Math.sin(phi); const cd = Math.cos(th1 - th2); // mass matrix M const M11 = (m1 + m2) * l1 * l1; const M12 = m2 * l1 * l2 * cd; const M22 = m2 * l2 * l2; const T1 = 0.5 * (M11 * u1 * u1 + 2 * M12 * u1 * u2 + M22 * u2 * u2); const s = Math.sqrt(K / T1); const w1 = s * u1, w2 = s * u2; this.y[0] = th1; this.y[1] = th2; this.y[2] = M11 * w1 + M12 * w2; this.y[3] = M12 * w1 + M22 * w2; this.t = 0; } } /** * Place the pendulum on the Poincaré section (θ1 = 0, dθ1/dt > 0) at normalized energy e, * with (θ2, p2) drawn uniformly over the energetically allowed region. Returns false if * no allowed point was found. */ export function seedOnSection(P: DoublePendulum, e: number, rand: () => number): boolean { const { m1, m2, l1, l2, g } = P.cfg; const E = e * P.vTop; // p2 range: bound from the kinetic energy available with θ1 = 0 const pMax = Math.sqrt(2 * E * m2 * l2 * l2 * (1 + m2 / m1)) + 1e-12; for (let tries = 0; tries < 400; tries++) { const th2 = (2 * rand() - 1) * Math.PI; const p2 = (2 * rand() - 1) * pMax; const V = m2 * g * l2 * (1 - Math.cos(th2)); if (V >= E) continue; const cd = Math.cos(-th2); const sd = Math.sin(-th2); const den = m1 + m2 * sd * sd; // T(p1) = E − V is a quadratic in p1 const a = m2 * l2 * l2; const b = -2 * m2 * l1 * l2 * p2 * cd; const c = (m1 + m2) * l1 * l1 * p2 * p2 - 2 * m2 * l1 * l1 * l2 * l2 * den * (E - V); const disc = b * b - 4 * a * c; if (disc < 0) continue; // the larger root is the one with dθ1/dt > 0 const p1 = (-b + Math.sqrt(disc)) / (2 * a); P.y[0] = 0; P.y[1] = th2; P.y[2] = p1; P.y[3] = p2; P.t = 0; return true; } return false; } export function wrapAngle(a: number): number { return Math.atan2(Math.sin(a), Math.cos(a)); }