OpenFPM_data  0.1.0
Project that contain the implementation and interfaces for basic structure like vectors, grids, graph ... .
 All Data Structures Namespaces Functions Variables Typedefs Friends
CellList_test.hpp
1 /*
2  * CellList_test.hpp
3  *
4  * Created on: Mar 23, 2015
5  * Author: Pietro Incardona
6  */
7 
8 #include "CellList.hpp"
9 #include "Grid/grid_sm.hpp"
10 
11 #ifndef CELLLIST_TEST_HPP_
12 #define CELLLIST_TEST_HPP_
13 
14 
20 template<unsigned int dim, typename T, typename CellS> void Test_cell_s()
21 {
23  //Space where is living the Cell list
24  SpaceBox<dim,T> box({0.0f,0.0f,0.0f},{1.0f,1.0f,1.0f});
25 
26  // Subdivisions
27  size_t div[dim] = {16,16,16};
28 
29  // Origin
30  Point<dim,T> org({0.0,0.0,0.0});
31 
32  // id Cell list
33  CellS cl2(box,div,org);
35 
36  // grid info
37  grid_sm<dim,void> g_info(div);
38 
39  // Test force reallocation in case of Cell list fast
40  for (size_t i = 0 ; i < CELL_REALLOC * 3 ; i++)
41  {
42  cl2.add(org,i);
43  }
44 
45  // Check the elements
46  BOOST_REQUIRE_EQUAL(cl2.getNelements(cl2.getCell(org)),CELL_REALLOC * 3ul);
47  for (size_t i = 0 ; i < CELL_REALLOC * 3 ; i++)
48  {
49  BOOST_REQUIRE_EQUAL(cl2.get(cl2.getCell(org),i),i);
50  }
51 
53 
54  // id Cell list
55  CellS cl1(box,div,org);
56 
57  // Create a grid iterator
58  grid_key_dx_iterator<dim> g_it(g_info);
59 
60  // Iterate through each element
61  // Add 1 element for each cell
62 
63  // Usefull definition of points
64  Point<dim,T> end = box.getP2();
65  Point<dim,T> middle = end / div / 2.0;
66  Point<dim,T> spacing = end / div;
67 
68  Point<dim,T> offset[dim] = {middle,middle,middle};
69 
70  // Create offset shift vectors
71  for (size_t i = 0 ; i < dim ; i++)
72  {
73  offset[i].get(i) += (1.0 / div[i]) / 8.0;
74  }
75 
76  size_t id = 0;
77 
78  while (g_it.isNext())
79  {
80  // Add 2 particles on each cell
81 
82  Point<dim,T> key = Point<dim,T>(g_it.get().toPoint());
83  key = key * spacing + offset[0];
84 
85  cl1.add(key,id);
86  ++id;
87 
88  key = Point<dim,T>(g_it.get().toPoint());
89  key = key * spacing + offset[1];
90 
91  cl1.add(key,id);
92  ++id;
93 
94  ++g_it;
95  }
96 
98 
99  // check the cell are correctly filled
100 
101  // reset iterator
102  g_it.reset();
103 
104  while (g_it.isNext())
105  {
106  // Add 2 particles on each cell
107 
108  Point<dim,T> key = Point<dim,T>(g_it.get().toPoint());
109  key = key * spacing + offset[2];
110 
111  size_t cell = cl1.getCell(key);
112  size_t n_ele = cl1.getNelements(cell);
113 
114  BOOST_REQUIRE_EQUAL(n_ele,2ul);
115  BOOST_REQUIRE_EQUAL(cl1.get(cell,1) - cl1.get(cell,0),1);
116 
117  ++g_it;
118  }
119 
120  // reset itarator
121  g_it.reset();
122 
124 
125  while (g_it.isNext())
126  {
127  // remove 1 particle on each cell
128 
129  Point<dim,T> key = Point<dim,T>(g_it.get().toPoint());
130  key = key * spacing + offset[0];
131 
132  auto cell = cl1.getCell(key);
133 
134  // Remove the first particle in the cell
135  cl1.remove(cell,0);
136  ++g_it;
137  }
138 
140 
141  // Check we have 1 object per cell
142  g_it.reset();
143 
144  while (g_it.isNext())
145  {
146  // remove 1 particle on each cell
147 
148  Point<dim,T> key = Point<dim,T>(g_it.get().toPoint());
149  key = key * spacing + offset[0];
150 
151  auto cell = cl1.getCell(key);
152  size_t n_ele = cl1.getNelements(cell);
153 
154  BOOST_REQUIRE_EQUAL(n_ele,1ul);
155  ++g_it;
156  }
157 
158 
159  // Check we have 1 object per cell
160 
161  // Create a grid iterator
162  grid_key_dx<dim> p1(1,1,1);
163  grid_key_dx<dim> p2(div[0]-2,div[1]-2,div[2]-2);
164  grid_key_dx_iterator_sub<dim> g_it_s(g_info,p1,p2);
165 
166  while (g_it_s.isNext())
167  {
168  // remove 1 particle on each cell
169 
171 
172  Point<dim,T> key = Point<dim,T>(g_it_s.get().toPoint());
173  key = key * spacing + offset[0];
174 
175  auto NN = cl1.template getNNIterator<NO_CHECK>(cl1.getCell(key));
176  size_t total = 0;
177 
178  while(NN.isNext())
179  {
180  // total
181 
182  total++;
183 
184  ++NN;
185  }
186 
188 
189  BOOST_REQUIRE_EQUAL(total,(size_t)openfpm::math::pow(3,dim));
190  ++g_it_s;
191  }
192 
193 }
194 
195 BOOST_AUTO_TEST_SUITE( CellList_test )
196 
197 BOOST_AUTO_TEST_CASE( CellDecomposer_use )
198 {
200  //Space where is living the Cell list
201  SpaceBox<3,double> box({0.0f,0.0f,0.0f},{1.0f,1.0f,1.0f});
202  Point<3,double> p({0.5,0.5,0.5});
203  double pp[3] = {0.5,0.5,0.5};
204 
205  // Number of cell on each dimension
206  size_t div[3] = {16,16,16};
207 
208  // grid info
209  grid_sm<3,void> g_info(div);
210 
211  // Origin
212  Point<3,double> org({0.0,0.0,0.0});
213 
214  // Test Cell decomposer
215  {
216  CellDecomposer_sm<3,double> cd(box,div,0);
217  size_t cell = cd.getCell(p);
218  BOOST_REQUIRE_EQUAL(cell,(size_t)(8*16*16 + 8*16 + 8));
219  auto key = cd.getCellGrid(p);
220  BOOST_REQUIRE_EQUAL(key.get(0),8);
221  BOOST_REQUIRE_EQUAL(key.get(1),8);
222  BOOST_REQUIRE_EQUAL(key.get(2),8);
223 
224  cell = cd.getCell(pp);
225  BOOST_REQUIRE_EQUAL(cell,(size_t)(8*16*16 + 8*16 + 8));
226  key = cd.getCellGrid(pp);
227  BOOST_REQUIRE_EQUAL(key.get(0),8);
228  BOOST_REQUIRE_EQUAL(key.get(1),8);
229  BOOST_REQUIRE_EQUAL(key.get(2),8);
230  }
231 
233 
235  {
236  CellDecomposer_sm<3,double> cd(box,div,1);
237  size_t cell = cd.getCell(p);
238  BOOST_REQUIRE_EQUAL(cell,(size_t)(9*18*18 + 9*18 + 9));
239  auto key = cd.getCellGrid(p);
240  BOOST_REQUIRE_EQUAL(key.get(0),9);
241  BOOST_REQUIRE_EQUAL(key.get(1),9);
242  BOOST_REQUIRE_EQUAL(key.get(2),9);
243 
244  cell = cd.getCell(pp);
245  BOOST_REQUIRE_EQUAL(cell,(size_t)(9*18*18 + 9*18 + 9));
246  key = cd.getCellGrid(pp);
247  BOOST_REQUIRE_EQUAL(key.get(0),9);
248  BOOST_REQUIRE_EQUAL(key.get(1),9);
249  BOOST_REQUIRE_EQUAL(key.get(2),9);
250  }
251 
253 
255  {
256  Point<3,double> sht({1.0,2.0,3.0});
257  double psht[3] = {1.5,2.5,3.5};
258 
259  CellDecomposer_sm< 3,double,shift<3,double> > cd(box,div,sht,1);
260  size_t cell = cd.getCell(p + sht);
261  BOOST_REQUIRE_EQUAL(cell,(size_t)(9*18*18 + 9*18 + 9));
262  auto key = cd.getCellGrid(p + sht);
263  BOOST_REQUIRE_EQUAL(key.get(0),9);
264  BOOST_REQUIRE_EQUAL(key.get(1),9);
265  BOOST_REQUIRE_EQUAL(key.get(2),9);
266 
267  cell = cd.getCell(psht);
268  BOOST_REQUIRE_EQUAL(cell,(size_t)(9*18*18 + 9*18 + 9));
269  key = cd.getCellGrid(psht);
270  BOOST_REQUIRE_EQUAL(key.get(0),9);
271  BOOST_REQUIRE_EQUAL(key.get(1),9);
272  BOOST_REQUIRE_EQUAL(key.get(2),9);
273  }
274 
276 
277 
278 }
279 
280 BOOST_AUTO_TEST_CASE( CellList_use)
281 {
282  std::cout << "Test cell list" << "\n";
283 
284  Test_cell_s<3,double,CellList<3,double,FAST>>();
285 // Test_cell_s<3,double,CellList<3,double,BALANCED>>();
286 // Test_cell_s<3,double,CellList<3,double,MEMORY>>();
287 
288  std::cout << "End cell list" << "\n";
289 
290  // Test the cell list
291 }
292 
293 BOOST_AUTO_TEST_CASE( CellDecomposer_get_grid_points )
294 {
295  {
296  // Cell decomposer
297  CellDecomposer_sm<3,float> cd;
298 
299  // Box
300  Box<3,float> box({0.0,0.0,0.0},{1.0,1.0,1.0});
301 
302  // Divisions
303  size_t div[] = {10,10,10};
304 
305  // padding
306  size_t padding = 1;
307 
308  // Set the dimensions of the decomposer
309  cd.setDimensions(box,div,padding);
310 
311  Box<3,float> box_small({0.2,0.3,0.4},{0.5,0.5,0.6});
312 
313  // Get the grid points box
314  Box<3,size_t> gp = cd.getGridPoints(box_small);
315 
316  BOOST_REQUIRE_EQUAL(gp.getLow(0),2+padding);
317  BOOST_REQUIRE_EQUAL(gp.getLow(1),3+padding);
318  BOOST_REQUIRE_EQUAL(gp.getLow(2),4+padding);
319 
320  BOOST_REQUIRE_EQUAL(gp.getHigh(0),5+padding-1);
321  BOOST_REQUIRE_EQUAL(gp.getHigh(1),5+padding-1);
322  BOOST_REQUIRE_EQUAL(gp.getHigh(2),6+padding-1);
323 
324  // Get the volume of the box
325  size_t vol = gp.getVolumeKey();
326 
327  BOOST_REQUIRE_EQUAL(vol,12ul);
328  }
329 
331  // 2D test
333 
334  {
335  // Cell decomposer
336  CellDecomposer_sm<2,float> cd;
337 
338  // Box
339  Box<2,float> box({0.0,0.0},{1.0,1.0});
340 
341  // Divisions
342  size_t div[] = {5,5};
343 
344  // padding
345  size_t padding = 1;
346 
347  // Set the dimensions of the decomposer
348  cd.setDimensions(box,div,padding);
349 
350  Box<2,float> box_small({0.4,0.4},{0.8,0.8});
351 
352  // Get the grid points box
353  Box<2,size_t> gp = cd.getGridPoints(box_small);
354 
355  BOOST_REQUIRE_EQUAL(gp.getLow(0),2+padding);
356  BOOST_REQUIRE_EQUAL(gp.getLow(1),2+padding);
357 
358  BOOST_REQUIRE_EQUAL(gp.getHigh(0),3+padding);
359  BOOST_REQUIRE_EQUAL(gp.getHigh(1),3+padding);
360 
361  // Get the volume of the box
362  size_t vol = gp.getVolumeKey();
363 
364  BOOST_REQUIRE_EQUAL(vol,4ul);
365  }
366 }
367 
368 BOOST_AUTO_TEST_SUITE_END()
369 
370 #endif /* CELLLIST_TEST_HPP_ */
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:484
grid_key_dx is the key to access any element in the grid
Definition: grid_key.hpp:18
T getHigh(int i) const
get the high interval of the box
Definition: Box.hpp:495
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:20
T get(int i) const
Get coordinate.
Definition: Point.hpp:42
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:202
T getVolumeKey() const
Get the volume spanned by the Box P1 and P2 interpreted as grid key.
Definition: Box.hpp:1011