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

3
#include <algorithm>
4
#include <memory>
5
#include <string>
6

7
#include <boost/numeric/mtl/vector/dense_vector.hpp>
8

9
10
11
#include <amdis/Output.hpp>
#include <amdis/common/ClonablePtr.hpp>
#include <amdis/common/ScalarTypes.hpp>
12
#include <amdis/linear_algebra/DOFVectorInterface.hpp>
13
#include <amdis/utility/MultiIndex.hpp>
14
15
16

namespace AMDiS
{
17
  /// The basic container that stores a base vector and a corresponding basis
18
  template <class GlobalBasis, class ValueType = double>
19
  class DOFVector
20
      : public DOFVectorInterface
21
22
  {
    using Self = DOFVector;
23

24
  public:
25
26
    /// The type of the \ref basis
    using Basis = GlobalBasis;
27

28
    /// The type of the base vector
29
    using BaseVector = mtl::vec::dense_vector<ValueType>;
30

31
    /// The index/size - type
32
    using size_type  = typename GlobalBasis::size_type;
33

34
    /// The type of the elements of the DOFVector
35
    using value_type = ValueType;
36

37
    /// Constructor. Constructs new BaseVector.
38
    DOFVector(GlobalBasis const& basis)
39
40
      : basis_(&basis)
      , vector_(ClonablePtr<BaseVector>::make())
41
42
43
    {
      compress();
    }
44

45
    /// Constructor. Takes reference to existing BaseVector
46
    DOFVector(GlobalBasis const& basis, BaseVector& vector)
47
      : basis_(&basis)
48
      , vector_(vector)
49
    {}
50

51
    /// Return the basis \ref basis_ of the vector
52
    Basis const& basis() const
53
    {
54
        return *basis_;
55
    }
56

57
    /// Return the data-vector \ref vector_
58
    BaseVector const& vector() const
59
    {
60
      return *vector_;
61
    }
62

63
    /// Return the data-vector \ref vector_
64
    BaseVector& vector()
65
    {
66
      return *vector_;
67
    }
68

69
70
    /// Return the size of the \ref basis
    size_type size() const
71
    {
72
      return basis_->dimension();
73
    }
74

75
    /// Resize the \ref vector to the size of the \ref basis.
76
    virtual void compress() override
77
    {
78
79
      if (num_rows(*vector_) != size()) {
        resize(size());
80
        *vector_ = value_type(0);
81
      }
82
    }
83

84
85
    template <class SizeInfo>
    void resize(SizeInfo s)
86
    {
87
      vector_->change_dim(size_type(s));
88
    }
89

90

91
92
    /// Access the entry \p i of the \ref vector with read-access.
    template <class Index>
93
    value_type const& operator[](Index idx) const
94
    {
95
96
      size_type i = flatMultiIndex(idx);
      test_exit_dbg(i < num_rows(*vector_),
97
98
        "Index ", i, " out of range [0,", num_rows(*vector_), ")" );
      return (*vector_)[i];
99
100
    }

101
102
    /// Access the entry \p i of the \ref vector with write-access.
    template <class Index>
103
    value_type& operator[](Index idx)
104
    {
105
106
      size_type i = flatMultiIndex(idx);
      test_exit_dbg(i < num_rows(*vector_),
107
108
        "Index ", i, " out of range [0,", num_rows(*vector_), ")" );
      return (*vector_)[i];
109
110
    }

111
112
113
114
115
116
117
118
    // /// 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;
    // }
119

120
121
    /// Sets each DOFVector to the scalar \p s.
    template <class Scalar>
122
    std::enable_if_t< Concepts::Arithmetic<Scalar>, Self&>
123
124
    operator=(Scalar s)
    {
125
      (*vector_) = s;
126
127
      return *this;
    }
128

129
130
131
    /// Calls the copy assignment operator of the BaseVector \ref vector
    void copy(Self const& that)
    {
132
      *vector_ = *that.vector_;
133
    }
134

135
  private:
136
    /// The finite element space / basis associated with the data vector
137
    Basis const* basis_;
138

139
    /// The data-vector (can hold a new BaseVector or a pointer to external data
140
    ClonablePtr<BaseVector> vector_;
141
  };
142

143
144
145
#if DUNE_HAVE_CXX_CLASS_TEMPLATE_ARGUMENT_DEDUCTION
  // Deduction rules
  template <class GlobalBasis>
146
  DOFVector(GlobalBasis const& basis)
147
148
    -> DOFVector<GlobalBasis, double>;

149
  template <class GlobalBasis, class ValueType>
150
  DOFVector(GlobalBasis const& basis, mtl::vec::dense_vector<ValueType>& coefficients)
151
    -> DOFVector<GlobalBasis, ValueType>;
152
153
154
#endif

  /// Constructor a dofvector from given basis and name
155
156
157
  template <class GlobalBasis, class ValueType = double>
  DOFVector<GlobalBasis, ValueType>
  makeDOFVector(GlobalBasis const& basis)
158
  {
159
    return {basis};
160
  }
161

162
  /// Constructor a dofvector from given basis, name, and coefficient vector
163
164
  template <class GlobalBasis, class ValueType>
  DOFVector<GlobalBasis, ValueType>
165
  makeDOFVector(GlobalBasis const& basis, mtl::vec::dense_vector<ValueType>& coefficients)
166
  {
167
    return {basis, coefficients};
168
169
  }

170
} // end namespace AMDiS