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

jenkins's avatar
jenkins committed
8 9
	#ifndef OPENFPM_NUMERICS_SRC_FINITEDIFFERENCE_SUM_HPP_
	#define OPENFPM_NUMERICS_SRC_FINITEDIFFERENCE_SUM_HPP_
incardon's avatar
incardon committed
10

jenkins's avatar
jenkins committed
11 12 13 14
	#include <boost/mpl/vector.hpp>
	#include "config.h"
	#include <unordered_map>
	#include "util/for_each_ref.hpp"
incardon's avatar
incardon committed
15

incardon's avatar
incardon committed
16 17 18 19 20
	/*! \brief sum functor value
	 *
	 * \param v_expr vector expression
	 *
	 */
jenkins's avatar
jenkins committed
21 22 23 24 25
	template<typename v_expr>
	struct sum_functor_value
	{
		//! Number of elements in the vector v_expr
		typedef boost::mpl::size<v_expr> size;
incardon's avatar
incardon committed
26

jenkins's avatar
jenkins committed
27 28
		//! Last element of sum
		typedef typename boost::mpl::at<v_expr,boost::mpl::int_<size::value-1> >::type last;
incardon's avatar
incardon committed
29

jenkins's avatar
jenkins committed
30 31
		//! sum functor
		std::unordered_map<long int,typename last::stype> & cols;
incardon's avatar
incardon committed
32

jenkins's avatar
jenkins committed
33 34
		//! Grid info
		const grid_sm<last::dims,void> & gs;
35

incardon's avatar
incardon committed
36
		//! grid mapping
jenkins's avatar
jenkins committed
37
		const typename stub_or_real<last,last::dims,typename last::stype,typename last::b_grid::decomposition::extended_type>::type & g_map;
38

incardon's avatar
incardon committed
39
		//! grid position
jenkins's avatar
jenkins committed
40
		grid_dist_key_dx<last::dims> & kmap;
incardon's avatar
incardon committed
41

jenkins's avatar
jenkins committed
42 43
		//! coefficent
		typename last::stype coeff;
incardon's avatar
incardon committed
44

jenkins's avatar
jenkins committed
45 46
		//! spacing
		typename last::stype (& spacing)[last::dims];
47 48


jenkins's avatar
jenkins committed
49
		/*! \brief constructor
incardon's avatar
incardon committed
50 51 52 53 54 55 56
		 *
		 * \param g_map Grid mapping, it convert grid position into vector/Matrix row
		 * \param kmap grid position
		 * \param gs grid information
		 * \param spacing grid spacing
		 * \param cols unordered map contain the map colum -> value
		 * \param coeff it contain an additional actual coefficients in front of the values
jenkins's avatar
jenkins committed
57 58
		 *
		 */
incardon's avatar
incardon committed
59 60 61 62 63 64
		sum_functor_value(const typename stub_or_real<last,last::dims,typename last::stype,typename last::b_grid::decomposition::extended_type>::type & g_map,
				          grid_dist_key_dx<last::dims> & kmap,
						  const grid_sm<last::dims,void> & gs,
						  typename last::stype (& spacing)[last::dims],
						  std::unordered_map<long int,typename last::stype> & cols,
						  typename last::stype coeff)
jenkins's avatar
jenkins committed
65
		:cols(cols),gs(gs),g_map(g_map),kmap(kmap),coeff(coeff),spacing(spacing)
incardon's avatar
incardon committed
66 67
	{};

incardon's avatar
incardon committed
68 69 70 71 72
	/*! \brief It call this function for every expression operand in the sum
	 *
	 * \param t expression operand id
	 *
	 */
incardon's avatar
incardon committed
73 74 75
	template<typename T>
	void operator()(T& t) const
	{
76
		boost::mpl::at<v_expr, boost::mpl::int_<T::value> >::type::value(g_map,kmap,gs,spacing,cols,coeff);
incardon's avatar
incardon committed
77
	}
78

incardon's avatar
incardon committed
79
};
incardon's avatar
incardon committed
80 81 82 83 84 85

/*! \brief It model an expression expr1 + ... exprn
 *
 * \tparam expr.. two or more expression to be summed
 * \tparam Sys_eqs stystem specification
 *
86 87 88 89
 * ## Example
 *
 * \snippet FDScheme_unit_tests.hpp sum example
 *
incardon's avatar
incardon committed
90 91
 */
