| 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_VIRTUALINTERFACE_HH |
| 6 |
#define DUNE_LOCALFUNCTIONS_COMMON_VIRTUALINTERFACE_HH |
| 7 |
|
| 8 |
#include <type_traits> |
| 9 |
#include <array> |
| 10 |
#include <vector> |
| 11 |
#include <functional> |
| 12 |
|
| 13 |
#include <dune/geometry/type.hh> |
| 14 |
|
| 15 |
#include <dune/localfunctions/common/localbasis.hh> |
| 16 |
#include <dune/localfunctions/common/localkey.hh> |
| 17 |
#include <dune/localfunctions/common/localfiniteelementtraits.hh> |
| 18 |
|
| 19 |
namespace Dune |
| 20 |
{ |
| 21 |
|
| 22 |
// forward declaration needed by the helper traits |
| 23 |
template<class DomainType, class RangeType> |
| 24 |
class LocalInterpolationVirtualInterface; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
// ----------------------------------------------------------------- |
| 29 |
// Basis |
| 30 |
// ----------------------------------------------------------------- |
| 31 |
|
| 32 |
/** |
| 33 |
* @brief virtual base class for a local basis |
| 34 |
* |
| 35 |
* Provides the local basis interface with pure virtual methods. |
| 36 |
* This class defines the interface using pure virtual methods. |
| 37 |
*/ |
| 38 |
template<class T> |
| 39 |
class LocalBasisVirtualInterface |
| 40 |
{ |
| 41 |
public: |
| 42 |
using Traits = T; |
| 43 |
|
| 44 |
|
| 45 |
virtual ~LocalBasisVirtualInterface() {} |
| 46 |
|
| 47 |
//! \brief Number of shape functions |
| 48 |
virtual unsigned int size () const = 0; |
| 49 |
|
| 50 |
//! \brief Polynomial order of the shape functions |
| 51 |
virtual unsigned int order () const = 0; |
| 52 |
|
| 53 |
/** \brief Evaluate all basis function at given position |
| 54 |
* |
| 55 |
* Evaluates all shape functions at the given position and returns |
| 56 |
* these values in a vector. |
| 57 |
*/ |
| 58 |
virtual void evaluateFunction (const typename Traits::DomainType& in, |
| 59 |
std::vector<typename Traits::RangeType>& out) const = 0; |
| 60 |
|
| 61 |
/** \brief Evaluate jacobian of all shape functions at given position. |
| 62 |
* |
| 63 |
* out[i][j][k] is \f$\partial_k \hat\phi_j^i \f$, when \f$\hat\phi^i \f$ is the |
| 64 |
* i'th shape function. |
| 65 |
* |
| 66 |
* \param [in] in The position where evaluated |
| 67 |
* \param [out] out The result |
| 68 |
*/ |
| 69 |
virtual void evaluateJacobian(const typename Traits::DomainType& in, // position |
| 70 |
std::vector<typename Traits::JacobianType>& out) const = 0; |
| 71 |
|
| 72 |
/** \brief Evaluate partial derivatives of any order of all shape functions |
| 73 |
* \param order Order of the partial derivatives, in the classic multi-index notation |
| 74 |
* \param in Position where to evaluate the derivatives |
| 75 |
* \param[out] out Return value: the desired partial derivatives |
| 76 |
*/ |
| 77 |
virtual void partial(const std::array<unsigned int,Traits::dimDomain>& order, |
| 78 |
const typename Traits::DomainType& in, |
| 79 |
std::vector<typename Traits::RangeType>& out) const = 0; |
| 80 |
}; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
// ----------------------------------------------------------------- |
| 85 |
// Interpolation |
| 86 |
// ----------------------------------------------------------------- |
| 87 |
|
| 88 |
/** |
| 89 |
* @brief virtual base class for a local interpolation |
| 90 |
* |
| 91 |
* This class defines the interface using pure virtual methods. |
| 92 |
* In applications you should use the derived class |
| 93 |
* LocalInterpolationVirtualInterface that also |
| 94 |
* contains a interpolate method where the function type |
| 95 |
* is a template parameter. |
| 96 |
* |
| 97 |
* This template method cannot be defined in the same |
| 98 |
* class as the virtual method. Otherwise name resolution fails. |
| 99 |
*/ |
| 100 |
template<class DomainType, class RangeType> |
| 101 |
class LocalInterpolationVirtualInterfaceBase |
| 102 |
{ |
| 103 |
public: |
| 104 |
|
| 105 |
//! type of function to interpolate |
| 106 |
using FunctionType = std::function<RangeType(DomainType)>; |
| 107 |
|
| 108 |
//! type of the coefficient vector in the interpolate method |
| 109 |
typedef typename RangeType::field_type CoefficientType; |
| 110 |
|
| 111 |
virtual ~LocalInterpolationVirtualInterfaceBase() {} |
| 112 |
|
| 113 |
/** \brief determine coefficients interpolating a given function |
| 114 |
* |
| 115 |
* This is the pure virtual method taking a VirtualFunction. |
| 116 |
* |
| 117 |
* \param[in] f Function instance used to interpolate. |
| 118 |
* \param[out] out Resulting coefficients vector. |
| 119 |
*/ |
| 120 |
virtual void interpolate (const FunctionType& f, std::vector<CoefficientType>& out) const = 0; |
| 121 |
}; |
| 122 |
|
| 123 |
/** |
| 124 |
* @brief virtual base class for a local interpolation |
| 125 |
* |
| 126 |
* This class defines the interface using pure virtual methods. |
| 127 |
* It also contains the interpolate method with function |
| 128 |
* type as template parameter. |
| 129 |
*/ |
| 130 |
template<class DomainType, class RangeType> |
| 131 |
class LocalInterpolationVirtualInterface |
| 132 |
: public LocalInterpolationVirtualInterfaceBase<DomainType, RangeType> |
| 133 |
{ |
| 134 |
public: |
| 135 |
|
| 136 |
//! type of function to interpolate |
| 137 |
using FunctionType = std::function<RangeType(DomainType)>; |
| 138 |
|
| 139 |
//! type of the coefficient vector in the interpolate method |
| 140 |
typedef typename RangeType::field_type CoefficientType; |
| 141 |
|
| 142 |
|
| 143 |
virtual ~LocalInterpolationVirtualInterface() {} |
| 144 |
|
| 145 |
// This method is only noted again for to make the documentation complete. |
| 146 |
|
| 147 |
/** \brief determine coefficients interpolating a given function |
| 148 |
* |
| 149 |
* This is the pure virtual method taking a VirtualFunction. |
| 150 |
* |
| 151 |
* \param[in] f Function instance used to interpolate. |
| 152 |
* \param[out] out Resulting coefficients vector. |
| 153 |
*/ |
| 154 |
virtual void interpolate (const FunctionType& f, std::vector<CoefficientType>& out) const = 0; |
| 155 |
|
| 156 |
/** \brief determine coefficients interpolating a given function |
| 157 |
* |
| 158 |
* \param[in] f Function instance used to interpolate. |
| 159 |
* \param[out] out Resulting coefficients vector. |
| 160 |
*/ |
| 161 |
template<class F, |
| 162 |
std::enable_if_t<not std::is_base_of<FunctionType, F>::value, int> = 0> |
| 163 |
void interpolate (const F& f, std::vector<CoefficientType>& out) const |
| 164 |
{ |
| 165 |
const LocalInterpolationVirtualInterfaceBase<DomainType, RangeType>& asBase = *this; |
| 166 |
asBase.interpolate(FunctionType(std::cref(f)),out); |
| 167 |
} |
| 168 |
|
| 169 |
/** \brief determine coefficients interpolating a given function |
| 170 |
* |
| 171 |
* \param[in] f Function instance used to interpolate. |
| 172 |
* \param[out] out Resulting coefficients vector. |
| 173 |
*/ |
| 174 |
template<class F, class C> |
| 175 |
void interpolate (const F& f, std::vector<C>& out) const |
| 176 |
{ |
| 177 |
std::vector<CoefficientType> outDummy; |
| 178 |
const LocalInterpolationVirtualInterfaceBase<DomainType, RangeType>& asBase = *this; |
| 179 |
asBase.interpolate(FunctionType(std::cref(f)),outDummy); |
| 180 |
out.resize(outDummy.size()); |
| 181 |
for(typename std::vector<CoefficientType>::size_type i=0; i<outDummy.size(); ++i) |
| 182 |
out[i] = outDummy[i]; |
| 183 |
} |
| 184 |
}; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
// ----------------------------------------------------------------- |
| 189 |
// Coefficients |
| 190 |
// ----------------------------------------------------------------- |
| 191 |
|
| 192 |
/** |
| 193 |
* @brief virtual base class for local coefficients |
| 194 |
* |
| 195 |
* This class defines the interface using pure virtual methods. |
| 196 |
*/ |
| 197 |
class LocalCoefficientsVirtualInterface |
| 198 |
{ |
| 199 |
public: |
| 200 |
|
| 201 |
virtual ~LocalCoefficientsVirtualInterface() {} |
| 202 |
|
| 203 |
//! number of coefficients |
| 204 |
virtual std::size_t size () const = 0; |
| 205 |
|
| 206 |
//! get i'th index |
| 207 |
const virtual LocalKey& localKey (std::size_t i) const = 0; |
| 208 |
|
| 209 |
}; |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
// ----------------------------------------------------------------- |
| 214 |
// Finite Element |
| 215 |
// ----------------------------------------------------------------- |
| 216 |
|
| 217 |
|
| 218 |
/** |
| 219 |
* @brief virtual base class for local finite elements with functions |
| 220 |
* |
| 221 |
* This class defines the same interface using pure virtual methods. |
| 222 |
*/ |
| 223 |
template<class T> |
| 224 |
class LocalFiniteElementVirtualInterface |
| 225 |
{ |
| 226 |
using LocalBasisTraits = T; |
| 227 |
public: |
| 228 |
typedef LocalFiniteElementTraits< |
| 229 |
LocalBasisVirtualInterface<LocalBasisTraits>, |
| 230 |
LocalCoefficientsVirtualInterface, |
| 231 |
LocalInterpolationVirtualInterface< |
| 232 |
typename LocalBasisTraits::DomainType, |
| 233 |
typename LocalBasisTraits::RangeType> > Traits; |
| 234 |
|
| 235 |
virtual ~LocalFiniteElementVirtualInterface() {} |
| 236 |
|
| 237 |
//! \copydoc LocalFiniteElementVirtualInterface::localBasis |
| 238 |
virtual const typename Traits::LocalBasisType& localBasis () const = 0; |
| 239 |
|
| 240 |
//! \copydoc LocalFiniteElementVirtualInterface::localCoefficients |
| 241 |
virtual const typename Traits::LocalCoefficientsType& localCoefficients () const = 0; |
| 242 |
|
| 243 |
//! \copydoc LocalFiniteElementVirtualInterface::localInterpolation |
| 244 |
virtual const typename Traits::LocalInterpolationType& localInterpolation () const = 0; |
| 245 |
|
| 246 |
//! \copydoc LocalFiniteElementVirtualInterface::size |
| 247 |
virtual unsigned int size () const = 0; |
| 248 |
|
| 249 |
//! \copydoc LocalFiniteElementVirtualInterface::type |
| 250 |
virtual const GeometryType type () const = 0; |
| 251 |
|
| 252 |
virtual LocalFiniteElementVirtualInterface<T>* clone() const = 0; |
| 253 |
}; |
| 254 |
} |
| 255 |
#endif |