dune-fem  2.8-git
ghost.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_SPACE_MAPPER_GHOST_HH
2 #define DUNE_FEM_SPACE_MAPPER_GHOST_HH
3 
4 #include <cstddef>
5 
6 #include <algorithm>
7 #include <iterator>
8 #include <tuple>
9 #include <type_traits>
10 #include <utility>
11 #include <vector>
12 
13 #include <dune/grid/common/datahandleif.hh>
14 #include <dune/grid/common/gridenums.hh>
15 
19 
20 namespace Dune
21 {
22 
23  namespace Fem
24  {
25 
26  // External Forward Declarations
27  // -----------------------------
28 
29  template< class GridPart, class Mapper >
30  class AuxiliaryDofs;
31 
32  template< class AuxiliaryDofs >
33  struct MasterDofs;
34 
35 
36 
37  namespace __GhostDofMapper
38  {
39 
40  // BuildDataHandle
41  // ---------------
42 
43  template< class BaseMapper >
45  : public CommDataHandleIF< BuildDataHandle< BaseMapper >, std::pair< int, std::size_t > >
46  {
47  typedef std::pair< int, std::size_t > Data;
48 
49  explicit BuildDataHandle ( int rank, const BaseMapper &baseMapper, std::vector< std::tuple< int, std::size_t, std::size_t > > &masters )
50  : rank_( rank ), baseMapper_( baseMapper ), masters_( masters )
51  {}
52 
53  bool contains ( int dim, int codim ) const { return baseMapper_.contains( codim ); }
54  bool fixedSize ( int dim, int codim ) const { return baseMapper_.fixedDataSize( codim ); }
55 
56  template< class Buffer, class Entity >
57  void gather ( Buffer &buffer, const Entity &entity ) const
58  {
59  baseMapper_.mapEachEntityDof( entity, [ this, &buffer ] ( int, auto index ) {
60  std::get< 0 >( masters_[ index ] ) = rank_;
61  buffer.write( Data( std::get< 0 >( masters_[ index ] ), std::get< 1 >( masters_[ index ] ) ) );
62  } );
63  }
64 
65  template< class Buffer, class Entity >
66  void scatter ( Buffer &buffer, const Entity &entity, std::size_t n )
67  {
68  assert( n == size( entity ) );
69 
70  baseMapper_.mapEachEntityDof( entity, [ this, &buffer ] ( int, auto index ) {
71  Data remote( -1, index );
72  buffer.read( remote );
73  assert( remote.first >= 0 );
74 
75  auto &local = masters_[ index ];
76  if( (std::get< 0 >( local ) < 0) || (remote.first < std::get< 0 >( local )) )
77  std::tie( std::get< 0 >( local ), std::get< 1 >( local ) ) = remote;
78  } );
79  }
80 
81  template< class Entity >
82  std::size_t size ( const Entity &entity ) const
83  {
84  return baseMapper_.numEntityDofs( entity );
85  }
86 
87  protected:
88  int rank_;
89  const BaseMapper &baseMapper_;
90  std::vector< std::tuple< int, std::size_t, std::size_t > > &masters_;
91  };
92 
93 
94 
95  // ConstIterator
96  // -------------
97 
98  template< class Index >
100  : public std::iterator< std::random_access_iterator_tag, Index, Index, Envelope< Index >, Index >
101  {
103 
104  public:
105  ConstIterator () noexcept = default;
106  explicit ConstIterator ( Index index ) noexcept : index_( index ) {}
107 
108  Index operator* () const noexcept { return index_; }
109  Envelope< Index > operator-> () const noexcept { return Envelope< Index >( index_ ); }
110 
111  Index operator[] ( Index n ) const noexcept { return index_ + n; }
112 
113  bool operator== ( const ThisType &other ) const noexcept { return (index_ == other.index_); }
114  bool operator!= ( const ThisType &other ) const noexcept { return (index_ != other.index_); }
115 
116  ThisType &operator++ () noexcept { ++index_; return *this; }
117  ThisType operator++ ( int ) noexcept { ThisType copy( *this ); ++(*this); return copy; }
118 
119  ThisType &operator-- () noexcept { --index_; return *this; }
120  ThisType operator-- ( int ) noexcept { ThisType copy( *this ); --(*this); return copy; }
121 
122  ThisType &operator+= ( Index n ) noexcept { index_ += n; return *this; }
123  ThisType &operator-= ( Index n ) noexcept { index_ -= n; return *this; }
124 
125  ThisType operator+ ( Index n ) const noexcept { return ThisType( index_ + n ); }
126  ThisType operator- ( Index n ) const noexcept { return ThisType( index_ - n ); }
127 
128  friend ThisType operator+ ( Index n, const ThisType &i ) noexcept { return i + n; }
129 
130  Index operator- ( const ThisType &other ) const noexcept { return (index_ - other.index_); }
131 
132  bool operator< ( const ThisType &other ) const noexcept { return (index_ < other.index_); }
133  bool operator<= ( const ThisType &other ) const noexcept { return (index_ <= other.index_); }
134  bool operator>= ( const ThisType &other ) const noexcept { return (index_ >= other.index_); }
135  bool operator> ( const ThisType &other ) const noexcept { return (index_ > other.index_); }
136 
137  private:
138  Index index_ = 0;
139  };
140 
141  } // namespace __GhostDofMapper
142 
143 
144 
145  // GhostDofMapper
146  // --------------
147 
148  template< class GridPart, class BaseMapper, class GlobalKey = std::size_t >
150  {
152 
153  public:
154  typedef GridPart GridPartType;
155  typedef BaseMapper BaseMapperType;
156 
157  typedef std::size_t SizeType;
158  typedef GlobalKey GlobalKeyType;
159 
160  typedef typename BaseMapperType::ElementType ElementType;
161 
163  : gridPart_( gridPart ), baseMapper_( baseMapper )
164  {
165  update();
166  }
167 
168  GhostDofMapper ( const ThisType & ) = delete;
169  GhostDofMapper ( ThisType && ) = delete;
170 
171  ThisType &operator= ( const ThisType & ) = delete;
172  ThisType &operator= ( ThisType && ) = delete;
173 
174  template< class Functor >
175  void mapEach ( const ElementType &element, Functor f ) const
176  {
177  baseMapper().mapEach( element, [ this, &f ] ( auto local, auto i ) { f( local, mapping_[ i ] ); } );
178  }
179 
180  void map ( const ElementType &element, std::vector< GlobalKeyType > &indices ) const
181  {
182  indices.resize( numDofs( element ) );
183  mapEach( element, [ &indices ] ( int local, GlobalKeyType global ) { indices[ local ] = global; } );
184  }
185 
186  void onSubEntity ( const ElementType &element, int i, int c, std::vector< bool > &indices ) const
187  {
188  baseMapper().onSubEntity( element, i, c, indices );
189  }
190 
191  unsigned int maxNumDofs () const { return baseMapper().maxNumDofs(); }
192  unsigned int numDofs ( const ElementType &element ) const { return baseMapper().numDofs( element ); }
193 
194  // assignment of DoFs to entities
195 
196  template< class Entity, class Functor >
197  void mapEachEntityDof ( const Entity &entity, Functor f ) const
198  {
199  baseMapper().mapEachEntityDof( entity, [ this, &f ] ( auto local, auto i ) { f( local, mapping_[ i ] ); } );
200  }
201 
202  template< class Entity >
203  void mapEntityDofs ( const Entity &entity, std::vector< GlobalKeyType > &indices ) const
204  {
205  indices.resize( numEntityDofs( entity ) );
206  mapEachEntityDof( entity, [ &indices ] ( int local, GlobalKeyType global ) { indices[ local ] = global; } );
207  }
208 
209  template< class Entity >
210  unsigned int numEntityDofs ( const Entity &entity ) const
211  {
212  return baseMapper().numEntityDofs( entity );
213  }
214 
215  // global information
216 
217  bool contains ( int codim ) const { return baseMapper().contains( codim ); }
218 
219  bool fixedDataSize ( int codim ) const { return baseMapper().fixedDataSize( codim ); }
220 
221  SizeType interiorSize () const { return interiorSize_; }
222  SizeType ghostSize () const { return ghostSize_; }
223 
224  SizeType size () const { return interiorSize() + ghostSize(); }
225 
226  // adaptation interface
227 
228  bool consecutive () const { return false; }
229 
230  int numBlocks () const { DUNE_THROW( NotImplemented, "Adaptive dof mapper interface not implemented." ); }
231  SizeType offSet ( int blk ) const { DUNE_THROW( NotImplemented, "Adaptive dof mapper interface not implemented." ); }
232  SizeType oldOffSet ( int blk ) const { DUNE_THROW( NotImplemented, "Adaptive dof mapper interface not implemented." ); }
233  SizeType numberOfHoles ( int blk ) const { DUNE_THROW( NotImplemented, "Adaptive dof mapper interface not implemented." ); }
234  SizeType oldIndex ( SizeType hole, int blk ) const { DUNE_THROW( NotImplemented, "Adaptive dof mapper interface not implemented." ); }
235  SizeType newIndex ( SizeType hole, int blk ) const { DUNE_THROW( NotImplemented, "Adaptive dof mapper interface not implemented." ); }
236 
237  // update
238 
239  void update ()
240  {
241  std::size_t baseSize = baseMapper().size();
242  mapping_.resize( baseSize );
243 
244  std::vector< std::tuple< int, std::size_t, std::size_t > > masters( baseSize );
245  for( std::size_t i = 0; i < baseSize; ++i )
246  masters[ i ] = std::make_tuple( -1, i, i );
247 
248  const int rank = gridPart().comm().rank();
249  __GhostDofMapper::BuildDataHandle< BaseMapper > dataHandle( rank, baseMapper_, masters );
250  gridPart().communicate( dataHandle, InteriorBorder_All_Interface, ForwardCommunication );
251  // at this point all shared DoFs are assigned to their master rank
252  // all other DoFs (with rank -1) are not shared at all
253 
254  // assign indices to interiors
255  interiorSize_ = ghostSize_ = 0;
256  for( const auto &m : masters )
257  {
258  if( (std::get< 0 >( m ) == -1) || (std::get< 0 >( m ) == rank ) )
259  mapping_[ std::get< 2 >( m ) ] = interiorSize_++;
260  else
261  masters[ ghostSize_++ ] = m;
262  }
263  masters.resize( ghostSize_ );
264 
265  // sort the masters (by the first two components) to find duplicate ghosts
266  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 ))); };
267  std::sort( masters.begin(), masters.end(), less );
268 
269  // assign indices to ghosts
270  ghostSize_ = 0;
271  std::tuple< int, std::size_t, std::size_t > current( -1, 0, 0 );
272  for( const auto &m : masters )
273  {
274  if( less( current, m ) )
275  {
276  current = m;
277  std::get< 2 >( current ) = interiorSize_ + ghostSize_++;
278  }
279  mapping_[ std::get< 2 >( m ) ] = std::get< 2 >( current );
280  }
281  }
282 
283  const GridPartType &gridPart () const { return gridPart_; }
284  const BaseMapperType &baseMapper () const { return baseMapper_; }
285 
286  const std::vector< GlobalKeyType > &mapping () const { return mapping_; }
287 
288  private:
289  const GridPartType &gridPart_;
290  BaseMapperType &baseMapper_;
291  std::vector< GlobalKeyType > mapping_;
292  SizeType interiorSize_, ghostSize_;
293  };
294 
295 
296 
297  // Capabilities for IndexSetDofMapper
298  // ----------------------------------
299 
300  namespace Capabilities
301  {
302 
303  template< class GridPart, class BaseMapper, class GlobalKey >
304  struct isAdaptiveDofMapper< GhostDofMapper< GridPart, BaseMapper, GlobalKey > >
305  {
306  static const bool v = false;
307  };
308 
309  template< class GridPart, class BaseMapper, class GlobalKey >
310  struct isConsecutiveIndexSet< GhostDofMapper< GridPart, BaseMapper, GlobalKey > >
311  {
312  static const bool v = true;
313  };
314 
315  } // namespace Capabilities
316 
317 
318 
319  // AuxiliaryDofs for GhostDofMapper
320  // ----------------------------
321 
322  template< class GridPart, class BaseMapper, class GlobalKey >
323  class AuxiliaryDofs< GridPart, GhostDofMapper< GridPart, BaseMapper, GlobalKey > >
324  {
326 
327  public:
328  typedef GridPart GridPartType;
330 
332  typedef typename MapperType::SizeType SizeType;
333 
335 
336  explicit AuxiliaryDofs ( const MapperType &mapper )
337  : mapper_( mapper )
338  {}
339 
340  AuxiliaryDofs ( const GridPartType &gridPart, const MapperType &mapper )
341  : AuxiliaryDofs( mapper )
342  {}
343 
345  GlobalKeyType operator [] ( int index ) const { return mapper().interiorSize() + index; }
346 
348  SizeType size () const { return mapper().ghostSize()+1; }
349 
350  ConstIteratorType begin () const { return ConstIteratorType( mapper().interiorSize() ); }
351  ConstIteratorType end () const { return ConstIteratorType( mapper().interiorSize() + mapper().ghostSize() ); }
352 
354  bool contains ( GlobalKeyType index ) const { return (static_cast< SizeType >( index ) >= mapper().interiorSize()); }
355 
356  void rebuild () {}
357 
358  const MapperType &mapper () const { return mapper_; }
359  const GridPartType &gridPart () const { return mapper().gridPart(); }
360 
361  private:
362  const MapperType &mapper_;
363  };
364 
365 
366 
367  // MasterDofs for AuxiliaryDofs< GhostDofMapper >
368  // ------------------------------------------
369 
370  template< class GridPart, class BaseMapper, class GlobalKey >
371  struct MasterDofs< AuxiliaryDofs< GridPart, GhostDofMapper< GridPart, BaseMapper, GlobalKey > > >
372  {
374 
379 
381 
382  explicit MasterDofs ( const AuxiliaryDofsType &auxiliaryDofs )
383  : mapper_( auxiliaryDofs.mapper() )
384  {}
385 
386  ConstIteratorType begin () const { return ConstIteratorType( 0 ); }
387  ConstIteratorType end () const { return ConstIteratorType( size() ); }
388 
389  SizeType size () const { return mapper().interiorSize(); }
390 
391  const MapperType &mapper () const { return mapper_; }
392  const GridPartType &gridPart () const { return mapper().gridPart(); }
393 
394  private:
395  const MapperType &mapper_;
396  };
397 
398  } // namespace Fem
399 
400 } // namespace Dune
401 
402 #endif // #ifndef DUNE_FEM_SPACE_MAPPER_GHOST_HH
int operator[](const int index) const
return dof number of auxiliary for index
Definition: auxiliarydofs.hh:116
const GridPartType & gridPart() const
Definition: auxiliarydofs.hh:144
Mapper MapperType
type of used mapper
Definition: auxiliarydofs.hh:57
const MapperType & mapper_
Definition: auxiliarydofs.hh:63
Definition: bindguard.hh:11
specialize with true if index set implements the interface for consecutive index sets
Definition: common/indexset.hh:42
static const bool v
Definition: common/indexset.hh:49
In parallel computations the dofs of a discrete function are made up by all primary dofs....
Definition: auxiliarydofs.hh:47
Definition: space/mapper/capabilities.hh:22
static const bool v
Definition: space/mapper/capabilities.hh:23
Definition: ghost.hh:33
bool fixedSize(int dim, int codim) const
Definition: ghost.hh:54
void gather(Buffer &buffer, const Entity &entity) const
Definition: ghost.hh:57
std::vector< std::tuple< int, std::size_t, std::size_t > > & masters_
Definition: ghost.hh:90
const BaseMapper & baseMapper_
Definition: ghost.hh:89
std::size_t size(const Entity &entity) const
Definition: ghost.hh:82
std::pair< int, std::size_t > Data
Definition: ghost.hh:47
BuildDataHandle(int rank, const BaseMapper &baseMapper, std::vector< std::tuple< int, std::size_t, std::size_t > > &masters)
Definition: ghost.hh:49
void scatter(Buffer &buffer, const Entity &entity, std::size_t n)
Definition: ghost.hh:66
bool contains(int dim, int codim) const
Definition: ghost.hh:53
bool operator>=(const ThisType &other) const noexcept
Definition: ghost.hh:134
ThisType & operator+=(Index n) noexcept
Definition: ghost.hh:122
ThisType & operator--() noexcept
Definition: ghost.hh:119
ThisType & operator++() noexcept
Definition: ghost.hh:116
ThisType operator-(Index n) const noexcept
Definition: ghost.hh:126
ThisType operator+(Index n) const noexcept
Definition: ghost.hh:125
Index operator[](Index n) const noexcept
Definition: ghost.hh:111
bool operator<=(const ThisType &other) const noexcept
Definition: ghost.hh:133
bool operator>(const ThisType &other) const noexcept
Definition: ghost.hh:135
Index operator*() const noexcept
Definition: ghost.hh:108
bool operator==(const ThisType &other) const noexcept
Definition: ghost.hh:113
Envelope< Index > operator->() const noexcept
Definition: ghost.hh:109
ThisType & operator-=(Index n) noexcept
Definition: ghost.hh:123
bool operator<(const ThisType &other) const noexcept
Definition: ghost.hh:132
bool operator!=(const ThisType &other) const noexcept
Definition: ghost.hh:114
Definition: ghost.hh:150
unsigned int numDofs(const ElementType &element) const
Definition: ghost.hh:192
bool fixedDataSize(int codim) const
Definition: ghost.hh:219
SizeType offSet(int blk) const
Definition: ghost.hh:231
SizeType numberOfHoles(int blk) const
Definition: ghost.hh:233
void onSubEntity(const ElementType &element, int i, int c, std::vector< bool > &indices) const
Definition: ghost.hh:186
SizeType size() const
Definition: ghost.hh:224
bool contains(int codim) const
Definition: ghost.hh:217
int numBlocks() const
Definition: ghost.hh:230
GhostDofMapper(const ThisType &)=delete
GhostDofMapper(ThisType &&)=delete
GhostDofMapper(const GridPartType &gridPart, BaseMapperType &baseMapper)
Definition: ghost.hh:162
void mapEntityDofs(const Entity &entity, std::vector< GlobalKeyType > &indices) const
Definition: ghost.hh:203
GlobalKey GlobalKeyType
Definition: ghost.hh:158
void update()
Definition: ghost.hh:239
SizeType oldOffSet(int blk) const
Definition: ghost.hh:232
void mapEachEntityDof(const Entity &entity, Functor f) const
Definition: ghost.hh:197
ThisType & operator=(const ThisType &)=delete
GridPart GridPartType
Definition: ghost.hh:154
unsigned int numEntityDofs(const Entity &entity) const
Definition: ghost.hh:210
void mapEach(const ElementType &element, Functor f) const
Definition: ghost.hh:175
const BaseMapperType & baseMapper() const
Definition: ghost.hh:284
BaseMapperType::ElementType ElementType
Definition: ghost.hh:160
bool consecutive() const
Definition: ghost.hh:228
SizeType ghostSize() const
Definition: ghost.hh:222
const GridPartType & gridPart() const
Definition: ghost.hh:283
const std::vector< GlobalKeyType > & mapping() const
Definition: ghost.hh:286
unsigned int maxNumDofs() const
Definition: ghost.hh:191
SizeType interiorSize() const
Definition: ghost.hh:221
BaseMapper BaseMapperType
Definition: ghost.hh:155
std::size_t SizeType
Definition: ghost.hh:157
SizeType newIndex(SizeType hole, int blk) const
Definition: ghost.hh:235
void map(const ElementType &element, std::vector< GlobalKeyType > &indices) const
Definition: ghost.hh:180
SizeType oldIndex(SizeType hole, int blk) const
Definition: ghost.hh:234
GhostDofMapper< GridPart, BaseMapper, GlobalKey > MapperType
Definition: ghost.hh:329
SizeType size() const
return number of auxiliary dofs
Definition: ghost.hh:348
__GhostDofMapper::ConstIterator< GlobalKeyType > ConstIteratorType
Definition: ghost.hh:334
bool contains(GlobalKeyType index) const
return true if index is contained, meaning it is an auxiliary dof
Definition: ghost.hh:354
AuxiliaryDofs(const GridPartType &gridPart, const MapperType &mapper)
Definition: ghost.hh:340
__GhostDofMapper::ConstIterator< GlobalKeyType > ConstIteratorType
Definition: ghost.hh:380
AuxiliaryDofs< GridPart, GhostDofMapper< GridPart, BaseMapper, GlobalKey > > AuxiliaryDofsType
Definition: ghost.hh:373
Definition: envelope.hh:11