/ interactive_tutorials / hamiltonian_systems
Sign in
interactive_tutorials / hamiltonian_systems
Double pendulum listening to orbits with damping slide
Jeremy Magland <jmagland@flatironinstitute.org> committed commit 01a2ed191899 parent abcc9b6 Browse files
3 changed files+86−13
app/index.htmlmodified+44−0View file
@@ -713,6 +713,50 @@
713713 </div>
714714 </div>
715715 </section>
716+
717+ <section id="double-damped" data-hint="Damping" class="page">
718+ <h2>Double pendulum: listening to orbits with damping</h2>
719+
720+ <div class="demo explorer listen">
721+ <div class="explorer-controls">
722+ <label class="explorer-energy">Initial energy <span data-tex="H"></span>
723+ <input id="double-damped-energy" type="range" min="-28" max="30" step="0.1">
724+ <output id="double-damped-energy-value"></output>
725+ </label>
726+ <label class="explorer-energy">Damping <span data-tex="b"></span>
727+ <input id="double-damped-damping" type="range" min="0" max="0.01" step="0.0005">
728+ <output id="double-damped-damping-value"></output>
729+ </label>
730+ <button id="double-damped-auto" class="run">Auto</button>
731+ <button id="double-damped-gpu" class="run" hidden>GPU fill</button>
732+ <button id="double-damped-reset" class="run" aria-label="Clear orbits" title="Clear orbits">&#8634;</button>
733+ </div>
734+ <div class="listen-body">
735+ <canvas id="double-damped-canvas"></canvas>
736+ <div class="listen-panel">
737+ <label class="listen-row">Signal
738+ <select id="double-damped-signal">
739+ <option value="theta1">θ₁</option>
740+ <option value="theta2">θ₂</option>
741+ <option value="p1">p₁</option>
742+ <option value="p2">p₂</option>
743+ <option value="x">lower bob, horizontal position</option>
744+ </select>
745+ </label>
746+ <label class="listen-row" title="How much the motion is sped up">Frequency
747+ <input id="double-damped-speed" type="range" min="0" max="1" step="any">
748+ <output id="double-damped-speed-value"></output>
749+ </label>
750+ <div class="stage">
751+ <canvas id="double-damped-spectrogram"></canvas>
752+ <div class="run-controls">
753+ <button id="double-damped-play" class="run" disabled>Play</button>
754+ </div>
755+ </div>
756+ </div>
757+ </div>
758+ </div>
759+ </section>
716760 </div>
717761 </main>
718762 <script src="crafted-link.js"></script>
app/main.jsmodified+40−11View file
@@ -953,7 +953,9 @@ function doubleEnergy(params, [t1, t2, p1, p2]) {
953953 }
954954
955955 // Hamilton's equations, as written in the recipe: the rates of change of
956-// (theta1, theta2, p1, p2), written into out.
956+// (theta1, theta2, p1, p2), written into out. With params.b (page 16) there
957+// is also friction, a force -b dtheta_k/dt on each angle, as on the damped
958+// spring page; the system is then no longer Hamiltonian.
957959 function doubleRates(params, t1, t2, p1, p2, out) {
958960 const { m1, m2, L1, L2, g } = params;
959961 const delta = t1 - t2;
@@ -969,6 +971,10 @@ function doubleRates(params, t1, t2, p1, p2, out) {
969971 out[1] = ((m1 + m2) * L1 * p2 - m2 * L2 * p1 * c) / (m2 * L1 * L2 * L2 * D);
970972 out[2] = -(m1 + m2) * g * L1 * Math.sin(t1) - dTdDelta;
971973 out[3] = -m2 * g * L2 * Math.sin(t2) + dTdDelta;
974+ if (params.b) {
975+ out[2] -= params.b * out[0];
976+ out[3] -= params.b * out[1];
977+ }
972978 }
973979
974980 // One step of the implicit midpoint rule, z' = z + h f((z + z') / 2), solved by
@@ -1670,7 +1676,7 @@ const EXPLORER_COLORS = ['#537eba', '#2e8b57', '#1a9cb0', '#6a4fb3', '#2c5aa0',
16701676 // P.maxDots crossings; the fixed-point iteration stops at a tolerance suited
16711677 // to 32-bit floats.
16721678 const EXPLORER_SHADER = `
1673-struct Params { m1: f32, m2: f32, L1: f32, L2: f32, g: f32, h: f32, steps: u32, maxDots: u32, n: u32 }
1679+struct Params { m1: f32, m2: f32, L1: f32, L2: f32, g: f32, h: f32, steps: u32, maxDots: u32, n: u32, b: f32 }
16741680 @group(0) @binding(0) var<uniform> P: Params;
16751681 @group(0) @binding(1) var<storage, read_write> state: array<vec4f>;
16761682 @group(0) @binding(2) var<storage, read_write> slopes: array<vec4f>;
@@ -1697,11 +1703,11 @@ fn rates(z: vec4f) -> vec4f {
16971703 let T = (P.m2 * P.L2 * P.L2 * z.z * z.z + (P.m1 + P.m2) * P.L1 * P.L1 * z.w * z.w
16981704 - 2.0 * P.m2 * P.L1 * P.L2 * z.z * z.w * c) / (2.0 * P.m2 * P.L1 * P.L1 * P.L2 * P.L2 * D);
16991705 let dT = (z.z * z.w * s / (P.L1 * P.L2) - 2.0 * P.m2 * T * s * c) / D;
1700- return vec4f(
1701- (P.L2 * z.z - P.L1 * z.w * c) / (P.L1 * P.L1 * P.L2 * D),
1702- ((P.m1 + P.m2) * P.L1 * z.w - P.m2 * P.L2 * z.z * c) / (P.m2 * P.L1 * P.L2 * P.L2 * D),
1703- -(P.m1 + P.m2) * P.g * P.L1 * sin(z.x) - dT,
1704- -P.m2 * P.g * P.L2 * sin(z.y) + dT);
1706+ let w1 = (P.L2 * z.z - P.L1 * z.w * c) / (P.L1 * P.L1 * P.L2 * D);
1707+ let w2 = ((P.m1 + P.m2) * P.L1 * z.w - P.m2 * P.L2 * z.z * c) / (P.m2 * P.L1 * P.L2 * P.L2 * D);
1708+ return vec4f(w1, w2,
1709+ -(P.m1 + P.m2) * P.g * P.L1 * sin(z.x) - dT - P.b * w1,
1710+ -P.m2 * P.g * P.L2 * sin(z.y) + dT - P.b * w2);
17051711 }
17061712
17071713 fn hermite(y0: vec4f, f0: vec4f, y1: vec4f, f1: vec4f, h: f32, s: f32) -> vec4f {
@@ -1790,8 +1796,10 @@ function explorerGpuSetup() {
17901796 return explorerGpu;
17911797 }
17921798
1793-// listen: also play orbits as sound, with createListener (page 15)
1794-function createExplorer({ id, listen = false }) {
1799+// listen: also play orbits as sound, with createListener (page 15).
1800+// damping: with a damping slider (page 16); orbits then spiral inward, so
1801+// they are not colored by dimension, and H is the energy they start with.
1802+function createExplorer({ id, listen = false, damping = false }) {
17951803 const canvas = document.getElementById(`${id}-canvas`);
17961804 const slider = document.getElementById(`${id}-energy`);
17971805 const output = document.getElementById(`${id}-energy-value`);
@@ -1883,6 +1891,7 @@ function createExplorer({ id, listen = false }) {
18831891
18841892 // the orbit's own cool color, blended toward red from d = 1.25 to d = 1.85
18851893 function orbitColor(o) {
1894+ if (damping) return o.base;
18861895 if (o.d === null) return null;
18871896 const t = Math.min(1, Math.max(0, (o.d - 1.25) / 0.6));
18881897 const a = o.base;
@@ -2045,7 +2054,7 @@ function createExplorer({ id, listen = false }) {
20452054 }
20462055 const n = o.dots.length / 2;
20472056 // the dimension estimate at 500 dots, then each time the count doubles
2048- if (n === 500 || (n > 1000 && (n & (n - 1)) === 0)) {
2057+ if (!damping && (n === 500 || (n > 1000 && (n & (n - 1)) === 0))) {
20492058 o.d = correlationDimension(o.firsts);
20502059 o.drawn = 0; // redraw the whole orbit in its new color
20512060 }
@@ -2066,7 +2075,7 @@ function createExplorer({ id, listen = false }) {
20662075 o.cells = null; // only needed while dots arrive
20672076 o.fine = null;
20682077 // too few dots for an estimate (stopped early by a click): it stays gray
2069- if (o.dots.length / 2 < 300) return;
2078+ if (damping || o.dots.length / 2 < 300) return;
20702079 o.d = correlationDimension(o.firsts);
20712080 o.drawn = 0;
20722081 }
@@ -2177,6 +2186,7 @@ function createExplorer({ id, listen = false }) {
21772186 const p = new ArrayBuffer(48);
21782187 new Float32Array(p).set([params.m1, params.m2, params.L1, params.L2, params.g, EXPLORER_STEP]);
21792188 new Uint32Array(p).set([run.steps, per, n], 6);
2189+ new Float32Array(p)[9] = params.b ?? 0;
21802190 device.queue.writeBuffer(run.params, 0, p);
21812191 device.queue.writeBuffer(run.running, 0, Uint32Array.from(run.orbits, (o) => (o.done ? 0 : 1)));
21822192 const encoder = device.createCommandEncoder();
@@ -2451,6 +2461,19 @@ function createExplorer({ id, listen = false }) {
24512461 reset();
24522462 });
24532463
2464+ if (damping) {
2465+ const bSlider = document.getElementById(`${id}-damping`);
2466+ const bOutput = document.getElementById(`${id}-damping-value`);
2467+ params.b = DAMPED_B;
2468+ bSlider.value = params.b;
2469+ bOutput.textContent = params.b.toFixed(4);
2470+ bSlider.addEventListener('input', () => {
2471+ params.b = Number(bSlider.value);
2472+ bOutput.textContent = params.b.toFixed(4);
2473+ reset();
2474+ });
2475+ }
2476+
24542477 function pause() {
24552478 active = false;
24562479 cancelAnimationFrame(rafId);
@@ -2795,10 +2818,16 @@ function createListener({ id, params, onChange }) {
27952818 const doubleExplorer = createExplorer({ id: 'double-explorer' });
27962819 const doubleListen = createExplorer({ id: 'double-listen', listen: true });
27972820
2821+// Page 16: listening to orbits with damping. The default b gives a decay time
2822+// of very roughly 1000 simulated seconds, a few seconds of sound at x400.
2823+const DAMPED_B = 0.002;
2824+const doubleDamped = createExplorer({ id: 'double-damped', listen: true, damping: true });
2825+
27982826 const sims = {
27992827 spring, energy, phase, pendulum, 'pendulum-phase': pendulumPhase, liouville, 'liouville-pendulum': liouvillePendulum, damped,
28002828 double: doublePendulum, 'double-phase': doublePhase, 'double-poincare': doublePoincare, 'double-dispersion': doubleDispersion,
28012829 'double-explorer': doubleExplorer, 'double-listen': doubleListen,
2830+ 'double-damped': doubleDamped,
28022831 };
28032832
28042833 // Reset buttons next to Start: back to the initial state, with trails cleared.
app/style.cssmodified+2−2View file
@@ -203,12 +203,12 @@ canvas.double { height: 700px; }
203203 spectrogram on the right. */
204204 .page:not(.split) > .demo.explorer.listen { width: 1724px; }
205205 .listen-body { display: grid; grid-template-columns: minmax(0, 1fr) 580px; gap: 48px; }
206-#double-listen-canvas { height: 700px; cursor: crosshair; }
206+#double-listen-canvas, #double-damped-canvas { height: 700px; cursor: crosshair; }
207207 .listen-panel { display: flex; flex-direction: column; gap: 20px; font-size: 28px; text-align: left; }
208208 .listen-row { display: flex; align-items: center; gap: 16px; }
209209 .listen-row select, .listen-row input { flex: 1; min-width: 0; }
210210 .listen-row output { width: 3.5em; text-align: right; font-variant-numeric: tabular-nums; }
211-#double-listen-spectrogram { height: 560px; }
211+#double-listen-spectrogram, #double-damped-spectrogram { height: 560px; }
212212 button.run:disabled { opacity: 0.4; cursor: default; background: var(--bg); color: var(--accent); }
213213
214214 /* A diagram with its Start/Stop and reset buttons in the top-right corner. */
↑↓ move↵openescclose