-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArtificialTest.cpp
236 lines (197 loc) · 6.32 KB
/
ArtificialTest.cpp
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
/* Stability-Optimized Explicit Runge-Kutta Methods:
3 fourth-order methods: LSRK(12,4), LSRK(13,4), LSRK(14,4) with different stages
3 test problems: 2) artificial test problem from Cash
increasing stiff as lambda increases
Sharon Yang
Math 6321 @ SMU
Fall 2020 */
#include <iostream>
#include <iomanip>
#include "LSRK12.hpp"
#include "LSRK13.hpp"
#include "LSRK14.hpp"
#include "erk4.hpp"
#include "fwd_euler.hpp"
using namespace std;
using namespace arma;
// ODE RHS function classes
class RHS: public RHSFunction {
public:
double lambda;
int Evaluate(double t, vec& y, vec& f) { // evaluates the RHS function, f(t,y)
f(0) = -lambda*y(0) + (lambda-1.0)*exp(-t);
return 0;
}
};
// Convenience function for analytical solution
vec ytrue(const double t) {
vec yt(1);
yt(0) = exp(-t);
return yt;
};
// main routine
int main() {
// time steps to try
vec h("0.1 0.05 0.04 0.03 0.02 0.01 0.005 0.001");
// initial condition and time span
vec y0("1.0");
double t0 = 0.0;
double Tf = 20.0;
vec lambdas("100.0 200.0 400.0");
// matrix for errors at each h and lambdas
mat e(h.n_elem,lambdas.n_elem);
e.fill(0.0);
mat conv(h.n_elem-1,lambdas.n_elem);
conv.fill(0.0);
// set desired output times
int Nout = 21; // includes initial condition
vec tspan = linspace(t0, Tf, Nout);
// create ODE RHS function object
RHS f;
// create true solution results
mat Ytrue(1,Nout);
for (size_t i=0; i<Nout; i++)
Ytrue.col(i) = ytrue(tspan(i));
// problem 2
cout << "\nProblem 2 Artificial Problem from Cash:\n";
//------------LSRK12---------------
cout << "\nLSRK12:\n";
for (size_t il=0; il<lambdas.n_elem; il++) {
f.lambda = lambdas(il);
cout << " lambda = " << f.lambda << ":\n";
LSRK12Stepper LSRK12(f,y0);
// create LSRK12 solvers
for (size_t ih=0; ih<h.n_elem; ih++) {
// call stepper
cout << " h = " << h(ih) << ":";
mat Y = LSRK12.Evolve(tspan, h(ih), y0);
// output solution, errors, and overall error
mat Yerr = abs(Y-Ytrue);
e(ih,il) = Yerr.max();
if (ih > 0) {
conv(ih-1,il) = log(e(ih,il)/e(ih-1,il))/log(h(ih)/h(ih-1));
cout << " Max error = " << e(ih,il) << ", conv rate = "
<< log(e(ih,il)/e(ih-1,il))/log(h(ih)/h(ih-1)) << endl;
}
else {
cout << " Max error = " << e(ih,il) << endl;
}
}
e.save("artificial_err_12.txt",raw_ascii);
conv.save("artificial_conv_12.txt",raw_ascii);
}
//------------LSRK13---------------
e.fill(0.0);
conv.fill(0.0);
cout << "\nLSRK13:\n";
for (size_t il=0; il<lambdas.n_elem; il++) {
f.lambda = lambdas(il);
cout << " lambda = " << f.lambda << ":\n";
LSRK13Stepper LSRK13(f,y0);
// create LSRK13 solvers
for (size_t ih=0; ih<h.n_elem; ih++) {
// call stepper
cout << " h = " << h(ih) << ":";
mat Y = LSRK13.Evolve(tspan, h(ih), y0);
// output solution, errors, and overall error
mat Yerr = abs(Y-Ytrue);
e(ih,il) = Yerr.max();
if (ih > 0) {
conv(ih-1,il) = log(e(ih,il)/e(ih-1,il))/log(h(ih)/h(ih-1));
cout << " Max error = " << e(ih,il) << ", conv rate = "
<< log(e(ih,il)/e(ih-1,il))/log(h(ih)/h(ih-1)) << endl;
}
else {
cout << " Max error = " << e(ih,il) << endl;
}
}
e.save("artificial_err_13.txt",raw_ascii);
conv.save("artificial_conv_13.txt",raw_ascii);
}
//------------LSRK14---------------
e.fill(0.0);
conv.fill(0.0);
cout << "\nLSRK14:\n";
for (size_t il=0; il<lambdas.n_elem; il++) {
f.lambda = lambdas(il);
cout << " lambda = " << f.lambda << ":\n";
LSRK14Stepper LSRK14(f,y0);
// create LSRK14 solvers
for (size_t ih=0; ih<h.n_elem; ih++) {
// call stepper
cout << " h = " << h(ih) << ":";
mat Y = LSRK14.Evolve(tspan, h(ih), y0);
// output solution, errors, and overall error
mat Yerr = abs(Y-Ytrue);
e(ih,il) = Yerr.max();
if (ih > 0) {
conv(ih-1,il) = log(e(ih,il)/e(ih-1,il))/log(h(ih)/h(ih-1));
cout << " Max error = " << e(ih,il) << ", conv rate = "
<< log(e(ih,il)/e(ih-1,il))/log(h(ih)/h(ih-1)) << endl;
}
else {
cout << " Max error = " << e(ih,il) << endl;
}
}
e.save("artificial_err_14.txt",raw_ascii);
conv.save("artificial_conv_14.txt",raw_ascii);
}
//------------ERK4---------------
e.fill(0.0);
conv.fill(0.0);
cout << "\nERK4:\n";
for (size_t il=0; il<lambdas.n_elem; il++) {
f.lambda = lambdas(il);
cout << " lambda = " << f.lambda << ":\n";
ERK4Stepper ERK4(f,y0);
// create ERK4 solvers
for (size_t ih=0; ih<h.n_elem; ih++) {
// call stepper
cout << " h = " << h(ih) << ":";
mat Y = ERK4.Evolve(tspan, h(ih), y0);
// output solution, errors, and overall error
mat Yerr = abs(Y-Ytrue);
e(ih,il) = Yerr.max();
if (ih > 0) {
conv(ih-1,il) = log(e(ih,il)/e(ih-1,il))/log(h(ih)/h(ih-1));
cout << " Max error = " << e(ih,il) << ", conv rate = "
<< log(e(ih,il)/e(ih-1,il))/log(h(ih)/h(ih-1)) << endl;
}
else {
cout << " Max error = " << e(ih,il) << endl;
}
}
e.save("artificial_err_erk4.txt",raw_ascii);
conv.save("artificial_conv_erk4.txt",raw_ascii);
}
//------------FE---------------
e.fill(0.0);
conv.fill(0.0);
cout << "\nFE:\n";
for (size_t il=0; il<lambdas.n_elem; il++) {
f.lambda = lambdas(il);
cout << " lambda = " << f.lambda << ":\n";
ForwardEulerStepper FE(f,y0);
// create ERK4 solvers
for (size_t ih=0; ih<h.n_elem; ih++) {
// call stepper
cout << " h = " << h(ih) << ":";
mat Y = FE.Evolve(tspan, h(ih), y0);
// output solution, errors, and overall error
mat Yerr = abs(Y-Ytrue);
e(ih,il) = Yerr.max();
if (ih > 0) {
conv(ih-1,il) = log(e(ih,il)/e(ih-1,il))/log(h(ih)/h(ih-1));
cout << " Max error = " << e(ih,il) << ", conv rate = "
<< log(e(ih,il)/e(ih-1,il))/log(h(ih)/h(ih-1)) << endl;
}
else {
cout << " Max error = " << e(ih,il) << endl;
}
}
e.save("artificial_err_fe.txt",raw_ascii);
conv.save("artificial_conv_fe.txt",raw_ascii);
}
h.save("artificial_h.txt",raw_ascii);
return 0;
}