Mercurial > repos > public > sbplib
annotate +grid/evalOnTest.m @ 577:e45c9b56d50d feature/grids
Add an Empty grid class
The need turned up for the flexural code when we may or may not have a grid for the open water and want to plot that solution.
In case there is no open water we need an empty grid to plot the empty gridfunction against to avoid errors.
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Thu, 07 Sep 2017 09:16:12 +0200 |
parents | 4c3f55a628c8 |
children | 190941ec12d8 |
rev | line source |
---|---|
157 | 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 | |
277
4c3f55a628c8
Made evalOn give more error message.
Jonatan Werpers <jonatan@werpers.com>
parents:
157
diff
changeset
|
28 % evalOn should give and error if the number of inputs to func is not the same as |
4c3f55a628c8
Made evalOn give more error message.
Jonatan Werpers <jonatan@werpers.com>
parents:
157
diff
changeset
|
29 % the number of dimensions of the grid. |
4c3f55a628c8
Made evalOn give more error message.
Jonatan Werpers <jonatan@werpers.com>
parents:
157
diff
changeset
|
30 function testNumberOfInputs(testCase) |
4c3f55a628c8
Made evalOn give more error message.
Jonatan Werpers <jonatan@werpers.com>
parents:
157
diff
changeset
|
31 cases = { |
4c3f55a628c8
Made evalOn give more error message.
Jonatan Werpers <jonatan@werpers.com>
parents:
157
diff
changeset
|
32 {getTestGrid('1d'), @(x,y)x-y}, |
4c3f55a628c8
Made evalOn give more error message.
Jonatan Werpers <jonatan@werpers.com>
parents:
157
diff
changeset
|
33 {getTestGrid('2d'), @(x)x }, |
4c3f55a628c8
Made evalOn give more error message.
Jonatan Werpers <jonatan@werpers.com>
parents:
157
diff
changeset
|
34 } |
4c3f55a628c8
Made evalOn give more error message.
Jonatan Werpers <jonatan@werpers.com>
parents:
157
diff
changeset
|
35 |
4c3f55a628c8
Made evalOn give more error message.
Jonatan Werpers <jonatan@werpers.com>
parents:
157
diff
changeset
|
36 for i = 1:length(cases) |
4c3f55a628c8
Made evalOn give more error message.
Jonatan Werpers <jonatan@werpers.com>
parents:
157
diff
changeset
|
37 g = cases{i}{1}; |
4c3f55a628c8
Made evalOn give more error message.
Jonatan Werpers <jonatan@werpers.com>
parents:
157
diff
changeset
|
38 f = cases{i}{2}; |
4c3f55a628c8
Made evalOn give more error message.
Jonatan Werpers <jonatan@werpers.com>
parents:
157
diff
changeset
|
39 testCase.verifyError(@()grid.evalOn(g, f),'grid:evalOn:WrongNumberOfInputs',sprintf('in(%d) = %s',i,toString(f))); |
4c3f55a628c8
Made evalOn give more error message.
Jonatan Werpers <jonatan@werpers.com>
parents:
157
diff
changeset
|
40 end |
4c3f55a628c8
Made evalOn give more error message.
Jonatan Werpers <jonatan@werpers.com>
parents:
157
diff
changeset
|
41 end |
4c3f55a628c8
Made evalOn give more error message.
Jonatan Werpers <jonatan@werpers.com>
parents:
157
diff
changeset
|
42 |
157 | 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 |