vector_dist_operators_apply_kernel.hpp 15.2 KB
Newer Older
Pietro Incardona's avatar
Pietro Incardona committed
1 2 3 4 5 6 7 8 9 10
/*
 * vector_dist_operators_apply_kernel.hpp
 *
 *  Created on: Jun 19, 2016
 *      Author: i-bird
 */

#ifndef OPENFPM_NUMERICS_SRC_OPERATORS_VECTOR_VECTOR_DIST_OPERATORS_APPLY_KERNEL_HPP_
#define OPENFPM_NUMERICS_SRC_OPERATORS_VECTOR_VECTOR_DIST_OPERATORS_APPLY_KERNEL_HPP_

incardon's avatar
incardon committed
11 12 13 14 15 16 17 18 19 20
//////// SET of small macro to make to define integral easy

#define DEFINE_INTERACTION_3D(name) struct name \
{\
\
	Point<3,double> value(const Point<3,double> & xp, const Point<3,double> xq)\
	{

#define END_INTERACTION }\
						};
Pietro Incardona's avatar
Pietro Incardona committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34

/*! \brief is_expression check if a type is simple a type or is just an encapsulation of an expression
 *
 * return true if T::is_expression is a valid expression
 *
 */

template<typename ObjType, typename Sfinae = void>
struct is_expression: std::false_type {};

template<typename ObjType>
struct is_expression<ObjType, typename Void<typename ObjType::is_expression>::type> : std::true_type
{};

35
//! It give the return type of the expression if applicable
Pietro Incardona's avatar
Pietro Incardona committed
36 37 38
template<typename exp, bool is_exp = is_expression<exp>::value>
struct apply_kernel_rtype
{
39
	//! indicate the return type of the expression exp
Pietro Incardona's avatar
Pietro Incardona committed
40 41 42
	typedef typename exp::return_type rtype;
};

43
//! It give the return type of the expression if applicable
Pietro Incardona's avatar
Pietro Incardona committed
44 45 46
template<typename exp>
struct apply_kernel_rtype<exp,false>
{
47
	//! indicate the return type of the expression exp
Pietro Incardona's avatar
Pietro Incardona committed
48 49 50 51
	typedef exp rtype;
};


52 53 54 55 56 57 58
/*! \brief Meta-function to return a compatible zero-element
 *
 *
 */
template<typename rtype>
struct set_zero
{
59
	//! return 0
60 61 62 63 64 65
	static rtype create()
	{
		return 0.0;
	}
};

66
//! Create a point with all compunent set to zero
67 68 69
template<unsigned int dim, typename T>
struct set_zero<Point<dim,T>>
{
70
	//! return a point with all the coordinates set to zero
71 72 73 74 75 76 77 78 79
	static Point<dim,T> create()
	{
		Point<dim,T> ret;

		ret.zero();
		return ret;
	}
};

Pietro Incardona's avatar
Pietro Incardona committed
80 81 82 83 84 85 86
/*! \brief Apply the kernel to particle differently that is a number or is an expression
 *
 *
 */
template<typename T, typename vector, typename exp,typename NN_type, typename Kernel, typename rtype, bool is_exp=is_expression<T>::value>
struct apply_kernel_is_number_or_expression
{
87 88 89 90 91 92 93 94 95 96 97
	/*! \brief Apply the kernel expression to a particle
	 *
	 * \param vd vector with the particles positions
	 * \param cl Cell-list
	 * \param v_exp expression to execute for each particle
	 * \param key to which particle to apply the expression
	 * \param lker kernel function
	 *
	 * \return the result of apply the kernel on the particle key
	 *
	 */
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)
Pietro Incardona's avatar
Pietro Incardona committed
99 100
	{
	    // accumulator
101
		typename std::remove_reference<rtype>::type pse = set_zero<typename std::remove_reference<rtype>::type>::create();
Pietro Incardona's avatar
Pietro Incardona committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

	    // position of particle p
	    Point<vector::dims,typename vector::stype> p = vd.getPos(key);

	    // property of the particle x
	    rtype prp_p = v_exp.value(key);

	    // Get the neighborhood of the particle
	    auto NN = cl.template getNNIterator<NO_CHECK>(cl.getCell(p));
	    while(NN.isNext())
	    {
	    	auto nnp = NN.get();

	    	// Calculate contribution given by the kernel value at position p,
	    	// given by the Near particle, exclude itself
	    	if (nnp != key.getKey())
	    	{
incardon's avatar
incardon committed
119 120 121
	    		vect_dist_key_dx nnp_k;
	    		nnp_k.setKey(nnp);

Pietro Incardona's avatar
Pietro Incardona committed
122
	    	    // property of the particle x
incardon's avatar
incardon committed
123
	    		rtype prp_q = v_exp.value(nnp_k);
Pietro Incardona's avatar
Pietro Incardona committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

	    	    // position of the particle q
	    		Point<vector::dims,typename vector::stype> q = vd.getPos(nnp);

	    	    pse += lker.value(p,q,prp_p,prp_q);
	    	}

	    	// Next particle
	    	++NN;
	    }

	    return pse;
	}
};


