Dune-Fufem 2.12-git
Loading...
Searching...
No Matches
productoperator.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
4// SPDX-FileCopyrightText: Copyright © DUNE-FUFEM 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_FUFEM_FORMS_PRODUCTOPERATOR_HH
8#define DUNE_FUFEM_FORMS_PRODUCTOPERATOR_HH
9
10#include <cstddef>
11#include <functional>
12#include <tuple>
13#include <type_traits>
14#include <utility>
15#include <vector>
16
18
22
23
24
25namespace Dune::Fufem::Forms::Impl {
26
27 template<class Operator0, class Operator1>
28 struct ProductOperatorBaseTraits
29 {
30 static auto baseType() {
31 if constexpr ((Operator0::arity==1) and (Operator1::arity==0))
33 else if constexpr ((Operator0::arity==0) and (Operator1::arity==1))
35 else
37 }
38 using type = std::decay_t<decltype(baseType())>;
39 };
40
41
42} // namespace Dune::Fufem::Forms::Impl
43
44
45
46namespace Dune::Fufem::Forms {
47
48
49
58 template<class P, class Operator0, class Operator1>
59 class ProductOperator : public Impl::ProductOperatorBaseTraits<Operator0, Operator1>::type
60 {
61 using Contraction = P;
62 using LocalOperator0 = decltype(localOperator(std::declval<Operator0>()));
63 using LocalOperator1 = decltype(localOperator(std::declval<Operator1>()));
64
65 public:
66
67 using Element = typename Operator0::Element;
69
70 ProductOperator(const Contraction& product, const Operator0& operator0, const Operator1& operator1) :
71 product_(product),
72 operator0_(operator0),
73 operator1_(operator1)
74 {
75 static_assert(isOperator_v<Operator0>, "ProductOperator: First factor must be an operator.");
76 static_assert(isOperator_v<Operator1>, "ProductOperator: Second factor must be an operator.");
77 static_assert(Operator0::arity+Operator1::arity <= 2, "ProductOperator: Arity sum of factors must not exceed 2");
78 if constexpr((Operator0::arity==1) and (Operator1::arity==1))
79 static_assert((Operator0::argIndex==0) and (Operator1::argIndex==1), "ProductOperator: Factors must be ordered according to argument index.");
80 }
81
83 {
85 using Op1Tensor = decltype(std::declval<LocalOperator1>()(0));
86 static constexpr bool op1IsLazy = Impl::Tensor::IsLazyTensor<Op1Tensor>::value;
87 public:
88
89 using Element = typename LocalOperator0::Element;
90 using Intersection = typename LocalOperator0::Intersection;
91 using CacheManager = typename LocalOperator0::CacheManager;
92 using Range = typename ProductOperator::Range;
93
94 LocalOperator(const Contraction& product, LocalOperator0&& localOperator0, LocalOperator1&& localOperator1):
95 product_(product),
96 localOperator0_(std::move(localOperator0)),
97 localOperator1_(std::move(localOperator1))
98 {}
99
100 auto quadratureRuleKey() const
101 {
102 return quadratureRuleKey_;
103 }
104
105 void bind(const Element& element)
106 {
107 localOperator0_.bind(element);
108 localOperator1_.bind(element);
109 quadratureRuleKey_ = localOperator0_.quadratureRuleKey().product(localOperator1_.quadratureRuleKey());
110 }
111
112 void bind(const Intersection& intersection, const Element& element, const Element& otherElement)
113 {
114 localOperator0_.bind(intersection, element, otherElement);
115 localOperator1_.bind(intersection, element, otherElement);
116 quadratureRuleKey_ = localOperator0_.quadratureRuleKey().product(localOperator1_.quadratureRuleKey());
117 }
118
119 void unbind()
120 {
121 localOperator0_.unbind();
122 localOperator1_.unbind();
123 }
124
125 template<class... LV>
126 void registerLocalViews(const LV&... lvs)
127 {
128 localOperator0_.registerLocalViews(lvs...);
129 localOperator1_.registerLocalViews(lvs...);
130 }
131
132 template<class... LV>
133 void registerOutsideLocalViews(const LV&... lvs)
134 {
135 localOperator0_.registerOutsideLocalViews(lvs...);
136 localOperator1_.registerOutsideLocalViews(lvs...);
137 }
138
139 void registerCaches(CacheManager& cacheManager)
140 {
141 localOperator0_.registerCaches(cacheManager);
142 localOperator1_.registerCaches(cacheManager);
143 }
144
146 {
147 localOperator0_.registerOutsideCaches(cacheManager);
148 localOperator1_.registerOutsideCaches(cacheManager);
149 }
150
151 template<class... OutsideCacheManager>
152 void bindToCaches(CacheManager& cacheManager, OutsideCacheManager&... outsideCacheManager)
153 {
154 localOperator0_.bindToCaches(cacheManager, outsideCacheManager...);
155 localOperator1_.bindToCaches(cacheManager, outsideCacheManager...);
156 // For a binary product cache evaluation of the second factor
157 if constexpr ((Operator0::arity==1) and (Operator1::arity==1) and (op1IsLazy))
158 {
159 const auto& quadRule = cacheManager.rule();
160 if (op1Cache_.size()<quadRule.size())
161 op1Cache_.resize(quadRule.size());
162 for (auto k : Dune::range(quadRule.size()))
163 {
164 auto y = localOperator1_(k);
165 op1Cache_[k].resize(y.nnz());
166 std::size_t l = 0;
167 sparseForEach(y, [&](const auto& y_j, auto... j) {
168 op1Cache_[k][l++] = y_j;
169 });
170 }
171 }
172 }
173
175 {
176 using namespace Impl::Tensor;
177 using namespace Dune::Indices;
178 // Be careful when changing the following code.! This is at the heart
179 // of assembling matrix entries and changing the code structure
180 // may lead to slightly different code generation in surprising and
181 // unpredictable ways and can thus have a significant performance impact.
182 // Whenever touching this one must carefully measure (using `forms-benchmark`)
183 // if this leads to performance regressions.
184 auto x = localOperator0_(index);
185 auto y = [&](){
186 if constexpr ((Operator0::arity==1) and (Operator1::arity==1) and (op1IsLazy))
187 return SparseTensor(_2, [&, y_noncached=localOperator1_(index), index](auto&& f) {
188 std::size_t l = 0;
189 sparseForEach(y_noncached, [&](const auto&, auto... j) {
190 f(op1Cache_[index][l++], j...);
191 });
192 },
193 op1Cache_[index].size(),
195 );
196 else
197 return localOperator1_(index);
198 }();
199 return interleavedOuterProduct(std::cref(product_), x, y);
200 }
201
202 private:
203 Contraction product_;
204 LocalOperator0 localOperator0_;
205 LocalOperator1 localOperator1_;
206 QuadratureRuleKey quadratureRuleKey_;
207 Op1Cache op1Cache_;
208 };
209
210 friend LocalOperator localOperator(const ProductOperator& productOperator)
211 {
212 return LocalOperator(productOperator.contraction(), localOperator(productOperator.operator0()), localOperator(productOperator.operator1()));
213 }
214
216 requires (ProductOperator::arity==0)
217 {
218 return LocalFunctionAdaptor<LocalOperator>(localOperator(productOperator));
219 }
220
221 const Operator0& operator0() const
222 {
223 return operator0_;
224 }
225
226 const Operator1& operator1() const
227 {
228 return operator1_;
229 }
230
231 const Contraction& contraction() const
232 {
233 return product_;
234 }
235
236 auto basis() const
237 {
238 return std::tuple_cat(operator0_.basis(), operator1_.basis());
239 }
240
241 auto treePath() const
242 {
243 return std::tuple_cat(operator0_.treePath(), operator1_.treePath());
244 }
245
247 template<std::size_t k>
248 requires(Operator0::arity + Operator1::arity==1)
249 auto rebindArgIndex() const
250 {
251 if constexpr(Operator0::arity==1)
252 {
253 auto reboundOperator0 = operator0_.template rebindArgIndex<k>();
254 using ReboundOperator0 = decltype(reboundOperator0);
255 return ProductOperator<P, ReboundOperator0, Operator1>(product_, reboundOperator0, operator1_);
256 }
257 else
258 {
259 auto reboundOperator1 = operator1_.template rebindArgIndex<k>();
260 using ReboundOperator1 = decltype(reboundOperator1);
261 return ProductOperator<P, Operator0, ReboundOperator1>(product_, operator0_, reboundOperator1);
262 }
263 }
264
265 private:
266 Contraction product_;
267 Operator0 operator0_;
268 Operator1 operator1_;
269 };
270
271
272
273} // namespace Dune::Fufem::Forms
274
275#endif // DUNE_FUFEM_FORMS_PRODUCTOPERATOR_HH
static constexpr IntegralRange< T > range(T from, T to) noexcept
std::ptrdiff_t index() const
Adaptor for turning a Fufem::Forms LocalOperator into a LocalFunction.
Definition localfunctionadaptor.hh:43
Base class for multilinear operator implementations.
Definition baseclass.hh:73
typename LocalOperator0::Intersection Intersection
Definition productoperator.hh:90
void registerOutsideLocalViews(const LV &... lvs)
Definition productoperator.hh:133
auto operator()(std::size_t index) const
Definition productoperator.hh:174
void bind(const Element &element)
Definition productoperator.hh:105
typename ProductOperator::Range Range
Definition productoperator.hh:92
void registerCaches(CacheManager &cacheManager)
Definition productoperator.hh:139
typename LocalOperator0::CacheManager CacheManager
Definition productoperator.hh:91
void registerOutsideCaches(CacheManager &cacheManager)
Definition productoperator.hh:145
typename LocalOperator0::Element Element
Definition productoperator.hh:89
void bind(const Intersection &intersection, const Element &element, const Element &otherElement)
Definition productoperator.hh:112
auto quadratureRuleKey() const
Definition productoperator.hh:100
void registerLocalViews(const LV &... lvs)
Definition productoperator.hh:126
void bindToCaches(CacheManager &cacheManager, OutsideCacheManager &... outsideCacheManager)
Definition productoperator.hh:152
LocalOperator(const Contraction &product, LocalOperator0 &&localOperator0, LocalOperator1 &&localOperator1)
Definition productoperator.hh:94
void unbind()
Definition productoperator.hh:119
Generic product of two multilinear operators.
Definition productoperator.hh:60
const Operator1 & operator1() const
Definition productoperator.hh:226
ProductOperator(const Contraction &product, const Operator0 &operator0, const Operator1 &operator1)
Definition productoperator.hh:70
friend LocalOperator localOperator(const ProductOperator &productOperator)
Definition productoperator.hh:210
decltype(std::declval< Contraction >()(std::declval< typename Operator0::Range >(), std::declval< typename Operator1::Range >())) Range
Definition productoperator.hh:68
const Operator0 & operator0() const
Definition productoperator.hh:221
auto basis() const
Definition productoperator.hh:236
const Contraction & contraction() const
Definition productoperator.hh:231
auto treePath() const
Definition productoperator.hh:241
friend LocalFunctionAdaptor< LocalOperator > localFunction(const ProductOperator &productOperator)
Definition productoperator.hh:215
auto rebindArgIndex() const
Get a copy of this operator bound to another argument.
Definition productoperator.hh:249
typename Operator0::Element Element
Definition productoperator.hh:67
Base class for unary multilinear operator implementations.
Definition baseclass.hh:91
A token that specifies a quadrature rule.
Definition quadraturerulecache.hh:40
T forward(T... args)
auto product(const Op &op, const L &l, const R &r)
Generic exterior product of two multilinear operators.
Definition userfunctions.hh:401
Definition baseclass.hh:22
STL namespace.
T cref(T... args)
T resize(T... args)
T size(T... args)
T tuple_cat(T... args)