DOFVector.hpp 7.75 KB
Newer Older
1
2
3
4
5
#pragma once

#include <memory>
#include <utility>

6
#include <dune/common/concept.hh>
7
8
#include <dune/common/shared_ptr.hh>

9
10
#include <dune/functions/functionspacebases/concepts.hh>

11
12
#include <amdis/DataTransfer.hpp>
#include <amdis/LinearAlgebra.hpp>
13
#include <amdis/Observer.hpp>
14
15
#include <amdis/common/Concepts.hpp>
#include <amdis/common/TypeTraits.hpp>
16
#include <amdis/gridfunctions/DiscreteFunction.hpp>
17
18
19
20
#include <amdis/typetree/TreePath.hpp>

namespace AMDiS
{
21
22
23
24
25
  // Forward declarations
  template <class PB>
  class ParallelGlobalBasis;


26
27
28
29
  /// \brief The basic container that stores a base vector and a corresponding basis
  /**
   * \tparam GB  Basis of the vector
   * \tparam T   Type of the coefficients
Praetorius, Simon's avatar
Praetorius, Simon committed
30
   * \tparam TraitsType  Collection of parameter for the linear-algebra backend
31
   **/
Praetorius, Simon's avatar
Praetorius, Simon committed
32
  template <class GB, class T = double, class TraitsType = BackendTraits<GB>>
33
  class DOFVector
Praetorius, Simon's avatar
Praetorius, Simon committed
34
      : public VectorFacade<T, TraitsType::template VectorImpl>
Praetorius, Simon's avatar
Praetorius, Simon committed
35
36
37
      , private Observer<event::preAdapt>
      , private Observer<event::adapt>
      , private Observer<event::postAdapt>
38
39
  {
    using Self = DOFVector;
Praetorius, Simon's avatar
Praetorius, Simon committed
40
    using Coefficients = VectorFacade<T, TraitsType::template VectorImpl>;
41
42
43
44
45
46
47
48

  public:
    using GlobalBasis = GB;

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

    /// The type of the elements of the DOFVector
Praetorius, Simon's avatar
Praetorius, Simon committed
49
    using value_type = T;
50

51
52
    /// Wrapper for the implementation of the transfer of data during grid adaption
    using DataTransfer = DataTransferWrapper<Self>;
53

Praetorius, Simon's avatar
Praetorius, Simon committed
54
55
56
    /// Collection of parameter for the linear-algebra backend
    using Traits = TraitsType;

57
  public:
58
    /// (1) Constructor. Stores the shared_ptr of the basis and creates a new DataTransfer.
Praetorius, Simon's avatar
Praetorius, Simon committed
59
    DOFVector(std::shared_ptr<GB> const& basis,
60
              DataTransferOperation op = DataTransferOperation::INTERPOLATE)
Praetorius, Simon's avatar
Praetorius, Simon committed
61
      : Coefficients(*basis)
Praetorius, Simon's avatar
Praetorius, Simon committed
62
      , Observer<event::preAdapt>(basis->gridView().grid())
63
      , Observer<event::adapt>(*basis)
Praetorius, Simon's avatar
Praetorius, Simon committed
64
      , Observer<event::postAdapt>(basis->gridView().grid())
65
66
      , dataTransfer_(op, basis)
      , basis_(basis)
67
68
    {}

69
70
    /// (2) Constructor. Forwards to (1) by wrapping reference into non-destroying
    /// shared_ptr, see \ref Dune::wrap_or_move.
71
72
73
74
75
76
    template <class GB_,
      REQUIRES(Concepts::Similar<GB_,GB>)>
    DOFVector(GB_&& basis, DataTransferOperation op = DataTransferOperation::INTERPOLATE)
      : DOFVector(Dune::wrap_or_move(FWD(basis)), op)
    {}

77
78
79
    /// (3) Constructor. Forwards to (1) by creating a new basis from a dune-functions basis.
    template <class GB_,
      REQUIRES(not Concepts::Similar<GB_,GB>),
80
      REQUIRES(Concepts::GlobalBasis<GB_>)>
81
82
83
84
    DOFVector(GB_&& basis, DataTransferOperation op = DataTransferOperation::INTERPOLATE)
      : DOFVector(std::make_shared<GB>(std::move(basis)), op)
    {}

Praetorius, Simon's avatar
Praetorius, Simon committed
85
    std::shared_ptr<GlobalBasis const> const& basis() const { return basis_; }
86
87
88
89
90

    template <class TreePath = RootTreePath>
    auto child(TreePath const& path = {})
    {
      auto&& tp = makeTreePath(path);
Praetorius, Simon's avatar
Praetorius, Simon committed
91
      return makeDiscreteFunction(coefficients(), *basis_, tp);
92
93
94
95
96
97
    }

    template <class TreePath = RootTreePath>
    auto child(TreePath const& path = {}) const
    {
      auto&& tp = makeTreePath(path);
Praetorius, Simon's avatar
Praetorius, Simon committed
98
      return makeDiscreteFunction(coefficients(), *basis_, tp);
99
100
101
102
103
    }


    /// Interpolation of GridFunction to DOFVector, assuming that there is no
    /// reference to this DOFVector in the expression.
104
    /// See \ref DiscreteFunction::interpolate_noalias
105
    template <class Expr, class Tag = tag::average>
Praetorius, Simon's avatar
Praetorius, Simon committed
106
    void interpolate_noalias(Expr&& expr, Tag strategy = {})
107
108
109
110
111
    {
      child().interpolate_noalias(FWD(expr), strategy);
    }

    /// Interpolation of GridFunction to DOFVector.
112
    /// See \ref DiscreteFunction::interpolate
113
    template <class Expr, class Tag = tag::average>
Praetorius, Simon's avatar
Praetorius, Simon committed
114
    void interpolate(Expr&& expr, Tag strategy = {})
115
116
117
118
119
    {
      child().interpolate(FWD(expr), strategy);
    }

    /// Interpolation of GridFunction to DOFVector.
120
    /// See \ref DiscreteFunction::interpolate
121
122
123
124
125
126
127
128
    template <class Expr>
    DOFVector& operator<<(Expr&& expr)
    {
      child().interpolate(FWD(expr));
      return *this;
    }


129
    Coefficients const& coefficients() const
Praetorius, Simon's avatar
Praetorius, Simon committed
130
131
132
133
    {
      return static_cast<Coefficients const&>(*this);
    }

134
    Coefficients& coefficients()
Praetorius, Simon's avatar
Praetorius, Simon committed
135
136
137
138
    {
      return static_cast<Coefficients&>(*this);
    }

139
140
141
142
143
144
    /// Write DOFVector to file
    void backup(std::string const& filename);

    /// Read backup data from file
    void restore(std::string const& filename);

145
146
147
148
149
150
151
152
153
154
155
156
157
    void resize()
    {
      Coefficients::resize(sizeInfo(*basis_));
    }

    using Coefficients::resize;

    void resizeZero()
    {
      Coefficients::resizeZero(sizeInfo(*basis_));
    }

    using Coefficients::resizeZero;
158
159

    /// Return the associated DataTransfer object
160
    DataTransfer const& dataTransfer() const
161
162
163
164
165
    {
      return dataTransfer_;
    }

    /// Return the associated DataTransfer object
166
    DataTransfer& dataTransfer()
167
168
169
170
171
    {
      return dataTransfer_;
    }

    /// Assign the DataTransfer object
172
    void setDataTransfer(DataTransfer const& dataTransfer)
173
174
175
176
    {
      dataTransfer_ = dataTransfer;
    }

177
178
179
180
    void setDataTransfer(DataTransfer&& dataTransfer)
    {
      dataTransfer_ = std::move(dataTransfer);
    }
181

182
183
    /// Create a new DataTransfer object based on the operation type
    void setDataTransfer(DataTransferOperation op)
184
    {
185
      setDataTransfer(DataTransfer(op, basis_));
186
187
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
188
189
  protected:

190
191
    /// Override of Observer update(event::preAdapt) method. Redirects to preAdapt method of the
    /// \ref DataTransfer object.
Praetorius, Simon's avatar
Praetorius, Simon committed
192
    void updateImpl(event::preAdapt e) override
193
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
194
      dataTransfer_.preAdapt(*this, e.value);
195
196
    }

197
198
    /// Override of Observer update(event::adapt) method. Redirects to adapt method of the
    /// \ref DataTransfer object.
Praetorius, Simon's avatar
Praetorius, Simon committed
199
    void updateImpl(event::adapt e) override
200
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
201
      assert(e.value);
202
      resize();
203
      dataTransfer_.adapt(*this);
204
205
    }

206
207
    /// Override of Observer update(event::postAdapt) method. Redirects to postAdapt method of the
    /// \ref DataTransfer object.
Praetorius, Simon's avatar
Praetorius, Simon committed
208
    void updateImpl(event::postAdapt) override
209
    {
210
      dataTransfer_.postAdapt(*this);
211
212
213
214
215
    }

