sum.hpp 3.16 KB
Newer Older
incardon's avatar
incardon committed
1 2 3 4 5 6 7 8 9 10 11
/*
 * sum.hpp
 *
 *  Created on: Oct 13, 2015
 *      Author: i-bird
 */

#ifndef OPENFPM_NUMERICS_SRC_FINITEDIFFERENCE_SUM_HPP_
#define OPENFPM_NUMERICS_SRC_FINITEDIFFERENCE_SUM_HPP_

#include <boost/mpl/vector.hpp>
incardon's avatar
incardon committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include "config.h"
#include <unordered_map>
#include "util/for_each_ref.hpp"

template<typename v_expr>
struct sum_functor_value
{
	//! Number of elements in the vector v_expr
	typedef boost::mpl::size<v_expr> size;

	//! Last element of sum
	typedef typename boost::mpl::at<v_expr,boost::mpl::int_<size::value-1> >::type last;

	//! sum functor
	std::unordered_map<long int,typename last::stype> & cols;

incardon's avatar
incardon committed
28 29 30
	//! Grid info
	const grid_sm<last::dims,void> & gs;

31 32 33 34 35 36
	// grid mapping
	const grid_dist_id<last::dims,typename last::stype,scalar<size_t>,typename last::b_grid::decomposition> & g_map;

	// grid position
	grid_dist_key_dx<last::dims> & kmap;

incardon's avatar
incardon committed
37 38 39 40 41 42 43 44 45
	//! position
	grid_key_dx<last::dims> & key;

	//! coefficent
	typename last::stype coeff;

	/*! \brief constructor
	 *
	 */
46
	sum_functor_value(const grid_dist_id<last::dims,typename last::stype,scalar<size_t>,typename last::b_grid::decomposition> & g_map, grid_dist_key_dx<last::dims> & kmap, const grid_sm<last::dims,void> & gs, std::unordered_map<long int,typename last::stype> & cols, typename last::stype coeff)
incardon's avatar
incardon committed
47
	:cols(cols),gs(gs),g_map(g_map),kmap(kmap),key(key),coeff(coeff)
incardon's avatar
incardon committed
48 49 50 51 52 53 54 55
	{};



	//! It call this function for every expression in the sum
	template<typename T>
	void operator()(T& t) const
	{
56
		boost::mpl::at<v_expr, boost::mpl::int_<T::value> >::type::value(g_map,kmap,gs,cols,coeff);
incardon's avatar
incardon committed
57 58
	}
};
incardon's avatar
incardon committed
59 60 61 62 63 64 65 66

/*! \brief It model an expression expr1 + ... exprn
 *
 * \tparam expr.. two or more expression to be summed
 * \tparam Sys_eqs stystem specification
 *
 */
template<typename ... expr>
incardon's avatar
incardon committed
67
struct sum
incardon's avatar
incardon committed
68 69 70 71
{
	// Transform from variadic template to boost mpl vector
	typedef boost::mpl::vector<expr... > v_expr;

incardon's avatar
incardon committed
72 73 74 75 76 77 78 79 80 81
	// size of v_expr
	typedef typename boost::mpl::size<v_expr>::type v_sz;

	typedef typename boost::mpl::at<v_expr, boost::mpl::int_<v_sz::type::value - 1> >::type Sys_eqs;

	/*! \brief Create the row of the Matrix
	 *
	 * \tparam ord
	 *
	 */
82
	inline static void value(const grid_dist_id<Sys_eqs::dims,typename Sys_eqs::stype,scalar<size_t>,typename Sys_eqs::b_grid::decomposition> & g_map, grid_dist_key_dx<Sys_eqs::dims> & kmap, const grid_sm<Sys_eqs::dims,void> & gs, std::unordered_map<long int,typename Sys_eqs::stype > & cols, typename Sys_eqs::stype coeff)
incardon's avatar
incardon committed
83
	{
incardon's avatar
incardon committed
84
		// Sum functor
85
		sum_functor_value<v_expr> sm(g_map,kmap,gs,cols,coeff);
incardon's avatar
incardon committed
86 87 88

		// for each element in the expression calculate the non-zero Matrix elements
		boost::mpl::for_each_ref< boost::mpl::range_c<int,0,v_sz::type::value - 1> >(sm);
incardon's avatar
incardon committed
89 90 91 92 93 94 95 96 97 98 99 100
	}

	/*! \brief Calculate the position where the derivative is calculated
	 *
	 * In case on non staggered case this function just return pos, in case of staggered,
	 *  it calculate where the operator is calculated on a staggered grid
	 *
	 */
	inline static grid_key_dx<Sys_eqs::dims> position(grid_key_dx<Sys_eqs::dims> & pos, const grid_sm<Sys_eqs::dims,void> & gs)
	{
		std::cerr << "Error " << __FILE__ << ":" << __LINE__ << " only CENTRAL, FORWARD, BACKWARD derivative are defined";
	}
incardon's avatar
incardon committed
101 102 103 104
};


#endif /* OPENFPM_NUMERICS_SRC_FINITEDIFFERENCE_SUM_HPP_ */