annotate diracDiscrTest.m @ 1299:73e52c74baac feature/boundary_optimized_grids

Closed branch feature/boundary_optimized_grids
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Wed, 08 Jul 2020 18:20:57 +0200
parents 25efceb0c392
children 60c875c18de3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
1 function tests = diracDiscrTest()
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
2 tests = functiontests(localfunctions);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
3 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
4
1243
4e0b88f3def1 Use rng(1) to set seed in diracDiscrTest because hex-speak crashed.
Martin Almquist <malmquist@stanford.edu>
parents: 1234
diff changeset
5 %TODO:
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
6 % 1. Test discretizing with smoothness conditions.
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
7 % Only discretization with moment conditions currently tested.
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
8 % 2. Test using other types of grids. Only equidistant grids currently used.
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
9
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
10 function testLeftRandom(testCase)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
11
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
12 orders = [2, 4, 6];
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
13 mom_conds = orders;
1243
4e0b88f3def1 Use rng(1) to set seed in diracDiscrTest because hex-speak crashed.
Martin Almquist <malmquist@stanford.edu>
parents: 1234
diff changeset
14 rng(1) % Set seed
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
15
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
16 for o = 1:length(orders)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
17 order = orders(o);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
18 mom_cond = mom_conds(o);
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
19 [g, H, fs] = setup1D(order, mom_cond);
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
20 xl = g.lim{1}{1};
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
21 h = g.scaling();
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
22 x = g.x{1};
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
23
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
24 % Test random points near left boundary
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
25 x0s = xl + 2*h*rand(1,10);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
26
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
27 for j = 1:length(fs)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
28 f = fs{j};
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
29 fx = f(x);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
30 for i = 1:length(x0s)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
31 x0 = x0s(i);
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
32 delta = diracDiscr(g, x0, mom_cond, 0, H);
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
33 integral = delta'*H*fx;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
34 err = abs(integral - f(x0));
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
35 testCase.verifyLessThan(err, 1e-12);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
36 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
37 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
38 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
39 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
40
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
41 function testRightRandom(testCase)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
42
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
43 orders = [2, 4, 6];
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
44 mom_conds = orders;
1243
4e0b88f3def1 Use rng(1) to set seed in diracDiscrTest because hex-speak crashed.
Martin Almquist <malmquist@stanford.edu>
parents: 1234
diff changeset
45 rng(1) % Set seed
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
46
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
47 for o = 1:length(orders)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
48 order = orders(o);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
49 mom_cond = mom_conds(o);
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
50 [g, H, fs] = setup1D(order, mom_cond);
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
51 xr = g.lim{1}{2};
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
52 h = g.scaling();
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
53 x = g.x{1};
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
54
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
55 % Test random points near right boundary
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
56 x0s = xr - 2*h*rand(1,10);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
57
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
58 for j = 1:length(fs)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
59 f = fs{j};
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
60 fx = f(x);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
61 for i = 1:length(x0s)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
62 x0 = x0s(i);
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
63 delta = diracDiscr(g, x0, mom_cond, 0, H);
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
64 integral = delta'*H*fx;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
65 err = abs(integral - f(x0));
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
66 testCase.verifyLessThan(err, 1e-12);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
67 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
68 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
69 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
70 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
71
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
72 function testInteriorRandom(testCase)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
73
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
74 orders = [2, 4, 6];
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
75 mom_conds = orders;
1243
4e0b88f3def1 Use rng(1) to set seed in diracDiscrTest because hex-speak crashed.
Martin Almquist <malmquist@stanford.edu>
parents: 1234
diff changeset
76 rng(1) % Set seed
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
77
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
78 for o = 1:length(orders)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
79 order = orders(o);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
80 mom_cond = mom_conds(o);
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
81 [g, H, fs] = setup1D(order, mom_cond);
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
82 xl = g.lim{1}{1};
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
83 xr = g.lim{1}{2};
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
84 h = g.scaling();
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
85 x = g.x{1};
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
86
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
87 % Test random points in interior
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
88 x0s = (xl+2*h) + (xr-xl-4*h)*rand(1,20);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
89
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
90 for j = 1:length(fs)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
91 f = fs{j};
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
92 fx = f(x);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
93 for i = 1:length(x0s)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
94 x0 = x0s(i);
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
95 delta = diracDiscr(g, x0, mom_cond, 0, H);
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
96 integral = delta'*H*fx;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
97 err = abs(integral - f(x0));
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
98 testCase.verifyLessThan(err, 1e-12);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
99 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
100 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
101 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
102 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
103
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
104 % x0 outside grid should yield 0 integral!
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
105 function testX0OutsideGrid(testCase)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
106
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
107 orders = [2, 4, 6];
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
108 mom_conds = orders;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
109
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
110 for o = 1:length(orders)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
111 order = orders(o);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
112 mom_cond = mom_conds(o);
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
113 [g, H, fs] = setup1D(order, mom_cond);
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
114 xl = g.lim{1}{1};
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
115 xr = g.lim{1}{2};
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
116 h = g.scaling();
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
117 x = g.x{1};
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
118
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
119 % Test points outisde grid
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
120 x0s = [xl-1.1*h, xr+1.1*h];
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
121
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
122 for j = 1:length(fs)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
123 f = fs{j};
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
124 fx = f(x);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
125 for i = 1:length(x0s)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
126 x0 = x0s(i);
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
127 delta = diracDiscr(g, x0, mom_cond, 0, H);
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
128 integral = delta'*H*fx;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
129 err = abs(integral - 0);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
130 testCase.verifyLessThan(err, 1e-12);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
131 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
132 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
133 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
134 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
135
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
136 function testAllGP(testCase)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
137
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
138 orders = [2, 4, 6];
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
139 mom_conds = orders;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
140
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
141 for o = 1:length(orders)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
142 order = orders(o);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
143 mom_cond = mom_conds(o);
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
144 [g, H, fs] = setup1D(order, mom_cond);
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
145 x = g.x{1};
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
146
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
147 % Test all grid points
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
148 x0s = x;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
149
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
150 for j = 1:length(fs)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
151 f = fs{j};
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
152 fx = f(x);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
153 for i = 1:length(x0s)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
154 x0 = x0s(i);
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
155 delta = diracDiscr(g, x0, mom_cond, 0, H);
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
156 integral = delta'*H*fx;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
157 err = abs(integral - f(x0));
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
158 testCase.verifyLessThan(err, 1e-12);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
159 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
160 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
161 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
162 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
163
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
164 function testHalfGP(testCase)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
165
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
166 orders = [2, 4, 6];
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
167 mom_conds = orders;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
168
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
169 for o = 1:length(orders)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
170 order = orders(o);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
171 mom_cond = mom_conds(o);
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
172 [g, H, fs] = setup1D(order, mom_cond);
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
173 x = g.x{1};
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
174
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
175 % Test halfway between all grid points
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
176 x0s = 1/2*(x(2:end)+x(1:end-1));
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
177
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
178 for j = 1:length(fs)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
179 f = fs{j};
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
180 fx = f(x);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
181 for i = 1:length(x0s)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
182 x0 = x0s(i);
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
183 delta = diracDiscr(g, x0, mom_cond, 0, H);
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
184 integral = delta'*H*fx;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
185 err = abs(integral - f(x0));
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
186 testCase.verifyLessThan(err, 1e-12);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
187 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
188 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
189 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
190 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
191
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
192 %=============== 2D tests ==============================
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
193 function testAllGP2D(testCase)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
194
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
195 orders = [2, 4, 6];
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
196 mom_conds = orders;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
197
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
198 for o = 1:length(orders)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
199 order = orders(o);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
200 mom_cond = mom_conds(o);
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
201 [g, H, fs] = setup2D(order, mom_cond);
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
202 H_global = kron(H{1}, H{2});
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
203 X = g.points();
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
204 % Test all grid points
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
205 x0s = X;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
206
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
207 for j = 1:length(fs)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
208 f = fs{j};
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
209 fx = f(X(:,1), X(:,2));
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
210 for i = 1:length(x0s)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
211 x0 = x0s(i,:);
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
212 delta = diracDiscr(g, x0, mom_cond, 0, H);
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
213 integral = delta'*H_global*fx;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
214 err = abs(integral - f(x0(1), x0(2)));
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
215 testCase.verifyLessThan(err, 1e-12);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
216 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
217 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
218 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
219 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
220
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
221 function testAllRandom2D(testCase)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
222
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
223 orders = [2, 4, 6];
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
224 mom_conds = orders;
1243
4e0b88f3def1 Use rng(1) to set seed in diracDiscrTest because hex-speak crashed.
Martin Almquist <malmquist@stanford.edu>
parents: 1234
diff changeset
225 rng(1) % Set seed
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
226
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
227 for o = 1:length(orders)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
228 order = orders(o);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
229 mom_cond = mom_conds(o);
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
230 [g, H, fs] = setup2D(order, mom_cond);
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
231 H_global = kron(H{1}, H{2});
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
232 X = g.points();
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
233 xl = g.lim{1}{1};
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
234 xr = g.lim{1}{2};
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
235 yl = g.lim{2}{1};
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
236 yr = g.lim{2}{2};
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
237 h = g.scaling();
1243
4e0b88f3def1 Use rng(1) to set seed in diracDiscrTest because hex-speak crashed.
Martin Almquist <malmquist@stanford.edu>
parents: 1234
diff changeset
238
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
239
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
240 % Test random points, even outside grid
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
241 Npoints = 100;
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
242 x0s = [(xl-3*h(1)) + (xr-xl+6*h(1))*rand(Npoints,1), ...
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
243 (yl-3*h(2)) + (yr-yl+6*h(2))*rand(Npoints,1) ];
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
244
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
245 for j = 1:length(fs)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
246 f = fs{j};
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
247 fx = f(X(:,1), X(:,2));
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
248 for i = 1:length(x0s)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
249 x0 = x0s(i,:);
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
250 delta = diracDiscr(g, x0, mom_cond, 0, H);
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
251 integral = delta'*H_global*fx;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
252
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
253 % Integral should be 0 if point is outside grid
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
254 if x0(1) < xl || x0(1) > xr || x0(2) < yl || x0(2) > yr
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
255 err = abs(integral - 0);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
256 else
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
257 err = abs(integral - f(x0(1), x0(2)));
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
258 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
259 testCase.verifyLessThan(err, 1e-12);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
260 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
261 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
262 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
263 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
264
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
265 %=============== 3D tests ==============================
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
266 function testAllGP3D(testCase)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
267
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
268 orders = [2, 4, 6];
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
269 mom_conds = orders;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
270
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
271 for o = 1:length(orders)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
272 order = orders(o);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
273 mom_cond = mom_conds(o);
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
274 [g, H, fs] = setup3D(order, mom_cond);
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
275 H_global = kron(kron(H{1}, H{2}), H{3});
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
276 X = g.points();
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
277 % Test all grid points
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
278 x0s = X;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
279
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
280 for j = 1:length(fs)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
281 f = fs{j};
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
282 fx = f(X(:,1), X(:,2), X(:,3));
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
283 for i = 1:length(x0s)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
284 x0 = x0s(i,:);
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
285 delta = diracDiscr(g, x0, mom_cond, 0, H);
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
286 integral = delta'*H_global*fx;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
287 err = abs(integral - f(x0(1), x0(2), x0(3)));
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
288 testCase.verifyLessThan(err, 1e-12);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
289 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
290 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
291 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
292 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
293
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
294 function testAllRandom3D(testCase)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
295
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
296 orders = [2, 4, 6];
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
297 mom_conds = orders;
1243
4e0b88f3def1 Use rng(1) to set seed in diracDiscrTest because hex-speak crashed.
Martin Almquist <malmquist@stanford.edu>
parents: 1234
diff changeset
298 rng(1) % Set seed
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
299
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
300 for o = 1:length(orders)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
301 order = orders(o);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
302 mom_cond = mom_conds(o);
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
303 [g, H, fs] = setup3D(order, mom_cond);
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
304 H_global = kron(kron(H{1}, H{2}), H{3});
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
305 X = g.points();
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
306 xl = g.lim{1}{1};
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
307 xr = g.lim{1}{2};
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
308 yl = g.lim{2}{1};
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
309 yr = g.lim{2}{2};
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
310 zl = g.lim{3}{1};
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
311 zr = g.lim{3}{2};
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
312 h = g.scaling();
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
313
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
314 % Test random points, even outside grid
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
315 Npoints = 200;
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
316 x0s = [(xl-3*h(1)) + (xr-xl+6*h(1))*rand(Npoints,1), ...
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
317 (yl-3*h(2)) + (yr-yl+6*h(2))*rand(Npoints,1), ...
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
318 (zl-3*h(3)) + (zr-zl+6*h(3))*rand(Npoints,1) ];
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
319
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
320 for j = 1:length(fs)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
321 f = fs{j};
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
322 fx = f(X(:,1), X(:,2), X(:,3));
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
323 for i = 1:length(x0s)
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
324 x0 = x0s(i,:);
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
325 delta = diracDiscr(g, x0, mom_cond, 0, H);
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
326 integral = delta'*H_global*fx;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
327
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
328 % Integral should be 0 if point is outside grid
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
329 if x0(1) < xl || x0(1) > xr || x0(2) < yl || x0(2) > yr || x0(3) < zl || x0(3) > zr
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
330 err = abs(integral - 0);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
331 else
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
332 err = abs(integral - f(x0(1), x0(2), x0(3)));
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
333 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
334 testCase.verifyLessThan(err, 1e-12);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
335 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
336 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
337 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
338 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
339
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
340
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
341 % ======================================================
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
342 % ============== Setup functions =======================
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
343 % ======================================================
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
344 function [g, H, fs] = setup1D(order, mom_cond)
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
345
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
346 % Grid
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
347 xl = -3;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
348 xr = 900;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
349 L = xr-xl;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
350 m = 101;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
351 g = grid.equidistant(m, {xl, xr});
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
352
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
353 % Quadrature
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
354 ops = sbp.D2Standard(m, {xl, xr}, order);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
355 H = ops.H;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
356
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
357 % Moment conditions
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
358 fs = cell(mom_cond,1);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
359 for p = 0:mom_cond-1
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
360 fs{p+1} = @(x) (x/L).^p;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
361 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
362
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
363 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
364
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
365 function [g, H, fs] = setup2D(order, mom_cond)
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
366
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
367 % Grid
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
368 xlims = {-3, 20};
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
369 ylims = {-11,5};
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
370 Lx = xlims{2} - xlims{1};
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
371 Ly = ylims{2} - ylims{1};
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
372 m = [15, 16];
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
373 g = grid.equidistant(m, xlims, ylims);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
374
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
375 % Quadrature
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
376 opsx = sbp.D2Standard(m(1), xlims, order);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
377 opsy = sbp.D2Standard(m(2), ylims, order);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
378 Hx = opsx.H;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
379 Hy = opsy.H;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
380 H = {Hx, Hy};
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
381
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
382 % Moment conditions
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
383 fs = cell(mom_cond,1);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
384 for p = 0:mom_cond-1
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
385 fs{p+1} = @(x,y) (x/Lx + y/Ly).^p;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
386 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
387 end
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
388
1234
f1806475498b - Pass grids to diracDiscr and adjust tests.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 1232
diff changeset
389 function [g, H, fs] = setup3D(order, mom_cond)
1229
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
390
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
391 % Grid
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
392 xlims = {-3, 20};
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
393 ylims = {-11,5};
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
394 zlims = {2,4};
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
395 Lx = xlims{2} - xlims{1};
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
396 Ly = ylims{2} - ylims{1};
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
397 Lz = zlims{2} - zlims{1};
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
398 m = [13, 14, 15];
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
399 g = grid.equidistant(m, xlims, ylims, zlims);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
400
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
401 % Quadrature
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
402 opsx = sbp.D2Standard(m(1), xlims, order);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
403 opsy = sbp.D2Standard(m(2), ylims, order);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
404 opsz = sbp.D2Standard(m(3), zlims, order);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
405 Hx = opsx.H;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
406 Hy = opsy.H;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
407 Hz = opsz.H;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
408 H = {Hx, Hy, Hz};
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
409
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
410 % Moment conditions
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
411 fs = cell(mom_cond,1);
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
412 for p = 0:mom_cond-1
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
413 fs{p+1} = @(x,y,z) (x/Lx + y/Ly + z/Lz).^p;
86ee5648e384 Add multi-d dirac discretization with tests
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
414 end
1246
25efceb0c392 Apply some nitpicking
Jonatan Werpers <jonatan@werpers.com>
parents: 1243
diff changeset
415 end