-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstellar_lifespan_v2.html
398 lines (324 loc) · 14.6 KB
/
stellar_lifespan_v2.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
<!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:200px;
}
.outputs {
display: inline-block;
border: 2px outset black;
background-color: lightblue;
text-align: left;
height:200px;
vertical-align: middle;
}
.visualization {
display: inline-block;
background-color: black;
border: 2px outset black;
height:200px;
color:white;
}
.upper-right {
position:absolute;
top:0;
right:10px;
}
.container {
display:flex;
}
</style>
</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 only? </div> <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>
</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>
// initialize model variables
sunLifespan = 1E10;
// Mass and Luminosity, relative to the sun
vars = [1.0,1.0];
// 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];
// 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);
}
updateDisplay();
drawStarsPlural();
function updateInputs(iVar, iMode) {
//console.log(iVar, iMode);
// adjust value by typing
if (iMode == 0) {
vars[iVar] = document.getElementById(ids[iVar][0]).value;
if (logsliders[iVar] == false) {
document.getElementById(ids[iVar][1]).value = vars[iVar];
}
if (logsliders[iVar] == true) {
document.getElementById(ids[iVar][1]).value = Math.log10(vars[iVar]);
}
}
// adjust values by slider
if (iMode == 1) {
if (logsliders[iVar] == false) {
vars[iVar] = document.getElementById(ids[iVar][1]).value;
}
if (logsliders[iVar] == true) {
vars[iVar] = trimNum(10**document.getElementById(ids[iVar][1]).value,2);
}
}
// if Realistic checked, then enforce M^4 relation
if (realistic) {
if (iVar == 0) {
vars[1] = vars[0]**4.0;
}
if (iVar == 1) {
vars[0] = vars[1]**(0.25);
}
//console.log(vars);
}
updateDisplay();
}
function updateRealistic() {
realistic = document.getElementById("realisticOnly").checked;
//console.log(realistic);
if (realistic) {
updateInputs(0, 0);
}
}
function updateDisplay() {
//console.log('updateDisplay called ');
for (var i = 0; i < vars.length; i++) {
document.getElementById(ids[i][0]).value = vars[i].toLocaleString(undefined, {minimumFractionDigits:0, maximumSignificantDigits: 2});
if (logsliders[i] == false) {
document.getElementById(ids[i][1]).value = vars[i];
}
else {
document.getElementById(ids[i][1]).value = Math.log10(vars[i]);
}
}
// update stellar lifespan and display
stellarLifespan = vars[0] / vars[1] * sunLifespan;
document.getElementById('lifespanDisplay').value = (stellarLifespan).toLocaleString(undefined, {minimumFractionDigits:0, maximumSignificantDigits: 2});
// update star display variables
deltaY = 30 * Math.log10(vars[0]);
starY = [100 + deltaY - starR[0] + starR[1], 100 - deltaY];
// adjust starL
starL[0] =scaleLuminosity(vars[1]);
// adjust starT
// adjust starR
if (realistic == true) {
starR[0] = r0 + 20*Math.log10(vars[0]**(0.75));
starT[0] = 5800 * (vars[1] / vars[0]**1.5);
console.log("realistic properties: ", vars[0], vars[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("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);
// 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(20, 100 + starR[1] - lineSlope * (180));
starContext.lineTo(360, 100 + starR[1] + lineSlope * (180));
starContext.stroke(); // Draw it
for (var i = 0; i < starX.length; i++) {
starC[i] = thermalColor(starT[i], starL[i]);
drawStar(starX[i],starY[i],starR[i],starC[i],starL[i]);
}
}
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(vars[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;
}
</script>
</body></html>