/ interactive_tutorials / hardening_softening
Sign in
interactive_tutorials / hardening_softening
hardening_softening / app / js / orbits.js
241 lines · 9.7 KBCodeBlameHistory
7212d95Hardening and softening tutorialJeremy Magland 1// Orbits in the (x, x') plane for the phase-space slide, computed with the
2// integrator of duffing.js from rest at a set of starting displacements.
3// Run in a Web Worker (orbits-worker.js), since a driven system needs long
4// transients.
5//
6// - Conservative (no damping, no drive): each start gives a closed curve of
7// constant energy, followed for one period. Starts that give the same curve
8// (in a symmetric well, x0 and -x0) are kept once. Starts past a softening
9// barrier give a path that runs off ('escape').
10// - Damped (no drive): each start spirals in, followed until its energy above
11// the bottom of its well has fallen to END_ENERGY of the start's.
12// - Driven: the motion lives in (x, x', drive phase), so each start is run
13// past its transient and the motion it settles to is drawn projected onto
14// (x, x'): periodic ones over their period (up to MAX_PERIOD drive
15// periods), others ('irregular': quasi-periodic or chaotic) over
16// IRREGULAR_PERIODS. Starts that settle to the same motion are kept once.
17//
18// Each orbit: { kind: 'closed' | 'spiral' | 'periodic' | 'irregular' |
19// 'escape' | 'open', pts (Float32Array of x, x' pairs), start ({ x, v, phase },
20// where to start playing it), ref (its largest |x|, for the sound's level),
21// period (drive periods, for 'periodic') }.
23import { amplitude, omegaBound, step, X_MAX } from './duffing.js';
25const STARTS = [0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5];
26const DAMPED_STARTS = [0.15, 0.3, 0.45];
27const DRIVEN_STARTS = [0, 0.1, 0.2, 0.3, 0.4, 0.5];
28const MAX_POINTS = 30000;
29const END_ENERGY = 1e-3;
30const MAX_PERIOD = 8;
31const IRREGULAR_PERIODS = 120;
32const STEPS_PER_PERIOD = 100; // driven: steps per drive period
33const MAX_TRANSIENT_PERIODS = 6000; // driven: a cap on the transient, for very light damping
35const potential = (p, x) => 0.5 * p.omega2 * x * x + 0.25 * p.alpha * x ** 4;
36const energy = (p, s) => 0.5 * s.v * s.v + potential(p, s.x);
38// lowest potential energy (the bottom of the wells)
39function bottomEnergy(p) {
40 if (p.omega2 < 0 && p.alpha > 0) return -(p.omega2 ** 2) / (4 * p.alpha);
41 return 0;
44// time between drawn points: about 80 per oscillation at the fastest rate
45// (with a floor of 1 Hz, for no restoring force at all)
46function sampleDt(p) {
47 const w = Math.max(omegaBound(p, 0.5), 2 * Math.PI);
48 return (2 * Math.PI) / (w * 80);
51// a local bound for the step size (see step in duffing.js)
52const localOmega = (p, s) => omegaBound(p, Math.min(X_MAX, 1.2 * Math.max(amplitude(p, s.x, s.v), Math.abs(s.x))));
54function finish(kind, pts, start, extra = {}) {
55 const arr = Float32Array.from(pts);
56 let ref = 0;
57 for (let i = 0; i < arr.length; i += 2) ref = Math.max(ref, Math.abs(arr[i]));
58 return { kind, pts: arr, start, ref, ...extra };
61// The closed curve through (x0, v0), followed for one period: past both
62// turning points (where x' changes sign) and back to x0 moving the same way.
63function closedOrbit(p, x0, v0 = 0) {
64 const start = { x: x0, v: v0, phase: 0 };
65 const s = { ...start, escaped: false };
66 const dt = sampleDt(p);
67 const pts = [x0, v0];
68 let sign = Math.sign(v0);
69 let flips = 0;
70 for (let i = 0; i < MAX_POINTS; i++) {
71 const prevX = s.x;
72 step(p, s, dt, localOmega(p, s));
73 if (s.escaped) return finish('escape', pts, start);
74 pts.push(s.x, s.v);
75 const sg = Math.sign(s.v);
76 if (sg !== 0 && sg !== sign) {
77 if (sign !== 0) flips++;
78 sign = sg;
79 }
80 // from rest: back at x0 after turning at the other end; otherwise, after
81 // both turning points, crossing x0 again moving the same way as at the start
82 const back = v0 === 0
83 ? flips >= 2
84 : flips >= 2 && Math.sign(s.v) === Math.sign(v0) && (prevX - x0) * (s.x - x0) <= 0;
85 if (back) {
86 pts.push(x0, v0);
87 return finish('closed', pts, start);
88 }
89 }
90 return finish('open', pts, start);
93function spiral(p, x0, v0 = 0) {
94 const start = { x: x0, v: v0, phase: 0 };
95 const s = { ...start, escaped: false };
96 const dt = sampleDt(p);
97 const eMin = bottomEnergy(p);
98 const e0 = energy(p, s) - eMin;
99 const pts = [x0, v0];
100 for (let i = 0; i < MAX_POINTS; i++) {
101 step(p, s, dt, localOmega(p, s));
102 if (s.escaped) return finish('escape', pts, start);
103 pts.push(s.x, s.v);
104 if (energy(p, s) - eMin < END_ENERGY * e0) break;
105 }
106 return finish('spiral', pts, start);
109// keepPath: also return the path from the start to the settled motion (as
110// path), and play from the start rather than from the settled motion.
111function drivenOrbit(p, x0, v0 = 0, keepPath = false) {
112 const start0 = { x: x0, v: v0, phase: 0 };
113 const s = { ...start0, escaped: false };
114 const T = (2 * Math.PI) / p.Omega;
115 const dt = T / STEPS_PER_PERIOD;
116 const settle = p.delta > 0 ? Math.min(20, 30 / p.delta) : 0;
117 const transient = Math.min(Math.ceil(settle / T), MAX_TRANSIENT_PERIODS) * STEPS_PER_PERIOD;
118 const path = [x0, v0];
119 for (let i = 0; i < transient; i++) {
120 step(p, s, dt, localOmega(p, s));
121 if (s.escaped) return finish('escape', path, start0);
122 if (i % 2 === 1 && path.length < 2 * MAX_POINTS) path.push(s.x, s.v);
123 }
124 // the settled motion: once-per-period samples to find its period
125 const start = { x: s.x, v: s.v, phase: s.phase };
126 const probe = { ...start, escaped: false };
127 const samples = [];
128 let lo = Infinity;
129 let hi = -Infinity;
130 for (let k = 0; k < 64; k++) {
131 for (let i = 0; i < STEPS_PER_PERIOD; i++) {
132 step(p, probe, dt, localOmega(p, probe));
133 lo = Math.min(lo, probe.x);
134 hi = Math.max(hi, probe.x);
135 }
136 if (probe.escaped) break;
137 samples.push(probe.x, probe.v / p.Omega); // x' / Omega: both in rest lengths
138 }
139 if (probe.escaped) return finish('escape', path, start0);
140 const tol = 0.01 * Math.max(hi - lo, 1e-3);
141 let period = 0;
142 for (let q = 1; q <= MAX_PERIOD && !period; q++) {
143 let ok = true;
144 for (let i = 0; i + 2 * q < samples.length; i += 2) {
145 if (Math.abs(samples[i + 2 * q] - samples[i]) > tol) { ok = false; break; }
146 }
147 if (ok) period = q;
148 }
149 const periods = period || IRREGULAR_PERIODS;
150 const rec = { ...start, escaped: false };
151 const pts = [rec.x, rec.v];
152 for (let i = 0; i < periods * STEPS_PER_PERIOD; i++) {
153 step(p, rec, dt, localOmega(p, rec));
154 pts.push(rec.x, rec.v);
155 }
156 const poincare = samples.slice(0, 2 * Math.max(period, 1) * 4);
157 const orbit = finish(period ? 'periodic' : 'irregular', pts, start, { period, poincare, range: [lo, hi] });
158 if (keepPath) {
159 // the path only until it has about arrived: up to the first drive period
160 // whose range of x is within 5% of the settled motion's (the loops after
161 // that pile up on the settled curve)
162 const perPeriod = 2 * (STEPS_PER_PERIOD / 2); // the path keeps every 2nd step
163 const tolArrive = 0.05 * Math.max(hi - lo, 1e-3);
164 for (let k = 0; k + perPeriod <= path.length; k += perPeriod) {
165 let mn = Infinity;
166 let mx = -Infinity;
167 for (let i = k; i < k + perPeriod; i += 2) { mn = Math.min(mn, path[i]); mx = Math.max(mx, path[i]); }
168 if (Math.abs(mn - lo) < tolArrive && Math.abs(mx - hi) < tolArrive) {
169 path.length = k + perPeriod;
170 break;
171 }
172 }
173 orbit.path = Float32Array.from(path);
174 orbit.start = start0;
175 for (let i = 0; i < orbit.path.length; i += 2) orbit.ref = Math.max(orbit.ref, Math.abs(orbit.path[i]));
176 }
177 return orbit;
180// whether two orbits are the same curve (or the same settled motion)
181function same(a, b) {
182 if (a.kind !== b.kind) return false;
183 if (a.kind === 'closed' || a.kind === 'spiral' || a.kind === 'escape' || a.kind === 'open') {
184 if (a.kind !== 'closed') return false;
185 const ext = (o) => {
186 let mn = Infinity;
187 let mx = -Infinity;
188 for (let i = 0; i < o.pts.length; i += 2) { mn = Math.min(mn, o.pts[i]); mx = Math.max(mx, o.pts[i]); }
189 return [mn, mx];
190 };
191 const [a0, a1] = ext(a);
192 const [b0, b1] = ext(b);
193 return Math.abs(a0 - b0) < 2e-3 && Math.abs(a1 - b1) < 2e-3;
194 }
195 if (a.kind === 'periodic') {
196 if (a.period !== b.period) return false;
197 const tol = 0.02 * Math.max(a.range[1] - a.range[0], 1e-3);
198 // every settled point of one lies on the other
199 for (let i = 0; i < a.poincare.length; i += 2) {
200 let found = false;
201 for (let j = 0; j < b.poincare.length && !found; j += 2) {
202 found = Math.abs(a.poincare[i] - b.poincare[j]) < tol && Math.abs(a.poincare[i + 1] - b.poincare[j + 1]) < tol;
203 }
204 if (!found) return false;
205 }
206 return true;
207 }
208 // irregular: the same attractor if it covers the same range
209 const w = Math.max(a.range[1] - a.range[0], b.range[1] - b.range[0], 1e-3);
210 return Math.abs(a.range[0] - b.range[0]) < 0.05 * w && Math.abs(a.range[1] - b.range[1]) < 0.05 * w;
213// The orbit from a chosen point (x, v): in a driven system from drive phase 0,
214// with the path to the settled motion (see drivenOrbit).
215export function computeOrbitFrom(p, { x, v }) {
216 if (p.F > 0 && p.Omega > 0) return drivenOrbit(p, x, v, true);
217 if (p.delta > 0) return spiral(p, x, v);
218 return closedOrbit(p, x, v);
221export function computeOrbits(p) {
222 const driven = p.F > 0 && p.Omega > 0;
223 const damped = !driven && p.delta > 0;
224 const signed = (list) => list.flatMap((x) => (x === 0 ? [0] : [x, -x]));
225 const orbits = [];
226 const add = (o) => {
227 if (!orbits.some((q) => same(q, o))) orbits.push(o);
228 };
229 if (driven) signed(DRIVEN_STARTS).forEach((x0) => add(drivenOrbit(p, x0)));
230 else if (damped) signed(DAMPED_STARTS).forEach((x0) => add(spiral(p, x0)));
231 else signed(STARTS).forEach((x0) => add(closedOrbit(p, x0)));
232 let xmax = 0;
233 let vmax = 0;
234 for (const o of orbits) {
235 for (let i = 0; i < o.pts.length; i += 2) {
236 xmax = Math.max(xmax, Math.abs(o.pts[i]));
237 vmax = Math.max(vmax, Math.abs(o.pts[i + 1]));
238 }
239 }
240 return { orbits, xmax: Math.min(xmax, X_MAX), vmax };
↑↓ move↵openescclose