Dune Core Modules (2.9.0)

analyticgridviewfunction.hh
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_FUNCTIONS_GRIDFUNCTIONS_ANALYTICGRIDVIEWFUNCTION_HH
4 #define DUNE_FUNCTIONS_GRIDFUNCTIONS_ANALYTICGRIDVIEWFUNCTION_HH
5 
6 #include <type_traits>
7 #include <optional>
8 
10 
11 #include <dune/functions/common/signature.hh>
12 #include <dune/functions/common/defaultderivativetraits.hh>
13 #include <dune/functions/common/differentiablefunction_imp.hh>
14 #include <dune/functions/common/differentiablefunction.hh>
15 #include <dune/functions/gridfunctions/gridviewentityset.hh>
16 #include <dune/functions/gridfunctions/localderivativetraits.hh>
17 
18 
19 namespace Dune {
20 namespace Functions {
21 
22 namespace Imp {
23 
24 template<class Signature, class GV, class FLocal, template<class> class DerivativeTraits=DefaultDerivativeTraits>
25 class LocalAnalyticGridViewFunction;
26 
27 template<class Range, class LocalDomain, class GV, class F, template<class> class DerivativeTraits>
28 class LocalAnalyticGridViewFunction<Range(LocalDomain), GV, F, DerivativeTraits>
29 {
30 public:
31  using Signature = Range(LocalDomain);
32  using RawSignature = typename SignatureTraits<Signature>::RawSignature;
33  using DerivativeSignature = typename DerivativeTraits<RawSignature>::Range(LocalDomain);
34 
35  using GridView = GV;
36  using EntitySet = GridViewEntitySet<GridView, 0>;
37  using Element = typename EntitySet::Element;
38 // using Geometry = typename Element::Geometry;
39  using Geometry = typename std::decay<typename Element::Geometry>::type;
40 
41  // Use the indirection via derivativeIfImplemented to also support
42  // function types F that do not implement derivative. In this case
43  // the interface type DifferentiableFunction is using a dummy for
44  // the derivative type
45  using DerivativeDummy = DifferentiableFunction<DerivativeSignature>;
46  using GlobalRawDerivative = decltype(Imp::derivativeIfImplemented<DerivativeDummy, F>(std::declval<F>()));
47  using LocalDerivative = LocalAnalyticGridViewFunction<DerivativeSignature, GridView, GlobalRawDerivative, DerivativeTraits>;
48 
50  template<class FT, disableCopyMove<LocalAnalyticGridViewFunction, FT> = 0>
51  LocalAnalyticGridViewFunction(FT&& f) :
52  f_(std::forward<FT>(f))
53  {}
54 
56  template<class FT>
57  LocalAnalyticGridViewFunction(FT&& f, const Element& element, const std::optional<Geometry>& geometry) :
58  f_(std::forward<FT>(f)),
59  element_(element),
60  geometry_(geometry)
61  {}
62 
63 
72  void bind(const Element& element)
73  {
74  element_ = element;
75  geometry_.emplace(element_.geometry());
76  }
77 
79  void unbind()
80  {
81  geometry_.reset();
82  }
83 
86  bool bound() const
87  {
88  return static_cast<bool>(geometry_);
89  }
90 
100  Range operator()(const LocalDomain& x) const
101  {
102  assert(!!geometry_);
103  return f_(geometry_->global(x));
104  }
105 
107  const Element& localContext() const
108  {
109  assert(!!geometry_);
110  return element_;
111  }
112 
121  friend LocalDerivative derivative(const LocalAnalyticGridViewFunction& t)
122  {
123  return LocalDerivative(Imp::derivativeIfImplemented<DerivativeDummy, F>(t.f_), t.element_, t.geometry_);
124  }
125 
126 private:
127  F f_;
128  Element element_;
129  std::optional<Geometry> geometry_ = std::nullopt;
130 };
131 
132 } // end namespace Imp
133 
134 
135 
136 
137 template<class Signature, class GV, class F, template<class> class DerivativeTraits=DefaultDerivativeTraits>
138 class AnalyticGridViewFunction;
139 
140 
146 template<class Range, class Domain, class GV, class F, template<class> class DerivativeTraits>
147 class AnalyticGridViewFunction<Range(Domain), GV, F, DerivativeTraits>
148 {
149 public:
150  using Signature = Range(Domain);
151  using RawSignature = typename SignatureTraits<Signature>::RawSignature;
152  using DerivativeSignature = typename DerivativeTraits<RawSignature>::Range(Domain);
153 
154  using GridView = GV;
156  using Element = typename EntitySet::Element;
157  using Geometry = typename Element::Geometry;
158 
159  // Use the indirection via derivativeIfImplemented to also support
160  // function types F that do not implement derivative. In this case
161  // the interface type DifferentiableFunction is used a dummy for
162  // the derivative type
163  using DerivativeDummy = DifferentiableFunction<DerivativeSignature>;
164  using GlobalRawDerivative = decltype(Imp::derivativeIfImplemented<DerivativeDummy, F>(std::declval<F>()));
165  using Derivative = AnalyticGridViewFunction<DerivativeSignature, GridView, GlobalRawDerivative, DerivativeTraits>;
166 
167  using LocalDomain = typename EntitySet::LocalCoordinate;
168  using LocalFunction = typename Imp::LocalAnalyticGridViewFunction<Range(LocalDomain), GridView, F, LocalDerivativeTraits<EntitySet, DerivativeTraits>::template Traits>;
169 
171  template<class FT>
172  AnalyticGridViewFunction(FT&& f, const GridView& gridView) :
173  f_(std::forward<FT>(f)),
174  entitySet_(gridView)
175  {}
176 
178  Range operator()(const Domain& x) const
179  {
180  return f_(x);
181  }
182 
184  friend Derivative derivative(const AnalyticGridViewFunction& t)
185  {
186  return Derivative(Imp::derivativeIfImplemented<DerivativeDummy, F>(t.f_), t.entitySet_.gridView());
187  }
188 
190  friend LocalFunction localFunction(const AnalyticGridViewFunction& t)
191  {
192  return LocalFunction(t.f_);
193  }
194 
196  const EntitySet& entitySet() const
197  {
198  return entitySet_;
199  }
200 
201 private:
202  F f_;
203  EntitySet entitySet_;
204 };
205 
206 
207 
224 template<class F, class GridView>
225 AnalyticGridViewFunction<
226  typename std::invoke_result<F, typename GridView::template Codim<0>::Geometry::GlobalCoordinate>::type // Range
227  (typename GridView::template Codim<0>::Geometry::GlobalCoordinate), // Domain
228  GridView,
229  typename std::decay<F>::type > // Raw type of F (without & or &&)
230  makeAnalyticGridViewFunction(F&& f, const GridView& gridView)
231 {
232  using Domain = typename GridView::template Codim<0>::Geometry::GlobalCoordinate;
233  using Range = typename std::invoke_result<F, Domain>::type;
234  using FRaw = typename std::decay<F>::type;
235 
236  return AnalyticGridViewFunction<Range(Domain), GridView, FRaw>(std::forward<F>(f), gridView);
237 }
238 
239 
240 
241 }} // namespace Dune::Functions
242 
243 
244 
245 #endif // DUNE_FUNCTIONS_GRIDFUNCTIONS_ANALYTICGRIDVIEWFUNCTION_HH
Range operator()(const Domain &x) const
Evaluate the wrapped function f directly in global coordinates x.
Definition: analyticgridviewfunction.hh:178
friend Derivative derivative(const AnalyticGridViewFunction &t)
Create a derivative grid-function by wrapping the derivative of f.
Definition: analyticgridviewfunction.hh:184
AnalyticGridViewFunction(FT &&f, const GridView &gridView)
Create the grid-function by wrapping a function f and create a GridViewEntitySet.
Definition: analyticgridviewfunction.hh:172
friend LocalFunction localFunction(const AnalyticGridViewFunction &t)
Construct the associated local-function.
Definition: analyticgridviewfunction.hh:190
const EntitySet & entitySet() const
Return the set of entities this local-function can be bound to.
Definition: analyticgridviewfunction.hh:196
GridView::template Codim< codim >::Entity Element
Type of Elements contained in this EntitySet.
Definition: gridviewentityset.hh:32
Element::Geometry::LocalCoordinate LocalCoordinate
Type of local coordinates with respect to the Element.
Definition: gridviewentityset.hh:35
Grid view abstract base class.
Definition: gridview.hh:66
TrigonometricFunction< K, -cosFactor, sinFactor > derivative(const TrigonometricFunction< K, sinFactor, cosFactor > &f)
Obtain derivative of TrigonometricFunction function.
Definition: trigonometricfunction.hh:39
AnalyticGridViewFunction< typename std::invoke_result< F, typename GridView::template Codim< 0 >::Geometry::GlobalCoordinate >::type(typename GridView::template Codim< 0 >::Geometry::GlobalCoordinate), GridView, typename std::decay< F >::type > makeAnalyticGridViewFunction(F &&f, const GridView &gridView)
Create an AnalyticGridViewFunction from a function and a grid view.
Definition: analyticgridviewfunction.hh:230
Dune namespace.
Definition: alignedallocator.hh:13
Static tag representing a codimension.
Definition: dimension.hh:24
Derivative traits for local functions.
Definition: localderivativetraits.hh:28
Helper class to deduce the signature of a callable.
Definition: signature.hh:56
Utilities for type computations, constraining overloads, ...
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 4, 22:30, 2024)