AMDiS  0.3
The Adaptive Multi-Dimensional Simulation Toolbox
VectorBackend.hpp
1 #pragma once
2 
3 #include <Eigen/Dense>
4 
5 #include <dune/common/ftraits.hh>
6 
7 #include <amdis/Output.hpp>
8 #include <amdis/algorithm/ForEach.hpp>
9 #include <amdis/algorithm/Transform.hpp>
10 #include <amdis/common/FakeContainer.hpp>
11 #include <amdis/linearalgebra/VectorBase.hpp>
12 #include <amdis/typetree/MultiIndex.hpp>
13 
14 namespace AMDiS
15 {
17  template <class T>
19  : public VectorBase<EigenVector<T>>
20  {
21  public:
23  using BaseVector = Eigen::Matrix<T, Eigen::Dynamic, 1>;
24 
26  using value_type = typename BaseVector::Scalar;
27 
29  using size_type = typename BaseVector::Index;
30 
31  private:
33  using block_type = value_type;
34 
36  using field_type = typename Dune::FieldTraits<value_type>::field_type;
37 
38  public:
40  template <class Basis>
41  explicit EigenVector(Basis const&) {}
42 
44  BaseVector const& vector() const
45  {
46  return vector_;
47  }
48 
51  {
52  return vector_;
53  }
54 
56  size_type size() const
57  {
58  return vector_.size();
59  }
60 
61 
63  template <class SizeInfo>
64  void init(SizeInfo const& size, bool clear)
65  {
66  vector_.resize(size_type(size));
67  if (clear)
68  vector_.setZero();
69  }
70 
71 
73  template <class MultiIndex>
74  value_type& at(MultiIndex const& idx)
75  {
76  const size_type i = flatMultiIndex(idx);
77  test_exit_dbg(i < size(), "Index {} out of range [0,{})", i, size());
78  return vector_.coeffRef(i);
79  }
80 
82  template <class MultiIndex>
83  value_type const& at(MultiIndex const& idx) const
84  {
85  const size_type i = flatMultiIndex(idx);
86  test_exit_dbg(i < size(), "Index {} out of range [0,{})", i, size());
87  return vector_.coeff(i);
88  }
89 
90  private:
92  BaseVector vector_;
93  };
94 
95 
96  namespace Recursive
97  {
98  template <class T>
99  struct ForEach<EigenVector<T>> : ForEach<VectorBase<EigenVector<T>>> {};
100 
101  template <class T>
102  struct Transform<EigenVector<T>> : Transform<VectorBase<EigenVector<T>>> {};
103 
104  template <class T>
105  struct InnerProduct<EigenVector<T>> : InnerProduct<VectorBase<EigenVector<T>>> {};
106 
107  } // end namespace Recursive
108 } // end namespace AMDiS
value_type const & at(MultiIndex const &idx) const
Access the entry i of the vector with read-access.
Definition: VectorBackend.hpp:83
constexpr bool MultiIndex
A multi-index type.
Definition: Concepts.hpp:150
typename BaseVector::Scalar value_type
The type of the elements of the DOFVector.
Definition: VectorBackend.hpp:26
BaseVector & vector()
Return the data-vector vector_.
Definition: VectorBackend.hpp:50
Eigen::Matrix< T, Eigen::Dynamic, 1 > BaseVector
The type of the base vector.
Definition: VectorBackend.hpp:23
value_type & at(MultiIndex const &idx)
Access the entry i of the vector with write-access.
Definition: VectorBackend.hpp:74
Definition: AdaptBase.hpp:6
EigenVector(Basis const &)
Constructor. Constructs new BaseVector.
Definition: VectorBackend.hpp:41
The basic container that stores a base vector and a corresponding basis.
Definition: VectorBackend.hpp:18
typename BaseVector::Index size_type
The index/size - type.
Definition: VectorBackend.hpp:29
void init(SizeInfo const &size, bool clear)
Resize the vector_ to the size s.
Definition: VectorBackend.hpp:64
CRTP base class for flat vector backends.
Definition: VectorBase.hpp:18
BaseVector const & vector() const
Return the data-vector vector_.
Definition: VectorBackend.hpp:44
size_type size() const
Return the current size of the vector_.
Definition: VectorBackend.hpp:56