| 1 |
#include <gtest/gtest.h> |
| 2 |
#include <vector> |
| 3 |
#include <stk_mesh/base/Comm.hpp> |
| 4 |
#include <stk_mesh/base/GetEntities.hpp> |
| 5 |
#include <stk_mesh/baseImpl/DeletedElementInfo.hpp> |
| 6 |
#include <stk_unit_test_utils/MeshFixture.hpp> |
| 7 |
#include <stk_util/parallel/ParallelReduce.hpp> |
| 8 |
#include "ElementGraphTester.hpp" |
| 9 |
namespace |
| 10 |
{ |
| 11 |
|
| 12 |
class ElemGraphDeleteElementsTester : public stk::unit_test_util::MeshFixture |
| 13 |
{ |
| 14 |
protected: |
| 15 |
ElemGraphDeleteElementsTester() : elementGraph(nullptr) { } |
| 16 |
~ElemGraphDeleteElementsTester() |
| 17 |
{ |
| 18 |
delete elementGraph; |
| 19 |
} |
| 20 |
|
| 21 |
void create_and_expect_graph_updated_after_elements_are_deleted(stk::mesh::BulkData::AutomaticAuraOption auraOption, |
| 22 |
stk::mesh::EntityIdVector elemIdsToDelete, |
| 23 |
size_t goldNumGlobalConnectionsAfterDeletes, |
| 24 |
GraphEdges &goldConnections) |
| 25 |
{ |
| 26 |
setup_mesh("generated:1x1x4", auraOption); |
| 27 |
elementGraph = new ElemElemGraphTester(get_bulk()); |
| 28 |
test(elemIdsToDelete, goldNumGlobalConnectionsAfterDeletes, goldConnections); |
| 29 |
} |
| 30 |
|
| 31 |
void test(const stk::mesh::EntityIdVector &elemIdsToDelete, size_t goldNumGlobalConnectionsAfterDeletes, GraphEdges &goldConnections) |
| 32 |
{ |
| 33 |
remove_elements(elemIdsToDelete); |
| 34 |
ASSERT_EQ(goldConnections.size(), goldNumGlobalConnectionsAfterDeletes); |
| 35 |
check_graph_connections_updated_after_deletions(goldConnections, goldNumGlobalConnectionsAfterDeletes); |
| 36 |
} |
| 37 |
|
| 38 |
void remove_elements(const stk::mesh::EntityIdVector &elemIdsToDelete) |
| 39 |
{ |
| 40 |
remove_inactive_elements(elemIdsToDelete); |
| 41 |
stk::mesh::impl::DeletedElementInfoVector elementsToDelete; |
| 42 |
fill_elements_to_delete(elemIdsToDelete, elementsToDelete); |
| 43 |
destroy_elements(elementsToDelete); |
| 44 |
elementGraph->delete_elements(elementsToDelete); |
| 45 |
} |
| 46 |
|
| 47 |
void remove_inactive_elements(const stk::mesh::EntityIdVector &elemIdsToDelete) |
| 48 |
{ |
| 49 |
for(stk::mesh::EntityId elemId : elemIdsToDelete) |
| 50 |
{ |
| 51 |
activeElements.erase(elemId); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
void fill_elements_to_delete(const stk::mesh::EntityIdVector &elemIdsToDelete, stk::mesh::impl::DeletedElementInfoVector &elements_to_delete) |
| 56 |
{ |
| 57 |
for(stk::mesh::EntityId elemId : elemIdsToDelete) |
| 58 |
{ |
| 59 |
stk::mesh::Entity elem = get_bulk().get_entity(stk::topology::ELEM_RANK, elemId); |
| 60 |
if(get_bulk().is_valid(elem) && get_bulk().bucket(elem).owned()) |
| 61 |
{ |
| 62 |
elements_to_delete.push_back( {elem, elemId, get_bulk().bucket(elem).topology()}); |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
void destroy_elements(stk::mesh::impl::DeletedElementInfoVector &elementsToDelete) |
| 68 |
{ |
| 69 |
get_bulk().modification_begin(); |
| 70 |
for(stk::mesh::impl::DeletedElementInfo elem : elementsToDelete) |
| 71 |
{ |
| 72 |
get_bulk().destroy_entity(elem.entity); |
| 73 |
stk::mesh::Entity elemCheck = get_bulk().get_entity(stk::topology::ELEM_RANK, elem.identifier); |
| 74 |
EXPECT_FALSE(get_bulk().is_valid(elemCheck)); |
| 75 |
} |
| 76 |
get_bulk().modification_end(); |
| 77 |
} |
| 78 |
|
| 79 |
void check_graph_connections_updated_after_deletions(GraphEdges &goldConnections, size_t goldNumGlobalConnectionsAfterDeletes) |
| 80 |
{ |
| 81 |
expect_local_elem_counts_are_equal_to_active_elements(get_meta().locally_owned_part(), activeElements.size()); |
| 82 |
|
| 83 |
stk::mesh::EntityVector elems; |
| 84 |
stk::mesh::get_selected_entities(get_meta().locally_owned_part(), get_bulk().buckets(stk::topology::ELEM_RANK), elems); |
| 85 |
|
| 86 |
expect_correct_number_of_connections_in_graph_after_deletes(get_meta().locally_owned_part(), goldNumGlobalConnectionsAfterDeletes, elems); |
| 87 |
|
| 88 |
size_t matchingEdgesFound = get_matching_edges_found_by_checking_against_gold_connections(goldConnections, elems); |
| 89 |
|
| 90 |
expect_all_edges_found_across_procs(goldConnections.size(), matchingEdgesFound); |
| 91 |
} |
| 92 |
|
| 93 |
void expect_local_elem_counts_are_equal_to_active_elements(stk::mesh::Selector locallyOwned, size_t numActiveElements) |
| 94 |
{ |
| 95 |
std::vector<size_t> counts; |
| 96 |
stk::mesh::comm_mesh_counts(get_bulk(), counts, &locallyOwned); |
| 97 |
EXPECT_EQ(numActiveElements, counts[stk::topology::ELEM_RANK]); |
| 98 |
} |
| 99 |
|
| 100 |
void expect_correct_number_of_connections_in_graph_after_deletes(stk::mesh::Selector /*locallyOwned*/, |
| 101 |
size_t goldNumGlobalConnectionsAfterDeletes, |
| 102 |
stk::mesh::EntityVector &elems) |
| 103 |
{ |
| 104 |
size_t localNumConnections = get_num_local_connections(elems); |
| 105 |
|
| 106 |
size_t globalNumConnections = 0; |
| 107 |
stk::all_reduce_sum(get_comm(), &localNumConnections, &globalNumConnections, 1); |
| 108 |
EXPECT_EQ(goldNumGlobalConnectionsAfterDeletes, globalNumConnections); |
| 109 |
} |
| 110 |
|
| 111 |
size_t get_num_local_connections(const stk::mesh::EntityVector &elems) |
| 112 |
{ |
| 113 |
size_t localNumConnections = 0; |
| 114 |
for(stk::mesh::Entity elem : elems) |
| 115 |
localNumConnections += elementGraph->get_num_connected_elems(elem); |
| 116 |
return localNumConnections; |
| 117 |
} |
| 118 |
|
| 119 |
size_t get_matching_edges_found_by_checking_against_gold_connections(const GraphEdges &goldConnections, stk::mesh::EntityVector &elems) |
| 120 |
{ |
| 121 |
size_t matchingEdgesFound = 0; |
| 122 |
for(stk::mesh::Entity elem : elems) |
| 123 |
for(size_t i = 0; i < elementGraph->get_num_connected_elems(elem); i++) |
| 124 |
expect_connection_is_in_gold_connections(goldConnections, get_bulk().identifier(elem), get_connection(elem, i), matchingEdgesFound); |
| 125 |
return matchingEdgesFound; |
| 126 |
} |
| 127 |
|
| 128 |
void expect_connection_is_in_gold_connections(const GraphEdges &goldConnections, stk::mesh::EntityId elemId, stk::mesh::impl::IdViaSidePair connection, size_t &edgesFound) |
| 129 |
{ |
| 130 |
for(const GraphEdgeMock &goldEdge : goldConnections) |
| 131 |
if(goldEdge.element1 == elemId && goldEdge.element2 == connection.id) |
| 132 |
{ |
| 133 |
EXPECT_EQ(goldEdge.sideOrdinalConnectingElement1ToElement2, connection.side); |
| 134 |
edgesFound++; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
void expect_all_edges_found_across_procs(size_t totalNumberEdges, size_t matchingEdgesFound) |
| 139 |
{ |
| 140 |
size_t globalMatchingEdgesFound = 0; |
| 141 |
stk::all_reduce_sum(get_comm(), &matchingEdgesFound, &globalMatchingEdgesFound, 1); |
| 142 |
EXPECT_EQ(totalNumberEdges, globalMatchingEdgesFound); |
| 143 |
} |
| 144 |
|
| 145 |
stk::mesh::impl::IdViaSidePair get_connection(stk::mesh::Entity elem, size_t sideOffset) |
| 146 |
{ |
| 147 |
if(elementGraph->is_connected_elem_locally_owned(elem, sideOffset)) |
| 148 |
return get_local_connection_information(elem, sideOffset); |
| 149 |
else |
| 150 |
return elementGraph->get_connected_remote_id_and_via_side(elem, sideOffset); |
| 151 |
} |
| 152 |
|
| 153 |
stk::mesh::impl::IdViaSidePair get_local_connection_information(stk::mesh::Entity elem, size_t sideOffset) |
| 154 |
{ |
| 155 |
stk::mesh::impl::ElementViaSidePair conn = elementGraph->get_connected_element_and_via_side(elem, sideOffset); |
| 156 |
return {get_bulk().identifier(conn.element), conn.side}; |
| 157 |
} |
| 158 |
|
| 159 |
protected: |
| 160 |
ElemElemGraphTester *elementGraph; |
| 161 |
std::set<stk::mesh::EntityId> activeElements = {1, 2, 3, 4}; |
| 162 |
}; |
| 163 |
|
| 164 |
TEST_F(ElemGraphDeleteElementsTester, deleteElements1and4WithAura) |
| 165 |
{ |
| 166 |
if(stk::parallel_machine_size(get_comm()) < 4) |
| 167 |
{ |
| 168 |
GraphEdges goldConnections = {{2, 3, 5}, {3, 2, 4}}; |
| 169 |
create_and_expect_graph_updated_after_elements_are_deleted(stk::mesh::BulkData::AUTO_AURA, {1, 4}, 2, goldConnections); |
| 170 |
} |
| 171 |
} |
| 172 |
TEST_F(ElemGraphDeleteElementsTester, deleteElements1and4WithoutAura) |
| 173 |
{ |
| 174 |
if(stk::parallel_machine_size(get_comm()) < 4) |
| 175 |
{ |
| 176 |
GraphEdges goldConnections = {{2, 3, 5}, {3, 2, 4}}; |
| 177 |
create_and_expect_graph_updated_after_elements_are_deleted(stk::mesh::BulkData::NO_AUTO_AURA, {1, 4}, 2, goldConnections); |
| 178 |
} |
| 179 |
} |
| 180 |
TEST_F(ElemGraphDeleteElementsTester, deleteElements1and3WithAura) |
| 181 |
{ |
| 182 |
if(stk::parallel_machine_size(get_comm()) < 4) |
| 183 |
{ |
| 184 |
GraphEdges goldConnections = {}; |
| 185 |
create_and_expect_graph_updated_after_elements_are_deleted(stk::mesh::BulkData::AUTO_AURA, {1, 3}, 0, goldConnections); |
| 186 |
} |
| 187 |
} |
| 188 |
TEST_F(ElemGraphDeleteElementsTester, deleteElements1and3WithoutAura) |
| 189 |
{ |
| 190 |
if(stk::parallel_machine_size(get_comm()) < 4) |
| 191 |
{ |
| 192 |
GraphEdges goldConnections = {}; |
| 193 |
create_and_expect_graph_updated_after_elements_are_deleted(stk::mesh::BulkData::NO_AUTO_AURA, {1, 3}, 0, goldConnections); |
| 194 |
} |
| 195 |
} |
| 196 |
TEST_F(ElemGraphDeleteElementsTester, deleteEveryElementWithAura) |
| 197 |
{ |
| 198 |
if(stk::parallel_machine_size(get_comm()) < 4) |
| 199 |
{ |
| 200 |
GraphEdges goldConnections = {}; |
| 201 |
create_and_expect_graph_updated_after_elements_are_deleted(stk::mesh::BulkData::AUTO_AURA, {1, 2, 3, 4}, 0, goldConnections); |
| 202 |
} |
| 203 |
} |
| 204 |
TEST_F(ElemGraphDeleteElementsTester, deleteEveryElementWithoutAura) |
| 205 |
{ |
| 206 |
if(stk::parallel_machine_size(get_comm()) < 4) |
| 207 |
{ |
| 208 |
GraphEdges goldConnections = {}; |
| 209 |
create_and_expect_graph_updated_after_elements_are_deleted(stk::mesh::BulkData::NO_AUTO_AURA, {1, 2, 3, 4}, 0, goldConnections); |
| 210 |
} |
| 211 |
} |
| 212 |
TEST_F(ElemGraphDeleteElementsTester, deleteNothingWithAura) |
| 213 |
{ |
| 214 |
if(stk::parallel_machine_size(get_comm()) < 4) |
| 215 |
{ |
| 216 |
GraphEdges goldConnections = {{1, 2, 5}, {2, 1, 4}, {2, 3, 5}, {3, 2, 4}, {3, 4, 5}, {4, 3, 4}}; |
| 217 |
create_and_expect_graph_updated_after_elements_are_deleted(stk::mesh::BulkData::AUTO_AURA, {}, 6, goldConnections); |
| 218 |
} |
| 219 |
} |
| 220 |
TEST_F(ElemGraphDeleteElementsTester, deleteNothingWithoutAura) |
| 221 |
{ |
| 222 |
if(stk::parallel_machine_size(get_comm()) < 4) |
| 223 |
{ |
| 224 |
GraphEdges goldConnections = {{1, 2, 5}, {2, 1, 4}, {2, 3, 5}, {3, 2, 4}, {3, 4, 5}, {4, 3, 4}}; |
| 225 |
create_and_expect_graph_updated_after_elements_are_deleted(stk::mesh::BulkData::NO_AUTO_AURA, {}, 6, goldConnections); |
| 226 |
} |
| 227 |
} |
| 228 |
} |