classdef RungekuttaExteriorRvBdf < 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
        
        % Properties related to the residual viscositys
        RV              % Residual Viscosity operator
        v_prev          % Solution vector at previous time levels, used for the RV evaluation
        DvDt            % Function for computing the time deriative used for the RV evaluation
        lowerBdfOrder   % Orders of the approximation of the time deriative, used for the RV evaluation.
                        % dictates which accuracy the boot-strapping should start from.
        upperBdfOrder   % Orders of the approximation of the time deriative, used for the RV evaluation.
                        % Dictates the order of accuracy used once the boot-strapping is complete.
    end
    methods
        function obj = RungekuttaExteriorRvBdf(F, k, t0, v0, RV, rkOrder, bdfOrders)
            obj.F = F;
            obj.k = k;
            obj.t = t0;
            obj.v = v0;
            obj.n = 0;
            % Extract the coefficients for the specified rkOrder
            % used for the RK updates from the Butcher tableua.
            [s,a,b,c] = time.rk.butcherTableau(rkOrder);
            obj.coeffs = struct('s',s,'a',a,'b',b,'c',c);
        
            obj.RV = RV;
            obj.lowerBdfOrder = bdfOrders.lowerBdfOrder;
            obj.upperBdfOrder = bdfOrders.upperBdfOrder;
            assert((obj.lowerBdfOrder >= 1) && (obj.upperBdfOrder <= 6));
            obj.v_prev = [];
            obj.DvDt = rv.time.BdfDerivative();
        end

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

        function state = getState(obj)
            if (size(obj.v_prev,2) >=  obj.lowerBdfOrder)
                dvdt = obj.DvDt.evaluate(obj.v, obj.v_prev, obj.k);
                [viscosity, Df, firstOrderViscosity, residualViscosity] = obj.RV.evaluate(obj.v, dvdt);
            else
                viscosity = zeros(size(obj.v));
                dvdt = zeros(size(obj.v));
                Df = zeros(size(obj.v));
                firstOrderViscosity = zeros(size(obj.v));
                residualViscosity = zeros(size(obj.v));
            end
            state = struct('v', obj.v, 'dvdt', dvdt, 'Df', Df, 'viscosity', viscosity, 'residualViscosity', residualViscosity, 'firstOrderViscosity', firstOrderViscosity, 't', obj.t);
        end

        function obj = step(obj)
            nStoredStages = size(obj.v_prev,2);

            %Calculate viscosity for the new time level
            if (nStoredStages >=  obj.lowerBdfOrder)
                viscosity = obj.RV.evaluateViscosity(obj.v, obj.DvDt.evaluate(obj.v, obj.v_prev, obj.k));
            else
                viscosity = zeros(size(obj.v));
            end

             % Store current time level and update v_prev
            if (nStoredStages < obj.upperBdfOrder)
                obj.v_prev = [obj.v, obj.v_prev];
            else
                obj.v_prev(:,2:end) = obj.v_prev(:,1:end-1);
                obj.v_prev(:,1) = obj.v;
            end

            % Fix the viscosity of the RHS function F
            F_visc = @(v,t) obj.F(v, t, viscosity);
            obj.v = time.rk.rungekutta(obj.v, obj.t, obj.k, F_visc, obj.coeffs);
            obj.t = obj.t + obj.k;
            obj.n = obj.n + 1;
        end
    end
end