ConstantGridFunction.hpp 4.92 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
  /// \brief LocalFunction of a Gridfunction returning a constant value.
18
19
20
21
22
  template <class Signature, class LocalContext, class Function>
  class ConstantLocalFunction;

  template <class R, class D, class LocalContext, class T>
  class ConstantLocalFunction<R(D), LocalContext, T>
Praetorius, Simon's avatar
Praetorius, Simon committed
23
  {
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  public:
    using Range = R;
    using Domain = D; // LocalDomain

    using Geometry = typename LocalContext::Geometry;

  public:
    ConstantLocalFunction(T const& value)
      : value_(value)
    {}

    void bind(LocalContext const& /*element*/) {}

    void unbind() {}

    Range const& operator()(Domain const& /*local*/) const
    {
      return value_;
    }

44
45
46
47
48
49
50
51
52
    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};
    }

    int order() const
53
54
55
56
57
58
59
60
    {
      return 0;
    }

  private:
    T value_;
  };

61
62
63
64
  /// \relates ConstantLocalFunction
  template <class R, class D, class LocalContext, class T>
  auto derivative(ConstantLocalFunction<R(D), LocalContext, T> const& lf)
  {
65
    return lf.derivative();
66
67
  }

68
69
70
71
72
73
74
75
  /// \relates ConstantLocalFunction
  template <class R, class D, class LocalContext, class T>
  int order(ConstantLocalFunction<R(D), LocalContext, T> const& lf)
  {
    return lf.order();
  }


76

77
78
79
80
81
82
83
84
85
86
87
88
  /**
    * \addtogroup GridFunctions
    * @{
    **/

  /// \brief Gridfunction returning a constant value.
  /**
   * A stored constant is return in global and local evaluation of this functor.
   * May be used with arithmetic types and vectors/matrices of arithmetic types.
   * It is also allowed to pass a \ref std::reference_wrapper to allow to
   * modify the value after construction.
   **/
89
90
91
92
93
94
95
96
97
  template <class T, class GridView>
  class ConstantGridFunction
  {
  public:
    using EntitySet = Dune::Functions::GridViewEntitySet<GridView, 0>;
    using Element = typename EntitySet::Element;

    using Domain = typename EntitySet::GlobalCoordinate;
    using LocalDomain = typename EntitySet::LocalCoordinate;
Praetorius, Simon's avatar
Praetorius, Simon committed
98
99
    using Range = Underlying_t<T>;

100
101
  public:
    using LocalFunction = ConstantLocalFunction<Range(LocalDomain), Element, T>;
Praetorius, Simon's avatar
Praetorius, Simon committed
102

103
104
105
  public:
    /// Constructor. Stores the function `fct` and creates an `EntitySet`.
    ConstantGridFunction(T const& value, GridView const& gridView)
Praetorius, Simon's avatar
Praetorius, Simon committed
106
      : value_(value)
107
      , entitySet_(gridView)
Praetorius, Simon's avatar
Praetorius, Simon committed
108
109
    {}

110
111
    /// Return the evaluated functor at global coordinates
    Range const& operator()(Domain const& /*x*/) const
Praetorius, Simon's avatar
Praetorius, Simon committed
112
113
114
115
    {
      return value_;
    }

116
    EntitySet const& entitySet() const
Praetorius, Simon's avatar
Praetorius, Simon committed
117
    {
118
      return entitySet_;
Praetorius, Simon's avatar
Praetorius, Simon committed
119
120
    }

121
122
123
124
    LocalFunction localFunction() const
    {
      return {value_};
    }
125

Praetorius, Simon's avatar
Praetorius, Simon committed
126
127
  private:
    T value_;
128
    EntitySet entitySet_;
Praetorius, Simon's avatar
Praetorius, Simon committed
129
130
  };

131
132
  /// Return the LocalFunction of the \ref ConstantGridFunction. \relates ConstantGridFunction
  template <class T, class GV>
133
  auto localFunction(ConstantGridFunction<T,GV> const& gf)
134
  {
135
    return gf.localFunction();
136
137
  }

Praetorius, Simon's avatar
Praetorius, Simon committed
138
139
  /** @} **/

140

Praetorius, Simon's avatar
Praetorius, Simon committed
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
  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
172
173
174
    /// \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
175
176
177
178
179
180
181
182
    template <class T>
    constexpr bool ConstantToGridFunction =
      Definition::ConstantToGridFunction<T>::value;

    /** @} **/

  } // end namespace Concepts

183

184
185
  template <class Value>
  struct GridFunctionCreator<Value, std::enable_if_t<Concepts::ConstantToGridFunction<Value>>>
Praetorius, Simon's avatar
Praetorius, Simon committed
186
  {
187
188
    template <class GridView>
    static auto create(Value const& value, GridView const& gridView)
189
    {
190
      return ConstantGridFunction<Value,GridView>{value, gridView};
191
    }
192
  };
Praetorius, Simon's avatar
Praetorius, Simon committed
193
194

} // end namespace AMDiS