DUNE PDELab (unstable)

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// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5#ifndef DUNE_LOCALFUNCTIONS_COMMON_LOCALFINITEELEMENTVARIANTCACHE_HH
6#define DUNE_LOCALFUNCTIONS_COMMON_LOCALFINITEELEMENTVARIANTCACHE_HH
7
8#include <limits>
9#include <vector>
10#include <tuple>
11#include <utility>
12#include <type_traits>
13
14#include <dune/common/std/type_traits.hh>
16#include <dune/common/typelist.hh>
17#include <dune/common/hybridutilities.hh>
18
19#include <dune/geometry/type.hh>
21
22#include <dune/localfunctions/common/localfiniteelementvariant.hh>
23
24
25namespace Dune {
26
27namespace Impl {
28
29 // This class provides the index method of LocalGeometryTypeIndex
30 // but throws a Dune::RangeError if the dimension does not match.
31 // This can be helpful to catch errors in a LocalFiniteElementVariantCache
32 // instance based on dimension specific GeometryType indices.
33 template<std::size_t dim>
34 struct FixedDimLocalGeometryTypeIndex {
35 inline static std::size_t index(const GeometryType &gt)
36 {
37 if (gt.dim() != dim)
38 DUNE_THROW(Dune::RangeError, "Asking for dim=" << dim << " specific index of GeometryType with dimension " << gt.dim());
40 }
41 };
42
43} // end namespace Impl
44
73template<class Base>
75{
76
77 template<class LFEImplTuple>
78 struct GenerateLFEVariant;
79
80 template<class Index, class... LFEImpl>
81 struct GenerateLFEVariant<std::tuple<std::pair<Index, LFEImpl>...>>
82 {
84 };
85
86 using Base::getImplementations;
87 using Base::index;
88 using Implementations = decltype(std::declval<Base>().getImplementations());
89
90public:
91
99 using FiniteElementType = typename GenerateLFEVariant<Implementations>::type;
100
105 template<class... Args>
107 Base(std::forward<Args>(args)...)
108 {
109 Dune::Hybrid::forEach(getImplementations(), [&,this](auto feImpl) {
110 auto implIndex = feImpl.first;
111 constexpr auto unusedIndex = std::numeric_limits<decltype(implIndex)>::max();
112 if (implIndex == unusedIndex)
113 return;
114 if (cache_.size() < implIndex+1)
115 cache_.resize(implIndex+1);
116 cache_[implIndex] = feImpl.second();
117 });
118 }
119
122
125
128
131
136 template<class... Key>
137 const auto& get(const Key&... key) const
138 {
139 auto implIndex = index(key...);
140 if (implIndex >= cache_.size())
141 DUNE_THROW(Dune::RangeError,"There is no LocalFiniteElement of the requested type.");
142 if (not(cache_[implIndex]))
143 DUNE_THROW(Dune::RangeError,"There is no LocalFiniteElement of the requested type.");
144 return cache_[implIndex];
145 }
146
147private:
148 std::vector<FiniteElementType> cache_;
149};
150
151
152
153} // namespace Dune
154
155
156
157
158#endif // DUNE_LOCALFUNCTIONS_COMMON_LOCALFINITEELEMENTVARIANT_HH
A cache storing a compile time selection of local finite element implementations.
Definition: localfiniteelementvariantcache.hh:75
LocalFiniteElementVariantCache & operator=(const LocalFiniteElementVariantCache &other)=default
Copy assignment.
LocalFiniteElementVariantCache(Args &&... args)
Default constructor.
Definition: localfiniteelementvariantcache.hh:106
LocalFiniteElementVariantCache(LocalFiniteElementVariantCache &&other)=default
Move constructor.
const auto & get(const Key &... key) const
Get the LocalFiniteElement for the given key data.
Definition: localfiniteelementvariantcache.hh:137
LocalFiniteElementVariantCache(const LocalFiniteElementVariantCache &other)=default
Copy constructor.
typename GenerateLFEVariant< Implementations >::type FiniteElementType
Type of exported LocalFiniteElement's.
Definition: localfiniteelementvariantcache.hh:99
static constexpr std::size_t index(const GeometryType &gt)
Compute the index for the given geometry type within its dimension.
Definition: typeindex.hh:73
Default exception class for range errors.
Definition: exceptions.hh:348
A few common exception classes.
#define DUNE_THROW(E,...)
Definition: exceptions.hh:314
bool gt(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first greater than second
Definition: float_cmp.cc:158
constexpr void forEach(Range &&range, F &&f)
Range based for loop.
Definition: hybridutilities.hh:261
constexpr auto max
Function object that returns the greater of the given values.
Definition: hybridutilities.hh:489
Dune namespace
Definition: alignedallocator.hh:13
typename Impl::UniqueTypesHelper< Target, TypeList< T... > >::type UniqueTypes_t
Remove duplicates from a list of types.
Definition: typelist.hh:220
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 & Uni Heidelberg  |  generated with Hugo v0.111.3 (May 19, 22:31, 2026)