DUNE PDELab (git)

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 <vector>
9#include <tuple>
10#include <utility>
11#include <type_traits>
12
13#include <dune/common/std/type_traits.hh>
15#include <dune/common/typelist.hh>
16#include <dune/common/hybridutilities.hh>
17
18#include <dune/geometry/type.hh>
20
21#include <dune/localfunctions/common/localfiniteelementvariant.hh>
22
23
24namespace Dune {
25
26namespace Impl {
27
28 // This class provides the index method of LocalGeometryTypeIndex
29 // but throws a Dune::RangeError if the dimension does not match.
30 // This can be helpful to catch errors in a LocalFiniteElementVariantCache
31 // instance based on dimension specific GeometryType indices.
32 template<std::size_t dim>
33 struct FixedDimLocalGeometryTypeIndex {
34 inline static std::size_t index(const GeometryType &gt)
35 {
36 if (gt.dim() != dim)
37 DUNE_THROW(Dune::RangeError, "Asking for dim=" << dim << " specific index of GeometryType with dimension " << gt.dim());
39 }
40 };
41
42} // end namespace Impl
43
66template<class Base>
68{
69
70 template<class LFEImplTuple>
71 struct GenerateLFEVariant;
72
73 template<class Index, class... LFEImpl>
74 struct GenerateLFEVariant<std::tuple<std::pair<Index, LFEImpl>...>>
75 {
77 };
78
79 using Base::getImplementations;
80 using Base::index;
81 using Implementations = decltype(std::declval<Base>().getImplementations());
82
83public:
84
92 using FiniteElementType = typename GenerateLFEVariant<Implementations>::type;
93
98 template<class... Args>
100 Base(std::forward<Args>(args)...)
101 {
102 Dune::Hybrid::forEach(getImplementations(), [&,this](auto feImpl) {
103 auto implIndex = feImpl.first;
104 if (cache_.size() < implIndex+1)
105 cache_.resize(implIndex+1);
106 cache_[implIndex] = feImpl.second();
107 });
108 }
109
112
115
118
121
126 template<class... Key>
127 const auto& get(const Key&... key) const
128 {
129 auto implIndex = index(key...);
130 if (implIndex >= cache_.size())
131 DUNE_THROW(Dune::RangeError,"There is no LocalFiniteElement of the requested type.");
132 if (not(cache_[implIndex]))
133 DUNE_THROW(Dune::RangeError,"There is no LocalFiniteElement of the requested type.");
134 return cache_[implIndex];
135 }
136
137private:
138 std::vector<FiniteElementType> cache_;
139};
140
141
142
143} // namespace Dune
144
145
146
147
148#endif // DUNE_LOCALFUNCTIONS_COMMON_LOCALFINITEELEMENTVARIANT_HH
A cache storing a compile time selection of local finite element implementations.
Definition: localfiniteelementvariantcache.hh:68
LocalFiniteElementVariantCache & operator=(const LocalFiniteElementVariantCache &other)=default
Copy assignment.
LocalFiniteElementVariantCache(Args &&... args)
Default constructor.
Definition: localfiniteelementvariantcache.hh:99
LocalFiniteElementVariantCache(LocalFiniteElementVariantCache &&other)=default
Move constructor.
const auto & get(const Key &... key) const
Get the LocalFiniteElement for the given key data.
Definition: localfiniteelementvariantcache.hh:127
LocalFiniteElementVariantCache(const LocalFiniteElementVariantCache &other)=default
Copy constructor.
typename GenerateLFEVariant< Implementations >::type FiniteElementType
Type of exported LocalFiniteElement's.
Definition: localfiniteelementvariantcache.hh:92
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:254
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
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:256
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  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)