Mercurial > repos > public > sbplib
view diracDiscrTest.m @ 754:5264ce57b573 feature/d1_staggered
Bugfix diracDiscr to make it work for grids that are non-equidistant near boundaries.
author | Martin Almquist <malmquist@stanford.edu> |
---|---|
date | Fri, 15 Jun 2018 14:01:13 -0700 |
parents | 4ee7d15bd8e6 |
children | c70131daaa6e |
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 testHalfGP(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 halfway between all grid points x0s = 1/2*( x(2:end)+x(1:end-1) ); 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 testAllGPStaggered(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] = setupStaggered(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 testHalfGPStaggered(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] = setupStaggered(order, mom_cond); % Test halfway between all grid points x0s = 1/2*( x(2:end)+x(1:end-1) ); 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 testRandomStaggered(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] = setupStaggered(order, mom_cond); % Test random points within grid boundaries x0s = xl + (xr-xl)*rand(1,300); 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 % ============== Setup functions ======================= function [xl, xr, m, h, x, H, fs] = setupStuff(order, mom_cond) % Grid xl = -3; xr = 900; L = xr-xl; 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/L).^p; end end function [xl, xr, m, h, x, H, fs] = setupStaggered(order, mom_cond) % Grid xl = -3; xr = 900; L = xr-xl; m = 101; [~, g_dual] = grid.primalDual1D(m, {xl, xr}); x = g_dual.points(); h = g_dual.h; % Quadrature ops = sbp.D1Staggered(m, {xl, xr}, order); H = ops.H_dual; % Moment conditions fs = cell(mom_cond,1); for p = 0:mom_cond-1 fs{p+1} = @(x) (x/L).^p; end end