// @HEADER // ***************************************************************************** // ShyLU: Scalable Hybrid LU Preconditioner and Solver // // Copyright 2011 NTESS and the ShyLU contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef _FROSCH_ENTITYSET_DEF_HPP #define _FROSCH_ENTITYSET_DEF_HPP #include namespace FROSch { using namespace std; using namespace Teuchos; using namespace Xpetra; template EntitySet::EntitySet(EntityType type) : Type_ (type) { } template EntitySet::EntitySet(const EntitySet &entitySet) : Type_ (entitySet.getEntityType()), EntityVector_ (entitySet.getEntityVector()) { } template EntitySet::~EntitySet() { } // Do we need sth here? template int EntitySet::addEntity(InterfaceEntityPtr entity) { FROSCH_ASSERT(Type_==DefaultType||entity->getEntityType()==Type_,"FROSch::EntitySet: Entity to add is of wrong type."); EntityVector_.push_back(entity); EntityMapIsUpToDate_ = false; return 0; } template int EntitySet::addEntitySet(EntitySetPtr entitySet) { for (UN i=0; igetNumEntities(); i++) { addEntity(entitySet->getEntity(i)); } return 0; } template typename EntitySet::EntitySetPtr EntitySet::deepCopy() { EntitySetPtr copy(new EntitySet(Type_)); for (UN i=0; i(getEntity(i)->getEntityType(), getEntity(i)->getDofsPerNode(), getEntity(i)->getMultiplicity(), getEntity(i)->getSubdomainsVector().getRawPtr())); for (UN j=0; jgetNumNodes(); j++) { entity->addNode(getEntity(i)->getNode(j).NodeIDGamma_, getEntity(i)->getNode(j).NodeIDLocal_, getEntity(i)->getNode(j).NodeIDGlobal_, getEntity(i)->getNode(j).DofsGamma_.size(), getEntity(i)->getNode(j).DofsGamma_, getEntity(i)->getNode(j).DofsLocal_, getEntity(i)->getNode(j).DofsGlobal_); } copy->addEntity(entity); } return copy; } template int EntitySet::buildEntityMap(ConstXMapPtr localToGlobalNodesMap) { if (!EntityMapIsUpToDate_) { LO localNumberEntities = getNumEntities(); LO globalNumberEntities = 0; // AH 10/13/2017: Can we stick with LO here LO maxLocalNumberEntities = 0; reduceAll(*localToGlobalNodesMap->getComm(),REDUCE_SUM,localNumberEntities,ptr(&globalNumberEntities)); reduceAll(*localToGlobalNodesMap->getComm(),REDUCE_MAX,localNumberEntities,ptr(&maxLocalNumberEntities)); GOVec localToGlobalVector(0); const GO INVALID = Teuchos::OrdinalTraits::invalid(); if (globalNumberEntities>0) { // Set the Unique iD setUniqueIDToFirstGlobalNodeID(); GOVec entities(maxLocalNumberEntities); for (UN i=0; igetUniqueID()+1; getEntity(i)->setLocalID(i); } XMapPtr entityMapping = MapFactory::Build(localToGlobalNodesMap->lib(),INVALID,entities(),0,localToGlobalNodesMap->getComm()); GOVec allEntities(maxLocalNumberEntities*localToGlobalNodesMap->getComm()->getSize(),0); //localToGlobalNodesMap->getComm().GatherAll(&(entities->at(0)),&(allEntities->at(0)),maxLocalNumberEntities); gatherAll(*localToGlobalNodesMap->getComm(),maxLocalNumberEntities,entities.getRawPtr(),maxLocalNumberEntities*localToGlobalNodesMap->getComm()->getSize(),allEntities.getRawPtr()); allEntities.push_back(0); // Um sicherzugehen, dass der erste Eintrag nach sort_unique eine 0 ist. sortunique(allEntities); localToGlobalVector.resize(localNumberEntities); int LocalID; for (UN i=1; igetLocalElement(allEntities[i]); if ( LocalID != -1) { localToGlobalVector[LocalID] = i-1; } } } EntityMap_ = MapFactory::Build(localToGlobalNodesMap->lib(),INVALID,localToGlobalVector(),0,localToGlobalNodesMap->getComm()); EntityMapIsUpToDate_ = true; } return 0; } template int EntitySet::findAncestorsInSet(EntitySetPtr entitySet) { for (UN i=0; ifindAncestorsInSet(entitySet); } return 0; } template typename EntitySet::EntitySetPtr EntitySet::findRoots() { EntitySetPtr Roots(new EntitySet(DefaultType)); for (UN i=0; ifindRoots(); if (tmpRoots.is_null()) { FROSCH_ASSERT(getEntity(i)->getAncestors()->getNumEntities()==0,"FROSch::EntitySet: getEntity(i)->getAncestors()->getNumEntities()!=0"); Roots->addEntity(getEntity(i)); } else { FROSCH_ASSERT(getEntity(i)->getAncestors()->getNumEntities()!=0,"FROSch::EntitySet: getEntity(i)->getAncestors()->getNumEntities()==0"); FROSCH_ASSERT(tmpRoots->getNumEntities()>0,"FROSch::EntitySet: tmpRoots->getNumEntities()<=0"); Roots->addEntitySet(tmpRoots); } } Roots->sortUnique(); return Roots; } template typename EntitySet::EntitySetPtr EntitySet::findLeafs() { EntitySetPtr Leafs(new EntitySet(DefaultType)); for (UN i=0; igetOffspring()->getNumEntities()==0) { Leafs->addEntity(getEntity(i)); } } Leafs->sortUnique(); return Leafs; } template int EntitySet::clearAncestors() { for (UN i=0; iclearAncestors(); } return 0; } template int EntitySet::clearOffspring() { for (UN i=0; iclearOffspring(); } return 0; } template int EntitySet::clearRoots() { for (UN i=0; iclearRoots(); } return 0; } template int EntitySet::clearLeafs() { for (UN i=0; iclearLeafs(); } return 0; } template int EntitySet::computeDistancesToRoots(UN dimension, ConstXMultiVectorPtr &nodeList, DistanceFunction distanceFunction) { for (UN i=0; icomputeDistancesToRoots(dimension,nodeList,distanceFunction); } return 0; } template int EntitySet::divideUnconnectedEntities(ConstXMatrixPtr matrix, int pID) { UN before = getNumEntities(); UN i=0; while (idivideEntity(matrix,pID); if (tmpEntity->getNumNodes()>0) { addEntity(tmpEntity); } i++; } if (getNumEntities()-before>0) EntityMapIsUpToDate_ = false; return getNumEntities()-before; } template int EntitySet::flagNodes() { for (UN i=0; igetNumNodes()==1) { getEntity(i)->resetEntityFlag(NodeFlag); } } return 0; } template int EntitySet::flagShortEntities() { for (UN i=0; igetNumNodes()==2) { getEntity(i)->resetEntityFlag(ShortFlag); } } return 0; } template int EntitySet::flagStraightEntities(UN dimension, ConstXMultiVectorPtr &nodeList) { FROSCH_ASSERT(dimension==nodeList->getNumVectors(),"FROSch::EntitySet: Inconsistent Dimension."); bool straight; LO length,j; SCVec pt1(dimension); SCVec dir1(dimension); SCVec dir2(dimension); for (UN i=0; igetNumNodes(); j=2; if (length>2) { // Anfangssteigung berechnen for (UN k=0; kgetData(k)[getEntity(i)->getLocalNodeID(0)]; } for (UN k=0; kgetData(k)[getEntity(i)->getLocalNodeID(1)]-pt1[k]; } while (jgetData(k)[getEntity(i)->getLocalNodeID(j)]-pt1[k]; } if (!ismultiple(dir1(),dir2())) { straight = false; break; } j++; } if (straight) { getEntity(i)->resetEntityFlag(StraightFlag); } } } return 0; } template typename EntitySet::EntitySetPtr EntitySet::sortOutEntities(EntityFlag flag) { UN before = getNumEntities(); EntitySetPtr removedEntities(new EntitySet(DefaultType)); for (UN i=0; igetEntityFlag()==flag) { removedEntities->addEntities(getEntity(i)); EntityVector_.erase(EntityVector_.begin()+i); i--; } } if (getNumEntities()-before>0) EntityMapIsUpToDate_ = false; return arcp(removedEntities); } template int EntitySet::removeEntity(UN iD) { FROSCH_ASSERT(iD=getNumEntities()."); EntityVector_.erase(EntityVector_.begin()+iD); EntityMapIsUpToDate_ = false; return 0; } template int EntitySet::removeNodesWithDofs(GOVecView dirichletBoundaryDofs) { UN dofsPerNode = 0; if (getNumEntities()>0) dofsPerNode = EntityVector_[0]->getDofsPerNode(); for (UN i=0; igetNumNodes(); for (UN j=0; jgetGlobalDofID(itmp,k); if (binary_search(dirichletBoundaryDofs.begin(),dirichletBoundaryDofs.end(),dofGlobal)) { getEntity(i)->removeNode(itmp); break; } k++; } } } return 0; } template int EntitySet::removeEmptyEntities() { UN before = getNumEntities(); for (UN i=0; igetNumNodes()==0) { EntityVector_.erase(EntityVector_.begin()+i); i--; } } if (getNumEntities()-before>0) EntityMapIsUpToDate_ = false; return 0; } template int EntitySet::sortUnique() { for (UN i=0; isortByGlobalID(); } std::sort(EntityVector_.begin(),EntityVector_.end(),compareInterfaceEntities); EntityVector_.erase(unique(EntityVector_.begin(),EntityVector_.end(),equalInterfaceEntities),EntityVector_.end()); EntityMapIsUpToDate_ = false; return 0; } template bool EntitySet::checkForVertices() { for (UN i=0; igetNumNodes()==1) { i--; return true; } } return false; } template bool EntitySet::checkForShortEdges() { for (UN i=0; igetNumNodes()==2) { i--; return true; } } return false; } template bool EntitySet::checkForStraightEdges(UN dimension, ConstXMultiVectorPtr &nodeList) { FROSCH_ASSERT(dimension==nodeList->getNumVectors(),"FROSch::EntitySet: Inconsistent Dimension."); bool straight; LO length,j; SCVec pt1(dimension); SCVec dir1(dimension); SCVec dir2(dimension); for (UN i=0; igetNumNodes(); j=2; if (length>2) { // Anfangssteigung berechnen for (UN k=0; kgetData(k)[getEntity(i)->getLocalNodeID(0)]; } for (UN k=0; kgetData(k)[getEntity(i)->getLocalNodeID(1)]-pt1[k]; } while (jgetData(k)[getEntity(i)->getLocalNodeID(j)]-pt1[k]; } if (!ismultiple(dir1(),dir2())) { straight = false; break; } j++; } if (straight) { i--; return true; } } } return false; } template bool EntitySet::checkForEmptyEntities() { for (UN i=0; igetNumNodes()==0) { i--; return true; } } return false; } ///////////////// // Set Methods // ///////////////// template int EntitySet::setUniqueIDToFirstGlobalNodeID() { for (UN i=0; isortByGlobalID(); getEntity(i)->setUniqueIDToFirstGlobalID(); } EntityMapIsUpToDate_ = false; return 0; } template int EntitySet::setRootID() { for (UN i=0; isetRootID(i); } return 0; } template int EntitySet::setLeafID() { for (UN i=0; isetLeafID(i); } return 0; } template int EntitySet::resetEntityType(EntityType type) { Type_ = type; for (UN i=0; iresetEntityType(type); } return 0; } ///////////////// // Get Methods // ///////////////// template EntityType EntitySet::getEntityType() const { return Type_; } template typename EntitySet::UN EntitySet::getNumEntities() const { return EntityVector_.size(); } template const typename EntitySet::InterfaceEntityPtrVec & EntitySet::getEntityVector() const { return EntityVector_; } template const typename EntitySet::InterfaceEntityPtr EntitySet::getEntity(UN iD) const { FROSCH_ASSERT(iD=getNumEntities()."); return EntityVector_[iD]; } template const typename EntitySet::XMapPtr EntitySet::getEntityMap() const { FROSCH_ASSERT(EntityMapIsUpToDate_,"FROSch::EntitySet: the entity map has not been built or is not up to date."); return EntityMap_; } template const typename EntitySet::SCVecPtr EntitySet::getDirection(UN dimension, ConstXMultiVectorPtr &nodeList, UN iD) const { FROSCH_ASSERT(iD=getNumEntities()."); if (getEntity(iD)->getEntityFlag()==StraightFlag) { LO length = getEntity(iD)->getNumNodes(); LO j=2; FROSCH_ASSERT(length>2,"Edge is not a straight edge!"); bool straight=true; SCVec pt1(dimension); SCVec dir2(dimension); SCVecPtr dir1(dimension); // Anfangssteigung berechnen for (UN k=0; kgetData(k)[getEntity(iD)->getLocalNodeID(0)]; } for (UN k=0; kgetData(k)[getEntity(iD)->getLocalNodeID(1)]-pt1[k]; } while (jgetData(k)[getEntity(iD)->getLocalNodeID(j)]-pt1[k]; } if (!ismultiple(dir1(),dir2())) { straight = false; break; } j++; } FROSCH_ASSERT(straight,"FROSch::EntitySet: Edge is not straight!"); return dir1; } else if (getEntity(iD)->getEntityFlag()==ShortFlag) { int length = getEntity(iD)->getNumNodes(); FROSCH_ASSERT(length==2,"FROSch::EntitySet: Edge is not a short edge!"); SCVec pt1(dimension); SCVecPtr dir1(dimension); // Anfangssteigung berechnen for (UN k=0; kgetData(k)[getEntity(iD)->getLocalNodeID(0)]; } for (UN k=0; kgetData(k)[getEntity(iD)->getLocalNodeID(1)]-pt1[k]; } return dir1; } else { FROSCH_ASSERT(false,"FROSch::EntitySet: There is a problem while computing the direction of an edge!"); } } } #endif