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

3
4
5
6
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

7
8
9
10
11
#include <string>
#include <memory>

#include <dune/functions/functionspacebases/interpolate.hh>

12
#include <dune/amdis/Output.hpp>
13
14
#include <dune/amdis/common/ClonablePtr.hpp>
#include <dune/amdis/common/ScalarTypes.hpp>
15
#include <dune/amdis/linear_algebra/mtl/MTLDenseVector.hpp>
16
17
18

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

25
  public:
26
    /// The type of the \ref feSpace
27
    using FeSpace    = FeSpaceType;
28

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

32
    /// The index/size - type
33
    using size_type  = typename FeSpace::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
42
43
44
45
46
47
48
    /// Constructor. Constructs new BaseVector.
    DOFVector(FeSpace const& feSpace, std::string name)
      : feSpace(feSpace)
      , name(name)
      , vector(ClonablePtr<BaseVector>::make())
    {
      compress();
    }
49

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

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

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

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

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

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

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


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

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

112
    /// \brief interpolate a function \p f to the basis \ref feSpace and store the
113
114
115
116
    /// coefficients in \ref vector.
    template <class F>
    void interpol(F const& f)
    {
117
118
      // NOTE: This needs a change in mtl dense_vector implementation, i.e. we
      // have to add an alias for the function change_dim with the name resize.
119
      Dune::Functions::interpolate(feSpace, wrapper(*vector), f);
120
    }
121

122
123
    /// Scale each DOFVector by the factor \p s.
    template <class Scalar>
124
    std::enable_if_t< Concepts::Arithmetic<Scalar>, Self&>
125
126
127
128
129
    operator*=(Scalar s)
    {
      (*vector) *= s;
      return *this;
    }
130

131
132
    /// Sets each DOFVector to the scalar \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
142
143
144
    /// Calls the copy assignment operator of the BaseVector \ref vector
    void copy(Self const& that)
    {
      *vector = that.getVector();
    }
145

146
  private:
147
    /// The finite element space / basis associated with the data vector
148
    FeSpace const& feSpace;
149

150
    /// The name of the DOFVector (used in file-writing)
151
    std::string	name;
152

153
    /// The data-vector (can hold a new BaseVector or a pointer to external data
154
    ClonablePtr<BaseVector> vector;
155

156
157
158
    // friend class declarations
    template <class, class> friend class SystemVector;
  };
159
160


161
162
163
  /// Constructor a dofvector from given feSpace and name
  template <class FeSpaceType, class ValueType = double>
  DOFVector<FeSpaceType, ValueType>
164
  makeDOFVector(FeSpaceType const& feSpace, std::string name)
165
166
167
  {
    return {feSpace, name};
  }
168
169

} // end namespace AMDiS