Dune-Fufem 2.12-git
Loading...
Searching...
No Matches
unaryoperators.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_UNARYOPERATORS_HH
8#define DUNE_FUFEM_FORMS_UNARYOPERATORS_HH
9
10#include <cstddef>
11#include <type_traits>
12#include <vector>
13
24
26
28
32
34
35
36
37namespace Dune::Fufem::Forms {
38
39
40
54 template<class B, class TP, std::size_t argIndex>
55 class FEOperatorBase : public UnaryOperator<argIndex>
56 {
57
58 protected:
59 using LocalView = typename B::LocalView;
60 using Tree = typename LocalView::Tree;
63
64 // Helper function to derive the leafTreePath associated
65 // to a treePath. For treePaths referring to a leaf this is
66 // the identity. For treePaths referring to the a power
67 // node whose children are leaf, it's the first child.
68 // This is used to implement vector valued ansatz functions
69 // for power nodes.
70 static auto leafTreePath(const TP& tp)
71 {
73 return tp;
76 return Dune::TypeTree::push_back(tp, Dune::Indices::_0);
77 else
78 static_assert(Dune::AlwaysFalse<TP>::value, "Only leaf nodes or power nodes directly below leaf are supported.");
79 }
80
83 using Basis = B;
84 using TreePath = TP;
85
86 public:
87
88 using Element = typename Basis::LocalView::Element;
89 using Intersection = typename Basis::GridView::Intersection;
90
91 FEOperatorBase(const Basis& basis, const TreePath& treePath, bool isAffine = true) :
94 {}
95
97 {
98 protected:
99
102
103 public:
104
108
109 LocalOperator(const SubspaceBasis& subspaceBasis, bool isAffine) :
110 subspaceBasis_(subspaceBasis),
112 leafNode_(nullptr),
113 localView_(nullptr),
115 cacheIndex_(std::numeric_limits<std::size_t>::max()),
117 {}
118
121 {}
122
123 void unbind()
124 {}
125
126 auto quadratureRuleKey() const
127 {
128 return quadratureRuleKey_;
129 }
130
131 template<class... LV>
132 void registerLocalViews(const LV&... lvs)
133 {
134 Impl::visitMatchingLocalView([&](const auto& localView) {
135 localView_ = &localView;
136 leafNode_ = & Dune::TypeTree::child(localView.tree(), leafTreePath_);
137 }, subspaceBasis_.rootBasis(), lvs...);
138 }
139
140 template<class... LV>
141 void registerOutsideLocalViews(const LV&... lvs)
142 {}
143
144 void registerCaches(CacheManager& cacheManager)
145 {
146 if (localView_ == nullptr)
147 DUNE_THROW(Dune::InvalidStateException, "The LocalOperator was not bound to a LocalView before registering caches.");
148 const auto& tree = localView_->tree();
149 cacheIndex_ = cacheManager.registerCache(UniqueCacheId(tree), TreeCache(tree));
150 if (not isAffine_)
151 cacheManager.template getCache<TreeCache>(cacheIndex_)[subspaceBasis_.prefixPath()].setNonAffine();
152 }
153
155 {}
156
157 protected:
158
159 const LeafNode& leafNode() const
160 {
161 return *leafNode_;
162 }
163
164 auto& leafNodeCache(CacheManager& cacheManager)
165 {
166 return cacheManager.template getCache<TreeCache>(cacheIndex_)[leafTreePath_];
167 }
168
176 };
177
178 auto basis() const
179 {
180 return std::tie(subspaceBasis_.rootBasis());
181 }
182
183 auto treePath() const
184 {
185 return std::tie(subspaceBasis_.prefixPath());
186 }
187
188 bool isAffine() const
189 {
190 return isAffine_;
191 }
192
197 template<std::size_t k>
199 {
201 }
202
203 protected:
206 };
207
208
209
219 template<class B, class TP, std::size_t argIndex>
220 class FEFunctionOperator : public FEOperatorBase<B, TP, argIndex>
221 {
223 using Node = typename Base::Node;
224 using LeafLBTraits = typename Base::LeafNode::FiniteElement::Traits::LocalBasisType::Traits;
225
226 public:
227
228 using Element = typename Base::Element;
231 typename LeafLBTraits::RangeType,
232 Dune::FieldVector<typename LeafLBTraits::RangeFieldType, Node::degree()>>;
233
234 using Base::Base;
235
237 : Base(other)
238 {}
239
241 {
242 using Base::LocalOperator::quadratureRuleKey_;
243 using Base::LocalOperator::leafNode;
244 using Base::LocalOperator::leafNodeCache;
246
247 public:
248
250 using CacheManager = typename Base::LocalOperator::CacheManager;
252
253 void bind(const Element&)
254 {
255 if (leafNode().size()!=0)
256 quadratureRuleKey_ = QuadratureRuleKey(leafNode().finiteElement());
257 else
258 quadratureRuleKey_ = QuadratureRuleKey(this->localView_->element().type(), 0);
259 }
260
261 void bind(const Intersection& intersection, const Element& element, const Element& otherElement)
262 {
263 bind(element);
264 }
265
266 template<class... OutsideCacheManager>
267 void bindToCaches(CacheManager& cacheManager, OutsideCacheManager&... outsideCacheManager)
268 {
269 valueCache_ = &(leafNodeCache(cacheManager).getValues());
270 }
271
273 {
274 using namespace Impl::Tensor;
275 using namespace Dune::Indices;
276 const auto& values = (*valueCache_)[index];
277 std::size_t size = leafNode().size();
278 std::size_t indexOffset = (size==0) ? 0 : leafNode().localIndex(0);
280 return SparseTensor(_2,
281 [&, size, indexOffset](auto&& f) {
282 for(std::size_t i=0; i<size; ++i)
283 f(values[i], _0, indexOffset+i);
284 },
285 size,
287 );
290 return SparseTensor(_2,
291 [&, size, indexOffset](auto&& f) {
292 for(std::size_t j=0; j<Node::degree(); ++j)
293 for(std::size_t i=0; i<size; ++i)
294 {
295 auto result = Range(0);
296 result[j] = Dune::Fufem::asNumber(values[i]);
297 f(result, _0, indexOffset + j*size + i);
298 }
299 },
300 Node::degree() * size,
302 );
303 }
304
305 private:
306 const typename Base::LocalOperator::LeafNodeCache::ValueCache* valueCache_ = nullptr;
307 };
308
310 {
311 return LocalOperator(op);
312 }
313
314 template<std::size_t i>
316 {
317 using Basis = typename Base::Basis;
318 auto childTP = Dune::TypeTree::push_back(Base::subspaceBasis_.prefixPath(), childIndex);
320 }
321
323 template<std::size_t k>
325 {
326 return {Base::template rebindArgIndex<k>()};
327 }
328
329 };
330
331
332
333
343 template<class B, class TP, std::size_t argIndex>
344 class FEFunctionJacobianOperator : public FEOperatorBase<B, TP, argIndex>
345 {
347 using Node = typename Base::Node;
348 using LeafLBTraits = typename Base::LeafNode::FiniteElement::Traits::LocalBasisType::Traits;
349
352 typename LeafLBTraits::JacobianType,
353 Dune::FieldMatrix<typename LeafLBTraits::RangeFieldType, Node::degree(), LeafLBTraits::dimDomain>>;
354
355 using GeometryJacobianInverse = typename Node::Element::Geometry::JacobianInverse;
356
357 public:
358
359 using Element = typename Base::Element;
360 using Range = typename Impl::GlobalJacobianTraits<ReferenceJacobian, GeometryJacobianInverse>::type;
361
362 using Base::Base;
363
365 : Base(other)
366 {}
367
369 {
370 using Base::LocalOperator::quadratureRuleKey_;
371 using Base::LocalOperator::leafNode;
372 using Base::LocalOperator::leafNodeCache;
374
375 public:
376
378 using CacheManager = typename Base::LocalOperator::CacheManager;
380
381 void bind(const Element&)
382 {
383 if (leafNode().size()!=0)
385 else
386 quadratureRuleKey_ = QuadratureRuleKey(this->localView_->element().type(), 0);
387 }
388
389 void bind(const Intersection& intersection, const Element& element, const Element& otherElement)
390 {
391 bind(element);
392 }
393
394 template<class... OutsideCacheManager>
395 void bindToCaches(CacheManager& cacheManager, OutsideCacheManager&... outsideCacheManager)
396 {
397 globalJacobianCache_ = &(leafNodeCache(cacheManager).getGlobalJacobians());
398 }
399
401 {
402 using namespace Impl::Tensor;
403 using namespace Dune::Indices;
404 const auto& globalJacobians = (*globalJacobianCache_)[index];
405 std::size_t size = leafNode().size();
406 std::size_t indexOffset = (size==0) ? 0 : leafNode().localIndex(0);
408 return SparseTensor(_2,
409 [&, size, indexOffset](auto&& f) {
410 for(std::size_t i=0; i<size; ++i)
411 f(globalJacobians[i], _0, indexOffset+i);
412 },
413 size,
415 );
418 return SparseTensor(_2,
419 [&, size, indexOffset](auto&& f) {
420 for(std::size_t j=0; j<Node::degree(); ++j)
421 for(std::size_t i=0; i<size; ++i)
422 {
423 auto result = Range(0);
424 result[j] = globalJacobians[i][0];
425 f(result, _0, indexOffset + j*size + i);
426 }
427 },
428 Node::degree() * size,
430 );
431 }
432
433 private:
434 const typename Base::LocalOperator::LeafNodeCache::GlobalJacobianCache* globalJacobianCache_ = nullptr;
435 };
436
438 {
439 return LocalOperator(op);
440 }
441
442 template<std::size_t i>
444 {
445 using Basis = typename Base::Basis;
446 auto childTP = Dune::TypeTree::push_back(Base::subspaceBasis_.prefixPath(), childIndex);
448 }
449
451 template<std::size_t k>
453 {
454 return {Base::template rebindArgIndex<k>()};
455 }
456
457 };
458
459
460
470 template<class B, class TP, std::size_t argIndex>
471 class FEFunctionDivergenceOperator : public FEOperatorBase<B, TP, argIndex>
472 {
474 using Node = typename Base::Node;
475 using LeafLBTraits = typename Base::LeafNode::FiniteElement::Traits::LocalBasisType::Traits;
476
477 public:
478
479 using Element = typename Base::Element;
482 typename LeafLBTraits::RangeFieldType,
483 typename LeafLBTraits::JacobianType::block_type>;
484
485 using Base::Base;
486
488 : Base(other)
489 {}
490
492 {
493 using Base::LocalOperator::quadratureRuleKey_;
494 using Base::LocalOperator::leafNode;
495 using Base::LocalOperator::leafNodeCache;
497
498 static constexpr int dimWorld = Base::LocalView::GridView::dimensionworld;
499
500 public:
501
503 using CacheManager = typename Base::LocalOperator::CacheManager;
505
506 void bind(const Element&)
507 {
508 if (leafNode().size()!=0)
510 else
511 quadratureRuleKey_ = QuadratureRuleKey(this->localView_->element().type(), 0);
512 }
513
514 void bind(const Intersection& intersection, const Element& element, const Element& otherElement)
515 {
516 bind(element);
517 }
518
519 template<class... OutsideCacheManager>
520 void bindToCaches(CacheManager& cacheManager, OutsideCacheManager&... outsideCacheManager)
521 {
522 globalJacobianCache_ = &(leafNodeCache(cacheManager).getGlobalJacobians());
523 }
524
526 {
527 using namespace Impl::Tensor;
528 using namespace Dune::Indices;
529 using LocalBasisTraits = typename Base::LeafNode::FiniteElement::Traits::LocalBasisType::Traits;
530 using Field = typename LocalBasisTraits::RangeFieldType;
531 const auto& globalJacobians = (*globalJacobianCache_)[index];
532 std::size_t size = leafNode().size();
533 std::size_t indexOffset = (size==0) ? 0 : leafNode().localIndex(0);
535 {
536 static_assert(LeafLBTraits::dimRange == dimWorld, "Number of (leaf FE) components does not match the world dimension.");
537 return SparseTensor(_2,
538 [&, size, indexOffset](auto&& f) {
539 for(std::size_t i=0; i<size; ++i)
540 {
541 auto div = Field{0};
542 for(std::size_t j=0; j<globalJacobians[i].N(); ++j)
543 div += globalJacobians[i][j][j];
544 f(div, _0, indexOffset+i);
545 }
546 },
547 size,
549 );
550 }
553 {
554 static_assert(Node::degree() == dimWorld, "Number of (power) components does not match the world dimension.");
555 return SparseTensor(_2,
556 [&, size, indexOffset](auto&& f) {
557 // Here we should compute the trace of the global Jacobian J
558 // that we would need to build first. But since only the
559 // j-th row of J is nonzero, we can simply
560 // return the diagonal entry of this row which coincides
561 // with the j-th entry of the gradient of
562 // the respective scalar basis function.
563 for(std::size_t j=0; j<Node::degree(); ++j)
564 for(std::size_t i=0; i<size; ++i)
565 f(globalJacobians[i][0][j], _0, indexOffset + j*size + i);
566 },
567 Node::degree() * size,
569 );
570 }
571 }
572
573 private:
574 const typename Base::LocalOperator::LeafNodeCache::GlobalJacobianCache* globalJacobianCache_ = nullptr;
575 };
576
578 {
579 return LocalOperator(op);
580 }
581
583 template<std::size_t k>
585 {
586 return {Base::template rebindArgIndex<k>()};
587 }
588
589 };
590
591
592
593} // namespace Dune::Fufem::Forms
594
595
596
597#endif // DUNE_FUFEM_FORMS_UNARYOPERATORS_HH
int size() const
std::ptrdiff_t index() const
#define DUNE_THROW(E,...)
size_t() const
decltype(auto) child(Node &&node, TreePath< Indices... > treePath)
constexpr index_constant< 0 > _0
A class for managing caches of different types.
Definition shapefunctioncache.hh:644
size_type registerCache(UniqueCacheId uniqueCacheId, Cache &&cache)
Register a new cache.
Definition shapefunctioncache.hh:782
void bindToCaches(CacheManager &cacheManager, OutsideCacheManager &... outsideCacheManager)
Definition unaryoperators.hh:520
void bind(const Element &)
Definition unaryoperators.hh:506
void bind(const Intersection &intersection, const Element &element, const Element &otherElement)
Definition unaryoperators.hh:514
typename Base::LocalOperator::Intersection Intersection
Definition unaryoperators.hh:502
typename Base::LocalOperator::CacheManager CacheManager
Definition unaryoperators.hh:503
typename FEFunctionDivergenceOperator::Range Range
Definition unaryoperators.hh:504
auto operator()(std::size_t index) const
Definition unaryoperators.hh:525
Linear map representing the divergenc of the elements of an FE-space.
Definition unaryoperators.hh:472
typename Base::Element Element
Definition unaryoperators.hh:479
std::conditional_t< Dune::TypeTree::Concept::LeafTreeNode< Node >, typename LeafLBTraits::RangeFieldType, typename LeafLBTraits::JacobianType::block_type > Range
Definition unaryoperators.hh:483
friend LocalOperator localOperator(const FEFunctionDivergenceOperator &op)
Definition unaryoperators.hh:577
FEFunctionDivergenceOperator(const Base &other)
Definition unaryoperators.hh:487
FEFunctionDivergenceOperator< B, TP, k > rebindArgIndex() const
Get a copy of this operator bound to another argument.
Definition unaryoperators.hh:584
void bind(const Element &)
Definition unaryoperators.hh:381
void bind(const Intersection &intersection, const Element &element, const Element &otherElement)
Definition unaryoperators.hh:389
typename Base::LocalOperator::Intersection Intersection
Definition unaryoperators.hh:377
typename Base::LocalOperator::CacheManager CacheManager
Definition unaryoperators.hh:378
void bindToCaches(CacheManager &cacheManager, OutsideCacheManager &... outsideCacheManager)
Definition unaryoperators.hh:395
auto operator()(std::size_t index) const
Definition unaryoperators.hh:400
typename FEFunctionJacobianOperator::Range Range
Definition unaryoperators.hh:379
Linear map representing the jacobians of the elements of an FE-space.
Definition unaryoperators.hh:345
typename Impl::GlobalJacobianTraits< ReferenceJacobian, GeometryJacobianInverse >::type Range
Definition unaryoperators.hh:360
FEFunctionJacobianOperator(const Base &other)
Definition unaryoperators.hh:364
FEFunctionJacobianOperator< B, TP, k > rebindArgIndex() const
Get a copy of this operator bound to another argument.
Definition unaryoperators.hh:452
friend LocalOperator localOperator(const FEFunctionJacobianOperator &op)
Definition unaryoperators.hh:437
auto childOperator(Dune::index_constant< i > childIndex) const
Definition unaryoperators.hh:443
typename Base::Element Element
Definition unaryoperators.hh:359
typename Base::LocalOperator::Intersection Intersection
Definition unaryoperators.hh:249
void bind(const Element &)
Definition unaryoperators.hh:253
void bind(const Intersection &intersection, const Element &element, const Element &otherElement)
Definition unaryoperators.hh:261
typename Base::LocalOperator::CacheManager CacheManager
Definition unaryoperators.hh:250
void bindToCaches(CacheManager &cacheManager, OutsideCacheManager &... outsideCacheManager)
Definition unaryoperators.hh:267
auto operator()(std::size_t index) const
Definition unaryoperators.hh:272
typename FEFunctionOperator::Range Range
Definition unaryoperators.hh:251
Linear map representing the elements of an FE-space.
Definition unaryoperators.hh:221
std::conditional_t< Dune::TypeTree::Concept::LeafTreeNode< Node >, typename LeafLBTraits::RangeType, Dune::FieldVector< typename LeafLBTraits::RangeFieldType, Node::degree()> > Range
Definition unaryoperators.hh:232
auto childOperator(Dune::index_constant< i > childIndex) const
Definition unaryoperators.hh:315
typename Base::Element Element
Definition unaryoperators.hh:228
FEFunctionOperator< B, TP, k > rebindArgIndex() const
Get a copy of this operator bound to another argument.
Definition unaryoperators.hh:324
FEFunctionOperator(const Base &other)
Definition unaryoperators.hh:236
friend LocalOperator localOperator(const FEFunctionOperator &op)
Definition unaryoperators.hh:309
bool isAffine_
Definition unaryoperators.hh:175
SubspaceBasis subspaceBasis_
Definition unaryoperators.hh:169
void unbind()
Definition unaryoperators.hh:123
LocalOperator(const SubspaceBasis &subspaceBasis, bool isAffine)
Definition unaryoperators.hh:109
void registerOutsideCaches(CacheManager &cacheManager)
Definition unaryoperators.hh:154
LocalOperator(const FEOperatorBase &op)
Definition unaryoperators.hh:119
const LocalView * localView_
Definition unaryoperators.hh:172
typename FEOperatorBase::Element Element
Definition unaryoperators.hh:105
void registerOutsideLocalViews(const LV &... lvs)
Definition unaryoperators.hh:141
void registerCaches(CacheManager &cacheManager)
Definition unaryoperators.hh:144
auto quadratureRuleKey() const
Definition unaryoperators.hh:126
ShapeFunctionCache< Tree > TreeCache
Definition unaryoperators.hh:100
QuadratureRuleKey quadratureRuleKey_
Definition unaryoperators.hh:173
auto & leafNodeCache(CacheManager &cacheManager)
Definition unaryoperators.hh:164
std::size_t cacheIndex_
Definition unaryoperators.hh:174
void registerLocalViews(const LV &... lvs)
Definition unaryoperators.hh:132
const LeafNode * leafNode_
Definition unaryoperators.hh:171
const LeafTreePath leafTreePath_
Definition unaryoperators.hh:170
const LeafNode & leafNode() const
Definition unaryoperators.hh:159
typename FEOperatorBase::Intersection Intersection
Definition unaryoperators.hh:106
Base class of elementary differential operators on an FE-space.
Definition unaryoperators.hh:56
typename Dune::TypeTree::ChildForTreePath< Tree, TP > Node
Definition unaryoperators.hh:61
typename B::LocalView LocalView
Definition unaryoperators.hh:59
SubspaceBasis subspaceBasis_
Definition unaryoperators.hh:204
typename Dune::TypeTree::ChildForTreePath< Tree, LeafTreePath > LeafNode
Definition unaryoperators.hh:82
B Basis
Definition unaryoperators.hh:83
typename Basis::LocalView::Element Element
Definition unaryoperators.hh:88
bool isAffine_
Definition unaryoperators.hh:205
decltype(leafTreePath(std::declval< TP >())) LeafTreePath
Definition unaryoperators.hh:81
FEOperatorBase(const Basis &basis, const TreePath &treePath, bool isAffine=true)
Definition unaryoperators.hh:91
typename Dune::Functions::SubspaceBasis< B, TP > SubspaceBasis
Definition unaryoperators.hh:62
TP TreePath
Definition unaryoperators.hh:84
static auto leafTreePath(const TP &tp)
Definition unaryoperators.hh:70
typename Basis::GridView::Intersection Intersection
Definition unaryoperators.hh:89
FEOperatorBase< B, TP, k > rebindArgIndex() const
Get a copy of this operator bound to another argument.
Definition unaryoperators.hh:198
typename LocalView::Tree Tree
Definition unaryoperators.hh:60
auto basis() const
Definition unaryoperators.hh:178
auto treePath() const
Definition unaryoperators.hh:183
bool isAffine() const
Definition unaryoperators.hh:188
A hierarchic cache for storing shape function evaluations for a tree.
Definition shapefunctioncache.hh:106
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
A token that specifies a quadrature rule.
Definition quadraturerulecache.hh:40
QuadratureRuleKey derivative() const
Definition quadraturerulecache.hh:164
T forward(T... args)
auto div(const Op &op)
Obtain the divergence of an operator.
Definition userfunctions.hh:1222
Definition baseclass.hh:22
const T & asNumber(const T &t)
Forward given value as number.
Definition asnumber.hh:23
STL namespace.
T tie(T... args)