| 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 |
|
| 6 |
#ifndef DUNE_LOCALFUNCTIONS_COMMON_LOCALTOGLOBALADAPTORS_HH |
| 7 |
#define DUNE_LOCALFUNCTIONS_COMMON_LOCALTOGLOBALADAPTORS_HH |
| 8 |
|
| 9 |
#include <cstddef> |
| 10 |
#include <vector> |
| 11 |
|
| 12 |
#include <dune/common/fmatrix.hh> |
| 13 |
#include <dune/common/fvector.hh> |
| 14 |
#include <dune/common/typetraits.hh> |
| 15 |
|
| 16 |
#include <dune/geometry/type.hh> |
| 17 |
|
| 18 |
namespace Dune { |
| 19 |
|
| 20 |
//! Traits class for local-to-global basis adaptors |
| 21 |
/** |
| 22 |
* \tparam LocalBasisTraits Traits class of the LocalBasis to be adapted. |
| 23 |
* \tparam dimDomainGlobal_ Dimension of the global coordinates, |
| 24 |
* i.e. Geometry::coorddimension, if the global |
| 25 |
* coordinates are determined by a Geometry. |
| 26 |
* |
| 27 |
* \implements BasisInterface::Traits |
| 28 |
*/ |
| 29 |
template<class LocalBasisTraits, std::size_t dimDomainGlobal_> |
| 30 |
struct LocalToGlobalBasisAdaptorTraits { |
| 31 |
typedef typename LocalBasisTraits::DomainFieldType DomainField; |
| 32 |
static const std::size_t dimDomainLocal = LocalBasisTraits::dimDomain; |
| 33 |
static const std::size_t dimDomainGlobal = dimDomainGlobal_; |
| 34 |
typedef typename LocalBasisTraits::DomainType DomainLocal; |
| 35 |
typedef FieldVector<DomainField, dimDomainGlobal> DomainGlobal; |
| 36 |
|
| 37 |
typedef typename LocalBasisTraits::RangeFieldType RangeField; |
| 38 |
static const std::size_t dimRange = LocalBasisTraits::dimRange; |
| 39 |
typedef typename LocalBasisTraits::RangeType Range; |
| 40 |
|
| 41 |
typedef FieldMatrix<RangeField, dimRange, dimDomainGlobal> Jacobian; |
| 42 |
}; |
| 43 |
|
| 44 |
//! Convert a simple scalar local basis into a global basis |
| 45 |
/** |
| 46 |
* The local basis must be scalar, i.e. LocalBasis::Traits::dimRange must be |
| 47 |
* 1. It's values are not transformed. |
| 48 |
* |
| 49 |
* For scalar function \f$f\f$, the gradient is equivalent to the transposed |
| 50 |
* Jacobian \f$\nabla f|_x = J_f^T(x)\f$. The Jacobian is thus transformed |
| 51 |
* using |
| 52 |
* \f[ |
| 53 |
* \nabla f|_{\mu(\hat x)} = |
| 54 |
* \hat J_\mu^{-T}(\hat x) \cdot \hat\nabla\hat f|_{\hat x} |
| 55 |
* \f] |
| 56 |
* Here the hat \f$\hat{\phantom x}\f$ denotes local quantities and |
| 57 |
* \f$\mu\f$ denotes the local-to-global map of the geometry. |
| 58 |
* |
| 59 |
* \tparam LocalBasis Type of the local basis to adapt. |
| 60 |
* \tparam Geometry Type of the local-to-global transformation. |
| 61 |
* |
| 62 |
* \implements BasisInterface |
| 63 |
*/ |
| 64 |
template<class LocalBasis, class Geometry> |
| 65 |
class ScalarLocalToGlobalBasisAdaptor { |
| 66 |
static_assert(LocalBasis::Traits::dimRange == 1, |
| 67 |
"ScalarLocalToGlobalBasisAdaptor can only wrap a " |
| 68 |
"scalar local basis."); |
| 69 |
static_assert((std::is_same<typename LocalBasis::Traits::DomainFieldType, |
| 70 |
typename Geometry::ctype>::value), |
| 71 |
"ScalarLocalToGlobalBasisAdaptor: LocalBasis must use " |
| 72 |
"the same ctype as Geometry"); |
| 73 |
static_assert |
| 74 |
( static_cast<std::size_t>(LocalBasis::Traits::dimDomain) == |
| 75 |
static_cast<std::size_t>(Geometry::mydimension), |
| 76 |
"ScalarLocalToGlobalBasisAdaptor: LocalBasis domain dimension must " |
| 77 |
"match local dimension of Geometry"); |
| 78 |
|
| 79 |
const LocalBasis& localBasis; |
| 80 |
Geometry geometry; |
| 81 |
|
| 82 |
public: |
| 83 |
typedef LocalToGlobalBasisAdaptorTraits<typename LocalBasis::Traits, |
| 84 |
Geometry::coorddimension> Traits; |
| 85 |
|
| 86 |
//! construct a ScalarLocalToGlobalBasisAdaptor |
| 87 |
/** |
| 88 |
* \param localBasis_ The local basis object to adapt. |
| 89 |
* \param geometry_ The geometry object to use for adaption. |
| 90 |
* |
| 91 |
* \note This class stores the references passed here. Any use of this |
| 92 |
* class after these references have become invalid results in |
| 93 |
* undefined behaviour. The exception is that the destructor of |
| 94 |
* this class may still be called. |
| 95 |
*/ |
| 96 |
ScalarLocalToGlobalBasisAdaptor(const LocalBasis& localBasis_, |
| 97 |
const Geometry& geometry_) : |
| 98 |
localBasis(localBasis_), geometry(geometry_) |
| 99 |
{ } |
| 100 |
|
| 101 |
std::size_t size() const { return localBasis.size(); } |
| 102 |
//! return maximum polynomial order of the base function |
| 103 |
/** |
| 104 |
* This is to determine the required quadrature order. For an affine |
| 105 |
* geometry this is the same order as for the local basis. For other |
| 106 |
* geometries this returns the order of the local basis plus the global |
| 107 |
* dimension minus 1. The assumption for non-affine geometries is that |
| 108 |
* they are still multi-linear. |
| 109 |
*/ |
| 110 |
std::size_t order() const { |
| 111 |
if(geometry.affine()) |
| 112 |
// affine linear |
| 113 |
return localBasis.order(); |
| 114 |
else |
| 115 |
// assume at most order dim |
| 116 |
return localBasis.order() + Traits::dimDomainGlobal - 1; |
| 117 |
} |
| 118 |
|
| 119 |
void evaluateFunction(const typename Traits::DomainLocal& in, |
| 120 |
std::vector<typename Traits::Range>& out) const |
| 121 |
{ |
| 122 |
localBasis.evaluateFunction(in, out); |
| 123 |
} |
| 124 |
|
| 125 |
void evaluateJacobian(const typename Traits::DomainLocal& in, |
| 126 |
std::vector<typename Traits::Jacobian>& out) const |
| 127 |
{ |
| 128 |
std::vector<typename LocalBasis::Traits::JacobianType> |
| 129 |
localJacobian(size()); |
| 130 |
localBasis.evaluateJacobian(in, localJacobian); |
| 131 |
|
| 132 |
const typename Geometry::JacobianInverseTransposed &geoJacobian = |
| 133 |
geometry.jacobianInverseTransposed(in); |
| 134 |
|
| 135 |
out.resize(size()); |
| 136 |
for(std::size_t i = 0; i < size(); ++i) |
| 137 |
geoJacobian.mv(localJacobian[i][0], out[i][0]); |
| 138 |
} |
| 139 |
}; |
| 140 |
|
| 141 |
//! Convert a local interpolation into a global interpolation |
| 142 |
/** |
| 143 |
* \tparam LocalInterpolation Type of the local interpolation to adapt. |
| 144 |
* \tparam Traits_ Traits of the corresposnding basis class. |
| 145 |
* |
| 146 |
* \implements InterpolationInterface |
| 147 |
*/ |
| 148 |
template<class LocalInterpolation, class Traits_> |
| 149 |
class LocalToGlobalInterpolationAdaptor { |
| 150 |
const LocalInterpolation& localInterpolation; |
| 151 |
|
| 152 |
public: |
| 153 |
typedef Traits_ Traits; |
| 154 |
|
| 155 |
//! construct a LocalToGlobalInterpolationAdaptor |
| 156 |
/** |
| 157 |
* \param localInterpolation_ The local interpolation object to adapt. |
| 158 |
* |
| 159 |
* \note This class stores the reference to the local interpolation object |
| 160 |
* passed here. Any use of this class after the reference have |
| 161 |
* become invalid results in undefined behaviour. The exception is |
| 162 |
* that the destructor of this class may still be called. |
| 163 |
*/ |
| 164 |
LocalToGlobalInterpolationAdaptor |
| 165 |
( const LocalInterpolation& localInterpolation_) : |
| 166 |
localInterpolation(localInterpolation_) |
| 167 |
{ } |
| 168 |
|
| 169 |
template<class Function, class Coeff> |
| 170 |
void interpolate(const Function& function, std::vector<Coeff>& out) const |
| 171 |
{ localInterpolation.interpolate(function, out); } |
| 172 |
}; |
| 173 |
|
| 174 |
//! \brief Convert a simple scalar local finite element into a global finite |
| 175 |
//! element |
| 176 |
/** |
| 177 |
* The local finite element must be scalar, |
| 178 |
* i.e. LocalBasis::Traits::dimRange must be 1. It's values are not |
| 179 |
* transformed, but the Jacobian is (see ScalarLocalToGlobalBasisAdaptor). |
| 180 |
* |
| 181 |
* \tparam LocalFiniteElement Type of the local finite element to adapt. |
| 182 |
* \tparam Geometry Type of the local-to-global transformation. |
| 183 |
* |
| 184 |
* \implements FiniteElementInterface |
| 185 |
*/ |
| 186 |
template<class LocalFiniteElement, class Geometry> |
| 187 |
struct ScalarLocalToGlobalFiniteElementAdaptor { |
| 188 |
/** |
| 189 |
* \implements FiniteElementInterface::Traits |
| 190 |
*/ |
| 191 |
struct Traits { |
| 192 |
typedef ScalarLocalToGlobalBasisAdaptor<typename LocalFiniteElement:: |
| 193 |
Traits::LocalBasisType, Geometry> Basis; |
| 194 |
typedef LocalToGlobalInterpolationAdaptor<typename LocalFiniteElement:: |
| 195 |
Traits::LocalInterpolationType, typename Basis::Traits> |
| 196 |
Interpolation; |
| 197 |
typedef typename LocalFiniteElement::Traits::LocalCoefficientsType |
| 198 |
Coefficients; |
| 199 |
}; |
| 200 |
|
| 201 |
private: |
| 202 |
const LocalFiniteElement &localFE; |
| 203 |
typename Traits::Basis basis_; |
| 204 |
typename Traits::Interpolation interpolation_; |
| 205 |
|
| 206 |
public: |
| 207 |
//! construct a ScalarLocalToGlobalFiniteElementAdaptor |
| 208 |
/** |
| 209 |
* \param localFE_ The local finite element object to adapt. |
| 210 |
* \param geometry The geometry object to use for adaption. |
| 211 |
* |
| 212 |
* \note This class stores the references passed here. Any use of this |
| 213 |
* class after these references have become invalid results in |
| 214 |
* undefined behaviour. The exception is that the destructor of |
| 215 |
* this class may still be called. |
| 216 |
*/ |
| 217 |
ScalarLocalToGlobalFiniteElementAdaptor |
| 218 |
( const LocalFiniteElement& localFE_, const Geometry &geometry) : |
| 219 |
localFE(localFE_), |
| 220 |
basis_(localFE.localBasis(), geometry), |
| 221 |
interpolation_(localFE.localInterpolation()) |
| 222 |
{ } |
| 223 |
|
| 224 |
const typename Traits::Basis& basis() const { return basis_; } |
| 225 |
const typename Traits::Interpolation& interpolation() const |
| 226 |
{ return interpolation_; } |
| 227 |
const typename Traits::Coefficients& coefficients() const |
| 228 |
{ return localFE.localCoefficients(); } |
| 229 |
GeometryType type() const { return localFE.type(); } |
| 230 |
}; |
| 231 |
|
| 232 |
//! Factory for ScalarLocalToGlobalFiniteElementAdaptor objects |
| 233 |
/** |
| 234 |
* Constructs ScalarLocalToGlobalFiniteElementAdaptor objects given a local |
| 235 |
* finite element object and a geometry. This class is restricted to one |
| 236 |
* base variant of the local finite element. |
| 237 |
* |
| 238 |
* \tparam LocalFiniteElement Type of the local finite element to adapt. |
| 239 |
* \tparam Geometry Type of the local-to-global transformation. |
| 240 |
* |
| 241 |
* \implements FiniteElementFactoryInterface |
| 242 |
*/ |
| 243 |
template<class LocalFiniteElement, class Geometry> |
| 244 |
class ScalarLocalToGlobalFiniteElementAdaptorFactory { |
| 245 |
const LocalFiniteElement& localFE; |
| 246 |
|
| 247 |
public: |
| 248 |
typedef ScalarLocalToGlobalFiniteElementAdaptor<LocalFiniteElement, |
| 249 |
Geometry> FiniteElement; |
| 250 |
|
| 251 |
//! construct a ScalarLocalToGlobalFiniteElementAdaptorFactory |
| 252 |
/** |
| 253 |
* \param localFE_ The local finite element object to adapt. |
| 254 |
* |
| 255 |
* \note This class stores the reference to the local finite element |
| 256 |
* object passed here. Any use of this class after this reference |
| 257 |
* has become invalid results in undefined behaviour. The exception |
| 258 |
* is that the destructor of this class may still be called. |
| 259 |
*/ |
| 260 |
ScalarLocalToGlobalFiniteElementAdaptorFactory |
| 261 |
(const LocalFiniteElement &localFE_) : localFE(localFE_) {} |
| 262 |
|
| 263 |
//! construct ScalarLocalToGlobalFiniteElementAdaptor |
| 264 |
/** |
| 265 |
* \param geometry The geometry object to use for adaption. |
| 266 |
* |
| 267 |
* \note The returned object stores the reference to the geometry passed |
| 268 |
* here as well as references to internal data of this factory. Any |
| 269 |
* use of the returned value after the geometry reference or the |
| 270 |
* factory object was become invalid results in undefined behaviour. |
| 271 |
* The exception is that the destructor of the returned value may |
| 272 |
* still be called. |
| 273 |
*/ |
| 274 |
const FiniteElement make(const Geometry& geometry) { |
| 275 |
return FiniteElement(localFE, geometry); |
| 276 |
} |
| 277 |
}; |
| 278 |
|
| 279 |
} // namespace Dune |
| 280 |
|
| 281 |
#endif // DUNE_LOCALFUNCTIONS_COMMON_LOCALTOGLOBALADAPTORS_HH |