Derivative.hpp 7.78 KB
Newer Older
incardon's avatar
incardon committed
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
13
#define FORWARD 2
incardon's avatar
incardon committed
14 15 16 17 18

#include "util/mathutil.hpp"
#include "Vector/map_vector.hpp"
#include "Grid/comb.hpp"
#include "FiniteDifference/util/common.hpp"
incardon's avatar
incardon committed
19
#include "util/util_num.hpp"
incardon's avatar
incardon committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

/*! \brief Derivative second order on h (spacing)
 *
 * \tparam d on which dimension derive
 * \tparam Field which field derive
 * \tparam impl which implementation
 *
 */
template<unsigned int d, typename Field, typename Sys_eqs, unsigned int impl=CENTRAL>
class D
{
	/*! \brief Create the row of the Matrix
	 *
	 * \tparam ord
	 *
	 */
incardon's avatar
incardon committed
36
	inline static void value(const grid_key_dx<Sys_eqs::dims> & pos, 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
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 65
	{
		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<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";
	}
};

/*! \brief Derivative on direction i
 *
 *
 */
template<unsigned int d, typename arg, typename Sys_eqs>
class D<d,arg,Sys_eqs,CENTRAL>
{
	public:

	/*! \brief fill the row
	 *
	 *
	 */
incardon's avatar
incardon committed
66
	inline static void value(const typename stub_or_real<Sys_eqs,Sys_eqs::dims,typename Sys_eqs::stype,typename Sys_eqs::b_grid::decomposition>::type & 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
67
	{
68 69 70
		// if the system is staggered the CENTRAL derivative is equivalent to a forward derivative
		if (is_grid_staggered<Sys_eqs>::value() == true)
		{
71
			D<d,arg,Sys_eqs,FORWARD>::value(g_map,kmap,gs,cols,coeff);
72 73 74
			return;
		}

75 76 77 78
		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's avatar
incardon committed
79

80 81 82 83 84

		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's avatar
incardon committed
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 136
	}


	/*! \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<Sys_eqs::dims> position(grid_key_dx<Sys_eqs::dims> & pos, long int fld = -1, const openfpm::vector<comb<Sys_eqs::dims>> & s_pos = openfpm::vector<comb<Sys_eqs::dims>>())
	{
		if (is_grid_staggered<Sys_eqs>::value())
		{
			if (fld == -1)
				return pos;

			if (s_pos[fld][d] == 1)
			{
				grid_key_dx<Sys_eqs::dims> ret = pos;
				ret.set_d(d,0);
				return pos;
			}
			else
			{
				grid_key_dx<Sys_eqs::dims> ret = pos;
				ret.set_d(d,1);
				return pos;
			}
		}

		return pos;
	}
};


/*! \brief Derivative on direction i
 *
 *
 */
template<unsigned int d, typename arg, typename Sys_eqs>
class D<d,arg,Sys_eqs,CENTRAL_B_ONE_SIDE>
{
public:

	/*! \brief fill the row
	 *
	 *
	 */
incardon's avatar
incardon committed
137
	static void value(const typename stub_or_real<Sys_eqs,Sys_eqs::dims,typename Sys_eqs::stype,typename Sys_eqs::b_grid::decomposition>::type & 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
138 139 140 141 142 143
	{
#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

144 145
		grid_key_dx<Sys_eqs::dims> pos = g_map.getGKey(kmap);

incardon's avatar
incardon committed
146 147
		if (pos.get(d) == (long int)gs.size(d)-1 )
		{
incardon's avatar
incardon committed
148
			arg::value(g_map,kmap,gs,cols,1.5*coeff);
incardon's avatar
incardon committed
149

150 151 152 153
			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's avatar
incardon committed
154

155
			old_val = kmap.getKeyRef().get(d);
incardon's avatar
incardon committed
156
			kmap.getKeyRef().set_d(d, kmap.getKeyRef().get(d) - 2);
157 158
			arg::value(g_map,kmap,gs,cols,0.5*coeff);
			kmap.getKeyRef().set_d(d,old_val);
incardon's avatar
incardon committed
159 160 161
		}
		else if (pos.get(d) == 0)
		{
incardon's avatar
incardon committed
162
			arg::value(g_map,kmap,gs,cols,-1.5*coeff);
incardon's avatar
incardon committed
163

164 165 166 167
			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's avatar
incardon committed
168

169
			old_val = kmap.getKeyRef().get(d);
incardon's avatar
incardon committed
170
			kmap.getKeyRef().set_d(d, kmap.getKeyRef().get(d) + 2);
171 172
			arg::value(g_map,kmap,gs,cols,-0.5*coeff);
			kmap.getKeyRef().set_d(d,old_val);
incardon's avatar
incardon committed
173 174 175
		}
		else
		{
176 177 178 179 180 181 182 183 184
			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's avatar
incardon committed
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 219
		}
	}

	/*! \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, long int fld = 0, const openfpm::vector<comb<Sys_eqs::dims>> & s_pos = openfpm::vector<comb<Sys_eqs::dims>>())
	{
		if (is_grid_staggered<Sys_eqs>::type::value)
		{
			if (fld == -1)
				return pos;

			if (s_pos[fld][d] == 1)
			{
				grid_key_dx<Sys_eqs::dims> ret = pos;
				ret.set_d(d,0);
				return pos;
			}
			else
			{
				grid_key_dx<Sys_eqs::dims> ret = pos;
				ret.set_d(d,1);
				return pos;
			}
		}

		return pos;
	}
};


220 221
/*! \brief Derivative FORWARD on direction i
 *
222
 *g
223 224 225 226 227 228 229 230 231 232
 */
template<unsigned int d, typename arg, typename Sys_eqs>
class D<d,arg,Sys_eqs,FORWARD>
{
	public:

	/*! \brief fill the row
	 *
	 *
	 */
incardon's avatar
incardon committed
233
	inline static void value(const typename stub_or_real<Sys_eqs,Sys_eqs::dims,typename Sys_eqs::stype,typename Sys_eqs::b_grid::decomposition>::type & 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)
234
	{
235 236 237 238 239

		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);
240 241

		// backward
242
		arg::value(g_map,kmap,gs,cols,-coeff);
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
	}


	/*! \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<Sys_eqs::dims> position(grid_key_dx<Sys_eqs::dims> & pos, long int fld = -1, const openfpm::vector<comb<Sys_eqs::dims>> & s_pos = openfpm::vector<comb<Sys_eqs::dims>>())
	{
		return pos;
	}
};

incardon's avatar
incardon committed
262 263

#endif /* OPENFPM_NUMERICS_SRC_FINITEDIFFERENCE_DERIVATIVE_HPP_ */