4#include <dune/common/dynvector.hh>
6#include <dune/grid/common/rangegenerators.hh>
8#include <dune/fem/common/bindguard.hh>
9#include <dune/fem/function/common/localcontribution.hh>
10#include <dune/fem/function/localfunction/const.hh>
11#include <dune/fem/operator/common/stencil.hh>
12#include <dune/fem/operator/common/operator.hh>
13#include <dune/fem/operator/common/localcontribution.hh>
14#include <dune/fem/operator/common/differentiableoperator.hh>
16#include <dune/fem/quadrature/cachingquadrature.hh>
19template<
class DiscreteFunction,
class LinearOperator >
21:
public LinearOperator
23 typedef MassOperator< DiscreteFunction, LinearOperator > ThisType;
24 typedef LinearOperator BaseType;
26 typedef DiscreteFunction DiscreteFunctionType;
27 typedef typename DiscreteFunctionType::DiscreteFunctionSpaceType DiscreteFunctionSpaceType;
29 typedef typename DiscreteFunctionSpaceType::DomainType DomainType;
30 typedef typename DiscreteFunctionSpaceType::RangeType RangeType;
32 typedef Dune::Fem::CachingQuadrature< typename DiscreteFunctionSpaceType::GridPartType, 0 > QuadratureType;
34 explicit MassOperator (
const DiscreteFunctionSpaceType &dfSpace )
35 : BaseType(
"mass operator", dfSpace , dfSpace ),
41 template<
class Function >
42 void assembleRHS(
const Function &u, DiscreteFunctionType &w )
const;
43 void assemble (LinearOperator &matrix)
const;
45 const DiscreteFunctionSpaceType &dfSpace_;
48template<
class DiscreteFunction,
class LinearOperator >
49class AffineMassOperator
50:
public Dune::Fem::DifferentiableOperator<LinearOperator>
53 typedef DiscreteFunction DiscreteFunctionType;
54 typedef typename DiscreteFunctionType::DiscreteFunctionSpaceType DiscreteFunctionSpaceType;
55 typedef LinearOperator JacobianOperatorType;
56 template<
class Function >
57 explicit AffineMassOperator (
const DiscreteFunctionSpaceType &dfSpace,
const Function &u )
58 : matrix_(dfSpace), rhs_(
"rhs", dfSpace)
60 matrix_.assembleRHS(u, rhs_);
62 const DiscreteFunctionType rhs()
const {
return rhs_; }
63 virtual void operator() (
const DiscreteFunctionType &u, DiscreteFunctionType &w )
const
68 virtual void jacobian (
const DiscreteFunctionType &u, JacobianOperatorType &jOp )
const
70 matrix_.assemble( jOp );
73 MassOperator<DiscreteFunction,LinearOperator> matrix_;
74 DiscreteFunction rhs_;
77template<
class DiscreteFunction,
class LinearOperator >
78template<
class Function >
79void MassOperator< DiscreteFunction, LinearOperator >
80 ::assembleRHS(
const Function &u, DiscreteFunctionType &w )
const
85 Dune::Fem::ConstLocalFunction< Function > uLocal( u );
86 Dune::Fem::AddLocalContribution< DiscreteFunctionType > wLocal( w );
89 for(
const auto &entity : elements( dfSpace_.gridPart(), Dune::Partitions::interiorBorder ) )
91 const auto geometry = entity.geometry();
93 auto guard = bindGuard( std::tie( uLocal, wLocal ), entity );
96 for(
const auto qp : QuadratureType( entity, uLocal.order()+wLocal.order() ) )
99 RangeType uValue = uLocal.evaluate( qp );
102 uValue *= qp.weight() * geometry.integrationElement( qp.position() );
105 wLocal.axpy( qp, uValue );
111template<
class DiscreteFunction,
class LinearOperator >
112void MassOperator< DiscreteFunction, LinearOperator >::assemble (LinearOperator &matrix)
const
114 matrix.reserve( Dune::Fem::DiagonalStencil<DiscreteFunctionSpaceType,DiscreteFunctionSpaceType>( dfSpace_, dfSpace_ ) );
117 Dune::Fem::AddLocalContribution< LinearOperator > localMatrix( matrix );
118 Dune::DynamicVector< typename DiscreteFunctionSpaceType::RangeType > values;
121 for(
const auto &entity : elements( dfSpace_.gridPart(), Dune::Partitions::interiorBorder ) )
123 const auto geometry = entity.geometry();
125 auto guard = bindGuard( localMatrix, entity, entity );
127 const auto &basis = localMatrix.domainBasisFunctionSet();
128 const unsigned int numBasisFunctions = basis.size();
129 values.resize( numBasisFunctions );
132 for(
const auto qp : QuadratureType( entity, localMatrix.order() ) )
135 basis.evaluateAll( qp, values );
138 const auto weight = qp.weight() * geometry.integrationElement( qp.position() );
139 std::for_each( values.begin(), values.end(), [ weight ] (
auto &a ) { a *= weight; } );
142 localMatrix.axpy( qp, values );