140 141 142 143 144 145 146
/*! \brief Apply the kernel to particle differently that is a number or is an expression
 *
 *
 */
template<typename vector, typename exp,typename NN_type, typename Kernel, typename rtype>
struct apply_kernel_is_number_or_expression_sim
{
147 148 149 150 151 152 153 154 155 156
	/*! \brief Apply the kernel expression to a particle
	 *
	 * \param vd vector with the particles positions
	 * \param cl Cell-list (symmetric version)
	 * \param key to which particle to apply the expression
	 * \param lker kernel function
	 *
	 * \return the result of apply the kernel on the particle key
	 *
	 */
157
	inline static typename std::remove_reference<rtype>::type apply(const vector & vd, NN_type & cl, const vect_dist_key_dx & key, Kernel & lker)
Pietro Incardona's avatar
Pietro Incardona committed
158 159
	{
	    // accumulator
160
		typename std::remove_reference<rtype>::type pse = set_zero<typename std::remove_reference<rtype>::type>::create();
Pietro Incardona's avatar
Pietro Incardona committed
161 162

	    // position of particle p
163
	    Point<vector::dims,typename vector::stype> p = vd.getPos(key);
Pietro Incardona's avatar
Pietro Incardona committed
164 165 166 167 168 169 170 171 172 173 174

	    // Get the neighborhood of the particle
	    auto NN = cl.template getNNIterator<NO_CHECK>(cl.getCell(p));
	    while(NN.isNext())
	    {
	    	auto nnp = NN.get();

	    	// Calculate contribution given by the kernel value at position p,
	    	// given by the Near particle, exclude itself
	    	if (nnp != key.getKey())
	    	{
175 176
	    	    // position of the particle q
	    		Point<vector::dims,typename vector::stype> q = vd.getPos(nnp);
Pietro Incardona's avatar
Pietro Incardona committed
177

178
	    	    pse += lker.value(p,q);
Pietro Incardona's avatar
Pietro Incardona committed
179 180 181 182 183 184 185 186 187 188 189
	    	}

	    	// Next particle
	    	++NN;
	    }

	    return pse;
	}
};


190

Pietro Incardona's avatar
Pietro Incardona committed
191 192 193 194 195 196 197
/*! \brief Apply the kernel to particle differently that is a number or is an expression
 *
 *
 */
template<typename T, typename vector, typename exp,typename NN_type, typename Kernel, typename rtype, bool is_exp=is_expression<T>::value>
struct apply_kernel_is_number_or_expression_gen
{
198 199 200 201 202 203 204 205 206 207 208
	/*! \brief Apply the kernel expression to a particle
	 *
	 * \param vd vector with the particles positions
	 * \param cl Cell-list
	 * \param v_exp expression to execute for each particle
	 * \param key to which particle to apply the expression
	 * \param lker kernel function
	 *
	 * \return the result of apply the kernel on the particle key
	 *
	 */
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)
Pietro Incardona's avatar
Pietro Incardona committed
210 211
	{
	    // accumulator
212
	    typename std::remove_reference<rtype>::type pse = set_zero<typename std::remove_reference<rtype>::type>::create();
Pietro Incardona's avatar
Pietro Incardona committed
213 214 215 216 217

	    // property of the particle x
	    rtype prp_p = v_exp.value(key);

	    // position of particle p
218
	    Point<vector::dims,typename vector::stype> p = vd.getPos(key);
Pietro Incardona's avatar
Pietro Incardona committed
219 220 221 222 223 224 225 226 227 228 229

	    // Get the neighborhood of the particle
	    auto NN = cl.template getNNIterator<NO_CHECK>(cl.getCell(p));
	    while(NN.isNext())
	    {
	    	auto nnp = NN.get();

	    	// Calculate contribution given by the kernel value at position p,
	    	// given by the Near particle, exclude itself
	    	if (nnp != key.getKey())
	    	{
incardon's avatar
incardon committed
230 231 232
	    		vect_dist_key_dx nnp_k;
	    		nnp_k.setKey(nnp);

233
	    	    // property of the particle x
incardon's avatar
incardon committed
234
	    		rtype prp_q = v_exp.value(nnp_k);
Pietro Incardona's avatar
Pietro Incardona committed
235

236
	    	    pse += lker.value(key.getKey(),nnp,prp_p,prp_q,vd);
Pietro Incardona's avatar
Pietro Incardona committed
237 238 239 240 241 242 243 244 245 246
	    	}

	    	// Next particle
	    	++NN;
	    }

	    return pse;
	}
};

