/ music / double-pendulum
Sign in
music / double-pendulum
double-pendulum / src / compositions / c04-descent / fall.ts
61 lines · 2.6 KBCodeBlameHistory
f7d91baAdd composition 4: crashJeremy Magland 1// Starting from straight up: the nudge that makes the pendulum visibly tip at a chosen time.
75ca48aOverhaul playback: background-computed, buffered, one clockJeremy Magland 2import { cos, pow, sin } from "../../physics/dmath.ts";
f7d91baAdd composition 4: crashJeremy Magland 3import { DoublePendulum, type PendulumConfig } from "../../physics/doublePendulum.ts";
5export const DT = 0.002; // simulation step (sim seconds), shared with the piece
7/** Inverted (both arms up), at rest, nudged by d (outer arm by d·ratio) toward `side` (±1). */
8export function inverted(cfg: PendulumConfig, d: number, ratio: number, side: number): DoublePendulum {
9 const p = new DoublePendulum(cfg);
10 p.controlled = false;
11 p.y[0] = Math.PI + side * d;
12 p.y[1] = Math.PI + side * d * ratio;
13 return p;
16export function bob(cfg: PendulumConfig, y: ArrayLike<number>): [number, number] {
75ca48aOverhaul playback: background-computed, buffered, one clockJeremy Magland 17 return [cfg.l1 * sin(y[0]) + cfg.l2 * sin(y[1]), cfg.l1 * cos(y[0]) + cfg.l2 * cos(y[1])];
f7d91baAdd composition 4: crashJeremy Magland 18}
20function tipTime(cfg: PendulumConfig, d: number, ratio: number, thr: number, maxT: number): number {
21 const p = inverted(cfg, d, ratio, 1);
22 for (let t = 0; t < maxT; t += DT) {
23 p.step(DT);
24 if (Math.abs(bob(cfg, p.y)[0]) > thr) return t + DT;
25 }
26 return Infinity;
29/**
3fb7715crash: robust tip calibration (scan + refine all brackets)Jeremy Magland 30 * Nudge whose bob moves `thr` sideways at sim time T. Tipping time is not monotonic in the nudge:
31 * it levels off (even rises a little) at a floor of a few seconds, then drops abruptly once the
32 * nudge itself is nearly visible. So scan a log grid, refine by bisection inside every interval
33 * that brackets T, and take whatever lands closest to T. Nudges below ~1e-15
34 * are lost to rounding.
f7d91baAdd composition 4: crashJeremy Magland 35 */
36export function nudgeForTip(cfg: PendulumConfig, ratio: number, T: number, thr: number): { nudge: number; tipsAt: number } {
37 const maxT = 3 * T + 10;
3fb7715crash: robust tip calibration (scan + refine all brackets)Jeremy Magland 38 const tip = (d: number) => tipTime(cfg, d, ratio, thr, maxT);
39 const grid: { d: number; t: number }[] = [];
75ca48aOverhaul playback: background-computed, buffered, one clockJeremy Magland 40 for (let i = 0; i <= 58; i++) {
41 const d = pow(10, -15 + 0.25 * i);
42 grid.push({ d, t: tip(d) });
43 }
3fb7715crash: robust tip calibration (scan + refine all brackets)Jeremy Magland 44 // refine every bracket (tip(a) > T ≥ tip(b)); keep whatever lands closest to T
45 const cands = [...grid];
46 for (let i = 0; i + 1 < grid.length; i++) {
47 let a = grid[i], b = grid[i + 1];
48 if (!(a.t > T && b.t <= T)) continue;
49 for (let k = 0; k < 14; k++) {
50 const d = Math.sqrt(a.d * b.d), t = tip(d);
51 if (t > T) a = { d, t };
52 else b = { d, t };
53 }
54 cands.push(a, b);
f7d91baAdd composition 4: crashJeremy Magland 55 }
3fb7715crash: robust tip calibration (scan + refine all brackets)Jeremy Magland 56 // closest to T; among near-ties (within 1%), the smallest nudge
57 const err = (c: { t: number }) => Math.abs(c.t - T);
58 const bestErr = Math.min(...cands.map(err));
59 const best = cands.filter((c) => err(c) <= bestErr + 0.01 * T).reduce((x, y) => (y.d < x.d ? y : x));
60 return { nudge: best.d, tipsAt: best.t };
f7d91baAdd composition 4: crashJeremy Magland 61}
↑↓ move↵openescclose