3#ifndef DUNE_MMESH_GRID_INDEXSETS_HH
4#define DUNE_MMESH_GRID_INDEXSETS_HH
11#include <dune/grid/common/indexidset.hh>
16template <
class Gr
idImp>
17class MMeshLeafIndexSet :
public IndexSet<GridImp, MMeshLeafIndexSet<GridImp>> {
18 typedef typename std::remove_const<GridImp>::type::HostGridType HostGrid;
21 using HostGridEntity =
typename GridImp::template HostGridEntity<codim>;
24 typedef std::size_t IndexType;
25 typedef const std::vector<GeometryType> Types;
27 typedef std::unordered_map<MMeshImpl::MultiId, std::size_t> CodimIndexMap;
33 enum { dim = std::remove_const<GridImp>::type::dimension };
36 MMeshLeafIndexSet(
const GridImp* grid) : grid_(grid) {}
38 MMeshLeafIndexSet(
const MMeshLeafIndexSet& leafIndexSet)
39 : grid_(leafIndexSet.grid_), sizeOfCodim_(leafIndexSet.sizeOfCodim_) {}
43 std::enable_if_t<codim == 0 || codim == dim, IndexType> index(
44 const Entity<codim, dim, GridImp, MMeshEntity>& e)
const {
45 auto hostEntity = e.impl().hostEntity();
46 IndexType index = hostEntity->info().index;
47 assert(index <= size(codim));
53 std::enable_if_t<codim == 1, IndexType> index(
54 const Entity<codim, dim, GridImp, MMeshEntity>& e)
const {
56 return codimIndexMap_[0].at(grid_->globalIdSet().id(e));
58 DUNE_THROW(InvalidStateException,
"Id of codim 1 entity not found.");
64 std::enable_if_t<codim == 2 && dim == 3, IndexType> index(
65 const Entity<codim, dim, GridImp, MMeshEntity>& e)
const {
67 return codimIndexMap_[1].at(grid_->globalIdSet().id(e));
69 DUNE_THROW(InvalidStateException,
"Id of codim 2 entity not found.");
74 template <
class Entity>
75 IndexType subIndex(
const Entity& e,
int i,
int codim) {
76 return subIndex<Entity::codimension>(e, i, codim);
81 std::enable_if_t<cc == dim, IndexType> subIndex(
82 const typename std::remove_const<GridImp>::type::Traits::template Codim<
84 int i,
int codim)
const {
85 const HostGridEntity<dim> hostEntity = e.impl().hostEntity();
86 return hostEntity->info().index;
91 std::enable_if_t<cc == 0, IndexType> subIndex(
92 const typename std::remove_const<GridImp>::type::Traits::template Codim<
94 int i,
int codim)
const {
95 assert(codim >= 0 && codim <= dim);
97 if (codim == 0)
return index(e);
100 .template subEntity<dim>(i)
106 return codimIndexMap_[0].at(
107 grid_->globalIdSet().id(e.impl().template subEntity<1>(i)));
109 return codimIndexMap_[1].at(
110 grid_->globalIdSet().id(e.impl().template subEntity<2>(i)));
112 DUNE_THROW(InvalidStateException,
113 "subIndex() was called for codim " << codim);
120 std::enable_if_t<cc != 0 && cc != dim, IndexType> subIndex(
121 const typename std::remove_const<GridImp>::type::Traits::template Codim<
123 int i,
int codim)
const {
124 DUNE_THROW(NotImplemented,
"SubIndices for codim != 0 || codim != dim.");
128 std::size_t size(GeometryType type)
const {
129 if (type == GeometryTypes::vertex)
131 else if (type == GeometryTypes::line)
132 return size(dim - 1);
133 else if (type == GeometryTypes::triangle)
134 return size(dim - 2);
135 else if (type == GeometryTypes::tetrahedron)
136 return size(dim - 3);
142 std::size_t size(
int codim)
const {
143 assert((0 <= codim) && (codim <= dim));
144 return sizeOfCodim_[codim];
148 const Types geomTypes(
int codim)
const {
return types(codim); }
151 Types types(
int codim)
const {
152 switch (dim - codim) {
154 return {{GeometryTypes::vertex}};
156 return {{GeometryTypes::line}};
158 return {{GeometryTypes::triangle}};
160 return {{GeometryTypes::tetrahedron}};
162 DUNE_THROW(InvalidStateException,
163 "Codim is not within 0 <= codim <= dim.");
169 template <
class EntityType,
int d = dim>
170 std::enable_if_t<d == 2 && EntityType::codimension == 0, bool> contains(
171 const EntityType& e)
const {
172 const auto hostEntity = e.impl().hostEntity();
173 return grid_->getHostGrid().is_face(
174 hostEntity->vertex(0), hostEntity->vertex(1), hostEntity->vertex(2));
177 template <
class EntityType,
int d = dim>
178 std::enable_if_t<d == 2 && EntityType::codimension == 1, bool> contains(
179 const EntityType& e)
const {
180 const auto hostEntity = e.impl().hostEntity();
181 return grid_->getHostGrid().is_edge(
182 hostEntity.first->vertex((hostEntity.second + 1) % 3),
183 hostEntity.first->vertex((hostEntity.second + 2) % 3));
186 template <
class EntityType,
int d = dim>
187 std::enable_if_t<d == 2 && EntityType::codimension == 2, bool> contains(
188 const EntityType& e)
const {
189 const auto hostEntity = e.impl().hostEntity();
190 return grid_->getHostGrid().tds().is_vertex(hostEntity);
195 template <
class EntityType,
int d = dim>
196 std::enable_if_t<d == 3 && EntityType::codimension == 0, bool> contains(
197 const EntityType& e)
const {
198 const auto hostEntity = e.impl().hostEntity();
199 return grid_->getHostGrid().is_cell(hostEntity);
204 template <
class EntityType,
int d = dim>
205 std::enable_if_t<d == 3 && EntityType::codimension == 1, bool> contains(
206 const EntityType& e)
const {
207 const auto hostEntity = e.impl().hostEntity();
208 return grid_->getHostGrid().tds().is_facet(hostEntity.first,
214 template <
class EntityType,
int d = dim>
215 std::enable_if_t<d == 3 && EntityType::codimension == 2, bool> contains(
216 const EntityType& e)
const {
217 const auto hostEntity = e.impl().hostEntity();
218 return grid_->getHostGrid().tds().is_edge(
219 hostEntity.first, hostEntity.second, hostEntity.third);
224 template <
class EntityType,
int d = dim>
225 std::enable_if_t<d == 3 && EntityType::codimension == 3, bool> contains(
226 const EntityType& e)
const {
227 const auto hostEntity = e.impl().hostEntity();
228 return grid_->getHostGrid().tds().is_vertex(hostEntity);
232 template <
int d = dim>
233 std::enable_if_t<d == 2, void> update(
const GridImp* grid) {
235 const auto& hostgrid = grid_->getHostGrid();
238 std::size_t elementCount = 0;
239 for (
const auto& element : elements(grid_->leafGridView(), Partitions::all))
240 element.impl().hostEntity()->info().index = elementCount++;
243 std::size_t vertexCount = 0;
244 for (
const auto& vertex : vertices(grid_->leafGridView(), Partitions::all))
245 vertex.impl().hostEntity()->info().index = vertexCount++;
248 codimIndexMap_[0].clear();
249 std::size_t edgeCount = 0;
250 for (
const auto& edge : edges(grid_->leafGridView(), Partitions::all))
251 codimIndexMap_[0][grid_->globalIdSet().id(edge)] = edgeCount++;
254 sizeOfCodim_[0] = elementCount;
255 sizeOfCodim_[1] = edgeCount;
256 sizeOfCodim_[2] = vertexCount;
260 template <
int d = dim>
261 std::enable_if_t<d == 3, void> update(
const GridImp* grid) {
263 const auto& hostgrid = grid_->getHostGrid();
266 std::size_t elementCount = 0;
267 for (
const auto& element : elements(grid_->leafGridView(), Partitions::all))
268 element.impl().hostEntity()->info().index = elementCount++;
271 std::size_t vertexCount = 0;
272 for (
const auto& vertex : vertices(grid_->leafGridView(), Partitions::all))
273 vertex.impl().hostEntity()->info().index = vertexCount++;
276 codimIndexMap_[0].clear();
277 std::size_t facetCount = 0;
278 for (
const auto& facet : facets(grid_->leafGridView(), Partitions::all))
279 codimIndexMap_[0][grid_->globalIdSet().id(facet)] = facetCount++;
282 codimIndexMap_[1].clear();
283 std::size_t edgeCount = 0;
284 for (
const auto& edge : edges(grid_->leafGridView(), Partitions::all))
285 codimIndexMap_[1][grid_->globalIdSet().id(edge)] = edgeCount++;
288 sizeOfCodim_[0] = elementCount;
289 sizeOfCodim_[1] = facetCount;
290 sizeOfCodim_[2] = edgeCount;
291 sizeOfCodim_[3] = vertexCount;
295 std::array<std::size_t, dim + 1> sizeOfCodim_;
296 std::array<CodimIndexMap, dim - 1> codimIndexMap_;
299template <
class Gr
idImp>
300class MMeshGlobalIdSet
301 :
public IdSet<GridImp, MMeshGlobalIdSet<GridImp>, MMeshImpl::MultiId> {
302 typedef typename std::remove_const<GridImp>::type::HostGridType HostGrid;
308 enum { dim = std::remove_const<GridImp>::type::dimension };
311 using HostGridEntity =
typename GridImp::template HostGridEntity<codim>;
315 using IdType = MMeshImpl::MultiId;
318 MMeshGlobalIdSet(
const GridImp* g) : grid_(g), nextVertexId_(0) { init(); }
322 const auto& hostgrid = grid_->getHostGrid();
325 for (
auto vh = hostgrid.finite_vertices_begin();
326 vh != hostgrid.finite_vertices_end(); ++vh)
327 if (vh->info().idWasSet && vh->info().id >= nextVertexId_)
328 nextVertexId_ = vh->info().id + 1;
337 std::enable_if_t<cd == dim, IdType> id(
338 const typename std::remove_const<GridImp>::type::Traits::template Codim<
339 cd>::Entity& e)
const {
340 return IdType(e.impl().hostEntity()->info().id);
344 std::enable_if_t<cd != dim, IdType> id(
345 const typename std::remove_const<GridImp>::type::Traits::template Codim<
346 cd>::Entity& e)
const {
347 return IdType(e.impl().id());
351 template <
int d = dim>
352 std::enable_if_t<d == 2, IdType> subId(
353 const typename std::remove_const<GridImp>::type::Traits::template Codim<
355 int i,
int codim)
const {
356 assert(0 <= codim && codim <= dim);
357 IdType dummyId({std::size_t(-4), std::size_t(-3), std::size_t(-2)});
360 return e.impl().id();
362 if (e.impl().id() != dummyId) {
363 auto id0 = e.impl().id().vt()[i < 2 ? 0 : 1];
364 auto id1 = e.impl().id().vt()[i == 0 ? 1 : 2];
365 return IdType({std::min(id0, id1), std::max(id0, id1)});
367 std::size_t id0 = (i < 2 ? -4 : -3);
368 std::size_t id1 = (i == 0 ? -3 : -2);
369 return IdType({id0, id1});
372 if (e.impl().id() != dummyId)
373 return e.impl().id().vt()[i];
375 return IdType(std::size_t(-4 + i));
380 template <
int d = dim>
381 std::enable_if_t<d == 3, IdType> subId(
382 const typename std::remove_const<GridImp>::type::Traits::template Codim<
384 int i,
int codim)
const {
385 assert(0 <= codim && codim <= dim);
388 return id<0>(e.impl().template subEntity<0>(i));
390 return id<1>(e.impl().template subEntity<1>(i));
392 return id<2>(e.impl().template subEntity<2>(i));
394 return id<3>(e.impl().template subEntity<3>(i));
400 template <
int d = dim>
401 std::enable_if_t<d == 2, void> update(
const GridImp* grid) {
403 const auto& hostgrid = grid_->getHostGrid();
406 for (
auto vh = hostgrid.finite_vertices_begin();
407 vh != hostgrid.finite_vertices_end(); ++vh)
408 if (!vh->info().idWasSet) {
409 vh->info().id = nextVertexId_++;
410 vh->info().idWasSet =
true;
414 for (
auto fh = hostgrid.all_faces_begin(); fh != hostgrid.all_faces_end();
416 fh->info().cgalIndex = MMeshImpl::computeCGALIndices<decltype(fh), 2>(fh);
420 template <
int d = dim>
421 std::enable_if_t<d == 3, void> update(
const GridImp* grid) {
423 const auto& hostgrid = grid_->getHostGrid();
426 for (
auto vh = hostgrid.finite_vertices_begin();
427 vh != hostgrid.finite_vertices_end(); ++vh)
428 if (!vh->info().idWasSet) {
429 vh->info().id = nextVertexId_++;
430 vh->info().idWasSet =
true;
434 for (
auto ch = hostgrid.all_cells_begin(); ch != hostgrid.all_cells_end();
436 ch->info().cgalIndex = MMeshImpl::computeCGALIndices<decltype(ch), 3>(ch);
440 std::size_t setNextId(HostGridEntity<dim> vh)
const {
441 assert(!vh->info().idWasSet);
442 vh->info().id = nextVertexId_++;
443 vh->info().idWasSet =
true;
444 return vh->info().id;
448 mutable std::size_t nextVertexId_;