73fe08adouble pendulum: album app with composition 1 (poincaré)Jeremy Magland 1// Background Poincaré portrait, computed in "portraits" at a single fixed energy.
2//
3// A portrait is a pool of energy-conserving orbits, all at exactly the same energy, seeded
4// uniformly over the section. Orbits are long-lived so invariant curves (islands) fill in;
5// after `life` crossings an orbit is reseeded elsewhere to explore more of the section.
6// A new portrait starts only when the pendulum's energy has moved by more than `band`
7// (relative) AND the current portrait is at least `minHold` seconds old. A portrait stops
8// computing once it has `maxPoints`, so a settled energy costs no CPU.
9// Work runs in short slices separated by sleeps, using about `cpu` of one core.
11import { DoublePendulum, seedOnSection, wrapAngle } from "../../physics/doublePendulum.ts";
12import { mulberry32 } from "../../core/rng.ts";
14export interface OrbitConfig {
15 running: boolean;
16 /** start a new portrait right away (new play / seek) */
17 reset: boolean;
18 /** current normalized energy of the pendulum */
19 energy: number;
20 m2: number;
21 l2: number;
22 count: number;
23 /** crossings per orbit before it is reseeded elsewhere */
24 life: number;
25 /** relative energy change that triggers a new portrait */
26 band: number;
27 /** minimum portrait lifetime (s) */
28 minHold: number;
29 /** points per portrait, after which computing pauses */
30 maxPoints: number;
31 /** fraction of one core, 0..1 */
32 cpu: number;
33}
35export interface OrbitBatch {
36 gen: number;
37 energy: number;
38 pts: Float32Array; // [θ2, p2, θ2, p2, …]
39}
41interface Orbit {
42 pend: DoublePendulum;
43 crossings: number;
44 prev: number;
45 ok: boolean;
46}
48const DT = 0.005;
49const SLICE_MS = 8;
50const STEPS_PER_CHECK = 100;
52let cfg: OrbitConfig | null = null;
53let timer: ReturnType<typeof setTimeout> | null = null;
54const rand = mulberry32((Math.random() * 2 ** 31) | 0);
56let gen = 0;
57let genE = 0;
58let genStart = 0;
59let genPoints = 0;
60let genM2 = 0;
61let genL2 = 0;
62let orbits: Orbit[] = [];
64self.onmessage = (ev: MessageEvent<OrbitConfig>) => {
65 const reset = cfg?.reset ?? false;
66 cfg = ev.data;
67 cfg.reset ||= reset; // keep a pending reset until work() has seen it
68 if (cfg.running && timer === null) timer = setTimeout(work, 0);
69};
71function newPortrait(c: OrbitConfig) {
72 gen++;
73 genE = c.energy;
74 genStart = performance.now();
75 genPoints = 0;
76 genM2 = c.m2;
77 genL2 = c.l2;
78 orbits = [];
79}
81function seed(o: Orbit) {
82 o.crossings = 0;
83 o.prev = 0;
84 o.ok = seedOnSection(o.pend, genE, rand);
85}
87function work() {
88 timer = null;
89 const c = cfg;
90 if (!c || !c.running) return;
91 const sleep = (ms: number) => (timer = setTimeout(work, ms));
92 if (c.energy < 1e-5) return void sleep(100);
94 const age = (performance.now() - genStart) / 1000;
95 const drift = genE > 0 ? Math.abs(Math.log(c.energy / genE)) : Infinity;
96 if (gen === 0 || c.reset || c.m2 !== genM2 || c.l2 !== genL2 || (drift > Math.log(1 + c.band) && age >= c.minHold)) {
97 c.reset = false;
98 newPortrait(c);
99 }
100 if (genPoints >= c.maxPoints) return void sleep(50); // portrait complete: idle
102 const count = Math.max(1, Math.round(c.count));
103 while (orbits.length < count) {
104 const pend = new DoublePendulum({ m1: 1, l1: 1, g: 1, m2: genM2, l2: genL2 });
105 pend.controlled = false;
106 const o: Orbit = { pend, crossings: 0, prev: 0, ok: false };
107 seed(o);
108 orbits.push(o);
109 }
110 if (orbits.length > count) orbits.length = count;
112 const out: number[] = [];
113 const t0 = performance.now();
114 while (performance.now() - t0 < SLICE_MS) {
115 for (const o of orbits) {
116 if (!o.ok || o.crossings >= c.life) {
117 seed(o);
118 if (!o.ok) continue;
119 }
120 const P = o.pend;
121 const y = P.y;
122 for (let i = 0; i < STEPS_PER_CHECK; i++) {
123 const pTh2 = y[1], pP2 = y[3];
124 P.step(DT);
125 const cur = wrapAngle(y[0]);
126 if (o.prev < 0 && cur >= 0 && cur - o.prev < Math.PI) {
127 const f = -o.prev / (cur - o.prev);
128 out.push(wrapAngle(pTh2 + f * (y[1] - pTh2)), (pP2 + f * (y[3] - pP2)) / P.p2Scale);
129 o.crossings++;
130 }
131 o.prev = cur;
132 }
133 }
134 }
135 genPoints += out.length / 2;
137 if (out.length) {
138 const pts = new Float32Array(out);
139 const msg: OrbitBatch = { gen, energy: genE, pts };
140 (self as unknown as Worker).postMessage(msg, [pts.buffer]);
141 }
142 const cpu = Math.max(0.02, Math.min(1, c.cpu));
143 sleep((SLICE_MS * (1 - cpu)) / cpu);
144}