3#ifndef DUNE_GRID_COMMON_SIZECACHE_HH
4#define DUNE_GRID_COMMON_SIZECACHE_HH
12#include <dune/common/hybridutilities.hh>
15#include <dune/geometry/referenceelements.hh>
17#include <dune/grid/common/gridenums.hh>
29 template <
class Gr
idImp>
34 enum { dim = GridImp::dimension };
37 enum { nCodim = GridImp::dimension+1 };
40 typedef GridImp GridType;
43 typedef typename GridType :: ctype ctype ;
46 mutable std::vector< int > levelSizes_[nCodim];
49 mutable std::vector< std::vector< int > > levelTypeSizes_[nCodim];
52 mutable int leafSizes_[nCodim];
55 mutable std::vector< int > leafTypeSizes_[nCodim];
58 const GridType & grid_;
61 template <
int codim,
bool gr
idHasCodim >
62 struct CountLevelEntitiesBase
64 template <
class SzCacheType >
65 static void apply(
const SzCacheType & sc,
int level,
int cd)
69 sc.template countLevelEntities<All_Partition,codim> (level);
74 template <
int codim >
75 struct CountLevelEntitiesBase< codim, false >
77 template <
class SzCacheType >
78 static void apply(
const SzCacheType & sc,
int level,
int cd)
82 sc.template countLevelEntitiesNoCodim<All_Partition,codim> (level);
87 template <
int codim >
88 struct CountLevelEntities
89 :
public CountLevelEntitiesBase< codim, Capabilities :: hasEntity< GridType, codim > :: v >
93 template <
int codim,
bool gr
idHasCodim >
94 struct CountLeafEntitiesBase
96 template <
class SzCacheType>
97 static void apply(
const SzCacheType & sc,
int cd)
101 sc.template countLeafEntities<All_Partition,codim> ();
107 template <
int codim >
108 struct CountLeafEntitiesBase< codim, false >
110 template <
class SzCacheType>
111 static void apply(
const SzCacheType & sc,
int cd)
115 sc.template countLeafEntitiesNoCodim<All_Partition,codim> ();
120 template <
int codim >
121 struct CountLeafEntities
122 :
public CountLeafEntitiesBase< codim, Capabilities :: hasEntity< GridType, codim > :: v >
127 return type.
id() >> 1 ;
130 int sizeCodim(
const int codim )
const
132 const int mydim = GridType :: dimension - codim;
133 return ((1 << mydim) + 1) / 2;
148 for(
int codim=0; codim<nCodim; ++codim)
150 leafSizes_[ codim ] = -1;
151 leafTypeSizes_[ codim ].resize( sizeCodim( codim ), -1 );
154 const int numMxl = grid_.maxLevel()+1;
155 for(
int codim=0; codim<nCodim; ++codim)
157 std::vector<int> & vec = levelSizes_[codim];
159 levelTypeSizes_[codim].resize( numMxl );
160 for(
int level = 0; level<numMxl; ++level)
163 levelTypeSizes_[codim][level].resize( sizeCodim( codim ), -1 );
172 int size (
int level,
int codim)
const
174 assert( codim >= 0 );
175 assert( codim < nCodim );
176 assert( level >= 0 );
177 if( level >= (
int) levelSizes_[codim].
size() )
return 0;
179 if( levelSizes_[codim][level] < 0)
180 Hybrid::forEach( std::make_index_sequence< dim+1 >{}, [ & ](
auto i ){ CountLevelEntities< i >::apply( *
this, level, codim ); } );
184 assert( levelSizes_[codim][level] >= 0 );
185 return levelSizes_[codim][level];
191 int codim = GridType ::dimension - type.
dim();
192 if( levelSizes_[codim][level] < 0)
193 Hybrid::forEach( std::make_index_sequence< dim+1 >{}, [ & ](
auto i ){ CountLevelEntities< i >::apply( *
this, level, codim ); } );
195 assert( levelTypeSizes_[codim][level][gtIndex( type )] >= 0 );
196 return levelTypeSizes_[codim][level][gtIndex( type )];
205 assert( codim >= 0 );
206 assert( codim < nCodim );
207 if( leafSizes_[codim] < 0 )
208 Hybrid::forEach( std::make_index_sequence< dim+1 >{}, [ & ](
auto i ){ CountLeafEntities< i >::apply( *
this, codim ); } );
210 assert( leafSizes_[codim] >= 0 );
211 return leafSizes_[codim];
217 int codim = GridType :: dimension - type.
dim();
218 if( leafSizes_[codim] < 0 )
219 Hybrid::forEach( std::make_index_sequence< dim+1 >{}, [ & ](
auto i ){ CountLeafEntities< i >::apply( *
this, codim ); } );
221 assert( leafTypeSizes_[codim][ gtIndex( type )] >= 0 );
222 return leafTypeSizes_[codim][ gtIndex( type )];
226 template <PartitionIteratorType pitype,
int codim>
227 void countLevelEntities(
int level)
const
229 typedef typename GridType :: LevelGridView
GridView ;
230 typedef typename GridView :: template
Codim< codim > :: template Partition<pitype> :: Iterator Iterator ;
231 GridView gridView = grid_.levelGridView( level );
232 Iterator it = gridView.template begin<codim,pitype> ();
233 Iterator end = gridView.template end<codim,pitype> ();
234 levelSizes_[codim][level] = countElements(it,end, levelTypeSizes_[codim][level]);
237 template <PartitionIteratorType pitype,
int codim>
238 void countLeafEntities()
const
241 typedef typename GridType :: LeafGridView GridView ;
242 typedef typename GridView :: template Codim< codim > :: template Partition<pitype> :: Iterator Iterator ;
243 GridView gridView = grid_.leafGridView();
244 Iterator it = gridView.template begin<codim,pitype> ();
245 Iterator end = gridView.template end<codim,pitype> ();
246 leafSizes_[codim] = countElements(it,end, leafTypeSizes_[codim] );
250 template <
class IteratorType>
251 int countElements(IteratorType & it,
const IteratorType & end, std::vector<int>& typeSizes)
const
254 const size_t types = typeSizes.size();
255 for(
size_t i=0; i<types; ++i) typeSizes[i] = 0;
256 for( ; it != end; ++it )
259 ++typeSizes[ gtIndex( type ) ];
264 for(
size_t i=0; i<types; ++i) sumtypes += typeSizes[i];
266 assert( overall == sumtypes );
270 template <PartitionIteratorType pitype,
int codim>
271 void countLevelEntitiesNoCodim(
int level)
const
273 typedef typename GridType :: LevelGridView GridView ;
274 typedef typename GridView :: template
Codim< 0 > :: template Partition<pitype> :: Iterator Iterator ;
275 GridView gridView = grid_.levelGridView( level );
276 Iterator it = gridView.template begin< 0, pitype> ();
277 Iterator end = gridView.template end< 0, pitype> ();
278 levelSizes_[codim][level] = countElementsNoCodim< codim >(it,end, levelTypeSizes_[codim][level]);
281 template <PartitionIteratorType pitype,
int codim>
282 void countLeafEntitiesNoCodim()
const
285 typedef typename GridType :: LeafGridView GridView ;
286 typedef typename GridView :: template
Codim< 0 > :: template Partition<pitype> :: Iterator Iterator ;
287 GridView gridView = grid_.leafGridView();
288 Iterator it = gridView.template begin< 0, pitype > ();
289 Iterator end = gridView.template end< 0, pitype > ();
290 leafSizes_[codim] = countElementsNoCodim< codim >(it,end, leafTypeSizes_[codim] );
294 template <
int codim,
class IteratorType >
295 int countElementsNoCodim(IteratorType & it,
const IteratorType & end, std::vector<int>& typeSizes)
const
297 typedef typename GridType :: LocalIdSet LocalIdSet ;
298 typedef typename LocalIdSet :: IdType IdType ;
300 typedef ReferenceElements< ctype, dim > ReferenceElementContainerType;
303 typedef std::set< IdType > CodimIdSetType ;
305 typedef typename IteratorType :: Entity ElementType ;
308 const LocalIdSet& idSet = grid_.localIdSet();
310 const size_t types = typeSizes.size();
311 for(
size_t i=0; i<types; ++i) typeSizes[ i ] = 0;
313 std::vector< CodimIdSetType > typeCount( types );
316 for( ; it != end; ++it )
319 const ElementType& element = *it ;
321 ReferenceElementType refElem =
322 ReferenceElementContainerType :: general( element.type() );
325 const int count = element.subEntities( codim );
326 for(
int i=0; i< count; ++ i )
331 const IdType
id = idSet.subId( element, i, codim );
333 typeCount[ gtIndex( geomType ) ].insert(
id );
339 for(
size_t i=0; i<types; ++i)
341 typeSizes[ i ] = typeCount[ i ].size();
342 overall += typeSizes[ i ];
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:123
constexpr unsigned int dim() const
Return dimension of the type.
Definition: type.hh:369
constexpr unsigned int id() const
Return the topology id of the type.
Definition: type.hh:374
Grid view abstract base class.
Definition: gridview.hh:63
organizes the caching of sizes for one grid and one GeometryType
Definition: sizecache.hh:31
int size(int level, GeometryType type) const
Return number of entities per level and geometry type in this process.
Definition: sizecache.hh:189
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:172
int size(int codim) const
Return number of leaf entities of a given codim in this process.
Definition: sizecache.hh:203
SizeCache(const GridType &grid)
constructor taking grid reference
Definition: sizecache.hh:140
int size(const GeometryType type) const
Return number of leaf entities per geometry type in this process.
Definition: sizecache.hh:215
void reset()
reset all cached sizes
Definition: sizecache.hh:146
A set of traits classes to store static information about grid implementation.
A few common exception classes.
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:130
unspecified-type ReferenceElement
Returns the type of reference element for the argument types T...
Definition: referenceelements.hh:415
constexpr void forEach(Range &&range, F &&f)
Range based for loop.
Definition: hybridutilities.hh:266
Dune namespace.
Definition: alignedallocator.hh:11
Static tag representing a codimension.
Definition: dimension.hh:22
A unique label for each type of element that can occur in a grid.