view diracDiscrTest.m @ 753:44c46bd6913a feature/d1_staggered

Fix stage rate bug in RK4 discrete data
author Martin Almquist <malmquist@stanford.edu>
date Fri, 15 Jun 2018 13:02:29 -0700
parents 4ee7d15bd8e6
children 5264ce57b573
line wrap: on
line source

function tests = diracDiscrTest()
	    tests = functiontests(localfunctions);
end

function testLeftGP(testCase)

    orders = [2, 4, 6];
    mom_conds = orders;

    for o = 1:length(orders)
        order = orders(o);
        mom_cond = mom_conds(o);
        [xl, xr, m, h, x, H, fs] = setupStuff(order, mom_cond);

        % Test left boundary grid points
        x0s = xl + [0, h, 2*h];

        for j = 1:length(fs)
                f = fs{j};
                fx = f(x);
            for i = 1:length(x0s)
                x0 = x0s(i);
                delta = diracDiscr(x0, x, mom_cond, 0, H);
                integral = delta'*H*fx;
                err = abs(integral - f(x0));
                testCase.verifyLessThan(err, 1e-12);
            end
        end
    end
end

function testLeftRandom(testCase)

    orders = [2, 4, 6];
    mom_conds = orders;
    
    for o = 1:length(orders)
        order = orders(o);
        mom_cond = mom_conds(o);
        [xl, xr, m, h, x, H, fs] = setupStuff(order, mom_cond);

        % Test random points near left boundary
        x0s = xl + 2*h*rand(1,10);

        for j = 1:length(fs)
                f = fs{j};
                fx = f(x);
            for i = 1:length(x0s)
                x0 = x0s(i);
                delta = diracDiscr(x0, x, mom_cond, 0, H);
                integral = delta'*H*fx;
                err = abs(integral - f(x0));
                testCase.verifyLessThan(err, 1e-12);
            end
        end
    end
end

function testRightGP(testCase)

    orders = [2, 4, 6];
    mom_conds = orders;
    
    for o = 1:length(orders)
        order = orders(o);
        mom_cond = mom_conds(o);
        [xl, xr, m, h, x, H, fs] = setupStuff(order, mom_cond);

        % Test right boundary grid points
        x0s = xr-[0, h, 2*h];

        for j = 1:length(fs)
                f = fs{j};
                fx = f(x);
            for i = 1:length(x0s)
                x0 = x0s(i);
                delta = diracDiscr(x0, x, mom_cond, 0, H);
                integral = delta'*H*fx;
                err = abs(integral - f(x0));
                testCase.verifyLessThan(err, 1e-12);
            end
        end
    end
end

function testRightRandom(testCase)

    orders = [2, 4, 6];
    mom_conds = orders;
    
    for o = 1:length(orders)
        order = orders(o);
        mom_cond = mom_conds(o);
        [xl, xr, m, h, x, H, fs] = setupStuff(order, mom_cond);

        % Test random points near right boundary
        x0s = xr - 2*h*rand(1,10);

        for j = 1:length(fs)
                f = fs{j};
                fx = f(x);
            for i = 1:length(x0s)
                x0 = x0s(i);
                delta = diracDiscr(x0, x, mom_cond, 0, H);
                integral = delta'*H*fx;
                err = abs(integral - f(x0));
                testCase.verifyLessThan(err, 1e-12);
            end
        end
    end
end

function testInteriorGP(testCase)

    orders = [2, 4, 6];
    mom_conds = orders;
    
    for o = 1:length(orders)
        order = orders(o);
        mom_cond = mom_conds(o);
        [xl, xr, m, h, x, H, fs] = setupStuff(order, mom_cond);

        % Test interior grid points
        m_half = round(m/2);
        x0s = xl + (m_half-1:m_half+1)*h;

        for j = 1:length(fs)
                f = fs{j};
                fx = f(x);
            for i = 1:length(x0s)
                x0 = x0s(i);
                delta = diracDiscr(x0, x, mom_cond, 0, H);
                integral = delta'*H*fx;
                err = abs(integral - f(x0));
                testCase.verifyLessThan(err, 1e-12);
            end
        end
    end
end

function testInteriorRandom(testCase)

    orders = [2, 4, 6];
    mom_conds = orders;
    
    for o = 1:length(orders)
        order = orders(o);
        mom_cond = mom_conds(o);
        [xl, xr, m, h, x, H, fs] = setupStuff(order, mom_cond);

        % Test random points in interior
        x0s = (xl+2*h) + (xr-xl-4*h)*rand(1,20);

        for j = 1:length(fs)
                f = fs{j};
                fx = f(x);
            for i = 1:length(x0s)
                x0 = x0s(i);
                delta = diracDiscr(x0, x, mom_cond, 0, H);
                integral = delta'*H*fx;
                err = abs(integral - f(x0));
                testCase.verifyLessThan(err, 1e-12);
            end
        end
    end
end

% x0 outside grid should yield 0 integral!
function testX0OutsideGrid(testCase)

    orders = [2, 4, 6];
    mom_conds = orders;
    
    for o = 1:length(orders)
        order = orders(o);
        mom_cond = mom_conds(o);
        [xl, xr, m, h, x, H, fs] = setupStuff(order, mom_cond);

        % Test points outisde grid
        x0s = [xl-1.1*h, xr+1.1*h];

        for j = 1:length(fs)
                f = fs{j};
                fx = f(x);
            for i = 1:length(x0s)
                x0 = x0s(i);
                delta = diracDiscr(x0, x, mom_cond, 0, H);
                integral = delta'*H*fx;
                err = abs(integral - 0);
                testCase.verifyLessThan(err, 1e-12);
            end
        end
    end
end

function testAllGP(testCase)

    orders = [2, 4, 6];
    mom_conds = orders;
    
    for o = 1:length(orders)
        order = orders(o);
        mom_cond = mom_conds(o);
        [xl, xr, m, h, x, H, fs] = setupStuff(order, mom_cond);

        % Test all grid points
        x0s = x;

        for j = 1:length(fs)
                f = fs{j};
                fx = f(x);
            for i = 1:length(x0s)
                x0 = x0s(i);
                delta = diracDiscr(x0, x, mom_cond, 0, H);
                integral = delta'*H*fx;
                err = abs(integral - f(x0));
                testCase.verifyLessThan(err, 1e-12);
            end
        end
    end
end

function [xl, xr, m, h, x, H, fs] = setupStuff(order, mom_cond)

    % Grid
    xl = -3;
    xr = 2;
    m = 101;
    h = (xr-xl)/(m-1);
    g = grid.equidistant(m, {xl, xr});
    x = g.points();

    % Quadrature
    ops = sbp.D2Standard(m, {xl, xr}, order);
    H = ops.H;

    % Moment conditions
    fs = cell(mom_cond,1);
    for p = 0:mom_cond-1
        fs{p+1} = @(x) x.^p;
    end

end