ConstantGridFunction.hpp 4.33 KB
Newer Older
Praetorius, Simon's avatar
Praetorius, Simon committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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>

#include <dune/amdis/common/Utility.hpp>
#include <dune/amdis/gridfunctions/AnalyticGridFunction.hpp>
#include <dune/amdis/gridfunctions/GridFunctionConcepts.hpp>

namespace AMDiS
{
  /**
    * \addtogroup GridFunctions
    * @{
    **/

22
23
24
25
26
27
28
29
30
31
32
33
  /// \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.
   **/
  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
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
  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_;
    }

    friend int order(ConstantLocalFunction const& /*lf*/)
    {
      return 0;
    }

  private:
    T value_;
  };


  template <class R, class D, class LC, class T>
  auto derivative(ConstantLocalFunction<R(D),LC,T> const& lf)
  {
    using RawSignature = typename Dune::Functions::SignatureTraits<R(D)>::RawSignature;
    using DerivativeRange = typename Dune::Functions::DefaultDerivativeTraits<RawSignature>::Range;
    DerivativeRange diff(0);
    return ConstantLocalFunction<DerivativeRange(D),LC,DerivativeRange>{diff};
  }


  /// A Gridfunction that evaluates a function with global coordinates
  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
85
86
    using Range = Underlying_t<T>;

87
88
  public:
    using LocalFunction = ConstantLocalFunction<Range(LocalDomain), Element, T>;
Praetorius, Simon's avatar
Praetorius, Simon committed
89

90
91
92
  public:
    /// Constructor. Stores the function `fct` and creates an `EntitySet`.
    ConstantGridFunction(T const& value, GridView const& gridView)
Praetorius, Simon's avatar
Praetorius, Simon committed
93
      : value_(value)
94
      , entitySet_(gridView)
Praetorius, Simon's avatar
Praetorius, Simon committed
95
96
    {}

97
98
    /// Return the evaluated functor at global coordinates
    Range const& operator()(Domain const& /*x*/) const
Praetorius, Simon's avatar
Praetorius, Simon committed
99
100
101
102
    {
      return value_;
    }

103
    friend auto localFunction(ConstantGridFunction const& gf)
Praetorius, Simon's avatar
Praetorius, Simon committed
104
    {
105
      return LocalFunction{gf.value_};
Praetorius, Simon's avatar
Praetorius, Simon committed
106
107
    }

108
    EntitySet const& entitySet() const
Praetorius, Simon's avatar
Praetorius, Simon committed
109
    {
110
      return entitySet_;
Praetorius, Simon's avatar
Praetorius, Simon committed
111
112
113
114
    }

  private:
    T value_;
115
    EntitySet entitySet_;
Praetorius, Simon's avatar
Praetorius, Simon committed
116
117
118
119
  };

  /** @} **/

120

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


    template <class T>
    constexpr bool ConstantToGridFunction =
      Definition::ConstantToGridFunction<T>::value;

    /** @} **/

  } // end namespace Concepts


  /**
    * \addtogroup GridFunctions
    * @{
    **/

166
  /// Generator for a GridFunction representing a constant value
Praetorius, Simon's avatar
Praetorius, Simon committed
167
168
169
170
  template <class T, class GridView,
    REQUIRES(Concepts::ConstantToGridFunction<T>)>
  auto makeGridFunction(T const& value, GridView const& gridView, Dune::PriorityTag<2>)
  {
171
    return ConstantGridFunction<T,GridView>{value, gridView};
Praetorius, Simon's avatar
Praetorius, Simon committed
172
173
174
175
176
  }

  /** @} **/

} // end namespace AMDiS