| 1 |
#include <gtest/gtest.h> // for AssertHelper, EXPECT_EQ, etc |
| 2 |
#include <stk_mesh/base/MetaData.hpp> // for MetaData |
| 3 |
#include <stk_mesh/base/BulkData.hpp> |
| 4 |
#include <stk_mesh/base/SkinBoundary.hpp> |
| 5 |
#include <stk_mesh/base/GetEntities.hpp> |
| 6 |
#include "stk_mesh/baseImpl/elementGraph/ElemElemGraph.hpp" |
| 7 |
#include <stk_unit_test_utils/MeshFixture.hpp> |
| 8 |
#include <stk_io/StkMeshIoBroker.hpp> |
| 9 |
#include <stk_io/FillMesh.hpp> |
| 10 |
#include <stk_io/WriteMesh.hpp> |
| 11 |
#include <stk_tools/mesh_tools/DisconnectBlocks.hpp> |
| 12 |
#include <stk_tools/mesh_tools/DisconnectBlocksImpl.hpp> |
| 13 |
#include <stk_tools/mesh_tools/DetectHingesImpl.hpp> |
| 14 |
#include <stk_unit_test_utils/TextMesh.hpp> |
| 15 |
#include <stk_util/environment/WallTime.hpp> |
| 16 |
#include "stk_unit_test_utils/getOption.h" |
| 17 |
#include <stk_unit_test_utils/BuildMesh.hpp> |
| 18 |
#include "stk_util/parallel/ParallelReduce.hpp" |
| 19 |
#include "stk_util/util/SortAndUnique.hpp" |
| 20 |
#include "stk_util/util/GraphCycleDetector.hpp" |
| 21 |
#include "stk_mesh/base/Types.hpp" |
| 22 |
#include "stk_mesh/base/FEMHelpers.hpp" |
| 23 |
#include "stk_mesh/base/EntityLess.hpp" |
| 24 |
#include "stk_mesh/base/Comm.hpp" |
| 25 |
#include <stk_mesh/base/HashEntityAndEntityKey.hpp> |
| 26 |
#include "stk_mesh/baseImpl/elementGraph/ElemElemGraph.hpp" |
| 27 |
#include "stk_util/util/TreeTraverser.hpp" |
| 28 |
|
| 29 |
#include <limits> |
| 30 |
#include <string> |
| 31 |
#include <algorithm> |
| 32 |
#include <unordered_map> |
| 33 |
|
| 34 |
using stk::unit_test_util::build_mesh; |
| 35 |
|
| 36 |
//#define DEBUG |
| 37 |
|
| 38 |
struct ElementCluster |
| 39 |
{ |
| 40 |
static constexpr int64_t INVALID_ID = std::numeric_limits<int64_t>::max(); |
| 41 |
|
| 42 |
int64_t globalId = INVALID_ID; |
| 43 |
stk::mesh::EntityVector elements{}; |
| 44 |
|
| 45 |
ElementCluster() : globalId(INVALID_ID) {} |
| 46 |
ElementCluster(stk::mesh::EntityVector& elements_) : globalId(INVALID_ID), elements(std::move(elements_)) {} |
| 47 |
}; |
| 48 |
|
| 49 |
struct ElementIdCluster |
| 50 |
{ |
| 51 |
static constexpr int64_t INVALID_ID = -1; |
| 52 |
|
| 53 |
int64_t globalId = INVALID_ID; |
| 54 |
stk::mesh::EntityIdVector elements{}; |
| 55 |
|
| 56 |
ElementIdCluster() : globalId(INVALID_ID) {} |
| 57 |
ElementIdCluster(int64_t id_) : globalId(id_) {} |
| 58 |
ElementIdCluster(int64_t id_, const stk::mesh::EntityIdVector& elements_) : globalId(id_), elements(elements_) {} |
| 59 |
}; |
| 60 |
|
| 61 |
struct ParallelClusterRecvInfo |
| 62 |
{ |
| 63 |
int64_t clusterIndex = -1; |
| 64 |
stk::mesh::EntityId clusterMinElemId = stk::mesh::InvalidEntityId; |
| 65 |
|
| 66 |
ParallelClusterRecvInfo(int64_t clusterIndex_, stk::mesh::EntityId clusterMinElemId_) |
| 67 |
: clusterIndex(clusterIndex_), clusterMinElemId(clusterMinElemId_) {} |
| 68 |
}; |
| 69 |
|
| 70 |
struct ParallelClusterInfo |
| 71 |
{ |
| 72 |
int64_t clusterIndex = -1; |
| 73 |
stk::mesh::EntityId minElemId = stk::mesh::InvalidEntityId; |
| 74 |
std::unordered_map<int, stk::mesh::EntityIdVector> sendData; |
| 75 |
std::unordered_map<int, std::vector<ParallelClusterRecvInfo>> recvData; |
| 76 |
|
| 77 |
ParallelClusterInfo() : clusterIndex(-1) {} |
| 78 |
ParallelClusterInfo(int64_t index) : clusterIndex(index) {} |
| 79 |
}; |
| 80 |
|
| 81 |
struct ClusterGraphNode |
| 82 |
{ |
| 83 |
int proc = -1; |
| 84 |
int64_t clusterIndex = -1; |
| 85 |
stk::mesh::EntityId clusterMinElemId = stk::mesh::InvalidEntityId; |
| 86 |
|
| 87 |
ClusterGraphNode() : proc(-1), clusterIndex(-1), clusterMinElemId(stk::mesh::InvalidEntityId) {} |
| 88 |
ClusterGraphNode(int proc_, int64_t clusterIndex_, stk::mesh::EntityId clusterMinElemId_) |
| 89 |
: proc(proc_), clusterIndex(clusterIndex_), clusterMinElemId(clusterMinElemId_) {} |
| 90 |
|
| 91 |
bool operator==(const ClusterGraphNode &rhs) const |
| 92 |
{ |
| 93 |
return ((proc == rhs.proc) && (clusterIndex == rhs.clusterIndex) && (clusterMinElemId == rhs.clusterMinElemId)); |
| 94 |
} |
| 95 |
|
| 96 |
bool operator!=(const ClusterGraphNode &rhs) const |
| 97 |
{ |
| 98 |
return ((proc != rhs.proc) || (clusterIndex != rhs.clusterIndex) || (clusterMinElemId != rhs.clusterMinElemId)); |
| 99 |
} |
| 100 |
|
| 101 |
bool operator<(const ClusterGraphNode &rhs) const |
| 102 |
{ |
| 103 |
if(proc < rhs.proc) |
| 104 |
return true; |
| 105 |
else if (proc == rhs.proc && clusterIndex < rhs.clusterIndex) |
| 106 |
return true; |
| 107 |
else if(proc == rhs.proc && clusterIndex == rhs.clusterIndex) { |
| 108 |
STK_ThrowRequireMsg(clusterMinElemId == rhs.clusterMinElemId, |
| 109 |
"Two equivalent cluster nodes {proc,clusterIndex} = {" |
| 110 |
<< proc << "," << clusterIndex << "} do not have the same clusterMinElemId: " |
| 111 |
<< clusterMinElemId << " vs " << rhs.clusterMinElemId); |
| 112 |
} |
| 113 |
|
| 114 |
return false; |
| 115 |
} |
| 116 |
}; |
| 117 |
|
| 118 |
struct ClusterGraphEdge |
| 119 |
{ |
| 120 |
ClusterGraphNode node1; |
| 121 |
ClusterGraphNode node2; |
| 122 |
|
| 123 |
ClusterGraphEdge(const ClusterGraphNode& node1_, const ClusterGraphNode& node2_) : node1(node1_), node2(node2_) {} |
| 124 |
ClusterGraphEdge reciprocal_edge() const {return ClusterGraphEdge(node2, node1);} |
| 125 |
}; |
| 126 |
|
| 127 |
inline std::ostream &operator<<(std::ostream &out, const ClusterGraphNode &t) |
| 128 |
{ |
| 129 |
return out << "{proc:" << t.proc << " index:" << t.clusterIndex << " minId:" << t.clusterMinElemId << "}"; |
| 130 |
} |
| 131 |
|
| 132 |
inline std::ostream &operator<<(std::ostream &out, const ClusterGraphEdge &t) |
| 133 |
{ |
| 134 |
return out << t.node1 << " <-> " << t.node2; |
| 135 |
} |
| 136 |
|
| 137 |
template <class T> |
| 138 |
inline void hash_combine(std::size_t & s, const T & v) |
| 139 |
{ |
| 140 |
std::hash<T> h; |
| 141 |
s ^= h(v) + 0x9e3779b9 + (s<< 6) + (s>> 2); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
namespace std { |
| 146 |
|
| 147 |
template<> |
| 148 |
struct hash<ClusterGraphNode> |
| 149 |
{ |
| 150 |
size_t operator()(ClusterGraphNode const& s) const |
| 151 |
{ |
| 152 |
std::size_t res = 0; |
| 153 |
hash_combine(res,s.proc); |
| 154 |
hash_combine(res,s.clusterIndex); |
| 155 |
hash_combine(res,s.clusterMinElemId); |
| 156 |
return res; |
| 157 |
} |
| 158 |
}; |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
using ElementClusterVector = std::vector<ElementIdCluster>; |
| 163 |
using ClusterGraphData = std::map<ClusterGraphNode, std::vector<ClusterGraphNode>>; |
| 164 |
|
| 165 |
template <typename GraphKey, typename GraphData> |
| 166 |
class ClusterGraph : public stk::util::TreeTraverser<GraphKey, typename GraphData::const_iterator> { |
| 167 |
public: |
| 168 |
using Key = GraphKey; |
| 169 |
using Iterator = typename GraphData::const_iterator; |
| 170 |
|
| 171 |
explicit ClusterGraph(const GraphData& clusterData) |
| 172 |
: stk::util::TreeTraverser<Key, Iterator>() |
| 173 |
, m_clusterData(clusterData) |
| 174 |
{ |
| 175 |
} |
| 176 |
|
| 177 |
Iterator begin() const override { return m_clusterData.begin(); } |
| 178 |
|
| 179 |
Iterator end() const override { return m_clusterData.end(); } |
| 180 |
|
| 181 |
const Key& get_node(Iterator iter) const override { return iter->first; } |
| 182 |
|
| 183 |
size_t size() const override { return m_clusterData.size(); } |
| 184 |
|
| 185 |
bool node_exists(const Key& node) const override { return m_clusterData.find(node) != m_clusterData.end(); } |
| 186 |
|
| 187 |
const std::vector<Key>& children(const Key& node) const override |
| 188 |
{ |
| 189 |
const auto& iter = m_clusterData.find(node); |
| 190 |
if(iter != m_clusterData.end()) { |
| 191 |
return iter->second; |
| 192 |
} |
| 193 |
|
| 194 |
return m_emptyChildren; |
| 195 |
} |
| 196 |
|
| 197 |
private: |
| 198 |
ClusterGraph() = delete; |
| 199 |
ClusterGraph(const ClusterGraph&) = delete; |
| 200 |
|
| 201 |
const GraphData& m_clusterData; |
| 202 |
const std::vector<Key> m_emptyChildren{}; |
| 203 |
}; |
| 204 |
|
| 205 |
class NeighborClusterConnectivityHelper |
| 206 |
{ |
| 207 |
public: |
| 208 |
NeighborClusterConnectivityHelper(stk::mesh::BulkData& bulk) |
| 209 |
: m_bulk(bulk), m_comm(bulk.parallel()) |
| 210 |
{ |
| 211 |
|
| 212 |
} |
| 213 |
|
| 214 |
void fill_neighbor_cluster_connectivity(std::vector<ElementCluster>& elementClusters, std::vector<ParallelClusterInfo>& pcInfoVec) |
| 215 |
{ |
| 216 |
std::unordered_map<stk::mesh::EntityId, int64_t> entityToClusterMap; |
| 217 |
for(size_t i=0; i<elementClusters.size(); i++) { |
| 218 |
for(stk::mesh::Entity entry : elementClusters[i].elements) { |
| 219 |
entityToClusterMap[m_bulk.identifier(entry)] = i; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
pack_and_communicate_local_cluster_info(pcInfoVec); |
| 224 |
unpack_and_update_cluster_connectivity(pcInfoVec, entityToClusterMap); |
| 225 |
} |
| 226 |
|
| 227 |
private: |
| 228 |
void pack_data_for_clusters(std::vector<ParallelClusterInfo>& pcInfoVec) |
| 229 |
{ |
| 230 |
for(const auto& pcInfo : pcInfoVec) |
| 231 |
{ |
| 232 |
for(const auto& item : pcInfo.sendData) { |
| 233 |
const int procRank = item.first; |
| 234 |
const stk::mesh::EntityIdVector &procElemIds = item.second; |
| 235 |
|
| 236 |
m_comm.send_buffer(procRank).pack<int64_t>(pcInfo.clusterIndex); |
| 237 |
m_comm.send_buffer(procRank).pack<stk::mesh::EntityId>(pcInfo.minElemId); |
| 238 |
|
| 239 |
m_comm.send_buffer(procRank).pack<unsigned>(procElemIds.size()); |
| 240 |
for(stk::mesh::EntityId procElemId : procElemIds) { |
| 241 |
m_comm.send_buffer(procRank).pack<stk::mesh::EntityId>(procElemId); |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
void pack_and_communicate_local_cluster_info(std::vector<ParallelClusterInfo>& pcInfoVec) |
| 248 |
{ |
| 249 |
stk::pack_and_communicate(m_comm, [this, &pcInfoVec]() |
| 250 |
{ |
| 251 |
pack_data_for_clusters(pcInfoVec); |
| 252 |
}); |
| 253 |
} |
| 254 |
|
| 255 |
void unpack_data_for_clusters(int rank, std::vector<ParallelClusterInfo>& pcInfoVec, |
| 256 |
std::unordered_map<stk::mesh::EntityId, int64_t>& entityToClusterMap) |
| 257 |
{ |
| 258 |
int64_t remoteClusterIndex; |
| 259 |
stk::mesh::EntityId remoteClusterMinElemId; |
| 260 |
|
| 261 |
m_comm.recv_buffer(rank).unpack<int64_t>(remoteClusterIndex); |
| 262 |
m_comm.recv_buffer(rank).unpack<stk::mesh::EntityId>(remoteClusterMinElemId); |
| 263 |
|
| 264 |
unsigned numLocalElemIds; |
| 265 |
m_comm.recv_buffer(rank).unpack<unsigned>(numLocalElemIds); |
| 266 |
|
| 267 |
stk::mesh::EntityId localElemId; |
| 268 |
for(unsigned i=0; i<numLocalElemIds; i++) { |
| 269 |
m_comm.recv_buffer(rank).unpack<stk::mesh::EntityId>(localElemId); |
| 270 |
|
| 271 |
#if defined(DEBUG) |
| 272 |
debugStream_ << "\tUnpacked local element: " << localElemId |
| 273 |
<< " from proc: " << rank |
| 274 |
<< " for remote cluster index: " << remoteClusterIndex |
| 275 |
<< " and remote min elem id: " << remoteClusterMinElemId |
| 276 |
<< std::endl; |
| 277 |
#endif |
| 278 |
|
| 279 |
auto iter = entityToClusterMap.find(localElemId); |
| 280 |
STK_ThrowRequireMsg(iter != entityToClusterMap.end(), |
| 281 |
"Could not find local element " << localElemId << " from proc: " << rank |
| 282 |
<< " for remote cluster index: " << remoteClusterIndex); |
| 283 |
|
| 284 |
int64_t localClusterIndex = iter->second; |
| 285 |
ParallelClusterRecvInfo entry(remoteClusterIndex, remoteClusterMinElemId); |
| 286 |
pcInfoVec[localClusterIndex].recvData[rank].push_back(entry); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
void unpack_and_update_cluster_connectivity(std::vector<ParallelClusterInfo>& pcInfoVec, |
| 291 |
std::unordered_map<stk::mesh::EntityId, int64_t>& entityToClusterMap) |
| 292 |
{ |
| 293 |
stk::unpack_communications(m_comm, [this, &pcInfoVec, &entityToClusterMap](int rank) |
| 294 |
{ |
| 295 |
unpack_data_for_clusters(rank, pcInfoVec, entityToClusterMap); |
| 296 |
}); |
| 297 |
} |
| 298 |
|
| 299 |
NeighborClusterConnectivityHelper() = delete; |
| 300 |
NeighborClusterConnectivityHelper(const NeighborClusterConnectivityHelper&) = delete; |
| 301 |
|
| 302 |
const stk::mesh::BulkData& m_bulk; |
| 303 |
stk::CommSparse m_comm; |
| 304 |
}; |
| 305 |
|
| 306 |
class GlobalClusterConnectivityHelper |
| 307 |
{ |
| 308 |
public: |
| 309 |
GlobalClusterConnectivityHelper(stk::mesh::BulkData& bulk) |
| 310 |
: m_bulk(bulk), m_comm(bulk.parallel()) |
| 311 |
{ |
| 312 |
|
| 313 |
} |
| 314 |
|
| 315 |
void fill_global_cluster_graph_edges(const std::vector<ParallelClusterInfo>& pcInfoVec, |
| 316 |
std::vector<ClusterGraphEdge>& globalClusterEdges) |
| 317 |
{ |
| 318 |
std::vector<ClusterGraphEdge> localClusterEdges; |
| 319 |
|
| 320 |
for(const ParallelClusterInfo& pcInfo : pcInfoVec) { |
| 321 |
ClusterGraphNode localClusterNode(m_bulk.parallel_rank(), pcInfo.clusterIndex, pcInfo.minElemId); |
| 322 |
std::set<ClusterGraphNode> remoteClusterNodesSet; |
| 323 |
|
| 324 |
for(const auto& item : pcInfo.recvData) { |
| 325 |
int proc = item.first; |
| 326 |
for(const ParallelClusterRecvInfo& recvInfo : item.second) { |
| 327 |
ClusterGraphNode remoteClusterNode(proc, recvInfo.clusterIndex, recvInfo.clusterMinElemId); |
| 328 |
remoteClusterNodesSet.insert(remoteClusterNode); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
for(const ClusterGraphNode& remoteClusterNode : remoteClusterNodesSet) { |
| 333 |
localClusterEdges.emplace_back(localClusterNode, remoteClusterNode); |
| 334 |
#if defined(DEBUG) |
| 335 |
debugStream_ << "\t\t" << localClusterEdges.back() << std::endl; |
| 336 |
#endif |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
pack_and_communicate_cluster_edges(localClusterEdges); |
| 341 |
unpack_remote_cluster_edges(globalClusterEdges); |
| 342 |
} |
| 343 |
|
| 344 |
private: |
| 345 |
void pack_data_for_cluster_edges(std::vector<ClusterGraphEdge>& clusterEdges) |
| 346 |
{ |
| 347 |
for(int i=0; i<m_bulk.parallel_size(); i++) { |
| 348 |
if(i == m_bulk.parallel_rank()) continue; |
| 349 |
|
| 350 |
for(const auto& clusterEdge : clusterEdges) |
| 351 |
{ |
| 352 |
m_comm.send_buffer(i).pack<int>(clusterEdge.node1.proc); |
| 353 |
m_comm.send_buffer(i).pack<int64_t>(clusterEdge.node1.clusterIndex); |
| 354 |
m_comm.send_buffer(i).pack<stk::mesh::EntityId>(clusterEdge.node1.clusterMinElemId); |
| 355 |
|
| 356 |
m_comm.send_buffer(i).pack<int>(clusterEdge.node2.proc); |
| 357 |
m_comm.send_buffer(i).pack<int64_t>(clusterEdge.node2.clusterIndex); |
| 358 |
m_comm.send_buffer(i).pack<stk::mesh::EntityId>(clusterEdge.node2.clusterMinElemId); |
| 359 |
} |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
void pack_and_communicate_cluster_edges(std::vector<ClusterGraphEdge>& clusterEdges) |
| 364 |
{ |
| 365 |
stk::pack_and_communicate(m_comm, [this, &clusterEdges]() |
| 366 |
{ |
| 367 |
pack_data_for_cluster_edges(clusterEdges); |
| 368 |
}); |
| 369 |
} |
| 370 |
|
| 371 |
void unpack_data_for_cluster_edges(int rank, std::vector<ClusterGraphEdge>& remoteClusterEdges) |
| 372 |
{ |
| 373 |
ClusterGraphNode node1; |
| 374 |
ClusterGraphNode node2; |
| 375 |
|
| 376 |
m_comm.recv_buffer(rank).unpack<int>(node1.proc); |
| 377 |
m_comm.recv_buffer(rank).unpack<int64_t>(node1.clusterIndex); |
| 378 |
m_comm.recv_buffer(rank).unpack<stk::mesh::EntityId>(node1.clusterMinElemId); |
| 379 |
|
| 380 |
m_comm.recv_buffer(rank).unpack<int>(node2.proc); |
| 381 |
m_comm.recv_buffer(rank).unpack<int64_t>(node2.clusterIndex); |
| 382 |
m_comm.recv_buffer(rank).unpack<stk::mesh::EntityId>(node2.clusterMinElemId); |
| 383 |
|
| 384 |
remoteClusterEdges.emplace_back(node1, node2); |
| 385 |
|
| 386 |
#if defined(DEBUG) |
| 387 |
debugStream_ << "\tUnpacked remote cluster edge: " << remoteClusterEdges.back() |
| 388 |
<< " from proc: " << rank << std::endl; |
| 389 |
#endif |
| 390 |
} |
| 391 |
|
| 392 |
void unpack_remote_cluster_edges(std::vector<ClusterGraphEdge>& remoteClusterEdges) |
| 393 |
{ |
| 394 |
stk::unpack_communications(m_comm, [this, &remoteClusterEdges](int rank) |
| 395 |
{ |
| 396 |
unpack_data_for_cluster_edges(rank, remoteClusterEdges); |
| 397 |
}); |
| 398 |
} |
| 399 |
|
| 400 |
GlobalClusterConnectivityHelper() = delete; |
| 401 |
GlobalClusterConnectivityHelper(const GlobalClusterConnectivityHelper&) = delete; |
| 402 |
|
| 403 |
const stk::mesh::BulkData& m_bulk; |
| 404 |
stk::CommSparse m_comm; |
| 405 |
}; |
| 406 |
|
| 407 |
class MeshTraverser |
| 408 |
{ |
| 409 |
public: |
| 410 |
static constexpr size_t INVALID_SEED = std::numeric_limits<size_t>::max(); |
| 411 |
|
| 412 |
MeshTraverser(stk::mesh::BulkData& bulk) : m_bulk(bulk), m_currentSeed(0), m_currentNumVisitedElements(0) |
| 413 |
{ |
| 414 |
m_bulk.initialize_face_adjacent_element_graph(); |
| 415 |
create_local_element_clusters(); |
| 416 |
assign_global_cluster_ids(); |
| 417 |
|
| 418 |
#if defined(DEBUG) |
| 419 |
std::cout << "P[" << m_bulk.parallel_rank() << "]\n" << debugStream_.str(); |
| 420 |
#endif |
| 421 |
} |
| 422 |
|
| 423 |
const std::vector<ElementCluster>& get_clusters() const { return m_elementClusters; } |
| 424 |
|
| 425 |
private: |
| 426 |
void fill_parallel_cluster_send_info(ParallelClusterInfo& pcInfo) |
| 427 |
{ |
| 428 |
if(pcInfo.clusterIndex < 0) return; |
| 429 |
|
| 430 |
stk::mesh::ElemElemGraph& graph = m_bulk.get_face_adjacent_element_graph(); |
| 431 |
const stk::mesh::EntityVector& elements = m_elementClusters[pcInfo.clusterIndex].elements; |
| 432 |
|
| 433 |
for(stk::mesh::Entity entry : elements) { |
| 434 |
stk::mesh::impl::LocalId localId = graph.get_local_element_id(entry); |
| 435 |
for(const stk::mesh::GraphEdge &graphEdge : graph.get_edges_for_element(localId)) { |
| 436 |
if(!graphEdge.is_elem2_local()) { |
| 437 |
stk::mesh::EntityId remoteElemId = graph.convert_negative_local_id_to_global_id(graphEdge.elem2()); |
| 438 |
int remoteProc = graph.get_parallel_info_for_graph_edge(graphEdge).get_proc_rank_of_neighbor(); |
| 439 |
pcInfo.sendData[remoteProc].push_back(remoteElemId); |
| 440 |
|
| 441 |
#if defined(DEBUG) |
| 442 |
debugStream_ << "\tFound remote boundary element: " << remoteElemId |
| 443 |
<< " on proc: " << remoteProc |
| 444 |
<< " for element: " << m_bulk.entity_key(entry) |
| 445 |
<< std::endl; |
| 446 |
#endif |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
pcInfo.minElemId = std::min(pcInfo.minElemId, m_bulk.identifier(entry)); |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
void update_global_cluster_ids_from_graph(const std::vector<ParallelClusterInfo>& pcInfoVec, |
| 455 |
ClusterGraph<ClusterGraphNode, ClusterGraphData>& globalClusterGraph) |
| 456 |
{ |
| 457 |
for(const ParallelClusterInfo& pcInfo : pcInfoVec) { |
| 458 |
ClusterGraphNode localClusterNode(m_bulk.parallel_rank(), pcInfo.clusterIndex, pcInfo.minElemId); |
| 459 |
std::vector<ClusterGraphNode> clusterList = globalClusterGraph.get_forward_traversal_list(localClusterNode); |
| 460 |
|
| 461 |
stk::mesh::EntityId globalId = m_elementClusters[pcInfo.clusterIndex].globalId; |
| 462 |
for(const auto& clusterNode : clusterList) { |
| 463 |
globalId = std::min(globalId, clusterNode.clusterMinElemId); |
| 464 |
} |
| 465 |
|
| 466 |
m_elementClusters[pcInfo.clusterIndex].globalId = globalId; |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
void fill_global_cluster_graph_edges(const std::vector<ParallelClusterInfo>& pcInfoVec, |
| 471 |
std::vector<ClusterGraphEdge>& globalClusterEdges) |
| 472 |
{ |
| 473 |
GlobalClusterConnectivityHelper helper(m_bulk); |
| 474 |
helper.fill_global_cluster_graph_edges(pcInfoVec, globalClusterEdges); |
| 475 |
} |
| 476 |
|
| 477 |
void fill_cluster_graph_data(const std::vector<ClusterGraphEdge>& clusterEdges, |
| 478 |
ClusterGraphData& clusterGraphData) |
| 479 |
{ |
| 480 |
for(const ClusterGraphEdge& clusterEdge : clusterEdges) { |
| 481 |
std::vector<ClusterGraphNode>& node1Entries = clusterGraphData[clusterEdge.node1]; |
| 482 |
std::vector<ClusterGraphNode>& node2Entries = clusterGraphData[clusterEdge.node2]; |
| 483 |
|
| 484 |
stk::util::insert_keep_sorted_and_unique(clusterEdge.node2, node1Entries); |
| 485 |
stk::util::insert_keep_sorted_and_unique(clusterEdge.node1, node2Entries); |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
void update_global_cluster_ids(const std::vector<ParallelClusterInfo>& pcInfoVec) |
| 490 |
{ |
| 491 |
std::vector<ClusterGraphEdge> globalClusterEdges; |
| 492 |
fill_global_cluster_graph_edges(pcInfoVec, globalClusterEdges); |
| 493 |
|
| 494 |
ClusterGraphData globalClusterGraphData; |
| 495 |
fill_cluster_graph_data(globalClusterEdges, globalClusterGraphData); |
| 496 |
|
| 497 |
ClusterGraph<ClusterGraphNode, ClusterGraphData> globalClusterGraph(globalClusterGraphData); |
| 498 |
update_global_cluster_ids_from_graph(pcInfoVec, globalClusterGraph); |
| 499 |
} |
| 500 |
|
| 501 |
void fill_neighbor_cluster_connectivity(std::vector<ParallelClusterInfo>& pcInfoVec) |
| 502 |
{ |
| 503 |
NeighborClusterConnectivityHelper helper(m_bulk); |
| 504 |
helper.fill_neighbor_cluster_connectivity(m_elementClusters, pcInfoVec); |
| 505 |
} |
| 506 |
|
| 507 |
void assign_global_cluster_ids() |
| 508 |
{ |
| 509 |
std::vector<ParallelClusterInfo> pcInfoVec; |
| 510 |
pcInfoVec.reserve(m_elementClusters.size()); |
| 511 |
|
| 512 |
for(size_t i=0; i<m_elementClusters.size(); i++) { |
| 513 |
pcInfoVec.emplace_back(i); |
| 514 |
|
| 515 |
ParallelClusterInfo& currentInfo = pcInfoVec.back(); |
| 516 |
fill_parallel_cluster_send_info(currentInfo); |
| 517 |
|
| 518 |
m_elementClusters[i].globalId = currentInfo.minElemId; |
| 519 |
} |
| 520 |
|
| 521 |
fill_neighbor_cluster_connectivity(pcInfoVec); |
| 522 |
update_global_cluster_ids(pcInfoVec); |
| 523 |
} |
| 524 |
|
| 525 |
void create_local_element_clusters() |
| 526 |
{ |
| 527 |
stk::mesh::EntityVector elements; |
| 528 |
stk::mesh::get_selected_entities(m_bulk.mesh_meta_data().locally_owned_part(), m_bulk.buckets(stk::topology::ELEM_RANK), elements); |
| 529 |
|
| 530 |
for (stk::mesh::Entity element : elements) { |
| 531 |
m_visitedElements[element] = false; |
| 532 |
} |
| 533 |
|
| 534 |
size_t newSeed = get_new_seed(elements); |
| 535 |
|
| 536 |
while(newSeed != INVALID_SEED) { |
| 537 |
add_sorted_element_cluster_from_seed(newSeed, elements); |
| 538 |
newSeed = get_new_seed(elements); |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
size_t get_new_seed(const stk::mesh::EntityVector& elements) |
| 543 |
{ |
| 544 |
for(size_t i=m_currentSeed; i<elements.size(); i++) { |
| 545 |
if(not m_visitedElements[elements[i]]) { |
| 546 |
return i; |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
return INVALID_SEED; |
| 551 |
} |
| 552 |
|
| 553 |
void update_cluster_queue_with_entry(stk::mesh::EntityVector& queue, stk::mesh::Entity entry) |
| 554 |
{ |
| 555 |
if(m_visitedElements[entry] || !m_bulk.bucket(entry).owned()) return; |
| 556 |
|
| 557 |
stk::mesh::ElemElemGraph& graph = m_bulk.get_face_adjacent_element_graph(); |
| 558 |
stk::mesh::impl::LocalId localId = graph.get_local_element_id(entry); |
| 559 |
for(const stk::mesh::GraphEdge &graphEdge : graph.get_edges_for_element(localId)) { |
| 560 |
if(graphEdge.is_elem2_local()) { |
| 561 |
stk::mesh::Entity neighbor = graph.get_entity(graphEdge.elem2()); |
| 562 |
|
| 563 |
if(not m_visitedElements[neighbor]) { |
| 564 |
queue.push_back(neighbor); |
| 565 |
|
| 566 |
#if defined(DEBUG) |
| 567 |
debugStream_ << "\t" << m_bulk.entity_key(neighbor) |
| 568 |
<< " [" << m_bulk.bucket(neighbor).topology() << "]" << std::endl; |
| 569 |
#endif |
| 570 |
} |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
m_visitedElements[entry] = true; |
| 575 |
} |
| 576 |
|
| 577 |
void add_sorted_element_cluster_from_seed(const size_t newSeed, const stk::mesh::EntityVector& elements) |
| 578 |
{ |
| 579 |
if(m_visitedElements[elements[newSeed]]) { |
| 580 |
return; |
| 581 |
} |
| 582 |
|
| 583 |
m_currentSeed = newSeed; |
| 584 |
|
| 585 |
m_elementClusters.emplace_back(); |
| 586 |
ElementCluster& cluster = m_elementClusters.back(); |
| 587 |
|
| 588 |
cluster.elements.reserve(elements.size() - m_currentNumVisitedElements); |
| 589 |
cluster.elements.push_back(elements[m_currentSeed]); |
| 590 |
|
| 591 |
#if defined(DEBUG) |
| 592 |
debugStream_ << "Cluster: " << m_elementClusters.size()-1 << std::endl; |
| 593 |
debugStream_ << "\t" << m_bulk.entity_key(cluster.elements.back()) |
| 594 |
<< " [" << m_bulk.bucket(cluster.elements.back()).topology() << "]" << std::endl; |
| 595 |
#endif |
| 596 |
|
| 597 |
for(size_t i=0; i<cluster.elements.size(); i++) { |
| 598 |
update_cluster_queue_with_entry(cluster.elements, cluster.elements[i]); |
| 599 |
} |
| 600 |
|
| 601 |
cluster.elements.resize(cluster.elements.size()); |
| 602 |
std::sort(cluster.elements.begin(), cluster.elements.end(), stk::mesh::EntityLess(m_bulk)); |
| 603 |
|
| 604 |
m_currentNumVisitedElements += cluster.elements.size(); |
| 605 |
|
| 606 |
#if defined(DEBUG) |
| 607 |
debugStream_ << std::endl; |
| 608 |
#endif |
| 609 |
} |
| 610 |
|
| 611 |
MeshTraverser() = delete; |
| 612 |
MeshTraverser(const MeshTraverser&) = delete; |
| 613 |
|
| 614 |
stk::mesh::BulkData& m_bulk; |
| 615 |
size_t m_currentSeed = 0; |
| 616 |
size_t m_currentNumVisitedElements = 0; |
| 617 |
std::unordered_map<stk::mesh::Entity, bool> m_visitedElements; |
| 618 |
std::vector<ElementCluster> m_elementClusters; |
| 619 |
|
| 620 |
#if defined(DEBUG) |
| 621 |
std::ostringstream debugStream_; |
| 622 |
#endif |
| 623 |
}; |
| 624 |
|
| 625 |
stk::mesh::EntityIdVector convert_entities_to_sorted_ids(const stk::mesh::BulkData& bulk, const stk::mesh::EntityVector& entities) |
| 626 |
{ |
| 627 |
stk::mesh::EntityIdVector ids; |
| 628 |
for(stk::mesh::Entity element : entities) { |
| 629 |
ids.push_back(bulk.identifier(element)); |
| 630 |
} |
| 631 |
std::sort(ids.begin(), ids.end()); |
| 632 |
|
| 633 |
return ids; |
| 634 |
} |
| 635 |
|
| 636 |
void test_clusters(const stk::mesh::BulkData& bulk, const std::vector<ElementCluster>& clusters, const ElementClusterVector& goldIds) |
| 637 |
{ |
| 638 |
EXPECT_EQ(goldIds.size(), clusters.size()); |
| 639 |
|
| 640 |
for(size_t i=0; i<goldIds.size(); i++) { |
| 641 |
EXPECT_EQ(goldIds[i].globalId, clusters[i].globalId); |
| 642 |
|
| 643 |
const stk::mesh::EntityIdVector& goldClusterIds = goldIds[i].elements; |
| 644 |
const stk::mesh::EntityVector& clusterElements = clusters[i].elements; |
| 645 |
|
| 646 |
stk::mesh::EntityIdVector clusterIds = convert_entities_to_sorted_ids(bulk, clusterElements); |
| 647 |
|
| 648 |
for(stk::mesh::EntityId goldClusterId : goldClusterIds) { |
| 649 |
EXPECT_TRUE(std::binary_search(clusterIds.begin(), clusterIds.end(), goldClusterId)); |
| 650 |
} |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
void run_traverser(const std::string& meshDesc, const ElementClusterVector& goldIds, const unsigned dim = 3) |
| 655 |
{ |
| 656 |
std::shared_ptr<stk::mesh::BulkData> bulk = build_mesh(dim,MPI_COMM_WORLD); |
| 657 |
stk::unit_test_util::setup_text_mesh(*bulk, meshDesc); |
| 658 |
|
| 659 |
MeshTraverser bfs(*bulk); |
| 660 |
test_clusters(*bulk, bfs.get_clusters(), goldIds); |
| 661 |
} |
| 662 |
|
| 663 |
TEST(Mechanism, twoHexConnected_Serial) |
| 664 |
{ |
| 665 |
if(stk::parallel_machine_size(MPI_COMM_WORLD) != 1) { GTEST_SKIP(); } |
| 666 |
std::string meshDesc = "0,1,HEX_8,1,2,3,4,5,6,7,8\n" |
| 667 |
"0,2,HEX_8,5,6,7,8,9,10,11,12"; |
| 668 |
|
| 669 |
ElementIdCluster cluster(1, stk::mesh::EntityIdVector{1,2}); |
| 670 |
|
| 671 |
run_traverser(meshDesc, ElementClusterVector{cluster}); |
| 672 |
} |
| 673 |
|
| 674 |
TEST(Mechanism, twoHexConnected_Parallel) |
| 675 |
{ |
| 676 |
if(stk::parallel_machine_size(MPI_COMM_WORLD) != 2) { GTEST_SKIP(); } |
| 677 |
std::string meshDesc = "0,1,HEX_8,1,2,3,4,5,6,7,8\n" |
| 678 |
"1,2,HEX_8,5,6,7,8,9,10,11,12"; |
| 679 |
|
| 680 |
unsigned myProc = stk::parallel_machine_rank(MPI_COMM_WORLD); |
| 681 |
ElementIdCluster cluster{1, stk::mesh::EntityIdVector{myProc+1}}; |
| 682 |
|
| 683 |
run_traverser(meshDesc, ElementClusterVector{cluster}); |
| 684 |
} |
| 685 |
|
| 686 |
TEST(Mechanism, threeHexWithTwoConnectedAcrossProcessorBoundary_Parallel) |
| 687 |
{ |
| 688 |
if(stk::parallel_machine_size(MPI_COMM_WORLD) != 2) { GTEST_SKIP(); } |
| 689 |
std::string meshDesc = "0,1,HEX_8,1,2,3,4,5,6,7,8\n" |
| 690 |
"0,2,HEX_8,9,10,11,12,13,14,15,16\n" |
| 691 |
"1,3,HEX_8,13,14,15,16,17,18,19,20"; |
| 692 |
|
| 693 |
ElementClusterVector goldIds; |
| 694 |
if(stk::parallel_machine_rank(MPI_COMM_WORLD) == 0) { |
| 695 |
ElementIdCluster cluster1{1, stk::mesh::EntityIdVector{1}}; |
| 696 |
ElementIdCluster cluster2{2, stk::mesh::EntityIdVector{2}}; |
| 697 |
goldIds = ElementClusterVector{cluster1, cluster2}; |
| 698 |
} else { |
| 699 |
ElementIdCluster cluster{2, stk::mesh::EntityIdVector{3}}; |
| 700 |
goldIds = ElementClusterVector{cluster}; |
| 701 |
} |
| 702 |
run_traverser(meshDesc, goldIds); |
| 703 |
} |
| 704 |
|
| 705 |
TEST(Mechanism, twoHexDisconnected_Serial) |
| 706 |
{ |
| 707 |
if(stk::parallel_machine_size(MPI_COMM_WORLD) != 1) { GTEST_SKIP(); } |
| 708 |
std::string meshDesc = "0,1,HEX_8,1,2,3,4,5,6,7,8\n" |
| 709 |
"0,2,HEX_8,9,10,11,12,13,14,15,16"; |
| 710 |
|
| 711 |
ElementIdCluster cluster1(1, stk::mesh::EntityIdVector{1}); |
| 712 |
ElementIdCluster cluster2(2, stk::mesh::EntityIdVector{2}); |
| 713 |
run_traverser(meshDesc, ElementClusterVector{cluster1, cluster2}); |
| 714 |
} |
| 715 |
|
| 716 |
TEST(Mechanism, threeHexConnected_Serial) |
| 717 |
{ |
| 718 |
if(stk::parallel_machine_size(MPI_COMM_WORLD) != 1) { GTEST_SKIP(); } |
| 719 |
std::string meshDesc = |
| 720 |
"0,1,HEX_8,1,2,3,4,5,6,7,8\n" |
| 721 |
"0,2,HEX_8,5,6,7,8,9,10,11,12\n" |
| 722 |
"0,3,HEX_8,9,10,11,12,13,14,15,16"; |
| 723 |
|
| 724 |
ElementIdCluster cluster(1, stk::mesh::EntityIdVector{1,2,3}); |
| 725 |
run_traverser(meshDesc, ElementClusterVector{cluster}); |
| 726 |
} |
| 727 |
|
| 728 |
TEST(Mechanism, threeHexConnected_Parallel) |
| 729 |
{ |
| 730 |
if(stk::parallel_machine_size(MPI_COMM_WORLD) != 3) { GTEST_SKIP(); } |
| 731 |
std::string meshDesc = |
| 732 |
"0,1,HEX_8,1,2,3,4,5,6,7,8\n" |
| 733 |
"1,2,HEX_8,5,6,7,8,9,10,11,12\n" |
| 734 |
"2,3,HEX_8,9,10,11,12,13,14,15,16"; |
| 735 |
|
| 736 |
unsigned myProc = stk::parallel_machine_rank(MPI_COMM_WORLD); |
| 737 |
ElementIdCluster cluster{1, stk::mesh::EntityIdVector{myProc+1}}; |
| 738 |
|
| 739 |
run_traverser(meshDesc, ElementClusterVector{cluster}); |
| 740 |
} |
| 741 |
|
| 742 |
TEST(Mechanism, fourHexConnected_Parallel) |
| 743 |
{ |
| 744 |
if(stk::parallel_machine_size(MPI_COMM_WORLD) != 4) { GTEST_SKIP(); } |
| 745 |
std::string meshDesc = |
| 746 |
"0,1,HEX_8,1,2,3,4,5,6,7,8\n" |
| 747 |
"1,2,HEX_8,5,6,7,8,9,10,11,12\n" |
| 748 |
"2,3,HEX_8,9,10,11,12,13,14,15,16\n" |
| 749 |
"3,4,HEX_8,13,14,15,16,17,18,19,20"; |
| 750 |
|
| 751 |
unsigned myProc = stk::parallel_machine_rank(MPI_COMM_WORLD); |
| 752 |
ElementIdCluster cluster{1, stk::mesh::EntityIdVector{myProc+1}}; |
| 753 |
|
| 754 |
run_traverser(meshDesc, ElementClusterVector{cluster}); |
| 755 |
} |
| 756 |
|
| 757 |
TEST(Mechanism, threeHexDisconnected_Serial) |
| 758 |
{ |
| 759 |
if(stk::parallel_machine_size(MPI_COMM_WORLD) != 1) { GTEST_SKIP(); } |
| 760 |
std::string meshDesc = |
| 761 |
"0,1,HEX_8,1,2,3,4,5,6,7,8\n" |
| 762 |
"0,2,HEX_8,9,10,11,12,13,14,15,16\n" |
| 763 |
"0,3,HEX_8,17,18,19,20,21,22,23,24"; |
| 764 |
|
| 765 |
ElementIdCluster cluster1(1, stk::mesh::EntityIdVector{1}); |
| 766 |
ElementIdCluster cluster2(2, stk::mesh::EntityIdVector{2}); |
| 767 |
ElementIdCluster cluster3(3, stk::mesh::EntityIdVector{3}); |
| 768 |
run_traverser(meshDesc, ElementClusterVector{cluster1, cluster2, cluster3}); |
| 769 |
} |
| 770 |
|
| 771 |
TEST(Mechanism, connectedPyramidAndHexAndTet_Serial) |
| 772 |
{ |
| 773 |
if(stk::parallel_machine_size(MPI_COMM_WORLD) != 1) { GTEST_SKIP(); } |
| 774 |
std::string meshDesc = |
| 775 |
"0,1,PYRAMID_5,1,2,3,4,5\n" |
| 776 |
"0,2,HEX_8,1,4,3,2,6,9,8,7\n" |
| 777 |
"0,3,TET_4,2,3,5,10"; |
| 778 |
|
| 779 |
ElementIdCluster cluster(1, stk::mesh::EntityIdVector{1,2,3}); |
| 780 |
run_traverser(meshDesc, ElementClusterVector{cluster}); |
| 781 |
} |
| 782 |
|
| 783 |
TEST(Mechanism, connectedPyramidAndHexAndDisconnectedTet_Serial) |
| 784 |
{ |
| 785 |
if(stk::parallel_machine_size(MPI_COMM_WORLD) != 1) { GTEST_SKIP(); } |
| 786 |
std::string meshDesc = |
| 787 |
"0,1,PYRAMID_5,1,2,3,4,5\n" |
| 788 |
"0,2,HEX_8,1,4,3,2,6,9,8,7\n" |
| 789 |
"0,3,TET_4,10,11,12,13"; |
| 790 |
|
| 791 |
ElementIdCluster cluster1(1, stk::mesh::EntityIdVector{1,2}); |
| 792 |
ElementIdCluster cluster2(3, stk::mesh::EntityIdVector{3}); |
| 793 |
run_traverser(meshDesc, ElementClusterVector{cluster1, cluster2}); |
| 794 |
} |
| 795 |
|
| 796 |
TEST(Mechanism, disconnectedPyramidAndHexAndTet_Serial) |
| 797 |
{ |
| 798 |
if(stk::parallel_machine_size(MPI_COMM_WORLD) != 1) { GTEST_SKIP(); } |
| 799 |
std::string meshDesc = |
| 800 |
"0,1,PYRAMID_5,1,2,3,4,5\n" |
| 801 |
"0,2,HEX_8,6,7,8,9,10,11,12,13\n" |
| 802 |
"0,3,TET_4,14,15,16,17"; |
| 803 |
|
| 804 |
ElementIdCluster cluster1(1, stk::mesh::EntityIdVector{1}); |
| 805 |
ElementIdCluster cluster2(2, stk::mesh::EntityIdVector{2}); |
| 806 |
ElementIdCluster cluster3(3, stk::mesh::EntityIdVector{3}); |
| 807 |
run_traverser(meshDesc, ElementClusterVector{cluster1, cluster2, cluster3}); |
| 808 |
} |
| 809 |
|
| 810 |
TEST(Mechanism, fourQuadBowtie_Serial) |
| 811 |
{ |
| 812 |
if(stk::parallel_machine_size(MPI_COMM_WORLD) != 1) { GTEST_SKIP(); } |
| 813 |
std::string meshDesc = "0,1,QUAD_4_2D,1,2,4,3,block_1\n" |
| 814 |
"0,2,QUAD_4_2D,5,6,7,4,block_1\n" |
| 815 |
"0,3,QUAD_4_2D,4,8,9,10,block_1\n" |
| 816 |
"0,4,QUAD_4_2D,4,11,12,13,block_1"; |
| 817 |
|
| 818 |
ElementIdCluster cluster1(1, stk::mesh::EntityIdVector{1}); |
| 819 |
ElementIdCluster cluster2(2, stk::mesh::EntityIdVector{2}); |
| 820 |
ElementIdCluster cluster3(3, stk::mesh::EntityIdVector{3}); |
| 821 |
ElementIdCluster cluster4(4, stk::mesh::EntityIdVector{4}); |
| 822 |
run_traverser(meshDesc, ElementClusterVector{cluster1, cluster2, cluster3, cluster4}, 2u); |
| 823 |
} |
| 824 |
|
| 825 |
TEST(Mechanism, fourQuadPacman_Serial) |
| 826 |
{ |
| 827 |
if(stk::parallel_machine_size(MPI_COMM_WORLD) != 1) { GTEST_SKIP(); } |
| 828 |
std::string meshDesc = "0,1,QUAD_4_2D,1,2,4,3,block_1\n" |
| 829 |
"0,2,QUAD_4_2D,2,5,6,4,block_1\n" |
| 830 |
"0,3,QUAD_4_2D,4,7,8,9,block_1\n" |
| 831 |
"0,4,QUAD_4_2D,3,4,9,10,block_1"; |
| 832 |
|
| 833 |
ElementIdCluster cluster(1, stk::mesh::EntityIdVector{1,2,3,4}); |
| 834 |
run_traverser(meshDesc, ElementClusterVector{cluster}, 2u); |
| 835 |
} |