OpenFPM  5.2.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/ParticleItCRS_Cells.hpp"
13 
14 
21 template<unsigned int dim, typename CellList> void FillCellList(size_t k, CellList & NN)
22 {
23  float pos[dim];
24 
25  //Fill with particles
26  for (size_t i = 0; i < k; i++)
27  {
28  for (size_t j = 0; j < dim; j++)
29  {
30  pos[j] = rand()/double(RAND_MAX);
31  }
32  NN.add(pos,i);
33  }
34 }
35 
36 BOOST_AUTO_TEST_SUITE( celllist_and_iterator_tests )
37 
38 BOOST_AUTO_TEST_CASE( celllist_lin_and_iterator_test )
39 {
41 
42  const size_t dim = 3;
43 
44  size_t div[dim] = {4,5,6};
45 
46  //Number of particles
47  size_t k = 300;
48 
50 
51  Box<dim,float> box;
52 
53  for (size_t i = 0; i < dim; i++)
54  {
55  box.setLow(i,0.0);
56  box.setHigh(i,1.0);
57  }
58 
59  // Initialize a cell list
61 
62  NN.Initialize(box,div,1);
63  NN.setGhostMarker(k*0.9);
64 
65  float pos[dim];
66 
67  //Fill with particles
68  for (size_t i = 0; i < k*0.9; i++)
69  {
70  for (size_t j = 0; j < dim; j++)
71  {
72  pos[j] = rand()/double(RAND_MAX);
73  }
74  NN.add(pos,i);
75  }
76 
77  //Test the iterator
78  auto it_cl = NN.getCellParticleIterator();
79 
80  size_t count = 0;
81 
82  while (it_cl.isNext())
83  {
84  auto p_key = it_cl.get();
85 
86  BOOST_REQUIRE(p_key < NN.getGhostMarker());
87 
88  count++;
89  ++it_cl;
90  }
91 
92  BOOST_REQUIRE_EQUAL(count,NN.getGhostMarker());
93 }
94 
95 BOOST_AUTO_TEST_CASE( celllist_hilb_and_iterator_test )
96 {
97  std::string c2 = std::string("test_data/NN_hilb_keys");
98 
100 
101  const size_t dim = 3;
102 
103  size_t div[dim] = {4,5,6};
104 
105  //Number of particles
106  size_t k = 300;
107 
109 
110  Box<dim,float> box;
111 
112  for (size_t i = 0; i < dim; i++)
113  {
114  box.setLow(i,0.0);
115  box.setHigh(i,1.0);
116  }
117 
118  // Initialize a cell list
120 
121  NN.setOpt(CL_NON_SYMMETRIC | CL_HILBERT_CELL_KEYS);
122  NN.Initialize(box,div,1);
123  NN.setGhostMarker(k*0.9);
124 
125  FillCellList<dim>((size_t)k*0.9,NN);
126 
127 
128  //Test the iterator
129  auto it_cl = NN.getCellParticleIterator();
130 
131  size_t count = 0;
132 
133  while (it_cl.isNext())
134  {
135  auto p_key = it_cl.get();
136 
137  BOOST_REQUIRE(p_key < NN.getGhostMarker());
138 
139  count++;
140  ++it_cl;
141  }
142 
143  BOOST_REQUIRE_EQUAL(count,NN.getGhostMarker());
144 
145  // Load previous results and check equality
146 
147  openfpm::vector<size_t> keys_old;
148 
149  keys_old.load(c2);
150 
151  for (size_t i = 0; i < keys_old.size(); i++)
152  {
153  size_t a1 = keys_old.get(i);
154  size_t a2 = NN.getCellSFCKeys().get(i);
155 
156  BOOST_REQUIRE_EQUAL(a1,a2);
157  }
158 
159  size_t s1 = keys_old.size();
160  size_t s2 = NN.getCellSFCKeys().size();
161 
162  BOOST_REQUIRE_EQUAL(s1,s2);
163 }
164 
165 BOOST_AUTO_TEST_CASE( ParticleItCRS_Cells_iterator )
166 {
168 
169  const size_t dim = 3;
170 
171  size_t div[dim] = {4,5,6};
172  size_t div_p[dim] = {6,7,8};
173 
174  grid_sm<3,void> gs(div_p);
175 
176  //Number of particles
177  size_t k = 300;
178 
180 
181  Box<dim,float> box;
182 
183  for (size_t i = 0; i < dim; i++)
184  {
185  box.setLow(i,0.0);
186  box.setHigh(i,1.0);
187  }
188 
189  // Initialize a cell list
191 
192  NN.Initialize(box,div,1);
193 
194  FillCellList<dim>(k,NN);
195 
196  float pos[dim];
197 
198  grid_key_dx<3> start(0,0,0);
199  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));
200 
203 
204  // No anomalous cells + all domain cells
205  grid_key_dx_iterator_sub<dim> it(gs,start,stop);
206 
207  while (it.isNext())
208  {
209  auto key = it.get();
210 
211  dom.add(gs.LinId(key));
212 
213  ++it;
214  }
215 
217 
218  //Test the iterator
220 
221  size_t count = 0;
222 
223  while (it_cl.isNext())
224  {
225  count++;
226  ++it_cl;
227  }
228 
229  BOOST_REQUIRE_EQUAL(count,k);
230 
232 
233  NN.clear();
234 
235  grid_key_dx<3> start2(1,1,1);
236  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));
237 
238  //Fill with particles
239  for (size_t i = 0; i < k; i++)
240  {
241  pos[0] = 0.999*rand()/double(RAND_MAX) + 0.0001;
242  pos[1] = 0.999*rand()/double(RAND_MAX) + 0.0001;
243  pos[2] = 0.999*rand()/double(RAND_MAX) + 0.0001;
244 
245  NN.add(pos,i);
246  }
247 
248  dom.clear();
249 
250  bool alternate = false;
251 
252  // No anomalous cells + all domain cells
253  grid_key_dx_iterator_sub<dim> it2(gs,start,stop);
254 
255  while (it2.isNext())
256  {
257  auto key = it2.get();
258 
259  if (alternate == false)
260  {
261  dom.add(gs.LinId(key));
262  alternate = true;
263  }
264  else
265  {
266  anom.add();
267  anom.last().subsub = gs.LinId(key);
268  alternate = false;
269  }
270 
271  ++it2;
272  }
273 
275 
277 
278  count = 0;
279 
280  while (it_cl2.isNext())
281  {
282  count++;
283  ++it_cl2;
284  }
285 
286  BOOST_REQUIRE_EQUAL(count,k);
287 }
288 
289 BOOST_AUTO_TEST_CASE( ParticleIt_Cells_NN_iterator )
290 {
292 
293  const size_t dim = 3;
294 
295  size_t div[dim] = {4,5,6};
296  size_t div_p[dim] = {6,7,8};
297 
298  grid_sm<3,void> gs2(div);
299  grid_sm<3,void> gs(div_p);
300 
302 
303  Box<dim,float> box;
304 
305  for (size_t i = 0; i < dim; i++)
306  {
307  box.setLow(i,0.0);
308  box.setHigh(i,1.0);
309  }
310 
311  // Initialize a cell list
313 
314  NN.Initialize(box,div,1);
315 
316  grid_key_dx_iterator<3> it(gs2);
317 
318  float spacing[dim];
319  float middle[dim];
320  for (size_t i = 0 ; i < dim ; i++)
321  {
322  spacing[i] = box.getHigh(i) / div[i];
323  middle[i] = spacing[i]/2;
324  }
325 
326  size_t cnt = 0;
327 
328  //Fill with particles
330  while (it.isNext())
331  {
332  auto key = it.get();
333 
334  Point<3,float> p;
335 
336  p.get(0) = key.get(0)*spacing[0] + middle[0];
337  p.get(1) = key.get(1)*spacing[1] + middle[1];
338  p.get(2) = key.get(2)*spacing[2] + middle[2];
339 
340  vp.add(p);
341 
342  NN.add(p,cnt);
343  cnt++;
344 
345  ++it;
346  }
347 
348  grid_key_dx<3> start2(2,2,2);
349  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));
350 
353 
354  // No anomalous cells + all domain cells
355  grid_key_dx_iterator_sub<dim> it2(gs,start2,stop2);
356 
357  bool alternate = false;
358  while (it2.isNext())
359  {
360  auto key = it2.get();
361 
362  if (alternate == false)
363  {
364  dom.add(gs.LinId(key));
365  alternate = true;
366  }
367  else
368  {
369  anom.add();
370  anom.last().subsub = gs.LinId(key);
371 
372  for(size_t j = 0 ; j < openfpm::math::pow(3,dim)/2+1 ; j++)
373  {anom.last().NN_subsub.add(NN.getNNc_sym()[j]);}
374 
375  alternate = false;
376  }
377 
378  ++it2;
379  }
380 
382 
383  //Test the iterator
385 
386  size_t count = 0;
387 
388  while (it_cl.isNext())
389  {
390  auto NN_it = it_cl.getNNIteratorCSR(vp);
391 
392  size_t size_NN = 0;
393 
394  while (NN_it.isNext())
395  {
396  size_NN++;
397 
398  ++NN_it;
399  }
400 
401  BOOST_REQUIRE_EQUAL(size_NN,14ul);
402 
403  count++;
404  ++it_cl;
405  }
406 
407  BOOST_REQUIRE_EQUAL(count,(div[0]-2)*(div[1]-2)*(div[2]-2));
408 }
409 
410 BOOST_AUTO_TEST_CASE( ParticleIt_Cells_iterator )
411 {
413 
414  const size_t dim = 3;
415 
416  size_t div[dim] = {4,5,6};
417  size_t div_p[dim] = {6,7,8};
418 
419  grid_sm<3,void> gs(div_p);
420 
421  //Number of particles
422  size_t k = 300;
423 
425 
426  Box<dim,float> box;
427 
428  for (size_t i = 0; i < dim; i++)
429  {
430  box.setLow(i,0.0);
431  box.setHigh(i,1.0);
432  }
433 
434  // Initialize a cell list
436 
437  NN.Initialize(box,div,1);
438 
439  FillCellList<dim>(k,NN);
440 
441  grid_key_dx<3> start(0,0,0);
442  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));
443 
444 
445  // No anomalous cells + all domain cells
446  grid_key_dx_iterator_sub<dim> it(gs,start,stop);
447 
449 
450  while (it.isNext())
451  {
452  auto key = it.get();
453 
454  dom.add(gs.LinId(key));
455 
456  ++it;
457  }
458 
459  //Test the iterator
461 
462  size_t count = 0;
463 
464  while (it_cl.isNext())
465  {
466  auto i = it_cl.get();
467 
468  count++;
469  ++it_cl;
470  }
471 
472  BOOST_REQUIRE_EQUAL(count,290ul);
473 }
474 
475 BOOST_AUTO_TEST_SUITE_END()
476 
477 #endif /* OPENFPM_DATA_SRC_NN_CELLLIST_CELLLISTITERATOR_TEST_HPP_ */
This class represent an N-dimensional box.
Definition: Box.hpp:60
__device__ __host__ T getHigh(int i) const
get the high interval of the box
Definition: Box.hpp:566
__device__ __host__ void setHigh(int i, T val)
set the high interval of the box
Definition: Box.hpp:543
__device__ __host__ void setLow(int i, T val)
set the low interval of the box
Definition: Box.hpp:532
Class for FAST cell list implementation.
Definition: CellList.hpp:558
const openfpm::vector< size_t > & getCellSFCKeys()
Get the space filling curve object.
Definition: CellList.hpp:1520
void setGhostMarker(size_t ghostMarker)
Set the ghost marker.
Definition: CellList.hpp:1488
__attribute__((always_inline)) inline const typename Mem_type size_t getGhostMarker() const
Return the starting point of the cell p.
Definition: CellList.hpp:1478
const NNc_array< dim,(unsigned int) openfpm::math::pow(3, dim)/2+1 > & getNNc_sym() const
Get the symmetric neighborhood.
Definition: CellList.hpp:1357
CellParticleIterator getCellParticleIterator()
return the celllist iterator (across cells)
Definition: CellList.hpp:1499
void add(const T(&pos)[dim], typename Mem_type::local_index_type ele)
Add an element in the cell list.
Definition: CellList.hpp:971
void clear()
Clear the cell list.
Definition: CellList.hpp:1410
size_t getPadding(size_t i) const
Return the number of padding cells of the Cell decomposer.
Definition: CellList.hpp:1391
void setOpt(size_t opt)
Sets the option flags that control the cell list.
Definition: CellList.hpp:1378
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:731
This iterator iterate across the particles of a Cell-list following the Cell structure.
size_t get()
Get the real particle id.
This iterator iterate across the particles of a Cell-list following the Cell structure.
grid_key_dx is the key to access any element in the grid
Definition: grid_key.hpp:19
Declaration grid_sm.
Definition: grid_sm.hpp:167
size_t size()
Stub size.
Definition: map_vector.hpp:212
Distributed vector.