247

Pietro Incardona's avatar
Pietro Incardona committed
248 249 250 251 252 253 254 255 256
/*! \brief Apply kernel operation
 *
 * \tparam exp1 expression1
 * \tparam NN list
 *
 */
template <typename exp1,typename vector_type>
class vector_dist_expression_op<exp1,vector_type,VECT_APPLYKER_IN>
{
257
	//! Get the type of the Cell-list
Pietro Incardona's avatar
Pietro Incardona committed
258
	typedef typename boost::mpl::at<vector_type,boost::mpl::int_<0>>::type NN;
259
	//! Get the type of the kernel
Pietro Incardona's avatar
Pietro Incardona committed
260
	typedef typename boost::mpl::at<vector_type,boost::mpl::int_<1>>::type Kernel;
261
	//! Get the type that contain the particle positions
Pietro Incardona's avatar
Pietro Incardona committed
262 263
	typedef typename boost::mpl::at<vector_type,boost::mpl::int_<2>>::type vector_orig;

264
	//! expression
Pietro Incardona's avatar
Pietro Incardona committed
265
	const exp1 o1;
266 267

	//! Cell-list
Pietro Incardona's avatar
Pietro Incardona committed
268
	NN & cl;
269 270

	//! kernel
Pietro Incardona's avatar
Pietro Incardona committed
271
	Kernel & ker;
272 273

	//! The vector that contain the particles
Pietro Incardona's avatar
Pietro Incardona committed
274 275
	const vector_orig & vd;

incardon's avatar
incardon committed
276
	//! Get the return type of applying the kernel to a particle
incardon's avatar
incardon committed
277
	typedef typename apply_kernel_rtype<decltype(o1.value(vect_dist_key_dx()))>::rtype rtype;
Pietro Incardona's avatar
Pietro Incardona committed
278 279 280 281 282 283 284 285 286 287 288 289 290

public:

	/*! \brief This function must be called before value
	 *
	 * it initialize the expression if needed
	 *
	 */
	inline void init() const
	{
		o1.init();
	}

291 292 293 294 295
	/*! \brief Constructor
	 *
	 * \param o1 expression
	 * \param cl Cell-list
	 * \param ker kernel function
296
	 * \param vd vector containing the particle positions
297 298
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
299 300 301 302 303 304 305 306
	vector_dist_expression_op(const exp1 & o1, NN & cl, Kernel & ker, const vector_orig & vd)
	:o1(o1),cl(cl),ker(ker),vd(vd)
	{}

	/*! \brief Evaluate the expression
	 *
	 * \param key where to evaluate the expression
	 *
307 308
	 * \return the result of the expression
	 *
Pietro Incardona's avatar
Pietro Incardona committed
309
	 */
310
	inline typename std::remove_reference<rtype>::type value(const vect_dist_key_dx & key) const
Pietro Incardona's avatar
Pietro Incardona committed
311 312 313 314 315 316 317 318 319 320 321 322
	{
		return apply_kernel_is_number_or_expression<decltype(o1.value(key)),vector_orig,exp1,NN,Kernel,rtype>::apply(vd,cl,o1,key,ker);
	}
};

/*! \brief Apply kernel operation
 *
 * \tparam exp1 expression1
 * \tparam NN list
 *
 */