  private:
    /// Data interpolation when the grid changes, set by default
    /// to \ref DataTransferOperation::INTERPOLATE.
216
217
    DataTransfer dataTransfer_;

Praetorius, Simon's avatar
Praetorius, Simon committed
218
    std::shared_ptr<GlobalBasis const> basis_;
219
220
  };

221
222

  // deduction guides
223
224
  template <class GB>
  DOFVector(GB&& basis, DataTransferOperation op = DataTransferOperation::INTERPOLATE)
225
    -> DOFVector<ParallelGlobalBasis<typename Underlying_t<GB>::PreBasis>>;
226

227
228
229
230
231
232
233
234
235
236
237

  /// \brief Create a DOFVector from a basis.
  /**
   * This generator function accepts the basis as reference, temporary, or
   * shared_ptr. Internally the reference is wrapped into a non-destroying
   * shared_ptr and the temporary is moved into a new shared_ptr.
   *
   * The DataTransferOperation controls what is done during grid changes with the
   * DOFVector. The default is interpolation of the data to the new grid. See
   * \ref DataTransferOperation for more options.
   **/
238
239
240
  template <class ValueType = double, class GB>
  DOFVector<ParallelGlobalBasis<typename Underlying_t<GB>::PreBasis>, ValueType>
  makeDOFVector(GB&& basis, DataTransferOperation op = DataTransferOperation::INTERPOLATE)
241
242
243
244
  {
    return {FWD(basis), op};
  }

Praetorius, Simon's avatar
Praetorius, Simon committed
245
246
247
248
249
250
251
252
253
254
255
256
257
258
  /// A Generator for a mutable \ref DiscreteFunction
  template <class GB, class T, class Path = RootTreePath>
  auto makeDiscreteFunction(DOFVector<GB,T>& dofVec, Path const& path = {})
  {
    return dofVec.child(path);
  }

  /// A Generator for a mutable \ref DiscreteFunction
  template <class GB, class T, class Path = RootTreePath>
  auto makeDiscreteFunction(DOFVector<GB,T> const& dofVec, Path const& path = {})
  {
    return dofVec.child(path);
  }

259
260
261
} // end namespace AMDiS

#include <amdis/DOFVector.inc.hpp>