DUNE-FUNCTIONS (unstable)

composedgridfunction.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_COMPOSEDGRIDFUNCTION_HH
8#define DUNE_FUNCTIONS_GRIDFUNCTIONS_COMPOSEDGRIDFUNCTION_HH
9
10#include <type_traits>
11#include <tuple>
12
13#include <dune/common/referencehelper.hh>
14#include <dune/common/typeutilities.hh>
15
16#include <dune/functions/common/defaultderivativetraits.hh>
17#include <dune/functions/common/differentiablefunction.hh>
18#include <dune/functions/gridfunctions/gridfunction.hh>
19
20
21namespace Dune {
22namespace Functions {
23
24
25
44template<class OF, class... IF>
46{
47 using InnerFunctions = std::tuple<IF...>;
48 using InnerLocalFunctions = std::tuple<decltype(localFunction(resolveRef(std::declval<const IF&>())))...>;
49
50 template<std::size_t i>
51 using InnerFunction = std::decay_t<ResolveRef_t<std::tuple_element_t<i, InnerFunctions>>>;
52
53 using OuterFunction = OF;
54
55public:
56
57 using EntitySet = typename InnerFunction<0>::EntitySet;
58 using Element = typename EntitySet::Element;
59
60 using Domain = typename EntitySet::GlobalCoordinate;
61 using LocalDomain = typename EntitySet::LocalCoordinate;
62
63 using Range = decltype(std::declval<OF>()(std::declval<IF>()(std::declval<Domain>())...));
64
65private:
66
67 using Traits = Imp::GridFunctionTraits<Range(Domain), EntitySet, DefaultDerivativeTraits, 16>;
68
69 class LocalFunction
70 {
71 public:
78 LocalFunction(const ComposedGridFunction& globalFunction) :
79 globalFunction_(globalFunction),
80 innerLocalFunctions_(globalFunction.innerLocalFunctions())
81 {}
82
92 void bind(const Element& element)
93 {
94 std::apply([&](auto&... innerFunction) {
95 (innerFunction.bind(element),...);
96 }, innerLocalFunctions_);
97 }
98
100 void unbind()
101 {
102 std::apply([&](auto&... innerFunction) {
103 (innerFunction.unbind(),...);
104 }, innerLocalFunctions_);
105 }
106
109 bool bound() const
110 {
111 return std::apply([](const auto&... innerFunction) {
112 return (innerFunction.bound() && ...);
113 }, innerLocalFunctions_);
114 }
115
125 Range operator()(const LocalDomain& x) const
126 {
127 return std::apply([&](const auto&... innerFunction) {
128 return globalFunction_.outerFunction_(innerFunction(x)...);
129 }, innerLocalFunctions_);
130 }
131
141 const Element& localContext() const
142 {
143 return std::get<0>(innerLocalFunctions_).localContext();
144 }
145
147 friend typename Traits::LocalFunctionTraits::DerivativeInterface derivative(const LocalFunction& t)
148 {
149 DUNE_THROW(NotImplemented,"not implemented");
150 }
151
152 private:
153 const ComposedGridFunction& globalFunction_;
154 InnerLocalFunctions innerLocalFunctions_;
155 };
156
157public:
158
168 template<class OFT, class... IFT,
169 disableCopyMove<ComposedGridFunction, OFT> = 0,
170 std::enable_if_t<(sizeof...(IFT) > 0), int> = 0>
171 ComposedGridFunction(OFT&& outerFunction, IFT&&... innerFunctions) :
172 outerFunction_(std::forward<OFT>(outerFunction)),
173 innerFunctions_(std::forward<IFT>(innerFunctions)...)
174 {}
175
177 Range operator()(const Domain& x) const
178 {
179 return std::apply([&](const auto&... innerFunction) {
180 return outerFunction_(innerFunction(x)...);
181 }, innerFunctions_);
182 }
183
185 friend typename Traits::DerivativeInterface derivative(const ComposedGridFunction& t)
186 {
187 DUNE_THROW(NotImplemented,"not implemented");
188 }
189
196 friend LocalFunction localFunction(const ComposedGridFunction& cgf)
197 {
198 return LocalFunction(cgf);
199 }
200
211 const EntitySet& entitySet() const
212 {
213 return resolveRef(std::get<0>(innerFunctions_)).entitySet();
214 }
215
216protected:
217
218 InnerLocalFunctions innerLocalFunctions() const
219 {
220 return std::apply([&](const auto&... innerFunction) {
221 return std::make_tuple(localFunction(resolveRef(innerFunction))...);
222 }, innerFunctions_);
223 }
224
225 OuterFunction outerFunction_;
226 InnerFunctions innerFunctions_;
227};
228
229// deduction guides
230template<class OF, class... IF>
231ComposedGridFunction(const OF&, const IF&...)
232 -> ComposedGridFunction<OF,IF...>;
233
234
257template<class OF, class... IF>
258auto makeComposedGridFunction(OF&& outerFunction, IF&&... innerFunction)
259{
260 using ComposedGridFunctionType = ComposedGridFunction<std::decay_t<OF>, std::decay_t<IF>...>;
261 return ComposedGridFunctionType(std::forward<OF>(outerFunction), std::forward<IF>(innerFunction)...);
262}
263
264
265
266}} // namespace Dune::Functions
267
268
269
270#endif // DUNE_FUNCTIONS_GRIDFUNCTIONS_COMPOSEDGRIDFUNCTION_HH
Composition of grid functions with another function.
Definition: composedgridfunction.hh:46
ComposedGridFunction(OFT &&outerFunction, IFT &&... innerFunctions)
Create ComposedGridFunction.
Definition: composedgridfunction.hh:171
const EntitySet & entitySet() const
Return the EntitySet associated to this composed grid-function.
Definition: composedgridfunction.hh:211
friend LocalFunction localFunction(const ComposedGridFunction &cgf)
Create a local-function of this composed grid-function.
Definition: composedgridfunction.hh:196
Range operator()(const Domain &x) const
Evaluation of the composed grid function in coordinates x
Definition: composedgridfunction.hh:177
friend Traits::DerivativeInterface derivative(const ComposedGridFunction &t)
Not implemented.
Definition: composedgridfunction.hh:185
auto makeComposedGridFunction(OF &&outerFunction, IF &&... innerFunction)
Create a ComposedGridFunction that composes grid-functions with another function.
Definition: composedgridfunction.hh:258
Definition: polynomial.hh:17
Default implementation for derivative traits.
Definition: defaultderivativetraits.hh:41
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Aug 13, 22:30, 2024)