5#ifndef DUNE_GRID_COMMON_SIZECACHE_HH
6#define DUNE_GRID_COMMON_SIZECACHE_HH
14#include <dune/common/hybridutilities.hh>
17#include <dune/geometry/referenceelements.hh>
19#include <dune/grid/common/gridenums.hh>
31 template <
class Gr
idImp>
36 constexpr static int dim = GridImp::dimension;
39 constexpr static int nCodim = GridImp::dimension + 1;
42 typedef GridImp GridType;
45 typedef typename GridType :: ctype ctype ;
48 mutable std::vector< int > levelSizes_[nCodim];
51 mutable std::vector< std::vector< int > > levelTypeSizes_[nCodim];
54 mutable int leafSizes_[nCodim];
57 mutable std::vector< int > leafTypeSizes_[nCodim];
60 const GridType & grid_;
63 template <
int codim,
bool gr
idHasCodim >
64 struct CountLevelEntitiesBase
66 template <
class SzCacheType >
67 static void apply(
const SzCacheType & sc,
int level,
int cd)
71 sc.template countLevelEntities<All_Partition,codim> (level);
76 template <
int codim >
77 struct CountLevelEntitiesBase< codim, false >
79 template <
class SzCacheType >
80 static void apply(
const SzCacheType & sc,
int level,
int cd)
84 sc.template countLevelEntitiesNoCodim<All_Partition,codim> (level);
89 template <
int codim >
90 struct CountLevelEntities
91 :
public CountLevelEntitiesBase< codim, Capabilities :: hasEntity< GridType, codim > :: v >
95 template <
int codim,
bool gr
idHasCodim >
96 struct CountLeafEntitiesBase
98 template <
class SzCacheType>
99 static void apply(
const SzCacheType & sc,
int cd)
103 sc.template countLeafEntities<All_Partition,codim> ();
109 template <
int codim >
110 struct CountLeafEntitiesBase< codim, false >
112 template <
class SzCacheType>
113 static void apply(
const SzCacheType & sc,
int cd)
117 sc.template countLeafEntitiesNoCodim<All_Partition,codim> ();
122 template <
int codim >
123 struct CountLeafEntities
124 :
public CountLeafEntitiesBase< codim, Capabilities :: hasEntity< GridType, codim > :: v >
129 return type.
id() >> 1 ;
132 int sizeCodim(
const int codim )
const
134 const int mydim = GridType :: dimension - codim;
135 return ((1 << mydim) + 1) / 2;
150 for(
int codim=0; codim<nCodim; ++codim)
152 leafSizes_[ codim ] = -1;
153 leafTypeSizes_[ codim ].resize( sizeCodim( codim ), -1 );
156 const int numMxl = grid_.maxLevel()+1;
157 for(
int codim=0; codim<nCodim; ++codim)
159 std::vector<int> & vec = levelSizes_[codim];
161 levelTypeSizes_[codim].resize( numMxl );
162 for(
int level = 0; level<numMxl; ++level)
165 levelTypeSizes_[codim][level].resize( sizeCodim( codim ), -1 );
174 int size (
int level,
int codim)
const
176 assert( codim >= 0 );
177 assert( codim < nCodim );
178 assert( level >= 0 );
179 if( level >= (
int) levelSizes_[codim].
size() )
return 0;
181 if( levelSizes_[codim][level] < 0)
182 Hybrid::forEach( std::make_index_sequence< dim+1 >{}, [ & ](
auto i ){ CountLevelEntities< i >::apply( *
this, level, codim ); } );
186 assert( levelSizes_[codim][level] >= 0 );
187 return levelSizes_[codim][level];
193 int codim = GridType ::dimension - type.
dim();
194 if( levelSizes_[codim][level] < 0)
195 Hybrid::forEach( std::make_index_sequence< dim+1 >{}, [ & ](
auto i ){ CountLevelEntities< i >::apply( *
this, level, codim ); } );
197 assert( levelTypeSizes_[codim][level][gtIndex( type )] >= 0 );
198 return levelTypeSizes_[codim][level][gtIndex( type )];
207 assert( codim >= 0 );
208 assert( codim < nCodim );
209 if( leafSizes_[codim] < 0 )
210 Hybrid::forEach( std::make_index_sequence< dim+1 >{}, [ & ](
auto i ){ CountLeafEntities< i >::apply( *
this, codim ); } );
212 assert( leafSizes_[codim] >= 0 );
213 return leafSizes_[codim];
219 int codim = GridType :: dimension - type.
dim();
220 if( leafSizes_[codim] < 0 )
221 Hybrid::forEach( std::make_index_sequence< dim+1 >{}, [ & ](
auto i ){ CountLeafEntities< i >::apply( *
this, codim ); } );
223 assert( leafTypeSizes_[codim][ gtIndex( type )] >= 0 );
224 return leafTypeSizes_[codim][ gtIndex( type )];
228 template <PartitionIteratorType pitype,
int codim>
229 void countLevelEntities(
int level)
const
231 typedef typename GridType :: LevelGridView
GridView ;
232 typedef typename GridView :: template
Codim< codim > :: template Partition<pitype> :: Iterator Iterator ;
233 GridView gridView = grid_.levelGridView( level );
234 Iterator it = gridView.template begin<codim,pitype> ();
235 Iterator end = gridView.template end<codim,pitype> ();
236 levelSizes_[codim][level] = countElements(it,end, levelTypeSizes_[codim][level]);
239 template <PartitionIteratorType pitype,
int codim>
240 void countLeafEntities()
const
243 typedef typename GridType :: LeafGridView GridView ;
244 typedef typename GridView :: template Codim< codim > :: template Partition<pitype> :: Iterator Iterator ;
245 GridView gridView = grid_.leafGridView();
246 Iterator it = gridView.template begin<codim,pitype> ();
247 Iterator end = gridView.template end<codim,pitype> ();
248 leafSizes_[codim] = countElements(it,end, leafTypeSizes_[codim] );
252 template <
class IteratorType>
253 int countElements(IteratorType & it,
const IteratorType & end, std::vector<int>& typeSizes)
const
256 const size_t types = typeSizes.size();
257 for(
size_t i=0; i<types; ++i) typeSizes[i] = 0;
258 for( ; it != end; ++it )
261 ++typeSizes[ gtIndex( type ) ];
267 for(
size_t i=0; i<types; ++i)
268 sumtypes += typeSizes[i];
270 assert( overall == sumtypes );
276 template <PartitionIteratorType pitype,
int codim>
277 void countLevelEntitiesNoCodim(
int level)
const
279 typedef typename GridType :: LevelGridView GridView ;
280 typedef typename GridView :: template
Codim< 0 > :: template Partition<pitype> :: Iterator Iterator ;
281 GridView gridView = grid_.levelGridView( level );
282 Iterator it = gridView.template begin< 0, pitype> ();
283 Iterator end = gridView.template end< 0, pitype> ();
284 levelSizes_[codim][level] = countElementsNoCodim< codim >(it,end, levelTypeSizes_[codim][level]);
287 template <PartitionIteratorType pitype,
int codim>
288 void countLeafEntitiesNoCodim()
const
291 typedef typename GridType :: LeafGridView GridView ;
292 typedef typename GridView :: template
Codim< 0 > :: template Partition<pitype> :: Iterator Iterator ;
293 GridView gridView = grid_.leafGridView();
294 Iterator it = gridView.template begin< 0, pitype > ();
295 Iterator end = gridView.template end< 0, pitype > ();
296 leafSizes_[codim] = countElementsNoCodim< codim >(it,end, leafTypeSizes_[codim] );
300 template <
int codim,
class IteratorType >
301 int countElementsNoCodim(IteratorType & it,
const IteratorType & end, std::vector<int>& typeSizes)
const
303 typedef typename GridType :: LocalIdSet LocalIdSet ;
304 typedef typename LocalIdSet :: IdType IdType ;
306 typedef ReferenceElements< ctype, dim > ReferenceElementContainerType;
307 typedef typename ReferenceElementContainerType::ReferenceElement ReferenceElementType;
309 typedef std::set< IdType > CodimIdSetType ;
311 typedef typename IteratorType :: Entity ElementType ;
314 const LocalIdSet& idSet = grid_.localIdSet();
316 const size_t types = typeSizes.size();
317 for(
size_t i=0; i<types; ++i) typeSizes[ i ] = 0;
319 std::vector< CodimIdSetType > typeCount( types );
322 for( ; it != end; ++it )
325 const ElementType& element = *it ;
327 ReferenceElementType refElem =
328 ReferenceElementContainerType :: general( element.type() );
331 const int count = element.subEntities( codim );
332 for(
int i=0; i< count; ++ i )
337 const IdType
id = idSet.subId( element, i, codim );
339 typeCount[ gtIndex( geomType ) ].insert(
id );
345 for(
size_t i=0; i<types; ++i)
347 typeSizes[ i ] = typeCount[ i ].size();
348 overall += typeSizes[ i ];
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:114
constexpr unsigned int dim() const
Return dimension of the type.
Definition: type.hh:360
constexpr unsigned int id() const
Return the topology id of the type.
Definition: type.hh:365
Grid view abstract base class.
Definition: gridview.hh:66
organizes the caching of sizes for one grid and one GeometryType
Definition: sizecache.hh:33
int size(int level, GeometryType type) const
Return number of entities per level and geometry type in this process.
Definition: sizecache.hh:191
int size(int level, int codim) const
Return number of grid entities of a given codim on a given level in this process.
Definition: sizecache.hh:174
int size(int codim) const
Return number of leaf entities of a given codim in this process.
Definition: sizecache.hh:205
SizeCache(const GridType &grid)
constructor taking grid reference
Definition: sizecache.hh:142
int size(const GeometryType type) const
Return number of leaf entities per geometry type in this process.
Definition: sizecache.hh:217
void reset()
reset all cached sizes
Definition: sizecache.hh:148
A few common exception classes.
A set of traits classes to store static information about grid implementation.
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:132
constexpr void forEach(Range &&range, F &&f)
Range based for loop.
Definition: hybridutilities.hh:256
Dune namespace.
Definition: alignedallocator.hh:13
Static tag representing a codimension.
Definition: dimension.hh:24
A unique label for each type of element that can occur in a grid.