diff +time/Rungekutta.m @ 888:8732d6bd9890 feature/timesteppers

Add general Runge-Kutta class - Add a general Runge-Kutta class which time integrates the solution based on coefficients obtained from a Butcher tableau - Add butcher tableau which returns coefficents for the specified Runge-Kutta method - Remove RungKutta4proper, since obsolete
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Thu, 15 Nov 2018 17:10:01 -0800
parents
children 679f4ddd982f
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+time/Rungekutta.m	Thu Nov 15 17:10:01 2018 -0800
@@ -0,0 +1,46 @@
+classdef Rungekutta < time.Timestepper
+    properties
+        F       % RHS of the ODE
+        k       % Time step
+        t       % Time point
+        v       % Solution vector
+        n       % Time level
+        scheme  % The scheme used for the time stepping, e.g rk4, rk6 etc.
+    end
+
+
+    methods
+        % Timesteps v_t = F(v,t), using the specified RK method from t = t0 with
+        % timestep k and initial conditions v = v0
+        function obj = Rungekutta(F, k, t0, v0, method)
+            default_arg('method',"rk4");
+            obj.F = F;
+            obj.k = k;
+            obj.t = t0;
+            obj.v = v0;
+            obj.n = 0;
+            % 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
+                % Extract the coefficients for the specified method
+                % used for the RK updates from the Butcher tableua.
+                [s,a,b,c] = time.rk.butcherTableau(method);
+                coeffs = struct('s',s,'a',a,'b',b,'c',c);
+                obj.scheme = @(v,t,dt,F) time.rk.rungekutta(v, t , dt, F, coeffs);
+            end
+        end
+
+        function [v,t] = getV(obj)
+            v = obj.v;
+            t = obj.t;
+        end
+
+        function obj = step(obj)
+            obj.v = obj.scheme(obj.v, obj.t, obj.k, obj.F);
+            obj.t = obj.t + obj.k;
+            obj.n = obj.n + 1;
+        end
+    end
+end
\ No newline at end of file