-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstellar_lifespan_v4.html
565 lines (466 loc) · 21 KB
/
stellar_lifespan_v4.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
561
562
563
564
565
<!DOCTYPE html>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Stellar Lifespan Explorer</title>
<style>
.inputs {
display: inline-block;
border: 2px outset black;
background-color: lightblue;
text-align: left;
float:left;
height:220px;
}
.outputs {
display: inline-block;
border: 2px outset black;
background-color: lightblue;
text-align: left;
height:220px;
vertical-align: middle;
}
.visualization {
display: inline-block;
background-color: black;
border: 2px outset black;
height:220px;
color:white;
}
.upper-right {
position:absolute;
top:0;
right:10px;
}
.container {
display:flex;
}
</style>
<!-- P5 for animations -->
<script src="https://github.com/processing/p5.js/releases/download/v1.4.2/p5.min.js">
</script>
</head>
<body style="font-family:sans-serif; font-size:15px; width:700px; margin-left:auto; margin-right:auto;">
<h3 id="pageHeader"><a href="https://foothill.edu/astronomy/astrosims.html" target=_blank>AstroSims</a> > Stellar Lifespan Explorer</h3>
<div class="container">
<div id="stellarParameters" class="inputs">
<p><b>Star properties</b></p>
Mass
<input type="text" id="stellarMassDisplay" size="8" value="1" oninput="updateInputs(0,0)"> <br>
<input id="stellarMassSlider" type ="range" min="-1" max="1.3" step="0.001" value="Error" oninput="updateInputs(0,1)"> <br>
Luminosity
<input type="text" id="stellarLuminosityDisplay" size="8" value="1" oninput="updateInputs(1,0)"> <br>
<input id="stellarLuminositySlider" type ="range" min="-4" max="5.2" step="0.001" value="Error" oninput="updateInputs(1,1)"> <br>
<div id="realisticLabel"> <input type="checkbox" id="realisticOnly" onclick="updateRealistic()"> Realistic stars </div> <br>
<b>Main Sequence lifespan</b>
<input type="text" id="lifespanDisplay" size="20" value="Error" readonly="true"> years <br>
</div>
<!-- <div id="lifeDisplay" class="outputs">
<p><b>Main Sequence lifespan</b></p>
<input type="text" id="lifespanDisplay" size="20" value="Error" readonly="true"> years <br>
<p><b>Time elapsed</b></p>
<input type="text" id="elapsedDisplay" size="20" value="Error" readonly="true"> years <br>
</div>
-->
<div id="starAnimation" class="visualization">
<canvas id="starDisplay" height="200" width="400">
</canvas>
</div>
<!-- <div id="infoLink" class="upper-right" onclick="popupInfo()">
<h3 onmouseclick="popupInfo()">ⓘ</h3>
</div>
-->
</div>
<script>
class Star {
// setting the co-ordinates, radius and the
// speed of a particle in both the co-ordinates axes.
constructor(x, y, m, L, r, T){
this.x = x;
this.y = y;
this.m = m;
this.L = L;
this.r = r;
this.T = T;
this.c = thermalColor(T, L);
this.fuel = m + this.L / (frm * sunPlayTime);
this.gauge = this.fuel / this.m;
//console.log('new star: ', this.x, this.y, this.m, this.L, this.r, this.c, this.fuel, this.gauge);
}
// creation of a particle.
createStar() {
//console.log('this star: ', this.x, this.y, this.m, this.L, this.r, this.c, this.fuel);
noStroke();
//fill(this.c);
//circle(this.x,this.y,this.r);
drawStar(this.x, this.y, this.r, this.c, this.L);
}
updateFuel() {
if (this.fuel > 0) {
this.fuel -= this.L / (frm * sunPlayTime);
}
if (this.fuel < 0) {
this.fuel = 0;
}
}
}
function changePlayState() {
if ((starG[0] == 0) && (starG[1] == 0)) {
resetPlayState();
}
if (playState == true) {
playState = false;
noLoop();
} else {
playState = true;
loop();
}
//console.log('Playstate: ', playState);
}
function resetPlayState() {
playState = false;
noLoop();
starG = [1,1];
timeElapsed = 0;
updateDisplay();
}
// initialize model variables
sunLifespan = 1E10;
// Mass and Luminosity, relative to the sun
inputs = [1.0,1.0];
frm = 30;
sunPlayTime = 10;
deltaTime = sunLifespan / (frm * sunPlayTime);
timeElapsed = 0;
let playState = false;
let stars = [];
// initialize interface variables
// names of the interface elements for each variable
ids = [['stellarMassDisplay','stellarMassSlider'],['stellarLuminosityDisplay','stellarLuminositySlider']];
// indicate if sliders are on a log scale
logsliders = [true, true];
realistic = false;
let starCanvas = document.getElementById("starDisplay");
let starContext = starCanvas.getContext("2d");
// read parameters from URL
var urlParams = (new URL(document.location)).searchParams;
//console.log("Params are: "+urlParams);
if (urlParams.has('mass') == true) {
document.getElementById(ids[0][0]).value = urlParams.get('mass');
updateInputs(0,0);
}
if (urlParams.has('luminosity') == true) {
document.getElementById(ids[1][0]).value = urlParams.get('luminosity');
updateInputs(1,0);
}
if (urlParams.has('realistic') == true) {
document.getElementById("realisticOnly").checked = true;
updateRealistic();
}
if (urlParams.has('fixedMass') == true) {
document.getElementById(ids[0][0]).disabled = true;
document.getElementById(ids[0][1]).disabled = true;
document.getElementById("realisticOnly").disabled = true;
document.getElementById("realisticLabel").style.color = "#aaaaaa";
}
if (urlParams.has('fixedLuminosity') == true) {
document.getElementById(ids[1][0]).disabled = true;
document.getElementById(ids[1][1]).disabled = true;
document.getElementById("realisticOnly").disabled = true;
document.getElementById("realisticLabel").style.color = "#aaaaaa";
}
if (urlParams.has('hideHeader') == true) {
//console.log("Fixed luminosity");
document.getElementById("pageHeader").style.display = "none";
}
// initialize drawing variables
var minDispl = 0.4;
var maxDispl = 1;
var r0 = 20;
var L0 = (minDispl + maxDispl) / 2;
var starR = [1.0 * r0, 1.0 * r0];
var starT = [5800,5800];
var starX = [100,300];
var starY = [100, 100];
var starC = ["red", "green"];
var starL = [1, L0];
var starG = [1, 1];
// Initialize wavelength and brightness array
var wavelengths = [], waveMin = 400, waveMax = 700, brightness = [];
for (i=waveMin; i < waveMax+1; i++) {
wavelengths.push(i);
brightness.push(1);
}
function setup() {
noLoop();
frameRate(frm);
boxleft = document.getElementById("starDisplay");
x = boxleft.getBoundingClientRect().left;
y = boxleft.getBoundingClientRect().top;
//cnv = createCanvas(400, 200);
//cnv.position(x, y);
//stars.push(new Star(100, 100, 1, 1, 30, 'rgba(255,255,20,0.5)'));
startStop = createButton('Play / Pause');
startStop.position(x+120,y+190);
startStop.mousePressed(changePlayState);
resetPlay = createButton('Reset');
resetPlay.position(x+230,y+190);
resetPlay.mousePressed(resetPlayState);
infoButton = createButton('Info');
infoButton.position(x+350,y+190);
infoButton.mousePressed(popupInfo);
noLoop();
}
updateDisplay();
drawStarsPlural();
function updateInputs(iVar, iMode) {
//console.log(iVar, iMode);
// adjust value by typing
if (iMode == 0) {
inputs[iVar] = document.getElementById(ids[iVar][0]).value;
if (logsliders[iVar] == false) {
document.getElementById(ids[iVar][1]).value = inputs[iVar];
}
if (logsliders[iVar] == true) {
document.getElementById(ids[iVar][1]).value = Math.log10(inputs[iVar]);
}
}
// adjust values by slider
if (iMode == 1) {
if (logsliders[iVar] == false) {
inputs[iVar] = document.getElementById(ids[iVar][1]).value;
}
if (logsliders[iVar] == true) {
inputs[iVar] = trimNum(10**document.getElementById(ids[iVar][1]).value,2);
}
}
// if Realistic checked, then enforce M^4 relation
if (realistic) {
if (iVar == 0) {
inputs[1] = inputs[0]**4.0;
}
if (iVar == 1) {
inputs[0] = inputs[1]**(0.25);
}
//console.log(inputs);
}
resetPlayState()
}
function updateRealistic() {
realistic = document.getElementById("realisticOnly").checked;
//console.log(realistic);
if (realistic) {
updateInputs(0, 0);
}
resetPlayState();
}
function updateDisplay() {
//console.log('updateDisplay called ');
for (var i = 0; i < inputs.length; i++) {
document.getElementById(ids[i][0]).value = inputs[i].toLocaleString(undefined, {minimumFractionDigits:0, maximumSignificantDigits: 2});
if (logsliders[i] == false) {
document.getElementById(ids[i][1]).value = inputs[i];
}
else {
document.getElementById(ids[i][1]).value = Math.log10(inputs[i]);
}
}
// update stellar lifespan and display
stellarLifespan = inputs[0] / inputs[1] * sunLifespan;
document.getElementById('lifespanDisplay').value = (stellarLifespan).toLocaleString(undefined, {minimumFractionDigits:0, maximumSignificantDigits: 2});
//document.getElementById('elapsedDisplay').value = (timeElapsed).toLocaleString(undefined, {minimumFractionDigits:0, maximumSignificantDigits: 3});
// update star display variables
deltaY = 30 * Math.log10(inputs[0]);
starY = [100 + deltaY - starR[0] + starR[1], 100 - deltaY];
// adjust starL
starL[0] =scaleLuminosity(inputs[1]);
// adjust starT
// adjust starR
if (realistic == true) {
starR[0] = r0 + 20*Math.log10(inputs[0]**(0.75));
starT[0] = 5800 * (inputs[1] / inputs[0]**1.5);
//console.log("realistic properties: ", inputs[0], inputs[1], starR[0], starT[0]);
}
else {
starR[0] = r0;
starT[0] = 5800;
}
drawStarsPlural();
}
function trimNum(input, toPrec) {
var numPlaces = Math.floor(Math.log10(input));
var divideBy = 10**(numPlaces - toPrec);
return divideBy * Math.floor(input / divideBy);
}
function popupInfo() {
var infoOption;
if (confirm("This tool calculates and visualizes the approximate main-sequence lifespan of a star based on its mass and its luminosity. Adjust these two properties using the sliders on the left. \n \n Use the Play / Pause button to act out the lifespan of this model star in comparison to the sun. The Reset button will restart this animation without changing the mass and luminosity selected. Use the \"Realistic stars\" checkbox on the left to examine realistic stars, whose main-sequence luminosity is determined by their mass. \n \n The Stellar Lifespan calculator is part of the Foothill College AstroSims project. Click OK to open the AstroSims homepage in a new tab to see our other simulations, as well as our listing of more than 300 other Astronomy education simulations and visualizations from authors around the world. \n \n Open https://foothill.edu/astronomy/astrosims.html in a new tab?")) {
window.open("https://foothill.edu/astronomy/astrosims.html","_blank");
}
}
function drawStarsPlural() {
starContext.clearRect(0,0,starCanvas.width,starCanvas.height);
// draw labels for model star and sun
starContext.fillStyle = "white";
starContext.font = "20px Georgia";
starContext.fillText("Model star", 60, 15);
starContext.fillText("The sun", 260, 15);
starContext.fillText('Fuel: ' + (100*starG[0]).toFixed(0).toString(10) + '%', 60, 32);
starContext.fillText('Fuel: ' + (100*starG[1]).toFixed(0).toString(10) + '%', 260, 32);
//starContext.fillText("Fuel: ", 260, 32);
starContext.fillText((timeElapsed).toLocaleString(undefined, {minimumFractionDigits:0, maximumSignificantDigits: 3}) + ' years', 120, 180);
// draw rectangle with slope matching
starContext.beginPath();
starContext.lineWidth = 5;
starContext.strokeStyle = "green"; // Green path
var lineSlope = (starY[0] + starR[0] - starY[1] - starR[1]) / (starX[0] - starX[1]);
starContext.moveTo(30, 100 + starR[1] - lineSlope * (170));
starContext.lineTo(350, 100 + starR[1] + lineSlope * (170));
starContext.stroke(); // Draw it
for (var i = 0; i < starX.length; i++) {
starC[i] = thermalColor(starT[i], starL[i]);
if (starG[i] == 0) {
starC[i] = "rgb(50,50,50)";
}
drawStar(starX[i],starY[i],starR[i],starC[i],starL[i]);
}
// draw fuel gauges for model star and sun
//starContext.fillStyle = "white";
//starContext.font = "16px Georgia";
//starContext.fillText("F", 5, 30);
//starContext.fillText("E", 5, 180);
//starContext.fillText("F", 385, 30);
//starContext.fillText("E", 385, 180);
//starContext.beginPath();
//starContext.lineWidth = 5;
//starContext.strokeStyle = "red"; // red path
//starContext.moveTo(5, 165 - 130 * starG[0]);
//starContext.lineTo(15, 165 - 130 * starG[0]);
//starContext.stroke(); // Draw it
//starContext.beginPath();
//starContext.lineWidth = 5;
//starContext.strokeStyle = "red"; // red path
//starContext.moveTo(385, 165 - 130 * starG[1]);
//starContext.lineTo(395, 165 - 130 * starG[1]);
//starContext.stroke(); // Draw it
}
function drawStar(x,y,r,c, L) {
//console.log('star properties: ', x, y, r, c, L);
var grd = starContext.createRadialGradient(x, y, r*Math.sqrt(L)*0.9, x, y, r*L*4);
grd.addColorStop(0, c);
grd.addColorStop(1, "black");
starContext.beginPath();
starContext.arc(x, y, r, 0, 2 * Math.PI, false);
starContext.fillStyle = grd;
starContext.fill();
}
function thermalColor(temperature, intensity = 1) {
//console.log('determining star color...');
//console.log("Temperature: ", temperature);
// console.log("wavelengths: ", wavelengths);
var brightness = PlanckArray(temperature, wavelengths);
// console.log("brightness: ", brightness);
var maxBrightness = Math.max(...brightness); // ... is the spread operator
var rSum = 0;
var gSum = 0;
var bSum = 0;
var brightSum = 0;
for (var i = 0; i < wavelengths.length; i++) {
var newRGB = getLineColor(wavelengths[i], brightness[i]/maxBrightness);
rSum += newRGB[0];
gSum += newRGB[1];
bSum += newRGB[2];
brightSum += brightness[i]/maxBrightness;
}
var brightest = Math.max(rSum, Math.max(gSum, bSum));
var r = rSum / brightest * 255 * intensity ;
var g = gSum / brightest * 255 * intensity ;
var b = bSum / brightest * 255 * intensity ;
if (maxBrightness == 0) {
r = 0;
g = 0;
b = 0;
}
//console.log("rgb("+r+","+g+","+b+")");
return "rgb("+r+","+g+","+b+")";
}
function getLineColor(lambda, relativeIntensity=1) {
// original routine from Andrew Duffy, modified
// outputs a value from 0 to 255
var redness = 0;
if ((lambda >= 400) && (lambda <= 500)) redness = Math.floor(160-160*(lambda-400)/100);
if ((lambda >= 558) && (lambda < 590)) redness = Math.floor(255-255*(590-lambda)*(590-lambda)/(32*32));
if ((lambda >= 590) && (lambda < 650)) redness = 255;
if ((lambda >= 650) && (lambda <= 700)) redness = Math.floor(255-2*(lambda-650));
redness = Math.floor(relativeIntensity * redness);
var greenness = 0;
if ((lambda > 460) && (lambda < 500)) greenness = Math.floor(255 - 255*(500 - lambda)*(500-lambda)/1600);
if ((lambda >= 500) && (lambda <= 570)) greenness = 255;
if ((lambda > 570) && (lambda <= 640)) greenness = Math.floor(255-255*(lambda-570)*(lambda-570)/(70*70));
greenness = Math.floor(relativeIntensity * greenness);
var blueness = 0;
if ((lambda >= 400) && (lambda < 460)) blueness = 255;
if ((lambda >= 460) && (lambda < 550)) blueness = Math.floor(255 - 255*(lambda-460)*(lambda-460)/(90*90));
blueness = Math.floor(relativeIntensity * blueness);
return [redness, greenness, blueness];
}
function PlanckFunction(temperature, wavelength) {
// CALCULATE PLANCK FUNCTION at a single wavelength
// outputs W / steradian / m2 / m
// divide by 1e12 to get kW / sr / m2 / nm
var h = 6.626e-34; // m2 kg / s
var c = 2.997e8; // m/s, speed of light
var kb = 1.38e-23; // m2kg /s2 / K Boltzmann constant
return ((2 * h * c*c) /
Math.pow(wavelength * 1e-9,5) /
(Math.exp( h*c / (wavelength * 1e-9 * kb * temperature)) - 1));
}
function PlanckArray(T, wavelengths) {
var B = [];
for (i=0; i < wavelengths.length; i++) {
B.push(PlanckFunction(T, wavelengths[i]));
}
return B;
}
function scaleLuminosity(luminosity) {
minL = -4.0;
maxL = 5.2;
sunL = L0;
logL = Math.log10(inputs[1]);
if (logL >= 0) {
var L = L0 + (1 - sunL) * logL / maxL;
}
else {
//console.log('low luminosity...');
//console.log('logL, minL, and quotient ', logL, minL, (logL / minL), (sunL - minDispl));
var L = L0 - (sunL - minDispl) * (logL / minL) ;
}
//console.log('scaled display L: ', L);
return L;
}
function updateFuel() {
deltaTime = sunLifespan / (frm * sunPlayTime);
timeElapsed += deltaTime;
//console.log('delta Time: ', deltaTime);
//console.log('star fuel use: ', inputs[1] / inputs[0] / (frm * sunPlayTime));
starG[0] = starG[0] - inputs[1] / inputs[0] / (frm * sunPlayTime);
starG[1] = starG[1] - (1 / 1) / (frm * sunPlayTime);
//starG[1] = starG[1] - (frm * sunPlayTime);
if (starG[0] < 0) {
starG[0] = 0;
}
if (starG[1] < 0) {
starG[1] = 0;
}
if ((starG[0] == 0) && (starG[1] == 0)){
playState = false;
noLoop();
updateDisplay();
}
//console.log('time elapsed: ', timeElapsed);
//console.log('fuel gauges: ', starG);
}
function draw() {
updateDisplay();
updateFuel();
}
</script>
</body></html>