view +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
line wrap: on
line source

classdef RungekuttaRv < 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
    end
    methods

        function obj = RungekuttaRv(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;
        end

        function [v, t] = getV(obj)
            v = obj.v;
            t = obj.t;
        end

        function state = getState(obj)
            dvdt = obj.DvDt(obj.v);
            [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 stabilized RHS
            m = length(obj.v);
            F = @(v,t) obj.F(v,t,spdiags(obj.RV.evaluateViscosity(obj.v, obj.DvDt(obj.v)),0,m,m));
            obj.v = obj.rkScheme(obj.v, obj.t, obj.k, F);   
            obj.t = obj.t + obj.k;
            obj.n = obj.n + 1;
        end
    end
end