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.hpp
1 /*
2  * vector_dist_operators_apply_kernel.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_HPP_
9 #define OPENFPM_NUMERICS_SRC_OPERATORS_VECTOR_VECTOR_DIST_OPERATORS_APPLY_KERNEL_HPP_
10 
12 
13 #define DEFINE_INTERACTION_3D(name) struct name \
14 {\
15 \
16  Point<3,double> value(const Point<3,double> & xp, const Point<3,double> xq)\
17  {
18 
19 #define END_INTERACTION }\
20  };
21 
28 template<typename ObjType, typename Sfinae = void>
29 struct is_expression: std::false_type {};
30 
31 template<typename ObjType>
32 struct is_expression<ObjType, typename Void<typename ObjType::is_expression>::type> : std::true_type
33 {};
34 
36 template<typename exp, bool is_exp = is_expression<exp>::value>
38 {
40  typedef typename exp::return_type rtype;
41 };
42 
44 template<typename exp>
45 struct apply_kernel_rtype<exp,false>
46 {
48  typedef exp rtype;
49 };
50 
51 
56 template<typename rtype>
57 struct set_zero
58 {
60  static rtype create()
61  {
62  return 0.0;
63  }
64 };
65 
67 template<unsigned int dim, typename T>
68 struct set_zero<Point<dim,T>>
69 {
72  {
73  Point<dim,T> ret;
74 
75  ret.zero();
76  return ret;
77  }
78 };
79 
84 template<typename T, typename vector, typename exp,typename NN_type, typename Kernel, typename rtype, bool is_exp=is_expression<T>::value>
86 {
98  inline static typename std::remove_reference<rtype>::type apply(const vector & vd, NN_type & cl, const exp & v_exp, const vect_dist_key_dx & key, Kernel & lker)
99  {
100  // accumulator
101  typename std::remove_reference<rtype>::type pse = set_zero<typename std::remove_reference<rtype>::type>::create();
102 
103  // position of particle p
105 
106  // property of the particle x
107  rtype prp_p = v_exp.value(key);
108 
109  // Get the neighborhood of the particle
110  auto NN = cl.template getNNIterator<NO_CHECK>(cl.getCell(p));
111  while(NN.isNext())
112  {
113  auto nnp = NN.get();
114 
115  // Calculate contribution given by the kernel value at position p,
116  // given by the Near particle, exclude itself
117  if (nnp != key.getKey())
118  {
119  vect_dist_key_dx nnp_k;
120  nnp_k.setKey(nnp);
121 
122  // property of the particle x
123  rtype prp_q = v_exp.value(nnp_k);
124 
125  // position of the particle q
127 
128  pse += lker.value(p,q,prp_p,prp_q);
129  }
130 
131  // Next particle
132  ++NN;
133  }
134 
135  return pse;
136  }
137 };
138 
139 
144 template<typename vector, typename exp,typename NN_type, typename Kernel, typename rtype>
146 {
157  inline static typename std::remove_reference<rtype>::type apply(const vector & vd, NN_type & cl, const vect_dist_key_dx & key, Kernel & lker)
158  {
159  // accumulator
160  typename std::remove_reference<rtype>::type pse = set_zero<typename std::remove_reference<rtype>::type>::create();
161 
162  // position of particle p
164 
165  // Get the neighborhood of the particle
166  auto NN = cl.template getNNIterator<NO_CHECK>(cl.getCell(p));
167  while(NN.isNext())
168  {
169  auto nnp = NN.get();
170 
171  // Calculate contribution given by the kernel value at position p,
172  // given by the Near particle, exclude itself
173  if (nnp != key.getKey())
174  {
175  // position of the particle q
177 
178  pse += lker.value(p,q);
179  }
180 
181  // Next particle
182  ++NN;
183  }
184 
185  return pse;
186  }
187 };
188 
189 
190 
195 template<typename T, typename vector, typename exp,typename NN_type, typename Kernel, typename rtype, bool is_exp=is_expression<T>::value>
197 {
209  inline static typename std::remove_reference<rtype>::type apply(const vector & vd, NN_type & cl, const exp & v_exp, const vect_dist_key_dx & key, Kernel & lker)
210  {
211  // accumulator
212  typename std::remove_reference<rtype>::type pse = set_zero<typename std::remove_reference<rtype>::type>::create();
213 
214  // property of the particle x
215  rtype prp_p = v_exp.value(key);
216 
217  // position of particle p
219 
220  // Get the neighborhood of the particle
221  auto NN = cl.template getNNIterator<NO_CHECK>(cl.getCell(p));
222  while(NN.isNext())
223  {
224  auto nnp = NN.get();
225 
226  // Calculate contribution given by the kernel value at position p,
227  // given by the Near particle, exclude itself
228  if (nnp != key.getKey())
229  {
230  vect_dist_key_dx nnp_k;
231  nnp_k.setKey(nnp);
232 
233  // property of the particle x
234  rtype prp_q = v_exp.value(nnp_k);
235 
236  pse += lker.value(key.getKey(),nnp,prp_p,prp_q,vd);
237  }
238 
239  // Next particle
240  ++NN;
241  }
242 
243  return pse;
244  }
245 };
246 
247 
254 template <typename exp1,typename vector_type>
255 class vector_dist_expression_op<exp1,vector_type,VECT_APPLYKER_IN>
256 {
258  typedef typename boost::mpl::at<vector_type,boost::mpl::int_<0>>::type NN;
260  typedef typename boost::mpl::at<vector_type,boost::mpl::int_<1>>::type Kernel;
262  typedef typename boost::mpl::at<vector_type,boost::mpl::int_<2>>::type vector_orig;
263 
265  const exp1 o1;
266 
268  NN & cl;
269 
272 
274  const vector_orig & vd;
275 
277  typedef typename apply_kernel_rtype<decltype(o1.value(vect_dist_key_dx()))>::rtype rtype;
278 
279 public:
280 
286  inline void init() const
287  {
288  o1.init();
289  }
290 
299  vector_dist_expression_op(const exp1 & o1, NN & cl, Kernel & ker, const vector_orig & vd)
300  :o1(o1),cl(cl),ker(ker),vd(vd)
301  {}
302 
310  inline typename std::remove_reference<rtype>::type value(const vect_dist_key_dx & key) const
311  {
312  return apply_kernel_is_number_or_expression<decltype(o1.value(key)),vector_orig,exp1,NN,Kernel,rtype>::apply(vd,cl,o1,key,ker);
313  }
314 };
315 
322 template <typename exp1,typename vector_type>
323 class vector_dist_expression_op<exp1,vector_type,VECT_APPLYKER_IN_SIM>
324 {
326  typedef typename boost::mpl::at<vector_type,boost::mpl::int_<0>>::type NN;
328  typedef typename boost::mpl::at<vector_type,boost::mpl::int_<1>>::type Kernel;
330  typedef typename boost::mpl::at<vector_type,boost::mpl::int_<2>>::type vector_orig;
331 
333  NN & cl;
337  const vector_orig & vd;
338 
341 
342 
343 public:
344 
350  inline void init() const
351  {
352  }
353 
362  :cl(cl),ker(ker),vd(vd)
363  {}
364 
372  inline typename std::remove_reference<rtype>::type value(const vect_dist_key_dx & key) const
373  {
375  }
376 };
377 
378 
385 template <typename exp1,typename vector_type>
386 class vector_dist_expression_op<exp1,vector_type,VECT_APPLYKER_IN_GEN>
387 {
389  typedef typename boost::mpl::at<vector_type,boost::mpl::int_<0>>::type NN;
391  typedef typename boost::mpl::at<vector_type,boost::mpl::int_<1>>::type Kernel;
392 
394  typedef typename boost::mpl::at<vector_type,boost::mpl::int_<2>>::type vector_orig;
395 
397  const exp1 o1;
398 
400  NN & cl;
401 
404 
406  const vector_orig & vd;
407 
409  typedef typename apply_kernel_rtype<decltype(o1.value(vect_dist_key_dx()))>::rtype rtype;
410 
411 public:
412 
418  inline void init() const
419  {
420  o1.init();
421  }
422 
431  vector_dist_expression_op(const exp1 & o1, NN & cl, Kernel & ker, const vector_orig & vd)
432  :o1(o1),cl(cl),ker(ker),vd(vd)
433  {}
434 
442  inline typename std::remove_reference<rtype>::type value(const vect_dist_key_dx & key) const
443  {
444  return apply_kernel_is_number_or_expression_gen<decltype(o1.value(key)),vector_orig,exp1,NN,Kernel,rtype>::apply(vd,cl,o1,key,ker);
445  }
446 };
447 
448 
451 
452 /* \brief Apply kernel expression
453  *
454  * \param va vector expression one
455  * \param vb vector expression two
456  *
457  * \return an object that encapsulate the expression
458  *
459  */
460 template<typename exp1, typename exp2, unsigned int op1, typename NN, typename Kernel, typename vector_type>
461 inline vector_dist_expression_op<vector_dist_expression_op<exp1,exp2,op1>,boost::mpl::vector<NN,Kernel,vector_type>,VECT_APPLYKER_IN>
462 applyKernel_in(const vector_dist_expression_op<exp1,exp2,op1> & va, vector_type & vd, NN & cl, Kernel & ker)
463 {
464  vector_dist_expression_op<vector_dist_expression_op<exp1,exp2,op1>,boost::mpl::vector<NN,Kernel,vector_type>,VECT_APPLYKER_IN> exp_sum(va,cl,ker,vd);
465 
466  return exp_sum;
467 }
468 
469 
470 /* \brief Apply kernel expression
471  *
472  * \param va vector expression one
473  * \param vb vector expression two
474  *
475  * \return an object that encapsulate the expression
476  *
477  */
478 template<typename exp1, typename exp2, unsigned int op1, typename NN, typename Kernel, typename vector_type>
479 inline vector_dist_expression_op<vector_dist_expression_op<exp1,exp2,op1>,boost::mpl::vector<NN,Kernel,vector_type>,VECT_APPLYKER_IN_GEN>
480 applyKernel_in_gen(const vector_dist_expression_op<exp1,exp2,op1> & va, vector_type & vd, NN & cl, Kernel & ker)
481 {
482  vector_dist_expression_op<vector_dist_expression_op<exp1,exp2,op1>,boost::mpl::vector<NN,Kernel,vector_type>,VECT_APPLYKER_IN_GEN> exp_sum(va,cl,ker,vd);
483 
484  return exp_sum;
485 }
486 
487 
489 
490 /* \brief Apply kernel expression
491  *
492  * \param va vector expression one
493  * \param vb vector expression two
494  *
495  * \return an object that encapsulate the expression
496  *
497  */
498 template<unsigned int prp, typename NN, typename Kernel, typename vector_type>
499 inline vector_dist_expression_op<vector_dist_expression<prp,vector_type>,boost::mpl::vector<NN,Kernel,vector_type>,VECT_APPLYKER_IN>
500 applyKernel_in(const vector_dist_expression<prp,vector_type> & va, vector_type & vd, NN & cl, Kernel & ker)
501 {
502  vector_dist_expression_op<vector_dist_expression<prp,vector_type>,boost::mpl::vector<NN,Kernel,vector_type>,VECT_APPLYKER_IN> exp_sum(va,cl,ker,vd);
503 
504  return exp_sum;
505 }
506 
507 
508 /* \brief Apply kernel expression
509  *
510  * \param va vector expression one
511  * \param vb vector expression two
512  *
513  * \return an object that encapsulate the expression
514  *
515  */
516 template<unsigned int prp, typename NN, typename Kernel, typename vector_type>
517 inline vector_dist_expression_op<vector_dist_expression<prp,vector_type>,boost::mpl::vector<NN,Kernel,vector_type>,VECT_APPLYKER_IN_GEN>
518 applyKernel_in_gen(const vector_dist_expression<prp,vector_type> & va, vector_type & vd, NN & cl, Kernel & ker)
519 {
520  vector_dist_expression_op<vector_dist_expression<prp,vector_type>,boost::mpl::vector<NN,Kernel,vector_type>,VECT_APPLYKER_IN_GEN> exp_sum(va,cl,ker,vd);
521 
522  return exp_sum;
523 }
524 
525 
526 /* \brief Apply kernel expression
527  *
528  * \param va vector expression one
529  * \param vb vector expression two
530  *
531  * \return an object that encapsulate the expression
532  *
533  */
534 template<typename NN, typename Kernel, typename vector_type>
536 applyKernel_in_sim(vector_type & vd, NN & cl, Kernel & ker)
537 {
538  vector_dist_expression_op<void,boost::mpl::vector<NN,Kernel,vector_type>,VECT_APPLYKER_IN_SIM> exp_sum(cl,ker,vd);
539 
540  return exp_sum;
541 }
542 
543 
544 #endif /* OPENFPM_NUMERICS_SRC_OPERATORS_VECTOR_VECTOR_DIST_OPERATORS_APPLY_KERNEL_HPP_ */
static std::remove_reference< rtype >::type apply(const vector &vd, NN_type &cl, const exp &v_exp, const vect_dist_key_dx &key, Kernel &lker)
Apply the kernel expression to a particle.
std::remove_reference< rtype >::type value(const vect_dist_key_dx &key) const
Evaluate the expression.
boost::mpl::at< vector_type, boost::mpl::int_< 2 > >::type vector_orig
Get the type of the vector containing the set of particles.
boost::mpl::at< vector_type, boost::mpl::int_< 2 > >::type vector_orig
Return the vector containing the position of the particles.
static rtype create()
return 0
static std::remove_reference< rtype >::type apply(const vector &vd, NN_type &cl, const vect_dist_key_dx &key, Kernel &lker)
Apply the kernel expression to a particle.
exp rtype
indicate the return type of the expression exp
apply_kernel_rtype< decltype(o1.value(vect_dist_key_dx()))>::rtype rtype
Get the return type of applying the kernel to a particle.
vector_dist_expression_op(const exp1 &o1, NN &cl, Kernel &ker, const vector_orig &vd)
Constructor.
Meta-function to return a compatible zero-element.
apply_kernel_rtype< decltype(o1.value(vect_dist_key_dx()))>::rtype rtype
Return type of the expression.
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:22
Grid key for a distributed grid.
T & value(size_t i)
Return the reference to the value at coordinate i.
Definition: Point.hpp:391
vector_dist_expression_op(const exp1 &o1, NN &cl, Kernel &ker, const vector_orig &vd)
Constructor.
boost::mpl::at< vector_type, boost::mpl::int_< 1 > >::type Kernel
Get the type of the kernel.
Unknown operation specialization.
vector_dist_expression_op(NN &cl, Kernel &ker, const vector_orig &vd)
Constructor.
boost::mpl::at< vector_type, boost::mpl::int_< 0 > >::type NN
Get the type of the Cell-list.
std::remove_reference< rtype >::type value(const vect_dist_key_dx &key) const
Evaluate the expression.
Main class that encapsulate a vector properties operand to be used for expressions construction...
static std::remove_reference< rtype >::type apply(const vector &vd, NN_type &cl, const exp &v_exp, const vect_dist_key_dx &key, Kernel &lker)
Apply the kernel expression to a particle.
const T & get(size_t i) const
Get coordinate.
Definition: Point.hpp:142
static Point< dim, T > create()
return a point with all the coordinates set to zero
boost::mpl::at< vector_type, boost::mpl::int_< 2 > >::type vector_orig
Get the type that contain the particle positions.
Void structure.
Definition: common.hpp:63
Apply the kernel to particle differently that is a number or is an expression.
size_t getKey() const
Get the key.
This class is a trick to indicate the compiler a specific specialization pattern. ...
Definition: memory_c.hpp:201
void setKey(size_t key)
set the key
apply_kernel_rtype< decltype(std::declval< Kernel >).value(Point< vector_orig::dims, typename vector_orig::stype >0.0), Point< vector_orig::dims, typename vector_orig::stype >0.0)))>::rtype rtype
Get the return type of the expression.
std::remove_reference< rtype >::type value(const vect_dist_key_dx &key) const
Evaluate the expression.
exp::return_type rtype
indicate the return type of the expression exp
Distributed vector.
void zero()
Set to zero the point coordinate.
Definition: Point.hpp:255
It give the return type of the expression if applicable.
Apply the kernel to particle differently that is a number or is an expression.
boost::mpl::at< vector_type, boost::mpl::int_< 1 > >::type Kernel
Return the type of the kernel.
Apply the kernel to particle differently that is a number or is an expression.
boost::mpl::at< vector_type, boost::mpl::int_< 1 > >::type Kernel
Get the type of the kernel.
boost::mpl::at< vector_type, boost::mpl::int_< 0 > >::type NN
Get the type of the Cell-list.
boost::mpl::at< vector_type, boost::mpl::int_< 0 > >::type NN
Return the type of the Cell-list.
is_expression check if a type is simple a type or is just an encapsulation of an expression ...