| 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> |
| 14 |
#include <dune/common/exceptions.hh> |
| 15 |
#include <dune/common/typelist.hh> |
| 16 |
#include <dune/common/hybridutilities.hh> |
| 17 |
|
| 18 |
#include <dune/geometry/type.hh> |
| 19 |
#include <dune/geometry/typeindex.hh> |
| 20 |
|
| 21 |
#include <dune/localfunctions/common/localfiniteelementvariant.hh> |
| 22 |
|
| 23 |
|
| 24 |
namespace Dune { |
| 25 |
|
| 26 |
namespace 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 >) |
| 35 |
{ |
| 36 |
if (gt.dim() != dim) |
| 37 |
DUNE_THROW(Dune::RangeError, "Asking for dim=" << dim << " specific index of GeometryType with dimension " << gt.dim()); |
| 38 |
return LocalGeometryTypeIndex::index(gt); |
| 39 |
} |
| 40 |
}; |
| 41 |
|
| 42 |
} // end namespace Impl |
| 43 |
|
| 44 |
/** \brief A cache storing a compile time selection of local finite element implementations |
| 45 |
* |
| 46 |
* This class stores a compile time selection of LocalFiniteElement implementations. |
| 47 |
* The exported FiniteElementType is a LocalFiniteElementVariant<Implementations...>. |
| 48 |
* |
| 49 |
* The base class is required to implement two methods: getImplementations() and index(). |
| 50 |
* The index(key...) method computes an index that uniquely identifies a LocalFiniteElement |
| 51 |
* implementation for the provided key data. |
| 52 |
* The getImplementations() method returns a std::tuple with one entry for each |
| 53 |
* LocalFiniteElement implementation. Each entry is an std::pair consisting of the |
| 54 |
* index of the implementation and a callable object that creates a corresponding |
| 55 |
* LocalFiniteElement implementation. |
| 56 |
* |
| 57 |
* The constructor forwards all arguments to the base class. |
| 58 |
* |
| 59 |
* For performance reasons all LocalFiniteElement implementations are created |
| 60 |
* during construction and stored in an std::vector according to the associated |
| 61 |
* indices. Hence densely packed indices should be preferred to avoid wasting space |
| 62 |
* for empty LocalFiniteElementVariant's string no implementation. |
| 63 |
* |
| 64 |
* \tparam Base Type of the base class providing getImplementations() and index(). |
| 65 |
*/ |
| 66 |
template<class Base> |
| 67 |
class LocalFiniteElementVariantCache : 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 |
{ |
| 76 |
using type = UniqueTypes_t<LocalFiniteElementVariant, decltype(std::declval<LFEImpl>()())...>; |
| 77 |
}; |
| 78 |
|
| 79 |
using Base::getImplementations; |
| 80 |
using Base::index; |
| 81 |
using Implementations = decltype(std::declval<Base>().getImplementations()); |
| 82 |
|
| 83 |
public: |
| 84 |
|
| 85 |
/** |
| 86 |
* \brief Type of exported LocalFiniteElement's |
| 87 |
* |
| 88 |
* This is a LocalFiniteElementVariant<Implementation...> |
| 89 |
* with Implementations... being the list of all provided |
| 90 |
* implementations. |
| 91 |
*/ |
| 92 |
using FiniteElementType = typename GenerateLFEVariant<Implementations>::type; |
| 93 |
|
| 94 |
/** \brief Default constructor |
| 95 |
* |
| 96 |
* Prefills the cache with all implementations. |
| 97 |
*/ |
| 98 |
template<class... Args> |
| 99 |
LocalFiniteElementVariantCache(Args&&... 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 |
|
| 110 |
/** \brief Copy constructor */ |
| 111 |
LocalFiniteElementVariantCache(const LocalFiniteElementVariantCache& other) = default; |
| 112 |
|
| 113 |
/** \brief Move constructor */ |
| 114 |
LocalFiniteElementVariantCache(LocalFiniteElementVariantCache&& other) = default; |
| 115 |
|
| 116 |
/** \brief Copy assignment */ |
| 117 |
LocalFiniteElementVariantCache& operator= (const LocalFiniteElementVariantCache& other) = default; |
| 118 |
|
| 119 |
/** \brief Move assignment */ |
| 120 |
LocalFiniteElementVariantCache& operator= (LocalFiniteElementVariantCache&& other) = default; |
| 121 |
|
| 122 |
/** \brief Get the LocalFiniteElement for the given key data |
| 123 |
* |
| 124 |
* \throws Dune::RangeError If the cache doesn't hold a value matching the requested type. |
| 125 |
*/ |
| 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 |
|
| 137 |
private: |
| 138 |
std::vector<FiniteElementType> cache_; |
| 139 |
}; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
} // namespace Dune |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
#endif // DUNE_LOCALFUNCTIONS_COMMON_LOCALFINITEELEMENTVARIANT_HH |