1 | /* |
2 | * grid_util_test.hpp |
3 | * |
4 | * Created on: Jul 18, 2015 |
5 | * Author: Pietro Incardona |
6 | */ |
7 | |
8 | #ifndef SRC_GRID_GRID_UTIL_TEST_HPP_ |
9 | #define SRC_GRID_GRID_UTIL_TEST_HPP_ |
10 | |
11 | #include "map_grid.hpp" |
12 | #include "Point_test.hpp" |
13 | #include "grid_key.hpp" |
14 | #include "Space/Shape/HyperCube.hpp" |
15 | |
16 | /*! \brief Fill the grid with some data |
17 | * |
18 | * \param grid to fill |
19 | * |
20 | */ |
21 | template<unsigned int dim, typename T> void fill_grid(T & grid) |
22 | { |
23 | typedef Point_test<float> P; |
24 | |
25 | auto key_it = grid.getIterator(); |
26 | |
27 | while (key_it.isNext()) |
28 | { |
29 | grid_key_dx<dim> kk = key_it.get(); |
30 | |
31 | grid.template get<P::x>(kk) = grid.getGrid().LinId(kk); |
32 | grid.template get<P::y>(kk) = grid.getGrid().LinId(kk)+1; |
33 | grid.template get<P::z>(kk) = grid.getGrid().LinId(kk)+2; |
34 | grid.template get<P::s>(kk) = grid.getGrid().LinId(kk)+3; |
35 | |
36 | grid.template get<P::v>(kk)[0] = grid.getGrid().LinId(kk)+123; |
37 | grid.template get<P::v>(kk)[1] = grid.getGrid().LinId(kk)+124; |
38 | grid.template get<P::v>(kk)[2] = grid.getGrid().LinId(kk)+125; |
39 | |
40 | grid.template get<P::t>(kk)[0][0] = grid.getGrid().LinId(kk)+567; |
41 | grid.template get<P::t>(kk)[0][1] = grid.getGrid().LinId(kk)+568; |
42 | grid.template get<P::t>(kk)[0][2] = grid.getGrid().LinId(kk)+569; |
43 | grid.template get<P::t>(kk)[1][0] = grid.getGrid().LinId(kk)+570; |
44 | grid.template get<P::t>(kk)[1][1] = grid.getGrid().LinId(kk)+571; |
45 | grid.template get<P::t>(kk)[1][2] = grid.getGrid().LinId(kk)+572; |
46 | grid.template get<P::t>(kk)[2][0] = grid.getGrid().LinId(kk)+573; |
47 | grid.template get<P::t>(kk)[2][1] = grid.getGrid().LinId(kk)+574; |
48 | grid.template get<P::t>(kk)[2][2] = grid.getGrid().LinId(kk)+575; |
49 | |
50 | ++key_it; |
51 | } |
52 | } |
53 | |
54 | template<unsigned int dim, typename g> void test_layout_gridNd(g & c3, size_t sz) |
55 | { |
56 | #ifdef VERBOSE_TEST |
57 | std::cout << dim << "D Array with grid_key (without redundant dimension): " << "\n" ; |
58 | |
59 | timer t; |
60 | t.start(); |
61 | #endif |
62 | |
63 | //! [Access to an N-dimensional grid with an iterator] |
64 | typedef Point_test<float> P; |
65 | |
66 | grid_key_dx_iterator<dim> key_it = c3.getIterator(); |
67 | |
68 | while (key_it.isNext()) |
69 | { |
70 | grid_key_dx<dim> kk = key_it.get(); |
71 | |
72 | c3.template get<P::x>(kk) = 1.1f; |
73 | c3.template get<P::y>(kk) = 1.2f; |
74 | c3.template get<P::z>(kk) = 1.3f; |
75 | c3.template get<P::s>(kk) = 1.0f; |
76 | |
77 | c3.template get<P::v>(kk)[0] = 1.0f; |
78 | c3.template get<P::v>(kk)[1] = 2.0f; |
79 | c3.template get<P::v>(kk)[2] = 3.0f; |
80 | |
81 | c3.template get<P::t>(kk)[0][0] = 1.0f; |
82 | c3.template get<P::t>(kk)[0][1] = 2.0f; |
83 | c3.template get<P::t>(kk)[0][2] = 3.0f; |
84 | c3.template get<P::t>(kk)[1][0] = 4.0f; |
85 | c3.template get<P::t>(kk)[1][1] = 5.0f; |
86 | c3.template get<P::t>(kk)[1][2] = 6.0f; |
87 | c3.template get<P::t>(kk)[2][0] = 7.0f; |
88 | c3.template get<P::t>(kk)[2][1] = 8.0f; |
89 | c3.template get<P::t>(kk)[2][2] = 9.0f; |
90 | |
91 | ++key_it; |
92 | |
93 | } |
94 | //! [Access to an N-dimensional grid with an iterator] |
95 | |
96 | #ifdef VERBOSE_TEST |
97 | t.stop(); |
98 | |
99 | std::cout << "End : " << pow(sz,dim)*16*4/1024/1024 << " MB " << " Bandwidth: " << pow(sz,dim)*16*4/1024/1024/t.getwct() << " MB/s " << "\n" ; |
100 | #endif |
101 | |
102 | /////////////////////////////////// MEM CHECK //////////////////////////////////////////////////////// |
103 | |
104 | bool passed = true; |
105 | |
106 | key_it = c3.getIterator(); |
107 | |
108 | while (key_it.isNext()) |
109 | { |
110 | grid_key_dx<dim> kk = key_it.get(); |
111 | |
112 | c3.template get<P::x>(kk) = c3.getGrid().LinId(kk); |
113 | c3.template get<P::y>(kk) = c3.getGrid().LinId(kk)+1; |
114 | c3.template get<P::z>(kk) = c3.getGrid().LinId(kk)+2; |
115 | c3.template get<P::s>(kk) = c3.getGrid().LinId(kk)+3; |
116 | |
117 | c3.template get<P::v>(kk)[0] = c3.getGrid().LinId(kk)+123; |
118 | c3.template get<P::v>(kk)[1] = c3.getGrid().LinId(kk)+124; |
119 | c3.template get<P::v>(kk)[2] = c3.getGrid().LinId(kk)+125; |
120 | |
121 | c3.template get<P::t>(kk)[0][0] = c3.getGrid().LinId(kk)+567; |
122 | c3.template get<P::t>(kk)[0][1] = c3.getGrid().LinId(kk)+568; |
123 | c3.template get<P::t>(kk)[0][2] = c3.getGrid().LinId(kk)+569; |
124 | c3.template get<P::t>(kk)[1][0] = c3.getGrid().LinId(kk)+570; |
125 | c3.template get<P::t>(kk)[1][1] = c3.getGrid().LinId(kk)+571; |
126 | c3.template get<P::t>(kk)[1][2] = c3.getGrid().LinId(kk)+572; |
127 | c3.template get<P::t>(kk)[2][0] = c3.getGrid().LinId(kk)+573; |
128 | c3.template get<P::t>(kk)[2][1] = c3.getGrid().LinId(kk)+574; |
129 | c3.template get<P::t>(kk)[2][2] = c3.getGrid().LinId(kk)+575; |
130 | |
131 | ++key_it; |
132 | } |
133 | |
134 | |
135 | key_it = c3.getIterator(); |
136 | |
137 | while (key_it.isNext()) |
138 | { |
139 | grid_key_dx<dim> kk = key_it.get(); |
140 | |
141 | if (c3.template get<P::x>(kk) != c3.getGrid().LinId(kk)) passed = false; |
142 | if (c3.template get<P::y>(kk) != c3.getGrid().LinId(kk)+1) passed = false; |
143 | if (c3.template get<P::z>(kk) != c3.getGrid().LinId(kk)+2) passed = false; |
144 | if (c3.template get<P::s>(kk) != c3.getGrid().LinId(kk)+3) passed = false; |
145 | |
146 | if (c3.template get<P::v>(kk)[0] != c3.getGrid().LinId(kk)+123) passed = false; |
147 | if (c3.template get<P::v>(kk)[1] != c3.getGrid().LinId(kk)+124) passed = false; |
148 | if (c3.template get<P::v>(kk)[2] != c3.getGrid().LinId(kk)+125) passed = false; |
149 | |
150 | if (c3.template get<P::t>(kk)[0][0] != c3.getGrid().LinId(kk)+567) passed = false; |
151 | if (c3.template get<P::t>(kk)[0][1] != c3.getGrid().LinId(kk)+568) passed = false; |
152 | if (c3.template get<P::t>(kk)[0][2] != c3.getGrid().LinId(kk)+569) passed = false; |
153 | if (c3.template get<P::t>(kk)[1][0] != c3.getGrid().LinId(kk)+570) passed = false; |
154 | if (c3.template get<P::t>(kk)[1][1] != c3.getGrid().LinId(kk)+571) passed = false; |
155 | if (c3.template get<P::t>(kk)[1][2] != c3.getGrid().LinId(kk)+572) passed = false; |
156 | if (c3.template get<P::t>(kk)[2][0] != c3.getGrid().LinId(kk)+573) passed = false; |
157 | if (c3.template get<P::t>(kk)[2][1] != c3.getGrid().LinId(kk)+574) passed = false; |
158 | if (c3.template get<P::t>(kk)[2][2] != c3.getGrid().LinId(kk)+575) passed = false; |
159 | |
160 | ++key_it; |
161 | } |
162 | |
163 | BOOST_REQUIRE_EQUAL(passed,true); |
164 | |
165 | // Check sub iterator |
166 | |
167 | /* |
168 | * Basically we first fill the interior part of the grid than the borders |
169 | * creating sub iterator always of smaller dimension |
170 | * |
171 | * Example: |
172 | * |
173 | * 2D |
174 | * |
175 | * if we have a square grid 16x16 we first will with 1 the interior 14x14 grid |
176 | * |
177 | * than the 4 line borders 14x1 with one |
178 | * than the 4 point borders 1x1 |
179 | * |
180 | * We check that |
181 | * |
182 | * 1) The number of points for each sub-grid correspond |
183 | * 2) No point is filled more than one time |
184 | * 3) All point are filled |
185 | * |
186 | * we use the property x of c3 |
187 | * |
188 | */ |
189 | |
190 | // Erase the property x |
191 | |
192 | key_it = c3.getIterator(); |
193 | |
194 | while (key_it.isNext()) |
195 | { |
196 | grid_key_dx<dim> kk = key_it.get(); |
197 | |
198 | c3.template get<P::x>(kk) = 0.0; |
199 | |
200 | ++key_it; |
201 | } |
202 | |
203 | for(size_t i = 0 ; i <= dim ; i++) |
204 | { |
205 | // get the combination of dimension dim-i |
206 | std::vector<comb<dim>> combs = HyperCube<dim>::getCombinations_R(dim-i); |
207 | |
208 | // For each combination create a sub iterator |
209 | |
210 | for (size_t j = 0 ; j < combs.size() ; j++) |
211 | { |
212 | // Grid key of the sub-iterator |
213 | |
214 | grid_key_dx<dim> start; |
215 | grid_key_dx<dim> stop; |
216 | |
217 | // sub iterator |
218 | |
219 | for (size_t k = 0 ; k < dim ; k++) |
220 | { |
221 | // if combination is 0 the hyper-cube |
222 | |
223 | if (combs[j].c[k] == -1) |
224 | { |
225 | start.set_d(k,0); |
226 | stop.set_d(k,0); |
227 | } |
228 | else if (combs[j].c[k] == 1) |
229 | { |
230 | start.set_d(k,c3.getGrid().size(k)-1); |
231 | stop.set_d(k,c3.getGrid().size(k)-1); |
232 | } |
233 | else |
234 | { |
235 | start.set_d(k,1); |
236 | stop.set_d(k,c3.getGrid().size(k)-2); |
237 | } |
238 | } |
239 | |
240 | bool make_test = true; |
241 | |
242 | #ifdef SE_CLASS1 |
243 | |
244 | if (c3.size() == 0) |
245 | {make_test = false;} |
246 | |
247 | #endif |
248 | |
249 | if (make_test == true) |
250 | { |
251 | auto key_it = c3.getSubIterator(start,stop); |
252 | |
253 | while (key_it.isNext()) |
254 | { |
255 | grid_key_dx<dim> kk = key_it.get(); |
256 | |
257 | BOOST_REQUIRE_EQUAL(c3.template get<P::x>(kk),0.0); |
258 | |
259 | c3.template get<P::x>(kk) = 1.0; |
260 | |
261 | ++key_it; |
262 | } |
263 | } |
264 | } |
265 | } |
266 | |
267 | // Check that everything is 1.0 |
268 | |
269 | key_it = c3.getIterator(); |
270 | |
271 | while (key_it.isNext()) |
272 | { |
273 | grid_key_dx<dim> kk = key_it.get(); |
274 | |
275 | BOOST_REQUIRE_EQUAL(c3.template get<P::x>(kk),1.0); |
276 | |
277 | ++key_it; |
278 | } |
279 | } |
280 | |
281 | #endif /* SRC_GRID_GRID_UTIL_TEST_HPP_ */ |
282 | |