template<typename ... expr>
incardon's avatar
incardon committed
92
struct sum
incardon's avatar
incardon committed
93
{
incardon's avatar
incardon committed
94
	//! Transform from variadic template to boost mpl vector
incardon's avatar
incardon committed
95 96
	typedef boost::mpl::vector<expr... > v_expr;

incardon's avatar
incardon committed
97
	//! size of v_expr
incardon's avatar
incardon committed
98 99
	typedef typename boost::mpl::size<v_expr>::type v_sz;

incardon's avatar
incardon committed
100
	//! struct that specify the syste, of equations
incardon's avatar
incardon committed
101 102
	typedef typename boost::mpl::at<v_expr, boost::mpl::int_<v_sz::type::value - 1> >::type Sys_eqs;

103
	/*! \brief Calculate which colums of the Matrix are non zero
incardon's avatar
incardon committed
104
	 *
incardon's avatar
incardon committed
105 106 107 108 109 110
	 * \param g_map Grid mapping, it convert grid position into vector/Matrix row
	 * \param kmap grid position
	 * \param gs grid information
	 * \param spacing grid spacing
	 * \param cols unordered map contain the map colum -> value
	 * \param coeff it contain an additional actual coefficients in front of the values
111 112 113 114
	 *
	 * ### Example
	 *
	 * \snippet FDScheme_unit_tests.hpp sum example
incardon's avatar
incardon committed
115 116
	 *
	 */
incardon's avatar
incardon committed
117 118 119 120 121 122
	inline static void value(const typename stub_or_real<Sys_eqs,Sys_eqs::dims,typename Sys_eqs::stype,typename Sys_eqs::b_grid::decomposition::extended_type>::type & g_map,
			                 grid_dist_key_dx<Sys_eqs::dims> & kmap,
							 const grid_sm<Sys_eqs::dims,void> & gs,
							 typename Sys_eqs::stype (& spacing )[Sys_eqs::dims],
							 std::unordered_map<long int,typename Sys_eqs::stype > & cols,
							 typename Sys_eqs::stype coeff)
incardon's avatar
incardon committed
123
	{
incardon's avatar
incardon committed
124
		// Sum functor
125
		sum_functor_value<v_expr> sm(g_map,kmap,gs,spacing,cols,coeff);
incardon's avatar
incardon committed
126 127 128

		// 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
129 130
	}

incardon's avatar
incardon committed
131

incardon's avatar
incardon committed
132
};
incardon's avatar
incardon committed
133

incardon's avatar
incardon committed
134 135 136 137 138 139
/*! \brief It ancapsulate the minus operation
 *
 * \tparam arg
 * \tparam Sys_eqs system of equation
 *
 */
140 141 142 143 144 145 146
template<typename arg, typename Sys_eqs>
struct minus
{
	/*! \brief Create the row of the Matrix
	 *
	 * \tparam ord
	 *
incardon's avatar
incardon committed
147 148 149 150 151 152 153 154
	 * \snippet FDScheme_unit_tests.hpp minus example
	 *
	 * \param g_map Grid mapping, it convert grid position into vector/Matrix row
	 * \param kmap grid position
	 * \param gs grid information
	 * \param spacing grid spacing
	 * \param cols unordered map contain the map colum -> value
	 * \param coeff it contain an additional actual coefficients in front of the values
155
	 *
156
	 */
incardon's avatar
incardon committed
157 158 159 160 161 162
	inline static void value(const typename stub_or_real<Sys_eqs,Sys_eqs::dims,typename Sys_eqs::stype,typename Sys_eqs::b_grid::decomposition::extended_type>::type & g_map,
			                 grid_dist_key_dx<Sys_eqs::dims> & kmap,
							 const grid_sm<Sys_eqs::dims,void> & gs,
							 typename Sys_eqs::stype (& spacing )[Sys_eqs::dims],
							 std::unordered_map<long int,typename Sys_eqs::stype > & cols,
							 typename Sys_eqs::stype coeff)
163 164 165 166
	{
		arg::value(g_map,kmap,gs,spacing,cols,-coeff);
	}

incardon's avatar
incardon committed
167

168 169
};

incardon's avatar
incardon committed
170
#endif /* OPENFPM_NUMERICS_SRC_FINITEDIFFERENCE_SUM_HPP_ */