-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
462 lines (366 loc) · 17.8 KB
/
Copy pathindex.html
File metadata and controls
462 lines (366 loc) · 17.8 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
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
<!DOCTYPE html>
<html>
<head>
<title>Rocket Cooling</title>
<link rel="stylesheet" href="main.css" />
<link href="https://fonts.googleapis.com/css2?family=Audiowide&display=swap" rel="stylesheet" />
</head>
<body>
<div class="Title"><h1>Rocket Cooling simulation</h1></div>
<div class="help Button" onclick="openHelp()">?</div>
<div class="Data Button" onclick="openData()">Data</div>
<!-- Menus -->
<div class="container">
<!-- Left menu -->
<div class="side-box left-box">
<!-- Select the file you want to display -->
<div class="Button" id="FileSelector">
<label id="fileInput">Engine shape:</label>
</div>
<!-- Regen cooling option -->
<div class="Button2 Reg switch-row" id="Reg" style="display: none;">
<span class="Button2">Regen cooling</span>
<input type="checkbox" role="switch" id="R">
</div>
<!-- Film cooling option -->
<div class="Button2 Film switch-row" id="Film" style="display: none;">
<span>Film cooling</span>
<input type="checkbox" role="switch" id="F">
</div>
<!-- Film cooling variables -->
<!-- <div class="Variables" style="display: none;"> -->
<div class="Variables" id="Film_V" style="display: none;">
<input type="Location" id="Film_Loc" placeholder="Starting Point">
</div>
<!-- Add Initial Conditions of the Sim-->
<div id="Live-Data" class="Live" style="display: none;">Data of selection:
<div class="live-item">x: <span id="live-x">--</span></div>
<div class="live-item">r: <span id="live-r">--</span></div>
<div class="live-item"><span id="type">D</span>: <span id="live-D">--</span></div>
</div>
<!-- Run simulation here -->
<div class="Button" id="Sim" style="display: none;">
Run simulation
</div>
</div>
<div id="canvas" class="rocket">
<div id="Status" class="Button2" id="Status" style="display: none;">Status:</div>
<canvas id="rocketCanvas"></canvas>
</div>
<!-- Results handling -->
<div class="side-box right-box">
<!-- Choose the output to look at from the simulation -->
<div id="Results" class="Button2" style="display: none;">
<select id="Choose">
</select>
</div>
<!-- Shows the ColorBar based on the results -->
<!-- <div id="ColorBar" style="display: none;"> -->
<div id="ColorBar">
<div id="MAX" class="Button2" style="display: none;">MAX</div>
<canvas id="legendCanvas" width="75%" height="300%"></canvas>
<div id="MIN" class="Button2" style="display: none;">MIN</div>
</div>
<!-- Resets everything -->
<div class="Button" id="Reset">Reset</div>
</div>
</div>
<input type="range" id="engineSlider" style="display: none;">
<input type="range" id="DataSlider" style="display: none;">
<div class="Error" id="Error">
</div>
<script>
window.onbeforeunload = () => {
const isChecked = false;
window.api.setSimType('regen', isChecked);
window.api.setSimType('film', isChecked);
};
let csvLines = []; // global variable to store parsed lines
let csvHeaders = [];
let coords = []; // Add this at the top
let val = []; // Add this at the top
const slider = document.getElementById("engineSlider");
const slider2 = document.getElementById("DataSlider");
const liveDisplay = document.getElementById("Error");
const filmLoc = document.getElementById('Film_Loc');
let Output = '';
slider.addEventListener("input", () => {
const canvas = document.getElementById('rocketCanvas');
const ctx = canvas.getContext('2d');
const location1 = Number(slider.value);
const Film = document.getElementById('F').checked;
if (!Film) {
liveDisplay.innerText = "Film is turned off";
return;
}
// Redraw rocket first
drawRocket(coords, val);
// Setup scale and margin same as in drawRocket
const marginX = 10;
const marginY = 10;
const maxX = Math.max(...coords.map(p => p.x));
const maxR = Math.max(...coords.map(p => p.r));
const scaleX = (canvas.width - 2 * marginX) / maxX;
const scaleY = (canvas.height - 2 * marginY) / (2 * maxR);
const centerY = canvas.height / 2;
// Get physical coordinates
const p1 = coords[location1];
// Location for circle
const x1 = p1.x * scaleX + marginX;
const y1_top = centerY - p1.r * scaleY;
const y1_bottom = centerY + p1.r * scaleY;
// Draw circles at both top and bottom of the wall
drawCircle(ctx, x1, y1_top);
drawCircle(ctx, x1, y1_bottom);
document.getElementById('Film_Loc').placeholder = p1.x.toFixed(3);
liveDisplay.innerText = `Selected Points: x = ${p1.x.toFixed(3)}, with r = ${p1.r.toFixed(3)}`;
});
slider2.addEventListener("input", () => {
const canvas = document.getElementById('rocketCanvas');
const ctx = canvas.getContext('2d');
const live_x = document.getElementById('live-x');
const live_r = document.getElementById('live-r');
const live_D = document.getElementById('live-D');
const location1 = Number(slider2.value);
// drawRocket(coords, val);
// Setup scale and margin same as in drawRocket
const marginX = 10;
const maxX = Math.max(...coords.map(p => p.x));
const maxR = Math.max(...coords.map(p => p.r));
const scaleX = (canvas.width - 2 * marginX) / maxX;
// Get physical coordinates
const p1 = coords[location1];
const p2 = val[location1];
// Location for circle
const x1 = p1.x * scaleX + marginX;
document.getElementById('Film_Loc').placeholder = p1.x.toFixed(3);
live_x.innerText = p1.x.toFixed(3);
live_r.innerText = p1.r.toFixed(3);
if (Output !== ''){
live_D.innerText = p2.toFixed(3);
}
});
function drawCircle(ctx, x, y, radius = 5) {
ctx.beginPath();
ctx.arc(x, y, radius, 0, 2 * Math.PI);
ctx.strokeStyle = 'white';
ctx.lineWidth = 2;
ctx.stroke();
}
filmLoc.addEventListener('input', function () {
const inputX = Number(this.value);
if (isNaN(inputX)) return;
// Find index of closest point in coords array
let closestIndex = 0;
let minDiff = Math.abs(coords[0].x - inputX);
for (let i = 1; i < coords.length; i++) {
const diff = Math.abs(coords[i].x - inputX);
if (diff < minDiff) {
minDiff = diff;
closestIndex = i;
}
}
// Set slider to that index and trigger input event
slider.value = closestIndex;
slider.dispatchEvent(new Event('input'));
});
document.getElementById('fileInput').addEventListener('click', async function (e) {
const label = this;
const filePath = await window.api.openFileDialog();
if (!filePath) return;
window.api.setInputFilePath(filePath);
const content = await window.api.readCSV(filePath);
document.getElementById('Sim').style.display = 'block';
document.getElementById('Reg').style.display = 'block';
document.getElementById('Film').style.display = 'block';
document.getElementById('Status').style.display = 'block';
document.getElementById('Live-Data').style.display = 'block';
slider2.style.display = 'block';
// Parse CSV
const lines = content.trim().split('\n');
coords = lines.slice(1).map(line => {
const [x, r] = line.split(',').map(Number);
return { x, r };
});
drawRocket(coords);
const fileName = filePath.split(/(\\|\/)/g).pop();
label.textContent = fileName;
document.getElementById('Sim').style.display = "block";
document.getElementById('engineSlider').max = coords.length - 1;
document.getElementById('engineSlider').value = 0;
});
document.getElementById('Sim').addEventListener('click', async function () {
const resultDiv = document.getElementById('Status');
resultDiv.textContent = 'Running simulation...';
const outputFile = await window.api.runSimulation();
resultDiv.textContent = 'Simulation Finished';
const content = await window.api.readCSV(outputFile); // assume output is fixed
const colorBar = document.getElementById('ColorBar');
document.getElementById('Results').style.display = 'block';
colorBar.style.display = 'block';
document.getElementById('engineSlider').style.display = 'none';
const lines = content.trim().split('\n');
csvLines = lines;
// Extract headers from the first line
const headers = lines[0].split(',');
csvHeaders = headers;
// Populate the <select> dropdown
const selectElement = document.getElementById('Choose');
selectElement.innerHTML = ''; // Clear existing options
headers.forEach((header, index) => {
if (index > 1) { // Skip x and r columns
const option = document.createElement('option');
option.value = header;
option.textContent = header;
selectElement.appendChild(option);
}
});
document.getElementById('Results').style.display = "block";
});
document.getElementById('Choose').addEventListener('change', function (){
// Add code to draw the rocket
const headers = csvHeaders;
Output = document.getElementById('Choose').value;
const columnIndex = headers.indexOf(Output);
const lines = csvLines;
document.getElementById('ColorBar').style.display = 'block';
document.getElementById('type').innerText = Output.charAt(0);
val = lines.slice(1).map(line => {
const cols = line.split(',').map(Number);
return cols[columnIndex];
});
const minVal = Math.min(...val);
const maxVal = Math.max(...val);
const selected = this.value;
const coords = lines.slice(1).map(line => {
const [x, r] = line.split(',').map(Number);
return { x, r };
});
drawRocket(coords, val);
drawColorBar(minVal,maxVal);
});
document.getElementById('R').addEventListener('change', function () {
const isChecked = this.checked;
window.api.setSimType('regen', isChecked);
});
document.getElementById('Reset').addEventListener('click', async function (){
await window.api.reset();
});
document.getElementById('F').addEventListener('change', function () {
const isChecked = this.checked;
drawRocket(coords, val);
document.getElementById('Film_V').style.display = isChecked ? 'block' : 'none';
document.getElementById('engineSlider').style.display = isChecked ? 'block' : 'none';
window.api.setSimType('film', isChecked);
});
const { ipcRenderer } = require('electron');
function drawRocket(points, values = []) {
const canvas = document.getElementById('rocketCanvas');
const ctx = canvas.getContext('2d');
// Set canvas resolution to match CSS size
canvas.width = canvas.clientWidth;
canvas.height = canvas.clientHeight;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Add margin to avoid clipping (in pixels)
const marginX = 10;
const marginY = 10;
const maxX = Math.max(...points.map(p => p.x));
const maxR = Math.max(...points.map(p => p.r));
const scaleX = (canvas.width - 2 * marginX) / maxX;
const scaleY = (canvas.height - 2 * marginY) / (2 * maxR);
ctx.lineWidth = 5;
// Use a gradient color based on values (e.g., temperature, stress)
function getColor(v, min, max) {
if (max === min) return 'rgb(0,0,255)'; // Default to blue if no range
const t = (v - min) / (max - min); // Normalize between 0 and 1
const r = Math.round(255 * clamp(1.5 - Math.abs(4 * t - 3), 0, 1));
const g = Math.round(255 * clamp(1.5 - Math.abs(4 * t - 2), 0, 1));
const b = Math.round(255 * clamp(1.5 - Math.abs(4 * t - 1), 0, 1));
return `rgb(${r},${g},${b})`;
function clamp(x, min, max) {
return Math.max(min, Math.min(max, x));
}
}
const minVal = Math.min(...values);
const maxVal = Math.max(...values);
// Draw upper side
for (let i = 0; i < points.length - 1; i++) {
const p1 = points[i];
const p2 = points[i + 1];
const x1 = p1.x * scaleX + marginX;
const y1 = canvas.height / 2 - p1.r * scaleY;
const x2 = p2.x * scaleX + marginX;
const y2 = canvas.height / 2 - p2.r * scaleY;
const color = values.length ? getColor(values[i], minVal, maxVal) : '#0000FF';
ctx.strokeStyle = color;
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
}
// Draw lower side (mirrored)
for (let i = points.length - 1; i > 0; i--) {
const p1 = points[i];
const p2 = points[i - 1];
const x1 = p1.x * scaleX + marginX;
const y1 = canvas.height / 2 + p1.r * scaleY;
const x2 = p2.x * scaleX + marginX;
const y2 = canvas.height / 2 + p2.r * scaleY;
const color = values.length ? getColor(values[i], minVal, maxVal) : '#0000FF';
ctx.strokeStyle = color;
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
}
}
function drawColorBar(minVal, maxVal) {
const canvas = document.getElementById('legendCanvas');
const ctx = canvas.getContext('2d');
const width = canvas.width;
const height = canvas.height;
const gradient = ctx.createLinearGradient(0, 0, 0, height);
// Approximate 'jet' colormap using multiple stops
gradient.addColorStop(0.0, '#800000'); // dark red
gradient.addColorStop(0.15, '#ff0000'); // red
gradient.addColorStop(0.35, '#ffff00'); // yellow
gradient.addColorStop(0.5, '#00ff00'); // green
gradient.addColorStop(0.65, '#00ffff'); // cyan
gradient.addColorStop(0.85, '#0000ff'); // blue
gradient.addColorStop(1.0, '#000080'); // dark blue
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, width, height);
// Draw min and max labels
document.getElementById('MAX').textContent = maxVal.toFixed(2);
document.getElementById('MIN').textContent = minVal.toFixed(2);
document.getElementById('MAX').style.display = 'block';
document.getElementById('MIN').style.display = 'block';
}
function openData() {
window.api.openDataWindow();
}
function openHelp() {
window.api.openHelpWindow();
}
document.getElementById('R').addEventListener('change', function () {
const isChecked = this.checked;
document.getElementById('Reg_V').style.display = isChecked ? 'block' : 'none';
window.api.setSimType('regen', isChecked);
});
window.addEventListener('DOMContentLoaded', () => {
const slider = document.getElementById('engineSlider');
const errorBox = document.getElementById('Error');
errorBox.textContent = "I am called";
slider.addEventListener('input', function () {
const index = parseInt(this.value);
errorBox.textContent = `Slider index: ${index}`;
if (coords.length && val.length) {
drawRocket(coords, val, index);
} else {
errorBox.textContent = 'No data loaded yet.';
}
});
});
</script>
</body>
</html>