| 1 |
#include <gtest/gtest.h> |
| 2 |
#include <algorithm> |
| 3 |
#include <map> |
| 4 |
#include <vector> |
| 5 |
#include <stk_mesh/base/FEMHelpers.hpp> |
| 6 |
#include <stk_mesh/base/GetEntities.hpp> |
| 7 |
#include <stk_util/parallel/ParallelVectorConcat.hpp> |
| 8 |
#include <stk_util/util/SortAndUnique.hpp> |
| 9 |
#include <stk_unit_test_utils/MeshFixture.hpp> |
| 10 |
#include "ElementGraphTester.hpp" |
| 11 |
|
| 12 |
namespace |
| 13 |
{ |
| 14 |
|
| 15 |
class ElemGraphAddElementsToEmptyGraphTester : public stk::unit_test_util::MeshTestFixture |
| 16 |
{ |
| 17 |
protected: |
| 18 |
ElemGraphAddElementsToEmptyGraphTester() : elementGraph(nullptr) { } |
| 19 |
~ElemGraphAddElementsToEmptyGraphTester() |
| 20 |
{ |
| 21 |
delete elementGraph; |
| 22 |
} |
| 23 |
|
| 24 |
virtual void run_test(stk::mesh::BulkData::AutomaticAuraOption auraOption) |
| 25 |
{ |
| 26 |
setup_empty_mesh(auraOption); |
| 27 |
create_elem_graph(); |
| 28 |
stk::io::fill_mesh("generated:1x1x4", get_bulk()); |
| 29 |
expect_graph_correct_after_moving_everything_to_proc0(); |
| 30 |
} |
| 31 |
|
| 32 |
void expect_graph_correct_after_moving_everything_to_proc0() |
| 33 |
{ |
| 34 |
add_elements_to_graph(); |
| 35 |
test_edges(); |
| 36 |
} |
| 37 |
|
| 38 |
void create_elem_graph() |
| 39 |
{ |
| 40 |
elementGraph = new ElemElemGraphTester(get_bulk()); |
| 41 |
EXPECT_EQ(0u, elementGraph->size()); |
| 42 |
} |
| 43 |
|
| 44 |
void add_elements_to_graph() |
| 45 |
{ |
| 46 |
stk::mesh::EntityVector elements_to_add; |
| 47 |
stk::mesh::get_entities(get_bulk(), stk::topology::ELEMENT_RANK, get_bulk().mesh_meta_data().locally_owned_part(), elements_to_add); |
| 48 |
elementGraph->add_elements(elements_to_add); |
| 49 |
EXPECT_EQ(elements_to_add.size(), elementGraph->size()); |
| 50 |
} |
| 51 |
|
| 52 |
void test_num_edges() |
| 53 |
{ |
| 54 |
std::vector<size_t> numGraphEdgesPerProc = {1, 2, 2, 1}; |
| 55 |
size_t numGraphEdges = numGraphEdgesPerProc[get_bulk().parallel_rank()]; |
| 56 |
EXPECT_EQ(numGraphEdges, elementGraph->num_edges()); |
| 57 |
EXPECT_EQ(numGraphEdges, elementGraph->num_parallel_edges()); |
| 58 |
} |
| 59 |
|
| 60 |
void test_edges() |
| 61 |
{ |
| 62 |
test_num_edges(); |
| 63 |
|
| 64 |
GraphEdges graphEdgesThisProc = goldElement1ToElement2SideOrdinalsPerOwningProc[get_bulk().parallel_rank()]; |
| 65 |
for(GraphEdgeMock const &graphEdge : graphEdgesThisProc) |
| 66 |
EXPECT_EQ(graphEdge.sideOrdinalConnectingElement1ToElement2, elementGraph->get_side_from_element1_to_element2(graphEdge.element1, graphEdge.element2)); |
| 67 |
} |
| 68 |
protected: |
| 69 |
ElemElemGraphTester *elementGraph; |
| 70 |
|
| 71 |
std::vector<GraphEdges> goldElement1ToElement2SideOrdinalsPerOwningProc = |
| 72 |
{ |
| 73 |
{{1, 2, 5}, {1, 3, -1}, {1, 4, -1}}, |
| 74 |
{{2, 1, 4}, {2, 3, 5}, {2, 4, -1}}, |
| 75 |
{{3, 2, 4}, {3, 4, 5}, {3, 1, -1}}, |
| 76 |
{{4, 3, 4}, {4, 1, -1}, {4, 2, -1}} |
| 77 |
}; |
| 78 |
}; |
| 79 |
TEST_F(ElemGraphAddElementsToEmptyGraphTester, withAura) |
| 80 |
{ |
| 81 |
run_test_on_num_procs(4, stk::mesh::BulkData::AUTO_AURA); |
| 82 |
} |
| 83 |
TEST_F(ElemGraphAddElementsToEmptyGraphTester, withoutAura) |
| 84 |
{ |
| 85 |
run_test_on_num_procs(4, stk::mesh::BulkData::NO_AUTO_AURA); |
| 86 |
} |
| 87 |
|
| 88 |
struct NodeSharingInfo |
| 89 |
{ |
| 90 |
unsigned owningProc; |
| 91 |
unsigned sharedNodeId; |
| 92 |
unsigned procSharedTo; |
| 93 |
}; |
| 94 |
|
| 95 |
void setup_node_sharing(stk::mesh::BulkData &mesh, const std::vector<NodeSharingInfo> & sharedNodes); |
| 96 |
|
| 97 |
class ElemGraphAddElementsToExistingGraphTester : public stk::unit_test_util::MeshTestFixture |
| 98 |
{ |
| 99 |
protected: |
| 100 |
ElemGraphAddElementsToExistingGraphTester() : elementGraph(nullptr), hexPart(nullptr) { } |
| 101 |
~ElemGraphAddElementsToExistingGraphTester() |
| 102 |
{ |
| 103 |
delete elementGraph; |
| 104 |
} |
| 105 |
|
| 106 |
virtual void run_test(stk::mesh::BulkData::AutomaticAuraOption auraOption) |
| 107 |
{ |
| 108 |
setup_empty_mesh(auraOption); |
| 109 |
hexPart = &get_meta().declare_part_with_topology("hex_part", stk::topology::HEX_8); |
| 110 |
stk::io::fill_mesh("generated:1x1x2", get_bulk()); |
| 111 |
create_element_graph(); |
| 112 |
expect_graph_correct_after_adding_two_new_elements(); |
| 113 |
} |
| 114 |
|
| 115 |
void create_element_graph() |
| 116 |
{ |
| 117 |
elementGraph = new ElemElemGraphTester(get_bulk()); |
| 118 |
EXPECT_EQ(1u, elementGraph->size()); |
| 119 |
} |
| 120 |
|
| 121 |
void expect_graph_correct_after_adding_two_new_elements() |
| 122 |
{ |
| 123 |
declare_new_hex_element_per_proc(); |
| 124 |
add_new_hex_element_to_existing_graph(); |
| 125 |
test_edges(); |
| 126 |
} |
| 127 |
|
| 128 |
void declare_new_hex_element_per_proc() |
| 129 |
{ |
| 130 |
get_bulk().modification_begin(); |
| 131 |
declare_hex_element(); |
| 132 |
setup_node_sharing_for_added_element(); |
| 133 |
get_bulk().modification_end(); |
| 134 |
} |
| 135 |
|
| 136 |
void declare_hex_element() |
| 137 |
{ |
| 138 |
const int rank = get_bulk().parallel_rank(); |
| 139 |
stk::mesh::EntityIdVector hexNodeIds[] |
| 140 |
{ |
| 141 |
{ 12, 11, 18, 17, 8, 7, 16, 15 }, |
| 142 |
{ 8, 7, 16, 15, 4, 3, 14, 13 } |
| 143 |
}; |
| 144 |
stk::mesh::declare_element(get_bulk(), *hexPart, hexElemIdsByProc[rank], hexNodeIds[rank]); |
| 145 |
} |
| 146 |
|
| 147 |
void setup_node_sharing_for_added_element() |
| 148 |
{ |
| 149 |
std::vector<NodeSharingInfo> sharedNodes |
| 150 |
{ |
| 151 |
{0, 3, 1}, {0, 4, 1}, {0, 11, 1}, {0, 12, 1}, {0, 15, 1}, {0, 16, 1}, // proc 0 |
| 152 |
{1, 3, 0}, {1, 4, 0}, {1, 11, 0}, {1, 12, 0}, {1, 15, 0}, {1, 16, 0}, // proc 1 |
| 153 |
}; |
| 154 |
setup_node_sharing(get_bulk(), sharedNodes ); |
| 155 |
} |
| 156 |
|
| 157 |
void add_new_hex_element_to_existing_graph() |
| 158 |
{ |
| 159 |
const int rank = get_bulk().parallel_rank(); |
| 160 |
stk::mesh::Entity elementToAdd = get_bulk().get_entity(stk::topology::ELEM_RANK, hexElemIdsByProc[rank]); |
| 161 |
elementGraph->add_elements({elementToAdd}); |
| 162 |
EXPECT_EQ(2u, elementGraph->size()); |
| 163 |
} |
| 164 |
|
| 165 |
void test_num_edges() |
| 166 |
{ |
| 167 |
EXPECT_EQ(4u, elementGraph->num_edges()); |
| 168 |
EXPECT_EQ(4u, elementGraph->num_parallel_edges()); |
| 169 |
} |
| 170 |
|
| 171 |
void test_edges() |
| 172 |
{ |
| 173 |
test_num_edges(); |
| 174 |
|
| 175 |
GraphEdges graphEdgesThisProc = goldElement1ToElement2SideOrdinalsPerOwningProc[get_bulk().parallel_rank()]; |
| 176 |
for(GraphEdgeMock const &graphEdge : graphEdgesThisProc) |
| 177 |
EXPECT_EQ(graphEdge.sideOrdinalConnectingElement1ToElement2, elementGraph->get_side_from_element1_to_element2(graphEdge.element1, graphEdge.element2)); |
| 178 |
} |
| 179 |
|
| 180 |
protected: |
| 181 |
ElemElemGraphTester *elementGraph; |
| 182 |
stk::mesh::Part *hexPart; |
| 183 |
|
| 184 |
stk::mesh::EntityIdVector hexElemIdsByProc = {3, 4}; |
| 185 |
std::vector<GraphEdges> goldElement1ToElement2SideOrdinalsPerOwningProc = |
| 186 |
{ |
| 187 |
{{1, 2, 5}, {1, 4, 2}, {3, 2, 0}, {3, 4, 5}}, |
| 188 |
{{2, 1, 4}, {2, 3, 2}, {4, 1, 0}, {4, 3, 4}}, |
| 189 |
}; |
| 190 |
}; |
| 191 |
TEST_F(ElemGraphAddElementsToExistingGraphTester, aura_on) |
| 192 |
{ |
| 193 |
run_test_on_num_procs(2, stk::mesh::BulkData::AUTO_AURA); |
| 194 |
} |
| 195 |
TEST_F(ElemGraphAddElementsToExistingGraphTester, aura_off) |
| 196 |
{ |
| 197 |
run_test_on_num_procs(2, stk::mesh::BulkData::NO_AUTO_AURA); |
| 198 |
} |
| 199 |
|
| 200 |
// THIS CODE EXISTS IN UnitTestSkinMesh.cpp AS WELL...doh! |
| 201 |
void setup_node_sharing(stk::mesh::BulkData &mesh, const std::vector<NodeSharingInfo> & sharedNodes) |
| 202 |
{ |
| 203 |
for(const NodeSharingInfo &nodeSharing : sharedNodes) |
| 204 |
{ |
| 205 |
if (static_cast<unsigned>(mesh.parallel_rank()) == nodeSharing.owningProc) |
| 206 |
{ |
| 207 |
stk::mesh::Entity node = mesh.get_entity(stk::topology::NODE_RANK, nodeSharing.sharedNodeId); |
| 208 |
mesh.add_node_sharing(node, nodeSharing.procSharedTo); |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
} |