| 1 |
#include <gtest/gtest.h> |
| 2 |
#include <stk_util/parallel/ParallelReduce.hpp> |
| 3 |
#include <stk_unit_test_utils/ioUtils.hpp> |
| 4 |
#include "stk_mesh/base/GetEntities.hpp" |
| 5 |
#include <stk_mesh/base/BulkData.hpp> // for BulkData |
| 6 |
#include <stk_mesh/base/MetaData.hpp> // for MetaData |
| 7 |
#include <stk_mesh/base/SideSetUtil.hpp> |
| 8 |
#include <stk_mesh/base/SidesetUpdater.hpp> |
| 9 |
#include <stk_io/StkIoUtils.hpp> |
| 10 |
#include "stk_unit_test_utils/ReadWriteSidesetTester.hpp" |
| 11 |
#include "stk_unit_test_utils/FaceTestingUtils.hpp" |
| 12 |
#include "stk_unit_test_utils/TextMesh.hpp" |
| 13 |
#include "stk_unit_test_utils/GenerateALefRAMesh.hpp" |
| 14 |
#include <stk_unit_test_utils/BuildMesh.hpp> |
| 15 |
|
| 16 |
namespace { |
| 17 |
|
| 18 |
using stk::unit_test_util::build_mesh; |
| 19 |
using stk::unit_test_util::build_mesh_no_simple_fields; |
| 20 |
|
| 21 |
stk::mesh::Entity verify_and_get_face(stk::mesh::BulkData &bulk, stk::mesh::Part* ssPart) |
| 22 |
{ |
| 23 |
stk::mesh::EntityVector faces; |
| 24 |
stk::mesh::get_selected_entities(*ssPart, bulk.buckets(stk::topology::FACE_RANK), faces); |
| 25 |
EXPECT_EQ(1u, faces.size()); |
| 26 |
return faces[0]; |
| 27 |
} |
| 28 |
|
| 29 |
void check_polarity_of_modified_AA(stk::ParallelMachine pm, |
| 30 |
stk::unit_test_util::SidesetDirection direction, |
| 31 |
const std::string& name, |
| 32 |
bool isPositivePolarity) |
| 33 |
{ |
| 34 |
const unsigned spatialDim = 3; |
| 35 |
std::shared_ptr<stk::mesh::BulkData> bulk = build_mesh(spatialDim, pm); |
| 36 |
|
| 37 |
if (!bulk->has_observer_type<stk::mesh::SidesetUpdater>()) { |
| 38 |
stk::mesh::Selector activeSelector = bulk->mesh_meta_data().universal_part(); |
| 39 |
|
| 40 |
bulk->register_observer(std::make_shared<stk::mesh::IncrementalSidesetUpdater>(*bulk, activeSelector)); |
| 41 |
} |
| 42 |
|
| 43 |
bulk->initialize_face_adjacent_element_graph(); |
| 44 |
|
| 45 |
stk::mesh::Part* ssPart = stk::unit_test_util::create_AA_mesh_with_sideset(*bulk, direction); |
| 46 |
stk::mesh::Entity face = verify_and_get_face(*bulk, ssPart); |
| 47 |
|
| 48 |
std::pair<bool,bool> sidesetPolarity = stk::mesh::is_positive_sideset_polarity(*bulk, *ssPart, face); |
| 49 |
EXPECT_TRUE(sidesetPolarity.first) << "Failure for " << name << " on first"; |
| 50 |
EXPECT_EQ(isPositivePolarity, sidesetPolarity.second) << "Failure for " << name << " on second"; |
| 51 |
} |
| 52 |
|
| 53 |
void check_polarity_of_modified_AB(stk::ParallelMachine pm, |
| 54 |
stk::unit_test_util::SidesetDirection direction, |
| 55 |
const std::string& name, |
| 56 |
bool isPositivePolarity) |
| 57 |
{ |
| 58 |
const unsigned spatialDim = 3; |
| 59 |
std::shared_ptr<stk::mesh::BulkData> bulk = build_mesh(spatialDim, pm); |
| 60 |
|
| 61 |
if (!bulk->has_observer_type<stk::mesh::SidesetUpdater>()) { |
| 62 |
stk::mesh::Selector activeSelector = bulk->mesh_meta_data().universal_part(); |
| 63 |
|
| 64 |
bulk->register_observer(std::make_shared<stk::mesh::IncrementalSidesetUpdater>(*bulk, activeSelector)); |
| 65 |
} |
| 66 |
|
| 67 |
bulk->initialize_face_adjacent_element_graph(); |
| 68 |
|
| 69 |
stk::mesh::Part* ssPart = stk::unit_test_util::create_AB_mesh_with_sideset(*bulk, direction); |
| 70 |
stk::mesh::Entity face = verify_and_get_face(*bulk, ssPart); |
| 71 |
|
| 72 |
std::pair<bool,bool> sidesetPolarity = stk::mesh::is_positive_sideset_polarity(*bulk, *ssPart, face); |
| 73 |
EXPECT_TRUE(sidesetPolarity.first) << "Failure for " << name << " on first"; |
| 74 |
EXPECT_EQ(isPositivePolarity, sidesetPolarity.second) << "Failure for " << name << " on second"; |
| 75 |
} |
| 76 |
|
| 77 |
TEST(StkIo, sideset_polarity_AA) |
| 78 |
{ |
| 79 |
stk::ParallelMachine pm = MPI_COMM_WORLD; |
| 80 |
|
| 81 |
const unsigned p_size = stk::parallel_machine_size(pm); |
| 82 |
|
| 83 |
if(p_size == 1) |
| 84 |
{ |
| 85 |
check_polarity_of_modified_AA(pm, stk::unit_test_util::RIGHT, "ARA", false); |
| 86 |
check_polarity_of_modified_AA(pm, stk::unit_test_util::LEFT , "ALA", true); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
TEST(StkIo, sideset_polarity_AB) |
| 91 |
{ |
| 92 |
stk::ParallelMachine pm = MPI_COMM_WORLD; |
| 93 |
|
| 94 |
const unsigned p_size = stk::parallel_machine_size(pm); |
| 95 |
|
| 96 |
if(p_size == 1) |
| 97 |
{ |
| 98 |
check_polarity_of_modified_AB(pm, stk::unit_test_util::RIGHT, "ARB", false); |
| 99 |
check_polarity_of_modified_AB(pm, stk::unit_test_util::LEFT , "ALB", true); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
void update_sidesets(const stk::mesh::BulkData& bulk, std::ostream& os) |
| 104 |
{ |
| 105 |
std::vector<std::shared_ptr<stk::mesh::SidesetUpdater> > updaters = bulk.get_observer_type<stk::mesh::SidesetUpdater>(); |
| 106 |
STK_ThrowRequireMsg(!updaters.empty(), "ERROR, no SidesetUpdater found on stk::mesh::BulkData"); |
| 107 |
|
| 108 |
updaters[0]->set_output_stream(os); |
| 109 |
|
| 110 |
std::vector<size_t> values; |
| 111 |
updaters[0]->fill_values_to_reduce(values); |
| 112 |
std::vector<size_t> maxValues(values); |
| 113 |
|
| 114 |
if (stk::parallel_machine_size(bulk.parallel()) > 1) { |
| 115 |
stk::all_reduce_max(bulk.parallel(), values.data(), maxValues.data(), maxValues.size()); |
| 116 |
} |
| 117 |
|
| 118 |
updaters[0]->set_reduced_values(maxValues); |
| 119 |
} |
| 120 |
|
| 121 |
TEST(StkIo, check_internal_sideset_warning_with_reconstruction) |
| 122 |
{ |
| 123 |
stk::ParallelMachine pm = MPI_COMM_WORLD; |
| 124 |
if (stk::parallel_machine_size(pm) != 1) { |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
const unsigned spatialDim = 3; |
| 129 |
std::shared_ptr<stk::mesh::BulkData> bulk = build_mesh(spatialDim, pm); |
| 130 |
stk::mesh::MetaData& meta = bulk->mesh_meta_data(); |
| 131 |
|
| 132 |
stk::unit_test_util::SidesetDirection direction = stk::unit_test_util::LEFT; |
| 133 |
stk::mesh::Part* ssPart = stk::unit_test_util::create_AA_mesh_with_sideset(*bulk, direction); |
| 134 |
EXPECT_TRUE(ssPart != nullptr); |
| 135 |
|
| 136 |
stk::mesh::Selector activeSelector(meta.universal_part()); |
| 137 |
bulk->register_observer(std::make_shared<stk::mesh::ReconstructionSidesetUpdater>(*bulk, activeSelector)); |
| 138 |
bulk->modification_end(); |
| 139 |
|
| 140 |
std::vector<std::shared_ptr<stk::mesh::SidesetUpdater> > updaters = bulk->get_observer_type<stk::mesh::SidesetUpdater>(); |
| 141 |
STK_ThrowRequireMsg(!updaters.empty(), "ERROR, no SidesetUpdater found on stk::mesh::BulkData"); |
| 142 |
updaters[0]->set_warn_about_internal_sideset(true); |
| 143 |
|
| 144 |
{//mesh was just modified, including elements touching the sideset, so expect warning |
| 145 |
std::ostringstream os; |
| 146 |
update_sidesets(*bulk, os); |
| 147 |
|
| 148 |
std::string warningString = os.str(); |
| 149 |
|
| 150 |
std::string expected = "WARNING, Internal sideset ("+ ssPart->name() +") detected. STK doesn't support internal sidesets\n(i.e., sidesets between elements where both elements are in the same block)\nExecution will continue but correct results are not guaranteed. Contact [email protected]\n"; |
| 151 |
|
| 152 |
EXPECT_EQ(expected, warningString); |
| 153 |
} |
| 154 |
|
| 155 |
bulk->modification_begin(); |
| 156 |
bulk->modification_end(); |
| 157 |
|
| 158 |
{//mesh now not modified, so expect no warning about internal sideset |
| 159 |
std::ostringstream os; |
| 160 |
update_sidesets(*bulk, os); |
| 161 |
|
| 162 |
std::string warningString = os.str(); |
| 163 |
std::string expected = ""; |
| 164 |
EXPECT_EQ(expected, warningString); |
| 165 |
} |
| 166 |
|
| 167 |
bulk->modification_begin(); |
| 168 |
|
| 169 |
//add an element that doesn't touch the sideset: |
| 170 |
stk::mesh::Part& elemTopoPart = meta.get_topology_root_part(stk::topology::HEX_8); |
| 171 |
stk::mesh::EntityId elemId = 3; |
| 172 |
stk::mesh::EntityIdVector nodeIds = {9, 10, 11, 12, 13, 14, 15, 16}; |
| 173 |
stk::mesh::declare_element(*bulk, elemTopoPart, elemId, nodeIds); |
| 174 |
|
| 175 |
bulk->modification_end(); |
| 176 |
|
| 177 |
{//mesh modified, but not sideset, so still no warning about internal sideset |
| 178 |
std::ostringstream os; |
| 179 |
update_sidesets(*bulk, os); |
| 180 |
|
| 181 |
std::string warningString = os.str(); |
| 182 |
std::string expected = ""; |
| 183 |
EXPECT_EQ(expected, warningString); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
TEST(StkIo, check_internal_sideset_warning_with_incremental_update) |
| 188 |
{ |
| 189 |
stk::ParallelMachine pm = MPI_COMM_WORLD; |
| 190 |
if (stk::parallel_machine_size(pm) != 1) { |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
const unsigned spatialDim = 3; |
| 195 |
std::shared_ptr<stk::mesh::BulkData> bulk = build_mesh(spatialDim, pm); |
| 196 |
stk::mesh::MetaData& meta = bulk->mesh_meta_data(); |
| 197 |
|
| 198 |
stk::mesh::Selector activeSelector(meta.universal_part()); |
| 199 |
bulk->register_observer(std::make_shared<stk::mesh::IncrementalSidesetUpdater>(*bulk, activeSelector)); |
| 200 |
|
| 201 |
std::vector<std::shared_ptr<stk::mesh::SidesetUpdater> > updaters = bulk->get_observer_type<stk::mesh::SidesetUpdater>(); |
| 202 |
STK_ThrowRequireMsg(!updaters.empty(), "ERROR, no SidesetUpdater found on stk::mesh::BulkData"); |
| 203 |
std::ostringstream os; |
| 204 |
updaters[0]->set_output_stream(os); |
| 205 |
updaters[0]->set_warn_about_internal_sideset(true); |
| 206 |
|
| 207 |
stk::unit_test_util::SidesetDirection direction = stk::unit_test_util::LEFT; |
| 208 |
stk::mesh::Part* ssPart = stk::unit_test_util::create_AA_mesh_with_sideset(*bulk, direction); |
| 209 |
EXPECT_TRUE(ssPart != nullptr); |
| 210 |
|
| 211 |
bulk->modification_end(); |
| 212 |
|
| 213 |
{//mesh was just modified, including elements touching the sideset, so expect warning |
| 214 |
std::string warningString = os.str(); |
| 215 |
|
| 216 |
std::string expected = "WARNING, Internal sideset ("+ ssPart->name() +") detected. STK doesn't support internal sidesets\n(i.e., sidesets between elements where both elements are in the same block)\nExecution will continue but correct results are not guaranteed. Contact [email protected]\n"; |
| 217 |
|
| 218 |
EXPECT_EQ(expected, warningString); |
| 219 |
} |
| 220 |
|
| 221 |
os.str(""); |
| 222 |
os.clear(); |
| 223 |
bulk->modification_begin(); |
| 224 |
bulk->modification_end(); |
| 225 |
|
| 226 |
{//mesh now not modified, so expect no warning about internal sideset |
| 227 |
std::string warningString = os.str(); |
| 228 |
std::string expected = ""; |
| 229 |
EXPECT_EQ(expected, warningString); |
| 230 |
} |
| 231 |
|
| 232 |
os.str(""); |
| 233 |
os.clear(); |
| 234 |
bulk->modification_begin(); |
| 235 |
|
| 236 |
//add an element that doesn't touch the sideset: |
| 237 |
stk::mesh::Part& elemTopoPart = meta.get_topology_root_part(stk::topology::HEX_8); |
| 238 |
stk::mesh::EntityId elemId = 3; |
| 239 |
stk::mesh::EntityIdVector nodeIds = {9, 10, 11, 12, 13, 14, 15, 16}; |
| 240 |
stk::mesh::declare_element(*bulk, elemTopoPart, elemId, nodeIds); |
| 241 |
|
| 242 |
bulk->modification_end(); |
| 243 |
|
| 244 |
{//mesh modified, but not sideset, so still no warning about internal sideset |
| 245 |
std::string warningString = os.str(); |
| 246 |
std::string expected = ""; |
| 247 |
EXPECT_EQ(expected, warningString); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
} |