DiscreteFunction.hpp 7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#pragma once

#include <vector>

#include <dune/common/std/optional.hh>
#include <dune/functions/common/defaultderivativetraits.hh>
#include <dune/functions/functionspacebases/defaultnodetorangemap.hh>
#include <dune/functions/functionspacebases/flatvectorview.hh>
#include <dune/functions/gridfunctions/gridviewentityset.hh>
#include <dune/typetree/childextraction.hh>

#include <amdis/GridFunctions.hpp>
#include <amdis/utility/FiniteElementType.hpp>

namespace AMDiS
{
  /// \brief A view on a subspace of a \ref DOFVector
  /**
   * \ingroup GridFunctions
   **/
  template <class GlobalBasisType, class RangeType, class TreePathType>
  class DiscreteFunction
  {
  public:
    using GlobalBasis = GlobalBasisType;
    using TreePath = TreePathType;

    using Tree = typename GlobalBasis::LocalView::Tree;
    using SubTree = typename Dune::TypeTree::ChildForTreePath<Tree, TreePath>;
    using NodeToRangeEntry = Dune::Functions::DefaultNodeToRangeMap<SubTree>;

    using GridView = typename GlobalBasis::GridView;
    using EntitySet = Dune::Functions::GridViewEntitySet<GridView, 0>;

    using Domain = typename EntitySet::GlobalCoordinate;
    using Range = RangeType_t<SubTree>;
    static_assert(std::is_arithmetic<RangeType>::value, "");
    // Don't know how to determine Range with non-trivial RangeType

    using RawSignature = typename Dune::Functions::SignatureTraits<Range(Domain)>::RawSignature;
    using DerivativeTraits = Dune::Functions::DefaultDerivativeTraits<RawSignature>;
    using DerivativeRange = typename DerivativeTraits::Range;

    using LocalDomain = typename EntitySet::LocalCoordinate;
    using Element = typename EntitySet::Element;
    using Geometry = typename Element::Geometry;

    enum { hasDerivative = false };

  public: // a local view on the gradients

    /// A LocalFunction representing the derivative of the DOFVector
    class GradientLocalFunction
    {
    public:
      using Domain = LocalDomain;
      using Range = DerivativeRange;

      enum { hasDerivative = false };

    private:
      using LocalView = typename GlobalBasis::LocalView;

    public:
      GradientLocalFunction(DiscreteFunction const& globalFunction)
        : globalFunction_(&globalFunction)
        , localView_(globalFunction_->basis().localView())
        , subTree_(&child(localView_.tree(), globalFunction_->treePath()))
      {}

      void bind(Element const& element)
      {
        localView_.bind(element);
        geometry_.emplace(element.geometry());
        bound_ = true;
      }

      void unbind()
      {
        localView_.unbind();
        geometry_.reset();
        bound_ = false;
      }

      /// Evaluate Gradient at bound element in local coordinates
      Range operator()(Domain const& x) const;

      friend int order(GradientLocalFunction const& self)
      {
        assert( self.bound_ );
        return std::max(0, polynomialDegree(*self.subTree_)-1);
      }

      /// Return the bound element
      Element const& localContext() const
      {
        assert( bound_ );
        return localView_.element();
      }

    private:
      DiscreteFunction const* globalFunction_;
      LocalView localView_;

      SubTree const* subTree_;

      Dune::Std::optional<Geometry> geometry_;
      bool bound_ = false;
    };


  public: // a local view on the values

    /// A LocalFunction, i.e., an element local view on the DOFVector
    class LocalFunction
    {
    public:
      using Domain = typename DiscreteFunction::LocalDomain;
      using Range = typename DiscreteFunction::Range;

      enum { hasDerivative = true };

    private:
      using LocalView = typename GlobalBasis::LocalView;

    public:
      LocalFunction(DiscreteFunction const& globalFunction)
        : globalFunction_(&globalFunction)
        , localView_(globalFunction_->basis().localView())
        , subTree_(&child(localView_.tree(), globalFunction_->treePath()))
      {}

      void bind(Element const& element)
      {
        localView_.bind(element);
        bound_ = true;
      }

      void unbind()
      {
        localView_.unbind();
        bound_ = false;
      }

      /// Evaluate LocalFunction at bound element in local coordinates
      Range operator()(Domain const& x) const;

      /// \brief Create a LocalFunction representing the gradient. \relates GradientLocalFunction
      friend GradientLocalFunction derivative(LocalFunction const& localFunction)
      {
        static_assert(isValidRange<DerivativeTraits>(),
          "Derivative of DOFVector not defined.");
        return GradientLocalFunction{*localFunction.globalFunction_};
      }

      friend int order(LocalFunction const& self)
      {
        assert( self.bound_ );
        return polynomialDegree(*self.subTree_);
      }

      /// Return the bound element
      Element const& localContext() const
      {
        assert( bound_ );
        return localView_.element();
      }

    private:
      DiscreteFunction const* globalFunction_;
      LocalView localView_;
      SubTree const* subTree_;

      bool bound_ = false;
    };


  public:
    /// Constructor. Stores a pointer to the dofVector and a copy of the treePath.
    DiscreteFunction(DOFVector<GlobalBasis,RangeType> const& dofVector, TreePath const& treePath)
      : dofVector_(&dofVector)
      , treePath_(treePath)
      , entitySet_(dofVector.basis().gridView())
      , nodeToRangeEntry_(Dune::Functions::makeDefaultNodeToRangeMap(dofVector.basis(), treePath))
    {}

    /// Evaluate DiscreteFunction in global coordinates. NOTE: expensive
    Range operator()(Domain const& x) const;

    /// \brief Create a local function for this view on the DOFVector. \relates LocalFunction
    friend LocalFunction localFunction(DiscreteFunction const& self)
    {
      return LocalFunction{self};
    }

    EntitySet const& entitySet() const
    {
      return entitySet_;
    }

  public:
    /// Return global basis
    GlobalBasis const& basis() const
    {
      return dofVector_->basis();
    }

    /// Return treePath associated with this view
    TreePath const& treePath() const
    {
      return treePath_;
    }

    /// Return const coefficient vector
    DOFVector<GlobalBasis,RangeType> const& coefficients() const
    {
      return *dofVector_;
    }

  protected:
    DOFVector<GlobalBasis,RangeType> const* dofVector_;
    TreePath const treePath_;

    EntitySet entitySet_;
    NodeToRangeEntry nodeToRangeEntry_;
  };

  /// A Generator for a \ref DiscreteFunction
  template <class GlobalBasis, class RangeType, class TreePath>
  auto makeDiscreteFunction(DOFVector<GlobalBasis, RangeType> const& dofVector, TreePath const& treePath)
  {
    return DiscreteFunction<GlobalBasis, RangeType, TreePath>{dofVector, treePath};
  }

  /// A Generator for a \ref DiscreteFunction
  template <class GlobalBasis, class RangeType>
  auto makeDiscreteFunction(DOFVector<GlobalBasis, RangeType> const& dofVector)
  {
    auto treePath = Dune::TypeTree::hybridTreePath();
    return DiscreteFunction<GlobalBasis, RangeType, decltype(treePath)>{dofVector, treePath};
  }

} // end namespace AMDiS

#include "DiscreteFunction.inc.hpp"