DUNE PDELab (git)

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
4// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file AUTHORS.md
5// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
6
7#ifndef DUNE_FUNCTIONS_GRIDFUNCTIONS_ANALYTICGRIDVIEWFUNCTION_HH
8#define DUNE_FUNCTIONS_GRIDFUNCTIONS_ANALYTICGRIDVIEWFUNCTION_HH
9
10#include <type_traits>
11#include <optional>
12
13#include <dune/common/copyableoptional.hh>
15
16#include <dune/functions/common/signature.hh>
17#include <dune/functions/common/defaultderivativetraits.hh>
18#include <dune/functions/common/differentiablefunction_imp.hh>
19#include <dune/functions/common/differentiablefunction.hh>
20#include <dune/functions/gridfunctions/gridviewentityset.hh>
21#include <dune/functions/gridfunctions/localderivativetraits.hh>
22
23
24namespace Dune {
25namespace Functions {
26
27namespace Imp {
28
29template<class Signature, class GV, class FLocal, template<class> class DerivativeTraits=DefaultDerivativeTraits>
30class LocalAnalyticGridViewFunction;
31
32template<class Range, class LocalDomain, class GV, class F, template<class> class DerivativeTraits>
33class LocalAnalyticGridViewFunction<Range(LocalDomain), GV, F, DerivativeTraits>
34{
35public:
36 using Signature = Range(LocalDomain);
37 using RawSignature = typename SignatureTraits<Signature>::RawSignature;
38 using DerivativeSignature = typename DerivativeTraits<RawSignature>::Range(LocalDomain);
39
40 using GridView = GV;
41 using EntitySet = GridViewEntitySet<GridView, 0>;
42 using Element = typename EntitySet::Element;
43// using Geometry = typename Element::Geometry;
44 using Geometry = std::decay_t<typename Element::Geometry>;
45
46 // Use the indirection via derivativeIfImplemented to also support
47 // function types F that do not implement derivative. In this case
48 // the interface type DifferentiableFunction is using a dummy for
49 // the derivative type
50 using DerivativeDummy = DifferentiableFunction<DerivativeSignature>;
51 using GlobalRawDerivative = decltype(Imp::derivativeIfImplemented<DerivativeDummy, F>(std::declval<F>()));
52 using LocalDerivative = LocalAnalyticGridViewFunction<DerivativeSignature, GridView, GlobalRawDerivative, DerivativeTraits>;
53
55 template<class FT, disableCopyMove<LocalAnalyticGridViewFunction, FT> = 0>
56 LocalAnalyticGridViewFunction(FT&& f) :
57 f_(std::forward<FT>(f))
58 {}
59
61 template<class FT>
62 LocalAnalyticGridViewFunction(FT&& f, const Element& element, const std::optional<Geometry>& geometry) :
63 f_(std::forward<FT>(f)),
64 element_(element),
65 geometry_(geometry)
66 {}
67
68
77 void bind(const Element& element)
78 {
79 element_ = element;
80 geometry_.emplace(element_.geometry());
81 }
82
84 void unbind()
85 {
86 geometry_.reset();
87 }
88
91 bool bound() const
92 {
93 return static_cast<bool>(geometry_);
94 }
95
105 Range operator()(const LocalDomain& x) const
106 {
107 assert(!!geometry_);
108 return (*f_)(geometry_->global(x));
109 }
110
112 const Element& localContext() const
113 {
114 assert(!!geometry_);
115 return element_;
116 }
117
126 friend LocalDerivative derivative(const LocalAnalyticGridViewFunction& t)
127 {
128 return LocalDerivative(Imp::derivativeIfImplemented<DerivativeDummy, F>(*t.f_), t.element_, t.geometry_);
129 }
130
131private:
132 // Wrap the function into CopyableOptional to make it copy-assignable
133 CopyableOptional<F> f_;
134 Element element_;
135 std::optional<Geometry> geometry_ = std::nullopt;
136};
137
138} // end namespace Imp
139
140
141
142
143template<class Signature, class GV, class F, template<class> class DerivativeTraits=DefaultDerivativeTraits>
144class AnalyticGridViewFunction;
145
146
152template<class Range, class Domain, class GV, class F, template<class> class DerivativeTraits>
153class AnalyticGridViewFunction<Range(Domain), GV, F, DerivativeTraits>
154{
155public:
156 using Signature = Range(Domain);
157 using RawSignature = typename SignatureTraits<Signature>::RawSignature;
158 using DerivativeSignature = typename DerivativeTraits<RawSignature>::Range(Domain);
159
160 using GridView = GV;
162 using Element = typename EntitySet::Element;
163 using Geometry = typename Element::Geometry;
164
165 // Use the indirection via derivativeIfImplemented to also support
166 // function types F that do not implement derivative. In this case
167 // the interface type DifferentiableFunction is used a dummy for
168 // the derivative type
169 using DerivativeDummy = DifferentiableFunction<DerivativeSignature>;
170 using GlobalRawDerivative = decltype(Imp::derivativeIfImplemented<DerivativeDummy, F>(std::declval<F>()));
171 using Derivative = AnalyticGridViewFunction<DerivativeSignature, GridView, GlobalRawDerivative, DerivativeTraits>;
172
173 using LocalDomain = typename EntitySet::LocalCoordinate;
174 using LocalFunction = typename Imp::LocalAnalyticGridViewFunction<Range(LocalDomain), GridView, F, LocalDerivativeTraits<EntitySet, DerivativeTraits>::template Traits>;
175
177 template<class FT>
178 AnalyticGridViewFunction(FT&& f, const GridView& gridView) :
179 f_(std::forward<FT>(f)),
180 entitySet_(gridView)
181 {}
182
184 Range operator()(const Domain& x) const
185 {
186 return (*f_)(x);
187 }
188
190 friend Derivative derivative(const AnalyticGridViewFunction& t)
191 {
192 return Derivative(Imp::derivativeIfImplemented<DerivativeDummy, F>(*t.f_), t.entitySet_.gridView());
193 }
194
196 friend LocalFunction localFunction(const AnalyticGridViewFunction& t)
197 {
198 return LocalFunction(*t.f_);
199 }
200
202 const EntitySet& entitySet() const
203 {
204 return entitySet_;
205 }
206
207private:
208 // Wrap the function into CopyableOptional to make it copy-assignable
210 EntitySet entitySet_;
211};
212
213// deduction guides
214template<class F, class GridView,
215 class Domain = typename GridView::template Codim<0>::Geometry::GlobalCoordinate,
216 class Range = std::invoke_result_t<F,Domain>>
217AnalyticGridViewFunction(const F&, const GridView&)
218 -> AnalyticGridViewFunction<Range(Domain), GridView, F>;
219
220
237template<class F, class GridView>
238auto makeAnalyticGridViewFunction(F&& f, const GridView& gridView)
239{
240 using Domain = typename GridView::template Codim<0>::Geometry::GlobalCoordinate;
241 using Range = std::invoke_result_t<F, Domain>;
242 using FRaw = std::decay_t<F>;
243
244 return AnalyticGridViewFunction<Range(Domain), GridView, FRaw>(std::forward<F>(f), gridView);
245}
246
247
248
249}} // namespace Dune::Functions
250
251
252
253#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:184
friend Derivative derivative(const AnalyticGridViewFunction &t)
Create a derivative grid-function by wrapping the derivative of f.
Definition: analyticgridviewfunction.hh:190
AnalyticGridViewFunction(FT &&f, const GridView &gridView)
Create the grid-function by wrapping a function f and create a GridViewEntitySet.
Definition: analyticgridviewfunction.hh:178
friend LocalFunction localFunction(const AnalyticGridViewFunction &t)
Construct the associated local-function.
Definition: analyticgridviewfunction.hh:196
const EntitySet & entitySet() const
Return the set of entities this local-function can be bound to.
Definition: analyticgridviewfunction.hh:202
GridView::template Codim< codim >::Entity Element
Type of Elements contained in this EntitySet.
Definition: gridviewentityset.hh:36
Element::Geometry::LocalCoordinate LocalCoordinate
Type of local coordinates with respect to the Element.
Definition: gridviewentityset.hh:39
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:43
auto makeAnalyticGridViewFunction(F &&f, const GridView &gridView)
Create an AnalyticGridViewFunction from a function and a grid view.
Definition: analyticgridviewfunction.hh:238
Dune namespace.
Definition: alignedallocator.hh:13
STL namespace.
Static tag representing a codimension.
Definition: dimension.hh:24
Derivative traits for local functions.
Definition: localderivativetraits.hh:32
Helper class to deduce the signature of a callable.
Definition: signature.hh:60
Utilities for type computations, constraining overloads, ...
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 24, 23:30, 2024)