view +time/CdiffNonlin.m @ 774:66eb4a2bbb72 feature/grids

Remove default scaling of the system. The scaling doens't seem to help actual solutions. One example that fails in the flexural code. With large timesteps the solutions seems to blow up. One particular example is profilePresentation on the tdb_presentation_figures branch with k = 0.0005
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 18 Jul 2018 15:42:52 -0700
parents d1f9dd55a2b0
children b5e5b195da1e
line wrap: on
line source

classdef CdiffNonlin < time.Timestepper
    properties
        D
        E
        S
        k
        t
        v
        v_prev
        n
    end


    methods
        function obj = CdiffNonlin(D, E, S, k, t0,n0, v, v_prev)
            m = size(D(v),1);
            default_arg('E',0);
            default_arg('S',0);

            if isnumeric(S)
                S = @(v,t)S;
            end

            if isnumeric(E)
                E = @(v)E;
            end


            % m = size(D,1);
            % default_arg('E',sparse(m,m));
            % default_arg('S',sparse(m,1));

            obj.D = D;
            obj.E = E;
            obj.S = S;
            obj.k = k;
            obj.t = t0;
            obj.n = n0;
            obj.v = v;
            obj.v_prev = v_prev;
        end

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

        function [vt,t] = getVt(obj)
            vt = (obj.v-obj.v_prev)/obj.k; % Could be improved using u_tt = f(u))
            t = obj.t;
        end

        function obj = step(obj)
            D = obj.D(obj.v);
            E = obj.E(obj.v);
            S = obj.S(obj.v,obj.t);

            m = size(D,1);
            I = speye(m);

            %% Calculate for which indices we need to solve system of equations
            [rows,cols] = find(E);
            j = union(rows,cols);
            i = setdiff(1:m,j);


            %% Calculate matrices need for the timestep
            % Before optimization:  A =  1/k^2 * I - 1/(2*k)*E;
            k = obj.k;

            Aj = 1/k^2 * I(j,j) - 1/(2*k)*E(j,j);
            B =  2/k^2 * I + D;
            C = -1/k^2 * I - 1/(2*k)*E;

            %% Take the timestep
            v = obj.v;
            v_prev = obj.v_prev;

            % Want to solve the seq A*v_next = b where
            b = (B*v + C*v_prev + S);

            % Before optimization:  obj.v = A\b;

            obj.v(i) = k^2*b(i);
            obj.v(j) =  Aj\b(j);

            obj.v_prev = v;

            %% Update state of the timestepper
            obj.t = obj.t + obj.k;
            obj.n = obj.n + 1;
        end
    end
end