diff --git a/dune/subgrid/subgrid/subgridindexstorage.hh b/dune/subgrid/subgrid/subgridindexstorage.hh index 9ff1e5d581b88512497ee47e2ade9dd08acbd0cf..26d100dfdea5f2a299a39f281586c1cb7ca33077 100644 --- a/dune/subgrid/subgrid/subgridindexstorage.hh +++ b/dune/subgrid/subgrid/subgridindexstorage.hh @@ -73,23 +73,26 @@ class SubGridIndexStorageBase template <class GridImp_, int EntityDim> friend class SubGridMapSubindexSetter; + using ctype = typename GridType::ctype; public: + template <int codim> + using GridEntity = typename std::remove_const<GridType>::type::template Codim<codim>::Entity; typedef typename std::remove_const<GridType>::type::HostGridType HostGridType; typedef typename HostGridType::Traits::GlobalIdSet HostIdSet; typedef typename HostIdSet::IdType HostIdType; //! type map GeometryTypes to MultilevelCounters - typedef typename std::map<GeometryType, MultilevelCounter> EntityCounter; + typedef std::map<GeometryType, MultilevelCounter> EntityCounter; enum {dim = std::remove_const<GridType>::type::dimension}; // The codimension of this level iterator wrt the host grid enum {CodimInHostGrid = HostGridType::dimension - dim}; - /** \brief Virtual constructor */ - virtual ~SubGridIndexStorageBase() {} + /** \brief Virtual constructor */ + virtual ~SubGridIndexStorageBase() {} //! This constructor initializes the grid and hostgrid references SubGridIndexStorageBase (const GridType& _grid) : @@ -102,8 +105,8 @@ class SubGridIndexStorageBase int levelSize (int codim, int level) const { unsigned int sum = 0; - for (size_t i=0; i<levelTypes[dim-codim][level].size(); i++) - sum += numEntities.find(levelTypes[dim-codim][level][i])->second.level[level]; + for (const auto& type : levelTypes[dim-codim][level]) + sum += numEntities.find(type)->second.level[level]; return sum; } @@ -111,7 +114,7 @@ class SubGridIndexStorageBase //! get number of entities of given type and and level int levelSize (GeometryType type, int level) const { - EntityCounter::const_iterator it = numEntities.find(type); + const auto it = numEntities.find(type); return (it==numEntities.end()) ? 0 : it->second.level[level]; } @@ -127,8 +130,8 @@ class SubGridIndexStorageBase int leafSize (int codim) const { unsigned int sum = 0; - for (size_t i=0; i<leafTypes[dim-codim].size(); i++) - sum += numEntities.find(leafTypes[dim-codim][i])->second.leaf; + for (const auto& type : leafTypes[dim-codim]) + sum += numEntities.find(type)->second.leaf; return sum; } @@ -136,7 +139,7 @@ class SubGridIndexStorageBase //! get number of entities of given type and leaf level int leafSize (const GeometryType& type) const { - EntityCounter::const_iterator it = numEntities.find(type); + const auto it = numEntities.find(type); return (it==numEntities.end()) ? 0 : it->second.leaf; } @@ -150,9 +153,7 @@ class SubGridIndexStorageBase //! print information about stored indices virtual void report() const - { - return; - } + {} protected: @@ -166,19 +167,15 @@ class SubGridIndexStorageBase levelTypes[i].resize(grid.maxLevel()+1); } - EntityCounter::iterator it = numEntities.begin(); - for (; it!=numEntities.end(); ++it) + for (const auto& it : numEntities) { - if (it->second.leaf > 0) - leafTypes[it->first.dim()].push_back(it->first); + if (it.second.leaf > 0) + leafTypes[it.first.dim()].push_back(it.first); - for (size_t level=0; level<it->second.level.size(); ++level) - { - if (it->second.level[level] > 0) - levelTypes[it->first.dim()][level].push_back(it->first); - } + for (size_t level=0; level < it.second.level.size(); ++level) + if (it.second.level[level] > 0) + levelTypes[it.first.dim()][level].push_back(it.first); } - return; } @@ -186,14 +183,14 @@ class SubGridIndexStorageBase MultilevelCounter& getGeometryTypeCountForLevel(const GeometryType& gt, int level DUNE_UNUSED) { // Is this the first time we see this kind of entity? - EntityCounter::iterator countIt = numEntities.find(gt); - if (countIt==numEntities.end()) + auto countIt = numEntities.find(gt); + if (countIt == numEntities.end()) { MultilevelCounter newGeometryType; newGeometryType.leaf = 0; newGeometryType.level.resize(grid.maxLevel()+1, 0); - std::pair<EntityCounter::iterator, bool> p = numEntities.insert(std::make_pair(gt, newGeometryType)); + auto p = numEntities.insert(std::make_pair(gt, newGeometryType)); return p.first->second; } return countIt->second; @@ -207,15 +204,15 @@ class SubGridIndexStorageBase //! get geometry type of given subentity template <int cc> - GeometryType getSubGeometryType(const typename std::remove_const<GridType>::type::template Codim<cc>::Entity& e, int i, unsigned int codim) const + GeometryType getSubGeometryType(const GridEntity<cc>& e, int i, unsigned int codim) const { - return Dune::ReferenceElements<double,dim>::general(e.type()).type(i,codim); + return Dune::ReferenceElements<ctype,dim>::general(e.type()).type(i,codim); } //! get level index of host grid entity encapsulated in given entity template <int codim> - int getHostLevelIndex(int level, const typename std::remove_const<GridType>::type::template Codim<codim>::Entity& e) const + int getHostLevelIndex(int level, const GridEntity<codim>& e) const { return hostgrid.levelIndexSet(level).index(grid.getRealImplementation(e).hostEntity()); } @@ -223,7 +220,7 @@ class SubGridIndexStorageBase //! get level index of host grid subentity encapsulated in given subentity template <int cc> - int getHostLevelSubIndex(int level, const typename std::remove_const<GridType>::type::template Codim<cc>::Entity& e, int i, unsigned int codim) const + int getHostLevelSubIndex(int level, const GridEntity<cc>& e, int i, unsigned int codim) const { return hostgrid.levelIndexSet(level).subIndex(grid.getRealImplementation(e).hostEntity(), i, codim+CodimInHostGrid); } @@ -231,20 +228,19 @@ class SubGridIndexStorageBase //! get id of host grid entity encapsulated in given entity template <int codim> - HostIdType getHostId(const typename std::remove_const<GridType>::type::template Codim<codim>::Entity& e) const + HostIdType getHostId(const GridEntity<codim>& e) const { return hostgrid.globalIdSet().id(grid.getRealImplementation(e).hostEntity()); } //! get id of host grid subentity encapsulated in given subentity - HostIdType getHostSubId(const typename std::remove_const<GridType>::type::template Codim<0>::Entity& e, int i, unsigned int codim) const + HostIdType getHostSubId(const GridEntity<0>& e, int i, unsigned int codim) const { return hostgrid.globalIdSet().subId(grid.getRealImplementation(e).hostEntity(), i, codim); } - const GridType& grid; const HostGridType& hostgrid; @@ -587,14 +583,18 @@ template <class GridType> class SubGridMapIndexStorage : public SubGridIndexStorageBase<GridType> { - template <class GridImp_, int EntityDim> - friend class SubGridMapSubindexSetter; + template <class GridImp_, int EntityDim> + friend class SubGridMapSubindexSetter; + + using Base = SubGridIndexStorageBase<GridType>; + template <int codim> + using GridEntity = typename Base::template GridEntity<codim>; public: - typedef typename std::remove_const<GridType>::type::HostGridType HostGridType; - typedef typename HostGridType::Traits::GlobalIdSet HostIdSet; - typedef typename HostIdSet::IdType HostIdType; + using HostGridType = typename Base::HostGridType; + using HostIdSet = typename Base::HostIdSet; + using HostIdType = typename Base::HostIdType; //! \todo Please doc me ! typedef typename std::map<HostIdType, SubGridMultilevelIndex> GlobalToIndexMap; @@ -604,7 +604,7 @@ class SubGridMapIndexStorage : //! \todo Please doc me ! SubGridMapIndexStorage (const GridType& _grid) : - SubGridIndexStorageBase<GridType> (_grid) + Base(_grid) {} @@ -644,10 +644,10 @@ class SubGridMapIndexStorage : //! \todo Please doc me ! template <int codim> - bool isLeaf(const typename std::remove_const<GridType>::type::template Codim<codim>::Entity& e) const + bool isLeaf(const GridEntity<codim>& e) const { HostIdType id = grid.globalIdSet().id(e); - typename GlobalToIndexMap::const_iterator it = indices[codim].find(id); + const auto it = indices[codim].find(id); if (it == indices[codim].end()) return false; return it->second.isLeafOnLevel(e.level()); @@ -659,7 +659,7 @@ class SubGridMapIndexStorage : bool isLeaf(const typename HostGridType::template Codim<codim>::Entity& e) const { HostIdType id = hostgrid.globalIdSet().id(e); - typename GlobalToIndexMap::const_iterator it = indices[codim].find(id); + const auto it = indices[codim].find(id); if (it == indices[codim].end()) return false; return it->second.isLeafOnLevel(e.level()); @@ -668,10 +668,10 @@ class SubGridMapIndexStorage : //! \todo Please doc me ! template <int codim> - int levelIndex(const typename std::remove_const<GridType>::type::template Codim<codim>::Entity& e, int level) const + int levelIndex(const GridEntity<codim>& e, int level) const { HostIdType id = grid.globalIdSet().id(e); - typename GlobalToIndexMap::const_iterator it = indices[codim].find(id); + const auto it = indices[codim].find(id); if (it == indices[codim].end()) return -1; return it->second.getLevelIndex(level); @@ -680,13 +680,13 @@ class SubGridMapIndexStorage : //! \todo Please doc me ! template <int cc> - int levelSubIndex(const typename std::remove_const<GridType>::type::template Codim<cc>::Entity& e, int i, int level, unsigned int codim) const + int levelSubIndex(const GridEntity<cc>& e, int i, int level, unsigned int codim) const { int result = -1; Hybrid::ifElse(Std::bool_constant<cc==0>(),[&](auto id) { HostIdType hid = this->grid.globalIdSet().subId(e, i, codim); - typename GlobalToIndexMap::const_iterator it = indices[codim].find(hid); + const auto it = indices[codim].find(hid); if (it != indices[codim].end()) result = it->second.getLevelIndex(level); }); @@ -701,10 +701,10 @@ class SubGridMapIndexStorage : //! \todo Please doc me ! template <int codim> - int leafIndex(const typename std::remove_const<GridType>::type::template Codim<codim>::Entity& e) const + int leafIndex(const GridEntity<codim>& e) const { HostIdType id = grid.globalIdSet().id(e); - typename GlobalToIndexMap::const_iterator it = indices[codim].find(id); + const auto it = indices[codim].find(id); if (it == indices[codim].end()) return -1; return it->second.leaf; @@ -713,13 +713,13 @@ class SubGridMapIndexStorage : //! \todo Please doc me ! template <int cc> - int leafSubIndex(const typename std::remove_const<GridType>::type::template Codim<cc>::Entity& e, int i, unsigned int codim) const + int leafSubIndex(const GridEntity<cc>& e, int i, unsigned int codim) const { int result = -1; Hybrid::ifElse(Std::bool_constant<cc==0>(),[&](auto id) { HostIdType hid = this->grid.globalIdSet().subId(e, i, codim); - typename GlobalToIndexMap::const_iterator it = indices[codim].find(hid); + const auto it = indices[codim].find(hid); if (it == indices[codim].end()) result = it->second.leaf; }); @@ -736,7 +736,7 @@ class SubGridMapIndexStorage : // this should only be called by update //! \todo Please doc me ! - void insert(const typename std::remove_const<GridType>::type::template Codim<0>::Entity& e, bool isLeaf, int level) + void insert(const GridEntity<0>& e, bool isLeaf, int level) { // get index storage of entity HostIdType id = this->template getHostId<0>(e); @@ -761,11 +761,11 @@ class SubGridMapIndexStorage : protected: - using SubGridIndexStorageBase<GridType>::grid; - using SubGridIndexStorageBase<GridType>::hostgrid; - using SubGridIndexStorageBase<GridType>::numEntities; - using SubGridIndexStorageBase<GridType>::levelTypes; - using SubGridIndexStorageBase<GridType>::leafTypes; + using Base::grid; + using Base::hostgrid; + using Base::numEntities; + using Base::levelTypes; + using Base::leafTypes; //data members, protected since helper class SubGridMapSubindexSetter need access @@ -797,19 +797,19 @@ class LeafOnLevelCounter } - //! \todo Please doc me ! + //! Total number of leaf entities int leafCount; - //! \todo Please doc me ! + //! Number of copied leaf entities int copyLeafCount; - //! \todo Please doc me ! + //! Number of leaf entities that are no copy of coarser level entities int newLeafCount; - //! \todo Please doc me ! + //! Leaf index of first leaf entity that is not a copy on that level int newLeafIndexStart; - //! \todo Please doc me ! + //! The shift that is used to compute leaf indices of new leaf entities from the level index of the leaf entity int newLeafOffset; }; @@ -908,8 +908,6 @@ class SubGridVectorSubindexSetter // recursive call of template meta program to set indices for codim-1 subentities SubGridVectorSubindexSetter<GridType, codim-1>::insertSubEntities(indexStorage, e, isLeaf, level); - - return; } }; @@ -965,19 +963,20 @@ template <class GridType> class SubGridVectorIndexStorage : public SubGridIndexStorageBase<GridType> { - template <class GridImp_, int EntityDim> - friend class SubGridVectorSubindexSetter; + template <class GridImp_, int EntityDim> + friend class SubGridVectorSubindexSetter; - using Base = SubGridIndexStorageBase<GridType>; + using Base = SubGridIndexStorageBase<GridType>; + template <int codim> + using GridEntity = typename Base::template GridEntity<codim>; public: - typedef typename std::remove_const<GridType>::type::HostGridType HostGridType; - typedef typename HostGridType::Traits::GlobalIdSet HostIdSet; - typedef typename HostIdSet::IdType HostIdType; - + using HostGridType = typename Base::HostGridType; + using HostIdSet = typename Base::HostIdSet; + using HostIdType = typename Base::HostIdType; - //! \todo Please doc me ! + //! For all possible geometry types of a grid dimension, store an index for each entity in the host grid typedef typename std::vector<std::vector<int> > EntityToIndex; //! \todo Please doc me ! @@ -994,10 +993,7 @@ class SubGridVectorIndexStorage : //! \todo Please doc me ! SubGridVectorIndexStorage (const GridType& _grid) : - SubGridIndexStorageBase<GridType> (_grid), - uninitializedValue(-3), - visitedValue(-2), - hasLeafIndexValue(-1) + Base(_grid) {} bool indexUninitializedOrVisited(int index) const @@ -1010,15 +1006,9 @@ class SubGridVectorIndexStorage : return (index>-3); } - - - //! \todo Please doc me ! + //! Setup the index storage void update() { - - typedef typename GridType::LevelGridView LevelGridView; - typedef typename GridType::template Codim<0>::LevelIterator LevelIterator; - numEntities.clear(); levelIndices.clear(); @@ -1041,49 +1031,38 @@ class SubGridVectorIndexStorage : for (int codim=0; codim<=dim; ++codim) { const auto& types = hostgrid.levelIndexSet(level).types(codim); - auto it = types.begin(); - auto end = types.end(); - - for (; it!=end; ++it) + for (const auto& type : types) { - int gtIndex = GlobalGeometryTypeIndex::index(*it); + int gtIndex = GlobalGeometryTypeIndex::index(type); - levelIndices[level][gtIndex].resize(hostgrid.size(level, *it), uninitializedValue); - leafIndices[level][gtIndex].resize(hostgrid.size(level, *it), uninitializedValue); + levelIndices[level][gtIndex].resize(hostgrid.size(level, type), uninitializedValue); + leafIndices[level][gtIndex].resize(hostgrid.size(level, type), uninitializedValue); } } - // insert entities - LevelGridView levelGridView = grid.levelGridView(level); - LevelIterator it = levelGridView.template begin<0>(); - LevelIterator end = levelGridView.template end<0>(); + // insert elements + const auto& levelGridView = grid.levelGridView(level); if (level==grid.maxLevel()) { - for(; it!=end; ++it) - insert(*it, true, level); + for(const auto& element : elements(levelGridView)) + insert(element, true, level); } else { - for(; it!=end; ++it) + for(const auto& element: elements(levelGridView)) { - if (it->hbegin(level+1)==it->hend(level+1)) - insert(*it, true, level); + if (element.hbegin(level+1)==element.hend(level+1)) + insert(element, true, level); else - insert(*it, false, level); + insert(element, false, level); } } // now we build the rest of the real level indices for (int codim=0; codim<=dim; ++codim) { - - const auto& types = hostgrid.levelIndexSet(level).types(codim); - auto it = types.begin(); - auto end = types.end(); - - for (; it!=end; ++it) + for (const auto& gt : hostgrid.levelIndexSet(level).types(codim)) { - const GeometryType& gt = *it; int gtIndex = GlobalGeometryTypeIndex::index(gt); MultilevelCounter &count = this->getGeometryTypeCountForLevel(gt, level); @@ -1111,22 +1090,22 @@ class SubGridVectorIndexStorage : std::vector<int>& gtLevelIndices = levelIndices[level][gtIndex]; + // new leaf indices start after the copy leaf indices int newLeafCounter = counter.copyLeafCount; + // the non-leaf indices start after the new leaf indices int nonLeafCounter = counter.leafCount; - for (size_t i=0; i<gtLevelIndices.size(); ++i) + // copy leaf indices were already set in the helper function + for (auto& gtLevelIndex : gtLevelIndices) { - if (indexContained(gtLevelIndices[i])) + if (indexContained(gtLevelIndex)) { ++count.level[level]; - if (gtLevelIndices[i] == visitedValue) - gtLevelIndices[i] = nonLeafCounter++; - else + if (gtLevelIndex == visitedValue) + gtLevelIndex = nonLeafCounter++; + else if (gtLevelIndex == hasLeafIndexValue) { - if (gtLevelIndices[i] == hasLeafIndexValue) - { - gtLevelIndices[i] = newLeafCounter++; - ++count.leaf; - } + gtLevelIndex = newLeafCounter++; + ++count.leaf; } } } @@ -1159,7 +1138,7 @@ class SubGridVectorIndexStorage : //! \todo Please doc me ! template <int codim> - int levelIndex(const typename std::remove_const<GridType>::type::template Codim<codim>::Entity& e, int level) const + int levelIndex(const GridEntity<codim>& e, int level) const { return levelIndices[level][GlobalGeometryTypeIndex::index(e.type())][this->template getHostLevelIndex<codim>(level, e)]; } @@ -1167,7 +1146,7 @@ class SubGridVectorIndexStorage : //! \todo Please doc me ! template <int cc> - int levelSubIndex(const typename std::remove_const<GridType>::type::template Codim<cc>::Entity& e, int i, int level, unsigned int codim) const + int levelSubIndex(const GridEntity<cc>& e, int i, int level, unsigned int codim) const { if (cc!=0) DUNE_THROW( NotImplemented, "levelSubIndex for higher codimension entity not implemented for SubGrid." ); @@ -1177,7 +1156,7 @@ class SubGridVectorIndexStorage : //! \todo Please doc me ! template <int codim> - int leafIndex(const typename std::remove_const<GridType>::type::template Codim<codim>::Entity& e) const + int leafIndex(const GridEntity<codim>& e) const { int level = e.level(); int gtIndex = GlobalGeometryTypeIndex::index(e.type()); @@ -1194,7 +1173,7 @@ class SubGridVectorIndexStorage : //! \todo Please doc me ! template <int cc> - int leafSubIndex(const typename std::remove_const<GridType>::type::template Codim<cc>::Entity& e, int i, unsigned int codim) const + int leafSubIndex(const GridEntity<cc>& e, int i, unsigned int codim) const { if (cc!=0) DUNE_THROW( NotImplemented, "leafSubIndex for higher codimension entity not implemented for SubGrid." ); @@ -1234,25 +1213,18 @@ class SubGridVectorIndexStorage : for (int level=0; level<=grid.maxLevel(); ++level) { for (int codim=0; codim<=dim; ++codim) - { - const auto& types = hostgrid.levelIndexSet(level).types(codim); - auto it = types.begin(); - auto end = types.end(); - - for (; it!=end; ++it) - report(level, *it); - } + for (const auto& type : hostgrid.levelIndexSet(level).types(codim)) + report(level, type); std::cout << std::setfill('-') << std::setw(112) << "" << std::setfill(' ') << std::endl; } return; } - private: // this should only be called by update //! \todo Please doc me ! - void insert(const typename std::remove_const<GridType>::type::template Codim<0>::Entity& e, bool isLeaf, int level) + void insert(const GridEntity<0>& e, bool isLeaf, int level) { // get host index and GeometryType index of entity int hostIndex = this->template getHostLevelIndex<0>(level, e); @@ -1274,27 +1246,27 @@ class SubGridVectorIndexStorage : protected: - using SubGridIndexStorageBase<GridType>::grid; - using SubGridIndexStorageBase<GridType>::hostgrid; - using SubGridIndexStorageBase<GridType>::numEntities; - using SubGridIndexStorageBase<GridType>::levelTypes; - using SubGridIndexStorageBase<GridType>::leafTypes; + using Base::grid; + using Base::hostgrid; + using Base::numEntities; + using Base::levelTypes; + using Base::leafTypes; //data members, protected since helper class SubGridVectorSubindexSetter need access - //! \todo Please doc me ! - int uninitializedValue; + //! Value to mark that an index is still uninitialised + int uninitializedValue{-3}; - //! \todo Please doc me ! - int visitedValue; + //! Value to mark that an index has already been visited during the setup process + int visitedValue{-2}; - //! \todo Please doc me ! - int hasLeafIndexValue; + //! Value to mark entities in the levelIndices vector which are leaf + int hasLeafIndexValue{-1}; - //! \todo Please doc me ! + //! For each level and each Geometry type this contains information about new leaf entities and copies of coarser ones std::vector< std::vector<LeafOnLevelCounter> > leafInfo; - //! \todo Please doc me ! + //! Contains leaf indices of leaf entities that are copies MultilevelEntityToIndex leafIndices; //! \todo Please doc me !