Dune Core Modules (2.8.0)

localfiniteelementvariantcache.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_LOCALFUNCTIONS_COMMON_LOCALFINITEELEMENTVARIANTCACHE_HH
4#define DUNE_LOCALFUNCTIONS_COMMON_LOCALFINITEELEMENTVARIANTCACHE_HH
5
6#include <vector>
7#include <tuple>
8#include <utility>
9#include <type_traits>
10
11#include <dune/common/std/type_traits.hh>
13#include <dune/common/typelist.hh>
14#include <dune/common/hybridutilities.hh>
15
16#include <dune/geometry/type.hh>
18
19#include <dune/localfunctions/common/localfiniteelementvariant.hh>
20
21
22namespace Dune {
23
24namespace Impl {
25
26 // This class provides the index method of LocalGeometryTypeIndex
27 // but throws a Dune::RangeError if the dimension does not match.
28 // This can be helpful to catch errors in a LocalFiniteElementVariantCache
29 // instance based on dimension specific GeometryType indices.
30 template<std::size_t dim>
31 struct FixedDimLocalGeometryTypeIndex {
32 inline static std::size_t index(const GeometryType &gt)
33 {
34 if (gt.dim() != dim)
35 DUNE_THROW(Dune::RangeError, "Asking for dim=" << dim << " specific index of GeometryType with dimension " << gt.dim());
37 }
38 };
39
40} // end namespace Impl
41
64template<class Base>
66{
67
68 template<class LFEImplTuple>
69 struct GenerateLFEVariant;
70
71 template<class Index, class... LFEImpl>
72 struct GenerateLFEVariant<std::tuple<std::pair<Index, LFEImpl>...>>
73 {
75 };
76
77 using Base::getImplementations;
78 using Base::index;
79 using Implementations = decltype(std::declval<Base>().getImplementations());
80
81public:
82
90 using FiniteElementType = typename GenerateLFEVariant<Implementations>::type;
91
96 template<class... Args>
98 Base(std::forward<Args>(args)...)
99 {
100 Dune::Hybrid::forEach(getImplementations(), [&,this](auto feImpl) {
101 auto implIndex = feImpl.first;
102 if (cache_.size() < implIndex+1)
103 cache_.resize(implIndex+1);
104 cache_[implIndex] = feImpl.second();
105 });
106 }
107
110
113
118 template<class... Key>
119 const auto& get(const Key&... key) const
120 {
121 auto implIndex = index(key...);
122 if (implIndex >= cache_.size())
123 DUNE_THROW(Dune::RangeError,"There is no LocalFiniteElement of the requested type.");
124 if (not(cache_[implIndex]))
125 DUNE_THROW(Dune::RangeError,"There is no LocalFiniteElement of the requested type.");
126 return cache_[implIndex];
127 }
128
129private:
130 std::vector<FiniteElementType> cache_;
131};
132
133
134
135} // namespace Dune
136
137
138
139
140#endif // DUNE_LOCALFUNCTIONS_COMMON_LOCALFINITEELEMENTVARIANT_HH
A cache storing a compile time selection of local finite element implementations.
Definition: localfiniteelementvariantcache.hh:66
LocalFiniteElementVariantCache(Args &&... args)
Default constructor.
Definition: localfiniteelementvariantcache.hh:97
LocalFiniteElementVariantCache(LocalFiniteElementVariantCache &&other)=default
Move constructor.
const auto & get(const Key &... key) const
Get the LocalFiniteElement for the given key data.
Definition: localfiniteelementvariantcache.hh:119
LocalFiniteElementVariantCache(const LocalFiniteElementVariantCache &other)=default
Copy constructor.
typename GenerateLFEVariant< Implementations >::type FiniteElementType
Type of exported LocalFiniteElement's.
Definition: localfiniteelementvariantcache.hh:90
static constexpr std::size_t index(const GeometryType &gt)
Compute the index for the given geometry type within its dimension.
Definition: typeindex.hh:71
Default exception class for range errors.
Definition: exceptions.hh:252
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
bool gt(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first greater than second
Definition: float_cmp.cc:156
constexpr void forEach(Range &&range, F &&f)
Range based for loop.
Definition: hybridutilities.hh:266
Dune namespace.
Definition: alignedallocator.hh:11
typename Impl::UniqueTypesHelper< Target, TypeList< T... > >::type UniqueTypes_t
Remove duplicates from a list of types.
Definition: typelist.hh:218
STL namespace.
A unique label for each type of element that can occur in a grid.
Helper classes to provide indices for geometrytypes for use in a vector.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 21, 23:30, 2024)