OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
VerletList_test.hpp
1 /*
2  * VerletList_test.hpp
3  *
4  * Created on: Aug 16, 2016
5  * Author: i-bird
6  */
7 
8 #ifndef OPENFPM_DATA_SRC_NN_VERLETLIST_VERLETLIST_TEST_HPP_
9 #define OPENFPM_DATA_SRC_NN_VERLETLIST_VERLETLIST_TEST_HPP_
10 
11 #include "NN/VerletList/VerletList.hpp"
12 #include "NN/VerletList/VerletListM.hpp"
13 
20 template<unsigned int dim, typename T> void create_particles_on_grid(grid_sm<dim,void> & g_info, SpaceBox<dim,T> & box, openfpm::vector<Point<dim,T>> & v)
21 {
22  // Create a grid iterator
23  grid_key_dx_iterator<dim> g_it(g_info);
24 
25  // Usefull definition of points
26  Point<dim,T> end = box.getP2() - box.getP1();
27 
28  Point<dim,T> middle;
29  Point<dim,T> spacing;
30 
31  for (size_t i = 0 ; i < dim ; i++)
32  {
33  middle.get(i) = end.get(i) / g_info.size(i) / 2.0;
34  spacing.get(i) = end.get(i) / g_info.size(i);
35  }
36 
37  while (g_it.isNext())
38  {
39  // Add 2 particles on each cell
40 
41  Point<dim,T> key = Point<dim,T>(g_it.get().toPoint());
42  key = pmul(key,spacing) + box.getP1() + middle;
43 
44  v.add(key);
45 
46  ++g_it;
47  }
48 }
49 
56 template<unsigned int dim, typename T> void create_particles_on_gridM(grid_sm<dim,void> & g_info, T r_cut, SpaceBox<dim,T> & box, openfpm::vector<pos_v<dim,T>> & v, CellListM<dim,T,2> & cl)
57 {
58  // Create a grid iterator
59  grid_key_dx_iterator<dim> g_it(g_info);
60 
61  // Usefull definition of points
62  Point<dim,T> end = box.getP2() - box.getP1();
63 
64  Point<dim,T> middle;
65  Point<dim,T> spacing;
66 
67  Point<dim,T> offset[dim];
68 
69  size_t div[dim];
70  for (size_t i = 0 ; i < dim ; i++)
71  {
72  offset[i] = middle;
73  div[i] = 20 - 1;
74  }
75 
76  for (size_t i = 0 ; i < dim ; i++)
77  offset[i].get(i) += (1.0 / g_info.size(i)) / 32.0;
78 
79  for (size_t i = 0 ; i < dim ; i++)
80  {
81  middle.get(i) = end.get(i) / g_info.size(i) / 2.0;
82  spacing.get(i) = end.get(i) / g_info.size(i);
83  }
84 
85  cl.Initialize(box,div);
86 
87  while (g_it.isNext())
88  {
89  // Add 2 particles on each cell
90 
91  Point<dim,T> key = Point<dim,T>(g_it.get().toPoint());
92  key = pmul(key,spacing) + box.getP1() + middle;
93 
94  v.get(0).pos.add(Point<dim,T>(key + offset[0]));
95  v.get(1).pos.add(Point<dim,T>(key + offset[1]));
96  v.get(2).pos.add(Point<dim,T>(key + offset[2]));
97 
98  cl.add(Point<dim,T>(key + offset[0]),v.get(0).pos.size()-1,0);
99  cl.add(Point<dim,T>(key + offset[1]),v.get(1).pos.size()-1,1);
100  cl.add(Point<dim,T>(key + offset[2]),v.get(2).pos.size()-1,2);
101 
102  ++g_it;
103  }
104 }
105 
111 template<unsigned int dim, typename T, typename VerS> void Verlet_list_s(SpaceBox<dim,T> & box)
112 {
113  T r_cut = 1.506 * (box.getHigh(0) - box.getLow(0)) / 20;
114 
115  for (size_t k = 0 ; k < 5; k++)
116  {
117  // id Cell list
118 
119  size_t div[dim];
120  for (size_t i = 0 ; i < dim ; i++)
121  div[i] = 20;
122 
123  grid_sm<3,void> ginfo(div);
124 
126  create_particles_on_grid(ginfo,box,pos);
127 
128  VerS vl1;
129 
130  Box<dim,T> innerBox;
131  for (size_t i = 0 ; i < dim ; i++)
132  {
133  innerBox.setLow(i,r_cut);
134  innerBox.setHigh(i,1.0-r_cut);
135  }
136 
137  vl1.Initialize(box,box,r_cut,pos,pos.size());
138 
139  // Check that the verlet is consistent
140  for (size_t i = 0 ; i < pos.size() ; i++)
141  {
142  if (innerBox.isInside(pos.get(i)) == false)
143  continue;
144 
145  Point<dim,T> p = pos.get(i);
146 
147  if (k == 0)
148  {BOOST_REQUIRE_EQUAL(vl1.getNNPart(i),19ul);}
149 
150  auto NN = vl1.getNNIterator(i);
151 
152  bool ret = true;
153 
154  while (NN.isNext())
155  {
156  auto k = NN.get();
157 
158  T dist = p.distance(Point<dim,T>(pos.get(k)));
159 
160  ret &= (dist < r_cut);
161 
162  ++NN;
163  }
164 
165  BOOST_REQUIRE_EQUAL(ret,true);
166  }
167 
168  r_cut += 0.075;
169  }
170 
171  r_cut = 1.506 * (box.getHigh(0) - box.getLow(0)) / 20;
172 
173  size_t div[dim];
174  for (size_t i = 0 ; i < dim ; i++)
175  div[i] = 20-1;
176 
177  grid_sm<3,void> ginfo(div);
178 
180  create_particles_on_grid(ginfo,box,pos);
181 
183 
184  // Initialize an external cell-list
186  Box<dim,T> bt = box;
187 
188  // Calculate the divisions for the Cell-lists
189  cl_param_calculate(bt,div,r_cut,Ghost<dim,T>(0.0));
190 
191  // Initialize a cell-list
192  cli.Initialize(bt,div,5);
193 
194  for (size_t i = 0; i < pos.size() ; i++)
195  cli.add(pos.get(i), i);
196 
198 
199  // Create cell-list
200 
201  for (size_t k = 0 ; k < 4; k++)
202  {
204 
205  VerS vl1;
206  vl1.Initialize(cli,r_cut,pos,pos,pos.size());
207 
209 
211 
212  VerS vl2;
213  vl2.Initialize(box,box,r_cut,pos,pos.size());
214 
216 
217  Box<dim,T> innerBox;
218  for (size_t i = 0 ; i < dim ; i++)
219  {
220  innerBox.setLow(i,r_cut);
221  innerBox.setHigh(i,1.0-r_cut);
222  }
223 
224  // Check that the verlet is consistent
225  for (size_t i = 0 ; i < pos.size() ; i++)
226  {
227  if (innerBox.isInside(pos.get(i)) == false)
228  continue;
229 
230  Point<dim,T> p = pos.get(i);
231 
232  if (k == 0)
233  {BOOST_REQUIRE_EQUAL(vl1.getNNPart(i),19ul);}
234  BOOST_REQUIRE_EQUAL(vl1.getNNPart(i),vl2.getNNPart(i));
235 
238 
240 
241  bool ret = true;
242  auto NN = vl1.getNNIterator(i);
243  while (NN.isNext())
244  {
245  auto k = NN.get();
246 
247  T dist = p.distance(Point<dim,T>(pos.get(k)));
248 
249  ret &= (dist < r_cut);
250 
251  v1.add(k);
252 
253  ++NN;
254  }
255 
257 
258  auto NN2 = vl2.getNNIterator(i);
259  while (NN2.isNext())
260  {
261  auto k = NN2.get();
262 
263  v2.add(k);
264 
265  ++NN2;
266  }
267 
268  ret = true;
269  v1.sort();
270  v2.sort();
271  for (size_t i = 0 ; i < v1.size(); i++)
272  {
273  ret &= v1.get(i) == v2.get(i);
274  }
275 
276  BOOST_REQUIRE_EQUAL(ret,true);
277  }
278 
279  r_cut += 0.075;
280  }
281 }
282 
283 
289 template<unsigned int dim, typename T, typename VerS> void Verlet_list_sM(SpaceBox<dim,T> & box)
290 {
291  T r_cut = (box.getHigh(0) - box.getLow(0)) / 20;
292 
293  for (size_t k = 0 ; k <= 0 ; k++)
294  {
295  // id Cell list
296 
297  size_t div[dim];
298  for (size_t i = 0 ; i < dim ; i++)
299  div[i] = 31;
300 
301  grid_sm<3,void> ginfo(div);
302 
306 
308  pos2.add(pos_v<dim,T>(ps1));
309  pos2.add(pos_v<dim,T>(ps2));
310  pos2.add(pos_v<dim,T>(ps3));
311 
313  create_particles_on_gridM(ginfo,r_cut,box,pos2,cl);
314 
315  VerS vl1;
316 
317  Box<dim,T> innerBox;
318  for (size_t i = 0 ; i < dim ; i++)
319  {
320  innerBox.setLow(i,r_cut);
321  innerBox.setHigh(i,1.0-r_cut);
322  }
323 
324  vl1.Initialize(cl,0,r_cut,pos2.get(0).pos,pos2,pos2.get(0).pos.size());
325 
326  // Check that the verlet is consistent
327  for (size_t i = 0 ; i < pos2.get(0).pos.size() ; i++)
328  {
329  if (innerBox.isInside(pos2.get(0).pos.get(i)) == false)
330  continue;
331 
332  Point<dim,T> p = pos2.get(0).pos.get(i);
333 
334  if (k == 0)
335  {BOOST_REQUIRE_EQUAL(vl1.getNNPart(i),57ul);}
336 
337  auto NN = vl1.getNNIterator(i);
338 
339  bool ret = true;
340 
341  while (NN.isNext())
342  {
343  auto k = NN.getP();
344  auto v = NN.getV();
345 
346  T dist = p.distance(Point<dim,T>(pos2.get(v).pos.get(k)));
347 
348  ret &= (dist < r_cut);
349 
350  ++NN;
351  }
352 
353  BOOST_REQUIRE_EQUAL(ret,true);
354  }
355 
356  r_cut += 0.075;
357  }
358 
359  r_cut = (box.getHigh(0) - box.getLow(0)) / 20;
360 
361  size_t div[dim];
362  for (size_t i = 0 ; i < dim ; i++)
363  div[i] = 31;
364 
365  grid_sm<3,void> ginfo(div);
366 
367  // Initialize an external cell-list
368  CellListM<dim,T,2> cli;
369 
370 
374 
376  pos2.add(pos_v<dim,T>(ps1));
377  pos2.add(pos_v<dim,T>(ps2));
378  pos2.add(pos_v<dim,T>(ps3));
379 
380  create_particles_on_gridM(ginfo,r_cut,box,pos2,cli);
381 
383 
385 
386  // Create cell-list
387 
388  for (size_t k = 0 ; k <= 0 ; k++)
389  {
391 
392  VerS vl1;
393  vl1.Initialize(cli,0,r_cut,pos2.get(0).pos,pos2,pos2.get(0).pos.size());
394 
396 
398 
399  VerS vl2;
400  vl2.Initialize(cli,0,r_cut,pos2.get(0).pos,pos2,pos2.get(0).pos.size());
401 
403 
404  Box<dim,T> innerBox;
405  for (size_t i = 0 ; i < dim ; i++)
406  {
407  innerBox.setLow(i,r_cut);
408  innerBox.setHigh(i,1.0-r_cut);
409  }
410 
411  // Check that the verlet is consistent
412  for (size_t i = 0 ; i < pos2.get(0).pos.size() ; i++)
413  {
414  if (innerBox.isInside(pos2.get(0).pos.get(i)) == false)
415  continue;
416 
417  Point<dim,T> p = pos2.get(0).pos.get(i);
418 
419  if (k == 0)
420  {BOOST_REQUIRE_EQUAL(vl1.getNNPart(i),57ul);}
421  BOOST_REQUIRE_EQUAL(vl1.getNNPart(i),vl2.getNNPart(i));
422 
425 
427 
428  bool ret = true;
429  auto NN = vl1.getNNIterator(i);
430  while (NN.isNext())
431  {
432  auto k = NN.getP();
433  auto v = NN.getV();
434 
435  T dist = p.distance(Point<dim,T>(pos2.get(v).pos.get(k)));
436 
437  ret &= (dist < r_cut);
438 
439  v1.add(NN.get());
440 
441  ++NN;
442  }
443 
445 
446  auto NN2 = vl2.getNNIterator(i);
447  while (NN2.isNext())
448  {
449  auto k = NN2.get();
450 
451  v2.add(k);
452 
453  ++NN2;
454  }
455 
456  ret = true;
457  v1.sort();
458  v2.sort();
459  for (size_t i = 0 ; i < v1.size(); i++)
460  {
461  ret &= v1.get(i) == v2.get(i);
462  }
463 
464  BOOST_REQUIRE_EQUAL(ret,true);
465  }
466 
467  r_cut += 0.075;
468  }
469 }
470 
471 
472 BOOST_AUTO_TEST_SUITE( VerletList_test )
473 
474 BOOST_AUTO_TEST_CASE( VerletList_use)
475 {
476  std::cout << "Test verlet list" << "\n";
477 
478  SpaceBox<3,double> box({0.0f,0.0f,0.0f},{1.0f,1.0f,1.0f});
479  SpaceBox<3,double> box2({-1.0f,-1.0f,-1.0f},{1.0f,1.0f,1.0f});
480 // Verlet_list_s<3,double,VerletList<3,double,FAST,shift<3,double>>>(box);
481 // Verlet_list_s<3,double,VerletList<3,double,FAST,shift<3,double>> >(box2);
482  Verlet_list_sM<3,double,VerletListM<3,double,2>>(box);
483 // Verlet_list_sM<3,double,CellListM<3,double,8>>(box2);
484 
485  std::cout << "End verlet list" << "\n";
486 
487  // Test the cell list
488 }
489 
490 BOOST_AUTO_TEST_SUITE_END()
491 
492 
493 
494 #endif /* OPENFPM_DATA_SRC_NN_VERLETLIST_VERLETLIST_TEST_HPP_ */
void add(const T(&pos)[dim], typename base::value_type ele)
Add an element in the cell list.
Definition: CellList.hpp:662
This class represent an N-dimensional box.
Definition: SpaceBox.hpp:26
T getLow(int i) const
get the i-coordinate of the low bound interval of the box
Definition: Box.hpp:479
bool isInside(const Point< dim, T > &p) const
Check if the point is inside the box.
Definition: Box.hpp:880
size_t size() const
Return the size of the grid.
Definition: grid_sm.hpp:572
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
size_t size()
Stub size.
Definition: map_vector.hpp:70
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:22
Structure that contain a reference to a vector of particles.
Point< dim, T > getP1() const
Get the point p1.
Definition: Box.hpp:605
Definition: Ghost.hpp:39
Class for Multi-Phase cell-list.
Definition: CellListM.hpp:53
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 add(const T(&pos)[dim], size_t ele, size_t v_id)
Add an element in the cell list.
Definition: CellListM.hpp:145
T distance(const Point< dim, T > &q)
It calculate the distance between 2 points.
Definition: Point.hpp:221
Implementation of 1-D std::vector like structure.
Definition: map_vector.hpp:61
Class for FAST cell list implementation.
Definition: CellList.hpp:269
Point< dim, T > getP2() const
Get the point p2.
Definition: Box.hpp:619