1#ifndef DUNE_PDELAB_COMMON_PARTITIONVIEWENTITYSET_HH
2#define DUNE_PDELAB_COMMON_PARTITIONVIEWENTITYSET_HH
13#include <dune/common/iteratorrange.hh>
14#include <dune/geometry/referenceelements.hh>
17#include <dune/grid/common/partitionset.hh>
18#include <dune/grid/common/rangegenerators.hh>
30 using Dune::descendantElements;
31 using Dune::intersections;
33 template<
typename GV,
typename P>
34 class PartitionViewEntitySet;
36 template<
typename GV,
typename P>
37 class PartitionViewEntitySetIndexSet;
39 template<
typename GV,
typename P>
40 struct PartitionViewEntitySetTraits
43 using Partitions =
typename std::decay<P>::type;
45 using Grid =
typename GV::Traits::Grid;
48 using IndexSet = PartitionViewEntitySetIndexSet<GV,Partitions>;
49 using BaseIndexSet =
typename GV::Traits::IndexSet;
51 using Element =
typename GV::template Codim<0>::Entity;
53 using Intersection =
typename GV::Traits::Intersection;
55 using IntersectionIterator =
typename GV::Traits::IntersectionIterator;
57 using CollectiveCommunication =
typename GV::Traits::CollectiveCommunication;
59 using size_type = std::size_t;
62 using Index =
typename BaseIndexSet::IndexType;
66 using CodimMask = std::bitset<GV::dimension + 1>;
70 constexpr static Index invalidIndex()
72 return ~static_cast<Index>(0ull);
75 static const bool conforming = GV::Traits::conforming;
77 static const dim_type dimension = GV::dimension;
79 static const dim_type dimensionworld = GV::dimensionworld;
81 template<dim_type codim>
85 using Iterator =
typename GV::template Codim<codim>::template Partition<Partitions::partitionIterator()>::Iterator;
87 using Entity =
typename GV::template Codim<codim>::Entity;
89 using Geometry =
typename GV::template Codim<codim>::Geometry;
91 using LocalGeometry =
typename GV::template Codim<codim>::LocalGeometry;
93 template<PartitionIteratorType pitype>
97 using Iterator =
typename GV::template Codim<codim>::template Partition<pitype>::Iterator;
118 template<
typename GV,
typename P>
124 using Traits = PartitionViewEntitySetTraits<GV,P>;
126 using Partitions =
typename Traits::Partitions;
127 using Grid =
typename Traits::Grid;
128 using GridView =
typename Traits::GridView;
129 using IndexSet =
typename Traits::IndexSet;
130 using BaseIndexSet =
typename Traits::BaseIndexSet;
131 using Element =
typename Traits::Element;
132 using Intersection =
typename Traits::Intersection;
133 using IntersectionIterator =
typename Traits::IntersectionIterator;
134 using CollectiveCommunication =
typename Traits::CollectiveCommunication;
135 using CodimMask =
typename Traits::CodimMask;
136 using CoordinateField =
typename Traits::CoordinateField;
137 using size_type =
typename Traits::size_type;
138 using dim_type =
typename Traits::dim_type;
140 using ctype = CoordinateField;
142 static const bool conforming = Traits::conforming;
143 static const dim_type dimension = Traits::dimension;
144 static const dim_type dimensionworld = Traits::dimensionworld;
146 template<dim_type codim>
147 using Codim =
typename Traits::template Codim<codim>;
149 constexpr static Partitions partitions()
154 constexpr static CodimMask allCodims()
159 const Grid& grid()
const
176 template<dim_type codim>
180 return gridView().template begin<codim,Partitions::partitionIterator()>();
183 template<dim_type codim>
187 return gridView().template end<codim,Partitions::partitionIterator()>();
190 template<dim_type codim, PartitionIteratorType pitype>
191 typename GV::template Codim<codim>::template Partition<pitype>::Iterator
194 return gridView().template begin<codim,pitype>();
197 template<dim_type codim, PartitionIteratorType pitype>
198 typename GV::template Codim<codim>::template Partition<pitype>::Iterator
201 return gridView().template end<codim,pitype>();
204 size_type size(dim_type codim)
const
209 size_type size(
const GeometryType&
gt)
const
214 template<
typename Entity>
215 bool contains(
const Entity& e)
const
220 bool contains(dim_type codim)
const
225 bool contains(
const GeometryType&
gt)
const
230 IntersectionIterator ibegin(
const typename Codim<0>::Entity& entity)
const
235 IntersectionIterator iend(
const typename Codim<0>::Entity& entity)
const
240 const CollectiveCommunication& comm()
const
257 template<
typename DataHandle>
260 gridView().communicate(data,iftype,dir);
270 : _index_set(
std::make_shared<
IndexSet>(gv,supported_codims,true))
273 explicit PartitionViewEntitySet(
const GridView& gv,
bool initialize =
true)
274 : _index_set(
std::make_shared<
IndexSet>(gv,CodimMask(initialize ? ~0ull : 0ull),initialize))
286 _index_set->addCodim(codim);
292 _index_set->removeCodim(codim);
298 return _index_set->needsUpdate();
312 return _index_set->update(force);
324 return _index_set == other._index_set;
329 return not (*
this == other);
334 std::shared_ptr<IndexSet> _index_set;
338 template<
typename GV,
typename P>
339 class PartitionViewEntitySetIndexSetBase
342 template<
typename,
typename>
343 friend class PartitionViewEntitySet;
347 using Traits = PartitionViewEntitySetTraits<GV,P>;
349 using Partitions =
typename Traits::Partitions;
350 using Grid =
typename Traits::Grid;
351 using GridView =
typename Traits::GridView;
352 using BaseIndexSet =
typename Traits::BaseIndexSet;
353 using size_type =
typename Traits::size_type;
354 using dim_type =
typename Traits::dim_type;
355 using Index =
typename Traits::Index;
356 using Types =
typename Traits::Types;
357 using CodimMask =
typename Traits::CodimMask;
359 using IndexType = Index;
361 constexpr static Index invalidIndex()
363 return Traits::invalidIndex();
366 template<dim_type codim>
367 using Codim =
typename Traits::template Codim<codim>;
369 PartitionViewEntitySetIndexSetBase(
const PartitionViewEntitySetIndexSetBase&) =
delete;
370 PartitionViewEntitySetIndexSetBase& operator=(
const PartitionViewEntitySetIndexSetBase&) =
delete;
375 bool update(
bool force)
377 if (!(_needs_update || force))
379 std::fill(_gt_offsets.begin(),_gt_offsets.end(),0);
380 std::fill(_mapped_gt_offsets.begin(),_mapped_gt_offsets.end(),0);
381 _active_geometry_types.reset();
382 _geometry_types.resize(0);
383 for (dim_type codim = 0; codim <= GV::dimension; ++codim)
385 if (!_wanted_codims.test(codim))
387 for (
const auto&
gt : baseIndexSet().types(codim))
390 _gt_offsets[gt_index + 1] = baseIndexSet().size(
gt);
391 _geometry_types.push_back(
gt);
392 _active_geometry_types.set(gt_index);
395 for (dim_type codim = 0; codim <= GV::dimension; ++codim)
397 auto range = std::equal_range(
398 _geometry_types.begin(),
399 _geometry_types.end(),
401 [](
const GeometryType& x,
const GeometryType& y)
404 return y.dim() < x.dim();
406 _per_codim_geometry_types[codim] = {range.first,range.second};
409 std::partial_sum(_gt_offsets.begin(),_gt_offsets.end(),_gt_offsets.begin());
410 _active_codims = _wanted_codims;
411 _needs_update =
false;
417 size_type size(GeometryType
gt)
const
419 assert(!needsUpdate());
421 return _mapped_gt_offsets[gt_index + 1] - _mapped_gt_offsets[gt_index];
424 size_type size(dim_type codim)
const
426 assert(!needsUpdate());
427 auto dim = GV::dimension;
432 template<
typename Entity>
433 bool contains(
const Entity& e)
const
435 return Partitions::contains(e.partitionType()) ? baseIndexSet().contains(e) :
false;
438 bool contains(dim_type codim)
const
440 return _active_codims.test(codim);
443 bool contains(
const GeometryType&
gt)
const
448 const BaseIndexSet& baseIndexSet()
const
450 return _gv.indexSet();
453 Types types(dim_type codim)
const
455 assert(!needsUpdate());
456 return _per_codim_geometry_types[codim];
461 assert(!needsUpdate());
462 return {_geometry_types.begin(),_geometry_types.end()};
465 PartitionViewEntitySetIndexSetBase(
const GV& gv, CodimMask wanted_codims)
467 , _needs_update(true)
468 , _wanted_codims(wanted_codims)
471 const GridView& gridView()
const
476 bool needsUpdate()
const
478 return _needs_update;
485 _needs_update =
true;
486 _wanted_codims.reset();
489 void addCodim(dim_type codim)
491 _wanted_codims.set(codim);
492 _needs_update = _wanted_codims != _active_codims || _wanted_codims.none();
495 void removeCodim(dim_type codim)
497 _wanted_codims.reset(codim);
498 _needs_update = _wanted_codims != _active_codims || _wanted_codims.none();
503 CodimMask _wanted_codims;
505 CodimMask _active_codims;
511 std::vector<GeometryType> _geometry_types;
512 std::array<Types,GV::dimension + 1> _per_codim_geometry_types;
516 template<
typename GV,
typename P>
517 class PartitionViewEntitySetIndexSet
518 :
public PartitionViewEntitySetIndexSetBase<GV,P>
521 using Base = PartitionViewEntitySetIndexSetBase<GV,P>;
523 template<
typename,
typename>
524 friend class PartitionViewEntitySet;
528 using typename Base::Index;
529 using typename Base::Partitions;
530 using typename Base::size_type;
531 using typename Base::dim_type;
533 using typename Base::Grid;
535 using Base::gridView;
536 using Base::baseIndexSet;
537 using Base::invalidIndex;
538 using Base::contains;
539 using typename Base::CodimMask;
540 using Base::needsUpdate;
549 template<dim_type dim = 0>
552 return Capabilities::hasEntityIterator<Grid,dim>::v && hasAllEntityIterators(
Dune::Dim<dim+1>{});
555 bool update(
bool force)
557 if (!Base::update(force))
559 _indices.assign(_gt_offsets.back(),invalidIndex());
560 _mapped_gt_offsets[0] = 0;
561 update_codims(std::integral_constant<
bool,hasAllEntityIterators()>{});
562 std::partial_sum(_mapped_gt_offsets.begin(),_mapped_gt_offsets.end(),_mapped_gt_offsets.begin());
566 void update_codims(std::true_type)
574 template<dim_type cd>
577 if (_active_codims.test(codim))
582 if (Partitions::contains(e.partitionType()))
583 _indices[_gt_offsets[gt_index] + baseIndexSet().index(e)] = _mapped_gt_offsets[gt_index + 1]++;
589 void update_codims(std::false_type)
591 std::fill(_indices.begin(),_indices.end(),invalidIndex());
593 auto& index_set = baseIndexSet();
597 if (!Partitions::contains(e.partitionType()))
603 if (!_active_codims.test(codim))
606 size_type sub_entity_count = ref_el.size(codim);
608 for(size_type i = 0; i < sub_entity_count; ++i)
610 auto gt = ref_el.type(i,codim);
612 auto index = index_set.subIndex(e,i,codim);
613 if (_indices[_gt_offsets[gt_index] + index] == invalidIndex())
614 _indices[_gt_offsets[gt_index] + index] = _mapped_gt_offsets[gt_index + 1]++;
624 Index index(
const E& e)
const
626 assert(!needsUpdate());
627 assert(Partitions::contains(e.partitionType()));
628 assert(contains(e.type()));
630 return _indices[_gt_offsets[gt_index] + baseIndexSet().index(e)];
634 Index subIndex(
const E& e, size_type i, dim_type codim)
const
636 assert(!needsUpdate());
637 assert(Partitions::contains(e.partitionType()));
639 assert(contains(
gt));
641 return _indices[_gt_offsets[gt_index] + baseIndexSet().subIndex(e,i,codim)];
646 Index uniqueIndex(
const E& e)
const
648 assert(!needsUpdate());
649 assert(Partitions::contains(e.partitionType()));
650 assert(contains(e.type()));
652 return _indices[_gt_offsets[gt_index] + baseIndexSet().index(e)] + _mapped_gt_offsets[gt_index];
656 Index uniqueSubIndex(
const E& e, size_type i, dim_type codim)
const
658 assert(!needsUpdate());
659 assert(Partitions::contains(e.partitionType()));
661 assert(contains(
gt));
663 return _indices[_gt_offsets[gt_index] + baseIndexSet().subIndex(e,i,codim)] + _mapped_gt_offsets[gt_index];
667 PartitionViewEntitySetIndexSet(
const GV& gv, CodimMask wanted_codims,
bool initialize)
668 : Base(gv,wanted_codims)
676 using Base::_active_codims;
677 using Base::_gt_offsets;
678 using Base::_mapped_gt_offsets;
680 std::vector<Index> _indices;
684 template<
typename GV>
685 class PartitionViewEntitySetIndexSet<GV,Partitions::
All>
686 :
public PartitionViewEntitySetIndexSetBase<GV,Partitions::All>
689 using Base = PartitionViewEntitySetIndexSetBase<GV,Dune::Partitions::All>;
691 template<
typename,
typename>
696 using typename Base::Index;
697 using typename Base::Partitions;
698 using typename Base::size_type;
699 using typename Base::dim_type;
700 using typename Base::CodimMask;
702 using Base::baseIndexSet;
703 using Base::contains;
709 if (!Base::update(force))
711 _mapped_gt_offsets[0] = 0;
712 for (
const auto&
gt : Base::types())
714 std::partial_sum(_mapped_gt_offsets.begin(),_mapped_gt_offsets.end(),_mapped_gt_offsets.begin());
721 Index index(
const E& e)
const
723 assert(contains(e.type()));
728 Index uniqueIndex(
const E& e)
const
730 assert(contains(e.type()));
735 Index subIndex(
const E& e, size_type i, dim_type codim)
const
739 assert(contains(
gt));
745 Index uniqueSubIndex(
const E& e, size_type i, dim_type codim)
const
748 assert(contains(
gt));
752 PartitionViewEntitySetIndexSet(
const GV& gv, CodimMask wanted_codims,
bool initialize =
true)
753 : Base(gv,wanted_codims)
761 using Base::_mapped_gt_offsets;
765 template<
typename GV>
766 using AllEntitySet = PartitionViewEntitySet<GV,Partitions::All>;
768 template<
typename GV>
769 using OverlappingEntitySet = PartitionViewEntitySet<GV,Partitions::InteriorBorderOverlapFront>;
771 template<
typename GV>
772 using NonOverlappingEntitySet = PartitionViewEntitySet<GV,Partitions::InteriorBorder>;
781 using type = std::false_type;
784 template<
typename GV,
typename P>
785 struct _isEntitySet<PartitionViewEntitySet<GV,P>>
787 using type = std::true_type;
796 using isEntitySet =
typename impl::_isEntitySet<T>::type;
static constexpr std::size_t index(const GeometryType >)
Compute the index for the given geometry type over all dimensions.
Definition: typeindex.hh:136
static constexpr std::size_t offset(std::size_t dim)
Compute the starting index for a given dimension including irregular geometry types.
Definition: typeindex.hh:111
static constexpr std::size_t size(std::size_t maxdim)
Compute total number of geometry types up to and including the given dimension.
Definition: typeindex.hh:123
Grid view abstract base class.
Definition: gridview.hh:63
Grid abstract base class.
Definition: grid.hh:372
@ dimension
The dimension of the grid.
Definition: grid.hh:386
ct ctype
Define type used for coordinates in grid module.
Definition: grid.hh:521
Index Set Interface base class.
Definition: indexidset.hh:76
Iterator const_iterator
The iterator belonging to this range.
Definition: iteratorrange.hh:31
Partition view (or entity set) of a grid view.
Definition: partitionviewentityset.hh:120
const IndexSet & indexSet() const
Returns the IndexSet of this EntitySet.
Definition: partitionviewentityset.hh:165
void reset()
Reset this EntitySet, which removes all entities from it.
Definition: partitionviewentityset.hh:278
bool operator==(const PartitionViewEntitySet &other) const
Compare to another partition view.
Definition: partitionviewentityset.hh:323
bool update(bool force=false)
Update the internal state of this EntitySet.
Definition: partitionviewentityset.hh:310
const GridView & gridView() const
Returns the underlying GridView.
Definition: partitionviewentityset.hh:264
bool operator!=(const PartitionViewEntitySet &other) const
Compare to another partition view.
Definition: partitionviewentityset.hh:328
void removeCodim(dim_type codim)
Remove all entities of the given codim from this EntitySet.
Definition: partitionviewentityset.hh:290
size_type overlapSize(dim_type codim) const
Returns the overlap size of this EntitySet, which depends on its PartitionSet.
Definition: partitionviewentityset.hh:246
const BaseIndexSet & baseIndexSet() const
Returns the IndexSet of the underlying GridView.
Definition: partitionviewentityset.hh:171
bool needsUpdate() const
Returns true if you need to call update on this EntitySet before using it.
Definition: partitionviewentityset.hh:296
void addCodim(dim_type codim)
Add all entities of the given codim to this EntitySet.
Definition: partitionviewentityset.hh:284
size_type ghostSize(dim_type codim) const
Returns the ghost size of this EntitySet, which depends on its PartitionSet.
Definition: partitionviewentityset.hh:252
A set of traits classes to store static information about grid implementation.
@ conforming
Output conforming data.
Definition: common.hh:71
bool gt(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first greater than second
Definition: float_cmp.cc:156
static constexpr bool isEntitySet()
Check if F models the GridFunction concept with given signature and entity set.
Definition: functionconcepts.hh:230
constexpr GeometryType none(unsigned int dim)
Returns a GeometryType representing a singular of dimension dim.
Definition: type.hh:479
constexpr Overlap overlap
PartitionSet for the overlap partition.
Definition: partitionset.hh:276
PartitionSet<... > All
Type of PartitionSet for all partitions.
Definition: partitionset.hh:266
constexpr All all
PartitionSet for all partitions.
Definition: partitionset.hh:294
constexpr Ghost ghost
PartitionSet for the ghost partition.
Definition: partitionset.hh:282
Dune namespace.
Definition: alignedallocator.hh:11
Static tag representing a codimension.
Definition: dimension.hh:22
Static tag representing a dimension.
Definition: dimension.hh:14
static const ReferenceElement & general(const GeometryType &type)
get general reference elements
Definition: referenceelements.hh:196
Helper classes to provide indices for geometrytypes for use in a vector.
Various macros to work with Dune module version numbers.