a1f5e15Islands in a sea of chaos: Duffing phase space with sonificationJeremy Magland 1// Renders the Poincaré section for a parameter set to a PNG, for choosing
2// presets: orbits from a row of starts on x' = 0 (and a few above it), each
3// in its own color. Prints, per start, whether it escaped.
4//
5// node scripts/section-png.mjs out.png freq alpha delta F drive [points] [xr] [vr]
6//
7// freq, drive in Hz; alpha, F in s^-2; delta in s^-1 (the tutorial's units).
8// xr, vr: the plotted half-ranges (default: from the orbits).
10import { writeFileSync } from 'node:fs';
11import { deflateSync } from 'node:zlib';
12import { createStepper, physicsOf } from '../app/js/duffing.js';
14const [out, freq, alpha, delta, F, drive, npts = '800', xr0, vr0] = process.argv.slice(2);
15const p = physicsOf({ freq: +freq, alpha: +alpha, delta: +delta, F: +F, drive: +drive });
16const { step, N } = createStepper(p);
17const SEEDS = 40;
18const XR = +xr0 || 0.9;
19const pts = [];
20const t0 = Date.now();
21let escaped = 0;
22for (let k = 0; k < SEEDS; k++) {
23 const x0 = XR * (-1 + (2 * k) / (SEEDS - 1));
24 const s = { x: x0, v: 0, escaped: false };
25 const o = [];
26 for (let i = 0; i < +npts && !s.escaped; i++) {
27 step(s);
28 if (!s.escaped) o.push(s.x, s.v);
29 }
30 if (s.escaped) escaped++;
31 pts.push(o);
32}
33const ms = Date.now() - t0;
34const V = (x) => 0.5 * p.omega2 * x * x + 0.25 * p.alpha * x ** 4;
35let xr = +xr0 || 0;
36let vr = +vr0 || (xr ? Math.sqrt(2 * (V(xr) - Math.min(V(0), p.omega2 < 0 && p.alpha > 0 ? -(p.omega2 ** 2) / (4 * p.alpha) : 0))) : 0);
37if (!xr) for (const o of pts) for (let i = 0; i < o.length; i += 2) { xr = Math.max(xr, Math.abs(o[i])); vr = Math.max(vr, Math.abs(o[i + 1])); }
38xr *= xr0 ? 1 : 1.05;
39vr *= vr0 ? 1 : 1.05;
41const W = 700;
42const H = 700;
43const img = Buffer.alloc(W * H * 3, 255);
44const hsl = (h) => {
45 const f = (n) => { const k = (n + h * 12) % 12; return 0.45 - 0.4 * Math.max(-1, Math.min(k - 3, 9 - k, 1)); };
46 return [f(0), f(8), f(4)].map((c) => Math.round(c * 255));
47};
48pts.forEach((o, k) => {
49 const c = hsl((k * 0.618) % 1);
50 for (let i = 0; i < o.length; i += 2) {
51 const px = Math.round(((o[i] / xr + 1) / 2) * (W - 1));
52 const py = Math.round(((1 - o[i + 1] / vr) / 2) * (H - 1));
53 if (px >= 0 && px < W && py >= 0 && py < H) img.set(c, 3 * (py * W + px));
54 }
55});
56// PNG: 8-bit RGB, no filtering
57const raw = Buffer.alloc(H * (1 + 3 * W));
58for (let y = 0; y < H; y++) img.copy(raw, y * (1 + 3 * W) + 1, 3 * y * W, 3 * (y + 1) * W);
59const crcTable = Array.from({ length: 256 }, (_, n) => { let c = n; for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; return c >>> 0; });
60const crc = (b) => { let c = ~0; for (const x of b) c = crcTable[(c ^ x) & 255] ^ (c >>> 8); return (~c) >>> 0; };
61const chunk = (type, data) => {
62 const len = Buffer.alloc(4); len.writeUInt32BE(data.length);
63 const td = Buffer.concat([Buffer.from(type), data]);
64 const c = Buffer.alloc(4); c.writeUInt32BE(crc(td));
65 return Buffer.concat([len, td, c]);
66};
67const ihdr = Buffer.alloc(13);
68ihdr.writeUInt32BE(W, 0); ihdr.writeUInt32BE(H, 4); ihdr[8] = 8; ihdr[9] = 2;
69writeFileSync(out, Buffer.concat([Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), chunk('IHDR', ihdr), chunk('IDAT', deflateSync(raw)), chunk('IEND', Buffer.alloc(0))]));
70console.log(`${out}: N=${N} steps/period, ${ms} ms, ${escaped}/${SEEDS} escaped, x ±${xr.toFixed(3)}, x' ±${vr.toFixed(0)}`);