-
Notifications
You must be signed in to change notification settings - Fork 1
/
solve_ERK.m
388 lines (311 loc) · 10.8 KB
/
solve_ERK.m
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
function [tvals,Y,nsteps,h] = solve_ERK(fcn,StabFn,tvals,Y0,B,rtol,atol,hmin,hmax,hinit)
% usage: [tvals,Y,nsteps,h] = solve_ERK(fcn,StabFn,tvals,Y0,B,rtol,atol,hmin,hmax,hinit)
%
% Adaptive time step explicit Runge-Kutta solver for the
% vector-valued ODE problem
% y' = F(t,Y), t in tvals, y in R^m,
% Y(t0) = [y1(t0), y2(t0), ..., ym(t0)]'.
%
% Inputs:
% fcn = string holding function name for F(t,Y)
% StabFn = string holding function name for stability constraint on F
% tvals = [t0, t1, t2, ..., tN]
% Y0 = initial value array (column vector of length m)
% B = Butcher matrix for IRK coefficients, of the form
% B = [c A;
% q b;
% p b2 ]
% Here, c is a vector of stage time fractions (s-by-1),
% A is a matrix of Butcher coefficients (s-by-s),
% q is an integer denoting the method order of accuracy,
% b is a vector of solution weights (1-by-s),
% p is an integer denoting the embedding order of accuracy,
% b2 is a vector of embedding weights (1-by-s),
% The [p, b2] row is optional. If that row is not
% provided the method will default to taking fixed
% step sizes of size hmin.
% rtol = desired relative error of solution (scalar)
% atol = desired absolute error of solution (vector or scalar)
% hmin = minimum internal time step size (hmin <= t(i)-t(i-1), for all i)
% hmax = maximum internal time step size (hmax >= hmin)
% hinit = initial internal time step size (hmin <= hinit <= hmax)
%
% Outputs:
% tvals = the same as the input array tvals
% y = [y(t0), y(t1), y(t2), ..., y(tN)], where each
% y(t*) is a column vector of length m.
% nsteps = number of internal time steps taken by method
% h = last internal step size
%
% Note: to run in fixed-step mode, call with hmin=hmax as the desired
% time step size.
%
% Daniel R. Reynolds
% Department of Mathematics
% Southern Methodist University
% July 2018
% All Rights Reserved
% determine whether adaptivity is desired
adaptive = 0;
if (abs(hmax-hmin)/abs(hmax) > sqrt(eps))
adaptive = 1;
end
% if adaptivity enabled, determine approach for error estimation,
% and set the lower-order of accuracy accordingly
[Brows, Bcols] = size(B);
embedded = 0;
p = 0;
if (hmax > hmin) % check whether adaptivity is desired
if (Brows > Bcols)
if (max(abs(B(Bcols+1,2:Bcols))) > eps) % check for embedding coeffs
embedded = 1;
p = B(Bcols+1,1);
end
end
end
if (embedded == 0)
p = B(Bcols,1);
end
% initialize output arrays
N = length(tvals);
m = length(Y0);
Y = zeros(m,N);
Y(:,1) = Y0;
% initialize diagnostics
h_a = 0; % number of accuracy-limited time steps
h_s = 0; % number of stability-limited time steps
a_fails = 0; % total accuracy failures
% set the solver parameters
h_reduce = 0.1; % failed step reduction factor
h_safety = 0.9; % adaptivity safety factor
h_growth = 10; % adaptivity growth bound
h_stable = 0.5; % fraction of stability step to take
ONEMSM = 1-sqrt(eps); % coefficients to account for
ONEPSM = 1+sqrt(eps); % floating-point roundoff
ERRTOL = 1.1; % upper bound on allowed step error
% (in WRMS norm)
% initialize temporary variables
t = tvals(1);
Ynew = Y0;
% set initial time step size
h = hinit;
% initialize work counter
nsteps = 0;
% iterate over output time steps
for tstep = 2:length(tvals)
% loop over internal time steps to get to desired output time
while ((t-tvals(tstep))*h < 0)
% bound internal time step
h = max([h, hmin]); % enforce minimum time step size
h = min([h, hmax]); % maximum time step size
h = min([h, tvals(tstep)-t]); % stop at output time
% reset step failure flag
st_fail = 0;
% compute updated solution and error estimate (if possible);
% increment internal time steps counter
if (adaptive)
if (embedded)
[Ynew,Yerr] = ERKstep_embedded(fcn, Y0, t, h, B);
nsteps = nsteps + 1;
else
[Ynew,Yerr] = ERKstep_Richardson(fcn, Y0, t, h, B);
nsteps = nsteps + 3;
end
else
[Ynew] = ERKstep_basic(fcn, Y0, t, h, B);
nsteps = nsteps + 1;
end
% if time step adaptivity enabled, check step accuracy
if (adaptive)
% estimate error in current step
err_step = max(norm(Yerr./(rtol*Ynew + atol),inf), eps);
% if error too high, flag step as a failure (will be recomputed)
if (err_step > ERRTOL*ONEPSM)
a_fails = a_fails + 1;
st_fail = 1;
end
end
% if step was successful (i.e. error acceptable)
if (st_fail == 0)
% update solution and time for last successful step
Y0 = Ynew;
t = t + h;
% for adaptive methods, use error estimate to adapt the time step
if (adaptive)
h_old = h;
if (err_step == 0.0) % no error, set max possible
h = tvals(end)-t;
else % set next h (I-controller)
h = h_safety * h_old * err_step^(-1.0/p);
end
% enforce maximum growth rate on step sizes
h = min(h_growth*h_old, h);
% otherwise, just use the fixed minimum input step size
else
h = hmin;
end
% limit time step by explicit stability condition
hstab = h_stable * StabFn(t, Ynew);
% keep statistics on how many steps are accuracy vs stability limited
if (h < hstab)
h_a = h_a + 1;
else
h_s = h_s + 1;
end
h = min([h, hstab]);
% if error test failed
else
% if already at minimum step, just return with failure
if (h <= hmin)
error('Cannot achieve desired accuracy.\n Consider reducing hmin or increasing rtol.\n');
return
end
% otherwise, reset guess, reduce time step, retry solve
Ynew = Y0;
h = h * h_reduce;
h_a = h_a + 1;
end % end logic tests for step success/failure
end % end while loop attempting to solve steps to next output time
% store updated solution in output array
Y(:,tstep) = Ynew;
end % time step loop
% end solve_ERK function
end
%------------------------- Utility routines -------------------------%
function [y,yerr] = ERKstep_embedded(fcn, y0, t0, h, B)
% Inputs:
% fcn = ODE RHS function, f(t,y)
% y0 = solution at beginning of time step
% t0 = 'time' at beginning of time step
% h = step size to take
% B = Butcher table to use
%
% Outputs:
% y = new solution at t0+h
% yerr = error vector
% extract ERK method information from B
[Brows, Bcols] = size(B);
s = Bcols - 1; % number of stages
c = B(1:s,1); % stage time fraction array
b = (B(s+1,2:s+1))'; % solution weights (convert to column)
A = B(1:s,2:s+1); % RK coefficients
d = (B(s+2,2:s+1))'; % embedding coefficients
% initialize storage for RHS vectors
k = zeros(length(y0),s);
% loop over stages
for stage=1:s
% construct stage solution and evaluate RHS
% zi = y_n + h*sum_{j=1}^{i-1} (A(i,j)*f(zj))
z = y0;
for j=1:stage-1
z = z + h*A(stage,j)*k(:,j);
end
% construct new stage RHS
k(:,stage) = fcn(t0+h*c(stage),z);
end
% compute new solution and error estimate
% ynew = yold + h*sum(b(j)*fj)
y = y0 + h*k*b;
yerr = h*k*(b-d);
% end of function
end
function [y] = ERKstep_basic(fcn, y0, t0, h, B)
% usage: [y] = ERKstep_basic(fcn, y0, t0, h, B)
%
% Inputs:
% fcn = ODE RHS function, f(t,y)
% y0 = solution at beginning of time step
% t0 = 'time' at beginning of time step
% h = step size to take
% B = Butcher table to use
%
% Outputs:
% y = new solution at t0+h
% extract ERK method information from B
[Brows, Bcols] = size(B);
s = Bcols - 1; % number of stages
c = B(1:s,1); % stage time fraction array
b = (B(s+1,2:s+1))'; % solution weights (convert to column)
A = B(1:s,2:s+1); % RK coefficients
% initialize storage for RHS vectors
k = zeros(length(y0),s);
% loop over stages
for stage=1:s
% construct stage solution and evaluate RHS
% zi = y_n + h*sum_{j=1}^{i-1} (A(i,j)*f(zj))
z = y0;
for j=1:stage-1
z = z + h*A(stage,j)*k(:,j);
end
% construct new stage RHS
k(:,stage) = fcn(t0+h*c(stage),z);
end
% compute new solution and error estimate
% ynew = yold + h*sum(b(j)*fj)
y = y0 + h*k*b;
% end of function
end
function [y,yerr] = ERKstep_Richardson(fcn, y0, t0, h, B)
% usage: [y,yerr] = ERKstep_Richardson(fcn, y0, t0, h, B)
%
% Inputs:
% fcn = ODE RHS function, f(t,y)
% y0 = solution at beginning of time step
% t0 = 'time' at beginning of time step
% h = step size to take
% B = Butcher table to use
%
% Outputs:
% y = new solution at t0+h
% yerr = error vector
% extract ERK method information from B
[Brows, Bcols] = size(B);
s = Bcols - 1; % number of stages
c = B(1:s,1); % stage time fraction array
b = (B(s+1,2:s+1))'; % solution weights (convert to column)
A = B(1:s,2:s+1); % RK coefficients
p = B(Bcols,1);
% initialize storage for RHS vectors
k = zeros(length(y0),s);
% First compute solution with a single step
for stage=1:s
% construct stage solution and evaluate RHS
% zi = y_n + h*sum_{j=1}^{i-1} (A(i,j)*f(zj))
z = y0;
for j=1:stage-1
z = z + h*A(stage,j)*k(:,j);
end
% construct new stage RHS
k(:,stage) = fcn(t0+h*c(stage),z);
end
% compute full-step solution
% ynew = yold + h*sum(b(j)*fj)
y1 = y0 + h*k*b;
% Second compute solution with two half steps
for stage=1:s
% construct stage solution and evaluate RHS
% zi = y_n + h*sum_{j=1}^{i-1} (A(i,j)*f(zj))
z = y0;
for j=1:stage-1
z = z + h/2*A(stage,j)*k(:,j);
end
% construct new stage RHS
k(:,stage) = fcn(t0+h/2*c(stage),z);
end
y2 = y0 + h/2*k*b;
for stage=1:s
% construct stage solution and evaluate RHS
% zi = y_n + h*sum_{j=1}^{i-1} (A(i,j)*f(zj))
z = y2;
for j=1:stage-1
z = z + h/2*A(stage,j)*k(:,j);
end
% construct new stage RHS
k(:,stage) = fcn(t0+h/2*(1+c(stage)),z);
end
y2 = y2 + h/2*k*b;
% Compute Richardson extrapolant and error estimate
y = (2^p)/(2^p-1)*y2 - 1/(2^p-1)*y1;
yerr = 1/(2^p-1)*(y1-y2);
% end of function
end