Mercurial > repos > public > sbplib
annotate +rv/+time/RungekuttaExteriorRV.m @ 1020:5359a61cb4d9 feature/advectionRV
Add utility for constructing the operators used by a discretization emplying RV-stabilization
- Add constructDiffOps which creates the operatores used for a scheme with RV-stabilization
- Minor clean up in RungekuttaExteriorRV
author | Vidar Stiernström <vidar.stiernstrom@it.uu.se> |
---|---|
date | Fri, 14 Dec 2018 18:33:10 +0100 |
parents | 2d7c1333bd6c |
children | dce08a74e0ad |
rev | line source |
---|---|
1013
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
1 classdef RungekuttaExteriorRV < time.Timestepper |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
2 properties |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
3 F % RHS of the ODE |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
4 k % Time step |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
5 t % Time point |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
6 v % Solution vector |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
7 n % Time level |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
8 coeffs % The coefficents used for the RK time integration |
1017
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
9 |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
10 % Properties related to the residual viscositys |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
11 RV % Residual Viscosity operator |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
12 viscosity % Viscosity vector |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
13 v_prev % Solution vector at previous time levels, used for the RV evaluation |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
14 DvDt % Function for computing the time deriative used for the RV evaluation |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
15 lowerBdfOrder % Orders of the approximation of the time deriative, used for the RV evaluation. |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
16 % dictates which accuracy the boot-strapping should start from. |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
17 upperBdfOrder % Orders of the approximation of the time deriative, used for the RV evaluation. |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
18 % Dictates the order of accuracy used once the boot-strapping is complete. |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
19 |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
20 % Convenience properties. Only for plotting |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
21 residual |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
22 dvdt |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
23 Df |
1013
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
24 end |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
25 methods |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
26 |
1017
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
27 % TODO: Decide on how to compute dvdt. |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
28 function obj = RungekuttaExteriorRV(F, k, t0, v0, RV, DvDt, rkOrder, bdfOrders) |
1013
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
29 obj.F = F; |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
30 obj.k = k; |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
31 obj.t = t0; |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
32 obj.v = v0; |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
33 obj.n = 0; |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
34 % Extract the coefficients for the specified rkOrder |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
35 % used for the RK updates from the Butcher tableua. |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
36 [s,a,b,c] = time.rk.butcherTableau(rkOrder); |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
37 obj.coeffs = struct('s',s,'a',a,'b',b,'c',c); |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
38 |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
39 obj.RV = RV; |
1014
e547794a9407
Add boot-strapping to RungeKuttaExteriorRV
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1013
diff
changeset
|
40 % TBD: Decide on if the initialization of the previous stages used by |
e547794a9407
Add boot-strapping to RungeKuttaExteriorRV
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1013
diff
changeset
|
41 % the BDF should be done here, or if it should be checked for each |
e547794a9407
Add boot-strapping to RungeKuttaExteriorRV
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1013
diff
changeset
|
42 % step taken. |
1016
4b42999874c0
Add lower level for boot-strapping to RungeKuttaExteriorRV
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1015
diff
changeset
|
43 % If it is moved here, then multiple branching stages can be removed in step() |
4b42999874c0
Add lower level for boot-strapping to RungeKuttaExteriorRV
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1015
diff
changeset
|
44 % but this will effectively result in a plotted simulation starting from n = upperBdfOrder. |
4b42999874c0
Add lower level for boot-strapping to RungeKuttaExteriorRV
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1015
diff
changeset
|
45 % In addition, the properties lowerBdfOrder and upperBdfOrder can be removed. |
1017
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
46 % obj.lowerBdfOrder = bdfOrders.lowerBdfOrder; |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
47 % obj.upperBdfOrder = bdfOrders.upperBdfOrder; |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
48 % assert((obj.lowerBdfOrder >= 1) && (obj.upperBdfOrder <= 6)); |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
49 % obj.v_prev = []; |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
50 % obj.DvDt = rv.time.BdfDerivative(); |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
51 % obj.viscosity = zeros(size(v0)); |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
52 % obj.residual = zeros(size(v0)); |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
53 % obj.dvdt = zeros(size(v0)); |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
54 % obj.Df = zeros(size(v0)); |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
55 |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
56 % Using the ODE: |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
57 obj.DvDt = DvDt; |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
58 obj.dvdt = obj.DvDt(obj.v); |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
59 [obj.viscosity, obj.Df] = RV.evaluate(obj.v,obj.dvdt); |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
60 obj.residual = obj.dvdt + obj.Df; |
1013
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
61 end |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
62 |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
63 function [v, t] = getV(obj) |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
64 v = obj.v; |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
65 t = obj.t; |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
66 end |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
67 |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
68 function state = getState(obj) |
1017
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
69 state = struct('v', obj.v, 'residual', obj.residual, 'dvdt', obj.dvdt, 'Df', obj.Df, 'viscosity', obj.viscosity, 't', obj.t); |
1013
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
70 end |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
71 |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
72 function obj = step(obj) |
1020
5359a61cb4d9
Add utility for constructing the operators used by a discretization emplying RV-stabilization
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1017
diff
changeset
|
73 % % Store current time level and update v_prev |
1017
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
74 % numStoredStages = size(obj.v_prev,2); |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
75 % if (numStoredStages < obj.upperBdfOrder) |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
76 % obj.v_prev = [obj.v, obj.v_prev]; |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
77 % numStoredStages = numStoredStages+1; |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
78 % else |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
79 % obj.v_prev(:,2:end) = obj.v_prev(:,1:end-1); |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
80 % obj.v_prev(:,1) = obj.v; |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
81 % end |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
82 |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
83 obj.dvdt = obj.DvDt(obj.v); |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
84 [obj.viscosity, obj.Df] = obj.RV.evaluate(obj.v,obj.dvdt); |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
85 obj.residual = obj.dvdt + obj.Df; |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
86 |
1013
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
87 % Fix the viscosity of the RHS function F |
1017
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
88 F_visc = @(v,t) obj.F(v,t,obj.viscosity); |
1014
e547794a9407
Add boot-strapping to RungeKuttaExteriorRV
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1013
diff
changeset
|
89 obj.v = time.rk.rungekutta(obj.v, obj.t, obj.k, F_visc, obj.coeffs); |
1013
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
90 obj.t = obj.t + obj.k; |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
91 obj.n = obj.n + 1; |
1017
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
92 |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
93 % %Calculate dvdt and evaluate RV for the new time level |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
94 % if ((numStoredStages >= obj.lowerBdfOrder) && (numStoredStages <= obj.upperBdfOrder)) |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
95 % obj.dvdt = obj.DvDt.evaluate(obj.v, obj.v_prev, obj.k); |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
96 % [obj.viscosity, obj.Df] = obj.RV.evaluate(obj.v,obj.dvdt); |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
97 % obj.residual = obj.dvdt + obj.Df; |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1016
diff
changeset
|
98 % end |
1013
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
99 end |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
100 end |
eb441fbdf379
Draft implementation of RungeKutta with exterior RV updates
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
101 end |