1#ifndef DUNE_FEM_SPACE_MAPPER_GHOST_HH
2#define DUNE_FEM_SPACE_MAPPER_GHOST_HH
13#include <dune/grid/common/gridenums.hh>
15#include <dune/fem/gridpart/common/indexset.hh>
16#include <dune/fem/space/mapper/capabilities.hh>
17#include <dune/fem/storage/envelope.hh>
28 template<
class Gr
idPart,
class Mapper >
31 template<
class AuxiliaryDofs >
36 namespace __GhostDofMapper
42 template<
class BaseMapper >
43 struct BuildDataHandle
44 :
public CommDataHandleIF< BuildDataHandle< BaseMapper >, std::pair< int, std::size_t > >
46 typedef std::pair< int, std::size_t > Data;
48 explicit BuildDataHandle (
int rank,
const BaseMapper &baseMapper, std::vector< std::tuple< int, std::size_t, std::size_t > > &masters )
49 : rank_( rank ), baseMapper_( baseMapper ), masters_( masters )
52 bool contains (
int dim,
int codim )
const {
return baseMapper_.contains( codim ); }
53 bool fixedSize (
int dim,
int codim )
const {
return baseMapper_.fixedDataSize( codim ); }
55 template<
class Buffer,
class Entity >
56 void gather ( Buffer &buffer,
const Entity &entity )
const
58 baseMapper_.mapEachEntityDof( entity, [
this, &buffer ] (
int,
auto index ) {
59 std::get< 0 >( masters_[ index ] ) = rank_;
60 buffer.write( Data( std::get< 0 >( masters_[ index ] ), std::get< 1 >( masters_[ index ] ) ) );
64 template<
class Buffer,
class Entity >
65 void scatter ( Buffer &buffer,
const Entity &entity, std::size_t n )
67 assert( n ==
size( entity ) );
69 baseMapper_.mapEachEntityDof( entity, [
this, &buffer ] (
int,
auto index ) {
70 Data remote( -1, index );
71 buffer.read( remote );
72 assert( remote.first >= 0 );
74 auto &local = masters_[ index ];
75 if( (std::get< 0 >( local ) < 0) || (remote.first < std::get< 0 >( local )) )
76 std::tie( std::get< 0 >( local ), std::get< 1 >( local ) ) = remote;
80 template<
class Entity >
81 std::size_t
size (
const Entity &entity )
const
83 return baseMapper_.numEntityDofs( entity );
88 const BaseMapper &baseMapper_;
89 std::vector< std::tuple< int, std::size_t, std::size_t > > &masters_;
97 template<
class Index >
100 typedef ConstIterator< Index > ThisType;
103 typedef std::random_access_iterator_tag iterator_category;
104 typedef Index value_type;
105 typedef Index difference_type;
106 typedef Envelope< Index > pointer;
107 typedef Index reference;
109 ConstIterator () noexcept = default;
110 explicit ConstIterator ( Index index ) noexcept : index_( index ) {}
112 Index operator* () const noexcept {
return index_; }
113 Envelope< Index > operator-> () const noexcept {
return Envelope< Index >( index_ ); }
115 Index operator[] ( Index n )
const noexcept {
return index_ + n; }
117 bool operator== (
const ThisType &other )
const noexcept {
return (index_ == other.index_); }
118 bool operator!= (
const ThisType &other )
const noexcept {
return (index_ != other.index_); }
120 ThisType &operator++ () noexcept { ++index_;
return *
this; }
121 ThisType operator++ (
int )
noexcept { ThisType copy( *
this ); ++(*this);
return copy; }
123 ThisType &operator-- () noexcept { --index_;
return *
this; }
124 ThisType operator-- (
int )
noexcept { ThisType copy( *
this ); --(*this);
return copy; }
126 ThisType &operator+= ( Index n )
noexcept { index_ += n;
return *
this; }
127 ThisType &operator-= ( Index n )
noexcept { index_ -= n;
return *
this; }
129 ThisType operator+ ( Index n )
const noexcept {
return ThisType( index_ + n ); }
130 ThisType operator- ( Index n )
const noexcept {
return ThisType( index_ - n ); }
132 friend ThisType operator+ ( Index n,
const ThisType &i )
noexcept {
return i + n; }
134 Index operator- (
const ThisType &other )
const noexcept {
return (index_ - other.index_); }
136 bool operator< (
const ThisType &other )
const noexcept {
return (index_ < other.index_); }
137 bool operator<= (
const ThisType &other )
const noexcept {
return (index_ <= other.index_); }
138 bool operator>= (
const ThisType &other )
const noexcept {
return (index_ >= other.index_); }
139 bool operator> (
const ThisType &other )
const noexcept {
return (index_ > other.index_); }
152 template<
class Gr
idPart,
class BaseMapper,
class GlobalKey = std::
size_t >
155 typedef GhostDofMapper< GridPart, BaseMapper, GlobalKey > ThisType;
158 typedef GridPart GridPartType;
159 typedef BaseMapper BaseMapperType;
161 typedef std::size_t SizeType;
162 typedef GlobalKey GlobalKeyType;
164 typedef typename BaseMapperType::ElementType ElementType;
166 GhostDofMapper (
const GridPartType &gridPart, BaseMapperType &baseMapper )
167 : gridPart_( gridPart ), baseMapper_( baseMapper )
172 GhostDofMapper (
const ThisType & ) =
delete;
173 GhostDofMapper ( ThisType && ) =
delete;
175 ThisType &operator= (
const ThisType & ) =
delete;
176 ThisType &operator= ( ThisType && ) =
delete;
178 template<
class Functor >
179 void mapEach (
const ElementType &element, Functor f )
const
181 baseMapper().mapEach( element, [
this, &f ] (
auto local,
auto i ) { f( local, mapping_[ i ] ); } );
184 void map (
const ElementType &element, std::vector< GlobalKeyType > &indices )
const
186 indices.resize( numDofs( element ) );
187 mapEach( element, [ &indices ] (
int local, GlobalKeyType global ) { indices[ local ] = global; } );
190 [[deprecated(
"Use onSubEntity method with char vector instead")]]
191 void onSubEntity (
const ElementType &element,
int i,
int c, std::vector< bool > &indices )
const
193 std::vector< char > _idx;
194 onSubEntity(element, i, c, _idx);
195 indices.resize( _idx.size() );
196 for (std::size_t i=0; i<_idx.size();++i)
197 _idx[i] = indices[i] > 0;
207 void onSubEntity (
const ElementType &element,
int i,
int c, std::vector< char > &indices )
const
209 baseMapper().onSubEntity( element, i, c, indices );
212 unsigned int maxNumDofs ()
const {
return baseMapper().maxNumDofs(); }
213 unsigned int numDofs (
const ElementType &element )
const {
return baseMapper().numDofs( element ); }
217 template<
class Entity,
class Functor >
218 void mapEachEntityDof (
const Entity &entity, Functor f )
const
220 baseMapper().mapEachEntityDof( entity, [
this, &f ] (
auto local,
auto i ) { f( local, mapping_[ i ] ); } );
223 template<
class Entity >
224 void mapEntityDofs (
const Entity &entity, std::vector< GlobalKeyType > &indices )
const
226 indices.resize( numEntityDofs( entity ) );
227 mapEachEntityDof( entity, [ &indices ] (
int local, GlobalKeyType global ) { indices[ local ] = global; } );
230 template<
class Entity >
231 unsigned int numEntityDofs (
const Entity &entity )
const
233 return baseMapper().numEntityDofs( entity );
238 bool contains (
int codim )
const {
return baseMapper().contains( codim ); }
240 bool fixedDataSize (
int codim )
const {
return baseMapper().fixedDataSize( codim ); }
242 SizeType interiorSize ()
const {
return interiorSize_; }
243 SizeType ghostSize ()
const {
return ghostSize_; }
245 SizeType
size ()
const {
return interiorSize() + ghostSize(); }
249 bool consecutive ()
const {
return false; }
251 int numBlocks ()
const {
DUNE_THROW( NotImplemented,
"Adaptive dof mapper interface not implemented." ); }
252 SizeType offSet (
int blk )
const {
DUNE_THROW( NotImplemented,
"Adaptive dof mapper interface not implemented." ); }
253 SizeType oldOffSet (
int blk )
const {
DUNE_THROW( NotImplemented,
"Adaptive dof mapper interface not implemented." ); }
254 SizeType numberOfHoles (
int blk )
const {
DUNE_THROW( NotImplemented,
"Adaptive dof mapper interface not implemented." ); }
255 SizeType oldIndex ( SizeType hole,
int blk )
const {
DUNE_THROW( NotImplemented,
"Adaptive dof mapper interface not implemented." ); }
256 SizeType newIndex ( SizeType hole,
int blk )
const {
DUNE_THROW( NotImplemented,
"Adaptive dof mapper interface not implemented." ); }
263 std::size_t baseSize = baseMapper().size();
264 mapping_.resize( baseSize );
266 std::vector< std::tuple< int, std::size_t, std::size_t > > masters( baseSize );
267 for( std::size_t i = 0; i < baseSize; ++i )
268 masters[ i ] = std::make_tuple( -1, i, i );
270 const int rank = gridPart().comm().rank();
271 __GhostDofMapper::BuildDataHandle< BaseMapper > dataHandle( rank, baseMapper_, masters );
278 interiorSize_ = ghostSize_ = 0;
279 for(
const auto &m : masters )
281 if( (std::get< 0 >( m ) == -1) || (std::get< 0 >( m ) == rank ) )
282 mapping_[ std::get< 2 >( m ) ] = interiorSize_++;
284 masters[ ghostSize_++ ] = m;
286 masters.resize( ghostSize_ );
289 const auto less = [] (
auto a,
auto b ) {
return (std::get< 0 >( a ) < std::get< 0 >( b )) || ((std::get< 0 >( a ) == std::get< 0 >( b )) && (std::get< 1 >( a ) < std::get< 1 >( b ))); };
290 std::sort( masters.begin(), masters.end(), less );
294 std::tuple< int, std::size_t, std::size_t > current( -1, 0, 0 );
295 for(
const auto &m : masters )
297 if( less( current, m ) )
300 std::get< 2 >( current ) = interiorSize_ + ghostSize_++;
302 mapping_[ std::get< 2 >( m ) ] = std::get< 2 >( current );
306 const GridPartType &gridPart ()
const {
return gridPart_; }
307 const BaseMapperType &baseMapper ()
const {
return baseMapper_; }
309 const std::vector< GlobalKeyType > &mapping ()
const {
return mapping_; }
312 const GridPartType &gridPart_;
313 BaseMapperType &baseMapper_;
314 std::vector< GlobalKeyType > mapping_;
315 SizeType interiorSize_, ghostSize_;
323 namespace Capabilities
326 template<
class Gr
idPart,
class BaseMapper,
class GlobalKey >
327 struct isAdaptiveDofMapper< GhostDofMapper< GridPart, BaseMapper, GlobalKey > >
329 static const bool v =
false;
332 template<
class Gr
idPart,
class BaseMapper,
class GlobalKey >
333 struct isConsecutiveIndexSet< GhostDofMapper< GridPart, BaseMapper, GlobalKey > >
335 static const bool v =
true;
345 template<
class Gr
idPart,
class BaseMapper,
class GlobalKey >
346 class AuxiliaryDofs< GridPart, GhostDofMapper< GridPart, BaseMapper, GlobalKey > >
348 typedef AuxiliaryDofs< GridPart, GhostDofMapper< GridPart, BaseMapper, GlobalKey > > ThisType;
352 typedef GhostDofMapper< GridPart, BaseMapper, GlobalKey >
MapperType;
354 typedef typename MapperType::GlobalKeyType GlobalKeyType;
355 typedef typename MapperType::SizeType SizeType;
357 typedef __GhostDofMapper::ConstIterator< GlobalKeyType > ConstIteratorType;
359 explicit AuxiliaryDofs (
const MapperType &mapper )
363 AuxiliaryDofs (
const GridPartType &gridPart,
const MapperType &mapper )
364 : AuxiliaryDofs( mapper )
368 GlobalKeyType
operator [] (
int index )
const {
return mapper().interiorSize() + index; }
371 SizeType
size ()
const {
return mapper().ghostSize()+1; }
374 SizeType
primarySize ()
const {
return mapper().interiorSize(); }
376 ConstIteratorType begin ()
const {
return ConstIteratorType( mapper().interiorSize() ); }
377 ConstIteratorType end ()
const {
return ConstIteratorType( mapper().interiorSize() + mapper().ghostSize() ); }
380 bool contains ( GlobalKeyType index )
const {
return (
static_cast< SizeType
>( index ) >= mapper().interiorSize()); }
384 const MapperType &mapper ()
const {
return mapper_; }
385 const GridPartType &gridPart ()
const {
return mapper().gridPart(); }
400 template <
class Gr
idPart,
class BaseMapper,
class GlobalKey,
class F>
403 const size_t size = auxiliaryDofs.mapper().interiorSize();
404 for(
size_t dof = 0 ; dof<
size; ++dof )
417 template<
class Gr
idPart,
class BaseMapper,
class GlobalKey >
418 struct PrimaryDofs< AuxiliaryDofs< GridPart, GhostDofMapper< GridPart, BaseMapper, GlobalKey > > >
420 typedef AuxiliaryDofs< GridPart, GhostDofMapper< GridPart, BaseMapper, GlobalKey > > AuxiliaryDofsType;
422 typedef typename AuxiliaryDofsType::GlobalKeyType GlobalKeyType;
423 typedef typename AuxiliaryDofsType::GridPartType GridPartType;
424 typedef typename AuxiliaryDofsType::MapperType MapperType;
425 typedef typename AuxiliaryDofsType::SizeType SizeType;
427 typedef __GhostDofMapper::ConstIterator< GlobalKeyType > ConstIteratorType;
429 [[deprecated(
"Use forEachPrimaryDof instead!")]]
430 explicit PrimaryDofs (
const AuxiliaryDofsType &auxiliaryDofs )
431 : mapper_( auxiliaryDofs.mapper() )
434 ConstIteratorType begin ()
const {
return ConstIteratorType( 0 ); }
435 ConstIteratorType end ()
const {
return ConstIteratorType(
size() ); }
437 SizeType
size ()
const {
return mapper().interiorSize(); }
439 const MapperType &mapper ()
const {
return mapper_; }
440 const GridPartType &gridPart ()
const {
return mapper().gridPart(); }
443 const MapperType &mapper_;
void scatter(MessageBufferImp &buff, const EntityType &e, size_t n)
unpack data from message buffer to user.
Definition: datahandleif.hh:143
void gather(MessageBufferImp &buff, const EntityType &e) const
pack data from user to message buffer
Definition: datahandleif.hh:129
In parallel computations the dofs of a discrete function are made up by all primary dofs....
Definition: auxiliarydofs.hh:46
Describes the parallel communication interface class for MessageBuffers and DataHandles.
GridPart GridPartType
type of grid part
Definition: auxiliarydofs.hh:53
bool contains(IndexType index) const
return true if index is contained, meaning it is a auxiliary dof
Definition: auxiliarydofs.hh:146
IndexType operator[](const IndexType index) const
return dof number of auxiliary for index
Definition: auxiliarydofs.hh:122
IndexType size() const
return number of auxiliary dofs
Definition: auxiliarydofs.hh:128
Mapper MapperType
type of used mapper
Definition: auxiliarydofs.hh:56
IndexType primarySize() const
return number of primaryDofs
Definition: auxiliarydofs.hh:134
static void forEachPrimaryDof(const AuxiliaryDofs< GridPart, GhostDofMapper< GridPart, BaseMapper, GlobalKey > > &auxiliaryDofs, F &&f)
Apply action encoded in Functor f to all primary dofs.
Definition: ghost.hh:401
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
EnableIfInterOperable< T1, T2, bool >::type operator<(const RandomAccessIteratorFacade< T1, V1, R1, D > &lhs, const RandomAccessIteratorFacade< T2, V2, R2, D > &rhs)
Comparison operator.
Definition: iteratorfacades.hh:638
EnableIfInterOperable< T1, T2, bool >::type operator>(const RandomAccessIteratorFacade< T1, V1, R1, D > &lhs, const RandomAccessIteratorFacade< T2, V2, R2, D > &rhs)
Comparison operator.
Definition: iteratorfacades.hh:684
EnableIfInterOperable< T1, T2, bool >::type operator<=(const RandomAccessIteratorFacade< T1, V1, R1, D > &lhs, const RandomAccessIteratorFacade< T2, V2, R2, D > &rhs)
Comparison operator.
Definition: iteratorfacades.hh:661
EnableIfInterOperable< T1, T2, bool >::type operator==(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for equality.
Definition: iteratorfacades.hh:238
EnableIfInterOperable< T1, T2, bool >::type operator>=(const RandomAccessIteratorFacade< T1, V1, R1, D > &lhs, const RandomAccessIteratorFacade< T2, V2, R2, D > &rhs)
Comparison operator.
Definition: iteratorfacades.hh:706
EnableIfInterOperable< T1, T2, bool >::type operator!=(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for inequality.
Definition: iteratorfacades.hh:260
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 std::bool_constant<((II==value)||...)> contains(std::integer_sequence< T, II... >, std::integral_constant< T, value >)
Checks whether or not a given sequence contains a value.
Definition: integersequence.hh:137
std::size_t fixedSize
The number of data items per index if it is fixed, 0 otherwise.
Definition: variablesizecommunicator.hh:264