generated from bssw-tutorial/hello-numerical-world
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathheat.C
220 lines (180 loc) · 5.53 KB
/
heat.C
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
#include "heat.H"
// Double class' statics
int Double::nadds = 0;
int Double::nmults = 0;
int Double::ndivs = 0;
std::size_t Double::nbytes = 0;
// Command-line argument variables
int noout = 0;
int savi = 0;
int outi = 100;
int save = 0;
char const *runame = "heat_results";
char const *alg = "ftcs";
char const *ic = "const(1)";
Double lenx = 1.0;
Double alpha = 0.2;
Double dt = 0.004;
Double dx = 0.1;
Double bc0 = 0;
Double bc1 = 1;
Double maxt = 2.0;
Double min_change = 1e-8*1e-8;
// Various arrays of numerical data
Double *curr = 0; // current solution
Double *last = 0; // last solution
Double *exact = 0; // exact solution (when available)
Double *change_history = 0; // solution l2norm change history
Double *error_history = 0; // solution error history (when available)
Double *cn_Amat = 0; // A matrix for Crank-Nicholson
// Number of points in space, x, and time, t.
int Nx = (int) (lenx/dx);
int Nt = (int) (maxt/dt);
// Utilities
extern Double
l2_norm(int n, Double const *a, Double const *b);
extern void
copy(int n, Double *dst, Double const *src);
extern void
write_array(int t, int n, Double dx, Double const *a);
extern void
set_initial_condition(int n, Double *a, Double dx, char const *ic);
extern void
initialize_crankn(int n,
Double alpha, Double dx, Double dt,
Double **_cn_Amat);
extern void
process_args(int argc, char **argv);
extern void
compute_exact_solution(int n, Double *a, Double dx, char const *ic,
Double alpha, Double t, Double bc0, Double bc1);
extern bool
update_solution_ftcs(int n,
Double *curr, Double const *last,
Double alpha, Double dx, Double dt,
Double bc_0, Double bc_1);
extern bool
update_solution_upwind15(int n,
Double *curr, Double const *last,
Double alpha, Double dx, Double dt,
Double bc_0, Double bc_1);
extern bool
update_solution_crankn(int n,
Double *curr, Double const *last,
Double const *cn_Amat,
Double bc_0, Double bc_1);
static void
initialize(void)
{
Nx = (int) (lenx/dx)+1;
Nt = (int) (maxt/dt);
dx = lenx/(Nx-1);
curr = new Double[Nx]();
last = new Double[Nx]();
if (save)
{
exact = new Double[Nx]();
change_history = new Double[Nt]();
error_history = new Double[Nt]();
}
assert(strncmp(alg, "ftcs", 4)==0 ||
strncmp(alg, "upwind15", 8)==0 ||
strncmp(alg, "crankn", 6)==0);
#ifdef HAVE_FEENABLEEXCEPT
feenableexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW);
#endif
if (!strncmp(alg, "crankn", 6))
initialize_crankn(Nx, alpha, dx, dt, &cn_Amat);
/* Initial condition */
set_initial_condition(Nx, last, dx, ic);
}
int finalize(int ti, Double maxt, Double change)
{
int retval = 0;
write_array(TFINAL, Nx, dx, curr);
if (save)
{
write_array(RESIDUAL, ti < Nt ? ti : Nt, dt, change_history);
write_array(ERROR, ti < Nt ? ti : Nt, dt, error_history);
}
if (outi)
{
printf("Iteration %04d: last change l2=%g\n", ti, (double) change);
printf("Counts: %s\n", Double::counts_string());
}
delete [] curr;
delete [] last;
if (exact) delete [] exact;
if (change_history) delete [] change_history;
if (error_history) delete [] error_history;
if (cn_Amat) delete [] cn_Amat;
if (strncmp(alg, "ftcs", 4)) free((void*)alg);
if (strncmp(ic, "const(1)", 8)) free((void*)ic);
return retval;
}
static bool
update_solution()
{
if (!strcmp(alg, "ftcs"))
return update_solution_ftcs(Nx, curr, last, alpha, dx, dt, bc0, bc1);
else if (!strcmp(alg, "upwind15"))
return update_solution_upwind15(Nx, curr, last, alpha, dx, dt, bc0, bc1);
else if (!strcmp(alg, "crankn"))
return update_solution_crankn(Nx, curr, last, cn_Amat, bc0, bc1);
return false;
}
static Double
update_output_files(int ti)
{
Double change;
if (ti>0 && save)
{
compute_exact_solution(Nx, exact, dx, ic, alpha, ti*dt, bc0, bc1);
if (savi && ti%savi==0)
write_array(ti, Nx, dx, exact);
}
if (ti>0 && savi && ti%savi==0)
write_array(ti, Nx, dx, curr);
change = l2_norm(Nx, curr, last);
if (save && ti < Nt)
{
change_history[ti] = change;
error_history[ti] = l2_norm(Nx, curr, exact);
}
return change;
}
int main(int argc, char **argv)
{
int ti;
Double change;
// Read command-line args and set values
process_args(argc, argv);
// Allocate arrays and set initial conditions
initialize();
// Iterate to max iterations or solution change is below threshold
for (ti = 0; ti*dt < maxt; ti++)
{
// compute the next solution step
if (!update_solution())
{
fprintf(stderr, "Solution criteria violated. Make better choices\n");
exit(1);
}
// compute amount of change in solution
change = update_output_files(ti);
// Handle possible termination by change threshold
if (maxt == INT_MAX && change < min_change)
{
printf("Stopped after %06d iterations for threshold %g\n",
ti, (double) change);
break;
}
// Output progress
if (outi && ti%outi==0)
printf("Iteration %04d: last change l2=%g\n", ti, (double) change);
// Copy current solution to last
copy(Nx, last, curr);
}
// Delete storage and output final results
return finalize(ti, maxt, change);
}