5#ifndef DUNE_L2INTERPOLATION_HH
6#define DUNE_L2INTERPOLATION_HH
12#include <dune/localfunctions/common/localinterpolation.hh>
13#include <dune/localfunctions/utility/lfematrix.hh>
32 template<
class B,
class Q,
bool onb >
35 template<
class B,
class Q >
36 class LocalL2InterpolationBase
38 typedef LocalL2InterpolationBase< B, Q > This;
44 static const unsigned int dimension = Basis::dimension;
47 template<
class Function,
class DofField, std::enable_if_t<models<Impl::FunctionWithEvaluate<
typename Function::DomainType,
typename Function::RangeType>, Function>(),
int> = 0 >
48 void interpolate (
const Function &function, std::vector< DofField > &coefficients )
const
50 typedef typename Quadrature::iterator Iterator;
53 const unsigned int size = basis().size();
54 static std::vector< RangeVector > basisValues( size );
56 coefficients.resize( size );
57 basisValues.resize( size );
58 for(
unsigned int i = 0; i < size; ++i )
61 const Iterator end = quadrature().end();
62 for( Iterator it = quadrature().begin(); it != end; ++it )
64 basis().evaluate( it->position(), basisValues );
66 function.
evaluate( field_cast<typename Function::DomainType::field_type>(it->position()), val );
67 RangeVector factor = field_cast< DofField >( val );
68 factor *= field_cast< DofField >( it->weight() );
69 for(
unsigned int i = 0; i < size; ++i )
70 coefficients[ i ] += factor * basisValues[ i ];
75 template<
class Function,
class DofField, std::enable_if_t<models<Impl::FunctionWithCallOperator<
typename Quadrature::value_type::Vector>, Function>(),
int> = 0 >
76 void interpolate (
const Function &function, std::vector< DofField > &coefficients )
const
78 typedef FieldVector< DofField, Basis::dimRange > RangeVector;
80 const unsigned int size = basis().size();
81 static std::vector< RangeVector > basisValues( size );
83 coefficients.resize( size );
84 basisValues.resize( size );
85 for(
unsigned int i = 0; i < size; ++i )
86 coefficients[ i ] = Zero< DofField >();
88 for (
auto&& qp : quadrature())
90 basis().evaluate( qp.position(), basisValues );
91 auto val = function( qp.position() );
92 RangeVector factor = field_cast< DofField >( val );
93 factor *= field_cast< DofField >( qp.weight() );
94 for(
unsigned int i = 0; i < size; ++i )
95 coefficients[ i ] += factor * basisValues[ i ];
99 const Basis &basis ()
const
104 const Quadrature &quadrature ()
const
110 LocalL2InterpolationBase (
const Basis &basis,
const Quadrature &quadrature )
112 quadrature_( quadrature )
116 const Quadrature &quadrature_;
119 template<
class B,
class Q >
120 struct LocalL2Interpolation<B,Q,true>
121 :
public LocalL2InterpolationBase<B,Q>
123 typedef LocalL2InterpolationBase<B,Q> Base;
124 template<
class BasisFactory,
bool onb >
125 friend class LocalL2InterpolationFactory;
126 using typename Base::Basis;
127 using typename Base::Quadrature;
129 LocalL2Interpolation (
const typename Base::Basis &basis,
const typename Base::Quadrature &quadrature )
130 : Base(basis,quadrature)
133 template<
class B,
class Q >
134 struct LocalL2Interpolation<B,Q,false>
135 :
public LocalL2InterpolationBase<B,Q>
137 typedef LocalL2InterpolationBase<B,Q> Base;
138 template<
class BasisFactory,
bool onb >
139 friend class LocalL2InterpolationFactory;
140 using typename Base::Basis;
141 using typename Base::Quadrature;
142 template<
class Function,
class DofField >
143 void interpolate (
const Function &function, std::vector< DofField > &coefficients )
const
145 const unsigned size = Base::basis().size();
146 Base::interpolate(function,val_);
147 coefficients.resize( size );
148 for (
unsigned int i=0; i<size; ++i)
151 for (
unsigned int j=0; j<size; ++j)
153 coefficients[i] += field_cast<DofField>(massMatrix_(i,j)*val_[j]);
158 LocalL2Interpolation (
const typename Base::Basis &basis,
const typename Base::Quadrature &quadrature )
159 : Base(basis,quadrature),
163 typedef FieldVector< Field, Base::Basis::dimRange > RangeVector;
164 typedef typename Base::Quadrature::iterator Iterator;
165 const unsigned size = basis.size();
166 std::vector< RangeVector > basisValues( size );
168 massMatrix_.resize( size,size );
169 for (
unsigned int i=0; i<size; ++i)
170 for (
unsigned int j=0; j<size; ++j)
171 massMatrix_(i,j) = 0;
172 const Iterator end = Base::quadrature().end();
173 for( Iterator it = Base::quadrature().begin(); it != end; ++it )
175 Base::basis().evaluate( it->position(), basisValues );
176 for (
unsigned int i=0; i<size; ++i)
177 for (
unsigned int j=0; j<size; ++j)
178 massMatrix_(i,j) += (basisValues[i]*basisValues[j])*it->weight();
180 if ( !massMatrix_.invert() )
182 DUNE_THROW(MathError,
"Mass matrix singular in LocalL2Interpolation");
186 typedef typename Base::Basis::StorageField Field;
187 typedef FieldVector< Field, Base::Basis::dimRange > RangeVector;
188 typedef LFEMatrix<Field> MassMatrix;
189 mutable std::vector<Field> val_;
190 MassMatrix massMatrix_;
197 template<
class BasisFactory,
bool onb >
200 static const unsigned int dimension = BasisFactory::dimension;
201 typedef typename BasisFactory::Key Key;
202 typedef typename BasisFactory::Object Basis;
203 typedef double Field;
209 template< GeometryType::Id geometryId >
210 static Object *create (
const Key &key )
213 const Basis *basis = BasisFactory::template create< geometry >( key );
215 return new Object( *basis, quadrature );
217 static void release (
Object *
object )
219 const Basis &basis =
object->basis();
220 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:41
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:48
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:126
Abstract base class for quadrature rules.
Definition: quadraturerules.hh:154
A container for all quadrature rules of dimension dim
Definition: quadraturerules.hh:200
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:266
Infrastructure for concepts.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13
A factory class for the local l2 interpolations taking a basis factory.
Definition: l2interpolation.hh:199
A local L2 interpolation taking a test basis and a quadrature rule.
Definition: l2interpolation.hh:33
A class representing the zero of a given Field.
Definition: field.hh:79