AMDiS  2.10
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  using LocalContext = LC;
33 
35  enum { hasDerivative = true };
36 
37  private:
38  using Geometry = typename LC::Geometry;
39 
40  public:
42  AnalyticLocalFunction(Function const& fct)
43  : fct_{fct}
44  {}
45 
47  void bind(LocalContext const& element)
48  {
49  localContext_.emplace(element);
50  geometry_.emplace(element.geometry());
51  }
52 
54  void unbind()
55  {
56  geometry_.reset();
57  localContext_.reset();
58  }
60  bool bound() const
61  {
62  return !!localContext_;
63  }
64 
66  LocalContext const& localContext() const
67  {
68  assert( !!localContext_ );
69  return *localContext_;
70  }
71 
74  Range operator()(Domain const& local) const
75  {
76  assert( !!geometry_ );
77  return fct_(geometry_.value().global(local));
78  }
79 
80  Function const& fct() const
81  {
82  return fct_;
83  }
84 
85  private:
86  Function fct_;
87  std::optional<LocalContext> localContext_;
88  std::optional<Geometry> geometry_;
89  };
90 
91 
94 
102  template <class Sig, class LC, class F,
103  REQUIRES(Concepts::HasFunctorOrder<F,1>)>
105  {
106  return order(lf.fct(),1);
107  }
108 
109 
113 
120  template <class R, class D, class LC, class F, class Type>
121  auto derivativeOf(AnalyticLocalFunction<R(D),LC,F> const& lf, Type const& type)
122  {
123  static_assert(Concepts::HasDerivative<F,Type>,
124  "No derivative(F,DerivativeType) defined for Functor F of AnalyticLocalFunction.");
125 
126  auto df = derivativeOf(lf.fct(), type);
127 
128  using RawSignature = typename Dune::Functions::SignatureTraits<R(D)>::RawSignature;
129  using DerivativeSignature = typename DerivativeTraits<RawSignature,Type>::Range(D);
131  // bind if lf is bound
132  if (lf.bound())
133  ldf.bind(lf.localContext());
134  return ldf;
135  }
136 
139 
149  template <class Function, class GridView>
151  {
152  public:
153  using EntitySet = Dune::Functions::GridViewEntitySet<GridView, 0>;
154  using Domain = typename EntitySet::GlobalCoordinate;
155  using Range = remove_cvref_t<std::result_of_t<Function(Domain)>>;
156 
157  enum { hasDerivative = true };
158 
159  private:
160  using Element = typename EntitySet::Element;
161  using LocalDomain = typename EntitySet::LocalCoordinate;
163 
164  public:
166  AnalyticGridFunction(Function const& fct, GridView const& gridView)
167  : fct_{fct}
168  , entitySet_{gridView}
169  {}
170 
172  Range operator()(Domain const& x) const
173  {
174  return fct_(x);
175  }
176 
179  {
180  return {gf.fct_};
181  }
182 
184  EntitySet const& entitySet() const
185  {
186  return entitySet_;
187  }
188 
190  Function const& fct() const { return fct_; }
191 
192  private:
193  Function fct_;
194  EntitySet entitySet_;
195  };
196 
197 
200 
206  template <class F, class GV, class Type>
207  auto derivativeOf(AnalyticGridFunction<F,GV> const& gf, Type const& type)
208  {
209  static_assert(Concepts::HasDerivative<F,Type>,
210  "No derivative(F,DerivativeType) defined for Functor of AnalyticLocalFunction.");
211 
212  auto df = derivativeOf(gf.fct(), type);
213  return AnalyticGridFunction<decltype(df), GV>{df, gf.entitySet().gridView()};
214  }
215 
216 
217 #ifndef DOXYGEN
218  // A pre-GridFunction that just stores the function
219  template <class Function>
221  {
223 
224  struct Creator
225  {
226  template <class GridView>
227  static auto create(Self const& self, GridView const& gridView)
228  {
229  using Coordinate = typename GridView::template Codim<0>::Entity::Geometry::GlobalCoordinate;
230  static_assert(std::is_invocable_v<Function, Coordinate>);
231 
232  return AnalyticGridFunction<Function, GridView>{self.fct_, gridView};
233  }
234  };
235 
236  Function fct_;
237  };
238 
239  namespace Traits
240  {
241  template <class Functor>
243  : std::true_type {};
244  }
245 #endif
246 
247 
250 
259  template <class Function>
260  auto evalAtQP(Function const& f)
261  {
263  }
264 
265 } // 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:74
AnalyticLocalFunction(Function const &fct)
Constructor. stores the function fct.
Definition: AnalyticGridFunction.hpp:42
void bind(LocalContext const &element)
Create a geometry object from the element.
Definition: AnalyticGridFunction.hpp:47
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:184
Definition: AnalyticGridFunction.hpp:224
constexpr bool Functor
A Functor is a function F with signature Signature.
Definition: Concepts.hpp:133
Definition: AnalyticGridFunction.hpp:18
Range operator()(Domain const &x) const
Return the evaluated functor at global coordinates.
Definition: AnalyticGridFunction.hpp:172
Definition: AdaptBase.hpp:6
Function const & fct() const
Returns fct_.
Definition: AnalyticGridFunction.hpp:190
void unbind()
Releases the geometry object.
Definition: AnalyticGridFunction.hpp:54
R Range
The range type of the LocalFunction.
Definition: AnalyticGridFunction.hpp:29
friend LocalFunction localFunction(AnalyticGridFunction const &gf)
Return the AnalyticLocalFunction of the AnalyticGridFunction.
Definition: AnalyticGridFunction.hpp:178
A Gridfunction that evaluates a function with global coordinates.
Definition: AnalyticGridFunction.hpp:150
Definition: AnalyticGridFunction.hpp:220
LocalContext const & localContext() const
Return the context this localfunction is bound to.
Definition: AnalyticGridFunction.hpp:66
Definition: DerivativeTraits.hpp:29
LC LocalContext
The context this localfunction can be bound to.
Definition: AnalyticGridFunction.hpp:32
bool bound() const
Check whether this localfunction is bound to an element.
Definition: AnalyticGridFunction.hpp:60
AnalyticGridFunction(Function const &fct, GridView const &gridView)
Constructor. Stores the function fct and creates an EntitySet.
Definition: AnalyticGridFunction.hpp:166
int order(AnalyticLocalFunction< Sig, LC, F > const &lf)
Definition: AnalyticGridFunction.hpp:104