Dune-Fufem 2.11-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
21
22#if DUNE_VERSION_GTE(DUNE_FUNCTIONS, 2, 11)
25#else
26#include <dune/typetree/childextraction.hh>
27#include <dune/typetree/treepath.hh>
28#endif
29
31
33
37
39
40
41
42namespace Dune::Fufem::Forms {
43
44
45
59 template<class B, class TP, std::size_t argIndex>
60 class FEOperatorBase : public UnaryOperator<argIndex>
61 {
62
63 protected:
64 using LocalView = typename B::LocalView;
65 using Tree = typename LocalView::Tree;
68
69 // Helper function to derive the leafTreePath associated
70 // to a treePath. For treePaths referring to a leaf this is
71 // the identity. For treePaths referring to the a power
72 // node whose children are leaf, it's the first child.
73 // This is used to implement vector valued ansatz functions
74 // for power nodes.
75 static auto leafTreePath(const TP& tp)
76 {
77 if constexpr(Dune::Fufem::Impl::Concept::LeafTreeNode<Node>)
78 return tp;
79 else if constexpr(Dune::Fufem::Impl::Concept::UniformInnerTreeNode<Node>
80 and Dune::Fufem::Impl::Concept::LeafTreeNode<Dune::TypeTree::Child<Node, 0>>)
81 return Dune::TypeTree::push_back(tp, Dune::Indices::_0);
82 }
83
86 using Basis = B;
87 using TreePath = TP;
88
89 public:
90
91 using Element = typename Basis::LocalView::Element;
92 using Intersection = typename Basis::GridView::Intersection;
93
98
100 {
101 protected:
102
105
106 public:
107
111
112 LocalOperator(const SubspaceBasis& subspaceBasis, bool isAffine) :
113 subspaceBasis_(subspaceBasis),
115 leafNode_(nullptr),
116 localView_(nullptr),
118 cacheIndex_(std::numeric_limits<std::size_t>::max()),
120 {}
121
125
126 void unbind()
127 {}
128
129 auto quadratureRuleKey() const
130 {
131 return quadratureRuleKey_;
132 }
133
134 template<class... LV>
135 void registerLocalViews(const LV&... lvs)
136 {
137 Impl::visitMatchingLocalView([&](const auto& localView) {
138 localView_ = &localView;
139 leafNode_ = & Dune::TypeTree::child(localView.tree(), leafTreePath_);
140 }, subspaceBasis_.rootBasis(), lvs...);
141 }
142
143 template<class... LV>
144 void registerOutsideLocalViews(const LV&... lvs)
145 {}
146
147 void registerCaches(CacheManager& cacheManager)
148 {
149 const auto& tree = localView_->tree();
150 cacheIndex_ = cacheManager.registerCache(UniqueCacheId(tree), TreeCache(tree));
151 if (not isAffine_)
152 cacheManager.template getCache<TreeCache>(cacheIndex_)[subspaceBasis_.prefixPath()].setNonAffine();
153 }
154
156 {}
157
158 protected:
159
160 const LeafNode& leafNode() const
161 {
162 return *leafNode_;
163 }
164
165 auto& leafNodeCache(CacheManager& cacheManager)
166 {
167 return cacheManager.template getCache<TreeCache>(cacheIndex_)[leafTreePath_];
168 }
169
177 };
178
179 auto basis() const
180 {
181 return std::tie(subspaceBasis_.rootBasis());
182 }
183
184 auto treePath() const
185 {
186 return std::tie(subspaceBasis_.prefixPath());
187 }
188
189 bool isAffine() const
190 {
191 return isAffine_;
192 }
193
198 template<std::size_t k>
203
204 protected:
207 };
208
209
210
220 template<class B, class TP, std::size_t argIndex>
221 class FEFunctionOperator : public FEOperatorBase<B, TP, argIndex>
222 {
224 using Node = typename Base::Node;
225 using LeafLBTraits = typename Base::LeafNode::FiniteElement::Traits::LocalBasisType::Traits;
226
227 public:
228
229 using Element = typename Base::Element;
231 Dune::Fufem::Impl::Concept::LeafTreeNode<Node>,
232 typename LeafLBTraits::RangeType,
233 Dune::FieldVector<typename LeafLBTraits::RangeFieldType, Node::degree()>>;
234
235 using Base::Base;
236
238 : Base(other)
239 {}
240
242 {
243 using Base::LocalOperator::quadratureRuleKey_;
244 using Base::LocalOperator::leafNode;
245 using Base::LocalOperator::leafNodeCache;
247
248 public:
249
251 using CacheManager = typename Base::LocalOperator::CacheManager;
253
254 void bind(const Element&)
255 {
256 if (leafNode().size()!=0)
257 quadratureRuleKey_ = QuadratureRuleKey(leafNode().finiteElement());
258 else
259 quadratureRuleKey_ = QuadratureRuleKey(this->localView_->element().type(), 0);
260 }
261
262 void bind(const Intersection& intersection, const Element& element, const Element& otherElement)
263 {
264 bind(element);
265 }
266
267 template<class... OutsideCacheManager>
268 void bindToCaches(CacheManager& cacheManager, OutsideCacheManager&... outsideCacheManager)
269 {
270 valueCache_ = &(leafNodeCache(cacheManager).getValues());
271 }
272
274 {
275 using namespace Impl::Tensor;
276 using namespace Dune::Indices;
277 const auto& values = (*valueCache_)[index];
278 std::size_t size = leafNode().size();
279 std::size_t indexOffset = (size==0) ? 0 : leafNode().localIndex(0);
280 if constexpr(Dune::Fufem::Impl::Concept::LeafTreeNode<Node>)
281 return SparseTensor(_2,
282 [&, size, indexOffset](auto&& f) {
283 for(std::size_t i=0; i<size; ++i)
284 f(values[i], _0, indexOffset+i);
285 },
286 size,
288 );
289 else if constexpr(Dune::Fufem::Impl::Concept::UniformInnerTreeNode<Node>
290 and Dune::Fufem::Impl::Concept::LeafTreeNode<Dune::TypeTree::Child<Node, 0>>)
291 return SparseTensor(_2,
292 [&, size, indexOffset](auto&& f) {
293 for(std::size_t j=0; j<Node::degree(); ++j)
294 for(std::size_t i=0; i<size; ++i)
295 {
296 auto result = Range(0);
297 result[j] = values[i];
298 f(result, _0, indexOffset + j*size + i);
299 }
300 },
301 Node::degree() * size,
303 );
304 }
305
306 private:
307 const typename Base::LocalOperator::LeafNodeCache::ValueCache* valueCache_ = nullptr;
308 };
309
311 {
312 return LocalOperator(op);
313 }
314
315 template<std::size_t i>
317 {
318 using Basis = typename Base::Basis;
319 auto childTP = Dune::TypeTree::push_back(Base::subspaceBasis_.prefixPath(), childIndex);
321 }
322
324 template<std::size_t k>
326 {
327 return {Base::template rebindArgIndex<k>()};
328 }
329
330 };
331
332
333
334
344 template<class B, class TP, std::size_t argIndex>
345 class FEFunctionJacobianOperator : public FEOperatorBase<B, TP, argIndex>
346 {
348 using Node = typename Base::Node;
349 using LeafLBTraits = typename Base::LeafNode::FiniteElement::Traits::LocalBasisType::Traits;
350
352 Dune::Fufem::Impl::Concept::LeafTreeNode<Node>,
353 typename LeafLBTraits::JacobianType,
354 Dune::FieldMatrix<typename LeafLBTraits::RangeFieldType, Node::degree(), LeafLBTraits::dimDomain>>;
355
356 using GeometryJacobianInverse = typename Node::Element::Geometry::JacobianInverse;
357
358 public:
359
360 using Element = typename Base::Element;
361 using Range = typename Impl::GlobalJacobianTraits<ReferenceJacobian, GeometryJacobianInverse>::type;
362
363 using Base::Base;
364
366 : Base(other)
367 {}
368
370 {
371 using Base::LocalOperator::quadratureRuleKey_;
372 using Base::LocalOperator::leafNode;
373 using Base::LocalOperator::leafNodeCache;
375
376 public:
377
379 using CacheManager = typename Base::LocalOperator::CacheManager;
381
382 void bind(const Element&)
383 {
384 if (leafNode().size()!=0)
386 else
387 quadratureRuleKey_ = QuadratureRuleKey(this->localView_->element().type(), 0);
388 }
389
390 void bind(const Intersection& intersection, const Element& element, const Element& otherElement)
391 {
392 bind(element);
393 }
394
395 template<class... OutsideCacheManager>
396 void bindToCaches(CacheManager& cacheManager, OutsideCacheManager&... outsideCacheManager)
397 {
398 globalJacobianCache_ = &(leafNodeCache(cacheManager).getGlobalJacobians());
399 }
400
402 {
403 using namespace Impl::Tensor;
404 using namespace Dune::Indices;
405 const auto& globalJacobians = (*globalJacobianCache_)[index];
406 std::size_t size = leafNode().size();
407 std::size_t indexOffset = (size==0) ? 0 : leafNode().localIndex(0);
408 if constexpr(Dune::Fufem::Impl::Concept::LeafTreeNode<Node>)
409 return SparseTensor(_2,
410 [&, size, indexOffset](auto&& f) {
411 for(std::size_t i=0; i<size; ++i)
412 f(globalJacobians[i], _0, indexOffset+i);
413 },
414 size,
416 );
417 else if constexpr(Dune::Fufem::Impl::Concept::UniformInnerTreeNode<Node>
418 and Dune::Fufem::Impl::Concept::LeafTreeNode<Dune::TypeTree::Child<Node, 0>>)
419 return SparseTensor(_2,
420 [&, size, indexOffset](auto&& f) {
421 for(std::size_t j=0; j<Node::degree(); ++j)
422 for(std::size_t i=0; i<size; ++i)
423 {
424 auto result = Range(0);
425 result[j] = globalJacobians[i][0];
426 f(result, _0, indexOffset + j*size + i);
427 }
428 },
429 Node::degree() * size,
431 );
432 }
433
434 private:
435 const typename Base::LocalOperator::LeafNodeCache::GlobalJacobianCache* globalJacobianCache_ = nullptr;
436 };
437
439 {
440 return LocalOperator(op);
441 }
442
443 template<std::size_t i>
445 {
446 using Basis = typename Base::Basis;
447 auto childTP = Dune::TypeTree::push_back(Base::subspaceBasis_.prefixPath(), childIndex);
449 }
450
452 template<std::size_t k>
454 {
455 return {Base::template rebindArgIndex<k>()};
456 }
457
458 };
459
460
461
471 template<class B, class TP, std::size_t argIndex>
472 class FEFunctionDivergenceOperator : public FEOperatorBase<B, TP, argIndex>
473 {
475 using Node = typename Base::Node;
476 using LeafLBTraits = typename Base::LeafNode::FiniteElement::Traits::LocalBasisType::Traits;
477
478 public:
479
480 using Element = typename Base::Element;
482 Dune::Fufem::Impl::Concept::LeafTreeNode<Node>,
483 typename LeafLBTraits::RangeFieldType,
484 typename LeafLBTraits::JacobianType::block_type>;
485
486 using Base::Base;
487
489 : Base(other)
490 {}
491
493 {
494 using Base::LocalOperator::quadratureRuleKey_;
495 using Base::LocalOperator::leafNode;
496 using Base::LocalOperator::leafNodeCache;
498
499 public:
500
502 using CacheManager = typename Base::LocalOperator::CacheManager;
504
505 void bind(const Element&)
506 {
507 if (leafNode().size()!=0)
509 else
510 quadratureRuleKey_ = QuadratureRuleKey(this->localView_->element().type(), 0);
511 }
512
513 void bind(const Intersection& intersection, const Element& element, const Element& otherElement)
514 {
515 bind(element);
516 }
517
518 template<class... OutsideCacheManager>
519 void bindToCaches(CacheManager& cacheManager, OutsideCacheManager&... outsideCacheManager)
520 {
521 globalJacobianCache_ = &(leafNodeCache(cacheManager).getGlobalJacobians());
522 }
523
525 {
526 using namespace Impl::Tensor;
527 using namespace Dune::Indices;
528 using LocalBasisTraits = typename Base::LeafNode::FiniteElement::Traits::LocalBasisType::Traits;
529 using Field = typename LocalBasisTraits::RangeFieldType;
530 const auto& globalJacobians = (*globalJacobianCache_)[index];
531 std::size_t size = leafNode().size();
532 std::size_t indexOffset = (size==0) ? 0 : leafNode().localIndex(0);
533 if constexpr(Dune::Fufem::Impl::Concept::LeafTreeNode<Node>)
534 return SparseTensor(_2,
535 [&, size, indexOffset](auto&& f) {
536 for(std::size_t i=0; i<size; ++i)
537 {
538 auto div = Field{0};
539 for(std::size_t j=0; j<globalJacobians[i].N(); ++j)
540 div += globalJacobians[i][j][j];
541 f(div, _0, indexOffset+i);
542 }
543 },
544 size,
546 );
547 else if constexpr(Dune::Fufem::Impl::Concept::UniformInnerTreeNode<Node>
548 and Dune::Fufem::Impl::Concept::LeafTreeNode<Dune::TypeTree::Child<Node, 0>>)
549 return SparseTensor(_2,
550 [&, size, indexOffset](auto&& f) {
551 // Here we should compute the trace of the global Jacobian J
552 // that we would need to build first. But since only the
553 // j-th row of J is nonzero, we can simply
554 // return the diagonal entry of this row which coincides
555 // with the j-th entry of the gradient of
556 // the respective scalar basis function.
557 for(std::size_t j=0; j<Node::degree(); ++j)
558 for(std::size_t i=0; i<size; ++i)
559 f(globalJacobians[i][0][j], _0, indexOffset + j*size + i);
560 },
561 Node::degree() * size,
563 );
564 }
565
566 private:
567 const typename Base::LocalOperator::LeafNodeCache::GlobalJacobianCache* globalJacobianCache_ = nullptr;
568 };
569
571 {
572 return LocalOperator(op);
573 }
574
576 template<std::size_t k>
578 {
579 return {Base::template rebindArgIndex<k>()};
580 }
581
582 };
583
584
585
586} // namespace Dune::Fufem::Forms
587
588
589
590#endif // DUNE_FUFEM_FORMS_UNARYOPERATORS_HH
This header provides fallback implementations for dune-typetree<2.11.
int size() const
std::ptrdiff_t index() const
size_t() const
decltype(auto) child(Node &&node, TreePath< Indices... > treePath)
constexpr index_constant< 0 > _0
auto div(const Op &op)
Obtain the divergence of an operator.
Definition userfunctions.hh:1201
STL namespace.
Definition baseclass.hh:22
Base class for unary multilinear operator implementations.
Definition baseclass.hh:91
A hierarchic cache for storing shape function evaluations for a tree.
Definition shapefunctioncache.hh:107
Objects of this class are used to uniquely identifies a cache.
Definition shapefunctioncache.hh:566
A class for managing caches of different types.
Definition shapefunctioncache.hh:612
size_type registerCache(UniqueCacheId uniqueCacheId, Cache &&cache)
Register a new cache.
Definition shapefunctioncache.hh:750
Base class of elementary differential operators on an FE-space.
Definition unaryoperators.hh:61
typename Dune::TypeTree::ChildForTreePath< Tree, TP > Node
Definition unaryoperators.hh:66
typename B::LocalView LocalView
Definition unaryoperators.hh:64
SubspaceBasis subspaceBasis_
Definition unaryoperators.hh:205
typename Dune::TypeTree::ChildForTreePath< Tree, LeafTreePath > LeafNode
Definition unaryoperators.hh:85
B Basis
Definition unaryoperators.hh:86
typename Basis::LocalView::Element Element
Definition unaryoperators.hh:91
bool isAffine_
Definition unaryoperators.hh:206
decltype(leafTreePath(std::declval< TP >())) LeafTreePath
Definition unaryoperators.hh:84
FEOperatorBase(const Basis &basis, const TreePath &treePath, bool isAffine=true)
Definition unaryoperators.hh:94
typename Dune::Functions::SubspaceBasis< B, TP > SubspaceBasis
Definition unaryoperators.hh:67
TP TreePath
Definition unaryoperators.hh:87
static auto leafTreePath(const TP &tp)
Definition unaryoperators.hh:75
typename Basis::GridView::Intersection Intersection
Definition unaryoperators.hh:92
FEOperatorBase< B, TP, k > rebindArgIndex() const
Get a copy of this operator bound to another argument.
Definition unaryoperators.hh:199
typename LocalView::Tree Tree
Definition unaryoperators.hh:65
auto basis() const
Definition unaryoperators.hh:179
auto treePath() const
Definition unaryoperators.hh:184
bool isAffine() const
Definition unaryoperators.hh:189
bool isAffine_
Definition unaryoperators.hh:176
SubspaceBasis subspaceBasis_
Definition unaryoperators.hh:170
void unbind()
Definition unaryoperators.hh:126
LocalOperator(const SubspaceBasis &subspaceBasis, bool isAffine)
Definition unaryoperators.hh:112
void registerOutsideCaches(CacheManager &cacheManager)
Definition unaryoperators.hh:155
LocalOperator(const FEOperatorBase &op)
Definition unaryoperators.hh:122
const LocalView * localView_
Definition unaryoperators.hh:173
typename FEOperatorBase::Element Element
Definition unaryoperators.hh:108
void registerOutsideLocalViews(const LV &... lvs)
Definition unaryoperators.hh:144
void registerCaches(CacheManager &cacheManager)
Definition unaryoperators.hh:147
auto quadratureRuleKey() const
Definition unaryoperators.hh:129
ShapeFunctionCache< Tree > TreeCache
Definition unaryoperators.hh:103
QuadratureRuleKey quadratureRuleKey_
Definition unaryoperators.hh:174
auto & leafNodeCache(CacheManager &cacheManager)
Definition unaryoperators.hh:165
std::size_t cacheIndex_
Definition unaryoperators.hh:175
void registerLocalViews(const LV &... lvs)
Definition unaryoperators.hh:135
const LeafNode * leafNode_
Definition unaryoperators.hh:172
const LeafTreePath leafTreePath_
Definition unaryoperators.hh:171
const LeafNode & leafNode() const
Definition unaryoperators.hh:160
typename FEOperatorBase::Intersection Intersection
Definition unaryoperators.hh:109
Linear map representing the elements of an FE-space.
Definition unaryoperators.hh:222
auto childOperator(Dune::index_constant< i > childIndex) const
Definition unaryoperators.hh:316
typename Base::Element Element
Definition unaryoperators.hh:229
FEFunctionOperator< B, TP, k > rebindArgIndex() const
Get a copy of this operator bound to another argument.
Definition unaryoperators.hh:325
FEFunctionOperator(const Base &other)
Definition unaryoperators.hh:237
friend LocalOperator localOperator(const FEFunctionOperator &op)
Definition unaryoperators.hh:310
std::conditional_t< Dune::Fufem::Impl::Concept::LeafTreeNode< Node >, typename LeafLBTraits::RangeType, Dune::FieldVector< typename LeafLBTraits::RangeFieldType, Node::degree()> > Range
Definition unaryoperators.hh:233
typename Base::LocalOperator::Intersection Intersection
Definition unaryoperators.hh:250
void bind(const Element &)
Definition unaryoperators.hh:254
void bind(const Intersection &intersection, const Element &element, const Element &otherElement)
Definition unaryoperators.hh:262
typename Base::LocalOperator::CacheManager CacheManager
Definition unaryoperators.hh:251
void bindToCaches(CacheManager &cacheManager, OutsideCacheManager &... outsideCacheManager)
Definition unaryoperators.hh:268
auto operator()(std::size_t index) const
Definition unaryoperators.hh:273
typename FEFunctionOperator::Range Range
Definition unaryoperators.hh:252
Linear map representing the jacobians of the elements of an FE-space.
Definition unaryoperators.hh:346
typename Impl::GlobalJacobianTraits< ReferenceJacobian, GeometryJacobianInverse >::type Range
Definition unaryoperators.hh:361
FEFunctionJacobianOperator(const Base &other)
Definition unaryoperators.hh:365
FEFunctionJacobianOperator< B, TP, k > rebindArgIndex() const
Get a copy of this operator bound to another argument.
Definition unaryoperators.hh:453
friend LocalOperator localOperator(const FEFunctionJacobianOperator &op)
Definition unaryoperators.hh:438
auto childOperator(Dune::index_constant< i > childIndex) const
Definition unaryoperators.hh:444
typename Base::Element Element
Definition unaryoperators.hh:360
void bind(const Element &)
Definition unaryoperators.hh:382
void bind(const Intersection &intersection, const Element &element, const Element &otherElement)
Definition unaryoperators.hh:390
typename Base::LocalOperator::Intersection Intersection
Definition unaryoperators.hh:378
typename Base::LocalOperator::CacheManager CacheManager
Definition unaryoperators.hh:379
void bindToCaches(CacheManager &cacheManager, OutsideCacheManager &... outsideCacheManager)
Definition unaryoperators.hh:396
auto operator()(std::size_t index) const
Definition unaryoperators.hh:401
typename FEFunctionJacobianOperator::Range Range
Definition unaryoperators.hh:380
Linear map representing the divergenc of the elements of an FE-space.
Definition unaryoperators.hh:473
typename Base::Element Element
Definition unaryoperators.hh:480
friend LocalOperator localOperator(const FEFunctionDivergenceOperator &op)
Definition unaryoperators.hh:570
FEFunctionDivergenceOperator(const Base &other)
Definition unaryoperators.hh:488
FEFunctionDivergenceOperator< B, TP, k > rebindArgIndex() const
Get a copy of this operator bound to another argument.
Definition unaryoperators.hh:577
std::conditional_t< Dune::Fufem::Impl::Concept::LeafTreeNode< Node >, typename LeafLBTraits::RangeFieldType, typename LeafLBTraits::JacobianType::block_type > Range
Definition unaryoperators.hh:484
void bindToCaches(CacheManager &cacheManager, OutsideCacheManager &... outsideCacheManager)
Definition unaryoperators.hh:519
void bind(const Element &)
Definition unaryoperators.hh:505
void bind(const Intersection &intersection, const Element &element, const Element &otherElement)
Definition unaryoperators.hh:513
typename Base::LocalOperator::Intersection Intersection
Definition unaryoperators.hh:501
typename Base::LocalOperator::CacheManager CacheManager
Definition unaryoperators.hh:502
typename FEFunctionDivergenceOperator::Range Range
Definition unaryoperators.hh:503
auto operator()(std::size_t index) const
Definition unaryoperators.hh:524
A token that specifies a quadrature rule.
Definition quadraturerulecache.hh:39
QuadratureRuleKey derivative() const
Definition quadraturerulecache.hh:154
T forward(T... args)
T tie(T... args)