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 | |
14 | /*! \brief create a vector of particles on a grid between 0.0 and 1.0 |
15 | * |
16 | * \param box where the box is defined |
17 | * \param v vector of positions |
18 | * |
19 | */ |
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 | |
50 | /*! \brief create a vector of particles on a grid between 0.0 and 1.0 |
51 | * |
52 | * \param box where the box is defined |
53 | * \param v vector of positions |
54 | * |
55 | */ |
56 | template<unsigned int dim, typename T> void create_particles_on_gridM(grid_sm<dim,void> & g_info, |
57 | T r_cut, |
58 | SpaceBox<dim,T> & box, |
59 | openfpm::vector<pos_v<openfpm::vector<Point<dim,T>>>> & v, |
60 | CellListM<dim,T,2> & cl) |
61 | { |
62 | // Create a grid iterator |
63 | grid_key_dx_iterator<dim> g_it(g_info); |
64 | |
65 | // Usefull definition of points |
66 | Point<dim,T> end = box.getP2() - box.getP1(); |
67 | |
68 | Point<dim,T> middle; |
69 | Point<dim,T> spacing; |
70 | |
71 | Point<dim,T> offset[dim]; |
72 | |
73 | size_t div[dim]; |
74 | for (size_t i = 0 ; i < dim ; i++) |
75 | { |
76 | offset[i] = middle; |
77 | div[i] = 20 - 1; |
78 | } |
79 | |
80 | for (size_t i = 0 ; i < dim ; i++) |
81 | offset[i].get(i) += (1.0 / g_info.size(i)) / 32.0; |
82 | |
83 | for (size_t i = 0 ; i < dim ; i++) |
84 | { |
85 | middle.get(i) = end.get(i) / g_info.size(i) / 2.0; |
86 | spacing.get(i) = end.get(i) / g_info.size(i); |
87 | } |
88 | |
89 | cl.Initialize(box,div); |
90 | |
91 | while (g_it.isNext()) |
92 | { |
93 | // Add 2 particles on each cell |
94 | |
95 | Point<dim,T> key = Point<dim,T>(g_it.get().toPoint()); |
96 | key = pmul(key,spacing) + box.getP1() + middle; |
97 | |
98 | v.get(0).pos.add(Point<dim,T>(key + offset[0])); |
99 | v.get(1).pos.add(Point<dim,T>(key + offset[1])); |
100 | v.get(2).pos.add(Point<dim,T>(key + offset[2])); |
101 | |
102 | cl.add(Point<dim,T>(key + offset[0]),v.get(0).pos.size()-1,0); |
103 | cl.add(Point<dim,T>(key + offset[1]),v.get(1).pos.size()-1,1); |
104 | cl.add(Point<dim,T>(key + offset[2]),v.get(2).pos.size()-1,2); |
105 | |
106 | ++g_it; |
107 | } |
108 | } |
109 | |
110 | /*! \brief Test cell structure |
111 | * |
112 | * \tparam CellS |
113 | * |
114 | */ |
115 | template<unsigned int dim, typename T, typename VerS> void Verlet_list_s(SpaceBox<dim,T> & box) |
116 | { |
117 | T r_cut = 1.506 * (box.getHigh(0) - box.getLow(0)) / 20; |
118 | |
119 | for (size_t k = 0 ; k < 5; k++) |
120 | { |
121 | // id Cell list |
122 | |
123 | size_t div[dim]; |
124 | for (size_t i = 0 ; i < dim ; i++) |
125 | div[i] = 20; |
126 | |
127 | grid_sm<3,void> ginfo(div); |
128 | |
129 | openfpm::vector<Point<dim,T>> pos; |
130 | create_particles_on_grid(ginfo,box,pos); |
131 | |
132 | VerS vl1; |
133 | |
134 | Box<dim,T> innerBox; |
135 | for (size_t i = 0 ; i < dim ; i++) |
136 | { |
137 | innerBox.setLow(i,r_cut); |
138 | innerBox.setHigh(i,1.0-r_cut); |
139 | } |
140 | |
141 | vl1.Initialize(box,box,r_cut,pos,pos.size()); |
142 | |
143 | // Check that the verlet is consistent |
144 | for (size_t i = 0 ; i < pos.size() ; i++) |
145 | { |
146 | if (innerBox.isInside(pos.get(i)) == false) |
147 | continue; |
148 | |
149 | Point<dim,T> p = pos.get(i); |
150 | |
151 | if (k == 0) |
152 | {BOOST_REQUIRE_EQUAL(vl1.getNNPart(i),19ul);} |
153 | |
154 | auto NN = vl1.getNNIterator(i); |
155 | |
156 | bool ret = true; |
157 | |
158 | while (NN.isNext()) |
159 | { |
160 | auto k = NN.get(); |
161 | |
162 | T dist = p.distance(Point<dim,T>(pos.get(k))); |
163 | |
164 | ret &= (dist < r_cut); |
165 | |
166 | ++NN; |
167 | } |
168 | |
169 | BOOST_REQUIRE_EQUAL(ret,true); |
170 | } |
171 | |
172 | r_cut += 0.075; |
173 | } |
174 | |
175 | r_cut = 1.506 * (box.getHigh(0) - box.getLow(0)) / 20; |
176 | |
177 | size_t div[dim]; |
178 | for (size_t i = 0 ; i < dim ; i++) |
179 | div[i] = 20-1; |
180 | |
181 | grid_sm<3,void> ginfo(div); |
182 | |
183 | openfpm::vector<Point<dim,T>> pos; |
184 | create_particles_on_grid(ginfo,box,pos); |
185 | |
186 | //! [Fill external cell list] |
187 | |
188 | // Initialize an external cell-list |
189 | CellList<dim,T,Mem_fast<>,shift<3,double>> cli; |
190 | Box<dim,T> bt = box; |
191 | |
192 | // Calculate the divisions for the Cell-lists |
193 | cl_param_calculate(bt,div,r_cut,Ghost<dim,T>(0.0)); |
194 | |
195 | // Initialize a cell-list |
196 | cli.Initialize(bt,div,5); |
197 | |
198 | for (size_t i = 0; i < pos.size() ; i++) |
199 | cli.add(pos.get(i), i); |
200 | |
201 | //! [Fill external cell list] |
202 | |
203 | // Create cell-list |
204 | |
205 | for (size_t k = 0 ; k < 4; k++) |
206 | { |
207 | //! [create verlet cell] |
208 | |
209 | VerS vl1; |
210 | vl1.Initialize(cli,r_cut,pos,pos,pos.size()); |
211 | |
212 | //! [create verlet cell] |
213 | |
214 | //! [create verlet] |
215 | |
216 | VerS vl2; |
217 | vl2.Initialize(box,box,r_cut,pos,pos.size()); |
218 | |
219 | //! [create verlet] |
220 | |
221 | Box<dim,T> innerBox; |
222 | for (size_t i = 0 ; i < dim ; i++) |
223 | { |
224 | innerBox.setLow(i,r_cut); |
225 | innerBox.setHigh(i,1.0-r_cut); |
226 | } |
227 | |
228 | // Check that the verlet is consistent |
229 | for (size_t i = 0 ; i < pos.size() ; i++) |
230 | { |
231 | if (innerBox.isInside(pos.get(i)) == false) |
232 | continue; |
233 | |
234 | Point<dim,T> p = pos.get(i); |
235 | |
236 | if (k == 0) |
237 | {BOOST_REQUIRE_EQUAL(vl1.getNNPart(i),19ul);} |
238 | BOOST_REQUIRE_EQUAL(vl1.getNNPart(i),vl2.getNNPart(i)); |
239 | |
240 | openfpm::vector<size_t> v1; |
241 | openfpm::vector<size_t> v2; |
242 | |
243 | //! [usage of verlet] |
244 | |
245 | bool ret = true; |
246 | auto NN = vl1.getNNIterator(i); |
247 | while (NN.isNext()) |
248 | { |
249 | auto k = NN.get(); |
250 | |
251 | T dist = p.distance(Point<dim,T>(pos.get(k))); |
252 | |
253 | ret &= (dist < r_cut); |
254 | |
255 | v1.add(k); |
256 | |
257 | ++NN; |
258 | } |
259 | |
260 | //! [usage of verlet] |
261 | |
262 | auto NN2 = vl2.getNNIterator(i); |
263 | while (NN2.isNext()) |
264 | { |
265 | auto k = NN2.get(); |
266 | |
267 | v2.add(k); |
268 | |
269 | ++NN2; |
270 | } |
271 | |
272 | ret = true; |
273 | v1.sort(); |
274 | v2.sort(); |
275 | for (size_t i = 0 ; i < v1.size(); i++) |
276 | { |
277 | ret &= v1.get(i) == v2.get(i); |
278 | } |
279 | |
280 | BOOST_REQUIRE_EQUAL(ret,true); |
281 | } |
282 | |
283 | r_cut += 0.075; |
284 | } |
285 | } |
286 | |
287 | |
288 | /*! \brief Test cell structure |
289 | * |
290 | * \tparam CellS |
291 | * |
292 | */ |
293 | template<unsigned int dim, typename T, typename VerS> void Verlet_list_sM(SpaceBox<dim,T> & box) |
294 | { |
295 | T r_cut = (box.getHigh(0) - box.getLow(0)) / 20; |
296 | |
297 | for (size_t k = 0 ; k <= 0 ; k++) |
298 | { |
299 | // id Cell list |
300 | |
301 | size_t div[dim]; |
302 | for (size_t i = 0 ; i < dim ; i++) |
303 | div[i] = 31; |
304 | |
305 | grid_sm<3,void> ginfo(div); |
306 | |
307 | openfpm::vector<Point<dim,T>> ps1; |
308 | openfpm::vector<Point<dim,T>> ps2; |
309 | openfpm::vector<Point<dim,T>> ps3; |
310 | |
311 | openfpm::vector<pos_v<openfpm::vector<Point<dim,T>>>> pos2; |
312 | pos2.add(pos_v<openfpm::vector<Point<dim,T>>>(ps1)); |
313 | pos2.add(pos_v<openfpm::vector<Point<dim,T>>>(ps2)); |
314 | pos2.add(pos_v<openfpm::vector<Point<dim,T>>>(ps3)); |
315 | |
316 | CellListM<dim,T,2> cl; |
317 | create_particles_on_gridM(ginfo,r_cut,box,pos2,cl); |
318 | |
319 | VerS vl1; |
320 | |
321 | Box<dim,T> innerBox; |
322 | for (size_t i = 0 ; i < dim ; i++) |
323 | { |
324 | innerBox.setLow(i,r_cut); |
325 | innerBox.setHigh(i,1.0-r_cut); |
326 | } |
327 | |
328 | vl1.Initialize(cl,0,r_cut,pos2.get(0).pos,pos2,pos2.get(0).pos.size()); |
329 | |
330 | // Check that the verlet is consistent |
331 | for (size_t i = 0 ; i < pos2.get(0).pos.size() ; i++) |
332 | { |
333 | if (innerBox.isInside(pos2.get(0).pos.get(i)) == false) |
334 | continue; |
335 | |
336 | Point<dim,T> p = pos2.get(0).pos.get(i); |
337 | |
338 | if (k == 0) |
339 | {BOOST_REQUIRE_EQUAL(vl1.getNNPart(i),57ul);} |
340 | |
341 | auto NN = vl1.getNNIterator(i); |
342 | |
343 | bool ret = true; |
344 | |
345 | while (NN.isNext()) |
346 | { |
347 | auto k = NN.getP(); |
348 | auto v = NN.getV(); |
349 | |
350 | T dist = p.distance(Point<dim,T>(pos2.get(v).pos.get(k))); |
351 | |
352 | ret &= (dist < r_cut); |
353 | |
354 | ++NN; |
355 | } |
356 | |
357 | BOOST_REQUIRE_EQUAL(ret,true); |
358 | } |
359 | |
360 | r_cut += 0.075; |
361 | } |
362 | |
363 | r_cut = (box.getHigh(0) - box.getLow(0)) / 20; |
364 | |
365 | size_t div[dim]; |
366 | for (size_t i = 0 ; i < dim ; i++) |
367 | div[i] = 31; |
368 | |
369 | grid_sm<3,void> ginfo(div); |
370 | |
371 | // Initialize an external cell-list |
372 | CellListM<dim,T,2> cli; |
373 | |
374 | |
375 | openfpm::vector<Point<dim,T>> ps1; |
376 | openfpm::vector<Point<dim,T>> ps2; |
377 | openfpm::vector<Point<dim,T>> ps3; |
378 | |
379 | openfpm::vector<pos_v<openfpm::vector<Point<dim,T>>>> pos2; |
380 | pos2.add(pos_v<openfpm::vector<Point<dim,T>>>(ps1)); |
381 | pos2.add(pos_v<openfpm::vector<Point<dim,T>>>(ps2)); |
382 | pos2.add(pos_v<openfpm::vector<Point<dim,T>>>(ps3)); |
383 | |
384 | create_particles_on_gridM(ginfo,r_cut,box,pos2,cli); |
385 | |
386 | //! [Fill external cell list] |
387 | |
388 | //! [Fill external cell list] |
389 | |
390 | // Create cell-list |
391 | |
392 | for (size_t k = 0 ; k <= 0 ; k++) |
393 | { |
394 | //! [create verlet cell] |
395 | |
396 | VerS vl1; |
397 | vl1.Initialize(cli,0,r_cut,pos2.get(0).pos,pos2,pos2.get(0).pos.size()); |
398 | |
399 | //! [create verlet cell] |
400 | |
401 | //! [create verlet] |
402 | |
403 | VerS vl2; |
404 | vl2.Initialize(cli,0,r_cut,pos2.get(0).pos,pos2,pos2.get(0).pos.size()); |
405 | |
406 | //! [create verlet] |
407 | |
408 | Box<dim,T> innerBox; |
409 | for (size_t i = 0 ; i < dim ; i++) |
410 | { |
411 | innerBox.setLow(i,r_cut); |
412 | innerBox.setHigh(i,1.0-r_cut); |
413 | } |
414 | |
415 | // Check that the verlet is consistent |
416 | for (size_t i = 0 ; i < pos2.get(0).pos.size() ; i++) |
417 | { |
418 | if (innerBox.isInside(pos2.get(0).pos.get(i)) == false) |
419 | continue; |
420 | |
421 | Point<dim,T> p = pos2.get(0).pos.get(i); |
422 | |
423 | if (k == 0) |
424 | {BOOST_REQUIRE_EQUAL(vl1.getNNPart(i),57ul);} |
425 | BOOST_REQUIRE_EQUAL(vl1.getNNPart(i),vl2.getNNPart(i)); |
426 | |
427 | openfpm::vector<size_t> v1; |
428 | openfpm::vector<size_t> v2; |
429 | |
430 | //! [usage of verlet] |
431 | |
432 | bool ret = true; |
433 | auto NN = vl1.getNNIterator(i); |
434 | while (NN.isNext()) |
435 | { |
436 | auto k = NN.getP(); |
437 | auto v = NN.getV(); |
438 | |
439 | T dist = p.distance(Point<dim,T>(pos2.get(v).pos.get(k))); |
440 | |
441 | ret &= (dist < r_cut); |
442 | |
443 | v1.add(NN.get()); |
444 | |
445 | ++NN; |
446 | } |
447 | |
448 | //! [usage of verlet] |
449 | |
450 | auto NN2 = vl2.getNNIterator(i); |
451 | while (NN2.isNext()) |
452 | { |
453 | auto k = NN2.get(); |
454 | |
455 | v2.add(k); |
456 | |
457 | ++NN2; |
458 | } |
459 | |
460 | ret = true; |
461 | v1.sort(); |
462 | v2.sort(); |
463 | for (size_t i = 0 ; i < v1.size(); i++) |
464 | { |
465 | ret &= v1.get(i) == v2.get(i); |
466 | } |
467 | |
468 | BOOST_REQUIRE_EQUAL(ret,true); |
469 | } |
470 | |
471 | r_cut += 0.075; |
472 | } |
473 | } |
474 | |
475 | |
476 | BOOST_AUTO_TEST_SUITE( VerletList_test ) |
477 | |
478 | BOOST_AUTO_TEST_CASE( VerletList_use) |
479 | { |
480 | std::cout << "Test verlet list" << "\n" ; |
481 | |
482 | SpaceBox<3,double> box({0.0f,0.0f,0.0f},{1.0f,1.0f,1.0f}); |
483 | SpaceBox<3,double> box2({-1.0f,-1.0f,-1.0f},{1.0f,1.0f,1.0f}); |
484 | // Verlet_list_s<3,double,VerletList<3,double,FAST,shift<3,double>>>(box); |
485 | // Verlet_list_s<3,double,VerletList<3,double,FAST,shift<3,double>> >(box2); |
486 | Verlet_list_sM<3,double,VerletListM<3,double,2>>(box); |
487 | // Verlet_list_sM<3,double,CellListM<3,double,8>>(box2); |
488 | |
489 | std::cout << "End verlet list" << "\n" ; |
490 | |
491 | // Test the cell list |
492 | } |
493 | |
494 | BOOST_AUTO_TEST_SUITE_END() |
495 | |
496 | |
497 | |
498 | #endif /* OPENFPM_DATA_SRC_NN_VERLETLIST_VERLETLIST_TEST_HPP_ */ |
499 | |