Dune-Fufem 2.12-git
Loading...
Searching...
No Matches
shapefunctioncache.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_SHAPEFUNCTIONCACHE_HH
8#define DUNE_FUFEM_FORMS_SHAPEFUNCTIONCACHE_HH
9
10#include <any>
11#include <cassert>
12#include <iostream>
13#include <list>
14#include <map>
15#include <memory>
16#include <type_traits>
17#include <typeindex>
18#include <utility>
19#include <vector>
20
25
28
29
30namespace Dune::Fufem::Forms::Impl {
31
32 template<class C, class... T>
33 static constexpr decltype(auto) accessByTreePath(C&& container, const Dune::TypeTree::TreePath<T...>& path)
34 {
35 if constexpr (sizeof...(T)==0)
36 return container;
37 else
38 return accessByTreePath(container[path.front()], pop_front(path));
39 }
40
41 template<class ReferenceJacobian, class GeometryJacobianInverse>
42 struct GlobalJacobianTraits
43 {
44 using GeometryField = typename GeometryJacobianInverse::field_type;
45 using ReferenceJacobianField = typename ReferenceJacobian::field_type;
47 static constexpr int dimWorld = GeometryJacobianInverse::cols;
49 };
50
51 template<class GeometryJacobianInverse, class JF, int dimRange, int dim>
52 struct GlobalJacobianTraits<Dune::FieldMatrix<JF, dimRange, dim>, GeometryJacobianInverse>
53 {
54 using GeometryField = typename GeometryJacobianInverse::field_type;
55 using ReferenceJacobianField = JF;
57 static constexpr int dimWorld = GeometryJacobianInverse::cols;
59 };
60
61
62
63} // end namespace Dune::Fufem::Forms::Impl
64
65
66
67namespace Dune::Fufem::Forms {
68
69
70
105 template<class Node, class CT=double>
107
108
109
110 template<class Node, class CT>
112 class ShapeFunctionCache<Node, CT>
113 {
114 using GeometryJacobianInverse = typename Node::Element::Geometry::JacobianInverse;
115 public:
116
118
119 using FEValue = typename Node::FiniteElement::Traits::LocalBasisType::Traits::RangeType;
120 using FEJacobian = typename Node::FiniteElement::Traits::LocalBasisType::Traits::JacobianType;
121 using FEGlobalJacobian = typename Impl::GlobalJacobianTraits<FEJacobian, GeometryJacobianInverse>::type;
122
126
127 ShapeFunctionCache(const QuadratureRule& rule, const Node& node)
128 {
129 setRule(rule);
130 setTree(node);
131 }
132
134 node_(nullptr)
135 {
136 setRule(rule);
137 }
138
139 ShapeFunctionCache(const Node& node) :
140 rule_(nullptr)
141 {
142 setTree(node);
143 }
144
145 ShapeFunctionCache(const ShapeFunctionCache& other) = default;
146
148
149 void setRule(const QuadratureRule& rule)
150 {
151 rule_ = &rule;
152 valueCache_.resize(rule_->size());
153 jacobianCache_.resize(rule_->size());
154 globalJacobianCache_.resize(rule_->size());
155 valueCache_[0].clear();
156 jacobianCache_[0].clear();
157 globalJacobianCache_[0].clear();
158 }
159
160 void setTree(const Node& node)
161 {
162 node_ = &node;
163 }
164
166 {
167 isNonAffine_ = true;
168 }
169
170 const QuadratureRule& rule() const
171 {
172 return *rule_;
173 }
174
176 {
177 // We use the size at the first quadrature point to indicate
178 // an invalidated cache. To this end we set it's size to zero.
179 if (isNonAffine_)
180 {
181 valueCache_[0].clear();
182 jacobianCache_[0].clear();
183 }
184 globalJacobianCache_[0].clear();
185 }
186
187 decltype(auto) operator[](const Dune::TypeTree::TreePath<>& treePath) const
188 {
189 return *this;
190 }
191
192 decltype(auto) operator[](const Dune::TypeTree::TreePath<>& treePath)
193 {
194 return *this;
195 }
196
197 const auto& getValues()
198 {
199 if (valueCache_[0].empty())
200 {
201 if (node_->size() == 0)
202 return valueCache_;
203 const auto& localBasis = node_->finiteElement().localBasis();
204 for(std::size_t k=0; k<rule_->size(); ++k)
205 localBasis.evaluateFunction((*rule_)[k].position(), valueCache_[k]);
206 }
207 return valueCache_;
208 }
209
210 const auto& getJacobians()
211 {
212 if (jacobianCache_[0].empty())
213 {
214 if (node_->size() == 0)
215 return jacobianCache_;
216 const auto& localBasis = node_->finiteElement().localBasis();
217 for(std::size_t k=0; k<rule_->size(); ++k)
218 localBasis.evaluateJacobian((*rule_)[k].position(), jacobianCache_[k]);
219 }
220 return jacobianCache_;
221 }
222
223 const auto& getGlobalJacobians()
224 {
225 if (globalJacobianCache_[0].empty())
226 {
227 if (node_->size() == 0)
228 return globalJacobianCache_;
229 const auto& geometry = node_->element().geometry();
230 const auto& jacobians = getJacobians();
231 for(std::size_t k=0; k<rule_->size(); ++k)
232 {
233 globalJacobianCache_[k].resize(jacobians[k].size());
234 const auto& jacobianInverse = geometry.jacobianInverse((*rule_)[k].position());
235 for(std::size_t i=0; i<jacobians[k].size(); ++i)
236 globalJacobianCache_[k][i] = jacobians[k][i] * jacobianInverse;
237 }
238 }
239 return globalJacobianCache_;
240 }
241
242 private:
243 const QuadratureRule* rule_ = nullptr;
244 const Node* node_ = nullptr;
245 ValueCache valueCache_;
246 JacobianCache jacobianCache_;
247 GlobalJacobianCache globalJacobianCache_;
248 bool isNonAffine_ = false;
249 };
250
251
252
253 template<class Node, class CT>
255 class ShapeFunctionCache<Node, CT>
256 {
257 using ChildNode = Dune::TypeTree::Child<Node,0>;
258 using ChildCache = ShapeFunctionCache<ChildNode, CT>;
259 public:
260
262
263 ShapeFunctionCache(const QuadratureRule& rule, const Node& node) :
264 childCache_(rule, node.child(0))
265 {}
266
268 childCache_(rule)
269 {}
270
271 ShapeFunctionCache(const Node& node) :
272 childCache_(node.child(0))
273 {}
274
275 ShapeFunctionCache(const ShapeFunctionCache& other) = default;
276
278
279 void setRule(const QuadratureRule& rule)
280 {
281 childCache_.setRule(rule);
282 }
283
284 void setTree(const Node& node)
285 {
286 childCache_.setTree(node.child(0));
287 }
288
290 {
291 childCache_.setNonAffine();
292 }
293
294 const QuadratureRule& rule() const
295 {
296 return childCache_.rule();
297 }
298
300 {
301 childCache_.invalidate();
302 }
303
305 {
306 assert(i<Node::degree());
307 return childCache_;
308 }
309
311 {
312 assert(i<Node::degree());
313 return childCache_;
314 }
315
316 template<class... T>
317 decltype(auto) operator[](const Dune::TypeTree::TreePath<T...>& treePath) const
318 {
319 return Impl::accessByTreePath(*this, treePath);
320 }
321
322 template<class... T>
323 decltype(auto) operator[](const Dune::TypeTree::TreePath<T...>& treePath)
324 {
325 return Impl::accessByTreePath(*this, treePath);
326 }
327
328 constexpr std::size_t size() const noexcept
329 {
330 return Node::degree();
331 }
332
333 private:
334 ChildCache childCache_;
335 };
336
337
338
339 namespace Impl {
340
341 template<class Node, class CT, class Indices>
342 struct TupleVectorOfChildShapeFunctionCaches
343 {};
344
345 template<class Node, class CT, std::size_t... indices>
346 struct TupleVectorOfChildShapeFunctionCaches<Node, CT, std::index_sequence<indices...>>
347 {
349 };
350
351 template<class Node>
352 using ChildIndices = std::make_index_sequence<decltype(Node::degree())::value>;
353
354 template<class Node, class CT>
355 using TupleVectorOfChildShapeFunctionCaches_t = typename TupleVectorOfChildShapeFunctionCaches<Node, CT, ChildIndices<Node>>::type;
356
357 } // end namespace Imp
358
359
360
361 template<class Node, class CT>
364 class ShapeFunctionCache<Node, CT>
365 : public Impl::TupleVectorOfChildShapeFunctionCaches_t<Node, CT>
366 {
367 using Base = Impl::TupleVectorOfChildShapeFunctionCaches_t<Node, CT>;
368 public:
369
371
372 ShapeFunctionCache(const QuadratureRule& rule, const Node& node)
373 {
374 setRule(rule);
375 setTree(node);
376 }
377
379 {
380 setRule(rule);
381 }
382
383 ShapeFunctionCache(const Node& node)
384 {
385 setTree(node);
386 }
387
388
389 ShapeFunctionCache(const ShapeFunctionCache& other) = default;
390
392
393 void setRule(const QuadratureRule& rule)
394 {
395 Hybrid::forEach(*this, [&](auto& childCache) {
396 childCache.setRule(rule);
397 });
398 }
399
400 void setTree(const Node& node)
401 {
402 Hybrid::forEach(Dune::range(Node::degree()), [&](const auto& i){
403 (*this)[i].setTree(node.child(i));
404 });
405 }
406
408 {
409 Hybrid::forEach(Dune::range(Node::degree()), [&](const auto& i){
410 (*this)[i].setNonAffine();
411 });
412 }
413
414 const QuadratureRule& rule() const
415 {
416 return (*this)[Dune::Indices::_0].rule();
417 }
418
419 using Base::operator[];
420
421 template<class... T>
422 decltype(auto) operator[](const Dune::TypeTree::TreePath<T...>& treePath) const
423 {
424 return Impl::accessByTreePath(*this, treePath);
425 }
426
427 template<class... T>
428 decltype(auto) operator[](const Dune::TypeTree::TreePath<T...>& treePath)
429 {
430 return Impl::accessByTreePath(*this, treePath);
431 }
432
433 void invalidate() {
434 Hybrid::forEach(*this, [&](auto& childCache) {
435 childCache.invalidate();
436 });
437 }
438
439 };
440
441
442
464 template<class C>
466 {
467 using Cache = C;
468 using CT = typename Cache::QuadratureRule::CoordType;
469 constexpr static int dimension = Cache::QuadratureRule::d;
470
473
474 public:
475
478
479 private:
480
481 template<class Key>
482 const QuadratureRule& getRule(const Key& key)
483 {
486 else
487 {
488 auto facet = key.second;
489 auto&& type = key.first.geometryType();
490 auto facetGeometryType = Dune::referenceElement<CT,dimension>(type).type(facet, 1);
491 auto facetQuadratureRuleKey = key.first;
492 facetQuadratureRuleKey.setGeometryType(facetGeometryType);
493 const auto& rule = QuadratureRuleCache<CT, dimension-1>::rule(facetQuadratureRuleKey);
494 // We need to store the face quadrature rule.
495 // By using an std::list, we can store pointers
496 // to the stored rules, because they never get invalidated.
497 facetQuadratureRules_.push_back(FacetQuadratureRule(type, facet, rule));
498 return facetQuadratureRules_.back();
499 }
500 }
501
502 template<class Key>
503 Cache makeCache(const Key& key)
504 {
505 auto cache = Cache(prototype_);
506 cache.setRule(getRule(key));
507 return cache;
508 }
509
510 public:
511
512 Cache& operator[](const QuadratureRuleKey& key) {
513 auto it = elementCacheMap_.find(key);
514 if (it == elementCacheMap_.end())
515 return (elementCacheMap_.insert(std::make_pair(key, makeCache(key)))).first->second;
516 else
517 return it->second;
518 }
519
520 Cache& operator[](const FacetKey& facetKey) {
521 auto it = facetCacheMap_.find(facetKey);
522 if (it == facetCacheMap_.end())
523 return (facetCacheMap_.insert(std::make_pair(facetKey, makeCache(facetKey)))).first->second;
524 else
525 return it->second;
526 }
527
528 Cache& prototype()
529 {
530 return prototype_;
531 }
532
533 void clear()
534 {
535 prototype_.clear();
536 elementCacheMap_.clear();
537 facetCacheMap_.clear();
538 facetQuadratureRules_.clear();
539 }
540
541 private:
542 std::map<ElementKey, Cache> elementCacheMap_;
543 std::map<FacetKey, Cache> facetCacheMap_;
544 Cache prototype_;
545 std::list<FacetQuadratureRule> facetQuadratureRules_;
546 };
547
548
549
570 class UniqueCacheId : public std::pair<const void*, std::type_index>
571 {
573 public:
574
583 template<class T>
584 UniqueCacheId(const T& t) :
585 Base(&t, std::type_index(typeid(T)))
586 {}
587
597 {
598 auto sharedState = std::make_shared<int>(0);
599 auto id = UniqueCacheId(*sharedState);
600 id.sharedState_ = std::move(sharedState);
601 return id;
602 }
603
604 private:
605 std::shared_ptr<int> sharedState_;
606 };
607
642 template<class CT, int dimension>
644 {
645 public:
648
649 private:
650
651 // Simple type erasure for stored caches.
652 class TypeErasedCache
653 {
654 struct {
655 std::any impl_;
656 void*(*toRawPtr_)(std::any&);
657 void(*invalidate_)(void*);
658 void(*setRule_)(void*, const QuadratureRule&);
659 } members_;
660 void* rawPtr_;
661
662 void* toRawPtr()
663 {
664 return members_.toRawPtr_(members_.impl_);
665 }
666
667 public:
668
669 template<class Impl>
670 TypeErasedCache(Impl&& impl)
671 : members_{
672 std::move(impl),
673 [](std::any& impl) -> void* { return &std::any_cast<Impl&>(impl); },
674 [](void* impl) { static_cast<Impl*>(impl)->invalidate(); },
675 [](void* impl, const QuadratureRule& rule) { static_cast<Impl*>(impl)->setRule(rule); },
676 }
677 , rawPtr_(toRawPtr())
678 {}
679
680 TypeErasedCache(TypeErasedCache&& other)
681 : members_(std::move(other.members_))
682 , rawPtr_(toRawPtr())
683 {}
684
685 TypeErasedCache(const TypeErasedCache& other)
686 : members_(other.members_)
687 , rawPtr_(toRawPtr())
688 {}
689
690 TypeErasedCache(TypeErasedCache& other)
691 : members_(other.members_)
692 , rawPtr_(toRawPtr())
693 {}
694
695 void invalidate()
696 {
697 members_.invalidate_(rawPtr_);
698 }
699
700 void setRule(const QuadratureRule& rule)
701 {
702 members_.setRule_(rawPtr_, rule);
703 }
704
705 template<class Impl>
706 Impl& get()
707 {
708 return *static_cast<Impl*>(rawPtr_);
709 }
710 };
711
712 const QuadratureRule* rule_ = nullptr;
715
716 public:
717
718 CacheManager() = default;
719 CacheManager(CacheManager&& other) = default;
720 CacheManager(const CacheManager& other) = default;
721
724
728 void clear()
729 {
730 rule_ = nullptr;
731 cacheIndex_.clear();
732 caches_.clear();
733 }
734
738 const QuadratureRule& rule() const
739 {
740 return *rule_;
741 }
742
747 {
748 rule_ = &rule;
749 for(auto index : Dune::range(caches_.size()))
750 caches_[index].setRule(rule);
751 }
752
757 {
758 for(auto index : Dune::range(caches_.size()))
759 caches_[index].invalidate();
760 }
761
762 // For diagnostics: Print number of stored caches
763 void report() const
764 {
765 std::cout << "Number of managed caches " << caches_.size() << std::endl;
766 }
767
781 template<class Cache>
782 size_type registerCache(UniqueCacheId uniqueCacheId, Cache&& cache)
783 {
784 auto it = cacheIndex_.find(uniqueCacheId);
785 if (it == cacheIndex_.end())
786 {
787 caches_.push_back(std::forward<Cache>(cache));
788 cacheIndex_[uniqueCacheId] = caches_.size()-1;
789 return caches_.size()-1;
790 }
791 else
792 return it->second;
793 }
794
805 template<class Cache>
807 {
808 return caches_[index].template get<Cache>();
809 }
810
811 };
812
813
814
820 template<class CT, int dimension>
822
823
824
835 template<class CT, int dimension, class V>
837 {
838 public:
839
841
842 using Value = V;
844
845 SimpleCache(bool isNonAffine) :
846 isNonAffine_(isNonAffine)
847 {}
848
849 SimpleCache(const SimpleCache& other) = default;
850
852 {
853 rule_ = &rule;
854 valueCache_.resize(rule_->size());
855 isEmpty_ = true;
856 }
857
858 const QuadratureRule& rule() const
859 {
860 return *rule_;
861 }
862
864 {
865 isNonAffine_ = true;
866 }
867
869 {
870 if (isNonAffine_)
871 isEmpty_ = true;
872 }
873
874 bool isEmpty()
875 {
876 return isEmpty_;
877 }
878
879 void setEmpty(bool isEmpty)
880 {
881 isEmpty_ = isEmpty;
882 }
883
884 auto& getValues()
885 {
886 return valueCache_;
887 }
888
889 const auto& getValues() const
890 {
891 return valueCache_;
892 }
893
894 private:
895 const QuadratureRule* rule_ = nullptr;
896 ValueCache valueCache_;
897 bool isEmpty_ = true;
898 bool isNonAffine_ = false;
899 };
900
901
902
903} // namespace Dune::Fufem::Forms
904
905
906#endif // DUNE_FUFEM_FORMS_SHAPEFUNCTIONCACHE_HH
907
int id()
void invalidate()
int size() const
bool empty() const
void pop_front()
static constexpr IntegralRange< T > range(T from, T to) noexcept
constexpr void forEach(Range &&range, F &&f)
std::ptrdiff_t index() const
typename Impl::ChildTraits< Node, indices... >::type Child
constexpr auto get(std::integer_sequence< T, II... >, std::integral_constant< std::size_t, pos >={})
constexpr index_constant< 0 > _0
constexpr auto front() const
decltype(std::declval< T1 >()+std::declval< T2 >()) PromotedType
A class for managing caches of different types.
Definition shapefunctioncache.hh:644
auto & getCache(size_type index)
Obtain a registered cache.
Definition shapefunctioncache.hh:806
CacheManager(const CacheManager &other)=default
void invalidate()
Invalidate all stores caches.
Definition shapefunctioncache.hh:756
CacheManager & operator=(const CacheManager &)=delete
size_type registerCache(UniqueCacheId uniqueCacheId, Cache &&cache)
Register a new cache.
Definition shapefunctioncache.hh:782
void report() const
Definition shapefunctioncache.hh:763
void setRule(const QuadratureRule &rule)
Set the associated quadrature rule for all stored caches.
Definition shapefunctioncache.hh:746
CacheManager(CacheManager &&other)=default
const QuadratureRule & rule() const
Obtain the associated quadrature rule.
Definition shapefunctioncache.hh:738
void clear()
Clear all stored data.
Definition shapefunctioncache.hh:728
A cache providing multiple versions for different quadrature rules.
Definition shapefunctioncache.hh:466
Cache & operator[](const QuadratureRuleKey &key)
Definition shapefunctioncache.hh:512
Cache & operator[](const FacetKey &facetKey)
Definition shapefunctioncache.hh:520
void clear()
Definition shapefunctioncache.hh:533
Cache & prototype()
Definition shapefunctioncache.hh:528
const auto & getValues()
Definition shapefunctioncache.hh:197
ShapeFunctionCache(const QuadratureRule &rule, const Node &node)
Definition shapefunctioncache.hh:127
const auto & getJacobians()
Definition shapefunctioncache.hh:210
ShapeFunctionCache(const Node &node)
Definition shapefunctioncache.hh:139
ChildCache & operator[](std::size_t i)
Definition shapefunctioncache.hh:310
const auto & getGlobalJacobians()
Definition shapefunctioncache.hh:223
typename std::vector< std::vector< FEValue > > ValueCache
Definition shapefunctioncache.hh:123
typename Node::FiniteElement::Traits::LocalBasisType::Traits::JacobianType FEJacobian
Definition shapefunctioncache.hh:120
typename Node::FiniteElement::Traits::LocalBasisType::Traits::RangeType FEValue
Definition shapefunctioncache.hh:119
ShapeFunctionCache(const QuadratureRule &rule)
Definition shapefunctioncache.hh:133
ShapeFunctionCache(const ShapeFunctionCache &other)=default
typename std::vector< std::vector< FEJacobian > > JacobianCache
Definition shapefunctioncache.hh:124
void setNonAffine()
Definition shapefunctioncache.hh:165
void setTree(const Node &node)
Definition shapefunctioncache.hh:160
void invalidate()
Definition shapefunctioncache.hh:175
constexpr std::size_t size() const noexcept
Definition shapefunctioncache.hh:328
typename std::vector< std::vector< FEGlobalJacobian > > GlobalJacobianCache
Definition shapefunctioncache.hh:125
const ChildCache & operator[](std::size_t i) const
Definition shapefunctioncache.hh:304
const QuadratureRule & rule() const
Definition shapefunctioncache.hh:170
void setRule(const QuadratureRule &rule)
Definition shapefunctioncache.hh:149
typename Impl::GlobalJacobianTraits< FEJacobian, GeometryJacobianInverse >::type FEGlobalJacobian
Definition shapefunctioncache.hh:121
A hierarchic cache for storing shape function evaluations for a tree.
Definition shapefunctioncache.hh:106
A simple cache implementation storing values.
Definition shapefunctioncache.hh:837
auto & getValues()
Definition shapefunctioncache.hh:884
SimpleCache(bool isNonAffine)
Definition shapefunctioncache.hh:845
bool isEmpty()
Definition shapefunctioncache.hh:874
void setEmpty(bool isEmpty)
Definition shapefunctioncache.hh:879
V Value
Definition shapefunctioncache.hh:842
void invalidate()
Definition shapefunctioncache.hh:868
void setNonAffine()
Definition shapefunctioncache.hh:863
const QuadratureRule & rule() const
Definition shapefunctioncache.hh:858
void setRule(const QuadratureRule &rule)
Definition shapefunctioncache.hh:851
SimpleCache(const SimpleCache &other)=default
const auto & getValues() const
Definition shapefunctioncache.hh:889
typename std::vector< Value > ValueCache
Definition shapefunctioncache.hh:843
Objects of this class are used to uniquely identifies a cache.
Definition shapefunctioncache.hh:571
UniqueCacheId(const T &t)
Create UniqueCacheId associated to given object.
Definition shapefunctioncache.hh:584
static UniqueCacheId makeUniqueCacheId()
Create a globally unique UniqueCacheId.
Definition shapefunctioncache.hh:596
Quadrature rule for a subentity of some given geometry type.
Definition subentityquadraturerule.hh:31
Definition quadraturerulecache.hh:200
static const Dune::QuadratureRule< coord_type, dim > & rule(const Dune::GeometryType &gt, const int order, int refinement)
Definition quadraturerulecache.hh:206
A token that specifies a quadrature rule.
Definition quadraturerulecache.hh:40
T clear(T... args)
T end(T... args)
T endl(T... args)
T find(T... args)
T forward(T... args)
T make_pair(T... args)
Definition baseclass.hh:22
STL namespace.
T push_back(T... args)
T size(T... args)