1 #ifndef DUNE_FEM_SCALARPRODURCTS_HH 2 #define DUNE_FEM_SCALARPRODURCTS_HH 13 #include <dune/common/exceptions.hh> 15 #include <dune/common/genericiterator.hh> 16 #include <dune/common/ftraits.hh> 17 #include <dune/grid/common/gridenums.hh> 18 #include <dune/grid/common/datahandleif.hh> 22 #include <dune/istl/scalarproducts.hh> 43 template<
class Space,
class Mapper >
91 : space_( key.
space() ),
92 gridPart_( space_.gridPart() ),
93 mapper_( key.mapper() ),
94 myRank_( gridPart_.comm().rank() ),
95 mySize_( gridPart_.comm().
size() ),
108 return slaves_[ index ];
114 return slaves_.
size();
120 typedef GenericIterator<const IndexMapType, const int> IteratorType;
122 return std::binary_search(IteratorType(slaves_, 0),
123 IteratorType(slaves_,
size()-1),
131 slaveSet_.insert( index );
146 slaves_.
set( slaveSet_ );
152 sequence_ = space_.sequence();
159 if( sequence_ != space_.sequence() )
174 if( !space_.continuous() )
191 template<
class Space,
class Mapper >
192 template<
class Map >
193 struct SlaveDofs< Space, Mapper >::InsertFunctor
195 explicit InsertFunctor ( Map &map ) : map_( map ) {}
197 template<
class Value >
198 void operator() (
const int,
const Value &value )
200 map_.insert( value );
212 template<
class Space,
class Mapper >
215 const PartitionIteratorType idxpitype = GridPartType :: indexSetPartitionType;
217 typedef typename GridPartType :: template Codim< 0 >
218 :: template Partition< idxpitype > :: IteratorType
221 const IteratorType endit =
gridPart_.template end< 0, idxpitype >();
222 for( IteratorType it =
gridPart_.template begin< 0, idxpitype >(); it != endit; ++it )
224 typedef typename GridPartType :: template Codim< 0 > :: EntityType
227 const EntityType &entity = *it;
228 if( entity.partitionType() != Dune::InteriorEntity )
230 mapper_.mapEachEntityDof( entity, InsertFunctor< ThisType >( *
this ) );
240 template<
class Space,
class Mapper >
254 ( handle, GridPartType::indexSetInterfaceType, ForwardCommunication );
257 catch(
const Exception &e )
259 std::cerr << e << std::endl;
260 std::cerr <<
"Exception thrown in: " << __FILE__ <<
" line:" << __LINE__ << std::endl;
272 template<
class Space,
class Mapper >
286 const MapperType &mapper )
293 : space_( other.space_ ),
294 mapper_( other.mapper_ )
300 return (space_ == other.
space_) && (mapper_ == other.
mapper_);
318 template<
class Space,
class Mapper >
320 :
public CommDataHandleIF< LinkBuilder, int >
326 enum { nCodim = SpaceType :: GridType :: dimension + 1 };
342 const SpaceType &
space,
343 const MapperType& mapper )
344 : myRank_( space.gridPart().comm().rank() ),
345 mySize_( space.gridPart().comm().
size() ),
354 return mapper_.contains( codim );
363 template<
class MessageBuffer,
class Entity >
364 inline void gather ( MessageBuffer &buffer,
365 const Entity &entity )
const 368 if( sendRank( entity ) ) buffer.write( myRank_ );
375 template<
class MessageBuffer,
class EntityType >
377 const EntityType &entity,
387 assert( (rank >= 0) && (rank < mySize_) );
390 if ( rank < myRank_ || ! sendRank( entity ) )
391 mapper_.mapEachEntityDof( entity, InsertFunctor< IndexMapType >( slaves_ ) );
396 template<
class Entity >
397 size_t size (
const Entity &entity )
const 399 return (sendRank( entity )) ? 1 : 0;
403 template <
class Entity>
406 const PartitionType ptype = entity.partitionType();
407 return (ptype == InteriorEntity) || (ptype == BorderEntity);
413 template<
class DiscreteFunctionSpace >
425 typedef typename DiscreteFunctionSpaceType :: BlockMapperType
MapperType;
427 enum { blockSize = DiscreteFunctionSpaceType :: localBlockSize };
446 slaveDofs_( getSlaveDofs( space_ ) )
461 SlaveDofsProviderType :: removeObject( *slaveDofs_ );
474 SlaveDofsKeyType key( space, space.blockMapper() );
475 return &(SlaveDofsProviderType :: getObject( key ));
480 template<
class DiscreteFunction >
484 :
public SlaveDofsProvider< typename DiscreteFunction :: DiscreteFunctionSpaceType >
490 typedef typename DiscreteFunctionType :: DiscreteFunctionSpaceType
502 typedef typename DiscreteFunctionSpaceType :: BlockMapperType
MapperType;
504 enum { blockSize = DiscreteFunctionSpaceType :: localBlockSize };
509 typedef typename DiscreteFunctionType :: ConstDofBlockPtrType
518 using BaseType :: space;
521 template <
class OtherDiscreteFunctionType >
524 const OtherDiscreteFunctionType &y )
const 526 typedef typename OtherDiscreteFunctionType :: ConstDofBlockPtrType
527 OtherConstDofBlockPtrType;
528 SlaveDofsType &slaveDofs = this->slaveDofs();
530 RangeFieldType scp = 0;
532 const int numSlaves = slaveDofs.
size();
533 for(
int slave = 0, i = 0 ; slave < numSlaves; ++slave )
535 const int nextSlave = slaveDofs[ slave ];
536 for(; i < nextSlave; ++i )
539 OtherConstDofBlockPtrType yPtr = y.block( i );
540 for(
unsigned int j = 0; j < blockSize; ++j )
541 scp += (*xPtr)[ j ] * (*yPtr)[ j ];
549 scp =
space().gridPart().comm().sum( scp );
554 template<
class DiscreteFunction >
567 typedef typename DiscreteFunctionType :: DiscreteFunctionSpaceType
571 typedef typename DiscreteFunctionSpaceType :: RangeFieldType
RangeFieldType;
573 typedef typename DiscreteFunctionType :: ConstDofIteratorType
574 ConstDofIteratorType;
587 template <
class OtherDiscreteFunctionType >
589 inline RangeFieldType scalarProductDofs (
const DiscreteFunctionType &x,
590 const OtherDiscreteFunctionType &y )
const 592 RangeFieldType scp = 0;
594 ConstDofIteratorType endit = x.dend ();
595 ConstDofIteratorType xit = x.dbegin ();
597 typedef typename OtherDiscreteFunctionType :: ConstDofIteratorType
598 OtherConstDofIteratorType;
599 OtherConstDofIteratorType yit = y.dbegin();
601 for( ; xit != endit; ++xit, ++yit )
602 scp += (*xit) * (*yit);
609 template<
class DiscreteFunctionSpaceImp >
613 < ISTLBlockVectorDiscreteFunction< DiscreteFunctionSpaceImp > >
614 :
public ScalarProduct
615 < typename ISTLBlockVectorDiscreteFunction< DiscreteFunctionSpaceImp >
623 typedef typename DiscreteFunctionType :: DofStorageType BlockVectorType;
625 typedef typename DiscreteFunctionType :: DiscreteFunctionSpaceType
629 typedef typename DiscreteFunctionSpaceType :: RangeFieldType
RangeFieldType;
632 typedef typename DiscreteFunctionSpaceType :: BlockMapperType
MapperType;
637 typedef typename SlaveDofsProviderType :: SlaveDofsType
SlaveDofsType;
641 typedef BlockVectorType domain_type;
642 typedef typename BlockVectorType :: block_type :: field_type field_type;
645 enum { category=SolverCategory::sequential };
648 const DiscreteFunctionSpaceType &
space_;
649 SlaveDofsProviderType slaveDofProvider_;
656 slaveDofProvider_( space )
663 SlaveDofsType& slaveDofs ()
665 return slaveDofProvider_.slaveDofs();
668 const SlaveDofsType& slaveDofs ()
const 670 return slaveDofProvider_.slaveDofs();
677 RangeFieldType scalarProductDofs(
const DiscreteFunctionType& x,
678 const DiscreteFunctionType& y)
const 680 std::cout <<
"ISTL prod" << std::endl;
681 return scalarProductDofs(x.blockVector(),y.blockVector());
688 virtual field_type dot (
const BlockVectorType& x,
689 const BlockVectorType& y)
691 return const_cast<ThisType&
> (*this).scalarProductDofs(x,y);
697 virtual typename Dune::FieldTraits< RangeFieldType >::real_type
698 norm (
const BlockVectorType& x)
700 return std::abs(
std::sqrt( const_cast<ThisType&> (*this).scalarProductDofs(x,x) ) );
704 void deleteNonInterior(BlockVectorType& x)
const 708 const bool deleteGhostEntries = (space_.gridPart().grid().overlapSize( 0 ) == 0) && !space_.continuous();
711 if( deleteGhostEntries )
714 SlaveDofsType &slaveDofs = slaveDofProvider_.slaveDofs();
717 const int slaveSize = slaveDofs.
size() - 1;
718 for(
int slave = 0; slave<slaveSize; ++slave)
720 x[ slaveDofs[slave] ] = 0;
731 RangeFieldType scalarProductDofs(
const BlockVectorType& x,
732 const BlockVectorType& y)
const 736 SlaveDofsType &slaveDofs = slaveDofProvider_.slaveDofs();
738 RangeFieldType scp = 0;
740 const int slaveSize = slaveDofs.
size();
741 for(
int slave = 0; slave<slaveSize; ++slave)
743 const int nextSlave = slaveDofs[slave];
744 for(; i<nextSlave; ++i)
751 scp = space_.gridPart().comm().sum( scp );
755 RangeFieldType scp = x * y;
756 scp = space_.gridPart().comm().sum( scp );
768 #endif // #ifndef DUNE_FEM_SCALARPRODURCTS_HH const int myRank_
Definition: scalarproducts.hh:78
Space SpaceType
Definition: scalarproducts.hh:276
const SpaceType & space_
Definition: scalarproducts.hh:337
static double sqrt(const Double &v)
Definition: double.hh:870
static SlaveDofsType * getSlaveDofs(const DiscreteFunctionSpaceType &space)
Definition: scalarproducts.hh:472
SlaveDofs< Space, Mapper > IndexMapType
Definition: scalarproducts.hh:334
IndexMapType & slaves_
Definition: scalarproducts.hh:335
bool sendRank(const Entity &entity) const
Definition: scalarproducts.hh:404
int DataType
Definition: scalarproducts.hh:329
void insert(const int index)
insert index
Definition: scalarproducts.hh:129
Fem::CommunicationIndexMap IndexMapType
Definition: scalarproducts.hh:71
void gather(MessageBuffer &buffer, const Entity &entity) const
read buffer and apply operation
Definition: scalarproducts.hh:364
Mapper MapperType
type of used mapper
Definition: scalarproducts.hh:68
const SpaceType & space() const
return reference to index set
Definition: scalarproducts.hh:304
bool isSlave(const int index) const
return true if index is contained, meaning is a slave dof
Definition: scalarproducts.hh:118
LinkBuilder(IndexMapType &slaves, const SpaceType &space, const MapperType &mapper)
Definition: scalarproducts.hh:341
Mapper MapperType
Definition: scalarproducts.hh:324
const SpaceType & space_
Definition: scalarproducts.hh:74
Singleton list for key/object pairs.
Definition: singletonlist.hh:49
const DiscreteFunctionSpaceType & space_
Definition: scalarproducts.hh:437
Mapper MapperType
Definition: scalarproducts.hh:277
Space SpaceType
Definition: scalarproducts.hh:323
~SlaveDofsProvider()
remove object comm
Definition: scalarproducts.hh:459
SingletonKey(const SpaceType &space, const MapperType &mapper)
constructor taking space
Definition: scalarproducts.hh:285
SlaveDofsType::SingletonKey SlaveDofsKeyType
Definition: scalarproducts.hh:431
size_t size(const Entity &entity) const
return local dof size to be communicated
Definition: scalarproducts.hh:397
size_t size() const
return size of map
Definition: commindexmap.hh:87
DiscreteFunctionSpaceType::BlockMapperType MapperType
type of used mapper
Definition: scalarproducts.hh:425
SlaveDofsType & slaveDofs() const
Definition: scalarproducts.hh:464
SlaveDofs< DiscreteFunctionSpaceType, MapperType > SlaveDofsType
Definition: scalarproducts.hh:430
int size() const
return number of slave dofs
Definition: scalarproducts.hh:112
const MapperType & mapper() const
return reference to index set
Definition: scalarproducts.hh:310
void buildDiscontinuousMaps()
Definition: scalarproducts.hh:213
void buildMaps()
Definition: scalarproducts.hh:172
Key for CommManager singleton list.
Definition: scalarproducts.hh:273
Double abs(const Double &a)
Definition: double.hh:860
SlaveDofs(const SingletonKey &key)
constructor taking space
Definition: scalarproducts.hh:90
void clear()
clear index map
Definition: commindexmap.hh:44
bool operator==(const Double &a, const Double &b)
Definition: double.hh:589
const MapperType & mapper_
Definition: scalarproducts.hh:338
Definition: scalarproducts.hh:483
DiscreteFunctionType::ConstDofBlockPtrType ConstDofBlockPtrType
Definition: scalarproducts.hh:510
void initialize()
initialize
Definition: scalarproducts.hh:135
SlaveDofsType *const slaveDofs_
Definition: scalarproducts.hh:440
DiscreteFunctionSpaceType::BlockMapperType MapperType
type of used mapper
Definition: scalarproducts.hh:502
Definition: coordinate.hh:4
void set(const std::set< GlobalKey > &idxSet)
insert sorted set of indices
Definition: commindexmap.hh:71
const GridPartType & gridPart_
Definition: scalarproducts.hh:75
Definition: scalarproducts.hh:319
std::set< int > slaveSet_
Definition: scalarproducts.hh:83
void scatter(MessageBuffer &buffer, const EntityType &entity, size_t n)
Definition: scalarproducts.hh:376
int operator[](const int index) const
return dof number of salve with index
Definition: scalarproducts.hh:106
SlaveDofs< DiscreteFunctionSpaceType, MapperType > SlaveDofsType
Definition: scalarproducts.hh:507
Definition: commindexmap.hh:17
const MapperType *const mapper_
Definition: scalarproducts.hh:281
int sequence_
know grid sequence number
Definition: scalarproducts.hh:86
const int mySize_
Definition: scalarproducts.hh:332
const SpaceType & space() const
return reference to discrete function space
Definition: scalarproducts.hh:168
void buildCommunicatedMaps()
Definition: scalarproducts.hh:241
DiscreteFunctionType::DiscreteFunctionSpaceType DiscreteFunctionSpaceType
type of the discrete function space
Definition: scalarproducts.hh:491
RangeFieldType scalarProductDofs(const DiscreteFunctionType &x, const OtherDiscreteFunctionType &y) const
evaluate scalar product and omit slave nodes
Definition: scalarproducts.hh:523
Definition: scalarproducts.hh:414
Definition: blockvectorfunction/blockvectorfunction.hh:44
SlaveDofsProvider(const DiscreteFunctionSpaceType &space)
constructor taking space
Definition: scalarproducts.hh:444
SingletonList< SlaveDofsKeyType, SlaveDofsType > SlaveDofsProviderType
Definition: scalarproducts.hh:434
DiscreteFunctionSpaceType::RangeFieldType RangeFieldType
type of range field
Definition: scalarproducts.hh:499
SpaceType DiscreteFunctionSpaceType
for convenience
Definition: scalarproducts.hh:62
DiscreteFunction DiscreteFunctionType
Definition: scalarproducts.hh:487
const MapperType & mapper_
Definition: scalarproducts.hh:76
bool fixedsize(int dim, int codim) const
Definition: scalarproducts.hh:357
Space SpaceType
type of discrete function space
Definition: scalarproducts.hh:55
SingletonKey(const SingletonKey &other)
copy constructor
Definition: scalarproducts.hh:292
SpaceType::GridPartType GridPartType
type of grid part
Definition: scalarproducts.hh:65
const DiscreteFunctionSpaceType & space() const
return discrete function space
Definition: scalarproducts.hh:456
const int myRank_
Definition: scalarproducts.hh:331
IndexMapType slaves_
Definition: scalarproducts.hh:82
bool contains(int dim, int codim) const
Definition: scalarproducts.hh:352
void finalize()
finalize
Definition: scalarproducts.hh:143
void rebuild()
check if grid has changed and rebuild cache if necessary
Definition: scalarproducts.hh:156
const int mySize_
Definition: scalarproducts.hh:79
DiscreteFunctionSpace DiscreteFunctionSpaceType
type of the discrete function space
Definition: scalarproducts.hh:418
Definition: scalarproducts.hh:44
ParallelScalarProduct(const DiscreteFunctionSpaceType &space)
constructor taking space
Definition: scalarproducts.hh:513
const SpaceType & space_
Definition: scalarproducts.hh:280