OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
CellListIterator_test.hpp
1 /*
2  * CellListIterator_test.hpp
3  *
4  * Created on: May 7, 2016
5  * Author: Yaroslav Zaluzhnyi
6  */
7 
8 #ifndef OPENFPM_DATA_SRC_NN_CELLLIST_CELLLISTITERATOR_TEST_HPP_
9 #define OPENFPM_DATA_SRC_NN_CELLLIST_CELLLISTITERATOR_TEST_HPP_
10 
11 #include "NN/CellList/CellListIterator.hpp"
12 #include "NN/CellList/ParticleIt_Cells.hpp"
13 #include "NN/CellList/ParticleItCRS_Cells.hpp"
14 
15 #ifdef OPENFPM_PDATA
16 #include "VCluster/VCluster.hpp"
17 #endif
18 
19 
26 template<unsigned int dim, typename CellList> void FillCellList(size_t k, CellList & NN)
27 {
28  float pos[dim];
29 
30  //Fill with particles
31  for (size_t i = 0; i < k; i++)
32  {
33  for (size_t j = 0; j < dim; j++)
34  {
35  pos[j] = rand()/double(RAND_MAX);
36  }
37  NN.add(pos,i);
38  }
39 }
40 
41 #include "CellListFast_gen.hpp"
42 
43 BOOST_AUTO_TEST_SUITE( celllist_gen_and_iterator_tests )
44 
45 BOOST_AUTO_TEST_CASE( celllist_lin_and_iterator_test )
46 {
48 
49  const size_t dim = 3;
50 
51  size_t div[dim] = {4,5,6};
52 
53  //Number of particles
54  size_t k = 300;
55 
57 
58  Box<dim,float> box;
59 
60  for (size_t i = 0; i < dim; i++)
61  {
62  box.setLow(i,0.0);
63  box.setHigh(i,1.0);
64  }
65 
66  // Initialize a cell list
68 
69  NN.Initialize(box,div,1);
70  NN.set_gm(k*0.9);
71 
72  float pos[dim];
73 
74  //Fill with particles
75  for (size_t i = 0; i < k*0.9; i++)
76  {
77  for (size_t j = 0; j < dim; j++)
78  {
79  pos[j] = rand()/double(RAND_MAX);
80  }
81  NN.add(pos,i);
82  }
83 
84  //Test the iterator
85  auto it_cl = NN.getIterator();
86 
87  size_t count = 0;
88 
89  while (it_cl.isNext())
90  {
91  auto p_key = it_cl.get();
92 
93  BOOST_REQUIRE(p_key < NN.get_gm());
94 
95  count++;
96  ++it_cl;
97  }
98 
99  BOOST_REQUIRE_EQUAL(count,NN.get_gm());
100 }
101 
102 BOOST_AUTO_TEST_CASE( celllist_hilb_and_iterator_test )
103 {
104 #ifdef OPENFPM_PDATA
105 
106  auto & v_cl = create_vcluster();
107 
108  std::string c2 = std::string("openfpm_data/test_data/NN_hilb_keys");
109 
110 #else
111 
112  std::string c2 = std::string("test_data/NN_hilb_keys");
113 
114 #endif
115 
117 
118  const size_t dim = 3;
119 
120  size_t div[dim] = {4,5,6};
121 
122  //Number of particles
123  size_t k = 300;
124 
126 
127  Box<dim,float> box;
128 
129  for (size_t i = 0; i < dim; i++)
130  {
131  box.setLow(i,0.0);
132  box.setHigh(i,1.0);
133  }
134 
135  // Initialize a cell list
137 
138  NN.Initialize(box,div,1);
139  NN.set_gm(k*0.9);
140 
141  FillCellList<dim>((size_t)k*0.9,NN);
142 
143 
144  //Test the iterator
145  auto it_cl = NN.getIterator();
146 
147  size_t count = 0;
148 
149  while (it_cl.isNext())
150  {
151  auto p_key = it_cl.get();
152 
153  BOOST_REQUIRE(p_key < NN.get_gm());
154 
155  count++;
156  ++it_cl;
157  }
158 
159  BOOST_REQUIRE_EQUAL(count,NN.get_gm());
160 
161  // Load previous results and check equality
162 
163  openfpm::vector<size_t> keys_old;
164 
165  keys_old.load(c2);
166 
167  for (size_t i = 0; i < keys_old.size(); i++)
168  {
169  size_t a1 = keys_old.get(i);
170  size_t a2 = NN.getCellSFC().getKeys().get(i);
171 
172  BOOST_REQUIRE_EQUAL(a1,a2);
173  }
174 
175  size_t s1 = keys_old.size();
176  size_t s2 = NN.getCellSFC().getKeys().size();
177 
178  BOOST_REQUIRE_EQUAL(s1,s2);
179 }
180 
181 BOOST_AUTO_TEST_CASE( ParticleItCRS_Cells_iterator )
182 {
184 
185  const size_t dim = 3;
186 
187  size_t div[dim] = {4,5,6};
188  size_t div_p[dim] = {6,7,8};
189 
190  grid_sm<3,void> gs(div_p);
191 
192  //Number of particles
193  size_t k = 300;
194 
196 
197  Box<dim,float> box;
198 
199  for (size_t i = 0; i < dim; i++)
200  {
201  box.setLow(i,0.0);
202  box.setHigh(i,1.0);
203  }
204 
205  // Initialize a cell list
207 
208  NN.Initialize(box,div,1);
209 
210  FillCellList<dim>(k,NN);
211 
212  float pos[dim];
213 
214  grid_key_dx<3> start(0,0,0);
215  grid_key_dx<3> stop(div[2]-1+2*NN.getPadding(2),div[1]-1+2*NN.getPadding(1),div[0]-1+2*NN.getPadding(0));
216 
219 
220  // No anomalous cells + all domain cells
221  grid_key_dx_iterator_sub<dim> it(gs,start,stop);
222 
223  while (it.isNext())
224  {
225  auto key = it.get();
226 
227  dom.add(gs.LinId(key));
228 
229  ++it;
230  }
231 
233 
234  //Test the iterator
236 
237  size_t count = 0;
238 
239  while (it_cl.isNext())
240  {
241  count++;
242  ++it_cl;
243  }
244 
245  BOOST_REQUIRE_EQUAL(count,k);
246 
248 
249  NN.clear();
250 
251  grid_key_dx<3> start2(1,1,1);
252  grid_key_dx<3> stop2(div[2]-2+2*NN.getPadding(2),div[1]-2+2*NN.getPadding(1),div[0]-2+2*NN.getPadding(0));
253 
254  //Fill with particles
255  for (size_t i = 0; i < k; i++)
256  {
257  pos[0] = 0.999*rand()/double(RAND_MAX) + 0.0001;
258  pos[1] = 0.999*rand()/double(RAND_MAX) + 0.0001;
259  pos[2] = 0.999*rand()/double(RAND_MAX) + 0.0001;
260 
261  NN.add(pos,i);
262  }
263 
264  dom.clear();
265 
266  bool alternate = false;
267 
268  // No anomalous cells + all domain cells
269  grid_key_dx_iterator_sub<dim> it2(gs,start,stop);
270 
271  while (it2.isNext())
272  {
273  auto key = it2.get();
274 
275  if (alternate == false)
276  {
277  dom.add(gs.LinId(key));
278  alternate = true;
279  }
280  else
281  {
282  anom.add();
283  anom.last().subsub = gs.LinId(key);
284  alternate = false;
285  }
286 
287  ++it2;
288  }
289 
291 
293 
294  count = 0;
295 
296  while (it_cl2.isNext())
297  {
298  count++;
299  ++it_cl2;
300  }
301 
302  BOOST_REQUIRE_EQUAL(count,k);
303 }
304 
305 BOOST_AUTO_TEST_CASE( ParticleIt_Cells_NN_iterator )
306 {
308 
309  const size_t dim = 3;
310 
311  size_t div[dim] = {4,5,6};
312  size_t div_p[dim] = {6,7,8};
313 
314  grid_sm<3,void> gs2(div);
315  grid_sm<3,void> gs(div_p);
316 
318 
319  Box<dim,float> box;
320 
321  for (size_t i = 0; i < dim; i++)
322  {
323  box.setLow(i,0.0);
324  box.setHigh(i,1.0);
325  }
326 
327  // Initialize a cell list
329 
330  NN.Initialize(box,div,1);
331 
332  grid_key_dx_iterator<3> it(gs2);
333 
334  float spacing[dim];
335  float middle[dim];
336  for (size_t i = 0 ; i < dim ; i++)
337  {
338  spacing[i] = box.getHigh(i) / div[i];
339  middle[i] = spacing[i]/2;
340  }
341 
342  size_t cnt = 0;
343 
344  //Fill with particles
346  while (it.isNext())
347  {
348  auto key = it.get();
349 
350  Point<3,float> p;
351 
352  p.get(0) = key.get(0)*spacing[0] + middle[0];
353  p.get(1) = key.get(1)*spacing[1] + middle[1];
354  p.get(2) = key.get(2)*spacing[2] + middle[2];
355 
356  vp.add(p);
357 
358  NN.add(p,cnt);
359  cnt++;
360 
361  ++it;
362  }
363 
364  grid_key_dx<3> start2(2,2,2);
365  grid_key_dx<3> stop2(div[2]-3+2*NN.getPadding(2),div[1]-3+2*NN.getPadding(1),div[0]-3+2*NN.getPadding(0));
366 
369 
370  // No anomalous cells + all domain cells
371  grid_key_dx_iterator_sub<dim> it2(gs,start2,stop2);
372 
373  bool alternate = false;
374  while (it2.isNext())
375  {
376  auto key = it2.get();
377 
378  if (alternate == false)
379  {
380  dom.add(gs.LinId(key));
381  alternate = true;
382  }
383  else
384  {
385  anom.add();
386  anom.last().subsub = gs.LinId(key);
387 
388  for(size_t j = 0 ; j < openfpm::math::pow(3,dim)/2+1 ; j++)
389  {anom.last().NN_subsub.add(NN.getNNc_sym()[j]);}
390 
391  alternate = false;
392  }
393 
394  ++it2;
395  }
396 
398 
399  //Test the iterator
401 
402  size_t count = 0;
403 
404  while (it_cl.isNext())
405  {
406  auto NN_it = it_cl.getNNIteratorCSR(vp);
407 
408  size_t size_NN = 0;
409 
410  while (NN_it.isNext())
411  {
412  size_NN++;
413 
414  ++NN_it;
415  }
416 
417  BOOST_REQUIRE_EQUAL(size_NN,14ul);
418 
419  count++;
420  ++it_cl;
421  }
422 
423  BOOST_REQUIRE_EQUAL(count,(div[0]-2)*(div[1]-2)*(div[2]-2));
424 }
425 
426 BOOST_AUTO_TEST_CASE( ParticleIt_Cells_iterator )
427 {
429 
430  const size_t dim = 3;
431 
432  size_t div[dim] = {4,5,6};
433  size_t div_p[dim] = {6,7,8};
434 
435  grid_sm<3,void> gs(div_p);
436 
437  //Number of particles
438  size_t k = 300;
439 
441 
442  Box<dim,float> box;
443 
444  for (size_t i = 0; i < dim; i++)
445  {
446  box.setLow(i,0.0);
447  box.setHigh(i,1.0);
448  }
449 
450  // Initialize a cell list
452 
453  NN.Initialize(box,div,1);
454 
455  FillCellList<dim>(k,NN);
456 
457  grid_key_dx<3> start(0,0,0);
458  grid_key_dx<3> stop(div[2]-1+2*NN.getPadding(2),div[1]-1+2*NN.getPadding(1),div[0]-1+2*NN.getPadding(0));
459 
460 
461  // No anomalous cells + all domain cells
462  grid_key_dx_iterator_sub<dim> it(gs,start,stop);
463 
465 
466  while (it.isNext())
467  {
468  auto key = it.get();
469 
470  dom.add(gs.LinId(key));
471 
472  ++it;
473  }
474 
475  //Test the iterator
477 
478  size_t count = 0;
479 
480  while (it_cl.isNext())
481  {
482  auto i = it_cl.get();
483 
484  count++;
485  ++it_cl;
486  }
487 
488  BOOST_REQUIRE_EQUAL(count,290ul);
489 }
490 
491 BOOST_AUTO_TEST_SUITE_END()
492 
493 #endif /* OPENFPM_DATA_SRC_NN_CELLLIST_CELLLISTITERATOR_TEST_HPP_ */
void set_gm(size_t g_m)
Set the ghost marker.
grid_key_dx is the key to access any element in the grid
Definition: grid_key.hpp:18
void clear()
Clear the cell list.
Definition: CellList.hpp:1100
void add(const T(&pos)[dim], typename Mem_type::local_index_type ele)
Add an element in the cell list.
Definition: CellList.hpp:726
__device__ __host__ index_type get(index_type i) const
Get the i index.
Definition: grid_key.hpp:503
Prock< dim, CellList_gen< dim, T, Prock, Mem_type, transform, vector_pos_type > >::Pit getIterator()
return the celllist iterator (across cells)
void Initialize(const Box< dim, T > &box, const size_t(&div)[dim], const size_t pad=1, size_t slot=STARTING_NSLOT)
size_t get()
Get the actual particle id.
size_t get_gm()
return the ghost marker
This iterator iterate across the particles of a Cell-list following the Cell structure.
const NNc_array< dim,(unsigned int) openfpm::math::pow(3, dim)/2+1 > & getNNc_sym() const
Get the symmetric neighborhood.
Definition: CellList.hpp:1069
__device__ __host__ void setHigh(int i, T val)
set the high interval of the box
Definition: Box.hpp:544
size_t getPadding(size_t i) const
Return the number of padding cells of the Cell decomposer.
Definition: CellList.hpp:1081
__device__ __host__ const T & get(unsigned int i) const
Get coordinate.
Definition: Point.hpp:172
__device__ __host__ void setLow(int i, T val)
set the low interval of the box
Definition: Box.hpp:533
size_t get()
Get the real particle id.
void Initialize(CellDecomposer_sm< dim, T, transform > &cd_sm, const Box< dim, T > &dom_box, const size_t pad=1, size_t slot=STARTING_NSLOT)
Definition: CellList.hpp:465
CellListType::SymNNIterator getNNIteratorCSR(const vector_pos_type &v) const
Get the neighborhood iterator according to the CRS scheme.
This class represent an N-dimensional box.
Definition: Box.hpp:60
Declaration grid_sm.
Definition: grid_sm.hpp:147
This iterator iterate across the particles of a Cell-list following the Cell structure.
Distributed vector.
const Prock< dim, CellList_gen< dim, T, Prock, Mem_type, transform, vector_pos_type > > & getCellSFC() const
Get the space filling curve object.
Class for FAST cell list implementation.
Definition: CellList.hpp:356
__device__ __host__ T getHigh(int i) const
get the high interval of the box
Definition: Box.hpp:567