1#ifndef DUNE_FEM_OPERATOR_LINEAR_BLOCKDIAGONAL_HH
2#define DUNE_FEM_OPERATOR_LINEAR_BLOCKDIAGONAL_HH
11#include <dune/fem/function/adaptivefunction.hh>
12#include <dune/fem/operator/common/localmatrix.hh>
13#include <dune/fem/operator/common/localmatrixwrapper.hh>
14#include <dune/fem/operator/common/operator.hh>
15#include <dune/fem/operator/matrix/columnobject.hh>
16#include <dune/fem/storage/objectstack.hh>
27 RangeFieldType, DiscreteFunctionSpace::localBlockSize, DiscreteFunctionSpace::localBlockSize > >
41 typedef typename DomainFunctionType::DiscreteFunctionSpaceType DomainSpaceType;
42 typedef typename RangeFunctionType::DiscreteFunctionSpaceType RangeSpaceType;
44 typedef typename DomainSpaceType::EntityType DomainEntityType;
45 typedef typename RangeSpaceType::EntityType RangeEntityType;
47 static const int localBlockSize = DomainSpaceType::localBlockSize;
49 typedef LocalBlock LocalBlockType;
52 typedef LocalBlockType* DofBlockPtrType;
53 typedef const LocalBlockType* ConstDofBlockPtrType;
54 typedef typename LocalBlockType::row_type DofType ;
55 typedef DomainSpaceType DiscreteFunctionSpaceType ;
57 template<
class Operation >
60 typedef typename DiscreteFunctionSpaceType
61 :: template CommDataHandle< ThisType, Operation > :: Type
67 class LocalMatrixTraits;
69 struct LocalMatrixFactory;
73 typedef LocalMatrixWrapper< LocalMatrixStackType > LocalMatrixType;
75 typedef ColumnObject< ThisType > LocalColumnObjectType;
79 const DomainSpaceType &domainSpace,
80 const RangeSpaceType &rangeSpace )
82 space_( domainSpace ),
83 localMatrixFactory_( *
this ),
84 localMatrixStack_( localMatrixFactory_ )
86 if( &domainSpace != &rangeSpace )
90 void operator() (
const DomainFunctionType &u, RangeFunctionType &w )
const
95 template <
class DomainSpace,
class RangeSpace >
102 template <
class DomainSpace,
class RangeSpace >
106 const auto uit = u.leakPointer();
107 auto wit = w.leakPointer();
108 for(
auto& entry : diagonal_ )
110 entry.mv( uit, wit );
114 assert( uit == u.leakPointer() + u.size() );
115 assert( wit == w.leakPointer() + w.
size() );
118 virtual void clear ()
120 for(
auto& entry : diagonal_ )
121 entry = RangeFieldType( 0 );
124 template<
class Functor >
125 void forEach (
const Functor &functor )
127 for(
auto& entry : diagonal_ )
133 for(
auto& entry : diagonal_ )
137 void rightmultiply(
const ThisType& other )
139 assert( other.diagonal_.size() == diagonal_.size() );
140 auto it = other.diagonal_.begin();
141 for(
auto& entry : diagonal_ )
143 entry.rightmultiply( *it );
148 void leftmultiply(
const ThisType& other )
150 assert( other.diagonal_.size() == diagonal_.size() );
151 auto it = other.diagonal_.begin();
152 for(
auto& entry : diagonal_ )
154 entry.leftmultiply( *it );
162 assert(
block < diagonal_.size() );
163 return &diagonal_[
block ];
169 assert(
block < diagonal_.size() );
170 return &diagonal_[
block ];
179 domainSpace().communicate( *
this );
185 template<
class Operation >
186 typename CommDataHandle< Operation > :: Type
189 return space().createDataHandle( *
this, operation );
192 template<
class Stencil >
193 void reserve (
const Stencil &stencil,
bool verbose =
false )
196 diagonal_.resize( domainSpace().blockMapper().
size() );
199 LocalColumnObjectType localColumn (
const DomainEntityType &domainEntity )
const
201 return LocalColumnObjectType( *
this, domainEntity );
204 LocalMatrixType localMatrix (
const DomainEntityType &domainEntity,
const RangeEntityType &rangeEntity )
const;
206 const DomainSpaceType &domainSpace ()
const
210 const RangeSpaceType &rangeSpace ()
const
216 const DomainSpaceType &
space ()
const
221 const std::string &name ()
const
228 const RangeSpaceType &space_;
229 std::vector< LocalBlockType > diagonal_;
230 LocalMatrixFactory localMatrixFactory_;
231 mutable LocalMatrixStackType localMatrixStack_;
239 template<
class DiscreteFunctionSpace,
class LocalBlock >
242 typedef BlockDiagonalLinearOperator< DiscreteFunctionSpace, LocalBlock > OperatorType;
245 typedef typename OperatorType::LocalMatrix LocalMatrixType;
247 typedef typename OperatorType::RangeFieldType RangeFieldType;
249 typedef typename OperatorType::DomainSpaceType DomainSpaceType;
250 typedef typename OperatorType::RangeSpaceType RangeSpaceType;
252 typedef RangeFieldType LittleBlockType;
260 template<
class DiscreteFunctionSpace,
class LocalBlock >
262 :
public LocalMatrixInterface< LocalMatrixTraits >
264 typedef LocalMatrix ThisType;
265 typedef LocalMatrixInterface< LocalMatrixTraits > BaseType;
268 typedef BlockDiagonalLinearOperator< DiscreteFunctionSpace, LocalBlock > OperatorType;
272 typedef typename BaseType::DomainBasisFunctionSetType DomainBasisFunctionSetType;
273 typedef typename BaseType::RangeBasisFunctionSetType RangeBasisFunctionSetType;
275 typedef typename BaseType::DomainEntityType DomainEntityType;
276 typedef typename BaseType::RangeEntityType RangeEntityType;
279 typedef DomainBasisFunctionSetType BasisFunctionSetType;
280 typedef typename OperatorType::LocalBlockType LocalBlockType;
282 struct SetLocalBlockFunctor
284 SetLocalBlockFunctor ( std::vector< LocalBlockType > &diagonal,
285 LocalBlockType *&localBlock )
286 : diagonal_( diagonal ),
287 localBlock_( localBlock )
290 void operator() (
int localDoF, std::size_t globalDoF )
292 assert( localDoF == 0 );
293 assert( globalDoF < diagonal_.size() );
294 localBlock_ = &diagonal_[ globalDoF ];
298 std::vector< LocalBlockType > &diagonal_;
299 LocalBlockType *&localBlock_;
303 explicit LocalMatrix ( OperatorType &op )
305 localBlock_( nullptr )
308 void init (
const DomainEntityType &domainEntity,
const RangeEntityType &rangeEntity )
310 basisFunctionSet_ = domainSpace().basisFunctionSet( domainEntity );
311 SetLocalBlockFunctor f( op_->diagonal_, localBlock_ );
312 domainSpace().blockMapper().mapEach( domainEntity, f );
313 if( &domainEntity != &rangeEntity )
315 static LocalBlockType dummyBlock( 0 );
317 LocalBlockType *otherBlock = 0;
318 SetLocalBlockFunctor f( op_->diagonal_, otherBlock );
319 rangeSpace().blockMapper().mapEach( rangeEntity, f );
322 if( otherBlock != localBlock_ )
323 localBlock_ = &dummyBlock ;
329 localBlock() = RangeFieldType( 0 );
331 void scale (
const RangeFieldType &a )
336 RangeFieldType
get (
int i,
int j )
const
338 return localBlock()[ i ][ j ];
340 void add (
int i,
int j,
const RangeFieldType &value )
342 localBlock()[ i ][ j ] += value;
344 void set (
int i,
int j,
const RangeFieldType &value )
346 localBlock()[ i ][ j ] = value;
349 void clearRow (
int i )
351 localBlock()[ i ] = RangeFieldType( 0 );
354 void clearCol (
int j )
356 for(
int i = 0; i < rows(); ++i )
357 localBlock()[ i ][ j ] = RangeFieldType( 0 );
360 template<
class DomainLocalFunction,
class RangeLocalFunction >
361 void multiplyAdd (
const DomainLocalFunction &x, RangeLocalFunction &y )
const
363 localBlock().umv( x, y );
373 return localBlock().N();
377 return localBlock().M();
380 const DomainSpaceType &domainSpace ()
const
382 return op_->domainSpace();
384 const RangeSpaceType &rangeSpace ()
const
385 {
return op_->rangeSpace();
388 const DomainBasisFunctionSetType &domainBasisFunctionSet ()
const
390 return basisFunctionSet_;
392 const RangeBasisFunctionSetType &rangeBasisFunctionSet ()
const
394 return basisFunctionSet_;
397 const DomainEntityType &domainEntity ()
const
399 return domainBasisFunctionSet().entity();
401 const RangeEntityType &rangeEntity ()
const
403 return rangeBasisFunctionSet().entity();
407 const LocalBlockType &localBlock ()
const
409 assert( localBlock_ );
412 LocalBlockType &localBlock ()
414 assert( localBlock_ );
419 BasisFunctionSetType basisFunctionSet_;
420 LocalBlockType *localBlock_;
428 template<
class DiscreteFunctionSpace,
class LocalBlock >
431 typedef BlockDiagonalLinearOperator< DiscreteFunctionSpace, LocalBlock > OperatorType;
432 typedef LocalMatrix ObjectType;
434 explicit LocalMatrixFactory ( OperatorType &op )
438 ObjectType *newObject ()
const
440 return new ObjectType( *op_ );
452 template<
class DiscreteFunctionSpace,
class LocalBlock >
453 inline typename BlockDiagonalLinearOperator< DiscreteFunctionSpace, LocalBlock >::LocalMatrixType
454 BlockDiagonalLinearOperator< DiscreteFunctionSpace, LocalBlock >
455 ::localMatrix (
const DomainEntityType &domainEntity,
const RangeEntityType &rangeEntity )
const
457 return LocalMatrixType( localMatrixStack_, domainEntity, rangeEntity );
Definition: adaptivefunction.hh:48
abstract matrix operator
Definition: operator.hh:133
BlockDiagonalLinearOperator.
Definition: blockdiagonal.hh:30
CommDataHandle< Operation >::Type dataHandle(const Operation &operation)
return reference to data handle object (needed to make this class work with CommunicationManager)
Definition: blockdiagonal.hh:187
ConstDofBlockPtrType block(const std::size_t block) const
return block matrix for given block number (== entity number)
Definition: blockdiagonal.hh:167
void communicate()
copy matrices to ghost cells to make this class work in parallel
Definition: blockdiagonal.hh:177
const DomainSpaceType & space() const
return reference to space (needed to make this class work with CommunicationManager)
Definition: blockdiagonal.hh:216
DofBlockPtrType block(const std::size_t block)
return block matrix for given block number (== entity number)
Definition: blockdiagonal.hh:160
SizeType size() const
Return the number of blocks in the block vector.
Definition: discretefunction.hh:755
default implementation for a general operator stencil
Definition: stencil.hh:35
A dense n x m matrix.
Definition: fmatrix.hh:117
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:281
Implements a matrix constructed from a given type representing a field and compile-time given number ...
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
constexpr void forEach(Range &&range, F &&f)
Range based for loop.
Definition: hybridutilities.hh:256
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
constexpr auto get(std::integer_sequence< T, II... >, std::integral_constant< std::size_t, pos >={})
Return the entry at position pos of the given sequence.
Definition: integersequence.hh:22
virtual void finalize()
finalization of operator
Definition: operator.hh:61
AdaptiveDiscreteFunction< DiscreteFunctionSpace > DomainFunctionType
type of discrete function in the operator's domain
Definition: operator.hh:36
RangeFunction::RangeFieldType RangeFieldType
field type of the operator's range
Definition: operator.hh:43
DomainFunction::RangeFieldType DomainFieldType
field type of the operator's domain
Definition: operator.hh:41
AdaptiveDiscreteFunction< DiscreteFunctionSpace > RangeFunctionType
type of discrete function in the operator's range
Definition: operator.hh:38