-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphantom_lcfs.html
executable file
·560 lines (509 loc) · 19 KB
/
phantom_lcfs.html
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
<html>
<head>
<title>w7xplorer</title>
<style>
body {
margin: 0;
}
canvas {
width: 100%;
height: 100%;
}
h4, p {
color: white;
text-align: left;
}
</style>
<link rel="stylesheet" href="datGUIlightTheme.css">
<script src="js/three.min.js"></script>
<script src="js/STLLoader.js"></script>
<script src="js/OrbitControls.js"></script>
<script src="js/dat.gui.min.js"></script>
<script src="js/THREE.TextSprite.js"></script>
<script src="js/THREE.TextTexture.min.js"></script>
<script src="js/jquery.min.js"></script>
</head>
<body>
<script>
// Initialize scene and renderer
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.001, 1e5);
camera.up = new THREE.Vector3(0, 0, 1);
camera.position.setX(-4.9499).setY(3.96425).setZ(-0.6451);
camera.setFocalLength(11.057);
var renderer = new THREE.WebGLRenderer(
{
antialias: true,
alpha: true
});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Browser camera controls
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 1;
controls.enableZoom = true;
controls.maxDistance = 20;
controls.minDistance = 0.1;
// Basic lights
var ambientLight = new THREE.AmbientLight(0xffffff);
scene.add(ambientLight);
var pointLight = new THREE.PointLight(0xffffff);
pointLight.position.set(0, 0, 100);
pointLight.shadowMapVisible = true;
scene.add(pointLight);
camera.lookAt(new THREE.Vector3(1.5382, 6.0620, 0.6750));
var LCFSDict = {};
var LCFSr, LCFSz, LCFSphi;
var key;
var currentConfig = 'A_standard_beta-0';
fetch('assets/lcfs/A_standard_beta-0.json').then(response => response.json()).then(json => LCFSDict[currentConfig] = plotLCFS(json));
var closestLCFSpoint;
var closestLCFSpointSphere;
var Bdirection;
var Bline;
var chord;
var chordLength;
var angleWithBdeg;
var configDict = {
'A_standard_beta-0': '1000_1000_1000_1000_+0000_+0000/01/00jh_l/',
'A_standard_beta-0.056': '1000_1000_1000_1000_+0000_+0000/01/32/',
'B-low-iota_beta-0': '1000_1000_1000_1000_+0750_+0750/01/00/',
'B_low-iota_beta-0.021': '1000_1000_1000_1000_+0750_+0750/01/10ss/',
'C_high-iota_beta-0': '1000_1000_1000_1000_-0690_-0690/01/00/',
'C_high-iota_beta-0.021': '1000_1000_1000_1000_-0690_-0690/01/10s/',
'D_low-mirror_beta-0': '1042_1042_1127_1127_+0000_+0000/01/00/',
'D_low-mirror_beta-0.043': '1042_1042_1127_1127_+0000_+0000/01/20/',
'E_high-mirror_beta-0': '0972_0926_0880_0852_+0000_+0000/01/00jh/',
'E_high-mirror_beta-0.053': '0972_0926_0880_0852_+0000_+0000/01/24a/'
};
var render = function()
{
requestAnimationFrame(render);
renderer.render(scene, camera);
};
render();
function saveDatGUItoServer()
{
console.log('Running method saveDatGUItoServer. This may take a while.');
$.ajax(
{
url: "cgi-bin/save.py",
type: "post",
datatype: "plain",
data:
{
text: JSON.stringify(gui.getSaveObject(), undefined, 2)
},
success: function(response)
{
console.log(response);
}
});
}
function getAngleWithBField(recursing = false)
{
console.log('Running method getAngleWithBField. Make sure you\'re connected to the IPP VPN!');
if (!closestLCFSpointSphere)
{
findClosestLCFSpoint();
plotClosestLCFSpointSphere();
}
else if (recursing)
{
var phi = Math.atan(closestLCFSpoint[1] / closestLCFSpoint[0]);
closestLCFSpoint[0] = closestLCFSpoint[0] - 0.001 * Math.cos(phi);
closestLCFSpoint[1] = closestLCFSpoint[1] - 0.001 * Math.sin(phi);
}
else
findClosestLCFSpoint();
if (Bline)
Bline.visible = false;
closestLCFSpointSphere.position.set(closestLCFSpoint[0], closestLCFSpoint[1], closestLCFSpoint[2]);
fetch('http://svvmec1.ipp-hgw.mpg.de:8080/vmecrest/v1/geiger/w7x/' + configDict[currentConfig] +
'magneticfield.json?x=' + closestLCFSpoint[0] + '&y=' + closestLCFSpoint[1] + '&z=' +
closestLCFSpoint[2])
.then(response => response.json())
.then(json => handleBfieldFetch(json));
}
function handleBfieldFetch(json)
{
if (json.magneticField.Bx[0] == null)
{
console.log("Moving closestLCFSpoint 1 mm radially inwards...");
getAngleWithBField(true);
return;
}
Bdirection = [json.magneticField.Bx[0], json.magneticField.By[0], json.magneticField.Bz[0]];
vec1 = [endXYZ.x - startXYZ.x, endXYZ.y - startXYZ.y, endXYZ.z - startXYZ.z];
vec2 = [Bdirection[0] - closestLCFSpoint[0], Bdirection[1] - closestLCFSpoint[1], Bdirection[2] - closestLCFSpoint[2]];
angleWithBdeg = angleBetweenVectors(vec1, Bdirection);
plotBfieldMarker();
}
function plotClosestLCFSpointSphere()
{
var geometry = new THREE.SphereGeometry(0.02, 32, 32);
var material = new THREE.MeshPhongMaterial(
{
color: 0x0000ff
});
closestLCFSpointSphere = new THREE.Mesh(geometry, material);
closestLCFSpointSphere.position.set(closestLCFSpoint[0], closestLCFSpoint[1], closestLCFSpoint[2]);
scene.add(closestLCFSpointSphere);
}
function plotBfieldMarker()
{
if (!Bline)
{
var material = new THREE.LineBasicMaterial(
{
color: 0x0000ff,
linewidth: 10
});
var geometry = new THREE.Geometry();
geometry.vertices.push(
new THREE.Vector3(closestLCFSpoint[0], closestLCFSpoint[1], closestLCFSpoint[2]),
new THREE.Vector3(closestLCFSpoint[0] + Bdirection[0] / 10,
closestLCFSpoint[1] + Bdirection[1] / 10,
closestLCFSpoint[2] + Bdirection[2] / 10)
);
Bline = new THREE.Line(geometry, material);
scene.add(Bline);
}
else
{
Bline.visible = true;
Bline.geometry.vertices[0].x = closestLCFSpoint[0];
Bline.geometry.vertices[0].y = closestLCFSpoint[1];
Bline.geometry.vertices[0].z = closestLCFSpoint[2];
Bline.geometry.vertices[1].x = closestLCFSpoint[0] + Bdirection[0] / 10;
Bline.geometry.vertices[1].y = closestLCFSpoint[1] + Bdirection[1] / 10;
Bline.geometry.vertices[1].z = closestLCFSpoint[2] + Bdirection[2] / 10;
Bline.geometry.verticesNeedUpdate = true;
}
}
function angleBetweenVectors(vec1, vec2)
{
var x1 = vec1[0] ? vec1[0] : vec1.x;
var y1 = vec1[1] ? vec1[1] : vec1.y;
var z1 = vec1[2] ? vec1[2] : vec1.z;
var x2 = vec2[0] ? vec2[0] : vec2.x;
var y2 = vec2[1] ? vec2[1] : vec2.y;
var z2 = vec2[2] ? vec2[2] : vec2.z;
var dot = x1 * x2 + y1 * y2 + z1 * z2;
var len1 = Math.sqrt(x1 ** 2 + y1 ** 2 + z1 ** 2);
var len2 = Math.sqrt(x2 ** 2 + y2 ** 2 + z2 ** 2);
return Math.acos(dot / (len1 * len2)) * 180 / Math.PI;
}
function findClosestLCFSpoint()
{
closestLCFSdistance = Number.MAX_SAFE_INTEGER;
points = LCFSDict[currentConfig].points;
for (i in LCFSDict[currentConfig].points)
{
LCFSdistance = Math.sqrt((endXYZ.x - points[i][0]) ** 2 + (endXYZ.y - points[i][1]) ** 2 + (endXYZ.z - points[i][2]) ** 2);
if (LCFSdistance < closestLCFSdistance)
{
closestLCFSdistance = LCFSdistance;
closestLCFSpoint = points[i];
}
}
return closestLCFSpoint;
}
function updateChordLength()
{
this.chordLength = Math.sqrt((endXYZ.x - startXYZ.x) ** 2 + (endXYZ.y - startXYZ.y) ** 2 + (endXYZ.z - startXYZ.z) ** 2) * 1000;
}
function help()
{
alert(`[Left mouse button drag] rotate
[Right mouse button drag] pan (changes center of zoom/rotation, useful for precision operations)
[Scroll] zoom in/out
[Dropdown] select configuration to open (switch to something else and back if it doesn't look right). Star (*) indicates unsaved changes
[Gear icon] make sure checkbox is *not* checked. Copy and save the presented text if you don't trust the server to keep it safe
[Save] save current configuration to the name selected in the dropdown
[New] you're prompted to give a name to create a new configuration
[Revert] revert the current configuration, if modified (*), to its last saved state
[Blue bars] drag left-right for coarse-grained control of parameter. Drag parameter up-down for fine-grained control`)
}
function updateLCFS(json)
{
LCFSDict[currentConfig].visible = false;
LCFSDict[json.name] = plotLCFS(json);
currentConfig = json.name;
}
function makeGUI(json)
{
gui = new dat.GUI(
{
load: json
});
gui.remember(this.parent);
var old = json.remembered[json.preset][0]
this.magneticConfig = 'A_standard_beta-0';
this.startR = old.startR;
this.startZ = old.startZ;
this.startPhi = old.startPhi;
this.endR = old.endR;
this.endZ = old.endZ;
this.endPhi = old.endPhi;
this.saveToServer = saveDatGUItoServer;
this.help = help;
plotChord();
this.startXYZ = chord.geometry.vertices[0];
this.endXYZ = chord.geometry.vertices[1];
this.getAngleWithB = getAngleWithBField;
updateChordLength();
if (!angleWithBdeg)
angleWithBdeg = 0.001;
gui.add(this, "saveToServer");
gui.add(this, "help");
gui.add(this, "magneticConfig", ['A_standard_beta-0',
'A_standard_beta-0.056',
'B_low-iota_beta-0',
'B_low-iota_beta-0.021',
'C_high-iota_beta-0',
'C_high-iota_beta-0.021',
'D_low-mirror_beta-0',
'D_low-mirror_beta-0.043',
'E_high-mirror_beta-0',
'E_high-mirror_beta-0.053'
])
.onChange(function(key)
{
if (key in LCFSDict)
{
for (var keyi in LCFSDict)
LCFSDict[currentConfig].visible = false;
LCFSDict[key].visible = true;
currentConfig = key;
}
else
fetch('assets/lcfs/' + key + '.json').then(response => response.json()).then(json => updateLCFS(json));
});
var controlsFolder = gui.addFolder('Controls');
controlsFolder.add(controls, "zoomSpeed", 0.05, 1.0, 0.05);
controlsFolder.add(controls, "rotateSpeed", 0.05, 1.0, 0.05);
var startFolder = gui.addFolder('Start');
startFolder.add(this, "startR", 4.0, 7.0, 0.001)
.onChange(function(r)
{
startXYZ.x = r * Math.cos(startPhi);
startXYZ.y = r * Math.sin(startPhi);
chord.geometry.verticesNeedUpdate = true;
updateChordLength();
});
startFolder.add(this, "startZ", -2.0, 2.0, 0.001)
.onChange(function(z)
{
startXYZ.z = z;
chord.geometry.verticesNeedUpdate = true;
updateChordLength();
});
startFolder.add(this, "startPhi", 0, 2 * Math.PI, 0.001)
.onChange(function(phi)
{
startXYZ.x = startR * Math.cos(phi);
startXYZ.y = startR * Math.sin(phi);
chord.geometry.verticesNeedUpdate = true;
updateChordLength();
});
var endFolder = gui.addFolder('End');
endFolder.add(this, "endR", 4.0, 7.0, 0.001)
.onChange(function(r)
{
endXYZ.x = r * Math.cos(endPhi);
endXYZ.y = r * Math.sin(endPhi);
chord.geometry.verticesNeedUpdate = true;
updateChordLength();
});
endFolder.add(this, "endZ", -2.0, 2.0, 0.001)
.onChange(function(z)
{
endXYZ.z = z;
chord.geometry.verticesNeedUpdate = true
updateChordLength();
});
endFolder.add(this, "endPhi", 0, 2 * Math.PI, 0.001)
.onChange(function(phi)
{
endXYZ.x = endR * Math.cos(phi);
endXYZ.y = endR * Math.sin(phi);
chord.geometry.verticesNeedUpdate = true;
updateChordLength();
});
var chordFolder = gui.addFolder('Chord')
chordFolder.add(this, "chordLength").listen();
updateChordLength();
chordFolder.add(this, "getAngleWithB");
chordFolder.add(this, "angleWithBdeg").listen();
controlsFolder.open();
startFolder.open();
endFolder.open();
chordFolder.open();
}
function plotChord()
{
var material = new THREE.LineBasicMaterial(
{
color: 0xff0000,
linewidth: 1
});
var geometry = new THREE.Geometry();
geometry.vertices.push(
new THREE.Vector3(startR * Math.cos(startPhi), startR * Math.sin(startPhi), startZ),
new THREE.Vector3(endR * Math.cos(endPhi), endR * Math.sin(endPhi), endZ)
);
chord = new THREE.Line(geometry, material);
scene.add(chord);
}
function plotLCFS(input)
{
var LCFS = new THREE.Object3D();
LCFS.points = [];
var mat = new THREE.LineBasicMaterial(
{
linewidth: 1,
color: 0xfff,
transparent: true,
opacity: 0.3
});
var lcfs_x = Array(80 * 252);
var lcfs_y = Array(80 * 252);
var lcfs_z = input.z;
for (i = 0; i < 252; i++)
{
var geom = new THREE.Geometry();
for (j = 0; j < 80; j++)
{
lcfs_x[i * 80 + j] = input.r[i * 80 + j] * Math.cos(input.phi[i]);
lcfs_y[i * 80 + j] = input.r[i * 80 + j] * Math.sin(input.phi[i]);
geom.vertices.push(new THREE.Vector3(lcfs_x[i * 80 + j], lcfs_y[i * 80 + j], lcfs_z[i * 80 + j]));
LCFS.points.push([lcfs_x[i * 80 + j], lcfs_y[i * 80 + j], lcfs_z[i * 80 + j]]);
}
LCFS.add(new THREE.Line(geom, mat, THREE.LineSegments));
}
for (i = 0; i < 79; i++)
{
var geom = new THREE.Geometry();
for (j = 0; j < 252; j++)
geom.vertices.push(new THREE.Vector3(lcfs_x[i + j * 80], lcfs_y[i + j * 80], lcfs_z[i + j * 80]));
geom.vertices.push(new THREE.Vector3(lcfs_x[i], lcfs_y[i], lcfs_z[i]));
LCFS.add(new THREE.Line(geom, mat, THREE.LineSegments));
}
LCFSr = input.r;
LCFSz = input.z;
LCFSphi = input.phi;
var geometry = new THREE.ParametricGeometry(parametricLCFS, 252, 80);
var material = new THREE.MeshPhongMaterial(
{
color: 0x156289,
emissive: 0x072534,
transparent: true,
opacity: 0.5
});
LCFS.add(new THREE.Mesh(geometry, material));
scene.add(LCFS);
return LCFS;
}
function parametricLCFS(u, v)
{
u = (u * 252) % 252;
v = (v * 80) % 80;
var r = LCFSr[v + u * 80];
var z = LCFSz[v + u * 80];
var phi = LCFSphi[u];
return new THREE.Vector3(r * Math.cos(phi), r * Math.sin(phi), z);
}
function getLookAtVector(controls)
{
var phi = controls.getAzimuthalAngle();
var theta = controls.getPolarAngle();
return new THREE.Vector3(-Math.sin(theta) * Math.sin(phi), -Math.cos(theta), -Math.sin(theta) * Math.cos(phi));
}
function addSTL(filename)
{
var loader = new THREE.STLLoader();
loader.load(filename, function(geometry)
{
var STLMaterial = new THREE.MeshPhongMaterial(
{
color: 0x888888,
specular: 0xfff,
shininess: 50,
side: THREE.DoubleSide
});
var STL = new THREE.Mesh(geometry, STLMaterial);
STL.scale.set(0.001, 0.001, 0.001);
STL.castShadow = true;
STL.receiveShadow = true;
scene.add(STL);
});
}
function addLabel(text, x, y, z, color)
{
let sprite = new THREE.TextSprite(
{
textSize: 0.3,
texture:
{
text: text,
fontFamily: 'Arial, Helvetica, sans-serif',
},
material:
{
color: color || 0x000
},
});
sprite.position.setX(x).setY(y).setZ(z);
scene.add(sprite);
}
function addAxes(length)
{
var axes = new THREE.Object3D();
axes.add(axis(new THREE.Vector3(0, 0, 0), new THREE.Vector3(length, 0, 0), 0x000, false));
axes.add(axis(new THREE.Vector3(0, 0, 0), new THREE.Vector3(-length, 0, 0), 0x000, true));
axes.add(axis(new THREE.Vector3(0, 0, 0), new THREE.Vector3(0, length, 0), 0x000, false));
axes.add(axis(new THREE.Vector3(0, 0, 0), new THREE.Vector3(0, -length, 0), 0x000, true));
axes.add(axis(new THREE.Vector3(0, 0, 0), new THREE.Vector3(0, 0, length), 0x000, false));
axes.add(axis(new THREE.Vector3(0, 0, 0), new THREE.Vector3(0, 0, -length), 0x000, true));
addLabel('x', 1.2 * length, 0, 0);
addLabel('y', 0, 1.2 * length, 0);
addLabel('z', 0, 0, 1.2 * length);
scene.add(axes);
}
function axis(src, dst, colorHex, dashed)
{
var geom = new THREE.Geometry(),
mat;
if (dashed)
{
mat = new THREE.LineDashedMaterial(
{
linewidth: 3,
color: colorHex,
dashSize: 0.1,
gapSize: 0.1,
transparent: true,
opacity: 0.4
});
}
else
{
mat = new THREE.LineBasicMaterial(
{
linewidth: 3,
color: colorHex,
transparent: true,
opacity: 0.4
});
}
geom.vertices.push(src.clone());
geom.vertices.push(dst.clone());
geom.computeLineDistances(); // This one is SUPER important, otherwise dashed lines will appear as simple plain lines
var axis = new THREE.Line(geom, mat, THREE.LineSegments);
return axis;
}
</script>
</body>
</html>