| 1 |
#include <gtest/gtest.h> // for AssertHelper, EXPECT_EQ, etc |
| 2 |
#include <stddef.h> // for size_t, nullptr |
| 3 |
#include <stk_io/StkMeshIoBroker.hpp> // for StkMeshIoBroker |
| 4 |
#include <stk_io/StkIoUtils.hpp> |
| 5 |
|
| 6 |
#include <string> // for string |
| 7 |
#include "mpi.h" // for MPI_COMM_WORLD |
| 8 |
#include <stk_io/DatabasePurpose.hpp> |
| 9 |
#include <stk_mesh/base/Comm.hpp> |
| 10 |
#include <stk_mesh/base/MetaData.hpp> |
| 11 |
#include <stk_io/FillMesh.hpp> |
| 12 |
#include <Ioss_SideSet.h> |
| 13 |
#include <Ioss_SideBlock.h> |
| 14 |
#include <stk_mesh/base/SideSetEntry.hpp> |
| 15 |
#include <stk_mesh/base/SideSetUtil.hpp> |
| 16 |
#include <stk_mesh/base/GetEntities.hpp> |
| 17 |
#include <stk_mesh/base/ExodusTranslator.hpp> |
| 18 |
#include "stk_io/WriteMesh.hpp" |
| 19 |
#include "stk_unit_test_utils/FaceTestingUtils.hpp" |
| 20 |
#include "stk_unit_test_utils/BuildMesh.hpp" |
| 21 |
|
| 22 |
using stk::unit_test_util::build_mesh_no_simple_fields; |
| 23 |
|
| 24 |
namespace |
| 25 |
{ |
| 26 |
|
| 27 |
void verify_block_membership(const stk::io::IossBlockMembership &goldBlockMemberships, const stk::io::IossBlockMembership &blockMemberships) |
| 28 |
{ |
| 29 |
for(stk::io::IossBlockMembership::const_iterator goldIter = goldBlockMemberships.begin(); goldIter != goldBlockMemberships.end(); goldIter++) |
| 30 |
{ |
| 31 |
std::string goldSideSetName = goldIter->first; |
| 32 |
stk::io::IossBlockMembership::const_iterator iossSideSetIter = blockMemberships.find(goldSideSetName); |
| 33 |
ASSERT_TRUE(iossSideSetIter != blockMemberships.end()); |
| 34 |
ASSERT_EQ(goldIter->second.size(), iossSideSetIter->second.size()); |
| 35 |
for(const std::string & goldBlockName : goldIter->second) |
| 36 |
{ |
| 37 |
auto result = std::find(iossSideSetIter->second.begin(), iossSideSetIter->second.end(), goldBlockName); |
| 38 |
EXPECT_TRUE(result != iossSideSetIter->second.end()) << "failed to block, " << goldBlockName << ", in sideset, " << goldSideSetName << "." ; |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
void read_and_test_block_membership(const std::string& filename, const stk::io::IossBlockMembership &goldBlockMemberships) |
| 44 |
{ |
| 45 |
stk::ParallelMachine comm = MPI_COMM_WORLD; |
| 46 |
if(stk::parallel_machine_size(comm) <= 2) |
| 47 |
{ |
| 48 |
stk::io::StkMeshIoBroker stkIo; |
| 49 |
stkIo.property_add(Ioss::Property("DECOMPOSITION_METHOD", "RCB")); |
| 50 |
stkIo.add_mesh_database(filename, stk::io::READ_MESH); |
| 51 |
stkIo.create_input_mesh(); |
| 52 |
stkIo.add_all_mesh_fields_as_input_fields(); |
| 53 |
stk::io::IossBlockMembership blockMemberships = stk::io::get_block_memberships(stkIo); |
| 54 |
verify_block_membership(goldBlockMemberships, blockMemberships); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
TEST(StkIo, readALBFromIoss_verifyBlockMembership) |
| 59 |
{ |
| 60 |
stk::io::IossBlockMembership goldBlockMem = { {"surface_1", {"block_1"}} }; |
| 61 |
read_and_test_block_membership("ALB.e", goldBlockMem); |
| 62 |
} |
| 63 |
|
| 64 |
TEST(StkIo, readARBFromIoss_verifyBlockMembership) |
| 65 |
{ |
| 66 |
stk::io::IossBlockMembership goldBlockMem = { {"surface_1", {"block_2"}} }; |
| 67 |
read_and_test_block_membership("ARB.e", goldBlockMem); |
| 68 |
} |
| 69 |
|
| 70 |
TEST(StkIo, readADBFromIoss_verifyBlockMembership) |
| 71 |
{ |
| 72 |
stk::io::IossBlockMembership goldBlockMem = { {"surface_1", {"block_2", "block_1"}} }; |
| 73 |
read_and_test_block_membership("ADB.e", goldBlockMem); |
| 74 |
} |
| 75 |
|
| 76 |
TEST(StkIo, readALRBFromIoss_verifyBlockMembership) |
| 77 |
{ |
| 78 |
stk::io::IossBlockMembership goldBlockMem = { {"surface_1", {"block_1"}}, {"surface_2", {"block_2"}} }; |
| 79 |
read_and_test_block_membership("ALRB.e", goldBlockMem); |
| 80 |
} |
| 81 |
|
| 82 |
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 83 |
|
| 84 |
typedef std::pair<uint64_t, int> ElementIdToExodusSide; |
| 85 |
typedef std::vector<ElementIdToExodusSide> ElementSidePairs; |
| 86 |
typedef std::map<int,ElementSidePairs> ExodusSideSet; |
| 87 |
|
| 88 |
ExodusSideSet set_gold_data(ExodusSideSet &goldSideSetData, ExodusSideSet &goldSideSetData0, ExodusSideSet &goldSideSetData1) |
| 89 |
{ |
| 90 |
ExodusSideSet goldData; |
| 91 |
if(stk::parallel_machine_size(MPI_COMM_WORLD) == 1) |
| 92 |
goldData = goldSideSetData; |
| 93 |
else if(stk::parallel_machine_rank(MPI_COMM_WORLD) == 0) |
| 94 |
goldData = goldSideSetData0; |
| 95 |
else |
| 96 |
goldData = goldSideSetData1; |
| 97 |
return goldData; |
| 98 |
} |
| 99 |
|
| 100 |
void verify_element_side_pairs(stk::mesh::BulkData& bulkData, const ExodusSideSet& goldSideset) |
| 101 |
{ |
| 102 |
stk::mesh::create_bulkdata_sidesets(bulkData); |
| 103 |
|
| 104 |
std::map<int,ElementSidePairs>::const_iterator iter = goldSideset.begin(); |
| 105 |
for(;iter!=goldSideset.end();++iter) |
| 106 |
{ |
| 107 |
int id = iter->first; |
| 108 |
stk::mesh::Part *part = stk::unit_test_util::get_surface_part_with_id(bulkData.mesh_meta_data(), id); |
| 109 |
stk::mesh::SideSet &sset = bulkData.get_sideset(*part); |
| 110 |
ElementSidePairs goldSet = iter->second; |
| 111 |
ASSERT_EQ(goldSet.size(), sset.size()); |
| 112 |
bool found_matching_sset_entry = false; |
| 113 |
for (auto && goldEntry : goldSet) |
| 114 |
{ |
| 115 |
for (auto && sset_entry : sset) |
| 116 |
{ |
| 117 |
if (goldEntry.first == bulkData.identifier(sset_entry.element) && |
| 118 |
goldEntry.second == static_cast<int>(sset_entry.side)) |
| 119 |
{ |
| 120 |
found_matching_sset_entry = true; |
| 121 |
break; |
| 122 |
} |
| 123 |
} |
| 124 |
EXPECT_TRUE(found_matching_sset_entry); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
void fill_mesh(stk::mesh::BulkData& bulkData, const std::string& filename) |
| 130 |
{ |
| 131 |
stk::io::StkMeshIoBroker stkIo; |
| 132 |
stkIo.property_add(Ioss::Property("DECOMPOSITION_METHOD", "RCB")); |
| 133 |
stkIo.set_bulk_data(bulkData); |
| 134 |
stkIo.add_mesh_database(filename, stk::io::READ_MESH); |
| 135 |
stkIo.create_input_mesh(); |
| 136 |
stkIo.add_all_mesh_fields_as_input_fields(); |
| 137 |
stkIo.populate_bulk_data(); |
| 138 |
} |
| 139 |
|
| 140 |
std::string get_output_file_name(const std::string &input_file_name) |
| 141 |
{ |
| 142 |
size_t original_length = input_file_name.length(); |
| 143 |
std::string base_file_name = input_file_name.substr(0, original_length-2); // assuming .e files here |
| 144 |
std::string output_file_name = base_file_name + "_new.e"; |
| 145 |
return output_file_name; |
| 146 |
} |
| 147 |
|
| 148 |
void write_mesh(const std::string& filename, stk::mesh::BulkData &bulkData) |
| 149 |
{ |
| 150 |
std::string output_file_name = get_output_file_name(filename); |
| 151 |
stk::mesh::create_bulkdata_sidesets(bulkData); |
| 152 |
stk::io::write_mesh(output_file_name, bulkData); |
| 153 |
} |
| 154 |
|
| 155 |
void fill_sideset_data_structure_and_test(const std::string& filename, const ExodusSideSet &goldSideset) |
| 156 |
{ |
| 157 |
stk::ParallelMachine comm = MPI_COMM_WORLD; |
| 158 |
if(stk::parallel_machine_size(comm) <= 2) |
| 159 |
{ |
| 160 |
std::shared_ptr<stk::mesh::BulkData> bulkData = build_mesh_no_simple_fields(comm); |
| 161 |
fill_mesh(*bulkData, filename); |
| 162 |
|
| 163 |
verify_element_side_pairs(*bulkData, goldSideset); |
| 164 |
write_mesh(filename, *bulkData); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
TEST(StkIo, readALBFromIoss_verifySideSetCreation) |
| 169 |
{ |
| 170 |
ExodusSideSet goldSideSetData = { {1, {{1u, 5}} } }; |
| 171 |
ExodusSideSet goldSideSetData0 = { {1, {{1u, 5}} } }; |
| 172 |
ExodusSideSet goldSideSetData1 = { }; |
| 173 |
ExodusSideSet goldData = set_gold_data(goldSideSetData, goldSideSetData0, goldSideSetData1); |
| 174 |
fill_sideset_data_structure_and_test("ALB.e", goldData); |
| 175 |
} |
| 176 |
|
| 177 |
TEST(StkIo, readARBFromIoss_verifySideSetCreation) |
| 178 |
{ |
| 179 |
ExodusSideSet goldSideSetData = { {1, {{2u, 4}} } }; |
| 180 |
ExodusSideSet goldSideSetData0 = { }; |
| 181 |
ExodusSideSet goldSideSetData1 = { {1, {{2u, 4}} } }; |
| 182 |
ExodusSideSet goldData = set_gold_data(goldSideSetData, goldSideSetData0, goldSideSetData1); |
| 183 |
fill_sideset_data_structure_and_test("ARB.e", goldData); |
| 184 |
} |
| 185 |
|
| 186 |
TEST(StkIo, readADBFromIoss_verifySideSetCreation) |
| 187 |
{ |
| 188 |
ExodusSideSet goldSideSetData = { {1, {{2u, 4}, {1u, 5}} } }; |
| 189 |
ExodusSideSet goldSideSetData0 = { {1, {{1u, 5}} } }; |
| 190 |
ExodusSideSet goldSideSetData1 = { {1, {{2u, 4}} } }; |
| 191 |
ExodusSideSet goldData = set_gold_data(goldSideSetData, goldSideSetData0, goldSideSetData1); |
| 192 |
fill_sideset_data_structure_and_test("ADB.e", goldData); |
| 193 |
} |
| 194 |
|
| 195 |
TEST(StkIo, readALRBFromIoss_verifySideSetCreation) |
| 196 |
{ |
| 197 |
ExodusSideSet goldSideSetData = { {1, {{1u, 5}} }, {2, {{2u, 4}} } }; |
| 198 |
ExodusSideSet goldSideSetData0 = { {1, {{1u, 5}} } }; |
| 199 |
ExodusSideSet goldSideSetData1 = { {2, {{2u, 4}} } }; |
| 200 |
ExodusSideSet goldData = set_gold_data(goldSideSetData, goldSideSetData0, goldSideSetData1); |
| 201 |
fill_sideset_data_structure_and_test("ALRB.e", goldData); |
| 202 |
} |
| 203 |
|
| 204 |
void fill_sideset_data_structure_and_test_output_on_block_1(const std::string& filename, const ExodusSideSet &goldSideset) |
| 205 |
{ |
| 206 |
stk::ParallelMachine comm = MPI_COMM_WORLD; |
| 207 |
if(stk::parallel_machine_size(comm) <= 2) |
| 208 |
{ |
| 209 |
std::string output_file_name = get_output_file_name(filename); |
| 210 |
{ |
| 211 |
std::shared_ptr<stk::mesh::BulkData> bulkData = build_mesh_no_simple_fields(comm); |
| 212 |
stk::mesh::MetaData& meta = bulkData->mesh_meta_data(); |
| 213 |
fill_mesh(*bulkData, filename); |
| 214 |
|
| 215 |
stk::mesh::Part* part = meta.get_part("block_1"); |
| 216 |
ASSERT_TRUE(part!=nullptr); |
| 217 |
stk::mesh::Selector subset = *part; |
| 218 |
stk::io::write_mesh_subset(output_file_name, *bulkData, subset); |
| 219 |
} |
| 220 |
|
| 221 |
{ |
| 222 |
std::shared_ptr<stk::mesh::BulkData> bulkData = build_mesh_no_simple_fields(comm); |
| 223 |
stk::io::fill_mesh(output_file_name, *bulkData); |
| 224 |
verify_element_side_pairs(*bulkData, goldSideset); |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
TEST(StkIo, readADBFromIoss_writeBlock1Subset_verifySideSetCreation) |
| 230 |
{ |
| 231 |
ExodusSideSet goldSideSetData = { {1, {{1u, 5}} } }; |
| 232 |
ExodusSideSet goldSideSetData0 = { {1, {{1u, 5}} } }; |
| 233 |
ExodusSideSet goldSideSetData1 = {}; |
| 234 |
ExodusSideSet goldData = set_gold_data(goldSideSetData, goldSideSetData0, goldSideSetData1); |
| 235 |
fill_sideset_data_structure_and_test_output_on_block_1("ADB.e", goldData); |
| 236 |
} |
| 237 |
|
| 238 |
} |