OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
grid_iterators_unit_tests.cpp
1/*
2 * grid_iterators_unit_tests.hpp
3 *
4 * Created on: Jun 27, 2016
5 * Author: i-bird
6 */
7
8#define BOOST_TEST_DYN_LINK
9#include <boost/test/unit_test.hpp>
10#include "Grid/iterators/grid_skin_iterator.hpp"
11#include "Grid/map_grid.hpp"
12#include "data_type/aggregate.hpp"
13#include "Grid/iterators/grid_key_dx_iterator_sub_bc.hpp"
14
15BOOST_AUTO_TEST_SUITE( grid_iterators_tests )
16
17template <unsigned int dim> void test_skin_iterator(Box<3,size_t> & bx1, Box<3,size_t> & bx2, grid_sm<3,void> & g_sm,size_t (& bc)[dim] , size_t vol_test)
18{
20 gtest.setMemory();
21 auto it = gtest.getSubIterator(0);
22
23 while (it.isNext())
24 {
25 auto key = it.get();
26
27 gtest.get<0>(key) = 0;
28
29 ++it;
30 }
31
32 size_t count = 0;
33 grid_skin_iterator_bc<3> gsi(g_sm,bx1,bx2,bc);
34
35 while (gsi.isNext() == true)
36 {
37 auto key = gsi.get();
38
39 gtest.get<0>(key) += 1;
40
41 count++;
42
43 ++gsi;
44 }
45
46 BOOST_REQUIRE_EQUAL(count,(size_t)vol_test);
47
48 bool ret = true;
49 auto it2 = gtest.getSubIterator(0);
50
51 while (it2.isNext())
52 {
53 auto key = it2.get();
54
55 ret &= gtest.get<0>(key) <= 1;
56
57 ++it2;
58 }
59
60 BOOST_REQUIRE_EQUAL(ret,true);
61}
62
63BOOST_AUTO_TEST_CASE( grid_skin_iterator_test )
64{
65 // Test box inside box
66
67 size_t sz[] = {112,112,112};
68 Box<3,size_t> bx1({20,25,30},{90,93,98});
69 Box<3,size_t> bx2({18,23,28},{92,95,100});
70 Box<3,size_t> bx3({20,25,30},{90,93,30});
71 Box<3,size_t> bx4({20,25,30},{90,93,50});
72 Box<3,size_t> bx5({19,24,29},{90,93,50});
73
74 grid_sm<3,void> g_sm(sz);
75 size_t bc[] = {PERIODIC,PERIODIC,PERIODIC};
76
77 // Volume calculation
78
79 size_t tot = 1;
80
81 tot *= bx2.getHigh(0) - bx2.getLow(0) + 1;
82 tot *= bx2.getHigh(1) - bx2.getLow(1) + 1;
83 tot *= bx2.getHigh(2) - bx2.getLow(2) + 1;
84
85 size_t tot_ = 1;
86
87 tot_ *= bx1.getHigh(0) - bx1.getLow(0) + 1 - 2;
88 tot_ *= bx1.getHigh(1) - bx1.getLow(1) + 1 - 2;
89 tot_ *= bx1.getHigh(2) - bx1.getLow(2) + 1 - 2;
90
91 tot -= tot_;
92
93 test_skin_iterator<3>(bx1,bx2,g_sm,bc,tot);
94 test_skin_iterator<3>(bx2,bx1,g_sm,bc,0);
95 test_skin_iterator<3>(bx3,bx3,g_sm,bc,4899);
96
97 Point<3,size_t> p({60,60,60});
98
99 bx2 += p;
100 bx1 += p;
101
102 test_skin_iterator<3>(bx1,bx2,g_sm,bc,tot);
103 test_skin_iterator<3>(bx2,bx1,g_sm,bc,0);
104
105 test_skin_iterator<3>(bx4,bx4,g_sm,bc,15042);
106 test_skin_iterator<3>(bx5,bx4,g_sm,bc,7679);
107}
108
109void test_stencil_iterator(grid_sm<3,void> & g_sm)
110{
112 gtest.setMemory();
113 auto it = gtest.getSubIterator(0);
114
115 while (it.isNext())
116 {
117 auto key = it.get();
118
119 gtest.get<0>(key) = key.get(0) + key.get(1) + key.get(2);
120
121 ++it;
122 }
123
124 grid_key_dx<3> stencil[1];
125 stencil[0].set_d(0,0);
126 stencil[0].set_d(1,0);
127 stencil[0].set_d(2,0);
128
129 bool ret = true;
131
132 while (gsi.isNext() == true)
133 {
134 auto key = gsi.get();
135 auto lin = gsi.getStencil<0>();
136
137 ret &= (gtest.get<0>(lin) == key.get(0) + key.get(1) + key.get(2));
138
139 ++gsi;
140 }
141
142
143 BOOST_REQUIRE_EQUAL(ret,true);
144}
145
146BOOST_AUTO_TEST_CASE( grid_iterator_stencil_test )
147{
148 size_t sz[] = {52,52,52};
149 grid_sm<3,void> g_sm(sz);
150 test_stencil_iterator(g_sm);
151}
152
153static grid_key_dx<3> star_stencil_3D[7] = {{0,0,0},
154 {0,0,-1},
155 {0,0,1},
156 {0,-1,0},
157 {0,1,0},
158 {-1,0,0},
159 {1,0,0}};
160
161void test_stencil_sub_iterator(grid_sm<3,void> & g_sm)
162{
164 gtest.setMemory();
165 auto it = gtest.getSubIterator(0);
166
167 while (it.isNext())
168 {
169 auto key = it.get();
170
171 gtest.get<0>(key) = key.get(0) + key.get(1) + key.get(2);
172
173 ++it;
174 }
175
176 grid_key_dx<3> start({1,1,1});
177 grid_key_dx<3> stop({(long int)gtest.getGrid().size(0)-2,(long int)gtest.getGrid().size(1)-2,(long int)gtest.getGrid().size(2)-2});
178
179 bool ret = true;
180 grid_key_dx_iterator_sub<3,stencil_offset_compute<3,7>> gsi(g_sm,start,stop,star_stencil_3D);
181
182 while (gsi.isNext() == true)
183 {
184 auto key = gsi.get();
185
186 size_t lin1 = gsi.getStencil<0>();
187 size_t lin2 = gsi.getStencil<1>();
188 size_t lin3 = gsi.getStencil<2>();
189 size_t lin4 = gsi.getStencil<3>();
190 size_t lin5 = gsi.getStencil<4>();
191 size_t lin6 = gsi.getStencil<5>();
192 size_t lin7 = gsi.getStencil<6>();
193
194
195 size_t sum = 6*gtest.get<0>(lin1) -
196 gtest.get<0>(lin2) -
197 gtest.get<0>(lin3) -
198 gtest.get<0>(lin4) -
199 gtest.get<0>(lin5) -
200 gtest.get<0>(lin6) -
201 gtest.get<0>(lin7);
202
203 ret &= (sum == 0);
204
205 ret &= g_sm.LinId(key) == (long int)lin1;
206
207 ++gsi;
208 }
209
210
211 BOOST_REQUIRE_EQUAL(ret,true);
212}
213
214BOOST_AUTO_TEST_CASE( grid_iterator_sub_stencil_test )
215{
216 size_t sz[] = {52,52,52};
217 grid_sm<3,void> g_sm(sz);
218 test_stencil_sub_iterator(g_sm);
219}
220
221
222
223BOOST_AUTO_TEST_CASE( grid_iterator_sub_bc )
224{
225 {
226 size_t sz[] = {52,52,52};
227 grid_sm<3,void> g_sm(sz);
228
229 grid_key_dx<3> start({51,51,51});
230 grid_key_dx<3> stop({52,52,52});
231
232 size_t bc[3] = {PERIODIC,PERIODIC,PERIODIC};
233
234 grid_key_dx_iterator_sub_bc<3> it(g_sm,start,stop,bc);
235
236 bool is_ok = true;
237 size_t cnt = 0;
238 while (it.isNext())
239 {
240 auto p = it.get();
241
242 if ((p.get(0) != 51 && p.get(0) != 0) ||
243 (p.get(1) != 51 && p.get(1) != 0) ||
244 (p.get(2) != 51 && p.get(2) != 0))
245 {
246 is_ok = false;
247 }
248
249 cnt++;
250
251 ++it;
252 }
253
254 BOOST_REQUIRE_EQUAL(is_ok,true);
255 BOOST_REQUIRE_EQUAL(cnt,8ul);
256 }
257
258 {
259 size_t sz[] = {52,52,52};
260 grid_sm<3,void> g_sm(sz);
261
262 grid_key_dx<3> start({-1,-1,-1});
263 grid_key_dx<3> stop({0,0,0});
264
265 size_t bc[3] = {PERIODIC,PERIODIC,PERIODIC};
266
267 grid_key_dx_iterator_sub_bc<3> it(g_sm,start,stop,bc);
268
269 bool is_ok = true;
270 size_t cnt = 0;
271 while (it.isNext())
272 {
273 auto p = it.get();
274
275 if ((p.get(0) != 51 && p.get(0) != 0) ||
276 (p.get(1) != 51 && p.get(1) != 0) ||
277 (p.get(2) != 51 && p.get(2) != 0))
278 {
279 is_ok = false;
280 }
281
282 cnt++;
283
284 ++it;
285 }
286
287 BOOST_REQUIRE_EQUAL(is_ok,true);
288 BOOST_REQUIRE_EQUAL(cnt,8ul);
289 }
290}
291
292BOOST_AUTO_TEST_CASE( grid_iterator_sub_bc_hd )
293{
294 grid_key_dx<50> start;
295 grid_key_dx<50> stop;
296 size_t sz[50];
297 size_t bc[50];
298 for (size_t i = 0 ; i < 50 ; i++)
299 {
300 sz[i] = 1;
301 start.set_d(i,0);
302 stop.set_d(i,0);
303 bc[i] = NON_PERIODIC;
304 }
305
306 sz[0] = 52;
307 sz[11] = 52;
308 sz[23] = 52;
309
310 bc[0] = PERIODIC;
311 bc[11] = PERIODIC;
312 bc[23] = PERIODIC;
313
314 start.set_d(0,51);
315 start.set_d(11,51);
316 start.set_d(23,51);
317
318 stop.set_d(0,52);
319 stop.set_d(11,52);
320 stop.set_d(23,52);
321
322 grid_sm<50,void> g_sm(sz);
323
324 grid_key_dx_iterator_sub_bc<50> it(g_sm,start,stop,bc);
325
326 bool is_ok = true;
327 size_t cnt = 0;
328 while (it.isNext())
329 {
330 auto p = it.get();
331
332 if ((p.get(0) != 51 && p.get(0) != 0) ||
333 (p.get(11) != 51 && p.get(11) != 0) ||
334 (p.get(23) != 51 && p.get(23) != 0))
335 {
336 is_ok = false;
337 }
338
339 cnt++;
340
341 ++it;
342 }
343
344 BOOST_REQUIRE_EQUAL(is_ok,true);
345 BOOST_REQUIRE_EQUAL(cnt,8ul);
346}
347
348BOOST_AUTO_TEST_SUITE_END()
349
This class represent an N-dimensional box.
Definition Box.hpp:61
__device__ __host__ T getLow(int i) const
get the i-coordinate of the low bound interval of the box
Definition Box.hpp:556
__device__ __host__ T getHigh(int i) const
get the high interval of the box
Definition Box.hpp:567
This class implement the point shape in an N-dimensional space.
Definition Point.hpp:28
The same as grid_key_dx_iterator_sub_p but with periodic boundary.
Declaration grid_key_dx_iterator_sub.
grid_key_dx is the key to access any element in the grid
Definition grid_key.hpp:19
__device__ __host__ void set_d(index_type i, index_type id)
Set the i index.
Definition grid_key.hpp:516
Declaration grid_sm.
Definition grid_sm.hpp:167
mem_id LinId(const grid_key_dx< N, ids_type > &gk, const signed char sum_id[N]) const
Linearization of the grid_key_dx with a specified shift.
Definition grid_sm.hpp:454
__device__ __host__ const size_t(& getSize() const)[N]
Return the size of the grid as an array.
Definition grid_sm.hpp:760
KeyT const ValueT ValueT OffsetIteratorT OffsetIteratorT int
[in] The number of segments that comprise the sorting data
It model an expression expr1 + ... exprn.
Definition sum.hpp:93