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

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

7
8
#include <dune/common/reservedvector.hh>
#include <dune/functions/functionspacebases/flatmultiindex.hh>
9
10
#include <dune/functions/functionspacebases/interpolate.hh>

11
12
13
#include <amdis/Output.hpp>
#include <amdis/common/ClonablePtr.hpp>
#include <amdis/common/ScalarTypes.hpp>
14
#include <amdis/linear_algebra/DOFVectorBase.hpp>
15
16
#include <amdis/linear_algebra/mtl/MTLDenseVector.hpp>
#include <amdis/utility/MultiIndex.hpp>
17
18
19

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

27
  public:
28
29
    /// The type of the \ref basis
    using Basis = GlobalBasis;
30

31
    /// The type of the base vector
32
    using BaseVector = MTLDenseVector<ValueType>;
33

34
    /// The index/size - type
35
    using size_type  = typename GlobalBasis::size_type;
36

37
    /// The type of the elements of the DOFVector
38
    using value_type = ValueType;
39

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

48
    /// Constructor. Takes reference to existing BaseVector
49
    DOFVector(GlobalBasis const& basis, BaseVector& vector_ref)
50
51
      : basis_(&basis)
      , vector_(vector_ref)
52
    {}
53

54
55
    /// Return the basis \ref basis of the vector
    Basis const& basis() const
56
    {
57
        return *basis_;
58
    }
59

60
    /// Return the data-vector \ref vector
61
    BaseVector const& vector() const
62
    {
63
      return *vector_;
64
    }
65

66
    /// Return the data-vector \ref vector
67
    BaseVector& vector()
68
    {
69
      return *vector_;
70
    }
71

72
73
    /// Return the size of the \ref basis
    size_type size() const
74
    {
75
      return basis_->dimension();
76
    }
77

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

87
88
    template <class SizeInfo>
    void resize(SizeInfo s)
89
    {
90
      vector_->change_dim(size_type(s));
91
    }
92

93

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

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

114
115
    /// Scale each DOFVector by the factor \p s.
    template <class Scalar>
116
    std::enable_if_t< Concepts::Arithmetic<Scalar>, Self&>
117
118
    operator*=(Scalar s)
    {
119
      (*vector_) *= s;
120
121
      return *this;
    }
122

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

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

138
  private:
139
    /// The finite element space / basis associated with the data vector
140
    Basis const* basis_;
141

142
    /// The data-vector (can hold a new BaseVector or a pointer to external data
143
    ClonablePtr<BaseVector> vector_;
144
  };
145

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

152
153
154
  template <class GlobalBasis, class ValueType>
  DOFVector(GlobalBasis const& basis, MTLDenseVector<ValueType>& coefficients)
    -> DOFVector<GlobalBasis, ValueType>;
155
156
157
#endif

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

165
  /// Constructor a dofvector from given basis, name, and coefficient vector
166
167
168
  template <class GlobalBasis, class ValueType>
  DOFVector<GlobalBasis, ValueType>
  makeDOFVector(GlobalBasis const& basis, MTLDenseVector<ValueType>& coefficients)
169
  {
170
    return {basis, coefficients};
171
172
  }

173
} // end namespace AMDiS