DOFVector.hpp 4.57 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
14
15
#include <amdis/Output.hpp>
#include <amdis/common/ClonablePtr.hpp>
#include <amdis/common/ScalarTypes.hpp>
#include <amdis/linear_algebra/mtl/MTLDenseVector.hpp>
#include <amdis/utility/MultiIndex.hpp>
16
17
18

namespace AMDiS
{
19
  /// The basic container that stores a base vector and a corresponding feSpace
20
  template <class Traits, class ValueType = double>
21
22
23
  class DOFVector
  {
    using Self = DOFVector;
24

25
  public:
26
    /// The type of the \ref feSpace
27
    using GlobalBasis = typename Traits::GlobalBasis;
28

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

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

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

38
    /// The underlying field-type (typically the same as \ref value_type)
39
    using field_type = typename BaseVector::value_type;
40

41
    /// Constructor. Constructs new BaseVector.
42
    DOFVector(GlobalBasis const& feSpace, std::string name)
43
44
45
46
47
      : feSpace(feSpace)
      , name(name)
      , vector(ClonablePtr<BaseVector>::make())
    {
      compress();
48
      *vector = ValueType(0);
49
    }
50

51
    /// Constructor. Takes reference to existing BaseVector
52
    DOFVector(GlobalBasis const& feSpace, std::string name,
53
54
55
56
57
              BaseVector& vector_ref)
      : feSpace(feSpace)
      , name(name)
      , vector(vector_ref)
    {}
58

59
    /// Return the basis \ref feSpace of the vector
60
    GlobalBasis const& getFeSpace() const
61
62
63
    {
        return feSpace;
    }
64

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

71
72
73
74
75
    /// Return the data-vector \ref vector
    BaseVector& getVector()
    {
      return *vector;
    }
76

77
78
79
    /// Return the size of the \ref feSpace
    size_type getSize() const
    {
80
      return feSpace.dimension();
81
    }
82

83
84
85
86
87
    /// Return the \ref name of this vector
    std::string getName() const
    {
      return name;
    }
88

89
90
91
    /// Resize the \ref vector to the size of the \ref feSpace.
    void compress()
    {
92
      if (num_rows(*vector) != getSize())
93
        resize(getSize());
94
    }
95

96
97
    template <class SizeInfo>
    void resize(SizeInfo s)
98
    {
99
      vector->change_dim(size_type(s));
100
    }
101

102

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

113
114
115
    /// Access the entry \p i of the \ref vector with write-access.
    template <class Index>
    value_type& operator[](Index idx)
116
    {
117
      auto const i = flatMultiIndex(idx);
118
119
120
121
122
      test_exit_dbg( i < num_rows(*vector) ,
        "Index ", i, " out of range [0,", num_rows(*vector), ")" );
      return (*vector)[i];
    }

123
    /// \brief interpolate a function \p f to the basis \ref feSpace and store the
124
125
    /// coefficients in \ref vector.
    template <class F>
126
    void interpol(F&& f)
127
    {
128
      Dune::Functions::interpolate(feSpace, *this, std::forward<F>(f));
129
    }
130

131
132
    /// Scale each DOFVector by the factor \p s.
    template <class Scalar>
133
    std::enable_if_t< Concepts::Arithmetic<Scalar>, Self&>
134
135
136
137
138
    operator*=(Scalar s)
    {
      (*vector) *= s;
      return *this;
    }
139

140
141
    /// Sets each DOFVector to the scalar \p s.
    template <class Scalar>
142
    std::enable_if_t< Concepts::Arithmetic<Scalar>, Self&>
143
144
145
146
147
    operator=(Scalar s)
    {
      (*vector) = s;
      return *this;
    }
148

149
150
151
152
153
    /// Calls the copy assignment operator of the BaseVector \ref vector
    void copy(Self const& that)
    {
      *vector = that.getVector();
    }
154

155
  private:
156
    /// The finite element space / basis associated with the data vector
157
    GlobalBasis const& feSpace;
158

159
    /// The name of the DOFVector (used in file-writing)
160
    std::string	name;
161

162
    /// The data-vector (can hold a new BaseVector or a pointer to external data
163
    ClonablePtr<BaseVector> vector;
164

165
166
167
    // friend class declarations
    template <class, class> friend class SystemVector;
  };
168
169


170
  /// Constructor a dofvector from given feSpace and name
171
172
173
  template <class Traits, class ValueType = double>
  DOFVector<Traits, ValueType>
  makeDOFVector(typename Traits::GlobalBasis const& basis, std::string name)
174
  {
175
    return {basis, name};
176
  }
177
178

} // end namespace AMDiS