-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdyson.html
More file actions
315 lines (297 loc) · 13.4 KB
/
dyson.html
File metadata and controls
315 lines (297 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<!DOCTYPE html>
<html>
<head>
<title>Dyson 2-Ring Action Minimizer</title>
<script src="ring.js"></script>
<script src="action.js"></script>
</head>
<body bgcolor="#000000" text="#ffffff" style="font-family:monospace;">
<p><canvas id="cv" width="600" height="400" style="border:1px solid #ffffff;"></canvas></p>
<pre id="log" style="color:#00ff00;font-size:11px;max-height:300px;overflow:auto;"></pre>
<script>
(function() {
var N = 2;
var rm = 0.001;
var r = 0.4;
var l = 1 - r*r;
var BASE = { increment: 0.1, work: 20, points: 8, trail: false };
var inc = BASE.increment / BASE.work;
var colors = ['#ff0000', '#00ffff'];
// ---- Orbit sampling (same approach as eight.html) ----
function sampleOrbit(sunR) {
var cosmos = new Cosmos2D(Object.assign({}, BASE, { moons: [
{ y: sunR, z: 0, vy: 0, vz: 0, mass: 1-N*rm, l: 0, fixed: true },
{ y: Math.sqrt(1 - r*r), z: r, l: l, vy: -r, vz: 0, mass: rm }
]}));
var ring0 = cosmos.rings[1];
var traj = [[ring0.p.y, ring0.p.z, ring0.v.y / inc, ring0.v.z / inc]];
var prevVz = 0, wentNeg = false, wentPos = false, T = 0;
for (var i = 0; i < 100000; i++) {
cosmos.multistep();
var vz = ring0.v.z;
traj.push([ring0.p.y, ring0.p.z, ring0.v.y / inc, ring0.v.z / inc]);
if (!wentNeg && vz < 0) wentNeg = true;
if ( wentNeg && !wentPos && vz >= 0) wentPos = true;
if ( wentPos && vz < 0) {
var alpha = prevVz / (prevVz - vz);
T = i + alpha;
break;
}
prevVz = vz;
}
var need = Math.ceil(2 * T) + 2;
while (traj.length <= need) {
cosmos.multistep();
traj.push([ring0.p.y, ring0.p.z, ring0.v.y / inc, ring0.v.z / inc]);
}
var samples = [];
for (var k = 0; k < N; k++) {
var w = k / N;
var t1 = k * T / N, i1 = Math.floor(t1), f1 = t1 - i1;
var t2 = T + k * T / N, i2 = Math.floor(t2), f2 = t2 - i2;
var p1 = traj[i1], q1 = traj[i1 + 1] || traj[i1];
var p2 = traj[i2], q2 = traj[i2 + 1] || traj[i2];
samples.push({
y: w*(p1[0]+f1*(q1[0]-p1[0])) + (1-w)*(p2[0]+f2*(q2[0]-p2[0])),
z: w*(p1[1]+f1*(q1[1]-p1[1])) + (1-w)*(p2[1]+f2*(q2[1]-p2[1])),
vy: w*(p1[2]+f1*(q1[2]-p1[2])) + (1-w)*(p2[2]+f2*(q2[2]-p2[2])),
vz: w*(p1[3]+f1*(q1[3]-p1[3])) + (1-w)*(p2[3]+f2*(q2[3]-p2[3])),
});
}
return { samples: samples, T: T, traj: traj };
}
function makeMoons(samples, sunR) {
var moons = [{ y: sunR, z: 0, vy: 0, vz: 0, mass: 1-N*rm, l: 0,
fixed: true, color: '#ffff00', radius: 10 }];
for (var i = 0; i < N; i++)
moons.push({ y: samples[i].y, z: samples[i].z,
vy: samples[i].vy, vz: samples[i].vz,
l: l, mass: rm, color: colors[i], radius: 3 });
return moons;
}
// Run the full N-ring system for one period, re-sample.
// Returns { samples, traj, T } so callers can also use the trajectory
// (e.g. to initialise the action minimiser from the 2-ring orbit).
function sampleOrbitFull(sunR, samples) {
var cosmos = new Cosmos2D(Object.assign({}, BASE,
{ moons: makeMoons(samples, sunR) }));
var ring0 = cosmos.rings[1];
var traj = [[ring0.p.y, ring0.p.z, ring0.v.y / inc, ring0.v.z / inc]];
var prevVz = 0, wentNeg = false, wentPos = false, T = 0;
for (var i = 0; i < 100000; i++) {
cosmos.multistep();
var vz = ring0.v.z;
traj.push([ring0.p.y, ring0.p.z, ring0.v.y / inc, ring0.v.z / inc]);
if (!wentNeg && vz < 0) wentNeg = true;
if ( wentNeg && !wentPos && vz >= 0) wentPos = true;
if ( wentPos && vz < 0) {
var alpha = prevVz / (prevVz - vz);
T = i + alpha;
break;
}
prevVz = vz;
}
var need = Math.ceil(2 * T) + 2;
while (traj.length <= need) {
cosmos.multistep();
traj.push([ring0.p.y, ring0.p.z, ring0.v.y / inc, ring0.v.z / inc]);
}
var out = [];
for (var k = 0; k < N; k++) {
var w = k / N;
var t1 = k * T / N, i1 = Math.floor(t1), f1 = t1 - i1;
var t2 = T + k * T / N, i2 = Math.floor(t2), f2 = t2 - i2;
var p1 = traj[i1], q1 = traj[i1 + 1] || traj[i1];
var p2 = traj[i2], q2 = traj[i2 + 1] || traj[i2];
out.push({
y: w*(p1[0]+f1*(q1[0]-p1[0])) + (1-w)*(p2[0]+f2*(q2[0]-p2[0])),
z: w*(p1[1]+f1*(q1[1]-p1[1])) + (1-w)*(p2[1]+f2*(q2[1]-p2[1])),
vy: w*(p1[2]+f1*(q1[2]-p1[2])) + (1-w)*(p2[2]+f2*(q2[2]-p2[2])),
vz: w*(p1[3]+f1*(q1[3]-p1[3])) + (1-w)*(p2[3]+f2*(q2[3]-p2[3])),
});
}
return { samples: out, traj: traj, T: T };
}
// Extract choreographic samples from the two z=0 crossings in a single-ring
// trajectory. For N=2, the time-reversal symmetry z(+d)=-z(-d) forces vy=0
// at every z=0 crossing, so only (y, vz) need to be found. The two crossings
// (upward then downward) are separated by T/2 and serve as the starting states
// for ring 0 and ring 1 respectively. Returns null if crossings are not found.
function z0Samples(traj, T) {
var t0 = -1, y1 = 0, vz1 = 0;
for (var i = 1; i < traj.length; i++) {
if (traj[i-1][1] < 0 && traj[i][1] >= 0) {
var alpha = -traj[i-1][1] / (traj[i][1] - traj[i-1][1]);
y1 = traj[i-1][0] + alpha*(traj[i][0] - traj[i-1][0]);
vz1 = traj[i-1][3] + alpha*(traj[i][3] - traj[i-1][3]);
t0 = (i - 1) + alpha;
break;
}
}
if (t0 < 0) return null;
// Find the downward crossing nearest to t0 + T/2.
var tHalf = t0 + T / 2, best = -1, bestDist = T;
for (var j = 1; j < traj.length - 1; j++) {
if (traj[j][1] > 0 && traj[j+1][1] <= 0) {
var dist = Math.abs(j - tHalf);
if (dist < bestDist) { bestDist = dist; best = j; }
}
}
if (best < 0) return null;
var p = traj[best], q = traj[best + 1];
var beta = p[1] / (p[1] - q[1]);
return [
{ y: y1, z: 0, vy: 0, vz: vz1 },
{ y: p[0] + beta*(q[0] - p[0]), z: 0, vy: 0,
vz: p[3] + beta*(q[3] - p[3]) }
];
}
function halfOrbitDy(sunR) {
var res = sampleOrbit(sunR);
var samples = res.samples;
var cosmos = new Cosmos2D(Object.assign({}, BASE,
{ moons: makeMoons(samples, sunR) }));
var ring0 = cosmos.rings[1];
var y0 = ring0.p.y, wentNeg = false, prevVz = 0;
for (var i = 0; i < 50000; i++) {
cosmos.multistep();
var vz = ring0.v.z;
if (!wentNeg && vz < -1e-12) wentNeg = true;
if ( wentNeg && vz >= 0) {
var alpha = -prevVz / (vz - prevVz);
return ring0.p.y - (1 - alpha) * ring0.v.y - y0;
}
prevVz = vz;
}
return ring0.p.y - y0;
}
function findSunR(maxSun, nScan, eps) {
var step = maxSun / nScan;
var prev = { s: 0, dy: halfOrbitDy(0) };
var sunR = 0;
for (var i = 1; i <= nScan; i++) {
var s = i * step;
var dy = halfOrbitDy(s);
if (prev.dy * dy <= 0) {
var lo = prev.s, dlo = prev.dy, hi = s;
for (var j = 0; j < 80; j++) {
var mid = (lo + hi) * 0.5;
var dm = halfOrbitDy(mid);
if (dlo * dm <= 0) { hi = mid; }
else { lo = mid; dlo = dm; }
if (hi - lo < eps) break;
}
sunR = (lo + hi) * 0.5; break;
}
prev = { s: s, dy: dy };
}
return sunR;
}
// ---- Action minimizer ----
// Refine the orbit to a true choreography using Fourier-space gradient descent.
// traj/T come from sampleOrbitFull(). Returns AM samples, or null if diverged to NaN.
//
// Strategy: run the AM with all harmonics free (better convergence), then call
// projectChoreography() to zero the n=k·N harmonics that create nonzero total
// momentum.
//
// Current status: the AM does not yet converge to a good choreographic orbit for
// N=2. The physical 2-ring trajectory already has ~7% even harmonics, which
// means the orbit lacks exact choreographic z-symmetry at these parameters.
// The AM stalls at rms≈3e-2 rather than converging to near-zero. The download
// therefore uses whatever the AM produces (both rings on the same Fourier orbit)
// but the dynamics will look wrong — this is a known open problem.
function runActionMinimizer(traj, T) {
var lg = document.getElementById('log');
var am = new ActionMinimizer({
N: N, rm: rm, l: l,
fixed: [{ y: sunR, z: 0, mass: 1 - N*rm }],
});
am.trajToParams(traj, T, inc);
lg.textContent += 'AM initial: T_phys=' + am.params.T_phys.toFixed(4) +
' rms=' + am.step(0).toExponential(3) + '\n';
am.run({
nSteps: 2000,
alpha: 1.0,
eps: 1e-8,
reportEvery: 500,
onProgress: function(iter, r, Tv) {
lg.textContent += 'AM iter ' + iter + ': rms=' + r.toExponential(3) +
' T=' + Tv.toFixed(6) + '\n';
},
});
// Zero harmonics divisible by N — enforces zero total momentum and COM
// centred on the orbit. See ActionMinimizer.projectChoreography().
am.projectChoreography();
var amSamples = am.getSamples();
for (var k = 0; k < N; k++) {
if (!isFinite(amSamples[k].y) || !isFinite(amSamples[k].z)) {
lg.textContent += 'AM diverged to NaN — download uses physical samples.\n';
return null;
}
}
lg.textContent += 'AM Ring 0: y=' + amSamples[0].y.toFixed(6) + ' z=' + amSamples[0].z.toFixed(6) + '\n';
lg.textContent += 'AM Ring 1: y=' + amSamples[1].y.toFixed(6) + ' z=' + amSamples[1].z.toFixed(6) + '\n';
return amSamples;
}
// ---- Main ----
var lg = document.getElementById('log');
var sunR = findSunR(0.5, 20, 1e-6);
lg.textContent += 'sunR=' + sunR.toFixed(8) + '\n';
var res = sampleOrbit(sunR);
// Seed from the z=0 crossings (upward then downward, separated by T/2).
// By time-reversal symmetry vy=0 at both crossings, so the orbit starts in
// the correct choreographic symmetry class and the iteration stays there.
// Fall back to the t=0/t=T/2 samples if crossings are not found.
var samples = z0Samples(res.traj, res.T) || res.samples;
lg.textContent += 'z=0 seed: Ring 0: y=' + samples[0].y.toFixed(6) +
' Ring 1: y=' + samples[1].y.toFixed(6) + '\n';
// Refine to self-consistency under the full 2-ring system.
// Keep the last full-system run so the AM can start from the 2-ring trajectory
// instead of the single-ring one — the 2-ring orbit is already close to the
// choreography, so the AM needs far smaller corrections.
var fullRes = { traj: res.traj, T: res.T };
for (var iter = 0; iter < 6; iter++) {
var fr = sampleOrbitFull(sunR, samples);
var maxD = 0;
for (var k = 0; k < N; k++)
maxD = Math.max(maxD, Math.abs(fr.samples[k].y - samples[k].y),
Math.abs(fr.samples[k].z - samples[k].z));
samples = fr.samples;
fullRes = fr;
if (maxD < 1e-9) break;
}
lg.textContent += 'Physical samples converged.\n';
lg.textContent += 'Ring 0: y=' + samples[0].y.toFixed(6) + ' z=' + samples[0].z.toFixed(6) +
' vz=' + samples[0].vz.toFixed(6) + '\n';
lg.textContent += 'Ring 1: y=' + samples[1].y.toFixed(6) + ' z=' + samples[1].z.toFixed(6) +
' vz=' + samples[1].vz.toFixed(6) + '\n';
// Run action minimizer starting from the converged 2-ring trajectory.
var amSamples = runActionMinimizer(fullRes.traj, fullRes.T);
var downloadSamples = amSamples || samples;
var downloadNote = amSamples ? '' : ' (action minimizer diverged — using physical samples)';
var ringOpts = Object.assign({}, BASE, {
background: '#000000',
scale: 400,
ymargin: 20,
framerate: 20,
trail: true,
traillen: 2000,
fade: 0.02,
stop: false,
moons: makeMoons(samples, sunR),
});
ring('cv', ringOpts);
// Download dyson_out.html using the action-minimized initial conditions.
var a = document.createElement('a');
a.textContent = 'Download dyson_out.html' + downloadNote;
a.style.cssText = 'color:#00ffff;cursor:pointer;';
a.onclick = function() {
var downloadOpts = Object.assign({}, ringOpts, { moons: makeMoons(downloadSamples, sunR) });
actionDownloadHTML('Dyson 2-Ring', downloadOpts);
};
document.body.appendChild(a);
})();
</script>
</body>
</html>