Mercurial > repos > public > sbplib
annotate +rv/+time/RungeKuttaRvInstage.m @ 1181:9ac86ccfd6a1 feature/rv
Move instage rungekutta time stepping to RungekuttaRvInstage
author | Vidar Stiernström <vidar.stiernstrom@it.uu.se> |
---|---|
date | Fri, 05 Jul 2019 16:51:05 +0200 |
parents | d02e5b8a0b24 |
children |
rev | line source |
---|---|
1169
d02e5b8a0b24
Rename RungekuttaRV time steppers. Add RungekuttaRVMultiStage time stepper
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1152
diff
changeset
|
1 classdef RungekuttaRvInstage < time.Timestepper |
1012
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
2 properties |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
3 F % RHS of the ODE |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
4 k % Time step |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
5 t % Time point |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
6 v % Solution vector |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
7 n % Time level |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
8 coeffs % The coefficents used for the RK time integration |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
9 RV % Residual Viscosity |
1031
2ef20d00b386
For easier comparison, return both the first order and residual viscosity when evaluating the residual. Add the first order and residual viscosity to the state of the RungekuttaRV time steppers
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1030
diff
changeset
|
10 DvDt % Function for computing the time deriative used for the RV evaluation |
1012
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
11 end |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
12 |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
13 methods |
1169
d02e5b8a0b24
Rename RungekuttaRV time steppers. Add RungekuttaRVMultiStage time stepper
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1152
diff
changeset
|
14 function obj = RungekuttaRvInstage(F, k, t0, v0, RV, DvDt, order) |
1012
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
15 obj.F = F; |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
16 obj.k = k; |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
17 obj.t = t0; |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
18 obj.v = v0; |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
19 obj.n = 0; |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
20 % Extract the coefficients for the specified order |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
21 % used for the RK updates from the Butcher tableua. |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
22 [s,a,b,c] = time.rk.butcherTableau(order); |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
23 obj.coeffs = struct('s',s,'a',a,'b',b,'c',c); |
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:
1012
diff
changeset
|
24 obj.RV = RV; |
2d7c1333bd6c
Add support for using the ODE to approximate the time derivative in the residual
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1012
diff
changeset
|
25 obj.DvDt = DvDt; |
1012
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
26 end |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
27 |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
28 function [v, t] = getV(obj) |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
29 v = obj.v; |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
30 t = obj.t; |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
31 end |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
32 |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
33 function state = getState(obj) |
1152
010bb2677230
Clean up in +rv/+time. Make the time stepping more efficient by not storing unnessecary properties in the RK-RV time steppers
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1031
diff
changeset
|
34 dvdt = obj.DvDt(obj.v); |
010bb2677230
Clean up in +rv/+time. Make the time stepping more efficient by not storing unnessecary properties in the RK-RV time steppers
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1031
diff
changeset
|
35 [viscosity, Df, firstOrderViscosity, residualViscosity] = obj.RV.evaluate(obj.v, dvdt); |
010bb2677230
Clean up in +rv/+time. Make the time stepping more efficient by not storing unnessecary properties in the RK-RV time steppers
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1031
diff
changeset
|
36 state = struct('v', obj.v, 'dvdt', dvdt, 'Df', Df, 'viscosity', viscosity, 'residualViscosity', residualViscosity, 'firstOrderViscosity', firstOrderViscosity, 't', obj.t); |
1012
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
37 end |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
38 |
1152
010bb2677230
Clean up in +rv/+time. Make the time stepping more efficient by not storing unnessecary properties in the RK-RV time steppers
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1031
diff
changeset
|
39 % Advances the solution vector one time step using the Runge-Kutta method given by |
010bb2677230
Clean up in +rv/+time. Make the time stepping more efficient by not storing unnessecary properties in the RK-RV time steppers
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1031
diff
changeset
|
40 % obj.coeffs, updating the Residual Viscosity in each Runge-Kutta stage |
1012
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
41 function obj = step(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:
1012
diff
changeset
|
42 obj.v = rv.time.rungekuttaRV(obj.v, obj.t, obj.k, obj.F, obj.RV, obj.DvDt, obj.coeffs); |
1012
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
43 obj.t = obj.t + obj.k; |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
44 obj.n = obj.n + 1; |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
45 end |
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
46 end |
1181
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
47 |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
48 % Takes one time step of size dt using the rungekutta method |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
49 % starting from v and where the function F(v,t,RV) gives the |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
50 % time derivatives. coeffs is a struct holding the RK coefficients |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
51 % for the specific method. RV is the residual viscosity which is updated |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
52 % in between the stages and after the updated solution is computed. |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
53 methods (Static) |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
54 function v = rungekuttaRV(v, t , dt, F, RV, DvDt, coeffs) |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
55 % Move one stage outside to avoid branching for updating the |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
56 % residual inside the loop. |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
57 k = zeros(length(v), coeffs.s); |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
58 k(:,1) = F(v,t,RV.evaluateViscosity(v,DvDt(v))); |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
59 |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
60 % Compute the intermediate stages k |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
61 for i = 2:coeffs.s |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
62 u = v; |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
63 for j = 1:i-1 |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
64 u = u + dt*coeffs.a(i,j)*k(:,j); |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
65 end |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
66 k(:,i) = F(u,t+coeffs.c(i)*dt, RV.evaluateViscosity(u,DvDt(u))); |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
67 end |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
68 |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
69 % Compute the updated solution as a linear combination |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
70 % of the intermediate stages. |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
71 u = v; |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
72 for i = 1:coeffs.s |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
73 u = u + dt*coeffs.b(i)*k(:,i); |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
74 end |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
75 v = u; |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
76 end |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
77 |
9ac86ccfd6a1
Move instage rungekutta time stepping to RungekuttaRvInstage
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1169
diff
changeset
|
78 |
1012
1e437c9e5132
Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
79 end |