annotate +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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
992
bbd165cc585c Move time.Rungekutta to time.rk.General
Jonatan Werpers <jonatan@werpers.com>
parents: 986
diff changeset
1 classdef General < time.Timestepper
888
8732d6bd9890 Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
2 properties
8732d6bd9890 Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
3 F % RHS of the ODE
918
679f4ddd982f Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 888
diff changeset
4 dt % Time step
888
8732d6bd9890 Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
5 t % Time point
8732d6bd9890 Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
6 v % Solution vector
8732d6bd9890 Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
7 n % Time level
8732d6bd9890 Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
8 scheme % The scheme used for the time stepping, e.g rk4, rk6 etc.
918
679f4ddd982f Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 888
diff changeset
9 coeffs % Butcher tableau coefficients
679f4ddd982f Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 888
diff changeset
10 V % All stage approximations in most recent time step
679f4ddd982f Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 888
diff changeset
11 K % All stage rates in most recent time step
888
8732d6bd9890 Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
12 end
8732d6bd9890 Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
13
8732d6bd9890 Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
14
8732d6bd9890 Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
15 methods
993
44e7e497c3b7 Make time.rk.General accept a butcher tableau instead of a string to choose method. String variant implemented as a static method
Jonatan Werpers <jonatan@werpers.com>
parents: 992
diff changeset
16 % Timesteps v_t = F(v,t), using the specified ButcherTableau
44e7e497c3b7 Make time.rk.General accept a butcher tableau instead of a string to choose method. String variant implemented as a static method
Jonatan Werpers <jonatan@werpers.com>
parents: 992
diff changeset
17 % from t = t0 with timestep dt and initial conditions v(0) = v0
44e7e497c3b7 Make time.rk.General accept a butcher tableau instead of a string to choose method. String variant implemented as a static method
Jonatan Werpers <jonatan@werpers.com>
parents: 992
diff changeset
18 function obj = General(F, dt, t0, v0, bt, discreteData)
44e7e497c3b7 Make time.rk.General accept a butcher tableau instead of a string to choose method. String variant implemented as a static method
Jonatan Werpers <jonatan@werpers.com>
parents: 992
diff changeset
19 assertType(bt, 'time.rk.ButcherTableau')
931
384ca2331a12 Make Rungekutta class allow for discrete data.
Martin Almquist <malmquist@stanford.edu>
parents: 918
diff changeset
20 default_arg('discreteData', []);
888
8732d6bd9890 Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
21 obj.F = F;
918
679f4ddd982f Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 888
diff changeset
22 obj.dt = dt;
888
8732d6bd9890 Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
23 obj.t = t0;
8732d6bd9890 Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
24 obj.v = v0;
8732d6bd9890 Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
25 obj.n = 0;
918
679f4ddd982f Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 888
diff changeset
26
993
44e7e497c3b7 Make time.rk.General accept a butcher tableau instead of a string to choose method. String variant implemented as a static method
Jonatan Werpers <jonatan@werpers.com>
parents: 992
diff changeset
27 assert(bt.isExplicit())
44e7e497c3b7 Make time.rk.General accept a butcher tableau instead of a string to choose method. String variant implemented as a static method
Jonatan Werpers <jonatan@werpers.com>
parents: 992
diff changeset
28 obj.coeffs = struct('s',bt.nStages,'a',bt.a(:,1:end-1),'b',bt.b,'c',bt.c);
918
679f4ddd982f Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 888
diff changeset
29
931
384ca2331a12 Make Rungekutta class allow for discrete data.
Martin Almquist <malmquist@stanford.edu>
parents: 918
diff changeset
30 if isempty(discreteData)
984
0585a2ee7ee7 Inline the rk.rungekutta_4 function.
Jonatan Werpers <jonatan@werpers.com>
parents: 933
diff changeset
31 obj.scheme = @(v,t,n) time.rk.rungekutta(v, t, dt, F, obj.coeffs);
888
8732d6bd9890 Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
32 else
932
3860dad28239 Remove unnecessary(?) arguments in Rungekutta.scheme.
Martin Almquist <malmquist@stanford.edu>
parents: 931
diff changeset
33 obj.scheme = @(v,t,n) time.rk.rungekuttaDiscreteData(v, t, dt, F, obj.coeffs, discreteData, n);
888
8732d6bd9890 Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
34 end
8732d6bd9890 Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
35 end
8732d6bd9890 Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
36
918
679f4ddd982f Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 888
diff changeset
37 % v: Current solution
679f4ddd982f Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 888
diff changeset
38 % t: Current time
679f4ddd982f Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 888
diff changeset
39 % V: All stage approximations in most recent time step
679f4ddd982f Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 888
diff changeset
40 % K: All stage rates in most recent time step
679f4ddd982f Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 888
diff changeset
41 % T: Time points (corresponding to V and K) in most recent time step
679f4ddd982f Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 888
diff changeset
42 function [v,t,V,T,K] = getV(obj)
888
8732d6bd9890 Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
43 v = obj.v;
8732d6bd9890 Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
44 t = obj.t;
918
679f4ddd982f Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 888
diff changeset
45 V = obj.V;
679f4ddd982f Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 888
diff changeset
46 K = obj.K;
679f4ddd982f Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 888
diff changeset
47 T = obj.t + obj.dt*obj.coeffs.b;
888
8732d6bd9890 Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
48 end
8732d6bd9890 Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
49
8732d6bd9890 Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
50 function obj = step(obj)
932
3860dad28239 Remove unnecessary(?) arguments in Rungekutta.scheme.
Martin Almquist <malmquist@stanford.edu>
parents: 931
diff changeset
51 [obj.v, obj.V, obj.K] = obj.scheme(obj.v, obj.t, obj.n);
918
679f4ddd982f Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 888
diff changeset
52 obj.t = obj.t + obj.dt;
888
8732d6bd9890 Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
53 obj.n = obj.n + 1;
8732d6bd9890 Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
54 end
933
34b3d092a4d0 Add methods timePoints and quadWeights to Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 932
diff changeset
55
34b3d092a4d0 Add methods timePoints and quadWeights to Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 932
diff changeset
56 % Returns a vector of time points, including substage points,
34b3d092a4d0 Add methods timePoints and quadWeights to Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 932
diff changeset
57 % in the time interval [t0, tEnd].
34b3d092a4d0 Add methods timePoints and quadWeights to Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 932
diff changeset
58 % The time-step obj.dt is assumed to be aligned with [t0, tEnd] already.
34b3d092a4d0 Add methods timePoints and quadWeights to Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 932
diff changeset
59 function tvec = timePoints(obj, t0, tEnd)
34b3d092a4d0 Add methods timePoints and quadWeights to Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 932
diff changeset
60 N = round( (tEnd-t0)/obj.dt );
34b3d092a4d0 Add methods timePoints and quadWeights to Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 932
diff changeset
61 tvec = zeros(N*obj.s, 1);
34b3d092a4d0 Add methods timePoints and quadWeights to Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 932
diff changeset
62 s = obj.coeffs.s;
34b3d092a4d0 Add methods timePoints and quadWeights to Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 932
diff changeset
63 c = obj.coeffs.c;
34b3d092a4d0 Add methods timePoints and quadWeights to Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 932
diff changeset
64 for i = 1:N
34b3d092a4d0 Add methods timePoints and quadWeights to Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 932
diff changeset
65 ind = (i-1)*s+1 : i*s;
34b3d092a4d0 Add methods timePoints and quadWeights to Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 932
diff changeset
66 tvec(ind) = ((i-1) + c')*obj.dt;
34b3d092a4d0 Add methods timePoints and quadWeights to Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 932
diff changeset
67 end
34b3d092a4d0 Add methods timePoints and quadWeights to Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 932
diff changeset
68 end
34b3d092a4d0 Add methods timePoints and quadWeights to Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 932
diff changeset
69
34b3d092a4d0 Add methods timePoints and quadWeights to Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 932
diff changeset
70 % Returns a vector of quadrature weights corresponding to grid points
34b3d092a4d0 Add methods timePoints and quadWeights to Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 932
diff changeset
71 % in time interval [t0, tEnd], substage points included.
34b3d092a4d0 Add methods timePoints and quadWeights to Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 932
diff changeset
72 % The time-step obj.dt is assumed to be aligned with [t0, tEnd] already.
34b3d092a4d0 Add methods timePoints and quadWeights to Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 932
diff changeset
73 function weights = quadWeights(obj, t0, tEnd)
34b3d092a4d0 Add methods timePoints and quadWeights to Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 932
diff changeset
74 N = round( (tEnd-t0)/obj.dt );
34b3d092a4d0 Add methods timePoints and quadWeights to Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 932
diff changeset
75 b = obj.coeffs.b;
34b3d092a4d0 Add methods timePoints and quadWeights to Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 932
diff changeset
76 weights = repmat(b', N, 1);
34b3d092a4d0 Add methods timePoints and quadWeights to Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents: 932
diff changeset
77 end
888
8732d6bd9890 Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
78 end
993
44e7e497c3b7 Make time.rk.General accept a butcher tableau instead of a string to choose method. String variant implemented as a static method
Jonatan Werpers <jonatan@werpers.com>
parents: 992
diff changeset
79
44e7e497c3b7 Make time.rk.General accept a butcher tableau instead of a string to choose method. String variant implemented as a static method
Jonatan Werpers <jonatan@werpers.com>
parents: 992
diff changeset
80 methods(Static)
44e7e497c3b7 Make time.rk.General accept a butcher tableau instead of a string to choose method. String variant implemented as a static method
Jonatan Werpers <jonatan@werpers.com>
parents: 992
diff changeset
81 % TBD: Function name
44e7e497c3b7 Make time.rk.General accept a butcher tableau instead of a string to choose method. String variant implemented as a static method
Jonatan Werpers <jonatan@werpers.com>
parents: 992
diff changeset
82 function ts = methodFromStr(F, dt, t0, v0, methodStr, discreteData)
44e7e497c3b7 Make time.rk.General accept a butcher tableau instead of a string to choose method. String variant implemented as a static method
Jonatan Werpers <jonatan@werpers.com>
parents: 992
diff changeset
83 default_arg('discreteData', []);
44e7e497c3b7 Make time.rk.General accept a butcher tableau instead of a string to choose method. String variant implemented as a static method
Jonatan Werpers <jonatan@werpers.com>
parents: 992
diff changeset
84
44e7e497c3b7 Make time.rk.General accept a butcher tableau instead of a string to choose method. String variant implemented as a static method
Jonatan Werpers <jonatan@werpers.com>
parents: 992
diff changeset
85 try
44e7e497c3b7 Make time.rk.General accept a butcher tableau instead of a string to choose method. String variant implemented as a static method
Jonatan Werpers <jonatan@werpers.com>
parents: 992
diff changeset
86 bt = time.rk.ButcherTableau.(method);
44e7e497c3b7 Make time.rk.General accept a butcher tableau instead of a string to choose method. String variant implemented as a static method
Jonatan Werpers <jonatan@werpers.com>
parents: 992
diff changeset
87 catch
44e7e497c3b7 Make time.rk.General accept a butcher tableau instead of a string to choose method. String variant implemented as a static method
Jonatan Werpers <jonatan@werpers.com>
parents: 992
diff changeset
88 error('Runge-Kutta method ''%s'' is not implemented', methodStr)
44e7e497c3b7 Make time.rk.General accept a butcher tableau instead of a string to choose method. String variant implemented as a static method
Jonatan Werpers <jonatan@werpers.com>
parents: 992
diff changeset
89 end
44e7e497c3b7 Make time.rk.General accept a butcher tableau instead of a string to choose method. String variant implemented as a static method
Jonatan Werpers <jonatan@werpers.com>
parents: 992
diff changeset
90
44e7e497c3b7 Make time.rk.General accept a butcher tableau instead of a string to choose method. String variant implemented as a static method
Jonatan Werpers <jonatan@werpers.com>
parents: 992
diff changeset
91 ts = time.rk.General(F, dt, t0, v0, bt, discreteData);
44e7e497c3b7 Make time.rk.General accept a butcher tableau instead of a string to choose method. String variant implemented as a static method
Jonatan Werpers <jonatan@werpers.com>
parents: 992
diff changeset
92 end
44e7e497c3b7 Make time.rk.General accept a butcher tableau instead of a string to choose method. String variant implemented as a static method
Jonatan Werpers <jonatan@werpers.com>
parents: 992
diff changeset
93 end
44e7e497c3b7 Make time.rk.General accept a butcher tableau instead of a string to choose method. String variant implemented as a static method
Jonatan Werpers <jonatan@werpers.com>
parents: 992
diff changeset
94 end