AMDiS  2.10
The Adaptive Multi-Dimensional Simulation Toolbox
ConstantGridFunction.hpp
1 #pragma once
2 
3 #include <functional>
4 #include <type_traits>
5 
6 #include <dune/common/diagonalmatrix.hh>
7 #include <dune/common/fmatrix.hh>
8 #include <dune/common/fvector.hh>
9 
10 #include <amdis/common/DerivativeTraits.hpp>
11 #include <amdis/common/TypeTraits.hpp>
12 #include <amdis/gridfunctions/AnalyticGridFunction.hpp>
13 #include <amdis/gridfunctions/GridFunction.hpp>
14 
15 namespace AMDiS
16 {
17 #ifndef DOXYGEN
18  template <class Signature, class LocalContext, class Function>
20 #endif
21 
23  template <class R, class D, class LC, class T>
24  class ConstantLocalFunction<R(D), LC, T>
25  {
26  public:
28  using Domain = D;
29 
31  using Range = R;
32 
34  enum { hasDerivative = true };
35 
36  private:
37  using LocalContext = LC;
38  using Geometry = typename LC::Geometry;
39 
40  public:
42  ConstantLocalFunction(T const& value)
43  : value_(value)
44  {}
45 
46  void bind(LocalContext const& element)
47  {
48  localContext_.emplace(element);
49  }
50 
51  void unbind() { /* do nothing */ }
52 
54  bool bound() const
55  {
56  return !!localContext_;
57  }
58 
59  LocalContext const& localContext() const
60  {
61  assert( !!localContext_ );
62  return *localContext_;
63  }
64 
66  Range const& operator()(Domain const& /*local*/) const
67  {
68  return value_;
69  }
70 
73  template <class Type>
74  auto makeDerivative(Type const& /*type*/) const
75  {
76  using RawSignature = typename Dune::Functions::SignatureTraits<R(D)>::RawSignature;
77  using DerivativeRange = typename DerivativeTraits<RawSignature,Type>::Range;
78  DerivativeRange diff(0);
80  // bind derivative if this is bound
81  if (bound())
82  df.bind(*localContext_);
83  return df;
84  }
85 
87  int order() const
88  {
89  return 0;
90  }
91 
92  private:
93  T value_;
94  std::optional<LocalContext> localContext_;
95  };
96 
97 
99 
107  template <class T, class GridView>
109  {
110  public:
111  using EntitySet = Dune::Functions::GridViewEntitySet<GridView, 0>;
112  using Domain = typename EntitySet::GlobalCoordinate;
113  using Range = Underlying_t<T>;
114 
115  enum { hasDerivative = false };
116 
117  private:
118  using Element = typename EntitySet::Element;
119  using LocalDomain = typename EntitySet::LocalCoordinate;
121 
122  public:
124  ConstantGridFunction(T const& value, GridView const& gridView)
125  : value_(value)
126  , entitySet_(gridView)
127  {}
128 
130  Range const& operator()(Domain const& /*x*/) const
131  {
132  return value_;
133  }
134 
135  EntitySet const& entitySet() const
136  {
137  return entitySet_;
138  }
139 
142  {
143  return {value_};
144  }
145 
146  private:
147  T value_;
148  EntitySet entitySet_;
149  };
150 
151 
152  namespace Concepts
153  {
158  namespace Definition
159  {
160  template <class T>
162  : std::is_arithmetic<T> {};
163 
164  template <class T>
165  struct ConstantToGridFunction<std::reference_wrapper<T>>
166  : ConstantToGridFunction<T> {};
167 
168  template <class T, int N>
169  struct ConstantToGridFunction<Dune::FieldVector<T, N>>
170  : ConstantToGridFunction<T> {};
171 
172  template <class T, int N, int M>
173  struct ConstantToGridFunction<Dune::FieldMatrix<T, N, M>>
174  : ConstantToGridFunction<T> {};
175 
176  template <class T, int N>
177  struct ConstantToGridFunction<Dune::DiagonalMatrix<T, N>>
178  : ConstantToGridFunction<T> {};
179 
180  } // end namespace Definition
181 
182 
186  template <class T>
187  constexpr bool ConstantToGridFunction =
189 
192  } // end namespace Concepts
193 
194 
195  template <class Value>
196  struct GridFunctionCreator<Value, std::enable_if_t<Concepts::ConstantToGridFunction<Value>>>
197  {
198  template <class GridView>
199  static auto create(Value const& value, GridView const& gridView)
200  {
201  return ConstantGridFunction<Value,GridView>{value, gridView};
202  }
203  };
204 
205 } // end namespace AMDiS
Range const & operator()(Domain const &) const
Return the constant value_
Definition: ConstantGridFunction.hpp:130
Definition: GridFunction.hpp:96
Definition: ConstantGridFunction.hpp:19
Definition: FieldMatVec.hpp:12
ConstantGridFunction(T const &value, GridView const &gridView)
Constructor. Stores the function fct and creates an EntitySet.
Definition: ConstantGridFunction.hpp:124
Definition: AdaptBase.hpp:6
Gridfunction returning a constant value.
Definition: ConstantGridFunction.hpp:108
Range const & operator()(Domain const &) const
Return the constant value_.
Definition: ConstantGridFunction.hpp:66
constexpr bool ConstantToGridFunction
Concepts that is true for all &#39;&#39;simple&#39;&#39; types that can be converted automatically to a GridFunction...
Definition: ConstantGridFunction.hpp:187
ConstantLocalFunction(T const &value)
Constructor. Stores the constant value.
Definition: ConstantGridFunction.hpp:42
auto makeDerivative(Type const &) const
Create a ConstantLocalFunction representing the derivative of a constant function, that ist, the value 0.
Definition: ConstantGridFunction.hpp:74
D Domain
The LocalDomain this LocalFunction can be evaluated in.
Definition: ConstantGridFunction.hpp:28
LocalFunction makeLocalFunction() const
Create an ConstantLocalFunction with the stores value_.
Definition: ConstantGridFunction.hpp:141
Definition: DerivativeTraits.hpp:29
bool bound() const
Check whether this localfunction is bound to an element.
Definition: ConstantGridFunction.hpp:54
R Range
The range type of the LocalFunction.
Definition: ConstantGridFunction.hpp:31
Definition: ConstantGridFunction.hpp:161
int order() const
Return the constant polynomial order 0.
Definition: ConstantGridFunction.hpp:87