template <typename exp1,typename vector_type>
323
class vector_dist_expression_op<exp1,vector_type,VECT_APPLYKER_IN_SIM>
Pietro Incardona's avatar
Pietro Incardona committed
324
{
325
	//! Return the type of the Cell-list
Pietro Incardona's avatar
Pietro Incardona committed
326
	typedef typename boost::mpl::at<vector_type,boost::mpl::int_<0>>::type NN;
327
	//! Return the type of the kernel
Pietro Incardona's avatar
Pietro Incardona committed
328
	typedef typename boost::mpl::at<vector_type,boost::mpl::int_<1>>::type Kernel;
329
	//! Return the vector containing the position of the particles
Pietro Incardona's avatar
Pietro Incardona committed
330 331
	typedef typename boost::mpl::at<vector_type,boost::mpl::int_<2>>::type vector_orig;

332
	//! Cell-list
Pietro Incardona's avatar
Pietro Incardona committed
333
	NN & cl;
334
	//! kernel
Pietro Incardona's avatar
Pietro Incardona committed
335
	Kernel & ker;
336
	//! vector with the particle positions
Pietro Incardona's avatar
Pietro Incardona committed
337 338
	const vector_orig & vd;

339
	//! Get the return type of the expression
340
	typedef typename 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;
Pietro Incardona's avatar
Pietro Incardona committed
341 342 343 344


public:

345 346 347 348 349 350 351 352 353
	/*! \brief This function must be called before value
	 *
	 * it initialize the expression if needed
	 *
	 */
	inline void init() const
	{
	}

354 355 356 357 358 359 360
	/*! \brief Constructor
	 *
	 * \param cl cell-list
	 * \param ker kernel
	 * \param vd Vector containing the particle positions
	 *
	 */
361 362
	vector_dist_expression_op(NN & cl, Kernel & ker, const vector_orig & vd)
	:cl(cl),ker(ker),vd(vd)
Pietro Incardona's avatar
Pietro Incardona committed
363 364
	{}

365
	/*! \brief Evaluate the expression
Pietro Incardona's avatar
Pietro Incardona committed
366
	 *
367
	 * \param key where to evaluate the expression
Pietro Incardona's avatar
Pietro Incardona committed
368
	 *
369 370
	 * \return the result produced by the expression
	 *
Pietro Incardona's avatar
Pietro Incardona committed
371
	 */
372
	inline typename std::remove_reference<rtype>::type value(const vect_dist_key_dx & key) const
Pietro Incardona's avatar
Pietro Incardona committed
373
	{
374 375 376 377 378 379 380 381 382 383 384 385 386 387
		return apply_kernel_is_number_or_expression_sim<vector_orig,exp1,NN,Kernel,rtype>::apply(vd,cl,key,ker);
	}
};


/*! \brief Apply kernel operation
 *
 * \tparam exp1 expression1
 * \tparam NN list
 *
 */
template <typename exp1,typename vector_type>
class vector_dist_expression_op<exp1,vector_type,VECT_APPLYKER_IN_GEN>
{
388
	//! Get the type of the Cell-list
389
	typedef typename boost::mpl::at<vector_type,boost::mpl::int_<0>>::type NN;
390
	//! Get the type of the kernel
391
	typedef typename boost::mpl::at<vector_type,boost::mpl::int_<1>>::type Kernel;
392 393

	//! Get the type of the vector containing the set of particles
394
	typedef typename boost::mpl::at<vector_type,boost::mpl::int_<2>>::type vector_orig;
Pietro Incardona's avatar
Pietro Incardona committed
395

396
	//! Expression
397
	const exp1 o1;
398 399

	//! Cell list
400
	NN & cl;
401 402

	//! Kernel
403
	Kernel & ker;
404 405

	//! Vector containing the particles
406
	const vector_orig & vd;
Pietro Incardona's avatar
Pietro Incardona committed
407

408
	//! Return type of the expression
incardon's avatar
incardon committed
409
	typedef typename apply_kernel_rtype<decltype(o1.value(vect_dist_key_dx()))>::rtype rtype;
Pietro Incardona's avatar
Pietro Incardona committed
410

411
public:
Pietro Incardona's avatar
Pietro Incardona committed
412

413 414 415 416 417 418 419 420
	/*! \brief This function must be called before value
	 *
	 * it initialize the expression if needed
	 *
	 */
	inline void init() const
	{
		o1.init();
Pietro Incardona's avatar
Pietro Incardona committed
421 422
	}

423 424 425 426 427 428 429 430
	/*! \brief Constructor
	 *
	 * \param o1 expression
	 * \param cl Cell-list
	 * \param ker Kernel
	 * \param vd vector containing the set of particles
	 *
	 */
431 432 433 434
	vector_dist_expression_op(const exp1 & o1, NN & cl, Kernel & ker, const vector_orig & vd)
	:o1(o1),cl(cl),ker(ker),vd(vd)
	{}

Pietro Incardona's avatar
Pietro Incardona committed
435 436 437 438
	/*! \brief Evaluate the expression
	 *
	 * \param key where to evaluate the expression
	 *
439 440
	 * \return the result of the expression
	 *
Pietro Incardona's avatar
Pietro Incardona committed
441
	 */
442
	inline typename std::remove_reference<rtype>::type value(const vect_dist_key_dx & key) const
Pietro Incardona's avatar
Pietro Incardona committed
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);
Pietro Incardona's avatar
Pietro Incardona committed
445 446 447
	}
};

