| 1 |
#include <gtest/gtest.h> // for AssertHelper, EXPECT_TRUE, etc |
| 2 |
#include <stddef.h> // for size_t |
| 3 |
#include <stk_mesh/base/BulkData.hpp> // for BulkData, etc |
| 4 |
#include <stk_mesh/base/MetaData.hpp> // for MetaData |
| 5 |
#include <stk_mesh/base/ModificationObserver.hpp> |
| 6 |
#include <stk_unit_test_utils/ioUtils.hpp> // for fill_mesh_using_stk_io |
| 7 |
#include <stk_unit_test_utils/BuildMesh.hpp> |
| 8 |
#include <vector> // for vector, vector<>::reference |
| 9 |
#include "mpi.h" // for MPI_COMM_WORLD, MPI_Comm, etc |
| 10 |
#include "stk_mesh/base/Entity.hpp" // for Entity |
| 11 |
#include "stk_mesh/base/EntityKey.hpp" // for EntityKey |
| 12 |
#include "stk_mesh/base/Types.hpp" // for EntityRank, EntityProc, etc |
| 13 |
#include "stk_topology/topology.hpp" // for topology, etc |
| 14 |
#include "stk_util/parallel/Parallel.hpp" // for parallel_machine_size |
| 15 |
namespace stk { namespace mesh { class Ghosting; } } |
| 16 |
|
| 17 |
namespace |
| 18 |
{ |
| 19 |
|
| 20 |
using stk::unit_test_util::build_mesh; |
| 21 |
|
| 22 |
class MockCommInfoObserver : public stk::mesh::ModificationObserver |
| 23 |
{ |
| 24 |
public: |
| 25 |
MockCommInfoObserver() |
| 26 |
: stk::mesh::ModificationObserver(stk::mesh::ModificationObserverPriority::APPLICATION), |
| 27 |
commInfoWasChangedByRank(stk::topology::NUM_RANKS, false) |
| 28 |
{ |
| 29 |
} |
| 30 |
|
| 31 |
virtual void local_entity_comm_info_changed_notification(stk::mesh::EntityRank rank) |
| 32 |
{ |
| 33 |
commInfoWasChangedByRank[rank] = true; |
| 34 |
} |
| 35 |
|
| 36 |
bool was_comm_info_changed(stk::mesh::EntityRank rank) const |
| 37 |
{ |
| 38 |
return commInfoWasChangedByRank[rank]; |
| 39 |
} |
| 40 |
|
| 41 |
void reset_comm_info_status() |
| 42 |
{ |
| 43 |
for(size_t i=0; i < commInfoWasChangedByRank.size(); i++) |
| 44 |
{ |
| 45 |
commInfoWasChangedByRank[i] = false; |
| 46 |
} |
| 47 |
} |
| 48 |
private: |
| 49 |
std::vector<bool> commInfoWasChangedByRank; |
| 50 |
}; |
| 51 |
|
| 52 |
class CommInfoObserverTest : public ::testing::Test |
| 53 |
{ |
| 54 |
protected: |
| 55 |
CommInfoObserverTest() |
| 56 |
: bulkPtr(build_mesh(MPI_COMM_WORLD, stk::mesh::BulkData::NO_AUTO_AURA)), |
| 57 |
meta(bulkPtr->mesh_meta_data()), |
| 58 |
bulk(*bulkPtr) |
| 59 |
{ |
| 60 |
stk::io::fill_mesh("generated:1x1x4", bulk); |
| 61 |
|
| 62 |
observer = std::make_shared<MockCommInfoObserver>(); |
| 63 |
bulk.register_observer(observer); |
| 64 |
|
| 65 |
bulk.modification_begin(); |
| 66 |
ghost = &bulk.create_ghosting("Clyde"); |
| 67 |
bulk.modification_end(); |
| 68 |
} |
| 69 |
|
| 70 |
virtual ~CommInfoObserverTest() |
| 71 |
{ |
| 72 |
} |
| 73 |
|
| 74 |
void ghost_element1_to_proc1_and_check_for_comm_info_change() |
| 75 |
{ |
| 76 |
ghost_element1_to_proc1(); |
| 77 |
expect_comm_info_changed_for_nodes_and_elements_only(); |
| 78 |
} |
| 79 |
|
| 80 |
void expect_comm_info_changed_for_nodes_and_elements_only() |
| 81 |
{ |
| 82 |
EXPECT_TRUE(observer->was_comm_info_changed(stk::topology::NODE_RANK)); |
| 83 |
EXPECT_TRUE(observer->was_comm_info_changed(stk::topology::ELEM_RANK)); |
| 84 |
|
| 85 |
EXPECT_TRUE(!observer->was_comm_info_changed(stk::topology::EDGE_RANK)); |
| 86 |
EXPECT_TRUE(!observer->was_comm_info_changed(stk::topology::FACE_RANK)); |
| 87 |
EXPECT_TRUE(!observer->was_comm_info_changed(stk::topology::CONSTRAINT_RANK)); |
| 88 |
} |
| 89 |
|
| 90 |
void add_element1_and_nodes_to_remove_from_ghosting(std::vector<stk::mesh::EntityKey>& entityKeysToStopGhosting) |
| 91 |
{ |
| 92 |
if(bulk.parallel_rank() == 1) |
| 93 |
{ |
| 94 |
entityKeysToStopGhosting.push_back(stk::mesh::EntityKey(stk::topology::ELEM_RANK, 1)); |
| 95 |
for(int nodeId=1; nodeId <= 8; nodeId++) |
| 96 |
{ |
| 97 |
entityKeysToStopGhosting.push_back(stk::mesh::EntityKey(stk::topology::NODE_RANK, nodeId)); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
void stop_ghosting_of_element1_to_proc1(bool testDuplicateRemoveGhostKeys) |
| 103 |
{ |
| 104 |
std::vector<stk::mesh::EntityKey> entityKeysToStopGhosting; |
| 105 |
add_element1_and_nodes_to_remove_from_ghosting(entityKeysToStopGhosting); |
| 106 |
if (testDuplicateRemoveGhostKeys) { |
| 107 |
add_element1_and_nodes_to_remove_from_ghosting(entityKeysToStopGhosting); |
| 108 |
} |
| 109 |
|
| 110 |
bulk.modification_begin(); |
| 111 |
bulk.change_ghosting(*ghost, {}, entityKeysToStopGhosting); |
| 112 |
bulk.modification_end(); |
| 113 |
} |
| 114 |
|
| 115 |
void reset_comm_info_status() |
| 116 |
{ |
| 117 |
observer->reset_comm_info_status(); |
| 118 |
expect_comm_info_is_reset(); |
| 119 |
} |
| 120 |
|
| 121 |
void destroy_ghosting() |
| 122 |
{ |
| 123 |
bulk.modification_begin(); |
| 124 |
bulk.destroy_all_ghosting(); |
| 125 |
bulk.modification_end(); |
| 126 |
} |
| 127 |
|
| 128 |
private: |
| 129 |
void ghost_element1_to_proc1() |
| 130 |
{ |
| 131 |
stk::mesh::EntityProcVec entitiesToGhost; |
| 132 |
if(bulk.parallel_rank() == 0) |
| 133 |
{ |
| 134 |
stk::mesh::Entity element1 = bulk.get_entity(stk::topology::ELEM_RANK, 1); |
| 135 |
int destProc = 1; |
| 136 |
entitiesToGhost.push_back(stk::mesh::EntityProc(element1, destProc)); |
| 137 |
} |
| 138 |
bulk.modification_begin(); |
| 139 |
bulk.change_ghosting(*ghost, entitiesToGhost); |
| 140 |
bulk.modification_end(); |
| 141 |
} |
| 142 |
|
| 143 |
void expect_comm_info_is_reset() |
| 144 |
{ |
| 145 |
for(stk::mesh::EntityRank rank = stk::topology::BEGIN_RANK; rank < stk::topology::END_RANK; rank++) |
| 146 |
{ |
| 147 |
EXPECT_TRUE(!observer->was_comm_info_changed(rank)); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
private: |
| 152 |
std::shared_ptr<stk::mesh::BulkData> bulkPtr; |
| 153 |
stk::mesh::MetaData& meta; |
| 154 |
stk::mesh::BulkData& bulk; |
| 155 |
std::shared_ptr<MockCommInfoObserver> observer; |
| 156 |
stk::mesh::Ghosting *ghost; |
| 157 |
}; |
| 158 |
|
| 159 |
TEST_F(CommInfoObserverTest, addAndRemoveFromGhosting) |
| 160 |
{ |
| 161 |
MPI_Comm comm = MPI_COMM_WORLD; |
| 162 |
if(stk::parallel_machine_size(comm) == 2) |
| 163 |
{ |
| 164 |
ghost_element1_to_proc1_and_check_for_comm_info_change(); |
| 165 |
|
| 166 |
reset_comm_info_status(); |
| 167 |
|
| 168 |
bool testDuplicateRemoveGhostKeys = false; |
| 169 |
stop_ghosting_of_element1_to_proc1(testDuplicateRemoveGhostKeys); |
| 170 |
expect_comm_info_changed_for_nodes_and_elements_only(); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
TEST_F(CommInfoObserverTest, addAndRemoveFromGhosting_testDuplicateKeys) |
| 175 |
{ |
| 176 |
MPI_Comm comm = MPI_COMM_WORLD; |
| 177 |
if(stk::parallel_machine_size(comm) == 2) |
| 178 |
{ |
| 179 |
ghost_element1_to_proc1_and_check_for_comm_info_change(); |
| 180 |
|
| 181 |
reset_comm_info_status(); |
| 182 |
|
| 183 |
bool testDuplicateRemoveGhostKeys = true; |
| 184 |
stop_ghosting_of_element1_to_proc1(testDuplicateRemoveGhostKeys); |
| 185 |
expect_comm_info_changed_for_nodes_and_elements_only(); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
TEST_F(CommInfoObserverTest, destroyGhosting) |
| 190 |
{ |
| 191 |
MPI_Comm comm = MPI_COMM_WORLD; |
| 192 |
if(stk::parallel_machine_size(comm) == 2) |
| 193 |
{ |
| 194 |
ghost_element1_to_proc1_and_check_for_comm_info_change(); |
| 195 |
|
| 196 |
reset_comm_info_status(); |
| 197 |
|
| 198 |
destroy_ghosting(); |
| 199 |
expect_comm_info_changed_for_nodes_and_elements_only(); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
} |