// Sanity checks for the pendulum model: energy conservation, controller tracking, // and typical Poincaré crossing rates / p2 ranges at several energies. import { DoublePendulum, wrapAngle } from "../src/physics/doublePendulum.ts"; const cfg = { m1: 1, m2: 1, l1: 1, l2: 1, g: 1 }; const dt = 0.002; for (const e of [0.005, 0.05, 0.2, 0.5, 1, 2]) { const P = new DoublePendulum(cfg); let s = 1; P.randomize(e, () => ((s = (s * 16807) % 2147483647) / 2147483647)); P.tau = 1e9; P.eTarget = P.energy(); const E0 = P.energy(); let prev = wrapAngle(P.y[0]); let n = 0, pmax = 0, sumAbs = 0; const Tsim = 400; for (let i = 0; i < Tsim / dt; i++) { P.step(dt); const cur = wrapAngle(P.y[0]); if (prev < 0 && cur >= 0 && cur - prev < Math.PI) { n++; const p2n = Math.abs(P.y[3]) / P.p2Scale; pmax = Math.max(pmax, p2n); sumAbs += p2n; } prev = cur; } console.log(`e=${e}: drift ${((P.energy() - E0) / E0).toExponential(1)}, crossings/simsec ${(n / Tsim).toFixed(3)}, |p2n| mean ${(sumAbs / Math.max(n, 1)).toFixed(3)} max ${pmax.toFixed(3)}`); } // controller tracking const P = new DoublePendulum(cfg); P.randomize(0.005, Math.random); P.tau = 2; for (const target of [0.5, 1.2, 0.1, 0]) { P.eTarget = target * P.vTop; for (let i = 0; i < 40 / dt; i++) P.step(dt); console.log(`target ${target} -> after 40 sim s: ${P.energyNorm().toFixed(4)}`); } // on-section seeding lands at the requested energy, moving upward through θ1 = 0 import { seedOnSection } from "../src/physics/doublePendulum.ts"; for (const e of [0.01, 0.3, 1.2]) { const Q = new DoublePendulum({ m1: 1, m2: 2, l1: 1, l2: 0.7, g: 1 }); let ok = 0, maxErr = 0; for (let i = 0; i < 200; i++) { if (!seedOnSection(Q, e, Math.random)) continue; ok++; maxErr = Math.max(maxErr, Math.abs(Q.energyNorm() - e) / e); if (Q.derived().w1 <= 0) throw new Error("w1 <= 0"); } console.log(`seedOnSection e=${e}: ${ok}/200 seeded, max rel energy err ${maxErr.toExponential(1)}`); }