Vector_eigen.hpp 4.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
/*
 * Vector_eigen.hpp
 *
 *  Created on: Nov 27, 2015
 *      Author: i-bird
 */

#ifndef OPENFPM_NUMERICS_SRC_VECTOR_VECTOR_EIGEN_HPP_
#define OPENFPM_NUMERICS_SRC_VECTOR_VECTOR_EIGEN_HPP_

/*!	\brief Copy scalar element
 *
 *
 *
 */
template<typename copy_type, typename T, typename Eqs_sys, bool sa>
struct copy_ele_sca_array
{
	template<typename Grid> inline void copy(Grid & grid_dst,size_t lin_id, size_t base_id, size_t gs_size)
	{
		if (Eqs_sys::ord == EQS_FIELD)
			grid_dst.template get<T::value>(key) = x(lin_id * Eqs_sys::nvar + base_id);
		else
			grid_dst.template get<T::value>(key) = x(base_id * gs_size + lin_id);
	}
};


/*! \brief Copy array element
 *
 *
 *
 */
template<typename copy_type, typename T, typename Eqs_sys>
struct copy_ele_sca_array<copy_type,T,Eqs_sys,true>
{
	template<typename Grid> inline void copy(Grid & grid_dst,size_t lin_id, size_t base_id, size_t gs_size)
	{
		for (size_t i = 0 ; i < std::extents<copy_type>::value ; i++)
		{
			if ()
				grid_dst.template get<T::value>(key)[i] = x(lin_id * Eqs_sys::nvar + base_id + i);
			else
				grid_dst.template get<T::value>(key)[i] = x(base_id * gs_size + lin_id);
		}
	}
};

/*! \brief this class is a functor for "for_each" algorithm
 *
 * This class is a functor for "for_each" algorithm. For each
 * element of the boost::vector the operator() is called.
 * Is mainly used to copy from the Vector_eigen to the grid target
 * in a generic way for a generic object T with variable number of properties
 *
 * \tparam dim Dimensionality
 * \tparam S type of grid
 * \tparam Ev Eigen vector type
 *
 */

template<unsigned int dim, typename S, typename Ev>
struct copy_ele
{
	//! where to copy the object in the grid
	const grid_dist_key_dx<dim> key;

	//! destination grid
	S & grid_dst;

	//! source Eigen vector
	const Ev & x;

	/*! \brief constructor
	 *
	 * It define the copy parameters.
	 *
	 * \param key which element we are modifying
	 * \param grid_dst grid we are updating
	 * \param v Source Eigen vector
	 *
	 */
	inline copy_ele(const grid_dist_key_dx<dim> & key, S & grid_dst, const Ev & x)
	:key(key),grid_dst(grid_dst),x(x){};


#ifdef SE_CLASS1
	/*! \brief Constructor
	 *
	 * Calling this constructor produce an error. This class store the reference of the object,
	 * this mean that the object passed must not be a temporal object
	 *
	 */
	inline copy_ele(grid_dist_key_dx<dim> & key, S & grid_dst, Ev && x)
	:key(key),grid_dst(grid_dst),x(x)
	{std::cerr << "Error: " <<__FILE__ << ":" << __LINE__ << " Passing a temporal object";};
#endif

	//! It call the copy function for each property
	template<typename T>
	inline void operator()(T& t) const
	{
		// This is the type of the object we have to copy
		typedef typename boost::mpl::at_c<S::value_type::type,T::value>::type copy_type;

/*		for (size_t i = 0 ; i < std::extents<copy_type>::value ; i++)
		{
			if ()
				grid_dst.template get<T::value>(key)[i] = x(lin_id * eqs_sys::nvar + base_id + i);
			else
				grid_dst.template get<T::value>(key)[i] = x(lin_id * eqs_sys::nvar + base_id + i);
		}*/
	}
};



template<typename T>
class Vector<T,Eigen::Matrix<T, Eigen::Dynamic, 1>>
{
	Eigen::Matrix<T, Eigen::Dynamic, 1> v;

public:

	/*! \brief Resize the Vector
	 *
	 * \param row numbers of row
	 *
	 */
	void resize(size_t row)
	{
		v.resize(row);
	}

	/*! \brief Return a reference to the vector element
	 *
	 * \param i element
	 *
	 * \return reference to the element vector
	 *
	 */
	T & get(size_t i)
	{
		return v(i);
	}

	/*! \brief Get the Eigen Vector object
	 *
	 * \return the Eigen Vector
	 *
	 */
	const Eigen::Matrix<T, Eigen::Dynamic, 1> & getVec() const
	{
		return v;
	}

	/*! \brief Get the Eigen Vector object
	 *
	 * \return the Eigen Vector
	 *
	 */
	Eigen::Matrix<T, Eigen::Dynamic, 1> & getVec()
	{
		return v;
	}

	/*! \brief Copy the vector into the grid
	 *
	 *
	 */
	template<typename Eqs_sys,typename Grid ,unsigned int ... pos> void copy(Grid & g, const grid_dist_iterator_sub<Eqs_sys::dims,typename Grid::d_grid> & g_i)
	{
		auto g_it = g_i;

		while (g_it.isNext() == true)
		{
			typedef typename to_boost_vmpl<pos...>::type vid;
			typedef boost::mpl::size<vid> v_size;

			copy_ele<Eqs_sys::dims,Grid,Eigen::Matrix<T, Eigen::Dynamic, 1>> cp(g_it.get(),g,v);

			boost::mpl::for_each_ref<boost::mpl::range_c<int,0,v_size::value>>(cp);

			++g_it;
		}
	}
};


#endif /* OPENFPM_NUMERICS_SRC_VECTOR_VECTOR_EIGEN_HPP_ */