classdef RungekuttaInteriorRv < time.Timestepper
    properties
        F       % RHS of the ODE
        k       % Time step
        t       % Time point
        v       % Solution vector
        n       % Time level
        coeffs  % The coefficents used for the RK time integration
        RV      % Residual Viscosity
        viscosity       % Viscosity vector
        DvDt            % Function for computing the time deriative used for the RV evaluation

        residual
        dvdt
        Df
    end

    methods
        function obj = RungekuttaInteriorRv(F, k, t0, v0, RV, DvDt, order)
            obj.F = F;
            obj.k = k;
            obj.t = t0;
            obj.v = v0;
            obj.n = 0;
            % Extract the coefficients for the specified order
            % used for the RK updates from the Butcher tableua.
            [s,a,b,c] = time.rk.butcherTableau(order);
            obj.coeffs = struct('s',s,'a',a,'b',b,'c',c);
            obj.RV = RV;
            obj.DvDt = DvDt;
            obj.dvdt = obj.DvDt(obj.v);
            [obj.viscosity, obj.Df] = obj.RV.evaluate(obj.v,obj.dvdt);
            obj.residual = obj.dvdt + obj.Df;
        end

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

        function state = getState(obj)
            state = struct('v', obj.v, 'residual', obj.residual, 'dvdt', obj.dvdt, 'Df', obj.Df, 'viscosity', obj.viscosity, 't', obj.t);
        end

        function obj = step(obj)
            obj.v = rv.time.rungekuttaRV(obj.v, obj.t, obj.k, obj.F, obj.RV, obj.DvDt, obj.coeffs);
            obj.t = obj.t + obj.k;
            obj.n = obj.n + 1;
            obj.dvdt = obj.DvDt(obj.v);
            [obj.viscosity, obj.Df] = obj.RV.evaluate(obj.v,obj.dvdt);
            obj.residual = obj.dvdt + obj.Df;
        end
    end
end