// Orbits in the (x, x') plane for the phase-space slide, computed with the // integrator of duffing.js from rest at a set of starting displacements. // Run in a Web Worker (orbits-worker.js), since a driven system needs long // transients. // // - Conservative (no damping, no drive): each start gives a closed curve of // constant energy, followed for one period. Starts that give the same curve // (in a symmetric well, x0 and -x0) are kept once. Starts past a softening // barrier give a path that runs off ('escape'). // - Damped (no drive): each start spirals in, followed until its energy above // the bottom of its well has fallen to END_ENERGY of the start's. // - Driven: the motion lives in (x, x', drive phase), so each start is run // past its transient and the motion it settles to is drawn projected onto // (x, x'): periodic ones over their period (up to MAX_PERIOD drive // periods), others ('irregular': quasi-periodic or chaotic) over // IRREGULAR_PERIODS. Starts that settle to the same motion are kept once. // // Each orbit: { kind: 'closed' | 'spiral' | 'periodic' | 'irregular' | // 'escape' | 'open', pts (Float32Array of x, x' pairs), start ({ x, v, phase }, // where to start playing it), ref (its largest |x|, for the sound's level), // period (drive periods, for 'periodic') }. import { amplitude, omegaBound, step, X_MAX } from './duffing.js'; const STARTS = [0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5]; const DAMPED_STARTS = [0.15, 0.3, 0.45]; const DRIVEN_STARTS = [0, 0.1, 0.2, 0.3, 0.4, 0.5]; const MAX_POINTS = 30000; const END_ENERGY = 1e-3; const MAX_PERIOD = 8; const IRREGULAR_PERIODS = 120; const STEPS_PER_PERIOD = 100; // driven: steps per drive period const MAX_TRANSIENT_PERIODS = 6000; // driven: a cap on the transient, for very light damping const potential = (p, x) => 0.5 * p.omega2 * x * x + 0.25 * p.alpha * x ** 4; const energy = (p, s) => 0.5 * s.v * s.v + potential(p, s.x); // lowest potential energy (the bottom of the wells) function bottomEnergy(p) { if (p.omega2 < 0 && p.alpha > 0) return -(p.omega2 ** 2) / (4 * p.alpha); return 0; } // time between drawn points: about 80 per oscillation at the fastest rate // (with a floor of 1 Hz, for no restoring force at all) function sampleDt(p) { const w = Math.max(omegaBound(p, 0.5), 2 * Math.PI); return (2 * Math.PI) / (w * 80); } // a local bound for the step size (see step in duffing.js) const localOmega = (p, s) => omegaBound(p, Math.min(X_MAX, 1.2 * Math.max(amplitude(p, s.x, s.v), Math.abs(s.x)))); function finish(kind, pts, start, extra = {}) { const arr = Float32Array.from(pts); let ref = 0; for (let i = 0; i < arr.length; i += 2) ref = Math.max(ref, Math.abs(arr[i])); return { kind, pts: arr, start, ref, ...extra }; } // The closed curve through (x0, v0), followed for one period: past both // turning points (where x' changes sign) and back to x0 moving the same way. function closedOrbit(p, x0, v0 = 0) { const start = { x: x0, v: v0, phase: 0 }; const s = { ...start, escaped: false }; const dt = sampleDt(p); const pts = [x0, v0]; let sign = Math.sign(v0); let flips = 0; for (let i = 0; i < MAX_POINTS; i++) { const prevX = s.x; step(p, s, dt, localOmega(p, s)); if (s.escaped) return finish('escape', pts, start); pts.push(s.x, s.v); const sg = Math.sign(s.v); if (sg !== 0 && sg !== sign) { if (sign !== 0) flips++; sign = sg; } // from rest: back at x0 after turning at the other end; otherwise, after // both turning points, crossing x0 again moving the same way as at the start const back = v0 === 0 ? flips >= 2 : flips >= 2 && Math.sign(s.v) === Math.sign(v0) && (prevX - x0) * (s.x - x0) <= 0; if (back) { pts.push(x0, v0); return finish('closed', pts, start); } } return finish('open', pts, start); } function spiral(p, x0, v0 = 0) { const start = { x: x0, v: v0, phase: 0 }; const s = { ...start, escaped: false }; const dt = sampleDt(p); const eMin = bottomEnergy(p); const e0 = energy(p, s) - eMin; const pts = [x0, v0]; for (let i = 0; i < MAX_POINTS; i++) { step(p, s, dt, localOmega(p, s)); if (s.escaped) return finish('escape', pts, start); pts.push(s.x, s.v); if (energy(p, s) - eMin < END_ENERGY * e0) break; } return finish('spiral', pts, start); } // keepPath: also return the path from the start to the settled motion (as // path), and play from the start rather than from the settled motion. function drivenOrbit(p, x0, v0 = 0, keepPath = false) { const start0 = { x: x0, v: v0, phase: 0 }; const s = { ...start0, escaped: false }; const T = (2 * Math.PI) / p.Omega; const dt = T / STEPS_PER_PERIOD; const settle = p.delta > 0 ? Math.min(20, 30 / p.delta) : 0; const transient = Math.min(Math.ceil(settle / T), MAX_TRANSIENT_PERIODS) * STEPS_PER_PERIOD; const path = [x0, v0]; for (let i = 0; i < transient; i++) { step(p, s, dt, localOmega(p, s)); if (s.escaped) return finish('escape', path, start0); if (i % 2 === 1 && path.length < 2 * MAX_POINTS) path.push(s.x, s.v); } // the settled motion: once-per-period samples to find its period const start = { x: s.x, v: s.v, phase: s.phase }; const probe = { ...start, escaped: false }; const samples = []; let lo = Infinity; let hi = -Infinity; for (let k = 0; k < 64; k++) { for (let i = 0; i < STEPS_PER_PERIOD; i++) { step(p, probe, dt, localOmega(p, probe)); lo = Math.min(lo, probe.x); hi = Math.max(hi, probe.x); } if (probe.escaped) break; samples.push(probe.x, probe.v / p.Omega); // x' / Omega: both in rest lengths } if (probe.escaped) return finish('escape', path, start0); const tol = 0.01 * Math.max(hi - lo, 1e-3); let period = 0; for (let q = 1; q <= MAX_PERIOD && !period; q++) { let ok = true; for (let i = 0; i + 2 * q < samples.length; i += 2) { if (Math.abs(samples[i + 2 * q] - samples[i]) > tol) { ok = false; break; } } if (ok) period = q; } const periods = period || IRREGULAR_PERIODS; const rec = { ...start, escaped: false }; const pts = [rec.x, rec.v]; for (let i = 0; i < periods * STEPS_PER_PERIOD; i++) { step(p, rec, dt, localOmega(p, rec)); pts.push(rec.x, rec.v); } const poincare = samples.slice(0, 2 * Math.max(period, 1) * 4); const orbit = finish(period ? 'periodic' : 'irregular', pts, start, { period, poincare, range: [lo, hi] }); if (keepPath) { // the path only until it has about arrived: up to the first drive period // whose range of x is within 5% of the settled motion's (the loops after // that pile up on the settled curve) const perPeriod = 2 * (STEPS_PER_PERIOD / 2); // the path keeps every 2nd step const tolArrive = 0.05 * Math.max(hi - lo, 1e-3); for (let k = 0; k + perPeriod <= path.length; k += perPeriod) { let mn = Infinity; let mx = -Infinity; for (let i = k; i < k + perPeriod; i += 2) { mn = Math.min(mn, path[i]); mx = Math.max(mx, path[i]); } if (Math.abs(mn - lo) < tolArrive && Math.abs(mx - hi) < tolArrive) { path.length = k + perPeriod; break; } } orbit.path = Float32Array.from(path); orbit.start = start0; for (let i = 0; i < orbit.path.length; i += 2) orbit.ref = Math.max(orbit.ref, Math.abs(orbit.path[i])); } return orbit; } // whether two orbits are the same curve (or the same settled motion) function same(a, b) { if (a.kind !== b.kind) return false; if (a.kind === 'closed' || a.kind === 'spiral' || a.kind === 'escape' || a.kind === 'open') { if (a.kind !== 'closed') return false; const ext = (o) => { let mn = Infinity; let mx = -Infinity; for (let i = 0; i < o.pts.length; i += 2) { mn = Math.min(mn, o.pts[i]); mx = Math.max(mx, o.pts[i]); } return [mn, mx]; }; const [a0, a1] = ext(a); const [b0, b1] = ext(b); return Math.abs(a0 - b0) < 2e-3 && Math.abs(a1 - b1) < 2e-3; } if (a.kind === 'periodic') { if (a.period !== b.period) return false; const tol = 0.02 * Math.max(a.range[1] - a.range[0], 1e-3); // every settled point of one lies on the other for (let i = 0; i < a.poincare.length; i += 2) { let found = false; for (let j = 0; j < b.poincare.length && !found; j += 2) { found = Math.abs(a.poincare[i] - b.poincare[j]) < tol && Math.abs(a.poincare[i + 1] - b.poincare[j + 1]) < tol; } if (!found) return false; } return true; } // irregular: the same attractor if it covers the same range const w = Math.max(a.range[1] - a.range[0], b.range[1] - b.range[0], 1e-3); return Math.abs(a.range[0] - b.range[0]) < 0.05 * w && Math.abs(a.range[1] - b.range[1]) < 0.05 * w; } // The orbit from a chosen point (x, v): in a driven system from drive phase 0, // with the path to the settled motion (see drivenOrbit). export function computeOrbitFrom(p, { x, v }) { if (p.F > 0 && p.Omega > 0) return drivenOrbit(p, x, v, true); if (p.delta > 0) return spiral(p, x, v); return closedOrbit(p, x, v); } export function computeOrbits(p) { const driven = p.F > 0 && p.Omega > 0; const damped = !driven && p.delta > 0; const signed = (list) => list.flatMap((x) => (x === 0 ? [0] : [x, -x])); const orbits = []; const add = (o) => { if (!orbits.some((q) => same(q, o))) orbits.push(o); }; if (driven) signed(DRIVEN_STARTS).forEach((x0) => add(drivenOrbit(p, x0))); else if (damped) signed(DAMPED_STARTS).forEach((x0) => add(spiral(p, x0))); else signed(STARTS).forEach((x0) => add(closedOrbit(p, x0))); let xmax = 0; let vmax = 0; for (const o of orbits) { for (let i = 0; i < o.pts.length; i += 2) { xmax = Math.max(xmax, Math.abs(o.pts[i])); vmax = Math.max(vmax, Math.abs(o.pts[i + 1])); } } return { orbits, xmax: Math.min(xmax, X_MAX), vmax }; }