73fe08adouble pendulum: album app with composition 1 (poincaré)Jeremy Magland 1// Sanity checks for the pendulum model: energy conservation, controller tracking,
2// and typical Poincaré crossing rates / p2 ranges at several energies.
3import { DoublePendulum, wrapAngle } from "../src/physics/doublePendulum.ts";
5const cfg = { m1: 1, m2: 1, l1: 1, l2: 1, g: 1 };
6const dt = 0.002;
8for (const e of [0.005, 0.05, 0.2, 0.5, 1, 2]) {
9 const P = new DoublePendulum(cfg);
10 let s = 1;
11 P.randomize(e, () => ((s = (s * 16807) % 2147483647) / 2147483647));
12 P.tau = 1e9;
13 P.eTarget = P.energy();
14 const E0 = P.energy();
15 let prev = wrapAngle(P.y[0]);
16 let n = 0, pmax = 0, sumAbs = 0;
17 const Tsim = 400;
18 for (let i = 0; i < Tsim / dt; i++) {
19 P.step(dt);
20 const cur = wrapAngle(P.y[0]);
21 if (prev < 0 && cur >= 0 && cur - prev < Math.PI) {
22 n++;
23 const p2n = Math.abs(P.y[3]) / P.p2Scale;
24 pmax = Math.max(pmax, p2n);
25 sumAbs += p2n;
26 }
27 prev = cur;
28 }
29 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)}`);
30}
32// controller tracking
33const P = new DoublePendulum(cfg);
34P.randomize(0.005, Math.random);
35P.tau = 2;
36for (const target of [0.5, 1.2, 0.1, 0]) {
37 P.eTarget = target * P.vTop;
38 for (let i = 0; i < 40 / dt; i++) P.step(dt);
39 console.log(`target ${target} -> after 40 sim s: ${P.energyNorm().toFixed(4)}`);
40}
42// on-section seeding lands at the requested energy, moving upward through θ1 = 0
43import { seedOnSection } from "../src/physics/doublePendulum.ts";
44for (const e of [0.01, 0.3, 1.2]) {
45 const Q = new DoublePendulum({ m1: 1, m2: 2, l1: 1, l2: 0.7, g: 1 });
46 let ok = 0, maxErr = 0;
47 for (let i = 0; i < 200; i++) {
48 if (!seedOnSection(Q, e, Math.random)) continue;
49 ok++;
50 maxErr = Math.max(maxErr, Math.abs(Q.energyNorm() - e) / e);
51 if (Q.derived().w1 <= 0) throw new Error("w1 <= 0");
52 }
53 console.log(`seedOnSection e=${e}: ${ok}/200 seeded, max rel energy err ${maxErr.toExponential(1)}`);
54}