1#ifndef DUNE_MULTIDOMAINGRID_SUBDOMAINGRID_HH
2#define DUNE_MULTIDOMAINGRID_SUBDOMAINGRID_HH
7#include <dune/common/exceptions.hh>
9#include <dune/grid/multidomaingrid/hostgridaccessor.hh>
10#include <dune/grid/multidomaingrid/subdomainset.hh>
12#include <dune/grid/multidomaingrid/subdomaingrid/geometry.hh>
13#include <dune/grid/multidomaingrid/subdomaingrid/localgeometry.hh>
14#include <dune/grid/multidomaingrid/subdomaingrid/entity.hh>
15#include <dune/grid/multidomaingrid/subdomaingrid/iterator.hh>
16#include <dune/grid/multidomaingrid/subdomaingrid/hierarchiciterator.hh>
17#include <dune/grid/multidomaingrid/subdomaingrid/intersection.hh>
18#include <dune/grid/multidomaingrid/subdomaingrid/intersectioniterator.hh>
19#include <dune/grid/multidomaingrid/subdomaingrid/idsets.hh>
20#include <dune/grid/multidomaingrid/subdomaingrid/indexsets.hh>
21#include <dune/grid/multidomaingrid/subdomaingrid/gridview.hh>
30template<
typename,
typename>
35template<
typename MDGr
id>
38template<
typename MDGr
id>
39struct SubDomainGridFamily {
43 static const int dim = MDGrid::dimension;
44 static const int dimw = MDGrid::dimensionworld;
51 using Grid = SubDomainGrid<MDGrid>;
54 using LeafIntersection = Dune::Intersection<
60 typename MDGrid::LeafIndexSetImp
62 typename MDGrid::LeafGridView::Intersection
66 using LevelIntersection = Dune::Intersection<
72 typename MDGrid::LevelIndexSetImp
74 typename MDGrid::LevelGridView::Intersection
78 using LeafIntersectionIterator = Dune::IntersectionIterator<
80 IntersectionIteratorWrapper<
84 typename MDGrid::LeafIndexSetImp
86 typename MDGrid::LeafGridView::IntersectionIterator
92 typename MDGrid::LeafIndexSetImp
94 typename MDGrid::LeafGridView::Intersection
98 using LevelIntersectionIterator = Dune::IntersectionIterator<
100 IntersectionIteratorWrapper<
104 typename MDGrid::LevelIndexSetImp
106 typename MDGrid::LevelGridView::IntersectionIterator
112 typename MDGrid::LevelIndexSetImp
114 typename MDGrid::LevelGridView::Intersection
118 using HierarchicIterator = Dune::EntityIterator< 0, const Grid, HierarchicIteratorWrapper< const Grid > >;
120 using LevelGridView = Dune::GridView<LevelGridViewTraits<const Grid>>;
121 using LeafGridView = Dune::GridView<LeafGridViewTraits<const Grid>>;
127 using Geometry = Dune::Geometry<dim-cd, dimw,
const Grid, GeometryWrapper>;
128 using LocalGeometry = Dune::Geometry<dim-cd, dim,
const Grid, LocalGeometryWrapper>;
130 using Entity = Dune::Entity<cd, dim, const Grid, EntityWrapper>;
132 using EntitySeed = EntitySeedWrapper<typename MDGrid::HostGrid::template Codim<cd>::EntitySeed>;
134 template <PartitionIteratorType pitype>
138 using LevelIterator = Dune::EntityIterator<
142 typename Traits::LevelGridView,
143 typename MDGrid::LevelGridView::template Codim<cd>::template Partition<pitype>::Iterator,
150 using LeafIterator = Dune::EntityIterator<
154 typename Traits::LeafGridView,
155 typename MDGrid::LeafGridView::template Codim<cd>::template Partition<pitype>::Iterator,
163 using LeafIterator =
typename Partition< All_Partition >::LeafIterator;
164 using LevelIterator =
typename Partition< All_Partition >::LevelIterator;
167 friend class Dune::Entity<cd, dim, const Grid, EntityWrapper>;
171 using LevelIndexSet = IndexSetWrapper<const Grid, typename MDGrid::LevelIndexSetImp>;
172 using LeafIndexSet = IndexSetWrapper<const Grid, typename MDGrid::LeafIndexSetImp>;
174 using GlobalIdSet = IdSet<
178 typename MDGrid::HostGrid::Traits::GlobalIdSet
180 typename MDGrid::HostGrid::Traits::GlobalIdSet::IdType
183 using LocalIdSet = IdSet<
187 typename MDGrid::HostGrid::Traits::LocalIdSet
189 typename MDGrid::HostGrid::Traits::LocalIdSet::IdType
192 using CollectiveCommunication
193 [[deprecated(
"Use Communication. Will be removed after release 2.9")]]
194 =
typename MDGrid::CollectiveCommunication;
196 using Communication =
typename MDGrid::Communication;
202template<
typename MDGr
id>
204 public GridDefaultImplementation<MDGrid::dimension,
205 MDGrid::dimensionworld,
206 typename MDGrid::ctype,
207 SubDomainGridFamily<MDGrid> > {
209 template<
typename,
typename>
210 friend class ::Dune::mdgrid::MultiDomainGrid;
212 template<
int codim,
int dim,
typename Gr
idImp>
213 friend class EntityWrapperBase;
215 template<
int codim,
int dim,
typename Gr
idImp>
216 friend class EntityWrapper;
218 template<
typename,
typename,
int codim, PartitionIteratorType pitype,
typename Gr
idImp>
219 friend class IteratorWrapper;
221 template<
typename Gr
idImp>
222 friend class HierarchicIteratorWrapper;
224 template<
int mydim,
int coorddim,
typename Gr
idImp>
225 friend class GeometryWrapper;
227 template<
int mydim,
int coorddim,
typename Gr
idImp>
228 friend class LocalGeometryWrapper;
230 template<
typename Gr
idImp,
typename WrappedIndexSet>
231 friend class IndexSetWrapper;
233 template<
typename Gr
idImp,
typename WrappedIdSet>
234 friend class IdSetWrapper;
236 template<
typename Gr
idImp>
237 friend struct ::Dune::mdgrid::detail::HostGridAccessor;
239 template<
typename,
typename,
typename>
240 friend class IntersectionIteratorWrapper;
242 template<
typename,
typename,
typename>
243 friend class IntersectionWrapper;
246 friend class LevelGridView;
249 friend class LeafGridView;
251 typedef GridDefaultImplementation<MDGrid::dimension,
252 MDGrid::dimensionworld,
253 typename MDGrid::ctype,
254 SubDomainGridFamily<MDGrid>
257 using GridImp = SubDomainGrid<MDGrid>;
261 using MultiDomainGrid = MDGrid;
263 using HostGrid =
typename MDGrid::HostGrid;
267 typedef IndexSetWrapper<const SubDomainGrid<MDGrid>,
typename MDGrid::LevelIndexSetImp> LevelIndexSetImp;
269 typedef IndexSetWrapper<const SubDomainGrid<MDGrid>,
typename MDGrid::LeafIndexSetImp> LeafIndexSetImp;
271 typedef IdSetWrapper<const SubDomainGrid<MDGrid>,
typename HostGrid::Traits::GlobalIdSet> GlobalIdSetImp;
273 typedef IdSetWrapper<const SubDomainGrid<MDGrid>,
typename HostGrid::Traits::LocalIdSet> LocalIdSetImp;
277 typedef SubDomainGridFamily<MDGrid> GridFamily;
278 typedef typename GridFamily::Traits Traits;
281 typedef typename MDGrid::ctype ctype;
284 typedef typename MDGrid::SubDomainIndex SubDomainIndex;
286 enum IntersectionType { neighbor, foreign, boundary, processor };
288 using BaseT::dimension;
289 using BaseT::dimensionworld;
294 template<
typename EntitySeed>
295 typename Traits::template Codim<EntitySeed::codimension>::Entity
296 entity(
const EntitySeed& entitySeed)
const
299 EntityWrapper<EntitySeed::codimension,dimension,const GridImp>(
301 typename MDGrid::template Codim<EntitySeed::codimension>::Entity(
302 _grid.entity(entitySeed)
307 int maxLevel()
const {
308 return _grid.maxLevel();
312 typename Traits::template Codim<codim>::LevelIterator lbegin(
int level)
const {
313 return IteratorWrapper<
314 typename Traits::LevelGridView,
315 typename MultiDomainGrid::LevelGridView::template Codim<codim>::template Partition<All_Partition>::Iterator,
320 &this->levelGridView(level).indexSet(),
321 _grid.levelGridView(level).template begin<codim>(),
322 _grid.levelGridView(level).template end<codim>()
327 typename Traits::template Codim<codim>::LevelIterator lend(
int level)
const {
328 return IteratorWrapper<
329 typename Traits::LevelGridView,
330 typename MultiDomainGrid::LevelGridView::template Codim<codim>::template Partition<All_Partition>::Iterator,
335 &this->levelGridView(level).indexSet(),
336 _grid.levelGridView(level).template end<codim>(),
337 _grid.levelGridView(level).template end<codim>()
341 template<
int codim, PartitionIteratorType pitype>
342 typename Traits::template Codim<codim>::template Partition<pitype>::LevelIterator lbegin(
int level)
const {
343 return IteratorWrapper<
344 typename Traits::LevelGridView,
345 typename MultiDomainGrid::LevelGridView::template Codim<codim>::template Partition<pitype>::Iterator,
350 &this->levelGridView(level).indexSet(),
351 _grid.levelGridView(level).template begin<codim,pitype>(),
352 _grid.levelGridView(level).template end<codim,pitype>()
356 template<
int codim, PartitionIteratorType pitype>
357 typename Traits::template Codim<codim>::template Partition<pitype>::LevelIterator lend(
int level)
const {
358 return IteratorWrapper<
359 typename Traits::LevelGridView,
360 typename MultiDomainGrid::LevelGridView::template Codim<codim>::template Partition<pitype>::Iterator,
365 &this->levelGridView(level).indexSet(),
366 _grid.levelGridView(level).template end<codim,pitype>(),
367 _grid.levelGridView(level).template end<codim,pitype>()
372 typename Traits::template Codim<codim>::LeafIterator leafbegin()
const {
373 return IteratorWrapper<
374 typename Traits::LeafGridView,
375 typename MultiDomainGrid::LeafGridView::template Codim<codim>::template Partition<All_Partition>::Iterator,
380 &this->leafGridView().indexSet(),
381 _grid.leafGridView().template begin<codim>(),
382 _grid.leafGridView().template end<codim>()
387 typename Traits::template Codim<codim>::LeafIterator leafend()
const {
388 return IteratorWrapper<
389 typename Traits::LeafGridView,
390 typename MultiDomainGrid::LeafGridView::template Codim<codim>::template Partition<All_Partition>::Iterator,
395 &this->leafGridView().indexSet(),
396 _grid.leafGridView().template end<codim>(),
397 _grid.leafGridView().template end<codim>()
401 template<
int codim, PartitionIteratorType pitype>
402 typename Traits::template Codim<codim>::template Partition<pitype>::LeafIterator leafbegin()
const {
403 return IteratorWrapper<
404 typename Traits::LeafGridView,
405 typename MultiDomainGrid::LeafGridView::template Codim<codim>::template Partition<pitype>::Iterator,
410 &this->leafGridView().indexSet(),
411 _grid.leafGridView().template begin<codim,pitype>(),
412 _grid.leafGridView().template end<codim,pitype>()
416 template<
int codim, PartitionIteratorType pitype>
417 typename Traits::template Codim<codim>::template Partition<pitype>::LeafIterator leafend()
const {
418 return IteratorWrapper<
419 typename Traits::LeafGridView,
420 typename MultiDomainGrid::LeafGridView::template Codim<codim>::template Partition<pitype>::Iterator,
425 &this->leafGridView().indexSet(),
426 _grid.leafGridView().template end<codim,pitype>(),
427 _grid.leafGridView().template end<codim,pitype>()
431 int size(
int level,
int codim)
const {
432 assert(level <= maxLevel());
433 return _levelIndexSets[level]->size(codim);
436 int size(
int codim)
const {
437 return _leafIndexSet.size(codim);
440 int size(
int level, GeometryType type)
const {
441 assert(level <= maxLevel());
442 return _levelIndexSets[level]->size(type);
445 int size(GeometryType type)
const {
447 return _leafIndexSet.size(type);
450 const typename Traits::GlobalIdSet& globalIdSet()
const {
454 const typename Traits::LocalIdSet& localIdSet()
const {
458 const typename Traits::LevelIndexSet& levelIndexSet(
int level)
const {
459 if (!_grid.supportLevelIndexSets()) {
460 DUNE_THROW(GridError,
"level index set support not enabled for this grid");
462 assert(level <= maxLevel());
463 assert(_levelIndexSets[level]);
464 return *_levelIndexSets[level];
467 const typename Traits::LeafIndexSet& leafIndexSet()
const {
468 return _leafIndexSet;
478 void globalRefine(
int refCount) {
479 DUNE_THROW(NotImplemented,
"grid modification only allowed on the MultiDomainGrid");
493 bool mark(
int refCount,
const typename Traits::template Codim<0>::Entity& e) {
494 return _grid.mark(refCount,multiDomainEntity(e));
505 int getMark(
const typename Traits::template Codim<0>::Entity& e) {
506 return _grid.getMark(multiDomainEntity(e));
517 DUNE_THROW(NotImplemented,
"grid modification only allowed on the MultiDomainGrid");
528 DUNE_THROW(NotImplemented,
"grid modification only allowed on the MultiDomainGrid");
539 DUNE_THROW(NotImplemented,
"grid modification only allowed on the MultiDomainGrid");
542 int overlapSize(
int level,
int codim)
const {
543 return _grid.overlapSize(level,codim);
546 int overlapSize(
int codim)
const {
547 return _grid.overlapSize(codim);
550 int ghostSize(
int level,
int codim)
const {
551 return _grid.ghostSize(level,codim);
554 int ghostSize(
int codim)
const {
555 return _grid.ghostSize(codim);
558 const typename Traits::Communication& comm()
const {
562 template<
typename DataHandleImp,
typename DataTypeImp>
563 void communicate (CommDataHandleIF<DataHandleImp,DataTypeImp> &data,
564 InterfaceType iftype,
565 CommunicationDirection dir,
568 DataHandleWrapper<CommDataHandleIF<DataHandleImp,DataTypeImp> > datahandle(data,*
this);
569 _grid._hostGrid.levelGridView(level).communicate(datahandle,iftype,dir);
572 template<
typename DataHandleImp,
typename DataTypeImp>
573 void communicate (CommDataHandleIF<DataHandleImp,DataTypeImp> &data,
574 InterfaceType iftype,
575 CommunicationDirection dir)
const
577 DataHandleWrapper<CommDataHandleIF<DataHandleImp,DataTypeImp> > datahandle(data,*
this);
578 _grid._hostGrid.leafGridView().communicate(datahandle,iftype,dir);
581 size_t numBoundarySegments()
const
583 return _grid.numBoundarySegments();
589 const MDGrid& multiDomainGrid()
const {
594 SubDomainIndex domain()
const {
599 if (_grid.supportLevelIndexSets()) {
600 while (_levelIndexSets.size() <=
static_cast<std::size_t
>(maxLevel())) {
601 _levelIndexSets.push_back(std::make_shared<LevelIndexSetImp>(*
this,_grid.levelIndexSet(_levelIndexSets.size())));
606 bool operator==(
const SubDomainGrid& rhs)
const {
607 return (&_grid == &rhs._grid && _subDomain == rhs._subDomain);
615 std::enable_if_t<std::is_same_v<typename MDGrid::Traits::template Codim<EntityType::codimension>::Entity,EntityType>,
int> = 0>
616 typename Traits::template Codim<EntityType::codimension>::Entity subDomainEntity(
const EntityType& mdEntity)
const {
617 return EntityWrapper<EntityType::codimension,dimension,const GridImp>(
this,mdEntity);
623 std::enable_if_t<std::is_same_v<typename MDGrid::Traits::template Codim<EntityType::codimension>::Entity,EntityType>,
int> = 0>
624 static const EntityType&
625 multiDomainEntity(
const EntityType &e) {
631 std::enable_if_t<!std::is_same_v<typename MDGrid::Traits::template Codim<EntityType::codimension>::Entity,EntityType>,
int> = 0>
632 static const typename MDGrid::Traits::template Codim<EntityType::codimension>::Entity &
633 multiDomainEntity(
const EntityType &e) {
634 return e.impl().multiDomainEntity();
637 template<
typename EntityType>
638 static const typename MDGrid::template HostEntity<EntityType>::type& hostEntity(
const EntityType& e) {
639 return e.impl().hostEntity();
642 static const auto& multiDomainIntersection(
const typename Traits::LeafIntersection& is) {
643 return is.impl().multiDomainIntersection();
646 static const auto& multiDomainIntersection(
const typename Traits::LevelIntersection& is) {
647 return is.impl().multiDomainIntersection();
652 typename Traits::LeafIntersectionIterator subDomainIntersectionIterator(
const typename MDGrid::LeafSubDomainInterfaceIterator it)
const {
653 assert(_subDomain == it->subDomain1() || _subDomain == it->subDomain2());
654 if (_subDomain == it->subDomain1())
655 return IntersectionIteratorWrapper<
657 typename GridImp::LeafGridView::IndexSet,
658 typename MDGrid::LeafGridView::IntersectionIterator
660 &this->leafGridView().indexSet(),
661 it->firstMultiDomainIntersectionIterator()
664 return IntersectionIteratorWrapper<
666 typename GridImp::LeafGridView::IndexSet,
667 typename MDGrid::LeafGridView::IntersectionIterator
669 &this->leafGridView().indexSet(),
670 it->secondMultiDomainIntersectionIterator()
674 typename Traits::LevelIntersectionIterator subDomainIntersectionIterator(
const typename MDGrid::LevelSubDomainInterfaceIterator it)
const {
675 assert(_subDomain == it->subDomain1() || _subDomain == it->subDomain2());
676 if (_subDomain == it->subDomain1())
677 return IntersectionIteratorWrapper<
679 typename GridImp::LevelGridView::IndexSet,
680 typename MDGrid::LevelGridView::IntersectionIterator
682 &this->levelGridView(it->firstMultiDomainIntersectionIterator()->inside().level()).indexSet(),
683 it->firstMultiDomainIntersectionIterator()
686 return IntersectionIteratorWrapper<
688 typename GridImp::LevelGridView::IndexSet,
689 typename MDGrid::LevelGridView::IntersectionIterator
691 &this->levelGridView(it->secondMultiDomainIntersectionIterator()->inside().level()).indexSet(),
692 it->secondMultiDomainIntersectionIterator()
696 template<
typename Intersection>
697 IntersectionType intersectionType(
const Intersection& intersection)
const {
698 return intersection.impl().intersectionType();
704 SubDomainIndex _subDomain;
705 GlobalIdSetImp _globalIdSet;
706 LocalIdSetImp _localIdSet;
707 LeafIndexSetImp _leafIndexSet;
708 std::vector<std::shared_ptr<LevelIndexSetImp> > _levelIndexSets;
710 SubDomainGrid(MDGrid& grid, SubDomainIndex subDomain) :
712 _subDomain(subDomain),
713 _globalIdSet(*this,grid._hostGrid.globalIdSet()),
714 _localIdSet(*this,grid._hostGrid.localIdSet()),
715 _leafIndexSet(*this,grid.leafIndexSet())
720 template<
typename EntityType>
721 bool containsMultiDomainEntity(
const EntityType& e)
const {
722 if (_grid.supportLevelIndexSets())
723 return levelIndexSet(e.level()).containsMultiDomainEntity(e);
725 return leafIndexSet().containsMultiDomainEntity(e);
728 template<
typename EntityType>
729 bool containsHostEntity(
const EntityType& e)
const {
730 if (_grid.supportLevelIndexSets())
731 return levelIndexSet(e.level()).containsHostEntity(e);
733 return leafIndexSet().containsHostEntity(e);
736 SubDomainGrid(
const SubDomainGrid& rv);
737 SubDomainGrid& operator=(
const SubDomainGrid& rv);
740 template<
typename Impl>
741 struct DataHandleWrapper
742 :
public Dune::CommDataHandleIF<DataHandleWrapper<Impl>,
743 typename Impl::DataType
747 bool contains(
int dim,
int codim)
const
749 return _impl.contains(dim,codim);
752 bool fixedSize(
int dim,
int codim)
const
758 template<
typename Entity>
759 std::size_t size(
const Entity& e)
const
761 if (_grid.containsHostEntity(e))
762 return _impl.size(_grid.subDomainEntity(_grid._grid.wrapHostEntity(e)));
767 template<
typename MessageBufferImp,
typename Entity>
768 void gather(MessageBufferImp& buf,
const Entity& e)
const
770 if (_grid.containsHostEntity(e))
771 _impl.gather(buf,_grid.subDomainEntity(_grid._grid.wrapHostEntity(e)));
774 template<
typename MessageBufferImp,
typename Entity>
775 void scatter(MessageBufferImp& buf,
const Entity& e, std::size_t n)
777 if (_grid.containsHostEntity(e))
778 _impl.scatter(buf,_grid.subDomainEntity(_grid._grid.wrapHostEntity(e)),n);
781 DataHandleWrapper(Impl& impl,
const SubDomainGrid<MDGrid>& grid)
787 const SubDomainGrid<MDGrid>& _grid;