Mercurial > repos > public > sbplib
annotate +time/Rungekutta.m @ 918:679f4ddd982f feature/timesteppers
Add properties for stage approximations and stage rates in the Rungekutta class.
author | Martin Almquist <malmquist@stanford.edu> |
---|---|
date | Mon, 26 Nov 2018 16:23:27 -0800 |
parents | 8732d6bd9890 |
children | 384ca2331a12 |
rev | line source |
---|---|
888
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
1 classdef Rungekutta < time.Timestepper |
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 |
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
16 % Timesteps v_t = F(v,t), using the specified RK method from t = t0 with |
918
679f4ddd982f
Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents:
888
diff
changeset
|
17 % timestep dt and initial conditions v = v0 |
679f4ddd982f
Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents:
888
diff
changeset
|
18 function obj = Rungekutta(F, dt, t0, v0, method) |
888
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
19 default_arg('method',"rk4"); |
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
20 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
|
21 obj.dt = dt; |
888
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
22 obj.t = t0; |
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
23 obj.v = v0; |
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
24 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
|
25 |
679f4ddd982f
Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents:
888
diff
changeset
|
26 % Extract the coefficients for the specified method |
679f4ddd982f
Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents:
888
diff
changeset
|
27 % used for the RK updates from the Butcher tableua. |
679f4ddd982f
Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents:
888
diff
changeset
|
28 [s,a,b,c] = time.rk.butcherTableau(method); |
679f4ddd982f
Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents:
888
diff
changeset
|
29 obj.coeffs = struct('s',s,'a',a,'b',b,'c',c); |
679f4ddd982f
Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents:
888
diff
changeset
|
30 |
888
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
31 % TODO: method "rk4" is also implemented in the butcher tableau, but the rungekutta_4.m implementation |
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
32 % might be slightly more efficient. Need to do some profiling before deciding whether or not to keep it. |
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
33 if (method == "rk4") |
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
34 obj.scheme = @time.rk.rungekutta_4; |
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
35 else |
918
679f4ddd982f
Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents:
888
diff
changeset
|
36 obj.scheme = @(v,t,dt,F) 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
|
37 end |
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
38 end |
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
39 |
918
679f4ddd982f
Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents:
888
diff
changeset
|
40 % 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
|
41 % 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
|
42 % 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
|
43 % 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
|
44 % 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
|
45 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
|
46 v = obj.v; |
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
47 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
|
48 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
|
49 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
|
50 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
|
51 end |
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
52 |
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
53 function obj = step(obj) |
918
679f4ddd982f
Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents:
888
diff
changeset
|
54 [obj.v, obj.V, obj.K] = obj.scheme(obj.v, obj.t, obj.dt, obj.F); |
679f4ddd982f
Add properties for stage approximations and stage rates in the Rungekutta class.
Martin Almquist <malmquist@stanford.edu>
parents:
888
diff
changeset
|
55 obj.t = obj.t + obj.dt; |
888
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
56 obj.n = obj.n + 1; |
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
57 end |
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
58 end |
8732d6bd9890
Add general Runge-Kutta class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
59 end |