Dune-Fufem 2.12-git
Loading...
Searching...
No Matches
boundunaryoperator.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_BOUNDUNARYOPERATOR_HH
8#define DUNE_FUFEM_FORMS_BOUNDUNARYOPERATOR_HH
9
10#include <cstddef>
11#include <type_traits>
12#include <tuple>
13#include <utility>
14
20
22#include <dune/functions/backends/concepts.hh>
24
30
31
32
33namespace Dune::Fufem::Forms {
34
35
54 template<class Op, class V>
56 {
57 using UnaryOperator = Op;
58 using LocalUnaryOperator = decltype(localOperator(std::declval<UnaryOperator>()));
59 using Tree = typename std::decay_t<decltype(std::get<0>(std::declval<UnaryOperator>().basis()))>::LocalView::Tree;
60 using NodeFlags = typename TypeTree::UniformTreeContainer<char, Tree>;
61
62 template<class OP, class Func>
63 static void forEachOperator(OP&& op, Func&& f)
64 {
65 if constexpr (isOperator_v<UnaryOperator>)
66 f(op);
67 else if constexpr (isSumOperator_v<UnaryOperator>)
68 Impl::forEachTupleEntry(op.operators(), f);
69 }
70
74
75 public:
76
77 // Types present in DiscreteGlobalBasisFunctionBase
79 using Vector = V;
81 using GridView = typename Basis::GridView;
83 using Domain = typename EntitySet::GlobalCoordinate;
84 using LocalDomain = typename EntitySet::LocalCoordinate;
85
86 // Types required by Operator interface
87 using Element = typename EntitySet::Element;
89
90 BoundUnaryOperator(const UnaryOperator& unaryOperator, const Vector& coefficients) :
91 cacheId_(UniqueCacheId::makeUniqueCacheId()),
92 unaryOperator_(unaryOperator),
94 entitySet_(std::get<0>(unaryOperator.basis()).gridView())
95 {
96 static_assert(isOperatorOrSumOperator_v<UnaryOperator>, "The type passed to BoundUnaryOperator is not a operator.");
97 static_assert(UnaryOperator::arity==1, "The operator passed to BoundUnaryOperator is not unary.");
98 }
99
101 cacheId_(UniqueCacheId::makeUniqueCacheId()),
102 unaryOperator_(unaryOperator),
103 coefficients_(std::make_shared<const Vector>(std::move(coefficients))),
104 entitySet_(std::get<0>(unaryOperator.basis()).gridView())
105 {
106 static_assert(isOperatorOrSumOperator_v<UnaryOperator>, "The type passed to BoundUnaryOperator is not a operator.");
107 static_assert(UnaryOperator::arity==1, "The operator passed to BoundUnaryOperator is not unary.");
108 }
109
111 cacheId_(UniqueCacheId::makeUniqueCacheId()),
112 unaryOperator_(unaryOperator),
113 coefficients_(std::move(coefficients)),
114 entitySet_(std::get<0>(unaryOperator.basis()).gridView())
115 {
116 static_assert(isOperatorOrSumOperator_v<UnaryOperator>, "The type passed to BoundUnaryOperator is not a operator.");
117 static_assert(UnaryOperator::arity==1, "The operator passed to BoundUnaryOperator is not unary.");
118 }
119
120
121
123 {
124 public:
125
127 using Intersection = typename BoundUnaryOperator::LocalUnaryOperator::Intersection;
130
131 private:
132 static constexpr int dimension = EntitySet::Element::dimension;
133
134 using LocalView = typename Basis::LocalView;
135 using Tree = typename LocalView::Tree;
136 using size_type = typename Basis::LocalView::Tree::size_type;
137 using QuadratureRule = typename CacheManager::QuadratureRule;
138 using QuadraturePoint = typename QuadratureRule::value_type;
140
141 void loadLocalDOFs(const LocalView& localView, std::vector<Coefficient>& localDoFs) const
142 {
143 localDoFs.resize(localView.size());
144 TypeTree::forEachLeafNode(localView.tree(), [&](auto&& node, auto&& treePath) {
145 if (usedNodes_[treePath])
146 for (auto i : Dune::range(node.size()))
147 {
148 auto localIndex = node.localIndex(i);
149 localDoFs[localIndex] = (*coefficients_)[localView.index(localIndex)];
150 }
151 });
152 }
153
154 public:
155
156 LocalOperator(LocalUnaryOperator&& localUnaryOperator, NodeFlags usedNodes, const Basis& basis, std::shared_ptr<const Vector> coefficients, UniqueCacheId cacheId) :
157 basis_(basis),
158 localUnaryOperator_(std::move(localUnaryOperator)),
159 usedNodes_(usedNodes),
160 coefficients_(std::move(coefficients)),
161 internalLocalView_{basis.localView(), basis.localView()},
162 cacheId_(cacheId)
163 {
164 localDoFs_[0].reserve(internalLocalView_[0].maxSize());
165 localDoFs_[1].reserve(internalLocalView_[1].maxSize());
166 localUnaryOperator_.registerLocalViews(internalLocalView_[0]);
167 localUnaryOperator_.registerOutsideLocalViews(internalLocalView_[1]);
168 }
169
171 LocalOperator(LocalUnaryOperator(other.localUnaryOperator_), other.usedNodes_, other.basis_, other.coefficients_, other.cacheId_)
172 {}
173
175 LocalOperator(std::move(other.localUnaryOperator_), other.usedNodes_, other.basis_, other.coefficients_, other.cacheId_)
176 {}
177
178 void bind(const Element& element)
179 {
180 if (externalLocalView_[0] == nullptr)
181 {
182 internalLocalView_[0].bind(element);
183 loadLocalDOFs(internalLocalView_[0], localDoFs_[0]);
184 }
185 else
186 loadLocalDOFs(*(externalLocalView_[0]), localDoFs_[0]);
187 localUnaryOperator_.bind(element);
188 }
189
190 void bind(const Intersection& intersection, const Element& element, const Element& otherElement)
191 {
192 if (externalLocalView_[0] == nullptr)
193 {
194 internalLocalView_[0].bind(element);
195 loadLocalDOFs(internalLocalView_[0], localDoFs_[0]);
196 }
197 else
198 loadLocalDOFs(*(externalLocalView_[0]), localDoFs_[0]);
199 if (externalLocalView_[1] == nullptr)
200 {
201 internalLocalView_[1].bind(otherElement);
202 loadLocalDOFs(internalLocalView_[1], localDoFs_[1]);
203 }
204 else
205 loadLocalDOFs(*(externalLocalView_[1]), localDoFs_[1]);
206 localUnaryOperator_.bind(intersection, element, otherElement);
207 }
208
210 void unbind()
211 {
212 if (externalLocalView_[0] == nullptr)
213 internalLocalView_[0].unbind();
214 if (externalLocalView_[1] == nullptr)
215 internalLocalView_[1].unbind();
216 localUnaryOperator_.unbind();
217 }
218
219 // Additional interface of local nullary operator
220 auto quadratureRuleKey() const
221 {
222 return localUnaryOperator_.quadratureRuleKey();
223 }
224
226 {
227 auto& valueCache = cacheManager_->template getCache<ValueCache>(cacheIndex_);
228 auto& values = valueCache.getValues();
229 if (valueCache.isEmpty())
230 {
231 const auto& rule = valueCache.rule();
232 for (auto k : Dune::range(rule.size()))
233 {
234 auto& y = values[k];
235 AssignOp()(y, 0);
236 forEachOperator(localUnaryOperator_, [&](auto& op) {
237 sparseForEach(op(k), [&](const auto& value, auto stencilIndex, auto localIndex) {
238 SumOp::addTo(y, MultOp()(localDoFs_[stencilIndex][localIndex], value));
239 });
240 });
241 }
242 valueCache.setEmpty(false);
243 }
244 return Impl::Tensor::RankZeroTensor(values[index]);
245 }
246
251 template<class... LV>
252 void registerLocalViews(const LV&... lvs)
253 {
254 localUnaryOperator_.registerLocalViews(lvs...);
255 Impl::visitMatchingLocalView([&](const auto& localView) {
256 externalLocalView_[0] = &localView;
257 localDoFs_[0].reserve(externalLocalView_[0]->maxSize());
258 }, basis_, lvs...);
259 }
260
261 template<class... LV>
262 void registerOutsideLocalViews(const LV&... lvs)
263 {
264 localUnaryOperator_.registerOutsideLocalViews(lvs...);
265 Impl::visitMatchingLocalView([&](const auto& localView) {
266 externalLocalView_[1] = &localView;
267 localDoFs_[1].reserve(externalLocalView_[1]->maxSize());
268 }, basis_, lvs...);
269 }
270
271 void registerCaches(CacheManager& cacheManager)
272 {
273 localUnaryOperator_.registerCaches(cacheManager);
274 cacheIndex_ = cacheManager.registerCache(cacheId_, ValueCache(true));
275 }
276
278 {
279 localUnaryOperator_.registerOutsideCaches(cacheManager);
280 }
281
282 template<class... OutsideCacheManager>
283 void bindToCaches(CacheManager& cacheManager, OutsideCacheManager&... outsideCacheManager)
284 {
285 localUnaryOperator_.bindToCaches(cacheManager, outsideCacheManager...);
286 cacheManager_ = &cacheManager;
287 }
288
289 private:
290 const Basis& basis_;
291 mutable LocalUnaryOperator localUnaryOperator_;
292 NodeFlags usedNodes_;
293 std::shared_ptr<const Vector> coefficients_;
294 mutable std::array<std::vector<Coefficient>,2> localDoFs_;
295 std::array<LocalView, 2> internalLocalView_;
296 std::array<const LocalView*,2> externalLocalView_ = {nullptr, nullptr};
297 mutable CacheManager* cacheManager_ = nullptr;
298 UniqueCacheId cacheId_;
299 std::size_t cacheIndex_ = 0;
300 };
301
302 auto basis() const
303 {
304 return std::tuple<>();
305 }
306
307 auto treePath() const
308 {
309 return std::tuple<>();
310 }
311
312 friend LocalOperator localOperator(const BoundUnaryOperator& boundUnaryOperator)
313 {
314 const auto& rootBasis = std::get<0>(boundUnaryOperator.unaryOperator_.basis());
315 // Mark all leaf nodes whose DOFs need to be loaded
316 auto localView = rootBasis.localView();
317 auto usedNodes = TypeTree::makeTreeContainer(localView.tree(), [](auto&&) { return char(false); });
318 forEachOperator(boundUnaryOperator.unaryOperator_, [&](auto& op) {
319 const auto& treePath = std::get<0>(op.treePath());
320 const auto& node = Dune::TypeTree::child(localView.tree(), treePath);
321 TypeTree::forEachLeafNode(node, [&](auto&& leafNode, auto&& leafTreePath) {
322 usedNodes[TypeTree::join(treePath,leafTreePath)] = true;
323 });
324 });
325 return LocalOperator(localOperator(boundUnaryOperator.unaryOperator_), usedNodes, rootBasis, boundUnaryOperator.coefficients_, boundUnaryOperator.cacheId_);
326 }
327
329 requires (BoundUnaryOperator::arity==0)
330 {
331 return LocalFunctionAdaptor<LocalOperator>(localOperator(boundUnaryOperator));
332 }
333
338 Range operator()(const Domain& x) const
339 {
340 DUNE_THROW(Dune::NotImplemented, "Evaluation of BoundUnaryOperator in global coordinates is not implemented");
341 }
342
344 const EntitySet& entitySet() const
345 {
346 return entitySet_;
347 }
348
349 const auto& unaryOperator() const
350 {
351 return unaryOperator_;
352 }
353
354 const auto& coefficients() const
355 {
356 return coefficients_;
357 }
358
359 friend auto jacobian(const BoundUnaryOperator& f)
360 {
361 static_assert(isOperator_v<UnaryOperator>, "jacobian(BoundUnaryOperator<SumOperator<...>,V>(...)) is not implemented.");
362 return Dune::Fufem::Forms::BoundUnaryOperator(jacobian(f.unaryOperator_), f.coefficients_);
363 }
364
365 friend auto gradient(const BoundUnaryOperator& f)
366 {
367 static_assert(isOperator_v<UnaryOperator>, "gradient(BoundUnaryOperator<SumOperator<...>,V>(...)) is not implemented.");
368 return Dune::Fufem::Forms::BoundUnaryOperator(gradient(f.unaryOperator_), f.coefficients_);
369 }
370
371 friend auto grad(const BoundUnaryOperator& f)
372 {
373 static_assert(isOperator_v<UnaryOperator>, "grad(BoundUnaryOperator<SumOperator<...>,V>(...)) is not implemented.");
374 return Dune::Fufem::Forms::BoundUnaryOperator(grad(f.unaryOperator_), f.coefficients_);
375 }
376
377 friend auto divergence(const BoundUnaryOperator& f)
378 {
379 static_assert(isOperator_v<UnaryOperator>, "divergence(BoundUnaryOperator<SumOperator<...>,V>(...)) is not implemented.");
380 return Dune::Fufem::Forms::BoundUnaryOperator(divergence(f.unaryOperator_), f.coefficients_);
381 }
382
383 friend auto div(const BoundUnaryOperator& f)
384 {
385 static_assert(isOperator_v<UnaryOperator>, "div(BoundUnaryOperator<SumOperator<...>,V>(...)) is not implemented.");
386 return Dune::Fufem::Forms::BoundUnaryOperator(div(f.unaryOperator_), f.coefficients_);
387 }
388
389 private:
390 UniqueCacheId cacheId_;
391 UnaryOperator unaryOperator_;
392 std::shared_ptr<const Vector> coefficients_;
393 EntitySet entitySet_;
394 };
395
396
397
398} // namespace Dune::Fufem::Forms
399
400
401#endif // DUNE_FUFEM_FORMS_BOUNDUNARYOPERATOR_HH
static constexpr IntegralRange< T > range(T from, T to) noexcept
typename AutonomousValueType< T >::type AutonomousValue
std::ptrdiff_t index() const
#define DUNE_THROW(E,...)
void forEachLeafNode(Tree &&tree, LeafFunc &&leafFunc)
auto makeTreeContainer(const Tree &tree, LeafToValue &&leafToValue)
std::shared_ptr< T > stackobject_to_shared_ptr(T &t)
constexpr auto get(std::integer_sequence< T, II... >, std::integral_constant< std::size_t, pos >={})
Definition boundunaryoperator.hh:123
void registerOutsideLocalViews(const LV &... lvs)
Definition boundunaryoperator.hh:262
LocalOperator(LocalOperator &&other)
Definition boundunaryoperator.hh:174
void registerCaches(CacheManager &cacheManager)
Definition boundunaryoperator.hh:271
typename BoundUnaryOperator::Range Range
Definition boundunaryoperator.hh:129
void registerOutsideCaches(CacheManager &cacheManager)
Definition boundunaryoperator.hh:277
void bind(const Element &element)
Definition boundunaryoperator.hh:178
LocalOperator(const LocalOperator &other)
Definition boundunaryoperator.hh:170
void bindToCaches(CacheManager &cacheManager, OutsideCacheManager &... outsideCacheManager)
Definition boundunaryoperator.hh:283
typename BoundUnaryOperator::LocalUnaryOperator::Intersection Intersection
Definition boundunaryoperator.hh:127
void registerLocalViews(const LV &... lvs)
Register LocalViews managed outside.
Definition boundunaryoperator.hh:252
LocalOperator(LocalUnaryOperator &&localUnaryOperator, NodeFlags usedNodes, const Basis &basis, std::shared_ptr< const Vector > coefficients, UniqueCacheId cacheId)
Definition boundunaryoperator.hh:156
void unbind()
Unbind the local-function.
Definition boundunaryoperator.hh:210
typename BoundUnaryOperator::Element Element
Definition boundunaryoperator.hh:126
void bind(const Intersection &intersection, const Element &element, const Element &otherElement)
Definition boundunaryoperator.hh:190
auto quadratureRuleKey() const
Definition boundunaryoperator.hh:220
auto operator()(std::size_t index) const
Definition boundunaryoperator.hh:225
Wrapper binding a linear operator to a coefficient vector.
Definition boundunaryoperator.hh:56
auto basis() const
Definition boundunaryoperator.hh:302
const auto & coefficients() const
Definition boundunaryoperator.hh:354
friend LocalOperator localOperator(const BoundUnaryOperator &boundUnaryOperator)
Definition boundunaryoperator.hh:312
friend auto divergence(const BoundUnaryOperator &f)
Definition boundunaryoperator.hh:377
auto treePath() const
Definition boundunaryoperator.hh:307
typename Dune::Functions::GridViewEntitySet< GridView, 0 > EntitySet
Definition boundunaryoperator.hh:82
BoundUnaryOperator(const UnaryOperator &unaryOperator, Vector &&coefficients)
Definition boundunaryoperator.hh:100
const auto & unaryOperator() const
Definition boundunaryoperator.hh:349
friend auto grad(const BoundUnaryOperator &f)
Definition boundunaryoperator.hh:371
friend LocalFunctionAdaptor< LocalOperator > localFunction(const BoundUnaryOperator &boundUnaryOperator)
Definition boundunaryoperator.hh:328
friend auto gradient(const BoundUnaryOperator &f)
Definition boundunaryoperator.hh:365
Dune::AutonomousValue< decltype(std::declval< Vector >()[std::declval< typename Basis::MultiIndex >()])> Coefficient
Definition boundunaryoperator.hh:80
friend auto jacobian(const BoundUnaryOperator &f)
Definition boundunaryoperator.hh:359
typename Basis::GridView GridView
Definition boundunaryoperator.hh:81
BoundUnaryOperator(const UnaryOperator &unaryOperator, std::shared_ptr< const Vector > coefficients)
Definition boundunaryoperator.hh:110
typename EntitySet::GlobalCoordinate Domain
Definition boundunaryoperator.hh:83
const EntitySet & entitySet() const
Get associated set of entities the local-function can be bound to.
Definition boundunaryoperator.hh:344
Range operator()(const Domain &x) const
Evaluate function in global coordinates.
Definition boundunaryoperator.hh:338
typename EntitySet::LocalCoordinate LocalDomain
Definition boundunaryoperator.hh:84
V Vector
Definition boundunaryoperator.hh:79
std::decay_t< decltype(MultOp()(std::declval< Coefficient >(), std::declval< typename UnaryOperator::Range >()))> Range
Definition boundunaryoperator.hh:88
typename EntitySet::Element Element
Definition boundunaryoperator.hh:87
friend auto div(const BoundUnaryOperator &f)
Definition boundunaryoperator.hh:383
BoundUnaryOperator(const UnaryOperator &unaryOperator, const Vector &coefficients)
Definition boundunaryoperator.hh:90
A class for managing caches of different types.
Definition shapefunctioncache.hh:644
Dune::QuadratureRule< CT, dimension > QuadratureRule
Definition shapefunctioncache.hh:646
size_type registerCache(UniqueCacheId uniqueCacheId, Cache &&cache)
Register a new cache.
Definition shapefunctioncache.hh:782
Adaptor for turning a Fufem::Forms LocalOperator into a LocalFunction.
Definition localfunctionadaptor.hh:43
Base class for multilinear operator implementations.
Definition baseclass.hh:73
static constexpr std::size_t arity
Definition baseclass.hh:75
A simple cache implementation storing values.
Definition shapefunctioncache.hh:837
Base class for unary multilinear operator implementations.
Definition baseclass.hh:91
Objects of this class are used to uniquely identifies a cache.
Definition shapefunctioncache.hh:571
T forward(T... args)
auto gradient(const Op &op)
Obtain the gradient of an operator.
Definition userfunctions.hh:1188
auto divergence(const FEFunctionOperator< B, TP, argIndex > &op)
Obtain the divergence of an operator.
Definition userfunctions.hh:1098
auto jacobian(const FEFunctionOperator< B, TP, argIndex > &op)
Obtain the jacobian of an operator.
Definition userfunctions.hh:1085
auto grad(const Op &op)
Obtain the gradient of an operator.
Definition userfunctions.hh:1208
Definition baseclass.hh:22
STL namespace.
T resize(T... args)
Definition localoperators.hh:46
Definition localoperators.hh:401
Definition localoperators.hh:66
static void addTo(K1 &x, const K2 &y)
Definition localoperators.hh:70