OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
Average.hpp
1/*
2 * Average.hpp
3 *
4 * Created on: Nov 18, 2015
5 * Author: Pietro Incardona
6 */
7
8#ifndef OPENFPM_NUMERICS_SRC_FINITEDIFFERENCE_AVERAGE_HPP_
9#define OPENFPM_NUMERICS_SRC_FINITEDIFFERENCE_AVERAGE_HPP_
10
11#include "FiniteDifference/FD_util_include.hpp"
12#include "util/mathutil.hpp"
13#include "Vector/map_vector.hpp"
14#include "Grid/comb.hpp"
15#include "FiniteDifference/util/common.hpp"
16#include "util/util_num.hpp"
17
25template<unsigned int d, typename Field, typename Sys_eqs, unsigned int impl=CENTRAL>
26class Avg
27{
33 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)
34 {
35 std::cerr << "Error " << __FILE__ << ":" << __LINE__ << " only CENTRAL, FORWARD, BACKWARD derivative are defined";
36 }
37
49 {
50 std::cerr << "Error " << __FILE__ << ":" << __LINE__ << " only CENTRAL, FORWARD, BACKWARD derivative are defined";
51 }
52};
53
64template<unsigned int d, typename arg, typename Sys_eqs>
65class Avg<d,arg,Sys_eqs,CENTRAL>
66{
67 public:
68
85 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)
86 {
87 // if the system is staggered the CENTRAL derivative is equivalent to a forward derivative
89 {
90 Avg<d,arg,Sys_eqs,BACKWARD>::value(g_map,kmap,gs,spacing,cols,coeff);
91 return;
92 }
93
94 long int old_val = kmap.getKeyRef().get(d);
95 kmap.getKeyRef().set_d(d, kmap.getKeyRef().get(d) + 1);
96 arg::value(g_map,kmap,gs,spacing,cols,coeff/2);
97 kmap.getKeyRef().set_d(d,old_val);
98
99
100 old_val = kmap.getKeyRef().get(d);
101 kmap.getKeyRef().set_d(d, kmap.getKeyRef().get(d) - 1);
102 arg::value(g_map,kmap,gs,spacing,cols,coeff/2);
103 kmap.getKeyRef().set_d(d,old_val);
104 }
105
106
117 {
118 auto arg_pos = arg::position(pos,gs,s_pos);
120 {
121 if (arg_pos.get(d) == -1)
122 {
123 arg_pos.set_d(d,0);
124 return arg_pos;
125 }
126 else
127 {
128 arg_pos.set_d(d,-1);
129 return arg_pos;
130 }
131 }
132
133 return arg_pos;
134 }
135};
136
147template<unsigned int d, typename arg, typename Sys_eqs>
148class Avg<d,arg,Sys_eqs,FORWARD>
149{
150 public:
151
168 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)
169 {
170
171 long int old_val = kmap.getKeyRef().get(d);
172 kmap.getKeyRef().set_d(d, kmap.getKeyRef().get(d) + 1);
173 arg::value(g_map,kmap,gs,spacing,cols,coeff/2);
174 kmap.getKeyRef().set_d(d,old_val);
175
176 // backward
177 arg::value(g_map,kmap,gs,spacing,cols,coeff/2);
178 }
179
180
192 {
193 return arg::position(pos,gs,s_pos);
194 }
195};
196
207template<unsigned int d, typename arg, typename Sys_eqs>
208class Avg<d,arg,Sys_eqs,BACKWARD>
209{
210 public:
211
228 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)
229 {
230 long int old_val = kmap.getKeyRef().get(d);
231 kmap.getKeyRef().set_d(d, kmap.getKeyRef().get(d) - 1);
232 arg::value(g_map,kmap,gs,spacing,cols,coeff/2);
233 kmap.getKeyRef().set_d(d,old_val);
234
235 // forward
236 arg::value(g_map,kmap,gs,spacing,cols,coeff/2);
237 }
238
239
251 {
252 return arg::position(pos,gs,s_pos);
253 }
254};
255
256#endif /* OPENFPM_NUMERICS_SRC_FINITEDIFFERENCE_AVERAGE_HPP_ */
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)
Calculate which colums of the Matrix are non zero.
Definition Average.hpp:228
static grid_key_dx< Sys_eqs::dims > position(grid_key_dx< Sys_eqs::dims > &pos, const grid_sm< Sys_eqs::dims, void > &gs, const comb< Sys_eqs::dims >(&s_pos)[Sys_eqs::nvar])
Calculate the position in the cell where the average is calculated.
Definition Average.hpp:250
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)
Calculate which colums of the Matrix are non zero.
Definition Average.hpp:85
static grid_key_dx< Sys_eqs::dims > position(grid_key_dx< Sys_eqs::dims > &pos, const grid_sm< Sys_eqs::dims, void > &gs, const comb< Sys_eqs::dims >(&s_pos)[Sys_eqs::nvar])
Calculate the position where the average is calculated.
Definition Average.hpp:116
static grid_key_dx< Sys_eqs::dims > position(grid_key_dx< Sys_eqs::dims > &pos, const grid_sm< Sys_eqs::dims, void > &gs, const comb< Sys_eqs::dims >(&s_pos)[Sys_eqs::nvar])
Calculate the position in the cell where the average is calculated.
Definition Average.hpp:191
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)
Calculate which colums of the Matrix are non zero.
Definition Average.hpp:168
Average.
Definition Average.hpp:27
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)
Create the row of the Matrix.
Definition Average.hpp:33
static grid_key_dx< Sys_eqs::dims > position(grid_key_dx< Sys_eqs::dims > &pos, const grid_sm< Sys_eqs::dims, void > &gs, const openfpm::vector< comb< Sys_eqs::dims > > &s_pos=openfpm::vector< comb< Sys_eqs::dims > >())
Calculate the position where the derivative is calculated.
Definition Average.hpp:48
Grid key for a distributed grid.
base_key & getKeyRef()
Get the reference key.
grid_key_dx is the key to access any element in the grid
Definition grid_key.hpp:19
Declaration grid_sm.
Definition grid_sm.hpp:167
Implementation of 1-D std::vector like structure.
Position of the element of dimension d in the hyper-cube of dimension dim.
Definition comb.hpp:35
is_grid_staggered analyse T if it has a property grid_type defined and indicate that the grid is stag...
Definition common.hpp:45