OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
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 
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 #include "CellListFast_gen.hpp"
37 
38 BOOST_AUTO_TEST_SUITE( celllist_gen_and_iterator_tests )
39 
40 BOOST_AUTO_TEST_CASE( celllist_lin_and_iterator_test )
41 {
43 
44  const size_t dim = 3;
45 
46  size_t div[dim] = {4,5,6};
47 
48  //Number of particles
49  size_t k = 300;
50 
52 
53  Box<dim,float> box;
54 
55  for (size_t i = 0; i < dim; i++)
56  {
57  box.setLow(i,0.0);
58  box.setHigh(i,1.0);
59  }
60 
61  // Initialize a cell list
63 
64  NN.Initialize(box,div,1);
65  NN.set_gm(k*0.9);
66 
67  float pos[dim];
68 
69  //Fill with particles
70  for (size_t i = 0; i < k*0.9; i++)
71  {
72  for (size_t j = 0; j < dim; j++)
73  {
74  pos[j] = rand()/double(RAND_MAX);
75  }
76  NN.add(pos,i);
77  }
78 
79  //Test the iterator
80  auto it_cl = NN.getIterator();
81 
82  size_t count = 0;
83 
84  while (it_cl.isNext())
85  {
86  auto p_key = it_cl.get();
87 
88  BOOST_REQUIRE(p_key < NN.get_gm());
89 
90  count++;
91  ++it_cl;
92  }
93 
94  BOOST_REQUIRE_EQUAL(count,NN.get_gm());
95 }
96 
97 BOOST_AUTO_TEST_CASE( celllist_hilb_and_iterator_test )
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.Initialize(box,div,1);
122  NN.set_gm(k*0.9);
123 
124  FillCellList<dim>((size_t)k*0.9,NN);
125 
126 
127  //Test the iterator
128  auto it_cl = NN.getIterator();
129 
130  size_t count = 0;
131 
132  while (it_cl.isNext())
133  {
134  auto p_key = it_cl.get();
135 
136  BOOST_REQUIRE(p_key < NN.get_gm());
137 
138  count++;
139  ++it_cl;
140  }
141 
142  BOOST_REQUIRE_EQUAL(count,NN.get_gm());
143 
144  // Load previous results and check equality
145 
146  openfpm::vector<size_t> keys_old;
147 
148  keys_old.load("NN_hilb_keys");
149 
150  for (size_t i = 0; i < keys_old.size(); i++)
151  {
152  size_t a1 = keys_old.get(i);
153  size_t a2 = NN.getCellSFC().getKeys().get(i);
154 
155  BOOST_REQUIRE_EQUAL(a1,a2);
156  }
157 
158  size_t s1 = keys_old.size();
159  size_t s2 = NN.getCellSFC().getKeys().size();
160 
161  BOOST_REQUIRE_EQUAL(s1,s2);
162 }
163 
164 BOOST_AUTO_TEST_CASE( ParticleItCRS_Cells_iterator )
165 {
167 
168  const size_t dim = 3;
169 
170  size_t div[dim] = {4,5,6};
171  size_t div_p[dim] = {6,7,8};
172 
173  grid_sm<3,void> gs(div_p);
174 
175  //Number of particles
176  size_t k = 300;
177 
179 
180  Box<dim,float> box;
181 
182  for (size_t i = 0; i < dim; i++)
183  {
184  box.setLow(i,0.0);
185  box.setHigh(i,1.0);
186  }
187 
188  // Initialize a cell list
190 
191  NN.Initialize(box,div,1);
192 
193  FillCellList<dim>(k,NN);
194 
195  float pos[dim];
196 
197  grid_key_dx<3> start(0,0,0);
198  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));
199 
202 
203  // No anomalous cells + all domain cells
204  grid_key_dx_iterator_sub<dim> it(gs,start,stop);
205 
206  while (it.isNext())
207  {
208  auto key = it.get();
209 
210  dom.add(gs.LinId(key));
211 
212  ++it;
213  }
214 
215  //Test the iterator
217 
218  size_t count = 0;
219 
220  while (it_cl.isNext())
221  {
222  count++;
223  ++it_cl;
224  }
225 
226  BOOST_REQUIRE_EQUAL(count,k);
227 
229 
230  NN.clear();
231 
232  grid_key_dx<3> start2(1,1,1);
233  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));
234 
235  //Fill with particles
236  for (size_t i = 0; i < k; i++)
237  {
238  pos[0] = 0.999*rand()/double(RAND_MAX) + 0.0001;
239  pos[1] = 0.999*rand()/double(RAND_MAX) + 0.0001;
240  pos[2] = 0.999*rand()/double(RAND_MAX) + 0.0001;
241 
242  NN.add(pos,i);
243  }
244 
245  dom.clear();
246 
247  bool alternate = false;
248 
249  // No anomalous cells + all domain cells
250  grid_key_dx_iterator_sub<dim> it2(gs,start,stop);
251 
252  while (it2.isNext())
253  {
254  auto key = it2.get();
255 
256  if (alternate == false)
257  {
258  dom.add(gs.LinId(key));
259  alternate = true;
260  }
261  else
262  {
263  anom.add();
264  anom.last().subsub = gs.LinId(key);
265  alternate = false;
266  }
267 
268  ++it2;
269  }
270 
272 
273  count = 0;
274 
275  while (it_cl2.isNext())
276  {
277  count++;
278  ++it_cl2;
279  }
280 
281  BOOST_REQUIRE_EQUAL(count,k);
282 }
283 
284 BOOST_AUTO_TEST_CASE( ParticleIt_Cells_NN_iterator )
285 {
287 
288  const size_t dim = 3;
289 
290  size_t div[dim] = {4,5,6};
291  size_t div_p[dim] = {6,7,8};
292 
293  grid_sm<3,void> gs2(div);
294  grid_sm<3,void> gs(div_p);
295 
297 
298  Box<dim,float> box;
299 
300  for (size_t i = 0; i < dim; i++)
301  {
302  box.setLow(i,0.0);
303  box.setHigh(i,1.0);
304  }
305 
306  // Initialize a cell list
308 
309  NN.Initialize(box,div,1);
310 
311  grid_key_dx_iterator<3> it(gs2);
312 
313  float spacing[dim];
314  float middle[dim];
315  for (size_t i = 0 ; i < dim ; i++)
316  {
317  spacing[i] = box.getHigh(i) / div[i];
318  middle[i] = spacing[i]/2;
319  }
320 
321  size_t cnt = 0;
322 
323  //Fill with particles
325  while (it.isNext())
326  {
327  auto key = it.get();
328 
329  Point<3,float> p;
330 
331  p.get(0) = key.get(0)*spacing[0] + middle[0];
332  p.get(1) = key.get(1)*spacing[1] + middle[1];
333  p.get(2) = key.get(2)*spacing[2] + middle[2];
334 
335  vp.add(p);
336 
337  NN.add(p,cnt);
338  cnt++;
339 
340  ++it;
341  }
342 
343  grid_key_dx<3> start2(2,2,2);
344  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));
345 
348 
349  // No anomalous cells + all domain cells
350  grid_key_dx_iterator_sub<dim> it2(gs,start2,stop2);
351 
352  bool alternate = false;
353  while (it2.isNext())
354  {
355  auto key = it2.get();
356 
357  if (alternate == false)
358  {
359  dom.add(gs.LinId(key));
360  alternate = true;
361  }
362  else
363  {
364  anom.add();
365  anom.last().subsub = gs.LinId(key);
366 
367  for(size_t j = 0 ; j < openfpm::math::pow(3,dim)/2+1 ; j++)
368  {anom.last().NN_subsub.add(NN.getNNc_sym()[j]);}
369 
370  alternate = false;
371  }
372 
373  ++it2;
374  }
375 
376  //Test the iterator
378 
379  size_t count = 0;
380 
381  while (it_cl.isNext())
382  {
383  auto NN_it = it_cl.getNNIteratorCSR(vp);
384 
385  size_t size_NN = 0;
386 
387  while (NN_it.isNext())
388  {
389  size_NN++;
390 
391  ++NN_it;
392  }
393 
394  BOOST_REQUIRE_EQUAL(size_NN,14ul);
395 
396  count++;
397  ++it_cl;
398  }
399 
400  BOOST_REQUIRE_EQUAL(count,(div[0]-2)*(div[1]-2)*(div[2]-2));
401 }
402 
403 BOOST_AUTO_TEST_CASE( ParticleIt_Cells_iterator )
404 {
406 
407  const size_t dim = 3;
408 
409  size_t div[dim] = {4,5,6};
410  size_t div_p[dim] = {6,7,8};
411 
412  grid_sm<3,void> gs(div_p);
413 
414  //Number of particles
415  size_t k = 300;
416 
418 
419  Box<dim,float> box;
420 
421  for (size_t i = 0; i < dim; i++)
422  {
423  box.setLow(i,0.0);
424  box.setHigh(i,1.0);
425  }
426 
427  // Initialize a cell list
429 
430  NN.Initialize(box,div,1);
431 
432  FillCellList<dim>(k,NN);
433 
434  grid_key_dx<3> start(0,0,0);
435  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));
436 
437 
438  // No anomalous cells + all domain cells
439  grid_key_dx_iterator_sub<dim> it(gs,start,stop);
440 
442 
443  while (it.isNext())
444  {
445  auto key = it.get();
446 
447  dom.add(gs.LinId(key));
448 
449  ++it;
450  }
451 
452  //Test the iterator
454 
455  size_t count = 0;
456 
457  while (it_cl.isNext())
458  {
459  count++;
460  ++it_cl;
461  }
462 
463  BOOST_REQUIRE_EQUAL(count,290ul);
464 }
465 
466 BOOST_AUTO_TEST_SUITE_END()
467 
468 #endif /* OPENFPM_DATA_SRC_NN_CELLLIST_CELLLISTITERATOR_TEST_HPP_ */
void add(const T(&pos)[dim], typename base::value_type ele)
Add an element in the cell list.
Definition: CellList.hpp:662
grid_key_dx is the key to access any element in the grid
Definition: grid_key.hpp:18
CellListType::SymNNIterator getNNIteratorCSR(const openfpm::vector< Point< dim, typename CellListType::stype >> &v) const
Get the neighborhood iterator according to the CRS scheme.
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:434
T getHigh(int i) const
get the high interval of the box
Definition: Box.hpp:490
void setHigh(int i, T val)
set the high interval of the box
Definition: Box.hpp:467
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:22
const NNc_array< dim,(unsigned int) openfpm::math::pow(3, dim)/2+1 > & getNNc_sym() const
Get the symmetric neighborhood.
Definition: CellList.hpp:965
This iterator iterate across the particles of a Cell-list following the Cell structure.
void Initialize(const Box< dim, T > &box, const size_t(&div)[dim], const size_t pad=1, size_t slot=STARTING_NSLOT)
size_t getPadding(size_t i) const
Return the number of padding cells of the Cell decomposer.
Definition: CellList.hpp:977
const Prock< dim, CellList_gen< dim, T, Prock, Mem_type, transform, base > > & getCellSFC() const
Get the space filling curve object.
const T & get(size_t i) const
Get coordinate.
Definition: Point.hpp:142
void setLow(int i, T val)
set the low interval of the box
Definition: Box.hpp:456
This class represent an N-dimensional box.
Definition: Box.hpp:56
This class is a trick to indicate the compiler a specific specialization pattern. ...
Definition: memory_c.hpp:201
void clear()
Clear the cell list.
Definition: CellList.hpp:996
Prock< dim, CellList_gen< dim, T, Prock, Mem_type, transform, base > >::Pit getIterator()
return the celllist iterator (across cells)
Declaration grid_sm.
Definition: grid_sm.hpp:71
This iterator iterate across the particles of a Cell-list following the Cell structure.
void set_gm(size_t g_m)
Set the ghost marker.
size_t get_gm()
return the ghost marker
Class for FAST cell list implementation.
Definition: CellList.hpp:269