| 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_L2INTERPOLATION_HH |
| 6 |
#define DUNE_L2INTERPOLATION_HH |
| 7 |
|
| 8 |
#include <dune/common/concept.hh> |
| 9 |
#include <dune/common/dynmatrix.hh> |
| 10 |
|
| 11 |
#include <dune/geometry/quadraturerules.hh> |
| 12 |
|
| 13 |
#include <dune/localfunctions/utility/field.hh> |
| 14 |
|
| 15 |
namespace Dune |
| 16 |
{ |
| 17 |
/** |
| 18 |
* @brief A local L2 interpolation taking a test basis and a quadrature |
| 19 |
* rule. |
| 20 |
* |
| 21 |
* This class computes a local interpolation where the coefficients |
| 22 |
* are of the form: |
| 23 |
* c = M^{-1}b |
| 24 |
* - M is the mass matrix with respect to the given basis and |
| 25 |
* - b = int f phi (where phi are the basis functions). |
| 26 |
* Thus the resulting local function u=c.varphi is defined through |
| 27 |
* the l2 interpolation int u phi = in f phi for all phi in the |
| 28 |
* base function set. |
| 29 |
* The third template argument can be used to specify that the |
| 30 |
* mass matrix is the unit matrix (onb=true). |
| 31 |
**/ |
| 32 |
template< class B, class Q, bool onb > |
| 33 |
struct LocalL2Interpolation; |
| 34 |
|
| 35 |
template< class B, class Q > |
| 36 |
class LocalL2InterpolationBase |
| 37 |
{ |
| 38 |
typedef LocalL2InterpolationBase< B, Q > This; |
| 39 |
|
| 40 |
public: |
| 41 |
typedef B Basis; |
| 42 |
typedef Q Quadrature; |
| 43 |
|
| 44 |
static const unsigned int dimension = Basis::dimension; |
| 45 |
|
| 46 |
/** \brief Interpolate a function that implements Range operator()(Domain) */ |
| 47 |
template< class Function, class DofField> |
| 48 |
void interpolate ( const Function &function, std::vector< DofField > &coefficients ) const |
| 49 |
{ |
| 50 |
typedef FieldVector< DofField, Basis::dimRange > RangeVector; |
| 51 |
|
| 52 |
const unsigned int size = basis().size(); |
| 53 |
static std::vector< RangeVector > basisValues( size ); |
| 54 |
|
| 55 |
coefficients.resize( size ); |
| 56 |
basisValues.resize( size ); |
| 57 |
for( unsigned int i = 0; i < size; ++i ) |
| 58 |
coefficients[ i ] = Zero< DofField >(); |
| 59 |
|
| 60 |
for (auto&& qp : quadrature()) |
| 61 |
{ |
| 62 |
basis().evaluate( qp.position(), basisValues ); |
| 63 |
auto val = function( qp.position() ); |
| 64 |
RangeVector factor = field_cast< DofField >( val ); |
| 65 |
factor *= field_cast< DofField >( qp.weight() ); |
| 66 |
for( unsigned int i = 0; i < size; ++i ) |
| 67 |
coefficients[ i ] += factor * basisValues[ i ]; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
const Basis &basis () const |
| 72 |
{ |
| 73 |
return basis_; |
| 74 |
} |
| 75 |
|
| 76 |
const Quadrature &quadrature () const |
| 77 |
{ |
| 78 |
return quadrature_; |
| 79 |
} |
| 80 |
|
| 81 |
protected: |
| 82 |
LocalL2InterpolationBase ( const Basis &basis, const Quadrature &quadrature ) |
| 83 |
: basis_( basis ), |
| 84 |
quadrature_( quadrature ) |
| 85 |
{} |
| 86 |
|
| 87 |
const Basis &basis_; |
| 88 |
const Quadrature &quadrature_; |
| 89 |
}; |
| 90 |
|
| 91 |
template< class B, class Q > |
| 92 |
struct LocalL2Interpolation<B,Q,true> |
| 93 |
: public LocalL2InterpolationBase<B,Q> |
| 94 |
{ |
| 95 |
typedef LocalL2InterpolationBase<B,Q> Base; |
| 96 |
template< class BasisFactory, bool onb > |
| 97 |
friend class LocalL2InterpolationFactory; |
| 98 |
using typename Base::Basis; |
| 99 |
using typename Base::Quadrature; |
| 100 |
private: |
| 101 |
LocalL2Interpolation ( const typename Base::Basis &basis, const typename Base::Quadrature &quadrature ) |
| 102 |
: Base(basis,quadrature) |
| 103 |
{} |
| 104 |
}; |
| 105 |
template< class B, class Q > |
| 106 |
struct LocalL2Interpolation<B,Q,false> |
| 107 |
: public LocalL2InterpolationBase<B,Q> |
| 108 |
{ |
| 109 |
typedef LocalL2InterpolationBase<B,Q> Base; |
| 110 |
template< class BasisFactory, bool onb > |
| 111 |
friend class LocalL2InterpolationFactory; |
| 112 |
using typename Base::Basis; |
| 113 |
using typename Base::Quadrature; |
| 114 |
template< class Function, class DofField > |
| 115 |
void interpolate ( const Function &function, std::vector< DofField > &coefficients ) const |
| 116 |
{ |
| 117 |
const unsigned size = Base::basis().size(); |
| 118 |
Base::interpolate(function,val_); |
| 119 |
coefficients.resize( size ); |
| 120 |
for (unsigned int i=0; i<size; ++i) |
| 121 |
{ |
| 122 |
coefficients[i] = 0; |
| 123 |
for (unsigned int j=0; j<size; ++j) |
| 124 |
{ |
| 125 |
coefficients[i] += field_cast<DofField>(massMatrix_[i][j]*val_[j]); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
private: |
| 130 |
LocalL2Interpolation ( const typename Base::Basis &basis, const typename Base::Quadrature &quadrature ) |
| 131 |
: Base(basis,quadrature), |
| 132 |
val_(basis.size()), |
| 133 |
massMatrix_(basis.size(),basis.size(),Field(0)) |
| 134 |
{ |
| 135 |
typedef FieldVector< Field, Base::Basis::dimRange > RangeVector; |
| 136 |
typedef typename Base::Quadrature::iterator Iterator; |
| 137 |
const unsigned size = basis.size(); |
| 138 |
std::vector< RangeVector > basisValues( size ); |
| 139 |
|
| 140 |
const Iterator end = Base::quadrature().end(); |
| 141 |
for( Iterator it = Base::quadrature().begin(); it != end; ++it ) |
| 142 |
{ |
| 143 |
Base::basis().evaluate( it->position(), basisValues ); |
| 144 |
for (unsigned int i=0; i<size; ++i) |
| 145 |
for (unsigned int j=0; j<size; ++j) |
| 146 |
massMatrix_[i][j] += (basisValues[i]*basisValues[j])*it->weight(); |
| 147 |
} |
| 148 |
massMatrix_.invert(); |
| 149 |
} |
| 150 |
typedef typename Base::Basis::StorageField Field; |
| 151 |
typedef FieldVector< Field, Base::Basis::dimRange > RangeVector; |
| 152 |
typedef DynamicMatrix<Field> MassMatrix; |
| 153 |
mutable std::vector<Field> val_; |
| 154 |
MassMatrix massMatrix_; |
| 155 |
}; |
| 156 |
|
| 157 |
/** |
| 158 |
* @brief A factory class for the local l2 interpolations |
| 159 |
* taking a basis factory. |
| 160 |
**/ |
| 161 |
template< class BasisFactory, bool onb > |
| 162 |
struct LocalL2InterpolationFactory |
| 163 |
{ |
| 164 |
static const unsigned int dimension = BasisFactory::dimension; |
| 165 |
typedef typename BasisFactory::Key Key; |
| 166 |
typedef typename BasisFactory::Object Basis; |
| 167 |
typedef double Field; |
| 168 |
typedef QuadratureRule<Field,dimension> Quadrature; |
| 169 |
typedef QuadratureRules<Field,dimension> QuadratureProvider; |
| 170 |
typedef LocalL2Interpolation< Basis, Quadrature, onb > LocalInterpolation; |
| 171 |
typedef const LocalInterpolation Object; |
| 172 |
|
| 173 |
template< GeometryType::Id geometryId > |
| 174 |
static Object *create ( const Key &key ) |
| 175 |
{ |
| 176 |
constexpr Dune::GeometryType geometry = geometryId; |
| 177 |
const Basis *basis = BasisFactory::template create< geometry >( key ); |
| 178 |
const Quadrature & quadrature = QuadratureProvider::rule(geometry, 2*basis->order()+1); |
| 179 |
return new Object( *basis, quadrature ); |
| 180 |
} |
| 181 |
static void release ( Object *object ) |
| 182 |
{ |
| 183 |
const Basis &basis = object->basis(); |
| 184 |
BasisFactory::release( &basis ); |
| 185 |
delete object; |
| 186 |
} |
| 187 |
}; |
| 188 |
|
| 189 |
} |
| 190 |
|
| 191 |
#endif // #ifndef DUNE_L2INTERPOLATION_HH |