dune-grid  2.8.0
mcmgmapper.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 
4 #ifndef DUNE_GRID_COMMON_MCMGMAPPER_HH
5 #define DUNE_GRID_COMMON_MCMGMAPPER_HH
6 
7 #include <functional>
8 #include <iostream>
9 
10 #include <dune/common/exceptions.hh>
11 #include <dune/common/rangeutilities.hh>
12 #include <dune/geometry/dimension.hh>
13 #include <dune/geometry/referenceelements.hh>
14 #include <dune/geometry/type.hh>
15 #include <dune/geometry/typeindex.hh>
16 
17 #include "mapper.hh"
18 
25 namespace Dune
26 {
34  //
35  // Common Layout templates
36  //
37 
62  using MCMGLayout = std::function<size_t(GeometryType, int)>;
63 
69  template<int codim>
70  MCMGLayout mcmgLayout(Codim<codim>)
71  {
72  return [](GeometryType gt, int dimgrid) {
73  return dimgrid - gt.dim() == codim;
74  };
75  }
76 
82  template<int dim>
84  {
85  return [](GeometryType gt, int) {
86  return gt.dim() == dim;
87  };
88  }
89 
96  {
97  return mcmgLayout(Codim<0>());
98  }
99 
106  {
107  return mcmgLayout(Dim<0>());
108  }
109 
111  //
112  // MultipleCodimMultipleGeomTypeMapper
113  //
114 
124  template <typename GV>
126  public Mapper<typename GV::Grid,MultipleCodimMultipleGeomTypeMapper<GV>, typename GV::IndexSet::IndexType >
127  {
128  public:
129 
131  typedef GV GridView;
132 
134  typedef typename GV::IndexSet::IndexType Index;
135 
140  using size_type = decltype(std::declval<typename GV::IndexSet>().size(0));
141 
154  : gridView_(gridView)
155  , indexSet_(&gridView_.indexSet())
156  , layout_(layout)
157  {
158  update(gridView);
159  }
160 
168  template<class EntityType>
169  Index index (const EntityType& e) const
170  {
171  const GeometryType gt = e.type();
172  assert(offset(gt) != invalidOffset);
173  return indexSet_->index(e)*blockSize(gt) + offset(gt);
174  }
175 
183  Index subIndex (const typename GV::template Codim<0>::Entity& e, int i, unsigned int codim) const
184  {
185  const GeometryType eType = e.type();
186  GeometryType gt = eType.isNone() ?
187  GeometryTypes::none( GV::dimension - codim ) :
188  ReferenceElements<double,GV::dimension>::general(eType).type(i,codim) ;
189  //GeometryType gt=ReferenceElements<double,GV::dimension>::general(e.type()).type(i,codim);
190  assert(offset(gt) != invalidOffset);
191  return indexSet_->subIndex(e, i, codim)*blockSize(gt) + offset(gt);
192  }
193 
202  size_type size () const
203  {
204  return n;
205  }
206 
209  {
210  return blockSize(gt);
211  }
212 
214  const std::vector< GeometryType >& types ( int codim ) const
215  {
216  return myTypes_[ codim ];
217  }
218 
228  template<class EntityType>
229  IntegralRange<Index> indices (const EntityType& e) const
230  {
231  if(!indexSet_->contains(e) || offset(e.type()) == invalidOffset)
232  return {0,0};
233  Index start = index(e);
234  return {start, start+blockSize(e.type())};
235  }
236 
248  IntegralRange<Index> indices (const typename GV::template Codim<0>::Entity& e, int i, int cc) const
249  {
250  const GeometryType eType = e.type();
251  const GeometryType gt = eType.isNone() ?
252  GeometryTypes::none(GV::dimension - cc) :
253  ReferenceElements<double,GV::dimension>::general(eType).type(i,cc) ;
254  if (offset(gt) == invalidOffset)
255  return {0,0};
256  else
257  {
258  Index start = subIndex(e,i,cc);
259  return {start, start+blockSize(gt)};
260  }
261  }
262 
269  template<class EntityType>
270  bool contains (const EntityType& e, Index& result) const
271  {
272  if(!indexSet_->contains(e) || offset(e.type()) == invalidOffset)
273  {
274  result = 0;
275  return false;
276  }
277  result = index(e);
278  return true;
279  }
280 
289  bool contains (const typename GV::template Codim<0>::Entity& e, int i, int cc, Index& result) const
290  {
291  const GeometryType eType = e.type();
292  const GeometryType gt = eType.isNone() ?
293  GeometryTypes::none( GV::dimension - cc ) :
294  ReferenceElements<double,GV::dimension>::general(eType).type(i,cc) ;
295  if (offset(gt) == invalidOffset)
296  return false;
297  result = indexSet_->subIndex(e, i, cc)*blockSize(gt) + offset(gt);
298  return true;
299  }
300 
306  void update (const GV& gridView)
307  {
308  gridView_ = gridView;
309  indexSet_ = &gridView_.indexSet();
310  update_();
311  }
312 
318  void update (GV&& gridView)
319  {
320  gridView_ = std::move(gridView);
321  indexSet_ = &gridView_.indexSet();
322  update_();
323  }
324 
327  [[deprecated("Use update(gridView) instead! Will be removed after release 2.8.")]]
328  void update ()
329  {
330  update_();
331  }
332 
333  const MCMGLayout &layout () const { return layout_; }
334  const GridView &gridView () const { return gridView_; }
335 
336  private:
337  void update_()
338  {
339  n = 0;
340 
341  std::fill(offsets.begin(),offsets.end(),Index(0));
342  std::fill(blocks.begin(),blocks.end(),Index(0));
343 
344  for (unsigned int codim = 0; codim <= GV::dimension; ++codim)
345  {
346  // walk over all geometry types in the codimension
347  for (const GeometryType& gt : indexSet_->types(codim)) {
348  Index offset;
349  size_t block = layout()(gt, GV::Grid::dimension);
350 
351  // if the geometry type is contained in the layout, increment offset
352  // and store geometry type
353  if (block) {
354  offset = n;
355  n += indexSet_->size(gt) * block;
356  myTypes_[codim].push_back(gt);
357  }
358  else {
359  offset = invalidOffset;
360  }
361 
362  offsets[GlobalGeometryTypeIndex::index(gt)] = offset;
363  blocks[GlobalGeometryTypeIndex::index(gt)] = block;
364  }
365  }
366  }
367 
368  Index offset(GeometryType gt) const
369  { return offsets[GlobalGeometryTypeIndex::index(gt)]; }
370  Index blockSize(GeometryType gt) const
371  { return blocks[GlobalGeometryTypeIndex::index(gt)]; }
372 
373  static const Index invalidOffset = std::numeric_limits<Index>::max();
374 
375  // number of data elements required
376  unsigned int n;
377  // GridView is needed to keep the IndexSet valid
378  GV gridView_;
379  const typename GV::IndexSet* indexSet_;
380  // provide an array for the offsets
381  std::array<Index, GlobalGeometryTypeIndex::size(GV::dimension)> offsets;
382  std::array<Index, GlobalGeometryTypeIndex::size(GV::dimension)> blocks;
383  const MCMGLayout layout_; // get layout object
384  std::vector<GeometryType> myTypes_[GV::dimension+1];
385  };
386 
388  //
389  // Leaf and level mapper
390  //
391 
398  template <typename G>
399  class [[deprecated("Use MultipleCodimMultipleGeomTypeMapper instead! Will be removed after release 2.8.")]]
401  : public MultipleCodimMultipleGeomTypeMapper<typename G::LeafGridView>
402  {
404  public:
405 
413  : Base(grid.leafGridView(), layout)
414  , gridPtr_(&grid)
415  {}
416 
422  void update ()
423  {
424  Base::update(gridPtr_->leafGridView());
425  }
426 
427  private:
428  const G* gridPtr_;
429  };
430 
438  template <typename G>
439  class [[deprecated("Use MultipleCodimMultipleGeomTypeMapper instead! Will be removed after release 2.8.")]]
441  : public MultipleCodimMultipleGeomTypeMapper<typename G::LevelGridView> {
443  public:
444 
452  LevelMultipleCodimMultipleGeomTypeMapper (const G& grid, int level, const MCMGLayout& layout)
453  : Base(grid.levelGridView(level),layout)
454  , gridPtr_(&grid)
455  , level_(level)
456  {}
457 
463  void update ()
464  {
465  Base::update(gridPtr_->levelGridView(level_));
466  }
467 
468  private:
469  const G* gridPtr_;
470  int level_;
471  };
472 
474 }
475 #endif
Provides classes with basic mappers which are used to attach data to a grid.
Grid< dim, dimworld, ct, GridFamily >::LeafGridView leafGridView(const Grid< dim, dimworld, ct, GridFamily > &grid)
leaf grid view for the given grid
Definition: common/grid.hh:808
Grid< dim, dimworld, ct, GridFamily >::LevelGridView levelGridView(const Grid< dim, dimworld, ct, GridFamily > &grid, int level)
level grid view for the given grid and level.
Definition: common/grid.hh:791
MCMGLayout mcmgLayout(Codim< codim >)
layout for entities of codimension codim
Definition: mcmgmapper.hh:70
MCMGLayout mcmgElementLayout()
layout for elements (codim-0 entities)
Definition: mcmgmapper.hh:95
std::function< size_t(GeometryType, int)> MCMGLayout
layout function for MultipleCodimMultipleGeomTypeMapper
Definition: mcmgmapper.hh:62
MCMGLayout mcmgVertexLayout()
layout for vertices (dim-0 entities)
Definition: mcmgmapper.hh:105
Include standard header files.
Definition: agrid.hh:58
int max(const DofVectorPointer< int > &dofVector)
Definition: dofvector.hh:335
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:130
Mapper interface.
Definition: mapper.hh:108
Implementation class for a multiple codim and multiple geometry type mapper.
Definition: mcmgmapper.hh:127
const GridView & gridView() const
Definition: mcmgmapper.hh:334
IntegralRange< Index > indices(const typename GV::template Codim< 0 >::Entity &e, int i, int cc) const
Returns a pair with the starting point in the dof vector and the number of degrees of freedom if the ...
Definition: mcmgmapper.hh:248
bool contains(const EntityType &e, Index &result) const
Returns true if the entity is contained in the index set.
Definition: mcmgmapper.hh:270
size_type size() const
Return total number of entities in the entity set managed by the mapper.
Definition: mcmgmapper.hh:202
Index subIndex(const typename GV::template Codim< 0 >::Entity &e, int i, unsigned int codim) const
Map subentity of codim 0 entity to starting index in array for dof block.
Definition: mcmgmapper.hh:183
MultipleCodimMultipleGeomTypeMapper(const GV &gridView, const MCMGLayout &layout)
construct mapper from grid and layout description
Definition: mcmgmapper.hh:153
bool contains(const typename GV::template Codim< 0 >::Entity &e, int i, int cc, Index &result) const
Returns true if the entity is contained in the index set.
Definition: mcmgmapper.hh:289
decltype(std::declval< typename GV::IndexSet >().size(0)) size_type
Number type used for the overall size (the return value of the 'size' method)
Definition: mcmgmapper.hh:140
Index index(const EntityType &e) const
Map entity to starting index in array for dof block.
Definition: mcmgmapper.hh:169
void update(const GV &gridView)
Recalculates indices after grid adaptation.
Definition: mcmgmapper.hh:306
const std::vector< GeometryType > & types(int codim) const
return the geometry types with entries
Definition: mcmgmapper.hh:214
GV::IndexSet::IndexType Index
Number type used for indices.
Definition: mcmgmapper.hh:134
IntegralRange< Index > indices(const EntityType &e) const
Returns a pair with the starting point in the dof vector and the number of degrees of freedom if the ...
Definition: mcmgmapper.hh:229
size_type size(GeometryType gt) const
return number of entries for a given geometry type
Definition: mcmgmapper.hh:208
const MCMGLayout & layout() const
Definition: mcmgmapper.hh:333
void update()
Recalculates indices after grid adaptation.
Definition: mcmgmapper.hh:328
GV GridView
Underlying GridView.
Definition: mcmgmapper.hh:131
void update(GV &&gridView)
Recalculates indices after grid adaptation.
Definition: mcmgmapper.hh:318
Multiple codim and multiple geometry type mapper for leaf entities.
Definition: mcmgmapper.hh:402
void update()
Recalculates indices after grid adaptation.
Definition: mcmgmapper.hh:422
LeafMultipleCodimMultipleGeomTypeMapper(const G &grid, const MCMGLayout &layout)
constructor
Definition: mcmgmapper.hh:412
Multiple codim and multiple geometry type mapper for entities of one level.
Definition: mcmgmapper.hh:441
void update()
Recalculates indices after grid adaptation.
Definition: mcmgmapper.hh:463
LevelMultipleCodimMultipleGeomTypeMapper(const G &grid, int level, const MCMGLayout &layout)
constructor
Definition: mcmgmapper.hh:452