Mercurial > repos > public > sbplib
comparison +time/+rk/General.m @ 993:44e7e497c3b7 feature/timesteppers
Make time.rk.General accept a butcher tableau instead of a string to choose method. String variant implemented as a static method
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Wed, 09 Jan 2019 11:14:16 +0100 |
parents | bbd165cc585c |
children | 10c5eda235b7 |
comparison
equal
deleted
inserted
replaced
992:bbd165cc585c | 993:44e7e497c3b7 |
---|---|
11 K % All stage rates in most recent time step | 11 K % All stage rates in most recent time step |
12 end | 12 end |
13 | 13 |
14 | 14 |
15 methods | 15 methods |
16 % Timesteps v_t = F(v,t), using the specified RK method from t = t0 with | 16 % Timesteps v_t = F(v,t), using the specified ButcherTableau |
17 % timestep dt and initial conditions v = v0 | 17 % from t = t0 with timestep dt and initial conditions v(0) = v0 |
18 function obj = General(F, dt, t0, v0, method, discreteData) | 18 function obj = General(F, dt, t0, v0, bt, discreteData) |
19 default_arg('method',"rk4"); | 19 assertType(bt, 'time.rk.ButcherTableau') |
20 default_arg('discreteData', []); | 20 default_arg('discreteData', []); |
21 obj.F = F; | 21 obj.F = F; |
22 obj.dt = dt; | 22 obj.dt = dt; |
23 obj.t = t0; | 23 obj.t = t0; |
24 obj.v = v0; | 24 obj.v = v0; |
25 obj.n = 0; | 25 obj.n = 0; |
26 | 26 |
27 % Extract the coefficients for the specified method | 27 assert(bt.isExplicit()) |
28 % used for the RK updates from the Butcher tableua. | 28 obj.coeffs = struct('s',bt.nStages,'a',bt.a(:,1:end-1),'b',bt.b,'c',bt.c); |
29 [s,a,b,c] = time.rk.butcherTableauFromStr(method); | |
30 obj.coeffs = struct('s',s,'a',a,'b',b,'c',c); | |
31 | 29 |
32 if isempty(discreteData) | 30 if isempty(discreteData) |
33 obj.scheme = @(v,t,n) time.rk.rungekutta(v, t, dt, F, obj.coeffs); | 31 obj.scheme = @(v,t,n) time.rk.rungekutta(v, t, dt, F, obj.coeffs); |
34 else | 32 else |
35 obj.scheme = @(v,t,n) time.rk.rungekuttaDiscreteData(v, t, dt, F, obj.coeffs, discreteData, n); | 33 obj.scheme = @(v,t,n) time.rk.rungekuttaDiscreteData(v, t, dt, F, obj.coeffs, discreteData, n); |
76 N = round( (tEnd-t0)/obj.dt ); | 74 N = round( (tEnd-t0)/obj.dt ); |
77 b = obj.coeffs.b; | 75 b = obj.coeffs.b; |
78 weights = repmat(b', N, 1); | 76 weights = repmat(b', N, 1); |
79 end | 77 end |
80 end | 78 end |
79 | |
80 methods(Static) | |
81 % TBD: Function name | |
82 function ts = methodFromStr(F, dt, t0, v0, methodStr, discreteData) | |
83 default_arg('discreteData', []); | |
84 | |
85 try | |
86 bt = time.rk.ButcherTableau.(method); | |
87 catch | |
88 error('Runge-Kutta method ''%s'' is not implemented', methodStr) | |
89 end | |
90 | |
91 ts = time.rk.General(F, dt, t0, v0, bt, discreteData); | |
92 end | |
93 end | |
81 end | 94 end |