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

#include <functional>
#include <type_traits>

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

10
#include <amdis/common/DerivativeTraits.hpp>
11
#include <amdis/common/TypeTraits.hpp>
12
#include <amdis/gridfunctions/AnalyticGridFunction.hpp>
13
#include <amdis/gridfunctions/GridFunction.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 LC, class T>
  class ConstantLocalFunction<R(D), LC, 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
  private:
37
    using Geometry = typename LC::Geometry;
38

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

45
    void bind(LC const& /*element*/) { /* do nothing */ }
46
    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
    template <class Type>
    auto makeDerivative(Type const& type) const
58
59
    {
      using RawSignature = typename Dune::Functions::SignatureTraits<R(D)>::RawSignature;
60
      using DerivativeRange = typename DerivativeTraits<RawSignature,Type>::Range;
61
      DerivativeRange diff(0);
62
      return ConstantLocalFunction<DerivativeRange(D),LC,DerivativeRange>{diff};
63
64
    }

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

  private:
    T value_;
  };

75

76
77
  /// \brief Gridfunction returning a constant value.
  /**
78
79
   * \ingroup GridFunctions
   *
80
   * A stored constant is return in global and local evaluation of this functor.
81
   * Maybe used with arithmetic types and vectors/matrices of arithmetic types.
82
83
84
   * It is also allowed to pass a \ref std::reference_wrapper to allow to
   * modify the value after construction.
   **/
85
86
87
88
89
90
  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
91
92
    using Range = Underlying_t<T>;

93
94
    enum { hasDerivative = false };

95
96
97
  private:
    using Element = typename EntitySet::Element;
    using LocalDomain = typename EntitySet::LocalCoordinate;
98
    using LocalFunction = ConstantLocalFunction<Range(LocalDomain), Element, T>;
Praetorius, Simon's avatar
Praetorius, Simon committed
99

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

107
    /// Return the constant `value_`
108
    Range const& operator()(Domain const& /*x*/) const
Praetorius, Simon's avatar
Praetorius, Simon committed
109
110
111
112
    {
      return value_;
    }

113
    EntitySet const& entitySet() const
Praetorius, Simon's avatar
Praetorius, Simon committed
114
    {
115
      return entitySet_;
Praetorius, Simon's avatar
Praetorius, Simon committed
116
117
    }

118
    /// \brief Create an \ref ConstantLocalFunction with the stores `value_`.
119
    LocalFunction makeLocalFunction() const
120
121
122
    {
      return {value_};
    }
123

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

129

Praetorius, Simon's avatar
Praetorius, Simon committed
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
  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
161
162
163
    /// \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
164
165
166
167
168
169
170
171
    template <class T>
    constexpr bool ConstantToGridFunction =
      Definition::ConstantToGridFunction<T>::value;

    /** @} **/

  } // end namespace Concepts

172

173
174
  template <class Value>
  struct GridFunctionCreator<Value, std::enable_if_t<Concepts::ConstantToGridFunction<Value>>>
Praetorius, Simon's avatar
Praetorius, Simon committed
175
  {
176
177
    template <class GridView>
    static auto create(Value const& value, GridView const& gridView)
178
    {
179
      return ConstantGridFunction<Value,GridView>{value, gridView};
180
    }
181
  };
Praetorius, Simon's avatar
Praetorius, Simon committed
182
183

} // end namespace AMDiS