| 1 |
#include "gtest/gtest.h" // for AssertHelper, EXPECT_EQ, etc |
| 2 |
#include <stk_mesh/base/BulkData.hpp> // for BulkData |
| 3 |
#include <stk_mesh/base/GetEntities.hpp> // for count_selected_entities |
| 4 |
#include <stk_mesh/base/CreateFaces.hpp> |
| 5 |
#include <stk_mesh/base/SkinBoundary.hpp> |
| 6 |
#include <stk_unit_test_utils/getOption.h> |
| 7 |
#include <stk_unit_test_utils/MeshFixture.hpp> |
| 8 |
#include <stk_unit_test_utils/PerformanceTester.hpp> |
| 9 |
|
| 10 |
#include <stk_unit_test_utils/TextMesh.hpp> |
| 11 |
#include "stk_mesh/base/Comm.hpp" |
| 12 |
#include <stk_mesh/base/FEMHelpers.hpp> |
| 13 |
#include <stk_mesh/base/SideSetUtil.hpp> |
| 14 |
#include <stk_mesh/base/SidesetUpdater.hpp> |
| 15 |
#include <stk_mesh/base/PolarityUtil.hpp> |
| 16 |
|
| 17 |
#include "stk_mesh/base/ExodusTranslator.hpp" |
| 18 |
#include "stk_mesh/baseImpl/elementGraph/ElemElemGraph.hpp" |
| 19 |
#include "stk_mesh/baseImpl/elementGraph/ElemElemGraphImpl.hpp" |
| 20 |
#include "stk_mesh/baseImpl/elementGraph/GraphEdgeData.hpp" |
| 21 |
|
| 22 |
namespace |
| 23 |
{ |
| 24 |
|
| 25 |
//============================================================================== |
| 26 |
class PolarityPerformanceBase : public stk::unit_test_util::PerformanceTester |
| 27 |
{ |
| 28 |
public: |
| 29 |
PolarityPerformanceBase(stk::mesh::BulkData &bulk) |
| 30 |
: stk::unit_test_util::PerformanceTester(bulk.parallel()), |
| 31 |
m_bulk(bulk) |
| 32 |
{ |
| 33 |
create_mesh(); |
| 34 |
} |
| 35 |
|
| 36 |
virtual ~PolarityPerformanceBase() = default; |
| 37 |
|
| 38 |
protected: |
| 39 |
|
| 40 |
virtual size_t get_value_to_output_as_iteration_count() override |
| 41 |
{ |
| 42 |
return 1; |
| 43 |
} |
| 44 |
|
| 45 |
std::string get_mesh_spec() |
| 46 |
{ |
| 47 |
std::string meshDesc; |
| 48 |
|
| 49 |
if(stk::parallel_machine_size(MPI_COMM_WORLD) == 2) { |
| 50 |
meshDesc = "0,1,HEX_8,1,2,3,4,5, 6, 7, 8,block_1\n" |
| 51 |
"1,2,HEX_8,5,6,7,8,9,10,11,12,block_2"; |
| 52 |
} else { |
| 53 |
meshDesc = "0,1,HEX_8,1,2,3,4,5, 6, 7, 8,block_1\n" |
| 54 |
"0,2,HEX_8,5,6,7,8,9,10,11,12,block_2"; |
| 55 |
} |
| 56 |
|
| 57 |
return meshDesc; |
| 58 |
} |
| 59 |
|
| 60 |
void create_mesh() |
| 61 |
{ |
| 62 |
stk::mesh::MetaData &meta = m_bulk.mesh_meta_data(); |
| 63 |
|
| 64 |
// Register sideset updater |
| 65 |
if (!m_bulk.has_observer_type<stk::mesh::SidesetUpdater>()) { |
| 66 |
stk::mesh::Selector activeSelector = meta.universal_part(); |
| 67 |
|
| 68 |
m_bulk.register_observer(std::make_shared<stk::mesh::IncrementalSidesetUpdater>(m_bulk, activeSelector)); |
| 69 |
} |
| 70 |
|
| 71 |
// Create sideset parts |
| 72 |
std::vector<std::string> sidesetNames{"surface_1", "surface_block_1_QUAD4_1"}; |
| 73 |
int sidesetId = 1; |
| 74 |
for(const std::string &name : sidesetNames) |
| 75 |
{ |
| 76 |
stk::mesh::Part &part = meta.declare_part_with_topology(name, stk::topology::QUAD_4); |
| 77 |
meta.set_part_id(part, sidesetId); |
| 78 |
m_sidesetParts.push_back(&part); |
| 79 |
} |
| 80 |
|
| 81 |
meta.declare_part_subset(*m_sidesetParts[0], *m_sidesetParts[1]); |
| 82 |
|
| 83 |
// Build mesh |
| 84 |
std::string meshDesc = get_mesh_spec(); |
| 85 |
std::vector<double> coordinates = { 0,0,0, 1,0,0, 1,1,0, 0,1,0, |
| 86 |
0,0,1, 1,0,1, 1,1,1, 0,1,1, |
| 87 |
0,0,2, 1,0,2, 1,1,2, 0,1,2 }; |
| 88 |
|
| 89 |
m_bulk.initialize_face_adjacent_element_graph(); |
| 90 |
stk::unit_test_util::setup_text_mesh(m_bulk, stk::unit_test_util::get_full_text_mesh_desc(meshDesc, coordinates)); |
| 91 |
|
| 92 |
// Create and populate sideset between elements 1 & 2, based on element 1 |
| 93 |
stk::mesh::SideSet &sideset = m_bulk.create_sideset(*m_sidesetParts[0]); |
| 94 |
sideset.set_accept_all_internal_non_coincident_entries(false); |
| 95 |
m_sideset = &sideset; |
| 96 |
|
| 97 |
std::vector<stk::mesh::EntityId> elementIds{1, 2}; |
| 98 |
std::vector<stk::mesh::ConnectivityOrdinal> elementOrdinals{5, 4}; |
| 99 |
std::vector<stk::mesh::Entity> elements(2); |
| 100 |
elements[0] = m_bulk.get_entity(stk::topology::ELEMENT_RANK, elementIds[0]); |
| 101 |
elements[1] = m_bulk.get_entity(stk::topology::ELEMENT_RANK, elementIds[1]); |
| 102 |
|
| 103 |
if (m_bulk.is_valid(elements[0])) { |
| 104 |
sideset.add({elements[0], elementOrdinals[0]}); |
| 105 |
} |
| 106 |
|
| 107 |
// Setup surface to block mapping for the sideset updater |
| 108 |
stk::mesh::Part* block_1 = meta.get_part("block_1"); |
| 109 |
EXPECT_TRUE(block_1 != nullptr); |
| 110 |
|
| 111 |
std::vector<const stk::mesh::Part*> touchingParts{block_1}; |
| 112 |
meta.set_surface_to_block_mapping(m_sidesetParts[1], touchingParts); |
| 113 |
|
| 114 |
// Create faces |
| 115 |
m_bulk.modification_begin(); |
| 116 |
int myProc = m_bulk.parallel_rank(); |
| 117 |
m_face = m_bulk.declare_element_side(elements[myProc], elementOrdinals[myProc], m_sidesetParts); |
| 118 |
m_bulk.modification_end(); |
| 119 |
|
| 120 |
// Make sure there is only 1 global face |
| 121 |
std::vector<size_t> counts; |
| 122 |
stk::mesh::comm_mesh_counts(m_bulk, counts); |
| 123 |
EXPECT_EQ(1u, counts[meta.side_rank()]); |
| 124 |
|
| 125 |
// Make sure it matches the one we created |
| 126 |
stk::mesh::EntityVector faces; |
| 127 |
stk::mesh::get_selected_entities(meta.universal_part(), m_bulk.buckets(meta.side_rank()), faces); |
| 128 |
EXPECT_EQ(1u, faces.size()); |
| 129 |
EXPECT_EQ(m_face, faces[0]); |
| 130 |
} |
| 131 |
|
| 132 |
int get_num_loops() |
| 133 |
{ |
| 134 |
return stk::unit_test_util::get_command_line_option("-l", m_defaultLoops); |
| 135 |
} |
| 136 |
|
| 137 |
stk::mesh::BulkData &m_bulk; |
| 138 |
stk::mesh::PartVector m_sidesetParts; |
| 139 |
stk::mesh::SideSet *m_sideset{nullptr}; |
| 140 |
stk::mesh::Entity m_face; |
| 141 |
|
| 142 |
const int m_defaultLoops = 100000; |
| 143 |
}; |
| 144 |
|
| 145 |
class PolarityUtilPerformance : public PolarityPerformanceBase |
| 146 |
{ |
| 147 |
public: |
| 148 |
PolarityUtilPerformance(stk::mesh::BulkData &bulk) |
| 149 |
: PolarityPerformanceBase(bulk), |
| 150 |
m_helper(bulk, bulk.mesh_meta_data().universal_part()) |
| 151 |
{ |
| 152 |
} |
| 153 |
|
| 154 |
~PolarityUtilPerformance() = default; |
| 155 |
|
| 156 |
protected: |
| 157 |
virtual void run_algorithm_to_time() override |
| 158 |
{ |
| 159 |
// Get polarity results with util wrapper code |
| 160 |
std::pair<bool,bool> expectedPolarity(true, true); |
| 161 |
int numLoops = get_num_loops(); |
| 162 |
|
| 163 |
for(int i=0; i<numLoops; i++) { |
| 164 |
std::pair<bool,bool> polarity1 = m_helper.is_positive_sideset_polarity(*m_sidesetParts[1], m_face, m_sideset); |
| 165 |
std::pair<bool,bool> polarity2 = m_helper.is_positive_sideset_face_polarity(m_face); |
| 166 |
|
| 167 |
|
| 168 |
EXPECT_EQ(expectedPolarity, polarity1); |
| 169 |
EXPECT_EQ(expectedPolarity, polarity2); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
stk::mesh::PolarityUtil m_helper; |
| 174 |
}; |
| 175 |
|
| 176 |
class PolarityFreePerformance : public PolarityPerformanceBase |
| 177 |
{ |
| 178 |
public: |
| 179 |
PolarityFreePerformance(stk::mesh::BulkData &bulk) |
| 180 |
: PolarityPerformanceBase(bulk) |
| 181 |
{ |
| 182 |
} |
| 183 |
|
| 184 |
~PolarityFreePerformance() = default; |
| 185 |
|
| 186 |
protected: |
| 187 |
virtual void run_algorithm_to_time() override |
| 188 |
{ |
| 189 |
// Get polarity results with util wrapper code |
| 190 |
std::pair<bool,bool> expectedPolarity(true, true); |
| 191 |
int numLoops = get_num_loops(); |
| 192 |
stk::mesh::Part& activePart = m_bulk.mesh_meta_data().universal_part(); |
| 193 |
|
| 194 |
for(int i=0; i<numLoops; i++) { |
| 195 |
// Get polarity results with free function code |
| 196 |
std::pair<bool,bool> polarity1 = stk::mesh::is_positive_sideset_polarity(m_bulk, *m_sidesetParts[1], m_face, |
| 197 |
&activePart, m_sideset); |
| 198 |
std::pair<bool,bool> polarity2 = stk::mesh::is_positive_sideset_face_polarity(m_bulk, m_face, &activePart); |
| 199 |
|
| 200 |
EXPECT_EQ(expectedPolarity, polarity1); |
| 201 |
EXPECT_EQ(expectedPolarity, polarity2); |
| 202 |
} |
| 203 |
} |
| 204 |
}; |
| 205 |
|
| 206 |
class PolarityPerformanceTest : public stk::unit_test_util::MeshFixture |
| 207 |
{ |
| 208 |
protected: |
| 209 |
void run_util_polarity_perf_test() |
| 210 |
{ |
| 211 |
PolarityUtilPerformance perfTester(get_bulk()); |
| 212 |
perfTester.run_performance_test(); |
| 213 |
} |
| 214 |
|
| 215 |
void run_free_polarity_perf_test() |
| 216 |
{ |
| 217 |
PolarityFreePerformance perfTester(get_bulk()); |
| 218 |
perfTester.run_performance_test(); |
| 219 |
} |
| 220 |
}; |
| 221 |
|
| 222 |
TEST_F(PolarityPerformanceTest, utilPolarityTestWithAura) |
| 223 |
{ |
| 224 |
if(stk::parallel_machine_size(MPI_COMM_WORLD) > 2) {GTEST_SKIP();} |
| 225 |
allocate_bulk(stk::mesh::BulkData::AUTO_AURA); |
| 226 |
run_util_polarity_perf_test(); |
| 227 |
} |
| 228 |
|
| 229 |
TEST_F(PolarityPerformanceTest, utilPolarityTestNoAura) |
| 230 |
{ |
| 231 |
if(stk::parallel_machine_size(MPI_COMM_WORLD) > 2) {GTEST_SKIP();} |
| 232 |
allocate_bulk(stk::mesh::BulkData::NO_AUTO_AURA); |
| 233 |
run_util_polarity_perf_test(); |
| 234 |
} |
| 235 |
|
| 236 |
TEST_F(PolarityPerformanceTest, freePolarityTestWithAura) |
| 237 |
{ |
| 238 |
if(stk::parallel_machine_size(MPI_COMM_WORLD) > 2) {GTEST_SKIP();} |
| 239 |
allocate_bulk(stk::mesh::BulkData::AUTO_AURA); |
| 240 |
run_free_polarity_perf_test(); |
| 241 |
} |
| 242 |
|
| 243 |
TEST_F(PolarityPerformanceTest, freePolarityTestNoAura) |
| 244 |
{ |
| 245 |
if(stk::parallel_machine_size(MPI_COMM_WORLD) > 2) {GTEST_SKIP();} |
| 246 |
allocate_bulk(stk::mesh::BulkData::NO_AUTO_AURA); |
| 247 |
run_free_polarity_perf_test(); |
| 248 |
} |
| 249 |
|
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|