Mercurial > repos > public > sbplib
changeset 1160:76e3bb7836cf feature/rv
First attempt at multi-grid calculation of the residual
author | Vidar Stiernström <vidar.stiernstrom@it.uu.se> |
---|---|
date | Tue, 25 Jun 2019 16:52:54 +0200 |
parents | 1ad7da049b50 |
children | 856bd6291d17 |
files | +rv/+time/RungekuttaExteriorRvMg.m +rv/constructDiffOps.m |
diffstat | 2 files changed, 66 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+rv/+time/RungekuttaExteriorRvMg.m Tue Jun 25 16:52:54 2019 +0200 @@ -0,0 +1,64 @@ +classdef RungekuttaExteriorRvMg < time.Timestepper + properties + F % RHS of the ODE + k % Time step + t % Time point + v % Solution vector + n % Time level + rkScheme % The particular RK scheme used for time integration + RV % Residual Viscosity operator + DvDt % Function for computing the time deriative used for the RV evaluation + v_unstable + viscosity + end + methods + + function obj = RungekuttaExteriorRvMg(F, k, t0, v0, RV, DvDt, order) + obj.F = F; + obj.k = k; + obj.t = t0; + obj.v = v0; + obj.n = 0; + + if (order == 4) % Use specialized RK4 scheme + obj.rkScheme = @time.rk.rungekutta_4; + else + % Extract the coefficients for the specified order + % used for the RK updates from the Butcher tableua. + [s,a,b,c] = time.rk.butcherTableau(order); + coeffs = struct('s',s,'a',a,'b',b,'c',c); + obj.rkScheme = @(v,t,dt,F) time.rk.rungekutta(v, t , dt, F, coeffs); + end + + obj.RV = RV; + obj.DvDt = DvDt; + obj.v_unstable = 0*v0 + obj.viscosity = 0*v0 + end + + function [v, t] = getV(obj) + v = obj.v; + t = obj.t; + end + + function state = getState(obj) + dvdt = obj.DvDt(obj.v_unstable); + [viscosity, Df, firstOrderViscosity, residualViscosity] = obj.RV.evaluate(obj.v, dvdt); + state = struct('v', obj.v, 'dvdt', dvdt, 'Df', Df, 'viscosity', viscosity, 'residualViscosity', residualViscosity, 'firstOrderViscosity', firstOrderViscosity, 't', obj.t); + end + + % Advances the solution vector one time step using the Runge-Kutta method given by + % obj.coeffs, using a fixed residual viscosity for the Runge-Kutta substeps + function obj = step(obj) + % Fix the viscosity of the RHS function F + m = length(obj.viscosity); + F_stable = @(v,t) obj.F(v,t,spdiags(obj.viscosity,0,m,m)); + F_unstable = @(v,t) obj.F(v,t,spdiags(0*obj.viscosity,0,m,m)); + obj.v = obj.rkScheme(obj.v, obj.t, obj.k, F_stable); + obj.v_unstable = obj.rkScheme(obj.v, obj.t, obj.k, F_unstable); + obj.viscosity = obj.RV.evaluateViscosity(obj.v, obj.DvDt(obj.v_unstable)); + obj.t = obj.t + obj.k; + obj.n = obj.n + 1; + end + end +end \ No newline at end of file
--- a/+rv/constructDiffOps.m Tue Jun 25 15:04:53 2019 +0200 +++ b/+rv/constructDiffOps.m Tue Jun 25 16:52:54 2019 +0200 @@ -5,7 +5,7 @@ D_rv = @(v,Viscosity)(D(v) + D2(Viscosity)*v); %% DiffOps for residual viscosity - [D_flux, residualPenalties] = constructFluxDiffOp(scheme, g, residualOrder, schemeParams, opSet, BCs); + [D_res, residualPenalties] = constructFluxDiffOp(scheme, g, residualOrder, schemeParams, opSet, BCs); % TODO: Construct D_flux without closures when using bdfs. % diffOp = scheme(g, residualOrder, schemeParams{:}, opSet); % if ~isa(diffOp.D, 'function_handle') @@ -16,7 +16,7 @@ % DiffOp for flux in residual viscosity. Due to sign conventions of the implemented schemes, we need to % change the sign. - D_flux = @(v) -D_flux(v); + D_flux = @(v) -D_res(v); % DiffOp for time derivative in residual viscosity DvDt = D; end