DOFVector.hpp 3.89 KB
Newer Older
1
2
3
4
5
6
7
8
9
#pragma once

#include <string>
#include <memory>

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

#include <dune/amdis/ClonablePtr.hpp>
#include <dune/amdis/Log.hpp>
10
#include <dune/amdis/linear_algebra/mtl/MTLDenseVector.hpp>
11
12
13

namespace AMDiS
{
14
  /// The basic container that stores a base vector and a corresponding feSpace
15
16
17
18
19
20
  template <class FeSpaceType, class ValueType = double>
  class DOFVector
  {
    using Self = DOFVector;
      
  public:
21
    /// The type of the \ref feSpace
22
    using FeSpace    = FeSpaceType;
23
24
    
    /// The type of the base vector
25
    using BaseVector = MTLDenseVector<ValueType>;
26
    
27
    /// The index/size - type
28
29
    using size_type  = typename FeSpace::size_type;
    
30
    /// The type of the elements of the DOFVector
31
    using value_type = ValueType;
32
33
    
    /// The underlying field-type (typically the same as \ref value_type)
34
35
36
37
38
39
40
41
42
43
44
    using field_type = typename BaseVector::value_type;
    
    /// Constructor. Constructs new BaseVector.
    DOFVector(FeSpace const& feSpace, std::string name)
      : feSpace(feSpace)
      , name(name)
      , vector(ClonablePtr<BaseVector>::make())
    {
      compress();
    }
    
45
    /// Constructor. Takes reference to existing BaseVector
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    DOFVector(FeSpace const& feSpace, std::string name, 
              BaseVector& vector_ref)
      : feSpace(feSpace)
      , name(name)
      , vector(vector_ref)
    {}
    
    /// Return the basis \ref feSpace of the vector
    FeSpace const& getFeSpace() const
    {
        return feSpace;
    }
    
    /// Return the data-vector \ref vector
    BaseVector const& getVector() const
    {
      return *vector;
    }
    
    /// Return the data-vector \ref vector
    BaseVector& getVector()
    {
      return *vector;
    }
    
    /// Return the size of the \ref feSpace
    size_type getSize() const
    {
      return feSpace.size();
    }
    
    /// Return the \ref name of this vector
    std::string getName() const
    {
      return name;
    }
    
    /// Resize the \ref vector to the size of the \ref feSpace.
    void compress()
    {
      vector->change_dim(getSize());
    }
    
    
    /// Access the entry \p i of the \ref vector with read-access.
    value_type const& operator[](size_type i) const
    {
93
94
      AMDIS_TEST_EXIT_DBG( i < num_rows(*vector) , 
        "Index " << i << " out of range [0," << num_rows(*vector) << ")" );
95
96
97
98
99
100
      return (*vector)[i];
    }
    
    /// Access the entry \p i of the \ref vector with write-access.
    value_type& operator[](size_type i)
    {
101
102
      AMDIS_TEST_EXIT_DBG( i < num_rows(*vector) , 
        "Index " << i << " out of range [0," << num_rows(*vector) << ")" );
103
104
105
      return (*vector)[i];
    }
    
106
    /// \brief interpolate a function \p f to the basis \ref feSpace and store the
107
108
109
110
    /// coefficients in \ref vector.
    template <class F>
    void interpol(F const& f)
    {
111
112
      // 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.
113
114
115
116
117
118
119
120
121
122
      Dune::Functions::interpolate(feSpace, *vector, f);
    }
    
    /// Calls the copy assignment operator of the BaseVector \ref vector
    void copy(Self const& that)
    {
      *vector = that.getVector();
    }
      
  private:
123
    /// The finite element space / basis associated with the data vector
124
    FeSpace const& feSpace;
125
126
    
    /// The name of the DOFVector (used in file-writing)
127
128
    std::string	name;
    
129
    /// The data-vector (can hold a new BaseVector or a pointer to external data
130
131
132
133
134
    ClonablePtr<BaseVector> vector;
      
    // friend class declarations
    template <class, class> friend class SystemVector;
  };
135
136
137
138
139
140
141
142
143
  
  
  /// Constructor a dofvector from given feSpace and name
  template <class FeSpaceType, class ValueType = double>
  DOFVector<FeSpaceType, ValueType>
  make_dofvector(FeSpaceType const& feSpace, std::string name)
  {
    return {feSpace, name};
  }
144
145

} // end namespace AMDiS