comparison +grid/evalOnTest.m @ 886:8894e9c49e40 feature/timesteppers

Merge with default for latest changes
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Thu, 15 Nov 2018 16:36:21 -0800
parents 190941ec12d8
children
comparison
equal deleted inserted replaced
816:b5e5b195da1e 886:8894e9c49e40
1 function tests = evalOnTest()
2 tests = functiontests(localfunctions);
3 end
4
5 function testInputConstant(testCase)
6 in = {
7 0,
8 47,
9 1,
10 [1; 2],
11 };
12
13 out = {
14 [0; 0; 0],
15 [47; 47; 47],
16 [1; 1; 1],
17 [1; 2; 1; 2; 1; 2],
18 };
19
20 g = getTestGrid('1d');
21
22 for i = 1:length(in)
23 gf = grid.evalOn(g,in{i});
24 testCase.verifyEqual(gf, out{i});
25 end
26 end
27
28 % evalOn should give and error if the number of inputs to func is not the same as
29 % the number of dimensions of the grid.
30 function testNumberOfInputs(testCase)
31 cases = {
32 {getTestGrid('1d'), @(x,y)x-y},
33 {getTestGrid('2d'), @(x)x },
34 };
35
36 for i = 1:length(cases)
37 g = cases{i}{1};
38 f = cases{i}{2};
39 testCase.verifyError(@()grid.evalOn(g, f),'grid:evalOn:WrongNumberOfInputs',sprintf('in(%d) = %s',i,toString(f)));
40 end
41 end
42
43 function testInputScalarFunction1d(testCase)
44 in = {
45 @(x)1+x*0,
46 @(x)x,
47 @(x)x.*x,
48 };
49
50 out = {
51 [1; 1; 1],
52 [0; 1; 2],
53 [0; 1; 4],
54 };
55
56 g = getTestGrid('1d');
57
58 for i = 1:length(in)
59 gf = grid.evalOn(g,in{i});
60 testCase.verifyEqual(gf, out{i});
61 end
62 end
63
64 function testInputScalarFunction2d(testCase)
65 in = {
66 @(x,y)1+x*0,
67 @(x,y)x-y,
68 @(x,y)x./(1+y),
69 };
70
71 out = {
72 [1; 1; 1; 1; 1; 1; 1; 1; 1],
73 [0; -1; -2; 1; 0; -1; 2; 1; 0],
74 [0; 0; 0; 1; 1/2; 1/3; 2; 1; 2/3],
75 };
76
77 g = getTestGrid('2d');
78
79 for i = 1:length(in)
80 gf = grid.evalOn(g, in{i});
81 testCase.verifyEqual(gf, out{i});
82 end
83 end
84
85
86 function testInputVectorFunction(testCase)
87 g = getTestGrid('1d');
88 in = @(x)[x; -2*x];
89 out = [0; 0; 1; -2; 2; -4];
90
91 gf = grid.evalOn(g,in);
92 testCase.verifyEqual(gf, out);
93
94 g = getTestGrid('2d');
95 in = @(x,y)[x.^2; -2*y];
96 out = [
97 0; 0;
98 0; -2;
99 0; -4;
100 1; 0;
101 1; -2;
102 1; -4;
103 4; 0;
104 4; -2;
105 4; -4;
106 ];
107
108 gf = grid.evalOn(g,in);
109 testCase.verifyEqual(gf, out);
110 end
111
112
113 function testInputErrorVectorValued(testCase)
114 in = {
115 [1,2,3],
116 @(x,y)[x,-y],
117 };
118
119 g = getTestGrid('2d');
120
121 for i = 1:length(in)
122 testCase.verifyError(@()grid.evalOn(g, in{i}),'grid:evalOn:VectorValuedWrongDim',sprintf('in(%d) = %s',i,toString(in{i})));
123 end
124 end
125
126 function g = getTestGrid(d)
127 switch d
128 case '1d'
129 g = grid.equidistant(3,{0,2});
130 case '2d'
131 g = grid.equidistant([3,3],{0,2},{0,2});
132 end
133 end