| 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_LOCALFINITEELEMENTVARIANT_HH |
| 6 |
#define DUNE_LOCALFUNCTIONS_COMMON_LOCALFINITEELEMENTVARIANT_HH |
| 7 |
|
| 8 |
#include <cstddef> |
| 9 |
#include <type_traits> |
| 10 |
#include <variant> |
| 11 |
|
| 12 |
#include <dune/common/typeutilities.hh> |
| 13 |
#include <dune/common/std/type_traits.hh> |
| 14 |
#include <dune/common/overloadset.hh> |
| 15 |
|
| 16 |
#include <dune/geometry/type.hh> |
| 17 |
|
| 18 |
#include <dune/localfunctions/common/localfiniteelementtraits.hh> |
| 19 |
#include <dune/localfunctions/common/localbasis.hh> |
| 20 |
#include <dune/localfunctions/common/localkey.hh> |
| 21 |
|
| 22 |
|
| 23 |
namespace Dune { |
| 24 |
|
| 25 |
namespace Impl { |
| 26 |
|
| 27 |
// Helper for visiting a variant containing monostate. |
| 28 |
// Since a generic lambda will in most cases not compile |
| 29 |
// for monostate, we add special empty overloads for monostate. |
| 30 |
// Hence visitIf will simply do nothing in the case of a |
| 31 |
// monostate value. |
| 32 |
template<class Visitor, class Variant> |
| 33 |
void visitIf(Visitor&& visitor, Variant&& variant) |
| 34 |
{ |
| 35 |
auto visitorWithFallback = overload([&](std::monostate&) {}, [&](const std::monostate&) {}, visitor); |
| 36 |
std::visit(visitorWithFallback, variant); |
| 37 |
} |
| 38 |
|
| 39 |
template<class... Implementations> |
| 40 |
class LocalBasisVariant |
| 41 |
{ |
| 42 |
|
| 43 |
template<class I0, class... II> |
| 44 |
struct FirstType |
| 45 |
{ using type = I0; }; |
| 46 |
|
| 47 |
using FirstImpTraits = typename FirstType<Implementations...>::type::Traits; |
| 48 |
|
| 49 |
public: |
| 50 |
|
| 51 |
// We do not simply copy Implementation::LocalBasisTraits because this |
| 52 |
// may be implementation specific. To stay clean, we simply put all its |
| 53 |
// data into the default LocalBasisTraits. |
| 54 |
using Traits = typename Dune::LocalBasisTraits< |
| 55 |
typename FirstImpTraits::DomainFieldType, |
| 56 |
FirstImpTraits::dimDomain, |
| 57 |
typename FirstImpTraits::DomainType, |
| 58 |
typename FirstImpTraits::RangeFieldType, |
| 59 |
FirstImpTraits::dimRange, |
| 60 |
typename FirstImpTraits::RangeType, |
| 61 |
typename FirstImpTraits::JacobianType>; |
| 62 |
|
| 63 |
template<class Implementation> |
| 64 |
LocalBasisVariant(const Implementation& impl) : |
| 65 |
impl_(&impl), |
| 66 |
size_(impl.size()), |
| 67 |
order_(impl.order()) |
| 68 |
{} |
| 69 |
|
| 70 |
LocalBasisVariant() = default; |
| 71 |
LocalBasisVariant(const LocalBasisVariant& other) = default; |
| 72 |
LocalBasisVariant(LocalBasisVariant&& other) = default; |
| 73 |
LocalBasisVariant& operator=(const LocalBasisVariant& other) = default; |
| 74 |
LocalBasisVariant& operator=(LocalBasisVariant&& other) = default; |
| 75 |
|
| 76 |
/** |
| 77 |
* \brief Number of shape functions |
| 78 |
*/ |
| 79 |
unsigned int size() const |
| 80 |
{ |
| 81 |
return size_; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* \brief Polynomial order of the shape functions |
| 86 |
*/ |
| 87 |
unsigned int order() const |
| 88 |
{ |
| 89 |
return order_; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* \brief Evaluate all shape functions |
| 94 |
*/ |
| 95 |
inline void evaluateFunction( |
| 96 |
const typename Traits::DomainType& x, |
| 97 |
std::vector<typename Traits::RangeType>& out) const |
| 98 |
{ |
| 99 |
Impl::visitIf([&](const auto* impl) { impl->evaluateFunction(x, out); }, impl_); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* \brief Evaluate Jacobian of all shape functions |
| 104 |
*/ |
| 105 |
inline void evaluateJacobian( |
| 106 |
const typename Traits::DomainType& x, |
| 107 |
std::vector<typename Traits::JacobianType>& out) const |
| 108 |
{ |
| 109 |
Impl::visitIf([&](const auto* impl) { impl->evaluateJacobian(x, out); }, impl_); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* \brief Evaluate partial derivatives of any order of all shape functions |
| 114 |
* |
| 115 |
* \param order Order of the partial derivatives, in the classic multi-index notation |
| 116 |
* \param x Position where to evaluate the derivatives |
| 117 |
* \param[out] out Return value: the desired partial derivatives |
| 118 |
*/ |
| 119 |
void partial( |
| 120 |
const std::array<unsigned int,Traits::dimDomain>& order, |
| 121 |
const typename Traits::DomainType& x, |
| 122 |
std::vector<typename Traits::RangeType>& out) const |
| 123 |
{ |
| 124 |
Impl::visitIf([&](const auto* impl) { impl->partial(order, x, out); }, impl_); |
| 125 |
} |
| 126 |
|
| 127 |
private: |
| 128 |
std::variant<std::monostate, const Implementations*...> impl_; |
| 129 |
std::size_t size_; |
| 130 |
std::size_t order_; |
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
template<class... Implementations> |
| 135 |
class LocalCoefficientsVariant |
| 136 |
{ |
| 137 |
public: |
| 138 |
|
| 139 |
template<class Implementation> |
| 140 |
LocalCoefficientsVariant(const Implementation& impl) : |
| 141 |
impl_(&impl), |
| 142 |
size_(impl.size()) |
| 143 |
{} |
| 144 |
|
| 145 |
LocalCoefficientsVariant() = default; |
| 146 |
LocalCoefficientsVariant(const LocalCoefficientsVariant& other) = default; |
| 147 |
LocalCoefficientsVariant(LocalCoefficientsVariant&& other) = default; |
| 148 |
LocalCoefficientsVariant& operator=(const LocalCoefficientsVariant& other) = default; |
| 149 |
LocalCoefficientsVariant& operator=(LocalCoefficientsVariant&& other) = default; |
| 150 |
|
| 151 |
/** |
| 152 |
* \brief Number of shape functions |
| 153 |
*/ |
| 154 |
unsigned int size() const |
| 155 |
{ |
| 156 |
return size_; |
| 157 |
} |
| 158 |
|
| 159 |
const Dune::LocalKey& localKey (std::size_t i) const |
| 160 |
{ |
| 161 |
// We can't use visitIf since we have to return something |
| 162 |
// even for a monostate value. Since the return type is |
| 163 |
// an l-value reference, we use a default constructed |
| 164 |
// dummy LocalKey value. |
| 165 |
static const Dune::LocalKey dummyLocalKey; |
| 166 |
return std::visit(overload( |
| 167 |
[&](const std::monostate&) -> decltype(auto) { return (dummyLocalKey);}, |
| 168 |
[&](const auto* impl) -> decltype(auto) { return impl->localKey(i); }), impl_); |
| 169 |
} |
| 170 |
|
| 171 |
private: |
| 172 |
std::variant<std::monostate, const Implementations*...> impl_; |
| 173 |
std::size_t size_; |
| 174 |
}; |
| 175 |
|
| 176 |
|
| 177 |
template<class... Implementations> |
| 178 |
class LocalInterpolationVariant |
| 179 |
{ |
| 180 |
public: |
| 181 |
|
| 182 |
template<class Implementation> |
| 183 |
LocalInterpolationVariant(const Implementation& impl) : |
| 184 |
impl_(&impl) |
| 185 |
{} |
| 186 |
|
| 187 |
LocalInterpolationVariant() = default; |
| 188 |
LocalInterpolationVariant(const LocalInterpolationVariant& other) = default; |
| 189 |
LocalInterpolationVariant(LocalInterpolationVariant&& other) = default; |
| 190 |
LocalInterpolationVariant& operator=(const LocalInterpolationVariant& other) = default; |
| 191 |
LocalInterpolationVariant& operator=(LocalInterpolationVariant&& other) = default; |
| 192 |
|
| 193 |
template<typename F, typename C> |
| 194 |
void interpolate (const F& ff, std::vector<C>& out) const |
| 195 |
{ |
| 196 |
Impl::visitIf([&](const auto* impl) { impl->interpolate(ff, out); }, impl_); |
| 197 |
} |
| 198 |
|
| 199 |
private: |
| 200 |
std::variant<std::monostate, const Implementations*...> impl_; |
| 201 |
}; |
| 202 |
|
| 203 |
} // namespace Impl |
| 204 |
|
| 205 |
|
| 206 |
/** |
| 207 |
* \brief Type erasure class for wrapping LocalFiniteElement classes |
| 208 |
* |
| 209 |
* This is a type erasure wrapper class for types implementing the |
| 210 |
* LocalFiniteElement interface. The types of the LocalFiniteElement |
| 211 |
* implementations that this class can hold have to be provided as |
| 212 |
* template parameter. |
| 213 |
* |
| 214 |
* The implementation is based on std::variant. |
| 215 |
* Notice that this prepends std::monostate to the Implementations |
| 216 |
* list for the internally stored std::variant such that |
| 217 |
* LocalFiniteElementVariant can be empty and is default-constructible. |
| 218 |
* As a consequence providing std::monostate manually to |
| 219 |
* LocalFiniteElementVariant is neither necessary nor allowed. |
| 220 |
* Access to the stored implementation is internally implemented |
| 221 |
* using std::visit(). To avoid multiple trivial std::visit() |
| 222 |
* calls, the results of size(), order(), and type() are cached |
| 223 |
* on creation and assignment. |
| 224 |
* |
| 225 |
* In empty state accessing any method beyond operator bool(), variant(), |
| 226 |
* or assignment leads to undefined behavior. |
| 227 |
* |
| 228 |
* The LocalBasisTraits are extracted from the implementation |
| 229 |
* provided as first template parameter. The other implementations are |
| 230 |
* required to be compatible with this one. |
| 231 |
* |
| 232 |
* \tparam Implementations List of supported LocalFiniteElement implementations |
| 233 |
*/ |
| 234 |
template<class... Implementations> |
| 235 |
class LocalFiniteElementVariant |
| 236 |
{ |
| 237 |
|
| 238 |
// In each LocalFooVariant we store a std::variant<std::monostate, const FooImpl*...>, i.e. a std::variant |
| 239 |
// with the pointer to the Foo implementation unless LocalFiniteElementVariant stores a monostate. In this |
| 240 |
// case each LocalFooVariant also stores a monostate (and not a monostate*). |
| 241 |
using LocalBasis = Impl::LocalBasisVariant<typename Implementations::Traits::LocalBasisType...>; |
| 242 |
using LocalCoefficients = Impl::LocalCoefficientsVariant<typename Implementations::Traits::LocalCoefficientsType...>; |
| 243 |
using LocalInterpolation = Impl::LocalInterpolationVariant<typename Implementations::Traits::LocalInterpolationType...>; |
| 244 |
|
| 245 |
// Update members after changing impl_ |
| 246 |
void updateMembers() |
| 247 |
{ |
| 248 |
std::visit(overload( |
| 249 |
[&](std::monostate&) { |
| 250 |
localBasis_ = LocalBasis(); |
| 251 |
localCoefficients_ = LocalCoefficients(); |
| 252 |
localInterpolation_ = LocalInterpolation(); |
| 253 |
size_ = 0; |
| 254 |
geometryType_ = GeometryType{}; |
| 255 |
}, [&](auto&& impl) { |
| 256 |
localBasis_ = LocalBasis(impl.localBasis()); |
| 257 |
localCoefficients_ = LocalCoefficients(impl.localCoefficients()); |
| 258 |
localInterpolation_ = LocalInterpolation(impl.localInterpolation()); |
| 259 |
size_ = impl.size(); |
| 260 |
geometryType_ = impl.type(); |
| 261 |
}), impl_); |
| 262 |
} |
| 263 |
|
| 264 |
public: |
| 265 |
|
| 266 |
/** |
| 267 |
* \brief Export LocalFiniteElementTraits |
| 268 |
*/ |
| 269 |
using Traits = typename Dune::LocalFiniteElementTraits<LocalBasis, LocalCoefficients, LocalInterpolation>; |
| 270 |
|
| 271 |
/** |
| 272 |
* \brief Construct empty LocalFiniteElementVariant |
| 273 |
*/ |
| 274 |
LocalFiniteElementVariant() = default; |
| 275 |
|
| 276 |
/** |
| 277 |
* \brief Construct empty LocalFiniteElementVariant |
| 278 |
*/ |
| 279 |
LocalFiniteElementVariant(const std::monostate& monostate) |
| 280 |
{} |
| 281 |
|
| 282 |
/** |
| 283 |
* \brief Construct LocalFiniteElementVariant |
| 284 |
* |
| 285 |
* The created LocalFiniteElementVariant will store a |
| 286 |
* copy of the provided implementation. |
| 287 |
*/ |
| 288 |
template<class Implementation, |
| 289 |
std::enable_if_t<std::disjunction<std::is_same<std::decay_t<Implementation>, Implementations>...>::value, int> = 0> |
| 290 |
LocalFiniteElementVariant(Implementation&& impl) : |
| 291 |
impl_(std::forward<Implementation>(impl)) |
| 292 |
{ |
| 293 |
updateMembers(); |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* \brief Copy constructor |
| 298 |
*/ |
| 299 |
LocalFiniteElementVariant(const LocalFiniteElementVariant& other) : |
| 300 |
impl_(other.impl_) |
| 301 |
{ |
| 302 |
updateMembers(); |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* \brief Move constructor |
| 307 |
*/ |
| 308 |
LocalFiniteElementVariant(LocalFiniteElementVariant&& other) : |
| 309 |
impl_(std::move(other.impl_)) |
| 310 |
{ |
| 311 |
updateMembers(); |
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
* \brief Copy assignment |
| 316 |
*/ |
| 317 |
LocalFiniteElementVariant& operator=(const LocalFiniteElementVariant& other) |
| 318 |
{ |
| 319 |
impl_ = other.impl_; |
| 320 |
updateMembers(); |
| 321 |
return *this; |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* \brief Move assignment |
| 326 |
*/ |
| 327 |
LocalFiniteElementVariant& operator=(LocalFiniteElementVariant&& other) |
| 328 |
{ |
| 329 |
impl_ = std::move(other.impl_); |
| 330 |
updateMembers(); |
| 331 |
return *this; |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* \brief Assignment from implementation |
| 336 |
*/ |
| 337 |
template<class Implementation, |
| 338 |
std::enable_if_t<std::disjunction<std::is_same<std::decay_t<Implementation>, Implementations>...>::value, int> = 0> |
| 339 |
LocalFiniteElementVariant& operator=(Implementation&& impl) |
| 340 |
{ |
| 341 |
impl_ = std::forward<Implementation>(impl); |
| 342 |
updateMembers(); |
| 343 |
return *this; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
/** |
| 348 |
* \brief Provide access to LocalBasis implementation of this LocalFiniteElement |
| 349 |
*/ |
| 350 |
const typename Traits::LocalBasisType& localBasis() const |
| 351 |
{ |
| 352 |
return localBasis_; |
| 353 |
} |
| 354 |
|
| 355 |
/** |
| 356 |
* \brief Provide access to LocalCoefficients implementation of this LocalFiniteElement |
| 357 |
*/ |
| 358 |
const typename Traits::LocalCoefficientsType& localCoefficients() const |
| 359 |
{ |
| 360 |
return localCoefficients_; |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* \brief Provide access to LocalInterpolation implementation of this LocalFiniteElement |
| 365 |
*/ |
| 366 |
const typename Traits::LocalInterpolationType& localInterpolation() const |
| 367 |
{ |
| 368 |
return localInterpolation_; |
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
* \brief Number of shape functions |
| 373 |
*/ |
| 374 |
unsigned int size() const |
| 375 |
{ |
| 376 |
return size_; |
| 377 |
} |
| 378 |
|
| 379 |
/** |
| 380 |
* \brief Number of shape functions |
| 381 |
*/ |
| 382 |
constexpr GeometryType type() const |
| 383 |
{ |
| 384 |
return geometryType_; |
| 385 |
} |
| 386 |
|
| 387 |
/** |
| 388 |
* \brief Provide access to underlying std::variant |
| 389 |
* |
| 390 |
* This allows to use std::visit on a higher level |
| 391 |
* which allows to avoid the indirection of the |
| 392 |
* std::variant - polymorphism inside the visitor code. |
| 393 |
* Notice that the provided std::variant contains |
| 394 |
* std::monostate in its type list. Hence any |
| 395 |
* visitor used to access the variant has to be |
| 396 |
* std::monostate-aware. |
| 397 |
*/ |
| 398 |
const auto& variant() const |
| 399 |
{ |
| 400 |
return impl_; |
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* \brief Check if LocalFiniteElementVariant stores an implementation |
| 405 |
* |
| 406 |
* This returns true iff variant() does not store a monostate. |
| 407 |
*/ |
| 408 |
operator bool () const |
| 409 |
{ |
| 410 |
return not(std::holds_alternative<std::monostate>(variant())); |
| 411 |
} |
| 412 |
|
| 413 |
private: |
| 414 |
std::variant<std::monostate, Implementations...> impl_; |
| 415 |
std::size_t size_; |
| 416 |
GeometryType geometryType_; |
| 417 |
LocalBasis localBasis_; |
| 418 |
LocalCoefficients localCoefficients_; |
| 419 |
LocalInterpolation localInterpolation_; |
| 420 |
}; |
| 421 |
|
| 422 |
} // end namespace Dune |
| 423 |
|
| 424 |
#endif // DUNE_LOCALFUNCTIONS_COMMON_LOCALFINITEELEMENTVARIANT_HH |