3#ifndef DUNE_PDELAB_BACKEND_EIGEN_MATRIX_HH
4#define DUNE_PDELAB_BACKEND_EIGEN_MATRIX_HH
12#include <dune/pdelab/backend/common/uncachedmatrixview.hh>
14#include <dune/pdelab/ordering/orderingbase.hh>
15#include <Eigen/Sparse>
25 struct MatrixPatternInserter
27 MatrixPatternInserter(M & mat)
31 template<
typename RI,
typename CI>
32 void add_link(
const RI& ri,
const CI& ci)
34 _matrix.coeffRef(ri.back(),ci.back()) = 0.0;
46 template<
typename GFSV,
typename GFSU,
typename ET,
int _Options>
48 :
public Backend::impl::Wrapper<::Eigen::SparseMatrix<ET,_Options>>
53 typedef ::Eigen::SparseMatrix<ET,_Options> Container;
57 friend Backend::impl::Wrapper<Container>;
61 typedef ET ElementType;
63 typedef ElementType field_type;
64 typedef typename Container::Index size_type;
65 typedef typename Container::Index index_type;
67 typedef GFSU TrialGridFunctionSpace;
68 typedef GFSV TestGridFunctionSpace;
70 typedef typename GFSV::Ordering::Traits::ContainerIndex RowIndex;
71 typedef typename GFSU::Ordering::Traits::ContainerIndex ColIndex;
73 template<
typename RowCache,
typename ColCache>
74 using LocalView = UncachedMatrixView<MatrixContainer,RowCache,ColCache>;
76 template<
typename RowCache,
typename ColCache>
77 using ConstLocalView = ConstUncachedMatrixView<const MatrixContainer,RowCache,ColCache>;
80 typename GFSV::Ordering::Traits::DOFIndex,
81 typename GFSV::Ordering::Traits::ContainerIndex
85 typename GFSU::Ordering::Traits::DOFIndex,
86 typename GFSU::Ordering::Traits::ContainerIndex
89 typedef MatrixPatternInserter<Container> Pattern;
92 MatrixContainer(
const GO& go)
93 : _container(
std::make_shared<Container>())
95 allocate_matrix(_container, go, ElementType(0));
99 MatrixContainer(
const GO& go,
const ElementType& e)
100 : _container(
std::make_shared<Container>())
102 allocate_matrix(_container, go, e);
106 explicit MatrixContainer(Backend::unattached_container = Backend::unattached_container())
110 explicit MatrixContainer(Backend::attached_container)
111 : _container(
std::make_shared<Container>())
114 MatrixContainer(
const MatrixContainer& rhs)
115 : _container(
std::make_shared<Container>(*(rhs._container)))
118 MatrixContainer& operator=(
const MatrixContainer& rhs)
128 _container = std::make_shared<Container>(rhs.base());
138 void attach(std::shared_ptr<Container> container)
140 _container = container;
143 bool attached()
const
145 return bool(_container);
148 const std::shared_ptr<Container>& storage()
const
155 return _container->rows();
160 return _container->cols();
163 MatrixContainer& operator=(
const ElementType& e)
165 if(!_container->isCompressed()) _container->makeCompressed();
166 for (
int i=0; i<_container->nonZeros(); i++)
167 _container->valuePtr()[i] = e;
173 MatrixContainer& operator*=(
const ElementType& e)
180 void mv(
const V& x, V& y)
const
182 y.base() = base() * x.base();
186 void usmv(
const ElementType alpha,
const V& x, V& y)
const
188 y.base() += alpha * (base() * x.base());
191 ElementType& operator()(
const RowIndex& ri,
const ColIndex& ci)
193 return _container->coeffRef(ri[0],ci[0]);
196 const ElementType operator()(
const RowIndex& ri,
const ColIndex& ci)
const
198 return _container->coeffRef(ri[0],ci[0]);
201 const Container& base()
const
213 const Container& native()
const
231 void clear_row(
const RowIndex& ri,
const ElementType& diagonal_entry)
233 _container->middleRows(ri[0],1) *= 0.0;
234 _container->coeffRef(ri[0],ri[0]) = diagonal_entry;
238 template<
typename GO>
239 static void allocate_matrix(std::shared_ptr<Container> & c,
const GO & go,
const ElementType& e)
242 int rows = go.testGridFunctionSpace().ordering().blockCount();
243 int cols = go.trialGridFunctionSpace().ordering().blockCount();
244 c->resize(rows,cols);
245 size_type nz = go.matrixBackend().avg_nz_per_row;
247 c->reserve(::Eigen::VectorXi::Constant(rows,nz));
250 go.fill_pattern(pattern);
255 std::shared_ptr< Container > _container;
Dune namespace.
Definition: alignedallocator.hh:13