3#ifndef DUNE_MMESH_GRID_INDEXSETS_HH
4#define DUNE_MMESH_GRID_INDEXSETS_HH
11#include <dune/grid/common/indexidset.hh>
18 template<
class Gr
idImp>
19 class MMeshLeafIndexSet :
20 public IndexSet<GridImp, MMeshLeafIndexSet<GridImp>>
22 typedef typename std::remove_const<GridImp>::type::HostGridType HostGrid;
25 using HostGridEntity =
typename GridImp::template HostGridEntity<codim>;
28 typedef std::size_t IndexType;
29 typedef const std::vector< GeometryType > Types;
31 typedef std::unordered_map< MMeshImpl::MultiId, std::size_t > CodimIndexMap;
37 enum {dim = std::remove_const<GridImp>::type::dimension};
40 MMeshLeafIndexSet (
const GridImp* grid)
44 MMeshLeafIndexSet (
const MMeshLeafIndexSet& leafIndexSet)
45 : grid_(leafIndexSet.grid_), sizeOfCodim_(leafIndexSet.sizeOfCodim_)
50 std::enable_if_t< codim == 0 || codim == dim, IndexType >
51 index (
const Entity< codim, dim, GridImp, MMeshEntity>& e)
const
53 auto hostEntity = e.impl().hostEntity();
54 IndexType index = hostEntity->info().index;
55 assert( index <= size(codim) );
61 std::enable_if_t< codim == 1, IndexType >
62 index (
const Entity< codim, dim, GridImp, MMeshEntity>& e)
const
65 return codimIndexMap_[0].at( grid_->globalIdSet().id( e ) );
67 DUNE_THROW(InvalidStateException,
"Id of codim 1 entity not found.");
73 std::enable_if_t< codim == 2 && dim == 3, IndexType >
74 index (
const Entity< codim, dim, GridImp, MMeshEntity>& e)
const
77 return codimIndexMap_[1].at( grid_->globalIdSet().id( e ) );
79 DUNE_THROW(InvalidStateException,
"Id of codim 2 entity not found.");
84 template<
class Entity>
85 IndexType subIndex (
const Entity& e,
int i,
int codim)
87 return subIndex< Entity::codimension >( e, i, codim );
92 std::enable_if_t< cc == dim, IndexType >
93 subIndex (
const typename std::remove_const<GridImp>::type::Traits::template Codim<cc>::Entity& e,
int i,
int codim)
const
95 const HostGridEntity<dim> hostEntity = e.impl().hostEntity();
96 return hostEntity->info().index;
101 std::enable_if_t< cc == 0, IndexType > subIndex (
const typename std::remove_const<GridImp>::type::Traits::template Codim<cc>::Entity& e,
int i,
int codim)
const
103 assert ( codim >= 0 && codim <= dim );
108 return e.impl().template subEntity<dim>( i ).impl().hostEntity()->info().index;
109 else if ( codim == 1 )
110 return codimIndexMap_[0].at( grid_->globalIdSet().id( e.impl().template subEntity<1>( i ) ) );
111 else if ( codim == 2 )
112 return codimIndexMap_[1].at( grid_->globalIdSet().id( e.impl().template subEntity<2>( i ) ) );
114 DUNE_THROW( InvalidStateException,
"subIndex() was called for codim " << codim );
121 std::enable_if_t< cc != 0 && cc != dim, IndexType > subIndex (
const typename std::remove_const<GridImp>::type::Traits::template Codim<cc>::Entity& e,
int i,
int codim)
const
123 DUNE_THROW( NotImplemented,
"SubIndices for codim != 0 || codim != dim." );
127 std::size_t size (GeometryType type)
const
129 if( type == GeometryTypes::vertex )
131 else if( type == GeometryTypes::line )
133 else if( type == GeometryTypes::triangle )
135 else if( type == GeometryTypes::tetrahedron )
142 std::size_t size (
int codim)
const
144 assert( (0 <= codim) && (codim <= dim) );
145 return sizeOfCodim_[codim];
149 const Types geomTypes (
int codim)
const
151 return types( codim );
155 Types types (
int codim)
const
157 switch ( dim - codim ) {
159 return {{ GeometryTypes::vertex }};
161 return {{ GeometryTypes::line }};
163 return {{ GeometryTypes::triangle }};
165 return {{ GeometryTypes::tetrahedron }};
167 DUNE_THROW(InvalidStateException,
"Codim is not within 0 <= codim <= dim.");
172 template<
class EntityType,
int d = dim >
173 std::enable_if_t< d == 2 && EntityType::codimension == 0, bool >
174 contains (
const EntityType& e)
const
176 const auto hostEntity = e.impl().hostEntity();
177 return grid_->getHostGrid().is_face( hostEntity->vertex(0), hostEntity->vertex(1), hostEntity->vertex(2) );
180 template<
class EntityType,
int d = dim >
181 std::enable_if_t< d == 2 && EntityType::codimension == 1, bool >
182 contains (
const EntityType& e)
const
184 const auto hostEntity = e.impl().hostEntity();
185 return grid_->getHostGrid().is_edge(
186 hostEntity.first->vertex((hostEntity.second+1)%3),
187 hostEntity.first->vertex((hostEntity.second+2)%3)
191 template<
class EntityType,
int d = dim >
192 std::enable_if_t< d == 2 && EntityType::codimension == 2, bool >
193 contains (
const EntityType& e)
const
195 const auto hostEntity = e.impl().hostEntity();
196 return grid_->getHostGrid().tds().is_vertex( hostEntity );
200 template<
class EntityType,
int d = dim >
201 std::enable_if_t< d == 3 && EntityType::codimension == 0, bool >
202 contains (
const EntityType& e)
const
204 const auto hostEntity = e.impl().hostEntity();
205 return grid_->getHostGrid().is_cell( hostEntity );
209 template<
class EntityType,
int d = dim >
210 std::enable_if_t< d == 3 && EntityType::codimension == 1, bool >
211 contains (
const EntityType& e)
const
213 const auto hostEntity = e.impl().hostEntity();
214 return grid_->getHostGrid().tds().is_facet(
221 template<
class EntityType,
int d = dim >
222 std::enable_if_t< d == 3 && EntityType::codimension == 2, bool >
223 contains (
const EntityType& e)
const
225 const auto hostEntity = e.impl().hostEntity();
226 return grid_->getHostGrid().tds().is_edge(
234 template<
class EntityType,
int d = dim >
235 std::enable_if_t< d == 3 && EntityType::codimension == 3, bool >
236 contains (
const EntityType& e)
const
238 const auto hostEntity = e.impl().hostEntity();
239 return grid_->getHostGrid().tds().is_vertex( hostEntity );
243 template<
int d = dim >
244 std::enable_if_t< d == 2, void >
245 update(
const GridImp* grid)
248 const auto& hostgrid = grid_->getHostGrid();
251 std::size_t elementCount = 0;
252 for (
const auto& element : elements(grid_->leafGridView(), Partitions::all))
253 element.impl().hostEntity()->info().index = elementCount++;
256 std::size_t vertexCount = 0;
257 for (
const auto& vertex : vertices(grid_->leafGridView(), Partitions::all))
258 vertex.impl().hostEntity()->info().index = vertexCount++;
261 codimIndexMap_[0].clear();
262 std::size_t edgeCount = 0;
263 for (
const auto& edge : edges(grid_->leafGridView(), Partitions::all))
264 codimIndexMap_[0][ grid_->globalIdSet().id( edge ) ] = edgeCount++;
267 sizeOfCodim_[0] = elementCount;
268 sizeOfCodim_[1] = edgeCount;
269 sizeOfCodim_[2] = vertexCount;
273 template<
int d = dim >
274 std::enable_if_t< d == 3, void >
275 update(
const GridImp* grid)
278 const auto& hostgrid = grid_->getHostGrid();
281 std::size_t elementCount = 0;
282 for (
const auto& element : elements(grid_->leafGridView(), Partitions::all))
283 element.impl().hostEntity()->info().index = elementCount++;
286 std::size_t vertexCount = 0;
287 for (
const auto& vertex : vertices(grid_->leafGridView(), Partitions::all))
288 vertex.impl().hostEntity()->info().index = vertexCount++;
291 codimIndexMap_[0].clear();
292 std::size_t facetCount = 0;
293 for (
const auto& facet : facets(grid_->leafGridView(), Partitions::all))
294 codimIndexMap_[0][ grid_->globalIdSet().id( facet ) ] = facetCount++;
297 codimIndexMap_[1].clear();
298 std::size_t edgeCount = 0;
299 for (
const auto& edge : edges(grid_->leafGridView(), Partitions::all))
300 codimIndexMap_[1][ grid_->globalIdSet().id( edge ) ] = edgeCount++;
303 sizeOfCodim_[0] = elementCount;
304 sizeOfCodim_[1] = facetCount;
305 sizeOfCodim_[2] = edgeCount;
306 sizeOfCodim_[3] = vertexCount;
310 std::array<std::size_t, dim+1> sizeOfCodim_;
311 std::array<CodimIndexMap, dim-1> codimIndexMap_;
315 template <
class Gr
idImp>
316 class MMeshGlobalIdSet :
317 public IdSet<GridImp, MMeshGlobalIdSet<GridImp>, MMeshImpl::MultiId>
319 typedef typename std::remove_const<GridImp>::type::HostGridType HostGrid;
325 enum {dim = std::remove_const<GridImp>::type::dimension};
328 using HostGridEntity =
typename GridImp::template HostGridEntity<codim>;
332 using IdType = MMeshImpl::MultiId;
335 MMeshGlobalIdSet (
const GridImp* g) : grid_(g), nextVertexId_(0)
343 const auto& hostgrid = grid_->getHostGrid();
346 for (
auto vh = hostgrid.finite_vertices_begin(); vh != hostgrid.finite_vertices_end(); ++vh)
347 if( vh->info().idWasSet && vh->info().id >= nextVertexId_ )
348 nextVertexId_ = vh->info().id+1;
357 std::enable_if_t< cd == dim, IdType >
358 id (
const typename std::remove_const<GridImp>::type::Traits::template Codim<cd>::Entity& e)
const
360 return IdType( e.impl().hostEntity()->info().id );
364 std::enable_if_t< cd != dim, IdType >
365 id (
const typename std::remove_const<GridImp>::type::Traits::template Codim<cd>::Entity& e)
const
367 return IdType( e.impl().id() );
371 template<
int d = dim >
372 std::enable_if_t< d == 2, IdType >
373 subId (
const typename std::remove_const<GridImp>::type::Traits::template Codim<0>::Entity& e,
int i,
int codim)
const
375 assert( 0 <= codim && codim <= dim );
376 IdType dummyId ( { std::size_t(-4), std::size_t(-3), std::size_t(-2) } );
380 return e.impl().id();
382 if (e.impl().id() != dummyId )
384 auto id0 = e.impl().id().vt()[i < 2 ? 0 : 1];
385 auto id1 = e.impl().id().vt()[i == 0 ? 1 : 2];
386 return IdType( { std::min(id0, id1), std::max(id0, id1) } );
390 std::size_t id0 = (i < 2 ? -4 : -3);
391 std::size_t id1 = (i == 0 ? -3 : -2);
392 return IdType( { id0, id1 } );
395 if (e.impl().id() != dummyId )
396 return e.impl().id().vt()[i];
398 return IdType( std::size_t(-4 + i) );
403 template<
int d = dim >
404 std::enable_if_t< d == 3, IdType >
405 subId (
const typename std::remove_const<GridImp>::type::Traits::template Codim<0>::Entity& e,
int i,
int codim)
const
407 assert( 0 <= codim && codim <= dim );
410 case 0:
return id<0>( e.impl().template subEntity<0>( i ) );
411 case 1:
return id<1>( e.impl().template subEntity<1>( i ) );
412 case 2:
return id<2>( e.impl().template subEntity<2>( i ) );
413 case 3:
return id<3>( e.impl().template subEntity<3>( i ) );
419 template<
int d = dim >
420 std::enable_if_t< d == 2, void >
421 update(
const GridImp* grid)
424 const auto& hostgrid = grid_->getHostGrid();
427 for (
auto vh = hostgrid.finite_vertices_begin(); vh != hostgrid.finite_vertices_end(); ++vh)
428 if( !vh->info().idWasSet )
430 vh->info().id = nextVertexId_++;
431 vh->info().idWasSet =
true;
435 for (
auto fh = hostgrid.all_faces_begin(); fh != hostgrid.all_faces_end(); ++fh)
436 fh->info().cgalIndex = MMeshImpl::computeCGALIndices<decltype(fh), 2>( fh );
440 template<
int d = dim >
441 std::enable_if_t< d == 3, void >
442 update(
const GridImp* grid)
445 const auto& hostgrid = grid_->getHostGrid();
448 for (
auto vh = hostgrid.finite_vertices_begin(); vh != hostgrid.finite_vertices_end(); ++vh)
449 if( !vh->info().idWasSet )
451 vh->info().id = nextVertexId_++;
452 vh->info().idWasSet =
true;
456 for (
auto ch = hostgrid.all_cells_begin(); ch != hostgrid.all_cells_end(); ++ch)
457 ch->info().cgalIndex = MMeshImpl::computeCGALIndices<decltype(ch), 3>( ch );
461 std::size_t setNextId( HostGridEntity<dim> vh )
const
463 assert( !vh->info().idWasSet );
464 vh->info().id = nextVertexId_++;
465 vh->info().idWasSet =
true;
466 return vh->info().id;
470 mutable std::size_t nextVertexId_;