Loading [MathJax]/extensions/tex2jax.js

DUNE MultiDomainGrid (2.8)

indexsets.hh
1#ifndef DUNE_MULTIDOMAINGRID_INDEXSETS_HH
2#define DUNE_MULTIDOMAINGRID_INDEXSETS_HH
3
4#include <map>
5#include <unordered_map>
6#include <vector>
7#include <array>
8#include <algorithm>
9#include <memory>
10#include <type_traits>
11#include <tuple>
12#include <utility>
13
14#include <dune/common/hybridutilities.hh>
15
16#include <dune/geometry/typeindex.hh>
17
18#include <dune/grid/common/exceptions.hh>
19#include <dune/grid/common/indexidset.hh>
20
21#include <dune/grid/multidomaingrid/utility.hh>
22#include <dune/grid/multidomaingrid/subdomaingrid/indexsets.hh>
23
24namespace Dune {
25
26namespace mdgrid {
27
28template<typename HostGrid, typename MDGridTraits>
29class MultiDomainGrid;
30
32
34namespace detail {
35
36template<template<int> class StructForCodim, typename Codims>
37struct _buildMap;
38
40template<template<int> class StructForCodim, std::size_t... codim>
41struct _buildMap<StructForCodim,std::index_sequence<codim...> > {
42
43 typedef std::tuple<StructForCodim<codim>... > type;
44
45};
46
47template<template<int> class StructForCodim, int dimension>
48struct buildMap {
49
50 typedef typename _buildMap<
51 StructForCodim,
52 decltype(std::make_index_sequence<dimension+1>())
53 >::type type;
54
55};
56
58
65template<bool doDispatch, typename Impl, typename resulttype,bool alternate_dispatch>
66struct invokeIf;
67
68template<typename Impl, typename resulttype, bool alternate_dispatch>
69struct invokeIf<true,Impl,resulttype,alternate_dispatch> {
70
71 typedef resulttype result_type;
72
73 template<int codim>
74 result_type invoke() {
75 return _impl.template invoke<codim>();
76 }
77
78 Impl& _impl;
79
80 invokeIf(Impl& impl) :
81 _impl(impl)
82 {}
83
84};
85
87template<typename Impl, typename resulttype>
88struct invokeIf<false,Impl, resulttype,false> {
89
90 typedef resulttype result_type;
91
92 template<int codim>
93 result_type invoke() {
94 DUNE_THROW(GridError,"codim not supported");
95 }
96
97 Impl& _impl;
98
99 invokeIf(Impl& impl) :
100 _impl(impl)
101 {}
102
103};
104
105template<typename Impl, typename resulttype>
106struct invokeIf<false,Impl, resulttype,true> {
107
108 typedef resulttype result_type;
109
110 template<int codim>
111 result_type invoke() {
112 return _impl.template invoke_unsupported<codim>();
113 }
114
115 Impl& _impl;
116
117 invokeIf(Impl& impl) :
118 _impl(impl)
119 {}
120
121};
122
124template<typename Impl, typename resulttype, typename MDGridTraits, int codim, bool protect = true, bool alternate_dispatch = false>
125struct dispatchToCodim
126 : public dispatchToCodim<Impl,resulttype,MDGridTraits,codim-1,protect,alternate_dispatch> {
127
128 typedef resulttype result_type;
129
130 result_type dispatch(int cc) {
131 if (cc == codim)
132 return invokeIf<MDGridTraits::template Codim<codim>::supported,Impl,result_type,alternate_dispatch>(static_cast<Impl&>(*this)).template invoke<codim>();
133 return static_cast<dispatchToCodim<Impl,result_type,MDGridTraits,codim-1,protect,alternate_dispatch>&>(*this).dispatch(cc);
134 }
135
136};
137
139template<typename Impl, typename resulttype, typename MDGridTraits, bool alternate_dispatch>
140struct dispatchToCodim<Impl,resulttype,MDGridTraits,0,true,alternate_dispatch> {
141
142 typedef resulttype result_type;
143
144 result_type dispatch(int cc) {
145 if (cc == 0)
146 return invokeIf<MDGridTraits::template Codim<0>::supported,Impl,result_type,alternate_dispatch>(static_cast<Impl&>(*this)).template invoke<0>();
147 DUNE_THROW(GridError,"invalid codimension specified");
148 }
149
150};
151
153template<typename Impl, typename resulttype, typename MDGridTraits, int codim, bool alternate_dispatch>
154struct dispatchToCodim<Impl,resulttype,MDGridTraits,codim,false,alternate_dispatch>
155 : public dispatchToCodim<Impl,resulttype,MDGridTraits,codim-1,false,alternate_dispatch> {
156
157 typedef resulttype result_type;
158
159 result_type dispatch(int cc) {
160 if (cc == codim)
161 return static_cast<Impl&>(*this).template invoke<codim>();
162 return static_cast<dispatchToCodim<Impl,result_type,MDGridTraits,codim-1,false,alternate_dispatch>&>(*this).dispatch(cc);
163 }
164
165};
166
168template<typename Impl, typename resulttype, typename MDGridTraits, bool alternate_dispatch>
169struct dispatchToCodim<Impl,resulttype,MDGridTraits,0,false,alternate_dispatch> {
170
171 typedef resulttype result_type;
172
173 result_type dispatch(int cc) {
174 if (cc == 0)
175 return static_cast<Impl&>(*this).template invoke<0>();
176 DUNE_THROW(GridError,"invalid codimension specified");
177 }
178
179};
180
186template<bool doApply, typename Impl>
187struct applyIf {
188
189 template<int codim, typename T>
190 void apply(T& t) const {
191 _impl.template apply<codim>(t);
192 }
193
194 Impl& _impl;
195
196 applyIf(Impl& impl) :
197 _impl(impl)
198 {}
199
200};
201
203template<typename Impl>
204struct applyIf<false,Impl> {
205
206 template<int codim, typename T>
207 void apply(T& t) const {
208 }
209
210 Impl& _impl;
211
212 applyIf(Impl& impl) :
213 _impl(impl)
214 {}
215
216};
217
219template<typename T>
220T& rw(const T& t) {
221 return const_cast<T&>(t);
222}
223
224}
225
227
231template<typename GridImp, typename HostGridViewType>
233 public Dune::IndexSet<GridImp,IndexSetWrapper<GridImp,HostGridViewType>,
234 typename HostGridViewType::IndexSet::IndexType,
235 typename HostGridViewType::IndexSet::Types>
236{
237
238 using Grid = std::remove_const_t<GridImp>;
239
240 template<typename, typename>
241 friend class IndexSetWrapper;
242
243 template<typename, typename>
244 friend class MultiDomainGrid;
245
246 template<typename, typename>
247 friend class subdomain::IndexSetWrapper;
248
249 template<typename, typename, typename, typename>
250 friend class SubDomainInterface;
251
252 template<typename>
253 friend class SubDomainToSubDomainController;
254
255 template<typename>
256 friend class AllInterfacesController;
257
259
260 using HostGrid = typename Grid::HostGrid;
261 typedef HostGridViewType HostGridView;
262 typedef typename HostGridView::IndexSet HostIndexSet;
263 using ctype = typename Grid::ctype;
264 using CellReferenceElement = Dune::ReferenceElement<typename HostGrid::template Codim<0>::Entity::Geometry>;
265
266 typedef Dune::IndexSet<
267 GridImp,
269 GridImp,
270 HostGridViewType
271 >,
272 typename HostGridViewType::IndexSet::IndexType,
273 typename HostGridViewType::IndexSet::Types
274 > BaseT;
275
276public:
277
278 typedef typename BaseT::Types Types;
279
280 using MDGridTraits = typename Grid::MDGridTraits;
281 typedef typename MDGridTraits::template Codim<0>::SubDomainSet SubDomainSet;
282 typedef typename MDGridTraits::SubDomainIndex SubDomainIndex;
283
284
285 typedef typename HostIndexSet::IndexType IndexType;
286 static const int dimension = Grid::dimension;
287 static const std::size_t maxSubDomains = SubDomainSet::maxSize;
288
289private:
290
291 typedef typename HostGridView::template Codim<0>::Iterator HostEntityIterator;
292 typedef typename HostGridView::template Codim<0>::Entity HostEntity;
293 using Codim0Entity = typename Grid::Traits::template Codim<0>::Entity;
294
295 template<int cc>
296 struct MapEntry {
297
298 typedef typename MDGridTraits::template Codim<cc>::SubDomainSet SubDomainSet;
299
300 SubDomainSet domains;
301 IndexType index;
302 };
303
305 struct NotSupported {};
306
307 template<int codim>
308 struct Containers {
309
310 static const bool supported = Grid::MDGridTraits::template Codim<codim>::supported;
311
312 static_assert((codim > 0 || supported), "index mapping of codimension 0 must be supported!");
313
314 using IndexMap = std::conditional_t<
315 supported,
316 std::vector<std::vector<MapEntry<codim> > >,
317 NotSupported
318 >;
319
320 using SizeMap = std::conditional_t<
321 supported,
322 std::vector<typename Grid::MDGridTraits::template Codim<codim>::SizeContainer>,
323 NotSupported
324 >;
325
326 using CodimSizeMap = std::conditional_t<
327 supported,
328 typename Grid::MDGridTraits::template Codim<codim>::SizeContainer,
329 NotSupported
330 >;
331
332 using MultiIndexMap = std::conditional_t<
333 supported,
334 std::vector<typename Grid::MDGridTraits::template Codim<codim>::MultiIndexContainer>,
335 NotSupported
336 >;
337
338 IndexMap indexMap;
339 SizeMap sizeMap;
340 CodimSizeMap codimSizeMap;
341 MultiIndexMap multiIndexMap;
342
343 // containers should not be assignable...
344 Containers& operator=(const Containers&) = delete;
345
346 // ...but must be movable
347 Containers& operator=(Containers&&) = default;
348
349 // make sure the compiler generates all necessary constructors...
350 Containers(const Containers&) = default;
351 Containers(Containers&&) = default;
352 Containers() = default;
353
354 };
355
356 typedef typename detail::buildMap<Containers,dimension>::type ContainerMap;
357
358 typedef std::vector<std::shared_ptr<IndexSetWrapper<GridImp, typename HostGridView::Grid::LevelGridView> > > LevelIndexSets;
359
361 template<typename Impl,typename result_type, bool protect = true, bool alternate_dispatch = false>
362 struct dispatchToCodim : public detail::dispatchToCodim<Impl,result_type,MDGridTraits,dimension,protect,alternate_dispatch> {};
363
364public:
365
367 template<int codim>
368 IndexType index(const typename Grid::Traits::template Codim<codim>::Entity& e) const {
369 return _hostGridView.indexSet().index(_grid.hostEntity(e));
370 }
371
373 template<typename Entity>
374 IndexType index(const Entity& e) const {
375 return _hostGridView.indexSet().index(_grid.hostEntity(e));
376 }
377
379 template<int codim, typename Entity>
380 IndexType subIndex(const Entity& e, int i) const {
381 return _hostGridView.indexSet().subIndex(_grid.hostEntity(e),i,codim);
382 }
383
385 template<typename Entity>
386 IndexType subIndex(const Entity& e, int i, unsigned int codim) const {
387 IndexType r = _hostGridView.indexSet().subIndex(_grid.hostEntity(e),i,codim);
388 return r;
389 }
390
392 Types types(int codim) const {
393 return _hostGridView.indexSet().types(codim);
394 }
395
397 IndexType size(GeometryType type) const {
398 return _hostGridView.indexSet().size(type);
399 }
400
402 IndexType size(int codim) const {
403 return _hostGridView.indexSet().size(codim);
404 }
405
407 template<typename EntityType>
408 bool contains(const EntityType& e) const {
409 return _hostGridView.indexSet().contains(_grid.hostEntity(e));
410 }
411
413 template<typename EntityType>
414 const typename MapEntry<EntityType::codimension>::SubDomainSet& subDomains(const EntityType& e) const {
415 return subDomainsForHostEntity(_grid.hostEntity(e));
416 }
417
420 template<int cc>
421 const typename MapEntry<cc>::SubDomainSet& subDomains(const typename Grid::Traits::template Codim<cc>::Entity& e) const {
422 return subDomainsForHostEntity<cc>(_grid.hostEntity(e));
423 }
424
426 template<class EntityType>
427 IndexType index(SubDomainIndex subDomain, const EntityType& e) const {
428 return index<EntityType::codimension>(subDomain,e);
429 }
430
433 template<int cc>
434 IndexType index(SubDomainIndex subDomain, const typename Grid::Traits::template Codim<cc>::Entity& e) const {
435 GeometryType gt = e.type();
436 IndexType hostIndex = _hostGridView.indexSet().index(_grid.hostEntity(e));
437 const MapEntry<cc>& me = indexMap<cc>()[LocalGeometryTypeIndex::index(gt)][hostIndex];
438 assert(me.domains.contains(subDomain));
439 if (me.domains.simple()) {
440 return me.index;
441 } else {
442 return multiIndexMap<cc>()[me.index][me.domains.domainOffset(subDomain)];
443 }
444 }
445
446private:
447
449 template<typename EntityType>
450 typename MapEntry<EntityType::codimension>::SubDomainSet& subDomains(const EntityType& e) {
451 return subDomainsForHostEntity(_grid.hostEntity(e));
452 }
453
456 template<int cc>
457 typename MapEntry<cc>::SubDomainSet& subDomains(const typename Grid::Traits::template Codim<cc>::Entity& e) {
458 return subDomainsForHostEntity<cc>(_grid.hostEntity(e));
459 }
460
462 template<typename HostEntity>
463 typename MapEntry<HostEntity::codimension>::SubDomainSet& subDomainsForHostEntity(const HostEntity& e) {
464 return subDomainsForHostEntity<HostEntity::codimension>(e);
465 }
466
469 template<int cc>
470 typename MapEntry<cc>::SubDomainSet& subDomainsForHostEntity(const typename Grid::HostGrid::Traits::template Codim<cc>::Entity& he) {
471 return indexMap<cc>()[LocalGeometryTypeIndex::index(he.type())][_hostGridView.indexSet().index(he)].domains;
472 }
473
475 template<typename HostEntity>
476 const typename MapEntry<HostEntity::codimension>::SubDomainSet& subDomainsForHostEntity(const HostEntity& e) const {
477 return subDomainsForHostEntity<HostEntity::codimension>(e);
478 }
479
482 template<int cc>
483 const typename MapEntry<cc>::SubDomainSet& subDomainsForHostEntity(const typename Grid::HostGrid::Traits::template Codim<cc>::Entity& he) const {
484 return indexMap<cc>()[LocalGeometryTypeIndex::index(he.type())][_hostGridView.indexSet().index(he)].domains;
485 }
486
487 template<int cc>
488 IndexType indexForSubDomain(SubDomainIndex subDomain, const typename Grid::HostGrid::Traits::template Codim<cc>::Entity& he) const {
489 const GeometryType gt = he.type();
490 const IndexType hostIndex = _hostGridView.indexSet().index(he);
491 const MapEntry<cc>& me = indexMap<cc>()[LocalGeometryTypeIndex::index(gt)][hostIndex];
492 assert(me.domains.contains(subDomain));
493 if (me.domains.simple()) {
494 return me.index;
495 } else {
496 return multiIndexMap<cc>()[me.index][me.domains.domainOffset(subDomain)];
497 }
498 }
499
501 struct getSubIndexForSubDomain : public dispatchToCodim<getSubIndexForSubDomain,IndexType> {
502
503 template<int codim>
504 IndexType invoke() const {
505 const MapEntry<codim>& me = _indexSet.indexMap<codim>()[LocalGeometryTypeIndex::index(_gt)][_hostIndex];
506 assert(me.domains.contains(_subDomain));
507 if (me.domains.simple()) {
508 return me.index;
509 } else {
510 return _indexSet.multiIndexMap<codim>()[me.index][me.domains.domainOffset(_subDomain)];
511 }
512 }
513
514 SubDomainIndex _subDomain;
515 GeometryType _gt;
516 IndexType _hostIndex;
517 const ThisType& _indexSet;
518
519 getSubIndexForSubDomain(SubDomainIndex subDomain, GeometryType gt, IndexType hostIndex, const ThisType& indexSet) :
520 _subDomain(subDomain),
521 _gt(gt),
522 _hostIndex(hostIndex),
523 _indexSet(indexSet)
524 {}
525
526 };
527
528 template<typename HostEntity>
529 IndexType subIndexForSubDomain(SubDomainIndex subDomain, const HostEntity& he, int i, int codim) const {
530 return getSubIndexForSubDomain(subDomain,
531 referenceElement(he.geometry()).type(i,codim - he.codimension),
532 _hostGridView.indexSet().subIndex(he,i,codim),
533 *this).dispatch(codim);
534 }
535
536 Types typesForSubDomain(SubDomainIndex subDomain, int codim) const {
537 return types(codim);
538 }
539
540 struct getGeometryTypeSizeForSubDomain : public dispatchToCodim<getGeometryTypeSizeForSubDomain,IndexType,true,true> {
541
542 template<int codim>
543 IndexType invoke() const {
544 return _indexSet.sizeMap<codim>()[LocalGeometryTypeIndex::index(_gt)][_subDomain];
545 }
546
547 template<int codim>
548 IndexType invoke_unsupported() const {
549 return 0;
550 }
551
552 SubDomainIndex _subDomain;
553 GeometryType _gt;
554 const ThisType& _indexSet;
555
556 getGeometryTypeSizeForSubDomain(SubDomainIndex subDomain, GeometryType gt, const ThisType& indexSet) :
557 _subDomain(subDomain),
558 _gt(gt),
559 _indexSet(indexSet)
560 {}
561
562 };
563
564 IndexType sizeForSubDomain(SubDomainIndex subDomain, GeometryType type) const {
565 return getGeometryTypeSizeForSubDomain(subDomain,type,*this).dispatch(dimension-type.dim());
566 }
567
568 struct getCodimSizeForSubDomain : public dispatchToCodim<getCodimSizeForSubDomain,IndexType,true,true> {
569
570 template<int codim>
571 IndexType invoke() const {
572 return _indexSet.codimSizes<codim>()[_subDomain];
573 }
574
575 template<int codim>
576 IndexType invoke_unsupported() const {
577 return 0;
578 }
579
580 SubDomainIndex _subDomain;
581 const ThisType& _indexSet;
582
583 getCodimSizeForSubDomain(SubDomainIndex subDomain, const ThisType& indexSet) :
584 _subDomain(subDomain),
585 _indexSet(indexSet)
586 {}
587
588 };
589
590 IndexType sizeForSubDomain(SubDomainIndex subDomain, int codim) const {
591 return getCodimSizeForSubDomain(subDomain,*this).dispatch(codim);
592 }
593
594 template<typename EntityType>
595 bool containsForSubDomain(SubDomainIndex subDomain, const EntityType& he) const {
596 const GeometryType gt = he.type();
597 const IndexType hostIndex = _hostGridView.indexSet().index(he);
598 const MapEntry<EntityType::codimension>& me = indexMap<EntityType::codimension>()[LocalGeometryTypeIndex::index(gt)][hostIndex];
599 return me.domains.contains(subDomain);
600 }
601
602public:
603
604 template<typename SubDomainEntity>
605 IndexType subIndex(SubDomainIndex subDomain, const SubDomainEntity& e, int i, int codim) const {
606 return subIndexForSubDomain(subDomain,_grid.hostEntity(e),i,codim);
607 }
608
609 Types types(SubDomainIndex subDomain, int codim) const {
610 return types(codim);
611 }
612
613 IndexType size(SubDomainIndex subDomain, GeometryType type) const {
614 return sizeForSubDomain(subDomain,type);
615 }
616
617 IndexType size(SubDomainIndex subDomain, int codim) const {
618 return sizeForSubDomain(subDomain,codim);
619 }
620
622 template<typename EntityType>
623 bool contains(SubDomainIndex subDomain, const EntityType& e) const {
624 const GeometryType gt = e.type();
625 const IndexType hostIndex = _hostGridView.indexSet().index(_grid.hostEntity(e));
626 const MapEntry<EntityType::codimension>& me = indexMap<EntityType::codimension>()[LocalGeometryTypeIndex::index(gt)][hostIndex];
627 return me.domains.contains(subDomain);
628 }
629
630private:
631
632 const GridImp& _grid;
633 HostGridView _hostGridView;
634 ContainerMap _containers;
635
636 void swap(ThisType& rhs) {
637 assert(&_grid == &rhs._grid);
638 std::swap(_containers,rhs._containers);
639 }
640
641 void addToSubDomain(SubDomainIndex subDomain, const Codim0Entity& e) {
642 GeometryType gt = e.type();
643 IndexType hostIndex = _hostGridView.indexSet().index(_grid.hostEntity(e));
644 indexMap<0>()[LocalGeometryTypeIndex::index(gt)][hostIndex].domains.add(subDomain);
645 }
646
647 void removeFromSubDomain(SubDomainIndex subDomain, const Codim0Entity& e) {
648 GeometryType gt = e.type();
649 IndexType hostIndex = _hostGridView.indexSet().index(_grid.hostEntity(e));
650 indexMap<0>()[LocalGeometryTypeIndex::index(gt)][hostIndex].domains.remove(subDomain);
651 }
652
653 void removeFromAllSubDomains(const Codim0Entity& e) {
654 GeometryType gt = e.type();
655 IndexType hostIndex = _hostGridView.indexSet().index(_grid.hostEntity(e));
656 indexMap<0>()[LocalGeometryTypeIndex::index(gt)][hostIndex].domains.clear();
657 }
658
659 void assignToSubDomain(SubDomainIndex subDomain, const Codim0Entity& e) {
660 GeometryType gt = e.type();
661 IndexType hostIndex = _hostGridView.indexSet().index(_grid.hostEntity(e));
662 indexMap<0>()[LocalGeometryTypeIndex::index(gt)][hostIndex].domains.set(subDomain);
663 }
664
665 void addToSubDomains(const typename MDGridTraits::template Codim<0>::SubDomainSet& subDomains, const Codim0Entity& e) {
666 GeometryType gt = e.type();
667 IndexType hostIndex = _hostGridView.indexSet().index(_grid.hostEntity(e));
668 indexMap<0>()[LocalGeometryTypeIndex::index(gt)][hostIndex].domains.addAll(subDomains);
669 }
670
671public:
672
673 // just make these public for std::make_shared() access
674
675 IndexSetWrapper(const GridImp& grid, HostGridView hostGridView) :
676 _grid(grid),
677 _hostGridView(hostGridView)
678 {}
679
680 explicit IndexSetWrapper(const ThisType& rhs) :
681 _grid(rhs._grid),
682 _hostGridView(rhs._hostGridView),
683 _containers(rhs._containers)
684 {}
685
686private:
687
690 template<int cc>
691 typename Containers<cc>::IndexMap& indexMap() {
692 return std::get<cc>(_containers).indexMap;
693 }
694
695 template<int cc>
696 typename Containers<cc>::SizeMap& sizeMap() {
697 return std::get<cc>(_containers).sizeMap;
698 }
699
700 template<int cc>
701 typename Containers<cc>::CodimSizeMap& codimSizes() {
702 return std::get<cc>(_containers).codimSizeMap;
703 }
704
705 template<int cc>
706 typename Containers<cc>::MultiIndexMap& multiIndexMap() {
707 return std::get<cc>(_containers).multiIndexMap;
708 }
709
710 template<int cc>
711 const typename Containers<cc>::IndexMap& indexMap() const {
712 return std::get<cc>(_containers).indexMap;
713 }
714
715 template<int cc>
716 const typename Containers<cc>::SizeMap& sizeMap() const {
717 return std::get<cc>(_containers).sizeMap;
718 }
719
720 template<int cc>
721 const typename Containers<cc>::CodimSizeMap& codimSizes() const {
722 return std::get<cc>(_containers).codimSizeMap;
723 }
724
725 template<int cc>
726 const typename Containers<cc>::MultiIndexMap& multiIndexMap() const {
727 return std::get<cc>(_containers).multiIndexMap;
728 }
729
730 template<typename Functor>
731 void applyToCodims(Functor func) const {
732 // static loop over container tuple
733 Hybrid::forEach(Dune::range(std::tuple_size<ContainerMap>{}),
734 [&](auto i){func(i, std::get<i>(_containers));}
735 );
736 }
737
738 template<typename Functor>
739 void applyToCodims(Functor func) {
740 // static loop over container tuple
741 Hybrid::forEach(Dune::range(std::tuple_size<ContainerMap>{}),
742 [&](auto i){func(i, std::get<i>(_containers));}
743 );
744 }
745
746 template<typename Impl>
747 struct applyToCodim {
748
749 template<typename I, typename T>
750 void operator()(I i, T&& t) const {
751 const int codim = I::value;
752 detail::applyIf<MDGridTraits::template Codim<codim>::supported,const Impl>(static_cast<const Impl&>(*this)).template apply<codim>(std::forward<T>(t));
753 }
754
755 };
756
757 struct resetPerCodim : public applyToCodim<resetPerCodim> {
758
759 template<int codim>
760 void apply(Containers<codim>& c) const {
761 // setup per-codim sizemap
762 _traits.template setupSizeContainer<codim>(c.codimSizeMap);
763 c.indexMap.resize(LocalGeometryTypeIndex::size(dimension-codim));
764 c.sizeMap.resize(LocalGeometryTypeIndex::size(dimension-codim));
765 for (auto gt : _his.types(codim)) {
766 const auto gt_index = LocalGeometryTypeIndex::index(gt);
767 if (_full) {
768 // resize index map
769 c.indexMap[gt_index].resize(_his.size(gt));
770 } else if (codim > 0) {
771 // clear out marked state for codim > 0 (we cannot keep the old
772 // state for subentities, as doing so will leave stale entries if
773 // elements are removed from a subdomain
774 for (auto& mapEntry : c.indexMap[gt_index])
775 mapEntry.domains.clear();
776 }
777 // setup / reset SizeMap counter
778 auto& size_map = c.sizeMap[gt_index];
779 _traits.template setupSizeContainer<codim>(size_map);
780 std::fill(size_map.begin(),size_map.end(),0);
781 }
782 // clear MultiIndexMap
783 c.multiIndexMap.clear();
784 }
785
786 resetPerCodim(bool full, const HostIndexSet& his, const MDGridTraits& traits) :
787 _full(full),
788 _his(his),
789 _traits(traits)
790 {}
791
792 const bool _full;
793 const HostIndexSet& _his;
794 const MDGridTraits& _traits;
795 };
796
797 void reset(bool full) {
798 const HostIndexSet& his = _hostGridView.indexSet();
799 if (full) {
800 ContainerMap cm = ContainerMap();
801 std::swap(_containers,cm);
802 }
803 applyToCodims(resetPerCodim(full,his,_grid._traits));
804 }
805
806 struct updatePerCodimSizes : public applyToCodim<updatePerCodimSizes> {
807
808 template<int codim>
809 void apply(Containers<codim>& c) const {
810 // reset size for this codim to zero
811 std::fill(c.codimSizeMap.begin(),c.codimSizeMap.end(),0);
812 // collect per-geometrytype sizes into codim size structure
813 std::for_each(c.sizeMap.begin(),
814 c.sizeMap.end(),
815 util::collect_elementwise<std::plus<IndexType> >(c.codimSizeMap));
816 }
817
818 };
819
820 void update(LevelIndexSets& levelIndexSets, bool full) {
821 const HostIndexSet& his = _hostGridView.indexSet();
822 //reset(full);
823 for (typename LevelIndexSets::iterator it = levelIndexSets.begin(); it != levelIndexSets.end(); ++it) {
824 (*it)->reset(full);
825 }
826
827 this->communicateSubDomainSelection();
828
829 typename Containers<0>::IndexMap& im = indexMap<0>();
830 typename Containers<0>::SizeMap& sm = sizeMap<0>();
831 for (const auto& he : elements(_hostGridView)) {
832 auto geo = he.geometry();
833 auto hgt = geo.type();
834 const auto hgt_index = LocalGeometryTypeIndex::index(hgt);
835 IndexType hostIndex = his.index(he);
836 MapEntry<0>& me = im[hgt_index][hostIndex];
837
838 if (_grid.supportLevelIndexSets()) {
839 levelIndexSets[he.level()]->template indexMap<0>()[hgt_index][levelIndexSets[he.level()]->_hostGridView.indexSet().index(he)].domains.addAll(me.domains);
840 markAncestors(levelIndexSets,he,me.domains);
841 }
842 updateMapEntry(me,sm[hgt_index],multiIndexMap<0>());
843 applyToCodims(markSubIndices(he,me.domains,his,geo));
844 }
845
846 propagateBorderEntitySubDomains();
847
848 applyToCodims(updateSubIndices(*this));
849 applyToCodims(updatePerCodimSizes());
850 for(typename LevelIndexSets::iterator it = levelIndexSets.begin(); it != levelIndexSets.end(); ++it) {
851 (*it)->updateLevelIndexSet();
852 }
853 }
854
855
856 void updateLevelIndexSet() {
857 const HostIndexSet& his = _hostGridView.indexSet();
858 typename Containers<0>::IndexMap& im = indexMap<0>();
859 typename Containers<0>::SizeMap& sm = sizeMap<0>();
860
861 communicateSubDomainSelection();
862
863 for (const auto& he : elements(_hostGridView)) {
864 auto geo = he.geometry();
865 const GeometryType hgt = geo.type();
866 const auto hgt_index = LocalGeometryTypeIndex::index(hgt);
867 IndexType hostIndex = his.index(he);
868 MapEntry<0>& me = im[hgt_index][hostIndex];
869 updateMapEntry(me,sm[hgt_index],multiIndexMap<0>());
870 applyToCodims(markSubIndices(he,me.domains,his,geo));
871 }
872
873 propagateBorderEntitySubDomains();
874
875 applyToCodims(updateSubIndices(*this));
876 applyToCodims(updatePerCodimSizes());
877 }
878
879 template<int codim, typename SizeContainer, typename MultiIndexContainer>
880 void updateMapEntry(MapEntry<codim>& me, SizeContainer& sizes, std::vector<MultiIndexContainer>& multiIndexMap) {
881 switch (me.domains.state()) {
882 case MapEntry<codim>::SubDomainSet::emptySet:
883 break;
884 case MapEntry<codim>::SubDomainSet::simpleSet:
885 me.index = sizes[*me.domains.begin()]++;
886 break;
887 case MapEntry<codim>::SubDomainSet::multipleSet:
888 me.index = multiIndexMap.size();
889 multiIndexMap.push_back(MultiIndexContainer());
890 MultiIndexContainer& mic = multiIndexMap.back();
891 for (typename SubDomainSet::Iterator it = me.domains.begin(); it != me.domains.end(); ++it) {
892 mic[me.domains.domainOffset(*it)] = sizes[*it]++;
893 }
894 }
895 }
896
897 template<typename SubDomainSet>
898 void markAncestors(LevelIndexSets& levelIndexSets, HostEntity he, const SubDomainSet& domains) {
899 while (he.level() > 0) {
900 he = he.father();
901 SubDomainSet& fatherDomains =
902 levelIndexSets[he.level()]->template indexMap<0>()[LocalGeometryTypeIndex::index(he.type())][levelIndexSets[he.level()]->_hostGridView.indexSet().index(he)].domains;
903 if (fatherDomains.containsAll(domains))
904 break;
905 fatherDomains.addAll(domains);
906 }
907 }
908
909 struct markSubIndices : public applyToCodim<const markSubIndices> {
910
911 template<int codim>
912 void apply(Containers<codim>& c) const {
913 if (codim == 0)
914 return;
915 const int size = _refEl.size(codim);
916 for (int i = 0; i < size; ++i) {
917 IndexType hostIndex = _his.subIndex(_he,i,codim);
918 GeometryType gt = _refEl.type(i,codim);
919 c.indexMap[LocalGeometryTypeIndex::index(gt)][hostIndex].domains.addAll(_domains);
920 }
921 }
922
923 typedef typename MapEntry<0>::SubDomainSet& DomainSet;
924
925 const HostEntity& _he;
926 DomainSet& _domains;
927 const HostIndexSet& _his;
928 CellReferenceElement _refEl;
929
930 markSubIndices(const HostEntity& he, DomainSet& domains, const HostIndexSet& his, const typename HostEntity::Geometry& geo) :
931 _he(he),
932 _domains(domains),
933 _his(his),
934 _refEl(referenceElement(geo))
935 {}
936
937 };
938
939 struct updateSubIndices : public applyToCodim<const updateSubIndices> {
940
941 template<int codim>
942 void apply(Containers<codim>& c) const {
943 if (codim == 0)
944 return;
945 for (std::size_t gt_index = 0,
946 gt_end = c.indexMap.size();
947 gt_index != gt_end;
948 ++gt_index)
949 for (auto& im_entry : c.indexMap[gt_index])
950 _indexSet.updateMapEntry(im_entry,c.sizeMap[gt_index],c.multiIndexMap);
951 }
952
953 ThisType& _indexSet;
954
955 updateSubIndices(ThisType& indexSet) :
956 _indexSet(indexSet)
957 {}
958 };
959
961 struct getSupportsCodim : public dispatchToCodim<getSupportsCodim,bool,false> {
962
963 template<int codim>
964 bool invoke() const {
965 return MDGridTraits::template Codim<codim>::supported && Dune::Capabilities::hasEntity<HostGrid,codim>::v;
966 }
967
968 };
969
970 bool supportsCodim(int codim) const
971 {
972 return getSupportsCodim().dispatch(codim);
973 }
974
975 template<typename Impl>
976 struct SubDomainSetDataHandleBase
977 : public Dune::CommDataHandleIF<Impl,
978 typename MapEntry<0>::SubDomainSet::DataHandle::DataType
979 >
980 {
981 typedef typename MapEntry<0>::SubDomainSet SubDomainSet;
982 typedef typename SubDomainSet::DataHandle DataHandle;
983
984 bool fixedSize(int dim, int codim) const
985 {
986 return DataHandle::fixedSize(dim,codim);
987 }
988
989 template<typename Entity>
990 std::size_t size(const Entity& e) const
991 {
992 return MapEntry<Entity::codimension>::SubDomainSet::DataHandle::size(_indexSet.subDomainsForHostEntity(e));
993 }
994
995 template<typename MessageBufferImp, typename Entity>
996 void gather(MessageBufferImp& buf, const Entity& e) const
997 {
998 MapEntry<Entity::codimension>::SubDomainSet::DataHandle::gather(buf,_indexSet.subDomainsForHostEntity(e));
999 }
1000
1001 template<typename MessageBufferImp, typename Entity>
1002 void scatter(MessageBufferImp& buf, const Entity& e, std::size_t n)
1003 {
1004 MapEntry<Entity::codimension>::SubDomainSet::DataHandle::scatter(buf,_indexSet.subDomainsForHostEntity(e),n);
1005 }
1006
1007 SubDomainSetDataHandleBase(ThisType& indexSet)
1008 : _indexSet(indexSet)
1009 {}
1010
1011 ThisType& _indexSet;
1012
1013 };
1014
1015 struct SelectionDataHandle
1016 : public SubDomainSetDataHandleBase<SelectionDataHandle>
1017 {
1018
1019 bool contains(int dim, int codim) const
1020 {
1021 return codim == 0;
1022 }
1023
1024 SelectionDataHandle(ThisType& indexSet)
1025 : SubDomainSetDataHandleBase<SelectionDataHandle>(indexSet)
1026 {}
1027
1028 };
1029
1030 struct BorderPropagationDataHandle
1031 : public SubDomainSetDataHandleBase<BorderPropagationDataHandle>
1032 {
1033
1034 bool contains(int dim, int codim) const
1035 {
1036 return codim > 0 && this->_indexSet.supportsCodim(codim);
1037 }
1038
1039 BorderPropagationDataHandle(ThisType& indexSet)
1040 : SubDomainSetDataHandleBase<BorderPropagationDataHandle>(indexSet)
1041 {}
1042
1043 };
1044
1045
1046 void communicateSubDomainSelection()
1047 {
1048 SelectionDataHandle dh(*this);
1049 _hostGridView.template communicate<SelectionDataHandle>(dh,Dune::InteriorBorder_All_Interface,Dune::ForwardCommunication);
1050 }
1051
1052 void propagateBorderEntitySubDomains()
1053 {
1054 BorderPropagationDataHandle dh(*this);
1055 _hostGridView.communicate(dh,Dune::InteriorBorder_All_Interface,Dune::ForwardCommunication);
1056 }
1057
1058};
1059
1060} // namespace mdgrid
1061
1062} // namespace Dune
1063
1064#endif // DUNE_MULTIDOMAINGRID_INDEXSETS_HH
Definition: indexsets.hh:236
IndexType size(GeometryType type) const
Returns the number of entities with GeometryType type in the grid.
Definition: indexsets.hh:397
const MapEntry< EntityType::codimension >::SubDomainSet & subDomains(const EntityType &e) const
Returns a constant reference to the SubDomainSet of the given entity.
Definition: indexsets.hh:414
IndexType index(const typename Grid::Traits::template Codim< codim >::Entity &e) const
Returns the index of the entity with codimension codim.
Definition: indexsets.hh:368
IndexType index(const Entity &e) const
Returns the index of the entity.
Definition: indexsets.hh:374
IndexType index(SubDomainIndex subDomain, const typename Grid::Traits::template Codim< cc >::Entity &e) const
Definition: indexsets.hh:434
IndexType subIndex(const Entity &e, int i, unsigned int codim) const
Returns the subdindex of the i-th subentity of e with codimension codim.
Definition: indexsets.hh:386
IndexType index(SubDomainIndex subDomain, const EntityType &e) const
Returns the index of the entity in a specific subdomain.
Definition: indexsets.hh:427
Types types(int codim) const
Returns a list of all geometry types with codimension codim contained in the grid.
Definition: indexsets.hh:392
const MapEntry< cc >::SubDomainSet & subDomains(const typename Grid::Traits::template Codim< cc >::Entity &e) const
Definition: indexsets.hh:421
bool contains(const EntityType &e) const
Returns true if the entity is contained in the grid.
Definition: indexsets.hh:408
IndexType size(int codim) const
Returns the number of entities with codimension codim in the grid.
Definition: indexsets.hh:402
IndexType subIndex(const Entity &e, int i) const
Returns the subdindex of the i-th subentity of e with codimension codim.
Definition: indexsets.hh:380
bool contains(SubDomainIndex subDomain, const EntityType &e) const
Returns true if the entity is contained in a specific subdomain.
Definition: indexsets.hh:623
A meta grid for dividing an existing DUNE grid into subdomains that can be accessed as a grid in thei...
Definition: multidomaingrid.hh:241
An intersection that forms part of the interface between two subdomains.
Definition: subdomaininterfaceiterator.hh:32
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Apr 10, 22:40, 2025)