view +time/Rungekutta.m @ 918:679f4ddd982f feature/timesteppers

Add properties for stage approximations and stage rates in the Rungekutta class.
author Martin Almquist <malmquist@stanford.edu>
date Mon, 26 Nov 2018 16:23:27 -0800
parents 8732d6bd9890
children 384ca2331a12
line wrap: on
line source

classdef Rungekutta < time.Timestepper
    properties
        F       % RHS of the ODE
        dt      % Time step
        t       % Time point
        v       % Solution vector
        n       % Time level
        scheme  % The scheme used for the time stepping, e.g rk4, rk6 etc.
        coeffs  % Butcher tableau coefficients
        V       % All stage approximations in most recent time step
        K       % All stage rates in most recent time step
    end


    methods
        % Timesteps v_t = F(v,t), using the specified RK method from t = t0 with
        % timestep dt and initial conditions v = v0
        function obj = Rungekutta(F, dt, t0, v0, method)
            default_arg('method',"rk4");
            obj.F = F;
            obj.dt = dt;
            obj.t = t0;
            obj.v = v0;
            obj.n = 0;

            % Extract the coefficients for the specified method
            % used for the RK updates from the Butcher tableua.
            [s,a,b,c] = time.rk.butcherTableau(method);
            obj.coeffs = struct('s',s,'a',a,'b',b,'c',c);

            % TODO: method "rk4" is also implemented in the butcher tableau, but the rungekutta_4.m implementation
            % might be slightly more efficient. Need to do some profiling before deciding whether or not to keep it.
            if (method == "rk4")
                obj.scheme = @time.rk.rungekutta_4;
            else
                obj.scheme = @(v,t,dt,F) time.rk.rungekutta(v, t , dt, F, obj.coeffs);
            end
        end

        % v: Current solution
        % t: Current time
        % V: All stage approximations in most recent time step
        % K: All stage rates in most recent time step
        % T: Time points (corresponding to V and K) in most recent time step
        function [v,t,V,T,K] = getV(obj)
            v = obj.v;
            t = obj.t;
            V = obj.V;
            K = obj.K;
            T = obj.t + obj.dt*obj.coeffs.b;
        end

        function obj = step(obj)
            [obj.v, obj.V, obj.K] = obj.scheme(obj.v, obj.t, obj.dt, obj.F);
            obj.t = obj.t + obj.dt;
            obj.n = obj.n + 1;
        end
    end
end