448

Pietro Incardona's avatar
Pietro Incardona committed
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
///////////////////////////////////////////// Apply kernel operator ////
////////////////////////////////////////////////////////////////////////

/* \brief Apply kernel expression
 *
 * \param va vector expression one
 * \param vb vector expression two
 *
 * \return an object that encapsulate the expression
 *
 */
template<typename exp1, typename exp2, unsigned int op1, typename NN, typename Kernel, typename vector_type>
inline vector_dist_expression_op<vector_dist_expression_op<exp1,exp2,op1>,boost::mpl::vector<NN,Kernel,vector_type>,VECT_APPLYKER_IN>
applyKernel_in(const vector_dist_expression_op<exp1,exp2,op1> & va, vector_type & vd, NN & cl, Kernel & ker)
{
	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);

	return exp_sum;
}

469

Pietro Incardona's avatar
Pietro Incardona committed
470 471 472 473 474 475 476 477 478
/* \brief Apply kernel expression
 *
 * \param va vector expression one
 * \param vb vector expression two
 *
 * \return an object that encapsulate the expression
 *
 */
template<typename exp1, typename exp2, unsigned int op1, typename NN, typename Kernel, typename vector_type>
479 480
inline vector_dist_expression_op<vector_dist_expression_op<exp1,exp2,op1>,boost::mpl::vector<NN,Kernel,vector_type>,VECT_APPLYKER_IN_GEN>
applyKernel_in_gen(const vector_dist_expression_op<exp1,exp2,op1> & va, vector_type & vd, NN & cl, Kernel & ker)
Pietro Incardona's avatar
Pietro Incardona committed
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);
Pietro Incardona's avatar
Pietro Incardona committed
483 484 485 486 487

	return exp_sum;
}


488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
//////////////////////////////////////// For vector expression ///////////////////////

/* \brief Apply kernel expression
 *
 * \param va vector expression one
 * \param vb vector expression two
 *
 * \return an object that encapsulate the expression
 *
 */
template<unsigned int prp, typename NN, typename Kernel, typename vector_type>
inline vector_dist_expression_op<vector_dist_expression<prp,vector_type>,boost::mpl::vector<NN,Kernel,vector_type>,VECT_APPLYKER_IN>
applyKernel_in(const vector_dist_expression<prp,vector_type> & va, vector_type & vd, NN & cl, Kernel & ker)
{
	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);

	return exp_sum;
}


/* \brief Apply kernel expression
 *
 * \param va vector expression one
 * \param vb vector expression two
 *
 * \return an object that encapsulate the expression
 *
 */
template<unsigned int prp, typename NN, typename Kernel, typename vector_type>
inline vector_dist_expression_op<vector_dist_expression<prp,vector_type>,boost::mpl::vector<NN,Kernel,vector_type>,VECT_APPLYKER_IN_GEN>
applyKernel_in_gen(const vector_dist_expression<prp,vector_type> & va, vector_type & vd, NN & cl, Kernel & ker)
{
	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);

	return exp_sum;
}


/* \brief Apply kernel expression
 *
 * \param va vector expression one
 * \param vb vector expression two
 *
 * \return an object that encapsulate the expression
 *
 */
template<typename NN, typename Kernel, typename vector_type>
inline vector_dist_expression_op<void,boost::mpl::vector<NN,Kernel,vector_type>,VECT_APPLYKER_IN_SIM>
applyKernel_in_sim(vector_type & vd, NN & cl, Kernel & ker)
{
	vector_dist_expression_op<void,boost::mpl::vector<NN,Kernel,vector_type>,VECT_APPLYKER_IN_SIM> exp_sum(cl,ker,vd);

	return exp_sum;
}

Pietro Incardona's avatar
Pietro Incardona committed
543 544

#endif /* OPENFPM_NUMERICS_SRC_OPERATORS_VECTOR_VECTOR_DIST_OPERATORS_APPLY_KERNEL_HPP_ */