DOFVector.hpp 3.67 KB
Newer Older
1
2
#pragma once

3
#include <boost/numeric/mtl/vector/dense_vector.hpp>
4

5
6
7
#include <amdis/Output.hpp>
#include <amdis/common/ClonablePtr.hpp>
#include <amdis/common/ScalarTypes.hpp>
8
#include <amdis/linear_algebra/DOFVectorBase.hpp>
9
#include <amdis/utility/MultiIndex.hpp>
10
11
12

namespace AMDiS
{
13
  /// The basic container that stores a base vector and a corresponding basis
14
  template <class BasisType, class ValueType = double>
15
  class DOFVector
16
      : public DOFVectorBase<BasisType>
17
18
  {
    using Self = DOFVector;
19
    using Super = DOFVectorBase<BasisType>;
20

21
  public:
22
    /// The type of the \ref basis
23
    using Basis = BasisType;
24

25
    /// The type of the base vector
26
    using BaseVector = mtl::vec::dense_vector<ValueType>;
27

28
    /// The index/size - type
29
    using size_type  = typename BasisType::size_type;
30

31
    /// The type of the elements of the DOFVector
32
    using value_type = ValueType;
33

34
    /// Constructor. Constructs new BaseVector.
35
36
    DOFVector(BasisType const& basis)
      : Super(basis)
37
      , vector_(ClonablePtr<BaseVector>::make())
38
39
40
    {
      compress();
    }
41

42
    /// Constructor. Takes reference to existing BaseVector
43
44
    DOFVector(BasisType const& basis, BaseVector& vector)
      : Super(basis)
45
      , vector_(vector)
46
    {}
47

48
    /// Return the data-vector \ref vector_
49
    BaseVector const& vector() const
50
    {
51
      return *vector_;
52
    }
53

54
    /// Return the data-vector \ref vector_
55
    BaseVector& vector()
56
    {
57
      return *vector_;
58
    }
59

60
    /// Resize the \ref vector to the size of the \ref basis.
61
    virtual void compress() override
62
    {
63
64
      if (mtl::vec::size(*vector_) != this->size()) {
        resize(this->size());
65
        *vector_ = value_type(0);
66
      }
67
    }
68

69
70
    template <class SizeInfo>
    void resize(SizeInfo s)
71
    {
72
      vector_->change_dim(size_type(s));
73
    }
74

75

76
77
    /// Access the entry \p i of the \ref vector with read-access.
    template <class Index>
78
    value_type const& operator[](Index idx) const
79
    {
80
      size_type i = flatMultiIndex(idx);
81
82
      test_exit_dbg(i < mtl::vec::size(*vector_),
        "Index ", i, " out of range [0,", mtl::vec::size(*vector_), ")" );
83
      return (*vector_)[i];
84
85
    }

86
87
    /// Access the entry \p i of the \ref vector with write-access.
    template <class Index>
88
    value_type& operator[](Index idx)
89
    {
90
      size_type i = flatMultiIndex(idx);
91
92
      test_exit_dbg(i < mtl::vec::size(*vector_),
        "Index ", i, " out of range [0,", mtl::vec::size(*vector_), ")" );
93
      return (*vector_)[i];
94
95
    }

96
97
98
99
100
101
102
103
    // /// Scale each DOFVector by the factor \p s.
    // template <class Scalar>
    // std::enable_if_t< Concepts::Arithmetic<Scalar>, Self&>
    // operator*=(Scalar s)
    // {
    //   (*vector_) *= s;
    //   return *this;
    // }
104

105
106
    /// Sets each DOFVector to the scalar \p s.
    template <class Scalar>
107
    std::enable_if_t< Concepts::Arithmetic<Scalar>, Self&>
108
109
    operator=(Scalar s)
    {
110
      (*vector_) = s;
111
112
      return *this;
    }
113

114
115
116
    /// Calls the copy assignment operator of the BaseVector \ref vector
    void copy(Self const& that)
    {
117
      *vector_ = *that.vector_;
118
    }
119

120
  private:
121
    /// The data-vector (can hold a new BaseVector or a pointer to external data
122
    ClonablePtr<BaseVector> vector_;
123
  };
124

125
  /// Constructor a dofvector from given basis and name
126
127
128
  template <class Basis, class ValueType = double>
  DOFVector<Basis, ValueType>
  makeDOFVector(Basis const& basis)
129
  {
130
    return {basis};
131
  }
132

133
  /// Constructor a dofvector from given basis, name, and coefficient vector
134
135
136
  template <class Basis, class ValueType>
  DOFVector<Basis, ValueType>
  makeDOFVector(Basis const& basis, mtl::vec::dense_vector<ValueType>& coefficients)
137
  {
138
    return {basis, coefficients};
139
140
  }

141
} // end namespace AMDiS