dune-functions 2.9.0
analyticgridviewfunction.hh
Go to the documentation of this file.
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
9#include <dune/common/typeutilities.hh>
10
17
18
19namespace Dune {
20namespace Functions {
21
22namespace Imp {
23
24template<class Signature, class GV, class FLocal, template<class> class DerivativeTraits=DefaultDerivativeTraits>
25class LocalAnalyticGridViewFunction;
26
27template<class Range, class LocalDomain, class GV, class F, template<class> class DerivativeTraits>
28class LocalAnalyticGridViewFunction<Range(LocalDomain), GV, F, DerivativeTraits>
29{
30public:
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
126private:
127 F f_;
128 Element element_;
129 std::optional<Geometry> geometry_ = std::nullopt;
130};
131
132} // end namespace Imp
133
134
135
136
137template<class Signature, class GV, class F, template<class> class DerivativeTraits=DefaultDerivativeTraits>
139
140
146template<class Range, class Domain, class GV, class F, template<class> class DerivativeTraits>
147class AnalyticGridViewFunction<Range(Domain), GV, F, DerivativeTraits>
148{
149public:
150 using Signature = Range(Domain);
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
164 using GlobalRawDerivative = decltype(Imp::derivativeIfImplemented<DerivativeDummy, F>(std::declval<F>()));
166
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
185 {
186 return Derivative(Imp::derivativeIfImplemented<DerivativeDummy, F>(t.f_), t.entitySet_.gridView());
187 }
188
191 {
192 return LocalFunction(t.f_);
193 }
194
196 const EntitySet& entitySet() const
197 {
198 return entitySet_;
199 }
200
201private:
202 F f_;
203 EntitySet entitySet_;
204};
205
206
207
224template<class F, class GridView>
225AnalyticGridViewFunction<
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
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
Definition: polynomial.hh:10
Definition: differentiablefunction.hh:29
Helper class to deduce the signature of a callable.
Definition: signature.hh:56
Definition: analyticgridviewfunction.hh:138
Range operator()(const Domain &x) const
Evaluate the wrapped function f directly in global coordinates x.
Definition: analyticgridviewfunction.hh:178
typename EntitySet::LocalCoordinate LocalDomain
Definition: analyticgridviewfunction.hh:167
friend Derivative derivative(const AnalyticGridViewFunction &t)
Create a derivative grid-function by wrapping the derivative of f.
Definition: analyticgridviewfunction.hh:184
typename Element::Geometry Geometry
Definition: analyticgridviewfunction.hh:157
typename EntitySet::Element Element
Definition: analyticgridviewfunction.hh:156
decltype(Imp::derivativeIfImplemented< DerivativeDummy, F >(std::declval< F >())) GlobalRawDerivative
Definition: analyticgridviewfunction.hh:164
Range(Domain) Signature
Definition: analyticgridviewfunction.hh:150
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
typename Imp::LocalAnalyticGridViewFunction< Range(LocalDomain), GridView, F, LocalDerivativeTraits< EntitySet, DerivativeTraits >::template Traits > LocalFunction
Definition: analyticgridviewfunction.hh:168
typename SignatureTraits< Signature >::RawSignature RawSignature
Definition: analyticgridviewfunction.hh:151
const EntitySet & entitySet() const
Return the set of entities this local-function can be bound to.
Definition: analyticgridviewfunction.hh:196
typename DerivativeTraits< RawSignature >::Range(Domain) DerivativeSignature
Definition: analyticgridviewfunction.hh:152
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
Derivative traits for local functions.
Definition: localderivativetraits.hh:28