OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
vector_dist_operators_apply_kernel_unit_tests.hpp
1 /*
2  * vector_dist_operators_apply_kernel_unit_tests.hpp
3  *
4  * Created on: Jun 19, 2016
5  * Author: i-bird
6  */
7 
8 #ifndef OPENFPM_NUMERICS_SRC_OPERATORS_VECTOR_VECTOR_DIST_OPERATORS_APPLY_KERNEL_UNIT_TESTS_HPP_
9 #define OPENFPM_NUMERICS_SRC_OPERATORS_VECTOR_VECTOR_DIST_OPERATORS_APPLY_KERNEL_UNIT_TESTS_HPP_
10 
11 template <typename vector,typename Kernel, typename NN_type> bool check_values_apply_kernel(vector & vd, Kernel & ker, NN_type & NN)
12 {
13  bool ret = true;
14  auto it = vd.getDomainIterator();
15 
16  // ### WIKI 13 ###
17  //
18  // Check that apply kernel work
19  //
20  auto it2 = vd.getDomainIterator();
21  while (it2.isNext())
22  {
23  float base2 = 0.0;
24  auto p = it2.get();
25 
26  Point<3,float> xp = vd.getPos(p);
27 
28  float base1 = vd.template getProp<A>(p);
29 
30  float prp_x = vd.template getProp<VC>(p) * vd.template getProp<VB>(p) + norm(vd.template getProp<VB>(p));
31 
32  // For each neighborhood particle
33  auto Np = NN.template getNNIterator<NO_CHECK>(NN.getCell(xp));
34 
35  while (Np.isNext())
36  {
37  // Neighborhood particle q
38  auto q = Np.get();
39 
40  if (q == p.getKey()) {++Np; continue;};
41 
42  // position q
43  Point<3,float> xq = vd.getPos(q);
44 
45  float prp_y = vd.template getProp<VC>(q) * vd.template getProp<VB>(q) + norm(vd.template getProp<VB>(q));
46 
47  base2 += ker.value(xp,xq,prp_x,prp_y);
48 
49  ++Np;
50  }
51 
52  base2 += vd.template getProp<C>(p);
53 
54  ret &= base1 == base2;
55 
56  ++it2;
57  }
58 
59  BOOST_REQUIRE_EQUAL(ret,true);
60 
61  return ret;
62 }
63 
64 template <typename vector,typename Kernel, typename NN_type> bool check_values_apply_kernel_reduce(vector & vd, Kernel & ker, NN_type & NN)
65 {
66  bool ret = true;
67  auto it = vd.getDomainIterator();
68 
69  float base1 = 0.0;
70  float base2 = 0.0;
71  float base3 = 0.0;
72 
73  auto it2 = vd.getDomainIterator();
74  while (it2.isNext())
75  {
76  auto p = it2.get();
77 
78  Point<3,float> xp = vd.getPos(p);
79 
80  float ker_accu = 0.0;
81  float prp_x = vd.template getProp<VC>(p) * vd.template getProp<VB>(p) + norm(vd.template getProp<VB>(p));
82 
83  // For each neighborhood particle
84  auto Np = NN.template getNNIterator<NO_CHECK>(NN.getCell(xp));
85 
86  while (Np.isNext())
87  {
88  // Neighborhood particle q
89  auto q = Np.get();
90 
91  if (q == p.getKey()) {++Np; continue;};
92 
93  // position q
94  Point<3,float> xq = vd.getPos(q);
95 
96  float prp_y = vd.template getProp<VC>(q) * vd.template getProp<VB>(q) + norm(vd.template getProp<VB>(q));
97 
98  ker_accu += ker.value(xp,xq,prp_x,prp_y);
99 
100  ++Np;
101  }
102 
103  base2 += ker_accu;
104 
105  ++it2;
106  }
107 
108  auto it3 = vd.getDomainIterator();
109  while (it3.isNext())
110  {
111  auto p = it3.get();
112 
113  base1 = vd.template getProp<A>(p);
114  base3 = vd.template getProp<C>(p) + base2;
115 
116  ret &= base1 == base3;
117 
118  ++it3;
119  }
120 
121  BOOST_REQUIRE_EQUAL(ret,true);
122 
123  return ret;
124 }
125 
126 template <typename vector,typename Kernel, typename NN_type> bool check_values_apply_kernel2(vector & vd, Kernel & ker, NN_type & NN)
127 {
128  bool ret = true;
129  auto it = vd.getDomainIterator();
130 
131  // ### WIKI 13 ###
132  //
133  // Check that apply kernel work
134  //
135  auto it2 = vd.getDomainIterator();
136  while (it2.isNext())
137  {
138  Point<3,float> base2 = 0.0;
139  auto p = it2.get();
140 
141  Point<3,float> xp = vd.getPos(p);
142 
143  Point<3,float> base1 = vd.template getProp<VA>(p);
144 
145  Point<3,float> prp_x = 2.0 * vd.template getProp<VC>(p) + vd.template getProp<VB>(p);
146 
147  // For each neighborhood particle
148  auto Np = NN.template getNNIterator<NO_CHECK>(NN.getCell(xp));
149 
150  while (Np.isNext())
151  {
152  // Neighborhood particle q
153  auto q = Np.get();
154 
155  if (q == p.getKey()) {++Np; continue;};
156 
157  // position q
158  Point<3,float> xq = vd.getPos(q);
159 
160  Point<3,float> prp_y = 2.0 * vd.template getProp<VC>(q) + vd.template getProp<VB>(q);
161 
162  base2 += ker.value(xp,xq,prp_x,prp_y);
163 
164  ++Np;
165  }
166 
167  base2 += vd.template getProp<VC>(p);
168 
169  ret &= base1 == base2;
170 
171  ++it2;
172  }
173 
174  BOOST_REQUIRE_EQUAL(ret,true);
175 
176  return ret;
177 }
178 
179 template <typename vector,typename Kernel, typename NN_type> bool check_values_apply_kernel3(vector & vd, Kernel & ker, NN_type & NN)
180 {
181  bool ret = true;
182  auto it = vd.getDomainIterator();
183 
184  // ### WIKI 13 ###
185  //
186  // Check that apply kernel work
187  //
188  auto it2 = vd.getDomainIterator();
189  while (it2.isNext())
190  {
191  Point<3,float> base2 = 0.0;
192  auto p = it2.get();
193 
194  Point<3,float> xp = vd.getPos(p);
195 
196  Point<3,float> base1 = vd.template getProp<VA>(p);
197 
198  Point<3,float> prp_x = vd.template getProp<VC>(p);
199 
200  // For each neighborhood particle
201  auto Np = NN.template getNNIterator<NO_CHECK>(NN.getCell(xp));
202 
203  while (Np.isNext())
204  {
205  // Neighborhood particle q
206  auto q = Np.get();
207 
208  if (q == p.getKey()) {++Np; continue;};
209 
210  // position q
211  Point<3,float> xq = vd.getPos(q);
212 
213  Point<3,float> prp_y = vd.template getProp<VC>(q);
214 
215  base2 += ker.value(xp,xq,prp_x,prp_y);
216 
217  ++Np;
218  }
219 
220  base2 += vd.template getProp<VC>(p);
221 
222  ret &= base1 == base2;
223 
224  ++it2;
225  }
226 
227  BOOST_REQUIRE_EQUAL(ret,true);
228 
229  return ret;
230 }
231 
232 template <typename vector,typename Kernel, typename NN_type> bool check_values_apply_kernel2_reduce(vector & vd, Kernel & ker, NN_type & NN)
233 {
234  bool ret = true;
235  auto it = vd.getDomainIterator();
236 
237  Point<3,float> base1 = 0.0;
238  Point<3,float> base2 = 0.0;
239  Point<3,float> base3 = 0.0;
240 
241  auto it2 = vd.getDomainIterator();
242  while (it2.isNext())
243  {
244  auto p = it2.get();
245 
246  Point<3,float> xp = vd.getPos(p);
247 
248  Point<3,float> ker_accu = 0.0;
249  Point<3,float> prp_x = 2.0f*vd.template getProp<VC>(p) + vd.template getProp<VB>(p);
250 
251  // For each neighborhood particle
252  auto Np = NN.template getNNIterator<NO_CHECK>(NN.getCell(xp));
253 
254  while (Np.isNext())
255  {
256  // Neighborhood particle q
257  auto q = Np.get();
258 
259  if (q == p.getKey()) {++Np; continue;};
260 
261  // position q
262  Point<3,float> xq = vd.getPos(q);
263  Point<3,float> prp_y = 2.0f*vd.template getProp<VC>(q) + vd.template getProp<VB>(q);
264 
265  ker_accu += ker.value(xp,xq,prp_x,prp_y);
266 
267  ++Np;
268  }
269 
270  base2 += ker_accu;
271 
272  ++it2;
273  }
274 
275  auto it3 = vd.getDomainIterator();
276  while (it3.isNext())
277  {
278  auto p = it3.get();
279 
280  base1 = vd.template getProp<VA>(p);
281  base3 = vd.template getProp<VC>(p) + base2;
282 
283  ret &= base1 == base3;
284 
285  ++it3;
286  }
287 
288  BOOST_REQUIRE_EQUAL(ret,true);
289 
290  return ret;
291 }
292 
293 template <typename vector,typename Kernel, typename NN_type> bool check_values_apply_kernel3_reduce(vector & vd, Kernel & ker, NN_type & NN, const Point<2,float> & p)
294 {
295  bool ret = true;
296  auto it = vd.getDomainIterator();
297 
298  Point<2,float> base2 = 0.0;
299 
300  auto it2 = vd.getDomainIterator();
301  while (it2.isNext())
302  {
303  auto p = it2.get();
304 
305  Point<3,float> xp = vd.getPos(p);
306 
307  Point<2,float> ker_accu = 0.0;
308 
309  // For each neighborhood particle
310  auto Np = NN.template getNNIterator<NO_CHECK>(NN.getCell(xp));
311 
312  while (Np.isNext())
313  {
314  // Neighborhood particle q
315  auto q = Np.get();
316 
317  if (q == p.getKey()) {++Np; continue;};
318 
319  // position q
320  Point<3,float> xq = vd.getPos(q);
321 
322  ker_accu += ker.value(xp,xq);
323 
324  ++Np;
325  }
326 
327  base2 += ker_accu;
328 
329  ++it2;
330  }
331 
332  BOOST_REQUIRE_EQUAL(p.get(0),base2.get(0));
333  BOOST_REQUIRE_EQUAL(p.get(1),base2.get(1));
334 
335  return ret;
336 }
337 
338 BOOST_AUTO_TEST_CASE( vector_dist_operators_apply_kernel_test )
339 {
340  if (create_vcluster().getProcessingUnits() > 3)
341  return;
342 
343  Box<3,float> box({0.0,0.0,0.0},{1.0,1.0,1.0});
344 
345  // Boundary conditions
346  size_t bc[3]={PERIODIC,PERIODIC,PERIODIC};
347 
348  // ghost
349  Ghost<3,float> ghost(0.05);
350 
351  vector_dist<3,float,aggregate<float,float,float,VectorS<3,float>,VectorS<3,float>,VectorS<3,float>,float>> vd(512,box,bc,ghost);
352 
353  auto vA = getV<A>(vd);
354  auto vC = getV<C>(vd);
355 
356  auto vVA = getV<VA>(vd);
357  auto vVB = getV<VB>(vd);
358  auto vVC = getV<VC>(vd);
359 
360  // fill vd with some value
361  fill_values(vd);
362 
363  vd.map();
364  vd.ghost_get<0,1,2,3,4,5,6>();
365 
366  // we apply an exponential kernel to calculate something
367 
368  auto cl = vd.getCellList(0.05);
369  exp_kernel ker(0.2);
370 
371  vA = applyKernel_in(vVC * vVB + norm(vVB),vd,cl,ker) + vC;
372  check_values_apply_kernel(vd,ker,cl);
373 
374  vVA = applyKernel_in(2.0*vVC + vVB ,vd,cl,ker) + vVC;
375  check_values_apply_kernel2(vd,ker,cl);
376 
377  vA = rsum(applyKernel_in(vVC * vVB + norm(vVB),vd,cl,ker),vd) + vC;
378  check_values_apply_kernel_reduce(vd,ker,cl);
379 
380  vVA = rsum(applyKernel_in(2.0*vVC + vVB ,vd,cl,ker),vd) + vVC;
381  check_values_apply_kernel2_reduce(vd,ker,cl);
382 
383  vA = applyKernel_in_gen(vVC * vVB + norm(vVB),vd,cl,ker) + vC;
384  check_values_apply_kernel(vd,ker,cl);
385 
386  vVA = applyKernel_in_gen(2.0*vVC + vVB ,vd,cl,ker) + vVC;
387  check_values_apply_kernel2(vd,ker,cl);
388 
389  vA = rsum(applyKernel_in_gen(vVC * vVB + norm(vVB),vd,cl,ker),vd) + vC;
390  check_values_apply_kernel_reduce(vd,ker,cl);
391 
392  vVA = rsum(applyKernel_in_gen(2.0*vVC + vVB ,vd,cl,ker),vd) + vVC;
393  check_values_apply_kernel2_reduce(vd,ker,cl);
394 
395  // Check it compile the code is the same
396  vVA = applyKernel_in_gen(vVC,vd,cl,ker) + vVC;
397  check_values_apply_kernel3(vd,ker,cl);
398 
399  vVA = applyKernel_in (vVC,vd,cl,ker) + vVC;
400  check_values_apply_kernel3(vd,ker,cl);
401 
402  // just check it compile
403  Point<2,float> p = rsum(applyKernel_in_sim(vd,cl,ker),vd).get();
404  check_values_apply_kernel3_reduce(vd,ker,cl,p);
405 }
406 
407 
408 #endif /* OPENFPM_NUMERICS_SRC_OPERATORS_VECTOR_VECTOR_DIST_OPERATORS_APPLY_KERNEL_UNIT_TESTS_HPP_ */
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:22
T & value(size_t i)
Return the reference to the value at coordinate i.
Definition: Point.hpp:391
Definition: Ghost.hpp:39
const T & get(size_t i) const
Get coordinate.
Definition: Point.hpp:142
Distributed vector.