| 1 |
|
| 2 |
#include <gtest/gtest.h> // for AssertHelper, EXPECT_EQ, etc |
| 3 |
#include <stk_mesh/base/BulkData.hpp> // for BulkData, etc |
| 4 |
#include <stk_mesh/base/GetEntities.hpp> // for count_selected_entities, etc |
| 5 |
#include <stk_mesh/base/MetaData.hpp> // for MetaData |
| 6 |
#include <stk_mesh/baseImpl/elementGraph/ProcessKilledElements.hpp> |
| 7 |
#include <stk_mesh/baseImpl/elementGraph/ElemElemGraphUpdater.hpp> |
| 8 |
#include <stk_topology/topology.hpp> // for topology, etc |
| 9 |
#include <stk_util/parallel/Parallel.hpp> // for parallel_machine_rank, etc |
| 10 |
#include <vector> // for vector |
| 11 |
#include "BulkDataElementGraphTester.hpp" |
| 12 |
#include "ElementGraphTester.hpp" // for ElemElemGraphTester |
| 13 |
#include "mpi.h" // for MPI_COMM_WORLD, etc |
| 14 |
#include "stk_mesh/base/Entity.hpp" // for Entity |
| 15 |
#include "stk_mesh/base/Types.hpp" // for EntityVector, PartVector |
| 16 |
#include "stk_unit_test_utils/ioUtils.hpp" // for fill_mesh_using_stk_io |
| 17 |
#include "stk_unit_test_utils/unittestMeshUtils.hpp" |
| 18 |
#include <stk_mesh/base/ModificationObserver.hpp> |
| 19 |
|
| 20 |
namespace stk { namespace mesh { class Part; } } |
| 21 |
|
| 22 |
namespace |
| 23 |
{ |
| 24 |
|
| 25 |
unsigned count_elements(const stk::mesh::BulkData &bulk, stk::mesh::Part &activePart) |
| 26 |
{ |
| 27 |
std::vector<size_t> countsPerEntityType; |
| 28 |
stk::mesh::count_entities(activePart, bulk, countsPerEntityType); |
| 29 |
return countsPerEntityType[stk::topology::ELEMENT_RANK]; |
| 30 |
} |
| 31 |
|
| 32 |
unsigned get_gold_num_elements_by_proc(int procRank, int procRankToKillElementsOn) |
| 33 |
{ |
| 34 |
unsigned goldNumElements = 2u; |
| 35 |
if (procRank == procRankToKillElementsOn) |
| 36 |
{ |
| 37 |
goldNumElements = 1u; |
| 38 |
} |
| 39 |
return goldNumElements; |
| 40 |
} |
| 41 |
|
| 42 |
// Test cases: (all cases must involve a "element death" starter) |
| 43 |
// * create neighbor then deactivate => "refinement" |
| 44 |
// * move processor boundary (change_entity_owner) => rebalance |
| 45 |
// ? framework change global id ? |
| 46 |
|
| 47 |
class ElementDeathMock |
| 48 |
{ |
| 49 |
public: |
| 50 |
ElementDeathMock(BulkDataElementGraphTester &bulk_, stk::mesh::Part &activePart_, stk::mesh::Part &boundaryPart_) : |
| 51 |
bulk(bulk_), |
| 52 |
activePart(activePart_), |
| 53 |
boundaryPart(boundaryPart_) |
| 54 |
{ } |
| 55 |
|
| 56 |
void kill_elements(stk::mesh::EntityVector &elementsToKill) |
| 57 |
{ |
| 58 |
deactivate_elements(elementsToKill); |
| 59 |
|
| 60 |
stk::mesh::impl::ParallelSelectedInfo remoteActiveSelector; |
| 61 |
stk::mesh::impl::populate_selected_value_for_remote_elements(bulk, bulk.get_face_adjacent_element_graph(), activePart, remoteActiveSelector); |
| 62 |
|
| 63 |
stk::mesh::PartVector newlyCreatedBoundaryFacesGetAddedToTheseParts = {&boundaryPart}; |
| 64 |
stk::mesh::PartVector exposedButExistingBoundaryFacesGetAddedToTheseParts = {&boundaryPart}; |
| 65 |
process_killed_elements(bulk, |
| 66 |
elementsToKill, |
| 67 |
activePart, |
| 68 |
remoteActiveSelector, |
| 69 |
newlyCreatedBoundaryFacesGetAddedToTheseParts, |
| 70 |
&exposedButExistingBoundaryFacesGetAddedToTheseParts); |
| 71 |
} |
| 72 |
private: |
| 73 |
void deactivate_elements(stk::mesh::EntityVector elementsToDeactivate) |
| 74 |
{ |
| 75 |
bulk.modification_begin(); |
| 76 |
for(stk::mesh::Entity element : elementsToDeactivate) |
| 77 |
{ |
| 78 |
deactivate_element(element); |
| 79 |
} |
| 80 |
bulk.modification_end(); |
| 81 |
} |
| 82 |
|
| 83 |
void deactivate_element(stk::mesh::Entity elementToDeactivate) |
| 84 |
{ |
| 85 |
bulk.change_entity_parts(elementToDeactivate, stk::mesh::ConstPartVector{}, stk::mesh::ConstPartVector{&activePart}); |
| 86 |
} |
| 87 |
private: |
| 88 |
BulkDataElementGraphTester &bulk; |
| 89 |
stk::mesh::Part &activePart; |
| 90 |
stk::mesh::Part &boundaryPart; |
| 91 |
}; |
| 92 |
|
| 93 |
class MeshRefinementMock |
| 94 |
{ |
| 95 |
public: |
| 96 |
MeshRefinementMock(stk::mesh::BulkData &bulk_, stk::mesh::Part &activePart_) |
| 97 |
: bulk(bulk_), activePart(activePart_) |
| 98 |
{ |
| 99 |
} |
| 100 |
|
| 101 |
void create_element() |
| 102 |
{ |
| 103 |
stk::mesh::EntityId newElementId = 5; |
| 104 |
stk::mesh::EntityIdVector nodesForNewElement = {17, 18, 20, 19, 21, 22, 24, 23}; |
| 105 |
stk::mesh::Part *block1Part = bulk.mesh_meta_data().get_part("block_1"); |
| 106 |
stk::mesh::PartVector elementParts = {block1Part, &activePart}; |
| 107 |
stk::mesh::declare_element(bulk, elementParts, newElementId, nodesForNewElement); |
| 108 |
} |
| 109 |
|
| 110 |
void create_element_on_proc1() |
| 111 |
{ |
| 112 |
bulk.modification_begin(); |
| 113 |
if(bulk.parallel_rank() == 1) |
| 114 |
{ |
| 115 |
create_element(); |
| 116 |
} |
| 117 |
bulk.modification_end(); |
| 118 |
} |
| 119 |
|
| 120 |
private: |
| 121 |
stk::mesh::BulkData &bulk; |
| 122 |
stk::mesh::Part &activePart; |
| 123 |
}; |
| 124 |
|
| 125 |
class UpdateElemElemGraphTest : public ::testing::Test |
| 126 |
{ |
| 127 |
public: |
| 128 |
UpdateElemElemGraphTest() : |
| 129 |
meta(3), |
| 130 |
bulk(meta, MPI_COMM_WORLD, stk::mesh::BulkData::NO_AUTO_AURA), |
| 131 |
activePart(meta.declare_part("active")), |
| 132 |
boundaryPart(meta.declare_part("boundary")) |
| 133 |
{ |
| 134 |
stk::io::fill_mesh("generated:1x1x4", bulk); |
| 135 |
stk::unit_test_util::put_mesh_into_part(bulk, activePart); |
| 136 |
} |
| 137 |
|
| 138 |
void add_element_to_deactivate_on_proc(int elementId, int procRankToKillElementsOn, stk::mesh::EntityVector &elementsDeactivated) |
| 139 |
{ |
| 140 |
int myProcRank = stk::parallel_machine_rank(bulk.parallel()); |
| 141 |
if(myProcRank == procRankToKillElementsOn) |
| 142 |
{ |
| 143 |
stk::mesh::Entity element = bulk.get_entity(stk::topology::ELEM_RANK, elementId); |
| 144 |
elementsDeactivated.push_back(element); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
void kill_element_1_on_proc_0(ElementDeathMock &elementKiller) |
| 149 |
{ |
| 150 |
stk::mesh::EntityVector elementsDeactivated; |
| 151 |
int element1Id = 1; |
| 152 |
int procRankToKillElementsOn = 0; |
| 153 |
add_element_to_deactivate_on_proc(element1Id, procRankToKillElementsOn, elementsDeactivated); |
| 154 |
|
| 155 |
elementKiller.kill_elements(elementsDeactivated); |
| 156 |
|
| 157 |
unsigned goldNumElements = get_gold_num_elements_by_proc(bulk.parallel_rank(), procRankToKillElementsOn); |
| 158 |
EXPECT_EQ(goldNumElements, count_elements(bulk, activePart)); |
| 159 |
} |
| 160 |
|
| 161 |
void kill_element_4_on_proc_1(ElementDeathMock &elementKiller) |
| 162 |
{ |
| 163 |
stk::mesh::EntityVector elementsDeactivated; |
| 164 |
int element4Id = 4; |
| 165 |
int procRankToKillElementsOn = 1; |
| 166 |
add_element_to_deactivate_on_proc(element4Id, procRankToKillElementsOn, elementsDeactivated); |
| 167 |
|
| 168 |
elementKiller.kill_elements(elementsDeactivated); |
| 169 |
} |
| 170 |
|
| 171 |
void move_element_3_to_proc_0() |
| 172 |
{ |
| 173 |
stk::mesh::EntityProcVec entityProcs; |
| 174 |
if(bulk.parallel_rank() == 1) |
| 175 |
{ |
| 176 |
stk::mesh::Entity element3 = bulk.get_entity(stk::topology::ELEMENT_RANK, 3); |
| 177 |
entityProcs.push_back(stk::mesh::EntityProc(element3, 0)); |
| 178 |
} |
| 179 |
bulk.change_entity_owner(entityProcs); |
| 180 |
} |
| 181 |
|
| 182 |
void delete_element_3_on_proc_1() |
| 183 |
{ |
| 184 |
bulk.modification_begin(); |
| 185 |
if(bulk.parallel_rank() == 1) |
| 186 |
{ |
| 187 |
stk::mesh::Entity element3 = bulk.get_entity(stk::topology::ELEMENT_RANK, 3); |
| 188 |
bulk.destroy_entity(element3); |
| 189 |
} |
| 190 |
bulk.modification_end(); |
| 191 |
} |
| 192 |
|
| 193 |
protected: |
| 194 |
stk::mesh::MetaData meta; |
| 195 |
BulkDataElementGraphTester bulk; |
| 196 |
stk::mesh::Part &activePart; |
| 197 |
stk::mesh::Part &boundaryPart; |
| 198 |
}; |
| 199 |
|
| 200 |
TEST_F(UpdateElemElemGraphTest, killRefineUpdateGraphKill) |
| 201 |
{ |
| 202 |
stk::ParallelMachine comm = MPI_COMM_WORLD; |
| 203 |
if(stk::parallel_machine_size(comm) == 2) |
| 204 |
{ |
| 205 |
ElementDeathMock elementKiller(bulk, activePart, boundaryPart); |
| 206 |
kill_element_1_on_proc_0(elementKiller); |
| 207 |
|
| 208 |
MeshRefinementMock meshRefinement(bulk, activePart); |
| 209 |
meshRefinement.create_element_on_proc1(); |
| 210 |
|
| 211 |
kill_element_4_on_proc_1(elementKiller); |
| 212 |
|
| 213 |
unsigned goldNumFaces = 1; |
| 214 |
unsigned goldNumElems = 1; |
| 215 |
if(bulk.parallel_rank() == 1) |
| 216 |
{ |
| 217 |
goldNumFaces = 2; |
| 218 |
goldNumElems = 2; |
| 219 |
} |
| 220 |
EXPECT_EQ(goldNumFaces, stk::mesh::count_selected_entities(activePart, bulk.buckets(stk::topology::FACE_RANK))); |
| 221 |
EXPECT_EQ(goldNumElems, stk::mesh::count_selected_entities(activePart, bulk.buckets(stk::topology::ELEM_RANK))); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
TEST_F(UpdateElemElemGraphTest, killChangeOwnerUpdateGraphKill) |
| 226 |
{ |
| 227 |
stk::ParallelMachine comm = MPI_COMM_WORLD; |
| 228 |
if(stk::parallel_machine_size(comm) == 2) |
| 229 |
{ |
| 230 |
ElementDeathMock elementKiller(bulk, activePart, boundaryPart); |
| 231 |
kill_element_1_on_proc_0(elementKiller); |
| 232 |
|
| 233 |
move_element_3_to_proc_0(); |
| 234 |
|
| 235 |
kill_element_4_on_proc_1(elementKiller); |
| 236 |
|
| 237 |
unsigned goldNumFaces = 2; |
| 238 |
unsigned goldNumElems = 2; |
| 239 |
if(bulk.parallel_rank() == 1) |
| 240 |
{ |
| 241 |
goldNumFaces = 1; |
| 242 |
goldNumElems = 0; |
| 243 |
} |
| 244 |
EXPECT_EQ(goldNumFaces, stk::mesh::count_selected_entities(activePart, bulk.buckets(stk::topology::FACE_RANK))); |
| 245 |
EXPECT_EQ(goldNumElems, stk::mesh::count_selected_entities(activePart, bulk.buckets(stk::topology::ELEM_RANK))); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
TEST_F(UpdateElemElemGraphTest, killDeleteUpdateGraphKill) |
| 250 |
{ |
| 251 |
stk::ParallelMachine comm = MPI_COMM_WORLD; |
| 252 |
if(stk::parallel_machine_size(comm) == 2) |
| 253 |
{ |
| 254 |
ElementDeathMock elementKiller(bulk, activePart, boundaryPart); |
| 255 |
kill_element_1_on_proc_0(elementKiller); |
| 256 |
|
| 257 |
delete_element_3_on_proc_1(); |
| 258 |
|
| 259 |
kill_element_4_on_proc_1(elementKiller); |
| 260 |
|
| 261 |
unsigned goldNumFaces = 1; |
| 262 |
unsigned goldNumElems = 1; |
| 263 |
if(bulk.parallel_rank() == 1) |
| 264 |
{ |
| 265 |
goldNumFaces = 0; |
| 266 |
goldNumElems = 0; |
| 267 |
} |
| 268 |
EXPECT_EQ(goldNumFaces, stk::mesh::count_selected_entities(activePart, bulk.buckets(stk::topology::FACE_RANK))); |
| 269 |
EXPECT_EQ(goldNumElems, stk::mesh::count_selected_entities(activePart, bulk.buckets(stk::topology::ELEM_RANK))); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
class ElemElemGraphUpdaterMock : public stk::mesh::ModificationObserver |
| 275 |
{ |
| 276 |
public: |
| 277 |
ElemElemGraphUpdaterMock() |
| 278 |
: stk::mesh::ModificationObserver(stk::mesh::ModificationObserverPriority::APPLICATION), |
| 279 |
numAdded(0) |
| 280 |
{ |
| 281 |
|
| 282 |
} |
| 283 |
|
| 284 |
virtual void entity_added(stk::mesh::Entity /*entity*/) |
| 285 |
{ |
| 286 |
numAdded++; |
| 287 |
} |
| 288 |
|
| 289 |
int get_num_entities_added() |
| 290 |
{ |
| 291 |
return numAdded; |
| 292 |
} |
| 293 |
private: |
| 294 |
int numAdded; |
| 295 |
}; |
| 296 |
|
| 297 |
TEST_F(UpdateElemElemGraphTest, NewEntityNotification) |
| 298 |
{ |
| 299 |
if(bulk.parallel_size() == 2) |
| 300 |
{ |
| 301 |
std::shared_ptr<ElemElemGraphUpdaterMock> observer = std::make_shared<ElemElemGraphUpdaterMock>(); |
| 302 |
bulk.register_observer(observer); |
| 303 |
|
| 304 |
MeshRefinementMock meshRefinement(bulk, activePart); |
| 305 |
meshRefinement.create_element_on_proc1(); |
| 306 |
|
| 307 |
if(bulk.parallel_rank() == 1) |
| 308 |
{ |
| 309 |
EXPECT_EQ(5, observer->get_num_entities_added()); |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
TEST_F(UpdateElemElemGraphTest, MultipleObservers) |
| 315 |
{ |
| 316 |
if(bulk.parallel_size() == 2) |
| 317 |
{ |
| 318 |
std::shared_ptr<ElemElemGraphUpdaterMock> observer1 = std::make_shared<ElemElemGraphUpdaterMock>(); |
| 319 |
bulk.register_observer(observer1); |
| 320 |
std::shared_ptr<ElemElemGraphUpdaterMock> observer2 = std::make_shared<ElemElemGraphUpdaterMock>(); |
| 321 |
bulk.register_observer(observer2); |
| 322 |
|
| 323 |
MeshRefinementMock meshRefinement(bulk, activePart); |
| 324 |
meshRefinement.create_element_on_proc1(); |
| 325 |
|
| 326 |
if(bulk.parallel_rank() == 1) |
| 327 |
{ |
| 328 |
EXPECT_EQ(5, observer1->get_num_entities_added()); |
| 329 |
EXPECT_EQ(5, observer2->get_num_entities_added()); |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
TEST(Observer, NewEntityNotification) |
| 335 |
{ |
| 336 |
std::shared_ptr<ElemElemGraphUpdaterMock> observer = std::make_shared<ElemElemGraphUpdaterMock>(); |
| 337 |
stk::mesh::Entity goldNewEntity(2); |
| 338 |
observer->entity_added(goldNewEntity); |
| 339 |
|
| 340 |
EXPECT_EQ(1, observer->get_num_entities_added()); |
| 341 |
} |
| 342 |
|
| 343 |
} // namespace |