OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
grid_sm_unit_tests.hpp
1 /*
2  * grid_sm_test.hpp
3  *
4  * Created on: Dec 14, 2015
5  * Author: i-bird
6  */
7 
8 #ifndef OPENFPM_DATA_SRC_GRID_GRID_SM_UNIT_TESTS_HPP_
9 #define OPENFPM_DATA_SRC_GRID_GRID_SM_UNIT_TESTS_HPP_
10 
11 #include "iterators/grid_key_dx_iterator_sub_bc.hpp"
12 #include "grid_key_dx_iterator_hilbert.hpp"
13 
14 BOOST_AUTO_TEST_SUITE( grid_sm_test )
15 
16 
17 BOOST_AUTO_TEST_CASE( grid_sm_linearization )
18 {
19  const grid_key_dx<3> key1(1,2,3);
20  const grid_key_dx<3> zero(0,0,0);
21  const grid_key_dx<3> seven(7,7,7);
22 
23  const comb<3> c({(char)1,(char)0,(char)-1});
24  size_t sz[3] = {8,8,8};
25 
26  grid_sm<3,int> gs(sz);
27  size_t bc[] = {NON_PERIODIC,NON_PERIODIC,NON_PERIODIC};
28 
29  long int lin = gs.LinId<CheckExistence>(key1,c.getComb(),bc);
30  BOOST_REQUIRE_EQUAL(lin,146);
31  lin = gs.LinId<CheckExistence>(zero,c.getComb(),bc);
32  BOOST_REQUIRE_EQUAL(lin,-1);
33  lin = gs.LinId<CheckExistence>(seven,c.getComb(),bc);
34  BOOST_REQUIRE_EQUAL(lin,-1);
35 
36  for (size_t i = 0 ; i < 3 ; i++)
37  bc[i] = PERIODIC;
38 
39  lin = gs.LinId<CheckExistence>(key1,c.getComb(),bc);
40  BOOST_REQUIRE_EQUAL(lin,146);
41  lin = gs.LinId<CheckExistence>(zero,c.getComb(),bc);
42  BOOST_REQUIRE_EQUAL(lin,71);
43  lin = gs.LinId<CheckExistence>(seven,c.getComb(),bc);
44  BOOST_REQUIRE_EQUAL(lin,62);
45 }
46 
47 
48 BOOST_AUTO_TEST_CASE( grid_iterator_sub_p )
49 {
50  const grid_key_dx<3> key1(4,4,4);
51  const grid_key_dx<3> key2(-1,-1,-1);
52  const grid_key_dx<3> key3(9,9,9);
53  size_t sz[3] = {8,8,8};
54 
55  grid_sm<3,int> gs(sz);
56 
57  grid_key_dx_iterator_sub_bc<3,no_stencil> it(gs,key2,key1,{PERIODIC,PERIODIC,PERIODIC});
58 
59  size_t cnt = 0;
60 
61  while (it.isNext())
62  {
63  auto key = it.get();
64 
65  for (size_t i = 0 ; i < 3 ; i++)
66  {
67  BOOST_REQUIRE_EQUAL(key.get(i) >= (long int)0,true);
68  BOOST_REQUIRE_EQUAL(key.get(i) < (long int)sz[i],true);
69  }
70 
71  cnt++;
72 
73  ++it;
74  }
75 
76  BOOST_REQUIRE_EQUAL(cnt,216ul);
77 
78  grid_key_dx_iterator_sub_bc<3,no_stencil> it2(gs,key2,key3,{PERIODIC,PERIODIC,PERIODIC});
79 
80  cnt = 0;
81 
82  while (it2.isNext())
83  {
84  auto key = it2.get();
85 
86  for (size_t i = 0 ; i < 3 ; i++)
87  {
88  BOOST_REQUIRE_EQUAL(key.get(i) >= (long int)0,true);
89  BOOST_REQUIRE_EQUAL(key.get(i) < (long int)sz[i],true);
90  }
91 
92  cnt++;
93 
94  ++it2;
95  }
96 
97  BOOST_REQUIRE_EQUAL(cnt,1331ul);
98 
99  cnt = 0;
100 
101  const grid_key_dx<3> key4(0,-1,0);
102  const grid_key_dx<3> key5(2,2,2);
103 
104  grid_key_dx_iterator_sub_bc<3,no_stencil> it3(gs,key4,key5,{NON_PERIODIC,PERIODIC,NON_PERIODIC});
105 
106  while (it3.isNext())
107  {
108  auto key = it3.get();
109 
110  for (size_t i = 0 ; i < 3 ; i++)
111  {
112  BOOST_REQUIRE_EQUAL(key.get(i) >= (long int)0,true);
113  BOOST_REQUIRE_EQUAL(key.get(i) < (long int)sz[i],true);
114  }
115 
116  cnt++;
117 
118  ++it3;
119  }
120 
121  BOOST_REQUIRE_EQUAL(cnt,36ul);
122 
123  // bc non periodic with out-of-bound
124 
126 
127  cnt = 0;
128 
129  while (it4.isNext())
130  {
131  auto key = it4.get();
132 
133  for (size_t i = 0 ; i < 3 ; i++)
134  {
135  BOOST_REQUIRE_EQUAL(key.get(i) >= (long int)0,true);
136  BOOST_REQUIRE_EQUAL(key.get(i) < (long int)sz[i],true);
137  }
138 
139  cnt++;
140 
141  ++it4;
142  }
143 
144  BOOST_REQUIRE_EQUAL(cnt,27ul);
145 
146  // case with no key
147 
148  const grid_key_dx<3> key6(-1,-1,-1);
149  const grid_key_dx<3> key7(-1,-1,8);
150 
152 
153  cnt = 0;
154 
155  while (it5.isNext())
156  {
157  auto key = it5.get();
158 
159  for (size_t i = 0 ; i < 3 ; i++)
160  {
161  BOOST_REQUIRE_EQUAL(key.get(i) >= 0,true);
162  BOOST_REQUIRE_EQUAL(key.get(i) < (long int)sz[i],true);
163  }
164 
165  cnt++;
166 
167  ++it5;
168  }
169 
170  BOOST_REQUIRE_EQUAL(cnt,0ul);
171 }
172 
173 BOOST_AUTO_TEST_CASE( grid_key_dx_iterator_hilbert_test )
174 {
175  // 2D test
176  {
177  size_t count = 0;
178 
179  //An order of a hilberts curve
180  int32_t m = 2;
181 
182  grid_key_dx<2> start (0,0);
183 
184  //Create an iterator
186 
187  while (h_it.isNext())
188  {
189  count++;
190 
191  ++h_it;
192  }
193 
194  //(2^m)^dim
195  BOOST_REQUIRE_EQUAL(count, (size_t)16);
196 
197  h_it.reset();
198 
199  bool val = h_it.get() == start;
200 
201  BOOST_REQUIRE_EQUAL(val,true);
202  }
203 
204  // 3D test
205  {
206  size_t count = 0;
207 
208  //An order of a hilberts curve
209  int32_t m = 2;
210 
211  grid_key_dx<3> start (0,0,0);
212 
213  //Create an iterator
215 
216  while (h_it.isNext())
217  {
218  count++;
219 
220  ++h_it;
221  }
222 
223  //(2^m)^dim
224  BOOST_REQUIRE_EQUAL(count, (size_t)64);
225 
226  h_it.reset();
227 
228  bool val = h_it.get() == start;
229 
230  BOOST_REQUIRE_EQUAL(val,true);
231  }
232 }
233 
234 
235 BOOST_AUTO_TEST_CASE( grid_iterator_sp_test )
236 {
237  size_t sz[3] = {16,16,16};
238 
240  c3.setMemory();
241 
242  grid_key_dx<3> start(2,2,2);
243  grid_key_dx<3> stop(10,10,10);
244 
245  auto info = c3.getGrid();
246 
247  grid_key_dx_iterator_sp<3> it(info,info.LinId(start),info.LinId(stop));
248 
249  size_t count = 0;
250 
251  while (it.isNext())
252  {
253  count++;
254 
255  ++it;
256  }
257 
258  BOOST_REQUIRE_EQUAL(count,2185ul);
259 }
260 
261 BOOST_AUTO_TEST_CASE( grid_iterator_test_use)
262 {
263  {
265  size_t count = 0;
266 
267  // Subdivisions
268  size_t div[3] = {16,16,16};
269 
270  // grid info
271  grid_sm<3,void> g_info(div);
272 
273  // Create a grid iterator
274  grid_key_dx_iterator<3> g_it(g_info);
275 
276  // Iterate on all the elements
277  while (g_it.isNext())
278  {
279  grid_key_dx<3> key = g_it.get();
280 
281  // set the grid key to zero without any reason ( to avoid warning compilations )
282  key.zero();
283 
284  count++;
285 
286  ++g_it;
287  }
288 
289  BOOST_REQUIRE_EQUAL(count, (size_t)16*16*16);
291  }
292 
293  {
294  size_t count = 0;
295  // Iterate only on the internal elements
296 
298  // Subdivisions
299  size_t div[3] = {16,16,16};
300 
301  // grid info
302  grid_sm<3,void> g_info(div);
303 
304  grid_key_dx<3> start(1,1,1);
305  grid_key_dx<3> stop(14,14,14);
306 
307  // Create a grid iterator (start and stop included)
308  grid_key_dx_iterator_sub<3> g_it(g_info,start,stop);
309 
310  // Iterate on all the elements
311  while (g_it.isNext())
312  {
313  grid_key_dx<3> key = g_it.get();
314 
315  // set the grid key to zero without any reason ( to avoid warning compilations )
316  key.zero();
317 
318  count++;
319 
320  ++g_it;
321  }
322 
323  BOOST_REQUIRE_EQUAL(count, (size_t)14*14*14);
324 
326 
327  // reset the iterator and check that it start from gk_start
328  g_it.reset();
329 
330  bool val = g_it.get() == start;
331 
332  BOOST_REQUIRE_EQUAL(val,true);
333  }
334 }
335 
336 BOOST_AUTO_TEST_CASE( grid_sub_iterator_test )
337 {
339  // Subdivisions
340  size_t count = 0;
341  typedef Point_test<float> p;
342 
343  size_t div[3] = {16,16,16};
344 
345  // grid info
347  g.setMemory();
348 
349  grid_key_dx<3> start(1,1,1);
350  grid_key_dx<3> stop(14,14,14);
351 
352  // Create a grid iterator (start and stop included)
353  auto g_it = g.getIterator(start,stop);
354 
355  // Iterate on all the elements
356  while (g_it.isNext())
357  {
358  grid_key_dx<3> key = g_it.get();
359 
360  // set the x value
361  g.template get<p::x>(key) = 1.0;
362 
363  count++;
364 
365  ++g_it;
366  }
367 
368  BOOST_REQUIRE_EQUAL(count, (size_t)14*14*14);
369 
371 }
372 
373 BOOST_AUTO_TEST_SUITE_END()
374 
375 
376 #endif /* OPENFPM_DATA_SRC_GRID_GRID_SM_UNIT_TESTS_HPP_ */
grid_key_dx is the key to access any element in the grid
Definition: grid_key.hpp:18
Position of the element of dimension d in the hyper-cube of dimension dim.
Definition: comb.hpp:34
__device__ __host__ index_type get(index_type i) const
Get the i index.
Definition: grid_key.hpp:503
The same as grid_key_dx_iterator_sub_p but with periodic boundary.
KeyT const ValueT ValueT OffsetIteratorT OffsetIteratorT int
[in] The number of segments that comprise the sorting data
const grid_key_dx< dim > get() const
Return the actual grid key iterator.
Declaration grid_sm.
Definition: grid_sm.hpp:147
Declaration grid_key_dx_iterator_sub.
Definition: grid_sm.hpp:156
void zero()
Set to zero the key.
Definition: grid_key.hpp:170
Test structure used for several test.
Definition: Point_test.hpp:105
Class to check if the edge can be created or not.
Definition: grid_sm.hpp:81