1/*
2 * Vector_util.hpp
3 *
4 * Created on: Dec 7, 2015
5 * Author: i-bird
6 */
7
8#ifndef OPENFPM_NUMERICS_SRC_VECTOR_VECTOR_UTIL_HPP_
9#define OPENFPM_NUMERICS_SRC_VECTOR_VECTOR_UTIL_HPP_
10
11#include "Grid/grid_dist_key.hpp"
12#include "Space/Shape/HyperCube.hpp"
13#include "util/mul_array_extents.hpp"
14
15/*! \brief Copy scalar elements
16 *
17 * \tparam copy_type Type that should be copied
18 * \tparam T property id to copy
19 * \tparam Ev Type of source the Vector
20 * \tparam sa dimensionality of the array 0 is a scalar
21 *
22 */
23template<typename copy_type, typename T, typename Ev, typename Eqs_sys, int sa>
24struct copy_ele_sca_array
25{
26 /*! \brief Constructor
27 *
28 * It define the copy parameters.
29 *
30 * \param key destination position
31 * \param grid_dst grid destination
32 * \param x Source vector
33 * \param lin_id source element inside the vector
34 * \param gs_size grid size
35 * \param base_id processor id start
36 *
37 */
38 template<typename Grid> inline static void copy(Grid & grid_dst, const grid_dist_key_dx<Eqs_sys::dims> & key, const Ev & x,size_t lin_id, size_t base_id, size_t gs_size)
39 {
40 grid_dst.template get<T::value>(key) = x(lin_id * Eqs_sys::nvar + base_id);
41 }
42};
43
44/*! \brief Copy 1D array elements
45 *
46 * spacialization in case of 1D array
47 *
48 * \tparam copy_type Type that should be copied
49 * \tparam T property id to copy
50 * \tparam Ev Type of source the Vector
51 *
52 */
53template<typename copy_type, typename T, typename Ev, typename Eqs_sys>
54struct copy_ele_sca_array<copy_type,T,Ev,Eqs_sys,1>
55{
56 /*! \brief Constructor
57 *
58 * It define the copy parameters.
59 *
60 * \param key destination position
61 * \param grid_dst grid destination
62 * \param x Source vector
63 * \param lin_id source element inside the vector
64 * \param gs_size grid size
65 * \param base_id processor id start
66 *
67 */
68 template<typename Grid> inline static void copy(Grid & grid_dst, const grid_dist_key_dx<Eqs_sys::dims> & key, const Ev & x,size_t lin_id, size_t base_id, size_t gs_size)
69 {
70 for (size_t i = 0 ; i < std::extent<copy_type>::value ; i++)
71 {
72 grid_dst.template get<T::value>(key)[i] = x(lin_id * Eqs_sys::nvar + base_id + i);
73 }
74 }
75};
76
77
78/*! \brief this class is a functor for "for_each" algorithm
79 *
80 * This class is a functor for "for_each" algorithm. For each
81 * element of the boost::vector the operator() is called.
82 * Is mainly used to copy from the Vector to the grid target
83 * \note properties can be scalars or arrays of C++ primitives
84 *
85 * \tparam Eqs_sys System of equation information
86 * \tparam S type of destination grid
87 * \tparam Ev type of vector
88 *
89 */
90template<typename Eqs_sys, typename S, typename Ev>
91struct copy_ele
92{
93 //! destination grid element
94 const grid_dist_key_dx<Eqs_sys::dims> key;
95
96 //! destination grid
97 S & grid_dst;
98
99 //! source element inside the vector
100 size_t lin_id;
101
102 //! counter
103 size_t prp_id;
104
105 //! It is basically the number of grid points the vector has inside
106 size_t gs_size;
107
108 //! source vector
109 const Ev & x;
110
111 /*! \brief Constructor
112 *
113 * It define the copy parameters.
114 *
115 * \param key destination position
116 * \param grid_dst grid destination
117 * \param x Source vector
118 * \param lin_id source element inside the vector
119 * \param gs_size grid size
120 *
121 */
122 inline copy_ele(const grid_dist_key_dx<Eqs_sys::dims> & key, S & grid_dst, const Ev & x, size_t lin_id, size_t gs_size)
123 :key(key),grid_dst(grid_dst),lin_id(lin_id),prp_id(0),gs_size(gs_size),x(x){};
124
125
126#ifdef SE_CLASS1
127 /*! \brief Constructor
128 *
129 * Calling this constructor produce an error. This class store the reference of the object,
130 * this mean that the object passed must not be a temporal object
131 *
132 */
133 inline copy_ele(grid_dist_key_dx<Eqs_sys::dims> & key, S & grid_dst, Ev && x)
134 :key(key),grid_dst(grid_dst),x(x)
135 {std::cerr << "Error: " <<__FILE__ << ":" << __LINE__ << " Passing a temporal object";};
136#endif
137
138 /*! \brief It call the copy function for each property
139 *
140 * \param t property id
141 *
142 */
143 template<typename T>
144 inline void operator()(T& t)
145 {
146 // This is the type of the object we have to copy
147 typedef typename boost::mpl::at_c<typename S::value_type::type,T::value>::type copy_type;
148
149 copy_ele_sca_array<copy_type,T,Ev,Eqs_sys,std::is_array<copy_type>::value>::copy(grid_dst,key,x,lin_id,prp_id,gs_size);
150 prp_id += array_extents<copy_type>::mul();
151 }
152};
153
154
155#endif /* OPENFPM_NUMERICS_SRC_VECTOR_VECTOR_UTIL_HPP_ */
156