-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobject1d.hpp
410 lines (348 loc) · 13.5 KB
/
object1d.hpp
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
#ifndef HEAT_OBJECT1D_HPP
#define HEAT_OBJECT1D_HPP
#include "matrix.hpp"
#include <functional>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
class object1d {
public:
typedef std::vector<double> data_type;
object1d(double lx, size_t nx, double alpha)
: m_data(nx),
m_lx(lx),
m_nx(nx),
m_alpha(alpha) {}
/// Initialize object using an initialization strategy
/* Valid strategies:
* FLAT: all cells initialized to `value`
* GAUSSIAN: smooth gaussian distribution
* GAUSSIAN_NOISE: gaussian distribution with noise
*
* @param i Initialization strategy to use
* @param value Value used by some initialization strategies. Default is 0.0
*/
void init(initial_condition i, double value = 0.0) {
switch (i) {
case FLAT:
for (size_t i = 0; i < m_nx; i++) {
m_data[i] = value;
}
break;
case GAUSSIAN:
m_data = gaussian(m_nx);
break;
case GAUSSIAN_NOISE:
m_data = gaussian(m_nx);
// TODO: noise
break;
default:
throw std::invalid_argument("Invalid initial condition");
}
}
double lx() const {return m_lx;}
size_t nx() const {return m_nx;}
double alpha() const {return m_alpha;}
double& operator[] (const size_t index) {
return m_data[index];
}
const double& operator[] (const size_t index) const {
return m_data[index];
}
/// output data as packed binary array
/* Output m_data as a packed array of 1 byte integers suitable for graphing
* or drawing. Output is in row major order.
*
* @param edges should the edges of the data set be included? Default is yes
*/
std::string binary(bool edges = true) const {
std::string data;
size_t s = (edges ? m_nx : m_nx-2);
size_t offset = (edges ? 0 : 1);
data.resize(s);
for (size_t x = 0; x < s; x++) {
data[x] = uint8_t(m_data[x+offset]*255);
}
return data;
}
/// output data as JSON array
/* Output m_data as a JSON array.
*
* @param edges should the edges of the data set be included? Default is yes
* @return json array as std::string
*/
std::string json(bool edges = true) const {
std::stringstream data;
size_t sx = (edges ? m_nx : m_nx-2);
size_t offset = (edges ? 0 : 1);
data << "[";
std::string xsep;
for (size_t x = 0; x < sx; x++) {
data << xsep << int(m_data[x+offset]*255);
xsep = ",";
}
data << "]";
return data.str();
}
/// runs a FTCS discritized simulation of heat diffusion of object o
/* Run a Forward-Time Central-Space simulation of heat diffusion of object o
*
* @param ts Number of timesteps to simulate
* @param dt Duration of each time step (s)
* @param bs Boundary handling style. Options are:
* CONSTANT: boundary cells are set to `v` and never changed
* PERIODIC: boundary cells wrap around the object to take on
* the temperature value on the opposite side.
* @param v Value to use for CONSTANT boundary handling style
* @param S Time independent source term to be added at each timestep
* @param callback function to call periodically during the simulation to
* provide feedback to the caller and test whether to halt
* the simulation early.
* @param callback_interval Number of timesteps between each callback
*/
void ftcs(size_t ts,
double dt,
boundary_style bs,
double v,
const object1d& S,
std::function<bool(const object1d&,size_t ts)> callback,
size_t callback_interval) const
{
// generated parameters
double dx = m_lx / m_nx; // distance between grid points (m)
double C = m_alpha*dt/(pow(dx,2)); // C! (unitless)
size_t nx = m_nx+2;
// Set up two buffers to store the current and previous timestep information. These will
// be swapped rather than copied. Each dimension of the buffer will be the size of the
// object plus two for the boundary conditions;
std::vector<object1d> buf; // optimization: buf should only be cleared if necessary
buf.push_back(object1d(m_lx,nx,m_alpha));
buf.push_back(object1d(m_lx,nx,m_alpha));
// fill the buffer array with data from o
for (int x = 1; x < nx-1; x++) {
buf[0][x] = m_data[x-1];
}
// compute boundary cell
if (bs == CONSTANT) {
compute_constant_boundaries(buf[0],v);
} else {
compute_periodic_boundaries(buf[0]);
}
// run simulation
size_t t;
for (t = 0; t < ts; t++) {
if (t%callback_interval == 0) {
if (!callback(buf[(t+1)%2],t)) {
break;
}
}
// simulate non-boundary squares
for (size_t x = 1; x < nx-1; x++) {
buf[(t+1)%2][x] = buf[t%2][x] + dt*S[x] +
C*( buf[t%2][x-1] + buf[t%2][x+1] - 2*buf[t%2][x] );
}
// boundary conditions
// The above simulation loop doesn't change the edges. If they were
// constant this is correct, if they were periodic they need to be
// re-filled.
if (bs == PERIODIC) {
compute_periodic_boundaries(buf[(t+1)%2]);
}
}
callback(buf[(t+1)%2],t);
}
/// runs a Crank Nichsolson discritized simulation of heat diffusion of object o
/* Run a Crank Nicholson simulation of heat diffusion of object o
*
* @param ts Number of timesteps to simulate
* @param dt Duration of each time step (s)
* @param bs Boundary handling style. Options are:
* CONSTANT: boundary cells are set to `v` and never changed
* PERIODIC: boundary cells wrap around the object to take on
* the temperature value on the opposite side.
* @param v Value to use for CONSTANT boundary handling style
* @param S Time independent source term to be added at each timestep
* @param callback function to call periodically during the simulation to
* provide feedback to the caller and test whether to halt
* the simulation early.
* @param callback_interval Number of timesteps between each callback
*/
void crank_nicholson(size_t ts,
double dt,
boundary_style bs,
double v,
const object1d& S,
std::function<bool(const object1d&,size_t ts)> callback,
size_t callback_interval) const
{
double dx = m_lx / m_nx;
double C = m_alpha*dt/(pow(dx,2));
size_t nx = m_nx;
if (nx < 2) {
throw std::invalid_argument("object must have size at least 2");
}
object1d buf(m_lx,nx,m_alpha);
matrix<double> A(m_nx,m_nx); // Coefficient matrix
std::vector<double> b(m_nx); //
std::vector<double> x(m_nx); // tnew
// load initial conditions
for (size_t i = 0; i < nx; i++) {
x[i] = m_data[i];
}
size_t t;
for (t = 0; t < ts; t++) {
if (t%callback_interval == 0) {
// this should be optimized with a object1d copy constructor from vector or a
// better callback that writes vectors rather than objects back to the wire.
for (size_t i = 0; i < nx; i++) {
buf[i] = x[i];
}
if (!callback(buf,t)) {
break;
}
}
// Fill in A
A[0][0] = (1+2*C);
A[0][1] = -1*C;
//A[0][nx-1] = 1; // in boundary condition wrap?
A[nx-1][nx-2] = -1*C;
A[nx-1][nx-1] = (1+2*C);
//A[nx-1][0] = 1; // in boundary condition wrap?
for (size_t i = 1; i < nx-1; i++) {
A[i][i-1] = -1*C;
A[i][i] = (1+2*C);
A[i][i+1] = -1*C;
}
// Fill in b
if (bs == CONSTANT) {
b[0] = x[0] + (C/2.0)*(v+x[1]-2*x[0]);
b[nx-1] = x[nx-1] + (C/2)*(x[nx-2]+v-2*x[nx-1]);
} else {
b[0] = x[0] + (C/2.0)*(x[nx-1]+x[1]-2*x[0]);
b[nx-1] = x[nx-1] + (C/2)*(x[nx-2]+x[0]-2*x[nx-1]);
}
for (size_t i = 1; i < nx-1; i++) {
b[i] = x[i] + (C/2.0)*(x[i-1]+x[i+1]-2*x[i]);
}
std::cout << "ts: " << t << std::endl;
std::cout << "initial A: " << std::endl << A << std::endl;
std::cout << "initial b: " << b << std::endl;
upper_triangulate(A,b);
std::cout << "solved A: " << std::endl << A << std::endl;
std::cout << "solved b: " << b << std::endl;
back_sub(A,b,x);
std::cout << "solved x: " << x << std::endl;
for (size_t i = 0; i < nx; i++) {
x[i] += S[i]*dt;
}
}
// this should be optimized with a object1d copy constructor from vector or a
// better callback that writes vectors rather than objects back to the wire.
for (size_t i = 0; i < nx; i++) {
buf[i] = x[i];
}
callback(buf,t);
}
/// runs a simulation of heat diffusion of object o using Jacobi iteration
/* Run a simulation of heat diffusion of object o using Jacobi iteration and
* backwards euler discretization.
*
* @param ts Number of timesteps to simulate
* @param dt Duration of each time step (s)
* @param bs Boundary handling style. Options are:
* CONSTANT: boundary cells are set to `v` and never changed
* PERIODIC: boundary cells wrap around the object to take on
* the temperature value on the opposite side.
* @param v Value to use for CONSTANT boundary handling style
* @param S Time independent source term to be added at each timestep
* @param callback function to call periodically during the simulation to
* provide feedback to the caller and test whether to halt
* the simulation early.
* @param callback_interval Number of timesteps between each callback
*/
void jacobi(size_t ts,
double dt,
boundary_style bs,
double v,
const object1d& S,
std::function<bool(const object1d&,size_t ts)> callback,
size_t callback_interval) const
{
double dx = m_lx / m_nx;
double C = m_alpha*dt/(pow(dx,2));
size_t nx = m_nx;
object1d xold(m_lx,nx,m_alpha);
object1d xcur(m_lx,nx,m_alpha);
object1d xnew(m_lx,nx,m_alpha);
// load initial values
xold = *this;
xcur = xold;
size_t t;
size_t MAX_ITER = 1000;
double EPSILON = 1e-6;
double C2 = C/(2*C+1);
double C3 = 1/(2*C+1);
int iter = 0;
for (t = 0; t < ts; t++) {
if (t%callback_interval == 0) {
if (!callback(xcur,t)) {
break;
}
}
size_t i = 0;
for (i = 0; i < MAX_ITER; i++) {
xnew[0] = C2*((bs == CONSTANT ? v : xcur[nx-1]) + xcur[1]) + C3*xold[0];
for (size_t x = 1; x < nx-1; x++) {
xnew[x] = C2*(xcur[x-1] + xcur[x+1]) + C3*xold[x];
}
xnew[nx-1] = C2*(xcur[nx-1] + (bs == CONSTANT ? v : xcur[0])) + C3*xold[nx-1];
if (xcur.mean_abs_diff(xnew) < EPSILON) {
break;
}
xcur = xnew;
}
iter += i;
for (size_t x = 0; x < nx; x++) {
xcur[x] += S[x]*dt;
}
xold = xcur;
}
std::cout << "average iterations: " << (iter/t) << std::endl;
callback(xcur,t);
}
/// Sets boundary cells of an object to a constant value.
/*
* @param o Object to write to
* @param val Value to write
*/
void compute_constant_boundaries(object1d& o, double val) const {
o[0] = val;
o[o.nx()-1] = val;
}
/// Sets boundary cells of an object to wrap around.
/*
* @param o Object to write to
*/
void compute_periodic_boundaries(object1d& o) const {
o[0] = o[o.nx()-2];
o[o.nx()-1] = o[1];
}
/// Compute the mean of the absolute value of the difference
double mean_abs_diff(object1d& o) const {
double val = 0;
if (o.nx() != nx()) {
throw std::invalid_argument("objects must be the same size to use mean_abs_diff");
}
for (size_t x = 0; x < m_nx; x++) {
val += fabs(m_data[x] - o[x]);
}
return val / m_nx;
}
private:
data_type m_data; // data vector
double m_lx; // length of x dimension (m)
size_t m_nx; // grid spaces in x dimension
double m_alpha; // thermal diffusivity (m^2/s)
};
#endif // HEAT_OBJECT1D_HPP