-
Notifications
You must be signed in to change notification settings - Fork 9
/
heat-diffusion.js
385 lines (312 loc) · 13.9 KB
/
heat-diffusion.js
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
/*
HeatSolver objects take in the starting temperatures of a piece of meat (or basically water, since the diffusion constants differ negligibly) and the desired discretization of time and space.
Has functionality for computing several iterations of the heat diffusion equation that models the movement of heat through the meat over time.
Options for using either the explicit euler method of calculation (which is relatively unstable and eventually yields values on the order of 10^69 °C for a 1000 second cooking at 180°C) or the Crank-Nicolson scheme (which allows greater timesteps while retaining stability)
*/
function HeatSolver(startingTemps, timestep, spacestep) {
var tempArray = [startingTemps];
var D = .14; // diffusion constant (of water) in units of mm^2/sec
//if the user has not specified space or time discretization values, these are set to 1s and 1mm, respectively
if (timestep === undefined) {
timestep = 1;
}
if (spacestep === undefined) {
spacestep = 1;
}
var timestep = 1;
var spacestep = 1;
var alpha = (D * timestep);
var alphacn = (D * timestep) / (2 * spacestep * spacestep);
//laplacian for the explicit euler
var laplacian = makeLaplacian();
//laplacian for the crank-nicolson
var cnLaplacian = makecnLaplacian();
//inverse of the cnLaplacian
var invcn = numeric.inv(cnLaplacian);
//array that keeps track of which sides are in contact with air -- a value of 1 corresponds to air, as air is "nonconductive" in cooking and does not contribute significantly to heat loss of the steak
var nonconductive = [0, 1];
/*
get_tempArray returns the tempArray, which is the range of values within the steak at each calculated timestep
*/
function get_tempArray() {
return tempArray;
}
/*
makeLaplacian makes a laplacian matrix for the explic with 0s as the first and last rows because the boundary values (the temps of the pan and the air) do not change
looks like:
1 0 0 0 0
1α -2α 1α 0 0
0 1α -2α 1α 0
0 0 1α -2α 1α
0 0 0 0 1
for a five-temperature probe system.
this means that the next temperature of each point is simply determined by the difference between this point and its two neighbors
*/
function makeLaplacian() {
var num_of_measurements = tempArray[0].length;
var newLaplacian = [];
var firstRow = [1];
for (var i = 1; i < num_of_measurements; i++) {
firstRow.push(0)
}
newLaplacian.push(firstRow);
var lastrow = Array.prototype.slice.call(firstRow)
lastrow.reverse();
for (var i = 1; i < num_of_measurements - 1; i++) {
var newRow = [];
for (var j = 1; j < i; j++) {
newRow.push(0);
}
newRow.push(1 * alpha, -2 * alpha, 1 * alpha);
for (var j = i + 2; j < num_of_measurements; j++) {
newRow.push(0);
}
newLaplacian.push(newRow);
}
newLaplacian.push(lastrow);
return newLaplacian;
}
function makecnLaplacian() {
var num_of_measurements = tempArray[0].length;
var newLaplacian = [];
var firstRow = [1];
for (var i = 1; i < num_of_measurements; i++) {
firstRow.push(0)
}
newLaplacian.push(firstRow);
var lastrow = Array.prototype.slice.call(firstRow)
lastrow.reverse();
for (var i = 1; i < num_of_measurements - 1; i++) {
var newRow = [];
for (var j = 1; j < i; j++) {
newRow.push(0);
}
newRow.push(-1 * alphacn, (1 + 2 * alphacn), -1 * alphacn);
for (var j = i + 2; j < num_of_measurements; j++) {
newRow.push(0);
}
newLaplacian.push(newRow);
}
newLaplacian.push(lastrow);
return newLaplacian;
}
/*
uses the explicit form of Euler's method to calculate the next time step's temperatures based on the most current temperatures
*/
function calculate_next_explicit() {
var currentTemps = tempArray[tempArray.length - 1];
currentTemps[currentTemps.length - 1] = currentTemps[currentTemps.length - 2]
var dudt = numeric.dotMV(laplacian, currentTemps);
dudt[0] = 0;
dudt[dudt.length - 1] = 0;
// dudt = numeric.neg(dudt);
var nextTemps = numeric.add(dudt, currentTemps);
tempArray.push(nextTemps)
}
/*
takes in a Crank-Nicolson vector associated with the data and uses the Crank-Nicolson scheme to calculate the next time step's temperatures based on the most recent temperatures
*/
function calculate_next_cn(cnVector) {
var nextTemps = numeric.dotMV(invcn, cnVector);
tempArray.push(nextTemps);
}
/*
takes in a Crank-Nicolson vector associated with the data and uses the Crank-Nicolson scheme to calculate the temperatures of the next n timesteps based on the most recent temperatures
*/
function calculate_next_n_cn(n) {
for (var i = 0; i < n; i++) {
var cnVector = make_crank_nicolson_vector();
calculate_next_cn(cnVector);
}
}
/*
uses the explicit form of Euler's method to calculate the temperatures of the next n timesteps based on the most current temperatures
*/
function calculate_next_n_exp(n) {
for (var i = 0; i < n; i++) {
calculate_next_explicit();
}
}
/*
takes in an array of length 3 arrays that describe which side(s) is being heated, at what times, and at what temperature. Calculates the progression of the temperatures for the entire cooking period and then returns the data cut into 60 slices of temperatures that are equally spaced in time.
*/
function sixty_graph_arrays(time_top_bottom) {
var grapharray = [];
var graphlabels = [];
var temperatures = [];
var count = 0;
var maxTemps = tempArray[0];
var lastTime = time_top_bottom[time_top_bottom.length - 1][0];
for (var j = 0; j < time_top_bottom.length; j++) {
var thisTime = time_top_bottom[j][0];
var nextTime;
if (j == time_top_bottom.length - 1) {
nextTime = lastTime;
} else {
nextTime = time_top_bottom[j + 1][0];
}
//set the conductivity of air to zero
if (time_top_bottom[j][1] > 15 && time_top_bottom[j][1] < 30) {
nonconductive[1] = 1;
} else {
nonconductive[1] = 0;
}
if (time_top_bottom[j][2] > 15 && time_top_bottom[j][2] < 30) {
nonconductive[0] = 1;
} else {
nonconductive[0] = 0;
}
change_temp(time_top_bottom[j][1], time_top_bottom[j][2])
for (var i = thisTime; i < nextTime; i += timestep) {
if (i == thisTime) {
temperatures.push([time_top_bottom[j][1], time_top_bottom[j][2]])
} else {
temperatures.push(temperatures[temperatures.length - 1]);
}
var cnVector = make_crank_nicolson_vector();
calculate_next_cn(cnVector);
for (var n = 0; n < maxTemps.length; n++) {
if (tempArray[tempArray.length - 1][n] > maxTemps[n]) {
maxTemps[n] = tempArray[tempArray.length - 1][n];
}
}
}
}
var arrays = tempArray.length - 1;
var len = tempArray[0].length;
var step = arrays / 60.0;
for (var i = 0; i < 60 * step; i += step) {
grapharray.push(tempArray[parseInt(i)].slice(1, len - 1));
if (temperatures[parseInt(i)][0] > 25 && temperatures[parseInt(i)][1] > 25) {
graphlabels.push([count, 0, temperatures[parseInt(i)][0]]);
graphlabels.push([count, 1, temperatures[parseInt(i)][1]]);
} else if (temperatures[parseInt(i)][0] > 25) {
graphlabels.push([count, 0, temperatures[parseInt(i)][0]]);
} else if (temperatures[parseInt(i)][1] > 25) {
graphlabels.push([count, 1, temperatures[parseInt(i)][1]]);
} else {
// graphlabels.push([count,0,temperatures[parseInt(i/60)][1]]);
}
count++;
}
return {
temps: grapharray,
points: graphlabels,
step: step,
maxTemps: maxTemps
}
}
/*
takes in an array of length 3 arrays that describe which side(s) is being heated, for how long, and at what temperature. Calculates the progression of the temperatures for the entire cooking period and then returns the data cut into 60 slices of temperatures that are equally spaced in time.
*/
function sixty_graph_arrays_duration(time_top_bottom) {
var grapharray = [];
var graphlabels = [];
var temperatures = [];
var count = 0;
var maxTemps = [];
var firstMaxTemp = [];
for (var i = tempArray[0].length - 1; i > -1; i--) {
firstMaxTemp.push(tempArray[0][i])
}
maxTemps.push(firstMaxTemp);
for (var j = 0; j < time_top_bottom.length; j++) {
//set the conductivity of air to zero
if (time_top_bottom[j][1] > 15 && time_top_bottom[j][1] < 30) {
nonconductive[1] = 1;
} else {
nonconductive[1] = 0;
}
if (time_top_bottom[j][2] > 15 && time_top_bottom[j][2] < 30) {
nonconductive[0] = 1;
} else {
nonconductive[0] = 0;
}
change_temp(time_top_bottom[j][1], time_top_bottom[j][2])
for (var i = 0; i < time_top_bottom[j][0]; i += timestep) {
if (i == 0) {
temperatures.push([time_top_bottom[j][1], time_top_bottom[j][2]])
} else {
temperatures.push(temperatures[temperatures.length - 1]);
}
var cnVector = make_crank_nicolson_vector();
calculate_next_cn(cnVector);
}
}
var arrays = tempArray.length - 1;
var len = tempArray[0].length;
var step = arrays / 60.0;
for (var i = 0; i < 60 * step; i += step) {
var newMaxTemp = [];
for (var n = 0; n < maxTemps[maxTemps.length - 1].length; n++) {
if (tempArray[parseInt(i)][n] > maxTemps[maxTemps.length - 1][n]) {
newMaxTemp.push(tempArray[parseInt(i)][n]);
} else {
newMaxTemp.push(maxTemps[maxTemps.length - 1][n])
}
}
maxTemps.push(newMaxTemp);
grapharray.push(tempArray[parseInt(i)].slice(1, len - 1));
if (temperatures[parseInt(i)][0] > 25 && temperatures[parseInt(i)][1] > 25) {
graphlabels.push([count, 0, temperatures[parseInt(i)][0]]);
graphlabels.push([count, 1, temperatures[parseInt(i)][1]]);
} else if (temperatures[parseInt(i)][0] > 25) {
graphlabels.push([count, 0, temperatures[parseInt(i)][0]]);
} else if (temperatures[parseInt(i)][1] > 25) {
graphlabels.push([count, 1, temperatures[parseInt(i)][1]]);
} else {
// graphlabels.push([count,0,temperatures[parseInt(i/60)][1]]);
}
count++;
}
return {
temps: grapharray,
points: graphlabels,
step: step,
maxTemps: maxTemps[maxTemps.length - 1],
allMaxTemps: maxTemps
};
}
/*
takes in new values for side 1 and side 2 to account for changing cooking temperatures
*/
function change_temp(toptemp, bottomtemp) {
tempArray[tempArray.length - 1][0] = bottomtemp;
tempArray[tempArray.length - 1][tempArray[0].length - 1] = toptemp;
}
/*
creates the crank-nicolson vector that is used to calculate the next range of temperatures inside the meat
*/
function make_crank_nicolson_vector() {
var currentTemps = tempArray[tempArray.length - 1];
var cnVector = [];
for (var i = 0; i < currentTemps.length; i++) {
if (i == 0 && nonconductive[0] == 0) {
cnVector.push(currentTemps[0]); //((1+2*alpha)*currentTemps[i]-alpha*(currentTemps[i+1]));
} else if (i == 0 && nonconductive[0] == 1) {
cnVector.push(alphacn * currentTemps[i] + (1 - 2 * alphacn) * currentTemps[i + 1] + alphacn * (currentTemps[i + 2]));
//((1+2*alpha)*currentTemps[i]-alpha*(currentTemps[i+1]));
} else if (i == currentTemps.length - 1 && nonconductive[1] == 1) {
cnVector.push(cnVector[i - 1]); //(-alpha*currentTemps[i-1]+(1+2*alpha)*currentTemps[i]);
} else if (i == currentTemps.length - 1 && nonconductive[1] == 0) {
cnVector.push(currentTemps[i]);
} else {
cnVector.push(alphacn * currentTemps[i - 1] + (1 - 2 * alphacn) * currentTemps[i] + alphacn * (currentTemps[i + 1]));
}
}
return cnVector;
}
return {
get_tempArray: get_tempArray,
make_crank_nicolson_vector: make_crank_nicolson_vector,
makecnLaplacian: makecnLaplacian,
makeLaplacian: makeLaplacian,
change_temp: change_temp,
calculate_next_cn: calculate_next_cn,
calculate_next_explicit: calculate_next_explicit,
calculate_next_n_cn: calculate_next_n_cn,
sixty_graph_arrays: sixty_graph_arrays,
sixty_graph_arrays_duration: sixty_graph_arrays_duration,
calculate_next_n_exp: calculate_next_n_exp
}
}