AMDiS  0.3
The Adaptive Multi-Dimensional Simulation Toolbox
AnalyticGridFunction.hpp
1 #pragma once
2 
3 #include <optional>
4 #include <type_traits>
5 
6 #include <dune/functions/common/signature.hh>
7 #include <dune/functions/gridfunctions/gridviewentityset.hh>
8 
9 #include <amdis/Operations.hpp>
10 #include <amdis/common/DerivativeTraits.hpp>
11 #include <amdis/common/Order.hpp>
12 #include <amdis/gridfunctions/GridFunction.hpp>
13 
14 namespace AMDiS
15 {
16 #ifndef DOXYGEN
17  template <class Signature, class LocalContext, class Function>
19 #endif
20 
21  template <class R, class D, class LC, class Function>
22  class AnalyticLocalFunction<R(D), LC, Function>
23  {
24  public:
26  using Domain = D;
27 
29  using Range = R;
30 
32  enum { hasDerivative = true };
33 
34  private:
35  using Geometry = typename LC::Geometry;
36 
37  public:
39  AnalyticLocalFunction(Function const& fct)
40  : fct_{fct}
41  {}
42 
44  void bind(LC const& element)
45  {
46  geometry_.emplace(element.geometry());
47  }
48 
50  void unbind()
51  {
52  geometry_.reset();
53  }
54 
57  Range operator()(Domain const& local) const
58  {
59  assert( bool(geometry_) );
60  return fct_(geometry_.value().global(local));
61  }
62 
63  Function const& fct() const
64  {
65  return fct_;
66  }
67 
68  private:
69  Function fct_;
70  std::optional<Geometry> geometry_;
71  };
72 
73 
76 
84  template <class Sig, class LC, class F,
85  REQUIRES(Concepts::HasFunctorOrder<F,1>)>
87  {
88  return order(lf.fct(),1);
89  }
90 
91 
95 
102  template <class R, class D, class LC, class F, class Type>
103  auto derivativeOf(AnalyticLocalFunction<R(D),LC,F> const& lf, Type const& type)
104  {
105  static_assert(Concepts::HasDerivative<F,Type>,
106  "No derivative(F,DerivativeType) defined for Functor F of AnalyticLocalFunction.");
107 
108  auto df = derivativeOf(lf.fct(), type);
109 
110  using RawSignature = typename Dune::Functions::SignatureTraits<R(D)>::RawSignature;
111  using DerivativeSignature = typename DerivativeTraits<RawSignature,Type>::Range(D);
113  }
114 
117 
127  template <class Function, class GridView>
129  {
130  public:
131  using EntitySet = Dune::Functions::GridViewEntitySet<GridView, 0>;
132  using Domain = typename EntitySet::GlobalCoordinate;
134 
135  enum { hasDerivative = true };
136 
137  private:
138  using Element = typename EntitySet::Element;
139  using LocalDomain = typename EntitySet::LocalCoordinate;
141 
142  public:
144  AnalyticGridFunction(Function const& fct, GridView const& gridView)
145  : fct_{fct}
146  , entitySet_{gridView}
147  {}
148 
150  Range operator()(Domain const& x) const
151  {
152  return fct_(x);
153  }
154 
157  {
158  return {fct_};
159  }
160 
162  EntitySet const& entitySet() const
163  {
164  return entitySet_;
165  }
166 
168  Function const& fct() const { return fct_; }
169 
170  private:
171  Function fct_;
172  EntitySet entitySet_;
173  };
174 
175 
176  namespace Concepts
177  {
178  namespace Definition
179  {
180  template <class F, int dow>
181  constexpr bool CallableDow =
182  Concepts::Callable<F, Dune::FieldVector<double, dow>>;
183  }
184 
186 #ifndef WORLDDIM
187  template <class F>
188  constexpr bool CallableDomain =
189  Definition::CallableDow<F, 1> || Definition::CallableDow<F, 2> || Definition::CallableDow<F, 3>;
190 #else
191  template <class F>
192  constexpr bool CallableDomain =
193  Definition::CallableDow<F, WORLDDIM>;
194 #endif
195 
196  template <class F>
197  using CallableDomain_t = bool_t<CallableDomain<F>>;
198 
199  } // end namespace Concepts
200 
201 
202  // Creator for the AnalyticGridFunction
203  template <class Function>
204  struct GridFunctionCreator<Function, std::enable_if_t<Concepts::CallableDomain<Function>>>
205  {
206  template <class GridView>
207  static auto create(Function const& fct, GridView const& gridView)
208  {
209  return AnalyticGridFunction<Function, GridView>{fct, gridView};
210  }
211  };
212 
213 
216 
222  template <class F, class GV, class Type>
223  auto derivativeOf(AnalyticGridFunction<F,GV> const& gf, Type const& type)
224  {
225  static_assert(Concepts::HasDerivative<F,Type>,
226  "No derivative(F,DerivativeType) defined for Functor of AnalyticLocalFunction.");
227 
228  auto df = derivativeOf(gf.fct(), type);
229  return AnalyticGridFunction<decltype(df), GV>{df, gf.entitySet().gridView()};
230  }
231 
232 
233 #ifndef DOXYGEN
234  // A pre-GridFunction that just stores the function
235  template <class Function>
237  {
239 
240  struct Creator
241  {
242  template <class GridView>
243  static auto create(Self const& self, GridView const& gridView)
244  {
245  return AnalyticGridFunction<Function, GridView>{self.fct_, gridView};
246  }
247  };
248 
249  Function fct_;
250  };
251 
252  namespace Traits
253  {
254  template <class Functor>
256  : std::true_type {};
257  }
258 #endif
259 
260 
263 
272  template <class Function,
273  REQUIRES(Concepts::CallableDomain<Function>)>
274  auto evalAtQP(Function const& f)
275  {
277  }
278 
279 } // end namespace AMDiS
Range operator()(Domain const &local) const
Evaluate the function in global coordinate by a local-to-global mapping of the local coordinates usin...
Definition: AnalyticGridFunction.hpp:57
AnalyticLocalFunction(Function const &fct)
Constructor. stores the function fct.
Definition: AnalyticGridFunction.hpp:39
Definition: GridFunction.hpp:26
D Domain
The LocalDomain this LocalFunction can be evaluated in.
Definition: AnalyticGridFunction.hpp:26
EntitySet const & entitySet() const
Returns entitySet_.
Definition: AnalyticGridFunction.hpp:162
Definition: AnalyticGridFunction.hpp:240
constexpr bool Functor
A Functor is a function F with signature Signature.
Definition: Concepts.hpp:134
Definition: AnalyticGridFunction.hpp:18
typename remove_cvref< T >::type remove_cvref_t
Helper alias template for remove_cvref.
Definition: TypeTraits.hpp:24
Definition: GridFunction.hpp:96
Definition: FieldMatVec.hpp:12
Range operator()(Domain const &x) const
Return the evaluated functor at global coordinates.
Definition: AnalyticGridFunction.hpp:150
Contains all classes needed for solving linear and non linear equation systems.
Definition: AdaptBase.hpp:6
Function const & fct() const
Returns fct_.
Definition: AnalyticGridFunction.hpp:168
void unbind()
Releases the geometry object.
Definition: AnalyticGridFunction.hpp:50
R Range
The range type of the LocalFunction.
Definition: AnalyticGridFunction.hpp:29
void bind(LC const &element)
Create a geometry object from the element.
Definition: AnalyticGridFunction.hpp:44
A Gridfunction that evaluates a function with global coordinates.
Definition: AnalyticGridFunction.hpp:128
LocalFunction makeLocalFunction() const
Return the AnalyticLocalFunction of the AnalyticGridFunction.
Definition: AnalyticGridFunction.hpp:156
auto derivativeOf(AnalyticLocalFunction< R(D), LC, F > const &lf, Type const &type)
Definition: AnalyticGridFunction.hpp:103
Definition: AnalyticGridFunction.hpp:236
Definition: DerivativeTraits.hpp:29
std::integral_constant< bool, B > bool_t
A wrapper for bool types.
Definition: Logical.hpp:12
AnalyticGridFunction(Function const &fct, GridView const &gridView)
Constructor. Stores the function fct and creates an EntitySet.
Definition: AnalyticGridFunction.hpp:144
int order(AnalyticLocalFunction< Sig, LC, F > const &lf)
Definition: AnalyticGridFunction.hpp:86