DOFVectorView.hpp 3.1 KB
Newer Older
1
2
#pragma once

3
#include <amdis/GridFunctions.hpp>
4
#include <amdis/gridfunctions/DiscreteFunction.hpp>
5

6
7
namespace AMDiS
{
8
  /// A mutable view on the subspace of a DOFVector, \relates DiscreteFunction
9
  template <class GlobalBasisType, class RangeType, class TreePathType>
10
11
  class DOFVectorView
      : public DiscreteFunction<GlobalBasisType, RangeType, TreePathType>
12
  {
13
14
15
    using Self = DOFVectorView;
    using Super = DiscreteFunction<GlobalBasisType, RangeType, TreePathType>;

16
    using GlobalBasis = GlobalBasisType;
17
18
19
    using TreePath = TreePathType;

  public:
20
21
22
23
    /// Constructor. Stores a pointer to the mutable `dofvector`.
    DOFVectorView(DOFVector<GlobalBasis,RangeType>& dofVector, TreePath const& treePath)
      : Super(dofVector, treePath)
      , mutableDofVector_(&dofVector)
24
25
    {}

26
27
28
29
30
  public:
    /// \brief Interpolation of GridFunction to DOFVector, assuming that there is no
    /// reference to this DOFVector in the expression.
    template <class Expr>
    void interpolate_noalias(Expr&& expr)
31
    {
32
33
      auto const& basis = this->basis();
      auto const& treePath = this->treePath();
34

35
      auto&& gridFct = makeGridFunction(std::forward<Expr>(expr), basis.gridView());
36

37
      Dune::Functions::interpolate(basis, treePath, coefficients(), std::forward<decltype(gridFct)>(gridFct));
38
39
    }

40
41
42
    /// Interpolation of GridFunction to DOFVector
    template <class Expr>
    void interpolate(Expr&& expr)
43
    {
44
45
46
47
      // create temporary copy of data
      DOFVector<GlobalBasis,RangeType> tmp(coefficients());
      Self tmpView{tmp, this->treePath()};
      tmpView.interpolate_noalias(std::forward<Expr>(expr));
48

49
50
      // move data from temporary vector into stored DOFVector
      coefficients().vector() = std::move(tmp.vector());
51
52
    }

53
54
55
    /// \brief Interpolation of GridFunction to DOFVector, alias to \ref interpolate()
    template <class Expr>
    DOFVectorView& operator<<(Expr&& expr)
56
    {
57
58
      interpolate(expr);
      return *this;
59
60
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
61
    template <class Expr>
62
    DOFVectorView& operator+=(Expr&& expr)
63
    {
64
      interpolate((*this) + expr);
65
66
      return *this;
    }
67

68
    template <class Expr>
69
    DOFVectorView& operator-=(Expr&& expr)
70
    {
71
72
      interpolate((*this) - expr);
      return *this;
73
74
75
    }


76
    /// Return the mutable DOFVector
77
    DOFVector<GlobalBasis,RangeType>& coefficients() { return *mutableDofVector_; }
78

79
    /// Return the const DOFVector
Praetorius, Simon's avatar
Praetorius, Simon committed
80
    using Super::coefficients;
81

82
  protected:
83
    DOFVector<GlobalBasis,RangeType>* mutableDofVector_;
84
85
  };

86

87
  /// A Generator for a mutable \ref DOFVectorView
88
89
  template <class GlobalBasis, class RangeType, class TreePath>
  auto makeDOFVectorView(DOFVector<GlobalBasis, RangeType>& dofVector, TreePath const& treePath)
90
  {
91
    return DOFVectorView<GlobalBasis, RangeType, TreePath>{dofVector, treePath};
92
93
  }

94
  /// A Generator for a mutable \ref DOFVectorView
95
96
  template <class GlobalBasis, class RangeType>
  auto makeDOFVectorView(DOFVector<GlobalBasis, RangeType>& dofVector)
97
98
  {
    auto treePath = Dune::TypeTree::hybridTreePath();
99
    return DOFVectorView<GlobalBasis, RangeType, decltype(treePath)>{dofVector, treePath};
100
101
  }

102
} // end namespace AMDiS