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

#include <memory>
#include <utility>

#include <dune/common/shared_ptr.hh>

#include <amdis/DataTransfer.hpp>
#include <amdis/LinearAlgebra.hpp>
10
#include <amdis/Observer.hpp>
11
12
#include <amdis/common/Concepts.hpp>
#include <amdis/common/TypeTraits.hpp>
13
#include <amdis/gridfunctions/DiscreteFunction.hpp>
14
15
#include <amdis/typetree/TreePath.hpp>

16
17
18
19
20
21
22
23
24
namespace Dune
{
  namespace Functions
  {
    template <class PB>
    class DefaultGlobalBasis;
  }
}

25
26
namespace AMDiS
{
27
28
29
30
31
  // Forward declarations
  template <class PB>
  class ParallelGlobalBasis;


32
33
34
35
  /// \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
36
   * \tparam TraitsType  Collection of parameter for the linear-algebra backend
37
   **/
Praetorius, Simon's avatar
Praetorius, Simon committed
38
  template <class GB, class T = double, class TraitsType = BackendTraits<GB>>
39
  class DOFVector
Praetorius, Simon's avatar
Praetorius, Simon committed
40
      : public VectorFacade<T, TraitsType::template VectorImpl>
Praetorius, Simon's avatar
Praetorius, Simon committed
41
42
43
      , private Observer<event::preAdapt>
      , private Observer<event::adapt>
      , private Observer<event::postAdapt>
44
45
  {
    using Self = DOFVector;
Praetorius, Simon's avatar
Praetorius, Simon committed
46
    using Coefficients = VectorFacade<T, TraitsType::template VectorImpl>;
47
48
49
50
51
52
53
54

  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
55
    using value_type = T;
56

57
58
    /// Wrapper for the implementation of the transfer of data during grid adaption
    using DataTransfer = DataTransferWrapper<Self>;
59

Praetorius, Simon's avatar
Praetorius, Simon committed
60
61
62
    /// Collection of parameter for the linear-algebra backend
    using Traits = TraitsType;

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

75
76
77
78
79
80
    /// Constructor creating a new basis from a Dune::Functions::DefaultGlobalBasis.
    // TODO(FM): Replace explicit type with concept check
    DOFVector(Dune::Functions::DefaultGlobalBasis<typename GB::PreBasis>&& basis,
              DataTransferOperation op = DataTransferOperation::INTERPOLATE)
      : DOFVector(std::make_shared<GB>(std::move(basis)), op)
    {}
81
82
83
84
85
86
87

    template <class GB_,
      REQUIRES(Concepts::Similar<GB_,GB>)>
    DOFVector(GB_&& basis, DataTransferOperation op = DataTransferOperation::INTERPOLATE)
      : DOFVector(Dune::wrap_or_move(FWD(basis)), op)
    {}

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

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

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


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

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

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


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

137
    Coefficients& coefficients()
Praetorius, Simon's avatar
Praetorius, Simon committed
138
139
140
141
    {
      return static_cast<Coefficients&>(*this);
    }

142
143
144
145
146
147
    /// Write DOFVector to file
    void backup(std::string const& filename);

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

148
149
150
151
152
153
154
155
156
157
158
159
160
    void resize()
    {
      Coefficients::resize(sizeInfo(*basis_));
    }

    using Coefficients::resize;

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

    using Coefficients::resizeZero;
161
162

    /// Return the associated DataTransfer object
163
    DataTransfer const& dataTransfer() const
164
165
166
167
168
    {
      return dataTransfer_;
    }

    /// Return the associated DataTransfer object
169
    DataTransfer& dataTransfer()
170
171
172
173
174
    {
      return dataTransfer_;
    }

    /// Assign the DataTransfer object
175
    void setDataTransfer(DataTransfer const& dataTransfer)
176
177
178
179
    {
      dataTransfer_ = dataTransfer;
    }

180
181
182
183
    void setDataTransfer(DataTransfer&& dataTransfer)
    {
      dataTransfer_ = std::move(dataTransfer);
    }
184

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

Praetorius, Simon's avatar
Praetorius, Simon committed
191
192
  protected:

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

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

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

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

Praetorius, Simon's avatar
Praetorius, Simon committed
221
    std::shared_ptr<GlobalBasis const> basis_;
222
223
224
225
226
  };

#if DUNE_HAVE_CXX_CLASS_TEMPLATE_ARGUMENT_DEDUCTION
  template <class GB>
  DOFVector(GB&& basis, DataTransferOperation op = DataTransferOperation::INTERPOLATE)
227
    -> DOFVector<ParallelGlobalBasis<typename Underlying_t<GB>::PreBasis>>;
228
229
230
231
232
233
234
235
236
237
238
239
#endif

  /// \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.
   **/
240
241
242
  template <class ValueType = double, class GB>
  DOFVector<ParallelGlobalBasis<typename Underlying_t<GB>::PreBasis>, ValueType>
  makeDOFVector(GB&& basis, DataTransferOperation op = DataTransferOperation::INTERPOLATE)
243
244
245
246
  {
    return {FWD(basis), op};
  }

Praetorius, Simon's avatar
Praetorius, Simon committed
247
248
249
250
251
252
253
254
255
256
257
258
259
260
  /// 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);
  }

261
262
263
} // end namespace AMDiS

#include <amdis/DOFVector.inc.hpp>