1 #ifndef DUNE_PDELAB_COMMON_PARTITIONVIEWENTITYSET_HH
2 #define DUNE_PDELAB_COMMON_PARTITIONVIEWENTITYSET_HH
10 #include <type_traits>
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;
48 using IndexSet = PartitionViewEntitySetIndexSet<GV,Partitions>;
57 using Communication =
typename GV::Traits::Communication;
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;
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;
130 using BaseIndexSet =
typename Traits::BaseIndexSet;
131 using Element =
typename Traits::Element;
134 using Communication =
typename Traits::Communication;
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
240 const Communication& 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;
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 static const dim_type dimension = Traits::dimension;
363 constexpr
static Index invalidIndex()
365 return Traits::invalidIndex();
368 template<dim_type codim>
369 using Codim =
typename Traits::template Codim<codim>;
371 PartitionViewEntitySetIndexSetBase(
const PartitionViewEntitySetIndexSetBase&) =
delete;
372 PartitionViewEntitySetIndexSetBase& operator=(
const PartitionViewEntitySetIndexSetBase&) =
delete;
377 bool update(
bool force)
379 if (!(_needs_update || force))
381 std::fill(_gt_offsets.begin(),_gt_offsets.end(),0);
382 std::fill(_mapped_gt_offsets.begin(),_mapped_gt_offsets.end(),0);
383 _active_geometry_types.reset();
384 _geometry_types.resize(0);
385 for (dim_type codim = 0; codim <= GV::dimension; ++codim)
387 if (!_wanted_codims.test(codim))
389 for (
const auto&
gt : baseIndexSet().types(codim))
392 _gt_offsets[gt_index + 1] = baseIndexSet().size(
gt);
393 _geometry_types.push_back(
gt);
394 _active_geometry_types.set(gt_index);
397 for (dim_type codim = 0; codim <= GV::dimension; ++codim)
399 auto range = std::equal_range(
400 _geometry_types.begin(),
401 _geometry_types.end(),
403 [](
const GeometryType& x,
const GeometryType& y)
406 return y.dim() < x.dim();
408 _per_codim_geometry_types[codim] = {range.first,range.second};
411 std::partial_sum(_gt_offsets.begin(),_gt_offsets.end(),_gt_offsets.begin());
412 _active_codims = _wanted_codims;
413 _needs_update =
false;
419 size_type
size(GeometryType
gt)
const
421 assert(!needsUpdate());
423 return _mapped_gt_offsets[gt_index + 1] - _mapped_gt_offsets[gt_index];
426 size_type
size(dim_type codim)
const
428 assert(!needsUpdate());
429 auto dim = GV::dimension;
434 template<
typename Entity>
437 return Partitions::contains(e.partitionType()) ? baseIndexSet().contains(e) :
false;
442 return _active_codims.test(codim);
450 const BaseIndexSet& baseIndexSet()
const
452 return _gv.indexSet();
455 Types types(dim_type codim)
const
457 assert(!needsUpdate());
458 return _per_codim_geometry_types[codim];
463 assert(!needsUpdate());
464 return {_geometry_types.begin(),_geometry_types.end()};
467 PartitionViewEntitySetIndexSetBase(
const GV& gv, CodimMask wanted_codims)
469 , _needs_update(true)
470 , _wanted_codims(wanted_codims)
478 bool needsUpdate()
const
480 return _needs_update;
487 _needs_update =
true;
488 _wanted_codims.reset();
491 void addCodim(dim_type codim)
493 _wanted_codims.set(codim);
494 _needs_update = _wanted_codims != _active_codims || _wanted_codims.none();
497 void removeCodim(dim_type codim)
499 _wanted_codims.reset(codim);
500 _needs_update = _wanted_codims != _active_codims || _wanted_codims.none();
505 CodimMask _wanted_codims;
507 CodimMask _active_codims;
513 std::vector<GeometryType> _geometry_types;
514 std::array<Types,GV::dimension + 1> _per_codim_geometry_types;
518 template<
typename GV,
typename P>
519 class PartitionViewEntitySetIndexSet
520 :
public PartitionViewEntitySetIndexSetBase<GV,P>
523 using Base = PartitionViewEntitySetIndexSetBase<GV,P>;
525 template<
typename,
typename>
526 friend class PartitionViewEntitySet;
530 using typename Base::Traits;
531 using typename Base::Index;
532 using typename Base::Partitions;
533 using typename Base::size_type;
534 using typename Base::dim_type;
538 using Base::gridView;
539 using Base::baseIndexSet;
540 using Base::invalidIndex;
541 using Base::contains;
542 using typename Base::CodimMask;
543 using Base::needsUpdate;
552 template<dim_type dim = 0>
555 return Capabilities::hasEntityIterator<Grid,dim>::v && hasAllEntityIterators(
Dune::Dim<dim+1>{});
558 bool update(
bool force)
560 if (!Base::update(force))
562 _indices.assign(_gt_offsets.back(),invalidIndex());
563 _mapped_gt_offsets[0] = 0;
564 update_codims(std::integral_constant<
bool,hasAllEntityIterators()>{});
565 std::partial_sum(_mapped_gt_offsets.begin(),_mapped_gt_offsets.end(),_mapped_gt_offsets.begin());
569 void update_codims(std::true_type)
577 template<dim_type cd>
580 if (_active_codims.test(codim))
585 if (Partitions::contains(e.partitionType()))
586 _indices[_gt_offsets[gt_index] + baseIndexSet().index(e)] = _mapped_gt_offsets[gt_index + 1]++;
592 void update_codims(std::false_type)
594 std::fill(_indices.begin(),_indices.end(),invalidIndex());
596 auto& index_set = baseIndexSet();
600 if (!Partitions::contains(e.partitionType()))
606 if (!_active_codims.test(codim))
609 size_type sub_entity_count = ref_el.size(codim);
611 for(size_type i = 0; i < sub_entity_count; ++i)
613 auto gt = ref_el.type(i,codim);
615 auto index = index_set.subIndex(e,i,codim);
616 if (_indices[_gt_offsets[gt_index] + index] == invalidIndex())
617 _indices[_gt_offsets[gt_index] + index] = _mapped_gt_offsets[gt_index + 1]++;
623 template<
class Entity>
624 Index indexImpl(
const Entity& e)
const
626 assert(!needsUpdate());
627 assert(Partitions::contains(e.partitionType()));
630 return _indices[_gt_offsets[gt_index] + baseIndexSet().index(e)];
634 Index subIndexImpl(
const E& e, size_type i, dim_type codim)
const
636 assert(!needsUpdate());
637 assert(Partitions::contains(e.partitionType()));
641 return _indices[_gt_offsets[gt_index] + baseIndexSet().subIndex(e,i,codim)];
651 template<
class Entity>
652 Index index(
const Entity& e)
const {
657 Index subIndex(
const typename Traits::template
Codim<cc>::Entity& e, size_type i, dim_type codim)
const {
658 return subIndexImpl(e,i,codim);
663 Index subIndex(
const E& e, size_type i, dim_type codim)
const {
664 return subIndexImpl(e,i,codim);
668 Index uniqueIndex(
const E& e)
const
670 assert(!needsUpdate());
671 assert(Partitions::contains(e.partitionType()));
674 return _indices[_gt_offsets[gt_index] + baseIndexSet().index(e)] + _mapped_gt_offsets[gt_index];
678 Index uniqueSubIndex(
const E& e, size_type i, dim_type codim)
const
680 assert(!needsUpdate());
681 assert(Partitions::contains(e.partitionType()));
685 return _indices[_gt_offsets[gt_index] + baseIndexSet().subIndex(e,i,codim)] + _mapped_gt_offsets[gt_index];
689 PartitionViewEntitySetIndexSet(
const GV& gv, CodimMask wanted_codims,
bool initialize)
690 : Base(gv,wanted_codims)
698 using Base::_active_codims;
699 using Base::_gt_offsets;
700 using Base::_mapped_gt_offsets;
702 std::vector<Index> _indices;
706 template<
typename GV>
707 class PartitionViewEntitySetIndexSet<GV,Partitions::
All>
708 :
public PartitionViewEntitySetIndexSetBase<GV,Partitions::All>
711 using Base = PartitionViewEntitySetIndexSetBase<GV,Dune::Partitions::All>;
713 template<
typename,
typename>
718 using typename Base::Traits;
719 using typename Base::Index;
720 using typename Base::Partitions;
721 using typename Base::size_type;
722 using typename Base::dim_type;
723 using typename Base::CodimMask;
725 using Base::baseIndexSet;
726 using Base::contains;
732 if (!Base::update(force))
734 _mapped_gt_offsets[0] = 0;
735 for (
const auto&
gt : Base::types())
737 std::partial_sum(_mapped_gt_offsets.begin(),_mapped_gt_offsets.end(),_mapped_gt_offsets.begin());
742 Index subIndexImpl(
const E& e, size_type i, dim_type codim)
const
754 Index index(
const E& e)
const
768 Index uniqueIndex(
const E& e)
const
776 Index subIndex(
const typename Traits::template
Codim<cc>::Entity& e, size_type i, dim_type codim)
const {
777 return subIndexImpl(e,i,codim);
781 Index subIndex(
const E& e, size_type i, dim_type codim)
const {
782 return subIndexImpl(e,i,codim);
786 Index uniqueSubIndex(
const E& e, size_type i, dim_type codim)
const
793 PartitionViewEntitySetIndexSet(
const GV& gv, CodimMask wanted_codims,
bool initialize =
true)
794 : Base(gv,wanted_codims)
802 using Base::_mapped_gt_offsets;
806 template<
typename GV>
807 using AllEntitySet = PartitionViewEntitySet<GV,Partitions::All>;
809 template<
typename GV>
810 using OverlappingEntitySet = PartitionViewEntitySet<GV,Partitions::InteriorBorderOverlapFront>;
812 template<
typename GV>
813 using NonOverlappingEntitySet = PartitionViewEntitySet<GV,Partitions::InteriorBorder>;
822 using type = std::false_type;
825 template<
typename GV,
typename P>
826 struct _isEntitySet<PartitionViewEntitySet<GV,P>>
828 using type = std::true_type;
837 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:138
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:113
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:125
Grid view abstract base class.
Definition: gridview.hh:66
Grid abstract base class.
Definition: grid.hh:375
constexpr static int dimension
The dimension of the grid.
Definition: grid.hh:387
ct ctype
Define type used for coordinates in grid module.
Definition: grid.hh:518
Index Set Interface base class.
Definition: indexidset.hh:78
Iterator const_iterator
The iterator belonging to this range.
Definition: iteratorrange.hh:33
Partition view (or entity set) of a grid view.
Definition: partitionviewentityset.hh:120
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
const IndexSet & indexSet() const
Returns the IndexSet of this EntitySet.
Definition: partitionviewentityset.hh:165
bool update(bool force=false)
Update the internal state of this EntitySet.
Definition: partitionviewentityset.hh:310
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
bool needsUpdate() const
Returns true if you need to call update on this EntitySet before using it.
Definition: partitionviewentityset.hh:296
const GridView & gridView() const
Returns the underlying GridView.
Definition: partitionviewentityset.hh:264
void addCodim(dim_type codim)
Add all entities of the given codim to this EntitySet.
Definition: partitionviewentityset.hh:284
const BaseIndexSet & baseIndexSet() const
Returns the IndexSet of the underlying GridView.
Definition: partitionviewentityset.hh:171
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:73
bool gt(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first greater than second
Definition: float_cmp.cc:158
static constexpr bool isEntitySet()
Check if F models the GridFunction concept with given signature and entity set.
Definition: functionconcepts.hh:231
constexpr GeometryType none(unsigned int dim)
Returns a GeometryType representing a singular of dimension dim.
Definition: type.hh:471
concept Intersection
Model of an intersection.
Definition: intersection.hh:23
concept GridView
Model of a grid view.
Definition: gridview.hh:81
concept Grid
Requirements for implementations of the Dune::Grid interface.
Definition: grid.hh:98
concept Entity
Model of a grid entity.
Definition: entity.hh:107
concept IndexSet
Model of an index set.
Definition: indexidset.hh:44
concept IntersectionIterator
Model of an intersection iterator.
Definition: intersectioniterator.hh:21
concept Geometry
Model of a geometry object.
Definition: geometry.hh:29
constexpr Overlap overlap
PartitionSet for the overlap partition.
Definition: partitionset.hh:277
PartitionSet<... > All
Type of PartitionSet for all partitions.
Definition: partitionset.hh:267
constexpr All all
PartitionSet for all partitions.
Definition: partitionset.hh:295
constexpr Ghost ghost
PartitionSet for the ghost partition.
Definition: partitionset.hh:283
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::bool_constant<((II==value)||...)> contains(std::integer_sequence< T, II... >, std::integral_constant< T, value >)
Checks whether or not a given sequence contains a value.
Definition: integersequence.hh:137
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
Static tag representing a codimension.
Definition: dimension.hh:24
Static tag representing a dimension.
Definition: dimension.hh:16
static const ReferenceElement & general(const GeometryType &type)
get general reference elements
Definition: referenceelements.hh:156
Helper classes to provide indices for geometrytypes for use in a vector.
Various macros to work with Dune module version numbers.