ConstantGridFunction.hpp 5.44 KB
Newer Older
Praetorius, Simon's avatar
Praetorius, Simon committed
1
2
3
4
5
6
7
8
9
10
#pragma once

#include <functional>
#include <type_traits>

#include <dune/common/diagonalmatrix.hh>
#include <dune/common/fmatrix.hh>
#include <dune/common/fvector.hh>
#include <dune/functions/common/defaultderivativetraits.hh>

11
12
13
#include <amdis/common/Utility.hpp>
#include <amdis/gridfunctions/AnalyticGridFunction.hpp>
#include <amdis/gridfunctions/GridFunctionConcepts.hpp>
Praetorius, Simon's avatar
Praetorius, Simon committed
14
15
16

namespace AMDiS
{
17
#ifndef DOXYGEN
18
19
  template <class Signature, class LocalContext, class Function>
  class ConstantLocalFunction;
20
#endif
21

22
  /// \brief LocalFunction of a Gridfunction returning a constant value.
23
24
  template <class R, class D, class LocalContext, class T>
  class ConstantLocalFunction<R(D), LocalContext, T>
Praetorius, Simon's avatar
Praetorius, Simon committed
25
  {
26
  public:
27
28
    /// The LocalDomain this LocalFunction can be evaluated in
    using Domain = D;
29

30
31
    /// The range type of the LocalFunction
    using Range = R;
32

33
    /// This LocalFunction has its own \ref derivative() function
34
35
    enum { hasDerivative = true };

36
37
38
  private:
    using Geometry = typename LocalContext::Geometry;

39
  public:
40
    /// \brief Constructor. Stores the constant value.
41
42
43
44
    ConstantLocalFunction(T const& value)
      : value_(value)
    {}

45
46
    void bind(LocalContext const& /*element*/) { /* do nothing */ }
    void unbind() { /* do nothing */ }
47

48
    /// Return the constant `value_`.
49
50
51
52
53
    Range const& operator()(Domain const& /*local*/) const
    {
      return value_;
    }

54
55
    /// \brief Create a \ref ConstantLocalFunction representing the derivative
    /// of a constant function, that ist, the value 0.
56
57
58
59
60
61
62
63
    auto derivative() const
    {
      using RawSignature = typename Dune::Functions::SignatureTraits<R(D)>::RawSignature;
      using DerivativeRange = typename Dune::Functions::DefaultDerivativeTraits<RawSignature>::Range;
      DerivativeRange diff(0);
      return ConstantLocalFunction<DerivativeRange(D),LocalContext,DerivativeRange>{diff};
    }

64
    /// Return the constant polynomial order 0.
65
    int order() const
66
67
68
69
70
71
72
73
    {
      return 0;
    }

  private:
    T value_;
  };

74
75
76
77
  /// \relates ConstantLocalFunction
  template <class R, class D, class LocalContext, class T>
  auto derivative(ConstantLocalFunction<R(D), LocalContext, T> const& lf)
  {
78
    return lf.derivative();
79
80
  }

81
82
83
84
  /// \relates ConstantLocalFunction
  template <class R, class D, class LocalContext, class T>
  int order(ConstantLocalFunction<R(D), LocalContext, T> const& lf)
  {
85
    return 0;
86
87
88
  }


89
90
  /// \brief Gridfunction returning a constant value.
  /**
91
92
   * \ingroup GridFunctions
   *
93
   * A stored constant is return in global and local evaluation of this functor.
94
   * Maybe used with arithmetic types and vectors/matrices of arithmetic types.
95
96
97
   * It is also allowed to pass a \ref std::reference_wrapper to allow to
   * modify the value after construction.
   **/
98
99
100
101
102
103
  template <class T, class GridView>
  class ConstantGridFunction
  {
  public:
    using EntitySet = Dune::Functions::GridViewEntitySet<GridView, 0>;
    using Domain = typename EntitySet::GlobalCoordinate;
Praetorius, Simon's avatar
Praetorius, Simon committed
104
105
    using Range = Underlying_t<T>;

106
107
    enum { hasDerivative = false };

108
109
110
  private:
    using Element = typename EntitySet::Element;
    using LocalDomain = typename EntitySet::LocalCoordinate;
111
    using LocalFunction = ConstantLocalFunction<Range(LocalDomain), Element, T>;
Praetorius, Simon's avatar
Praetorius, Simon committed
112

113
114
115
  public:
    /// Constructor. Stores the function `fct` and creates an `EntitySet`.
    ConstantGridFunction(T const& value, GridView const& gridView)
Praetorius, Simon's avatar
Praetorius, Simon committed
116
      : value_(value)
117
      , entitySet_(gridView)
Praetorius, Simon's avatar
Praetorius, Simon committed
118
119
    {}

120
    /// Return the constant `value_`
121
    Range const& operator()(Domain const& /*x*/) const
Praetorius, Simon's avatar
Praetorius, Simon committed
122
123
124
125
    {
      return value_;
    }

126
    EntitySet const& entitySet() const
Praetorius, Simon's avatar
Praetorius, Simon committed
127
    {
128
      return entitySet_;
Praetorius, Simon's avatar
Praetorius, Simon committed
129
130
    }

131
    /// \brief Create an \ref ConstantLocalFunction with the stores `value_`.
132
133
134
135
    LocalFunction localFunction() const
    {
      return {value_};
    }
136

Praetorius, Simon's avatar
Praetorius, Simon committed
137
138
  private:
    T value_;
139
    EntitySet entitySet_;
Praetorius, Simon's avatar
Praetorius, Simon committed
140
141
  };

142
143

