AMDiS 2.11-git
The Adaptive Multi-Dimensional Simulation Toolbox
 
Loading...
Searching...
No Matches
ComposerVectorGridFunction.hpp
1#pragma once
2
3#include <tuple>
4#include <type_traits>
5
6#include <amdis/Operations.hpp>
7#include <amdis/algorithm/Map.hpp>
8#include <amdis/common/ForEach.hpp>
9#include <amdis/common/Logical.hpp>
10#include <amdis/common/Order.hpp>
11#include <amdis/common/TypeTraits.hpp>
12#include <amdis/gridfunctions/Derivative.hpp>
13#include <amdis/gridfunctions/GridFunction.hpp>
14
15namespace AMDiS
16{
17#ifndef DOXYGEN
18 template <class Signatur, class Element, class Functor, class LocalFunctions>
20#endif
21
22 // implementation
23 template <class R, class D, class E, class Functor, class LocalFct>
24 class ComposerVectorLocalFunction<R(D), E, Functor, LocalFct>
25 {
26 public:
27 using Range = R;
28 using Domain = D;
29 using Element = E;
30
31 enum { hasDerivative = true };
32
33 private:
34 using LocalFctRange = TYPEOF( std::declval<LocalFct>()(std::declval<Domain>()) );
35
36 public:
38 ComposerVectorLocalFunction(Functor fct, std::vector<LocalFct> localFcts)
39 : fct_(std::move(fct))
40 , localFcts_(std::move(localFcts))
41 {}
42
44 void bind(Element const& element)
45 {
46 for (auto& lf : localFcts_)
47 lf.bind(element);
48 }
49
51 void unbind()
52 {
53 for (auto& lf : localFcts_)
54 lf.unbind();
55 }
56
58 bool bound() const
59 {
60 return localFcts_[0].bound();
61 }
62
64 Range operator()(Domain const& x) const
65 {
66 return fct_(Recursive::map([&](auto const& lf) { return lf(x); }, localFcts_));
67 }
68
70 Element const& localContext() const
71 {
72 return localFcts_[0].localContext();
73 }
74
75
76 public:
78 Functor const& fct() const
79 {
80 return fct_;
81 }
82
83 auto const& localFunctions() const
84 {
85 return localFcts_;
86 }
87
88 private:
89 Functor fct_;
90 std::vector<LocalFct> localFcts_;
91 };
92
93 template <class Element, class Functor, class LocalFct>
94 auto makeComposerVectorLocalFunction(Functor const& f, std::vector<LocalFct> lfs)
95 {
96 using D = typename Element::Geometry::LocalCoordinate;
97 using LocalFctRange = TYPEOF(std::declval<LocalFct>()(std::declval<D>()));
98 using R = TYPEOF(f(std::vector<LocalFctRange>{}));
99 return ComposerLocalFunction<R(D), Element, Functor, LocalFct>{f, std::move(lfs)};
100 }
101
102 namespace Operation
103 {
110 {
111 template <class T>
112 T operator()(std::vector<T> const& ts) const
113 {
114 return std::accumulate(ts.begin(),ts.end(),T(0));
115 }
116 };
117
118 inline int order(PlusVector const&, std::vector<int> const& orders)
119 {
120 return *std::max_element(orders.begin(), orders.end());
121 }
122
123 inline auto partial(PlusVector const&, std::size_t i)
124 {
125 return One{};
126 }
127
129
130 } // end namespace Operation
131
135
141 template <class Sig, class E, class F, class LF, class Type,
142 REQUIRES(Concepts::HasPartial<F>)>
143 auto derivativeOf(ComposerVectorLocalFunction<Sig,E,F,LF> const& composed, Type const& type)
144 {
145 // d_i(f)[lgfs...] * lgfs_i
146 auto term_i = [&](std::size_t i)
147 {
148 auto di_f = makeComposerVectorLocalFunction<E>(partial(composed.fct(), i),
149 composed.localFunctions());
150 auto df = makeComposerLocalFunction<E>(Operation::Multiplies{}, di_f,
151 derivativeOf(composed.localFunctions()[i], type));
152 if (composed.bound())
153 df.bind(composed.localContext());
154 return df;
155 };
156
157 // sum_i [ d_i(f)[lgfs...] * derivativeOf(lgfs_i)
158 std::vector<TYPEOF(term_i(0))> terms;
159 for (std::size_t i = 0; i < composed.localFunctions().size(); ++i)
160 terms.emplace_back(term_i(i));
161 return makeComposerVectorLocalFunction<E>(Operation::PlusVector{}, terms);
162 }
163
164
168
173 template <class Sig, class E, class F, class LF,
174 REQUIRES(Concepts::HasFunctorVectorOrder<F> && Concepts::Polynomial<LF>)>
175 int order(ComposerVectorLocalFunction<Sig,E,F,LF> const& composed)
176 {
177 return order(composed.fct(),
178 Recursive::map([](auto const& lf) { return order(lf); }, composed.localFunctions()));
179 }
180
181
184
200 template <class Sig, class EntitySet, class Functor, class GridFct>
202
203 template <class R, class D, class ES, class Functor, class GridFct>
204 class ComposerVectorGridFunction<R(D), ES, Functor, GridFct>
205 {
206 public:
207 using Range = R;
208 using Domain = D;
209 using EntitySet = ES;
210
211 enum { hasDerivative = false };
212
213 private:
214 using LocalFct = TYPEOF( localFunction(std::declval<GridFct const&>()) );
215 using LocalDomain = typename EntitySet::LocalCoordinate;
216 using Element = typename EntitySet::Element;
217
218 public:
219 using LocalFunction
220 = ComposerVectorLocalFunction<Range(LocalDomain), Element, Functor, LocalFct>;
221
223 ComposerVectorGridFunction(EntitySet const& entitySet, Functor fct, std::vector<GridFct> gridFcts)
224 : entitySet_(entitySet)
225 , fct_(std::move(fct))
226 , gridFcts_(std::move(gridFcts))
227 {}
228
230 Range operator()(Domain const& x) const
231 {
232 return fct_(Recursive::map([&](auto const& gf) { return gf(x); }, gridFcts_));
233 }
234
236 EntitySet const& entitySet() const
237 {
238 return entitySet_;
239 }
240
243 {
244 return LocalFunction{fct_,
245 Recursive::map([](auto const& gf) { return localFunction(gf); }, gridFcts_)};
246 }
247
248 private:
249 EntitySet entitySet_;
250 Functor fct_;
251 std::vector<GridFct> gridFcts_;
252 };
253
254
255 // Generator function for ComposerGridFunction expressions
256 template <class Functor, class GridView, class GridFct>
257 auto makeComposerVectorGridFunction(Functor const& f, GridView const& gridView,
258 std::vector<GridFct> gridFcts)
259 {
260 static_assert((Concepts::GridFunction<GridFct>),
261 "All passed parameters must be GridFunctions.");
262
263 using EntitySet = Dune::Functions::GridViewEntitySet<GridView, 0>;
264 using Domain = typename EntitySet::GlobalCoordinate;
265 using GridFctRange = TYPEOF( std::declval<GridFct>()(std::declval<Domain>()) );
266
267 static_assert(Concepts::Callable<Functor, std::vector<GridFctRange>>,
268 "Range types of grid functions are not compatible with the functor.");
269 using Range = TYPEOF(f(std::vector<GridFctRange>{}));
270
271 using FGF = ComposerVectorGridFunction<Range(Domain), EntitySet, Functor, GridFct>;
272 return FGF{EntitySet{gridView},f, std::move(gridFcts)};
273 }
274
275
276#ifndef DOXYGEN
277 // PreGridFunction related to ComposerGridFunction.
278 template <class Functor, class PreGridFct>
280 {
282
283 struct Creator
284 {
285 template <class GridView>
286 static auto create(Self const& self, GridView const& gridView)
287 {
288 return makeComposerVectorGridFunction(self.fct_, gridView,
289 Recursive::map([&](auto const& pgf) { return makeGridFunction(pgf, gridView); },
290 self.preGridFcts_));
291 }
292 };
293
294 ComposerVectorPreGridFunction(Functor fct, std::vector<PreGridFct> pgfs)
295 : fct_(std::move(fct))
296 , preGridFcts_(std::move(pgfs))
297 {}
298
299 private:
300 Functor fct_;
301 std::vector<PreGridFct> preGridFcts_;
302 };
303
304 namespace Traits
305 {
306 template <class Functor, class PreGridFct>
308 : std::true_type {};
309 }
310#endif
311
312
315
323 template <class Functor, class PreGridFct>
324 auto invokeVectorAtQP(Functor f, std::vector<PreGridFct> gridFcts)
325 {
326 return ComposerVectorPreGridFunction<Functor, PreGridFct>{std::move(f), std::move(gridFcts)};
327 }
328
329} // end namespace AMDiS
LocalFunction makeLocalFunction() const
Create the localFunction by composition of the inner localFunctions.
Definition ComposerVectorGridFunction.hpp:242
Range operator()(Domain const &x) const
Applies the functor to the evaluated gridfunctions.
Definition ComposerVectorGridFunction.hpp:230
ComposerVectorGridFunction(EntitySet const &entitySet, Functor fct, std::vector< GridFct > gridFcts)
Constructor. Stores copies of the functor and gridfunctions.
Definition ComposerVectorGridFunction.hpp:223
EntitySet const & entitySet() const
Return the stored EntitySet of the first GridFunction.
Definition ComposerVectorGridFunction.hpp:236
Definition ComposerVectorGridFunction.hpp:201
bool bound() const
Check whether the LocalFunction is bound to an element.
Definition ComposerVectorGridFunction.hpp:58
ComposerVectorLocalFunction(Functor fct, std::vector< LocalFct > localFcts)
Constructor. Stores copies of the functor and localFunction(gridfunction)s.
Definition ComposerVectorGridFunction.hpp:38
Range operator()(Domain const &x) const
Applies the functor fct_ to the evaluated localFunctions.
Definition ComposerVectorGridFunction.hpp:64
Functor const & fct() const
Return the stored functor.
Definition ComposerVectorGridFunction.hpp:78
void bind(Element const &element)
Calls bind for all localFunctions.
Definition ComposerVectorGridFunction.hpp:44
Element const & localContext() const
Get the element this localfunction (and all inner localfunctions) are bound to.
Definition ComposerVectorGridFunction.hpp:70
void unbind()
Calls unbind for all localFunctions.
Definition ComposerVectorGridFunction.hpp:51
Definition ComposerVectorGridFunction.hpp:19
constexpr bool Functor
A Functor is a function F with signature Signature.
Definition Concepts.hpp:133
auto invokeVectorAtQP(Functor f, std::vector< PreGridFct > gridFcts)
Generator function for ComposerVectorPreGridFunction.
Definition ComposerVectorGridFunction.hpp:324
Definition ComposerVectorGridFunction.hpp:284
Definition ComposerVectorGridFunction.hpp:280
Functor that represents A+B.
Definition ComposerVectorGridFunction.hpp:110
Definition GridFunction.hpp:28