3#ifndef DUNE_L2INTERPOLATION_HH
4#define DUNE_L2INTERPOLATION_HH
10#include <dune/localfunctions/common/localinterpolation.hh>
11#include <dune/localfunctions/utility/lfematrix.hh>
30 template<
class B,
class Q,
bool onb >
33 template<
class B,
class Q >
34 class LocalL2InterpolationBase
36 typedef LocalL2InterpolationBase< B, Q > This;
42 static const unsigned int dimension = Basis::dimension;
45 template<
class Function,
class DofField, std::enable_if_t<models<Impl::FunctionWithEvaluate<
typename Function::DomainType,
typename Function::RangeType>, Function>(),
int> = 0 >
46 void interpolate (
const Function &function, std::vector< DofField > &coefficients )
const
48 typedef typename Quadrature::iterator Iterator;
51 const unsigned int size = basis().size();
52 static std::vector< RangeVector > basisValues( size );
54 coefficients.resize( size );
55 basisValues.resize( size );
56 for(
unsigned int i = 0; i < size; ++i )
59 const Iterator end = quadrature().end();
60 for( Iterator it = quadrature().begin(); it != end; ++it )
62 basis().evaluate( it->position(), basisValues );
64 function.
evaluate( field_cast<typename Function::DomainType::field_type>(it->position()), val );
65 RangeVector factor = field_cast< DofField >( val );
66 factor *= field_cast< DofField >( it->weight() );
67 for(
unsigned int i = 0; i < size; ++i )
68 coefficients[ i ] += factor * basisValues[ i ];
73 template<
class Function,
class DofField, std::enable_if_t<models<Impl::FunctionWithCallOperator<
typename Quadrature::value_type::Vector>, Function>(),
int> = 0 >
74 void interpolate (
const Function &function, std::vector< DofField > &coefficients )
const
76 typedef FieldVector< DofField, Basis::dimRange > RangeVector;
78 const unsigned int size = basis().size();
79 static std::vector< RangeVector > basisValues( size );
81 coefficients.resize( size );
82 basisValues.resize( size );
83 for(
unsigned int i = 0; i < size; ++i )
84 coefficients[ i ] = Zero< DofField >();
86 for (
auto&& qp : quadrature())
88 basis().evaluate( qp.position(), basisValues );
89 auto val = function( qp.position() );
90 RangeVector factor = field_cast< DofField >( val );
91 factor *= field_cast< DofField >( qp.weight() );
92 for(
unsigned int i = 0; i < size; ++i )
93 coefficients[ i ] += factor * basisValues[ i ];
97 const Basis &basis ()
const
102 const Quadrature &quadrature ()
const
108 LocalL2InterpolationBase (
const Basis &basis,
const Quadrature &quadrature )
110 quadrature_( quadrature )
114 const Quadrature &quadrature_;
117 template<
class B,
class Q >
118 struct LocalL2Interpolation<B,Q,true>
119 :
public LocalL2InterpolationBase<B,Q>
121 typedef LocalL2InterpolationBase<B,Q> Base;
122 template<
class BasisFactory,
bool onb >
123 friend class LocalL2InterpolationFactory;
124 using typename Base::Basis;
125 using typename Base::Quadrature;
127 LocalL2Interpolation (
const typename Base::Basis &basis,
const typename Base::Quadrature &quadrature )
128 : Base(basis,quadrature)
131 template<
class B,
class Q >
132 struct LocalL2Interpolation<B,Q,false>
133 :
public LocalL2InterpolationBase<B,Q>
135 typedef LocalL2InterpolationBase<B,Q> Base;
136 template<
class BasisFactory,
bool onb >
137 friend class LocalL2InterpolationFactory;
138 using typename Base::Basis;
139 using typename Base::Quadrature;
140 template<
class Function,
class DofField >
141 void interpolate (
const Function &function, std::vector< DofField > &coefficients )
const
143 const unsigned size = Base::basis().size();
144 Base::interpolate(function,val_);
145 coefficients.resize( size );
146 for (
unsigned int i=0; i<size; ++i)
149 for (
unsigned int j=0; j<size; ++j)
151 coefficients[i] += field_cast<DofField>(massMatrix_(i,j)*val_[j]);
156 LocalL2Interpolation (
const typename Base::Basis &basis,
const typename Base::Quadrature &quadrature )
157 : Base(basis,quadrature),
161 typedef FieldVector< Field, Base::Basis::dimRange > RangeVector;
162 typedef typename Base::Quadrature::iterator Iterator;
163 const unsigned size = basis.size();
164 std::vector< RangeVector > basisValues( size );
166 massMatrix_.resize( size,size );
167 for (
unsigned int i=0; i<size; ++i)
168 for (
unsigned int j=0; j<size; ++j)
169 massMatrix_(i,j) = 0;
170 const Iterator end = Base::quadrature().end();
171 for( Iterator it = Base::quadrature().begin(); it != end; ++it )
173 Base::basis().evaluate( it->position(), basisValues );
174 for (
unsigned int i=0; i<size; ++i)
175 for (
unsigned int j=0; j<size; ++j)
176 massMatrix_(i,j) += (basisValues[i]*basisValues[j])*it->weight();
178 if ( !massMatrix_.invert() )
180 DUNE_THROW(MathError,
"Mass matrix singular in LocalL2Interpolation");
184 typedef typename Base::Basis::StorageField Field;
185 typedef FieldVector< Field, Base::Basis::dimRange > RangeVector;
186 typedef LFEMatrix<Field> MassMatrix;
187 mutable std::vector<Field> val_;
188 MassMatrix massMatrix_;
195 template<
class BasisFactory,
bool onb >
198 static const unsigned int dimension = BasisFactory::dimension;
199 typedef typename BasisFactory::Key Key;
200 typedef typename BasisFactory::Object Basis;
201 typedef double Field;
207 template< GeometryType::Id geometryId >
208 static Object *create (
const Key &key )
211 const Basis *basis = BasisFactory::template create< geometry >( key );
213 return new Object( *basis, quadrature );
215 static void release (
Object *
object )
217 const Basis &basis =
object->basis();
218 BasisFactory::release( &basis );
vector space out of a tensor product of fields.
Definition: fvector.hh:95
Base class template for function classes.
Definition: function.hh:39
void evaluate(const typename Traits::DomainType &x, typename Traits::RangeType &y) const
Function evaluation.
RawRangeType RangeType
Raw type of input variable with removed reference and constness.
Definition: function.hh:46
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:123
Abstract base class for quadrature rules.
Definition: quadraturerules.hh:152
A container for all quadrature rules of dimension dim
Definition: quadraturerules.hh:198
static const QuadratureRule & rule(const GeometryType &t, int p, QuadratureType::Enum qt=QuadratureType::GaussLegendre)
select the appropriate QuadratureRule for GeometryType t and order p
Definition: quadraturerules.hh:280
Infrastructure for concepts.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
Dune namespace.
Definition: alignedallocator.hh:11
A factory class for the local l2 interpolations taking a basis factory.
Definition: l2interpolation.hh:197
A local L2 interpolation taking a test basis and a quadrature rule.
Definition: l2interpolation.hh:31
A class representing the zero of a given Field.
Definition: field.hh:77