  /// \relates ConstantGridFunction
144
  template <class T, class GV>
145
  auto localFunction(ConstantGridFunction<T,GV> const& gf)
146
  {
147
    return gf.localFunction();
148
149
  }

Praetorius, Simon's avatar
Praetorius, Simon committed
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

  namespace Concepts
  {
    /** \addtogroup Concepts
     *  @{
     **/

    namespace Definition
    {
      template <class T>
      struct ConstantToGridFunction
        : std::is_arithmetic<T> {};

      template <class T>
      struct ConstantToGridFunction<std::reference_wrapper<T>>
        : ConstantToGridFunction<T> {};

      template <class T, int N>
      struct ConstantToGridFunction<Dune::FieldVector<T, N>>
        : ConstantToGridFunction<T> {};

      template <class T, int N, int M>
      struct ConstantToGridFunction<Dune::FieldMatrix<T, N, M>>
        : ConstantToGridFunction<T> {};

      template <class T, int N>
      struct ConstantToGridFunction<Dune::DiagonalMatrix<T, N>>
        : ConstantToGridFunction<T> {};

    } // end namespace Definition


Praetorius, Simon's avatar
Praetorius, Simon committed
182
183
184
    /// \brief Concepts that is true for all ''simple'' types that can be
    /// converted automatically to a GridFunction, e.g. arithmetic types,
    /// FieldVectors, and `std::reference_wrapper`.
Praetorius, Simon's avatar
Praetorius, Simon committed
185
186
187
188
189
190
191
192
    template <class T>
    constexpr bool ConstantToGridFunction =
      Definition::ConstantToGridFunction<T>::value;

    /** @} **/

  } // end namespace Concepts

193

194
195
  template <class Value>
  struct GridFunctionCreator<Value, std::enable_if_t<Concepts::ConstantToGridFunction<Value>>>
Praetorius, Simon's avatar
Praetorius, Simon committed
196
  {
197
198
    template <class GridView>
    static auto create(Value const& value, GridView const& gridView)
199
    {
200
      return ConstantGridFunction<Value,GridView>{value, gridView};
201
    }
202
  };
Praetorius, Simon's avatar
Praetorius, Simon committed
203
204

} // end namespace AMDiS