comparison +rv/+time/RungekuttaRv.m @ 1182:f35ff0861d5a feature/rv

Add standard RungekuttaRv
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Fri, 05 Jul 2019 17:47:13 +0200
parents
children
comparison
equal deleted inserted replaced
1181:9ac86ccfd6a1 1182:f35ff0861d5a
1 classdef RungekuttaRv < time.Timestepper
2 properties
3 F % RHS of the ODE
4 k % Time step
5 t % Time point
6 v % Solution vector
7 n % Time level
8 rkScheme % The particular RK scheme used for time integration
9 RV % Residual Viscosity operator
10 DvDt % Function for computing the time deriative used for the RV evaluation
11 end
12 methods
13
14 function obj = RungekuttaRv(F, k, t0, v0, RV, DvDt, order)
15 obj.F = F;
16 obj.k = k;
17 obj.t = t0;
18 obj.v = v0;
19 obj.n = 0;
20
21 if (order == 4) % Use specialized RK4 scheme
22 obj.rkScheme = @time.rk.rungekutta_4;
23 else
24 % Extract the coefficients for the specified order
25 % used for the RK updates from the Butcher tableua.
26 [s,a,b,c] = time.rk.butcherTableau(order);
27 coeffs = struct('s',s,'a',a,'b',b,'c',c);
28 obj.rkScheme = @(v,t,dt,F) time.rk.rungekutta(v, t , dt, F, coeffs);
29 end
30
31 obj.RV = RV;
32 obj.DvDt = DvDt;
33 end
34
35 function [v, t] = getV(obj)
36 v = obj.v;
37 t = obj.t;
38 end
39
40 function state = getState(obj)
41 dvdt = obj.DvDt(obj.v);
42 [viscosity, Df, firstOrderViscosity, residualViscosity] = obj.RV.evaluate(obj.v, dvdt);
43 state = struct('v', obj.v, 'dvdt', dvdt, 'Df', Df, 'viscosity', viscosity, 'residualViscosity', residualViscosity, 'firstOrderViscosity', firstOrderViscosity, 't', obj.t);
44 end
45
46 % Advances the solution vector one time step using the Runge-Kutta method given by
47 % obj.coeffs, using a fixed residual viscosity for the Runge-Kutta substeps
48 function obj = step(obj)
49 % Fix the viscosity of the stabilized RHS
50 m = length(obj.v);
51 F = @(v,t) obj.F(v,t,spdiags(obj.RV.evaluateViscosity(obj.v, obj.DvDt(obj.v)),0,m,m));
52 obj.v = obj.rkScheme(obj.v, obj.t, obj.k, F);
53 obj.t = obj.t + obj.k;
54 obj.n = obj.n + 1;
55 end
56 end
57 end