| 1 | /* |
| 2 | * sum.hpp |
| 3 | * |
| 4 | * Created on: Oct 13, 2015 |
| 5 | * Author: i-bird |
| 6 | */ |
| 7 | |
| 8 | #ifndef OPENFPM_NUMERICS_SRC_FINITEDIFFERENCE_SUM_HPP_ |
| 9 | #define OPENFPM_NUMERICS_SRC_FINITEDIFFERENCE_SUM_HPP_ |
| 10 | |
| 11 | #include <boost/mpl/vector.hpp> |
| 12 | #include "config.h" |
| 13 | #include <unordered_map> |
| 14 | #include "util/for_each_ref.hpp" |
| 15 | |
| 16 | /*! \brief sum functor value |
| 17 | * |
| 18 | * \param v_expr vector expression |
| 19 | * |
| 20 | */ |
| 21 | template<typename v_expr> |
| 22 | struct sum_functor_value |
| 23 | { |
| 24 | //! Number of elements in the vector v_expr |
| 25 | typedef boost::mpl::size<v_expr> size; |
| 26 | |
| 27 | //! Last element of sum |
| 28 | typedef typename boost::mpl::at<v_expr,boost::mpl::int_<size::value-1> >::type last; |
| 29 | |
| 30 | //! sum functor |
| 31 | std::unordered_map<long int,typename last::stype> & cols; |
| 32 | |
| 33 | //! Grid info |
| 34 | const grid_sm<last::dims,void> & gs; |
| 35 | |
| 36 | //! grid mapping |
| 37 | const typename stub_or_real<last,last::dims,typename last::stype,typename last::b_grid::decomposition::extended_type>::type & g_map; |
| 38 | |
| 39 | //! grid position |
| 40 | grid_dist_key_dx<last::dims> & kmap; |
| 41 | |
| 42 | //! coefficent |
| 43 | typename last::stype coeff; |
| 44 | |
| 45 | //! spacing |
| 46 | typename last::stype (& spacing)[last::dims]; |
| 47 | |
| 48 | |
| 49 | /*! \brief constructor |
| 50 | * |
| 51 | * \param g_map Grid mapping, it convert grid position into vector/Matrix row |
| 52 | * \param kmap grid position |
| 53 | * \param gs grid information |
| 54 | * \param spacing grid spacing |
| 55 | * \param cols unordered map contain the map colum -> value |
| 56 | * \param coeff it contain an additional actual coefficients in front of the values |
| 57 | * |
| 58 | */ |
| 59 | sum_functor_value(const typename stub_or_real<last,last::dims,typename last::stype,typename last::b_grid::decomposition::extended_type>::type & g_map, |
| 60 | grid_dist_key_dx<last::dims> & kmap, |
| 61 | const grid_sm<last::dims,void> & gs, |
| 62 | typename last::stype (& spacing)[last::dims], |
| 63 | std::unordered_map<long int,typename last::stype> & cols, |
| 64 | typename last::stype coeff) |
| 65 | :cols(cols),gs(gs),g_map(g_map),kmap(kmap),coeff(coeff),spacing(spacing) |
| 66 | {}; |
| 67 | |
| 68 | /*! \brief It call this function for every expression operand in the sum |
| 69 | * |
| 70 | * \param t expression operand id |
| 71 | * |
| 72 | */ |
| 73 | template<typename T> |
| 74 | void operator()(T& t) const |
| 75 | { |
| 76 | boost::mpl::at<v_expr, boost::mpl::int_<T::value> >::type::value(g_map,kmap,gs,spacing,cols,coeff); |
| 77 | } |
| 78 | |
| 79 | }; |
| 80 | |
| 81 | /*! \brief It model an expression expr1 + ... exprn |
| 82 | * |
| 83 | * \tparam expr.. two or more expression to be summed |
| 84 | * \tparam Sys_eqs stystem specification |
| 85 | * |
| 86 | * ## Example |
| 87 | * |
| 88 | * \snippet FDScheme_unit_tests.hpp sum example |
| 89 | * |
| 90 | */ |
| 91 | template<typename ... expr> |
| 92 | struct sum |
| 93 | { |
| 94 | //! Transform from variadic template to boost mpl vector |
| 95 | typedef boost::mpl::vector<expr... > v_expr; |
| 96 | |
| 97 | //! size of v_expr |
| 98 | typedef typename boost::mpl::size<v_expr>::type v_sz; |
| 99 | |
| 100 | //! struct that specify the syste, of equations |
| 101 | typedef typename boost::mpl::at<v_expr, boost::mpl::int_<v_sz::type::value - 1> >::type Sys_eqs; |
| 102 | |
| 103 | /*! \brief Calculate which colums of the Matrix are non zero |
| 104 | * |
| 105 | * \param g_map Grid mapping, it convert grid position into vector/Matrix row |
| 106 | * \param kmap grid position |
| 107 | * \param gs grid information |
| 108 | * \param spacing grid spacing |
| 109 | * \param cols unordered map contain the map colum -> value |
| 110 | * \param coeff it contain an additional actual coefficients in front of the values |
| 111 | * |
| 112 | * ### Example |
| 113 | * |
| 114 | * \snippet FDScheme_unit_tests.hpp sum example |
| 115 | * |
| 116 | */ |
| 117 | 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, |
| 118 | grid_dist_key_dx<Sys_eqs::dims> & kmap, |
| 119 | const grid_sm<Sys_eqs::dims,void> & gs, |
| 120 | typename Sys_eqs::stype (& spacing )[Sys_eqs::dims], |
| 121 | std::unordered_map<long int,typename Sys_eqs::stype > & cols, |
| 122 | typename Sys_eqs::stype coeff) |
| 123 | { |
| 124 | // Sum functor |
| 125 | sum_functor_value<v_expr> sm(g_map,kmap,gs,spacing,cols,coeff); |
| 126 | |
| 127 | // for each element in the expression calculate the non-zero Matrix elements |
| 128 | boost::mpl::for_each_ref< boost::mpl::range_c<int,0,v_sz::type::value - 1> >(sm); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | }; |
| 133 | |
| 134 | /*! \brief It ancapsulate the minus operation |
| 135 | * |
| 136 | * \tparam arg |
| 137 | * \tparam Sys_eqs system of equation |
| 138 | * |
| 139 | */ |
| 140 | template<typename arg, typename Sys_eqs> |
| 141 | struct minus |
| 142 | { |
| 143 | /*! \brief Create the row of the Matrix |
| 144 | * |
| 145 | * \tparam ord |
| 146 | * |
| 147 | * \snippet FDScheme_unit_tests.hpp minus example |
| 148 | * |
| 149 | * \param g_map Grid mapping, it convert grid position into vector/Matrix row |
| 150 | * \param kmap grid position |
| 151 | * \param gs grid information |
| 152 | * \param spacing grid spacing |
| 153 | * \param cols unordered map contain the map colum -> value |
| 154 | * \param coeff it contain an additional actual coefficients in front of the values |
| 155 | * |
| 156 | */ |
| 157 | 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, |
| 158 | grid_dist_key_dx<Sys_eqs::dims> & kmap, |
| 159 | const grid_sm<Sys_eqs::dims,void> & gs, |
| 160 | typename Sys_eqs::stype (& spacing )[Sys_eqs::dims], |
| 161 | std::unordered_map<long int,typename Sys_eqs::stype > & cols, |
| 162 | typename Sys_eqs::stype coeff) |
| 163 | { |
| 164 | arg::value(g_map,kmap,gs,spacing,cols,-coeff); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | }; |
| 169 | |
| 170 | #endif /* OPENFPM_NUMERICS_SRC_FINITEDIFFERENCE_SUM_HPP_ */ |
| 171 | |