Mercurial > repos > public > sbplib
changeset 641:070d578997f6 feature/d1_staggered
Add helper function for primal and dual 1D staggered grid, and a test for that function.
author | Martin Almquist <malmquist@stanford.edu> |
---|---|
date | Sat, 11 Nov 2017 20:26:37 -0800 |
parents | 4046b046c1f7 |
children | 8f0ee6ba6313 |
files | +grid/primalDual1D.m +grid/primalDual1DTest.m |
diffstat | 2 files changed, 62 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
diff -r 4046b046c1f7 -r 070d578997f6 +grid/primalDual1D.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+grid/primalDual1D.m Sat Nov 11 20:26:37 2017 -0800 @@ -0,0 +1,30 @@ +% Creates a 1D staggered grid of dimension length(m). +% over the interval xlims +% Primal grid: equidistant with m points. +% Dual grid: m + 1 points, h/2 spacing first point. +% Examples +% g = grid.primal_dual_1D(m, xlim) +% g = grid.primal_dual_1D(11, {0,1}) +function [g_primal, g_dual] = primalDual1D(m, xlims) + + if ~iscell(xlims) || numel(xlims) ~= 2 + error('grid:primalDual1D:InvalidLimits','The limits should be cell arrays with 2 elements.'); + end + + if xlims{1} > xlims{2} + error('grid:primalDual1D:InvalidLimits','The elements of the limit must be increasing.'); + end + + xl = xlims{1}; + xr = xlims{2}; + h = (xr-xl)/(m-1); + + % Primal grid + g_primal = grid.equidistant(m, xlims); + g_primal.h = h; + + % Dual grid + x = [xl; linspace(xl+h/2, xr-h/2, m-1)'; xr]; + g_dual = grid.Cartesian(x); + g_dual.h = h; +end \ No newline at end of file
diff -r 4046b046c1f7 -r 070d578997f6 +grid/primalDual1DTest.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+grid/primalDual1DTest.m Sat Nov 11 20:26:37 2017 -0800 @@ -0,0 +1,32 @@ +function tests = primalDual1DTest() + tests = functiontests(localfunctions); +end + + +function testErrorInvalidLimits(testCase) + in = { + {10,{1}}, + {10,[0,1]}, + {10,{1,0}}, + }; + + for i = 1:length(in) + testCase.verifyError(@()grid.primalDual1D(in{i}{:}),'grid:primalDual1D:InvalidLimits',sprintf('in(%d) = %s',i,toString(in{i}))); + end +end + +function testCompiles(testCase) + in = { + {5, {0,1}}, + }; + + out = { + {[0; 0.25; 0.5; 0.75; 1], [0; 0.125; 0.375; 0.625; 0.875; 1]}, + }; + + for i = 1:length(in) + [gp, gd] = grid.primalDual1D(in{i}{:}); + testCase.verifyEqual(gp.points(),out{i}{1}); + testCase.verifyEqual(gd.points(),out{i}{2}); + end +end