Derivative.hpp 7.69 KB
 incardon committed Oct 14, 2015 1 2 3 4 5 6 7 8 9 10 11 12 ``````/* * Derivative.hpp * * Created on: Oct 5, 2015 * Author: i-bird */ #ifndef OPENFPM_NUMERICS_SRC_FINITEDIFFERENCE_DERIVATIVE_HPP_ #define OPENFPM_NUMERICS_SRC_FINITEDIFFERENCE_DERIVATIVE_HPP_ #define CENTRAL 0 #define CENTRAL_B_ONE_SIDE 1 `````` incardon committed Oct 26, 2015 13 ``````#define FORWARD 2 `````` incardon committed Oct 14, 2015 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 `````` #include "util/mathutil.hpp" #include "Vector/map_vector.hpp" #include "Grid/comb.hpp" #include "FiniteDifference/util/common.hpp" /*! \brief Derivative second order on h (spacing) * * \tparam d on which dimension derive * \tparam Field which field derive * \tparam impl which implementation * */ template class D { /*! \brief Create the row of the Matrix * * \tparam ord * */ `````` incardon committed Oct 23, 2015 35 `````` inline static void value(const grid_key_dx & pos, const grid_sm & gs, std::unordered_map & cols, typename Sys_eqs::stype coeff) `````` incardon committed Oct 14, 2015 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 `````` { std::cerr << "Error " << __FILE__ << ":" << __LINE__ << " only CENTRAL, FORWARD, BACKWARD derivative are defined"; } /*! \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 position(grid_key_dx & pos, const grid_sm & gs) { std::cerr << "Error " << __FILE__ << ":" << __LINE__ << " only CENTRAL, FORWARD, BACKWARD derivative are defined"; } }; /*! \brief Derivative on direction i * * */ template class D { public: /*! \brief fill the row * * */ `````` incardon committed Oct 28, 2015 65 `````` inline static void value(const grid_dist_id,typename Sys_eqs::b_grid::decomposition> & g_map, grid_dist_key_dx & kmap , const grid_sm & gs, std::unordered_map & cols, typename Sys_eqs::stype coeff) `````` incardon committed Oct 14, 2015 66 `````` { `````` incardon committed Oct 26, 2015 67 68 69 `````` // if the system is staggered the CENTRAL derivative is equivalent to a forward derivative if (is_grid_staggered::value() == true) { `````` incardon committed Oct 28, 2015 70 `````` D::value(g_map,kmap,gs,cols,coeff); `````` incardon committed Oct 26, 2015 71 72 73 `````` return; } `````` incardon committed Oct 28, 2015 74 75 76 77 `````` long int old_val = kmap.getKeyRef().get(d); kmap.getKeyRef().set_d(d, kmap.getKeyRef().get(d) + 1); arg::value(g_map,kmap,gs,cols,coeff); kmap.getKeyRef().set_d(d,old_val); `````` incardon committed Oct 14, 2015 78 `````` `````` incardon committed Oct 28, 2015 79 80 81 82 83 `````` old_val = kmap.getKeyRef().get(d); kmap.getKeyRef().set_d(d, kmap.getKeyRef().get(d) - 1); arg::value(g_map,kmap,gs,cols,-coeff); kmap.getKeyRef().set_d(d,old_val); `````` incardon committed Oct 14, 2015 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 `````` } /*! \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 * * \param pos from the position * \param fld Field we are deriving, if not provided the function just return pos * \param s_pos position of the properties in the staggered grid * */ inline static grid_key_dx position(grid_key_dx & pos, long int fld = -1, const openfpm::vector> & s_pos = openfpm::vector>()) { if (is_grid_staggered::value()) { if (fld == -1) return pos; if (s_pos[fld][d] == 1) { grid_key_dx ret = pos; ret.set_d(d,0); return pos; } else { grid_key_dx ret = pos; ret.set_d(d,1); return pos; } } return pos; } }; /*! \brief Derivative on direction i * * */ template class D { public: /*! \brief fill the row * * */ `````` incardon committed Oct 28, 2015 136 `````` static void value(const grid_dist_id,typename Sys_eqs::b_grid::decomposition> & g_map, grid_dist_key_dx & kmap , const grid_sm & gs, std::unordered_map & cols, typename Sys_eqs::stype coeff) `````` incardon committed Oct 14, 2015 137 138 139 140 141 142 `````` { #ifdef SE_CLASS1 if (Sys_eqs::boundary[d] == PERIODIC) std::cerr << __FILE__ << ":" << __LINE__ << " error, it make no sense use one sided derivation with periodic boundary\n"; #endif `````` incardon committed Oct 28, 2015 143 144 `````` grid_key_dx pos = g_map.getGKey(kmap); `````` incardon committed Oct 14, 2015 145 146 147 148 `````` if (pos.get(d) == (long int)gs.size(d)-1 ) { arg::value(pos,gs,cols,1.5*coeff); `````` incardon committed Oct 28, 2015 149 150 151 152 `````` long int old_val = kmap.getKeyRef().get(d); kmap.getKeyRef().set_d(d, kmap.getKeyRef().get(d) - 1); arg::value(g_map,kmap,gs,cols,-2.0*coeff); kmap.getKeyRef().set_d(d,old_val); `````` incardon committed Oct 14, 2015 153 `````` `````` incardon committed Oct 28, 2015 154 155 156 157 `````` old_val = kmap.getKeyRef().get(d); pos.set_d(d, kmap.getKeyRef().get(d) - 2); arg::value(g_map,kmap,gs,cols,0.5*coeff); kmap.getKeyRef().set_d(d,old_val); `````` incardon committed Oct 14, 2015 158 159 160 161 162 `````` } else if (pos.get(d) == 0) { arg::value(pos,gs,cols,-1.5*coeff); `````` incardon committed Oct 28, 2015 163 164 165 166 `````` long int old_val = kmap.getKeyRef().get(d); kmap.getKeyRef().set_d(d, kmap.getKeyRef().get(d) + 1); arg::value(g_map,kmap,gs,cols,2.0*coeff); kmap.getKeyRef().set_d(d,old_val); `````` incardon committed Oct 14, 2015 167 `````` `````` incardon committed Oct 28, 2015 168 169 170 171 `````` old_val = kmap.getKeyRef().get(d); pos.set_d(d, kmap.getKeyRef().get(d) + 2); arg::value(g_map,kmap,gs,cols,-0.5*coeff); kmap.getKeyRef().set_d(d,old_val); `````` incardon committed Oct 14, 2015 172 173 174 `````` } else { `````` incardon committed Oct 28, 2015 175 176 177 178 179 180 181 182 183 `````` long int old_val = kmap.getKeyRef().get(d); kmap.getKeyRef().set_d(d, kmap.getKeyRef().get(d) + 1); arg::value(g_map,kmap,gs,cols,coeff); kmap.getKeyRef().set_d(d,old_val); old_val = kmap.getKeyRef().get(d); kmap.getKeyRef().set_d(d, kmap.getKeyRef().get(d) - 1); arg::value(g_map,kmap,gs,cols,-coeff); kmap.getKeyRef().set_d(d,old_val); `````` incardon committed Oct 14, 2015 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 `````` } } /*! \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 position(grid_key_dx & pos, long int fld = 0, const openfpm::vector> & s_pos = openfpm::vector>()) { if (is_grid_staggered::type::value) { if (fld == -1) return pos; if (s_pos[fld][d] == 1) { grid_key_dx ret = pos; ret.set_d(d,0); return pos; } else { grid_key_dx ret = pos; ret.set_d(d,1); return pos; } } return pos; } }; `````` incardon committed Oct 26, 2015 219 220 ``````/*! \brief Derivative FORWARD on direction i * `````` incardon committed Oct 28, 2015 221 `````` *g `````` incardon committed Oct 26, 2015 222 223 224 225 226 227 228 229 230 231 `````` */ template class D { public: /*! \brief fill the row * * */ `````` incardon committed Oct 28, 2015 232 `````` inline static void value(const grid_dist_id,typename Sys_eqs::b_grid::decomposition> & g_map, grid_dist_key_dx & kmap , const grid_sm & gs, std::unordered_map & cols, typename Sys_eqs::stype coeff) `````` incardon committed Oct 26, 2015 233 `````` { `````` incardon committed Oct 28, 2015 234 235 236 237 238 `````` long int old_val = kmap.getKeyRef().get(d); kmap.getKeyRef().set_d(d, kmap.getKeyRef().get(d) + 1); arg::value(g_map,kmap,gs,cols,coeff); kmap.getKeyRef().set_d(d,old_val); `````` incardon committed Oct 26, 2015 239 240 `````` // backward `````` incardon committed Oct 28, 2015 241 `````` arg::value(g_map,kmap,gs,cols,-coeff); `````` incardon committed Oct 26, 2015 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 `````` } /*! \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 * * \param pos from the position * \param fld Field we are deriving, if not provided the function just return pos * \param s_pos position of the properties in the staggered grid * */ inline static grid_key_dx position(grid_key_dx & pos, long int fld = -1, const openfpm::vector> & s_pos = openfpm::vector>()) { return pos; } }; `````` incardon committed Oct 14, 2015 261 262 `````` #endif /* OPENFPM_NUMERICS_SRC_FINITEDIFFERENCE_DERIVATIVE_HPP_ */``````