| 1 |
#include <gtest/gtest.h> |
| 2 |
#include <Ioss_IOFactory.h> |
| 3 |
#include <Ioss_Region.h> |
| 4 |
#include <Ioss_DBUsage.h> |
| 5 |
#include <Ioss_PropertyManager.h> |
| 6 |
#include <Ionit_Initializer.h> |
| 7 |
|
| 8 |
#include <Ioss_ElementBlock.h> |
| 9 |
#include <Ioss_ElementTopology.h> |
| 10 |
#include <Ioss_NodeBlock.h> |
| 11 |
#include <Ioss_SideSet.h> |
| 12 |
#include <Ioss_NodeSet.h> |
| 13 |
|
| 14 |
#include <stk_mesh/base/BulkData.hpp> |
| 15 |
#include <stk_mesh/base/MetaData.hpp> |
| 16 |
#include <stk_mesh/base/Comm.hpp> |
| 17 |
#include <stk_mesh/base/FieldBase.hpp> |
| 18 |
#include <stk_mesh/base/ForEachEntity.hpp> |
| 19 |
#include <stk_mesh/base/GetEntities.hpp> |
| 20 |
#include <stk_mesh/base/Field.hpp> |
| 21 |
#include <stk_mesh/base/ExodusTranslator.hpp> |
| 22 |
#include <stk_mesh/base/SideSetUtil.hpp> |
| 23 |
#include <stk_mesh/base/SideSetEntry.hpp> |
| 24 |
#include <stk_unit_test_utils/MeshFixture.hpp> |
| 25 |
|
| 26 |
#include <stk_util/parallel/Parallel.hpp> |
| 27 |
#include <stk_util/environment/memory_util.hpp> |
| 28 |
|
| 29 |
#include <stk_unit_test_utils/ioUtils.hpp> |
| 30 |
#include <stk_unit_test_utils/getOption.h> |
| 31 |
|
| 32 |
#include <stk_io/StkMeshIoBroker.hpp> |
| 33 |
#include <stk_io/FillMesh.hpp> |
| 34 |
#include <stk_io/WriteMesh.hpp> |
| 35 |
|
| 36 |
#include <string> |
| 37 |
#include <iostream> |
| 38 |
#include <unistd.h> // for unlink |
| 39 |
|
| 40 |
#include <stk_unit_test_utils/BuildMesh.hpp> |
| 41 |
|
| 42 |
namespace { |
| 43 |
|
| 44 |
using stk::unit_test_util::build_mesh; |
| 45 |
using stk::unit_test_util::build_mesh_no_simple_fields; |
| 46 |
|
| 47 |
Ioss::DatabaseIO* create_output_db_io(const std::string &filename) |
| 48 |
{ |
| 49 |
Ioss::Init::Initializer init_db; |
| 50 |
|
| 51 |
Ioss::DatabaseUsage db_usage = Ioss::WRITE_RESULTS; |
| 52 |
MPI_Comm communicator = MPI_COMM_WORLD; |
| 53 |
Ioss::PropertyManager properties; |
| 54 |
|
| 55 |
properties.add(Ioss::Property("INTEGER_SIZE_DB", 8)); |
| 56 |
properties.add(Ioss::Property("INTEGER_SIZE_API", 8)); |
| 57 |
|
| 58 |
Ioss::DatabaseIO *db_io = Ioss::IOFactory::create("exodus", filename, db_usage, |
| 59 |
communicator, properties); |
| 60 |
return db_io; |
| 61 |
} |
| 62 |
|
| 63 |
void verify_num_nodes_in_file(MPI_Comm comm, |
| 64 |
const std::string& meshFileName, |
| 65 |
unsigned expectedNumNodes) |
| 66 |
{ |
| 67 |
std::shared_ptr<stk::mesh::BulkData> bulkData = build_mesh(comm); |
| 68 |
stk::io::fill_mesh(meshFileName, *bulkData); |
| 69 |
|
| 70 |
std::vector<size_t> entity_counts; |
| 71 |
stk::mesh::comm_mesh_counts(*bulkData, entity_counts); |
| 72 |
EXPECT_EQ(expectedNumNodes, entity_counts[stk::topology::NODE_RANK]); |
| 73 |
} |
| 74 |
|
| 75 |
void fill_node_ids_and_coords(const stk::mesh::BulkData& bulk, |
| 76 |
std::vector<int64_t>& node_ids, |
| 77 |
std::vector<double>& coordinates) |
| 78 |
{ |
| 79 |
const stk::mesh::MetaData& meta = bulk.mesh_meta_data(); |
| 80 |
stk::mesh::Field<double> * coordField = meta.get_field<double>(stk::topology::NODE_RANK, "coordinates"); |
| 81 |
int spatial_dim = meta.spatial_dimension(); |
| 82 |
|
| 83 |
STK_ThrowAssert(coordField != NULL); |
| 84 |
|
| 85 |
stk::mesh::Selector locallyOwned = meta.locally_owned_part(); |
| 86 |
|
| 87 |
int node_counter = 0; |
| 88 |
stk::mesh::for_each_entity_run_no_threads(bulk, stk::topology::NODE_RANK, locallyOwned, |
| 89 |
[&](const stk::mesh::BulkData& mesh, stk::mesh::Entity node) |
| 90 |
{ |
| 91 |
int node_id = mesh.identifier(node); |
| 92 |
node_ids[node_counter] = node_id; |
| 93 |
|
| 94 |
const double* coords = stk::mesh::field_data(*coordField, node); |
| 95 |
for(int k=0;k<spatial_dim;++k) { |
| 96 |
coordinates[spatial_dim*node_counter+k] = coords[k]; |
| 97 |
} |
| 98 |
node_counter++; |
| 99 |
}); |
| 100 |
} |
| 101 |
|
| 102 |
void fill_elem_ids_and_connectivity(const stk::mesh::BulkData& bulkData, |
| 103 |
const stk::mesh::Part* elemBlock, |
| 104 |
std::vector<int64_t>& elem_ids, |
| 105 |
std::vector<int64_t>& connectivity) |
| 106 |
{ |
| 107 |
stk::mesh::EntityVector elems; |
| 108 |
stk::mesh::get_entities(bulkData, stk::topology::ELEM_RANK, *elemBlock, elems); |
| 109 |
|
| 110 |
elem_ids.resize(elems.size()); |
| 111 |
const unsigned connectivity_size = elems.size()*elemBlock->topology().num_nodes(); |
| 112 |
connectivity.resize(connectivity_size); |
| 113 |
|
| 114 |
unsigned conn_counter = 0; |
| 115 |
for(size_t j=0;j<elems.size();++j) { |
| 116 |
elem_ids[j] = bulkData.identifier(elems[j]); |
| 117 |
const stk::mesh::ConnectedEntities nodes = bulkData.get_connected_entities(elems[j], stk::topology::NODE_RANK); |
| 118 |
for(unsigned k=0;k<nodes.size();++k) { |
| 119 |
connectivity[conn_counter++] = bulkData.identifier(nodes[k]); |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
TEST(StkIo, write_stk_mesh_using_ioss_instead_of_stk_io) |
| 125 |
{ |
| 126 |
MPI_Comm comm = MPI_COMM_WORLD; |
| 127 |
std::string file_written = "out.exo"; |
| 128 |
|
| 129 |
if(stk::parallel_machine_size(comm) != 1) { GTEST_SKIP(); } |
| 130 |
|
| 131 |
{ |
| 132 |
std::shared_ptr<stk::mesh::BulkData> bulkData = build_mesh(comm); |
| 133 |
stk::mesh::MetaData& meta = bulkData->mesh_meta_data(); |
| 134 |
|
| 135 |
stk::io::fill_mesh("generated:2x2x2|sideset:xX|nodeset:x", *bulkData); |
| 136 |
|
| 137 |
Ioss::DatabaseIO* db_io = create_output_db_io(file_written); |
| 138 |
EXPECT_TRUE(db_io->ok()); |
| 139 |
Ioss::Region output_region(db_io); |
| 140 |
|
| 141 |
//////////////////////////////////////////////////////////// |
| 142 |
|
| 143 |
output_region.begin_mode(Ioss::STATE_DEFINE_MODEL); |
| 144 |
|
| 145 |
std::string NodeBlockName = "nodeblock_1"; |
| 146 |
|
| 147 |
std::vector<size_t> entity_counts; |
| 148 |
stk::mesh::comm_mesh_counts(*bulkData, entity_counts); |
| 149 |
|
| 150 |
int64_t num_nodes = entity_counts[stk::topology::NODE_RANK]; |
| 151 |
int spatial_dim = meta.spatial_dimension(); |
| 152 |
|
| 153 |
Ioss::NodeBlock *output_node_block = new Ioss::NodeBlock(db_io, NodeBlockName, num_nodes, spatial_dim); |
| 154 |
output_region.add(output_node_block); |
| 155 |
|
| 156 |
stk::mesh::PartVector elemBlockParts; |
| 157 |
stk::mesh::fill_element_block_parts(meta, stk::topology::HEX_8, elemBlockParts); |
| 158 |
|
| 159 |
for(const stk::mesh::Part* elemBlock : elemBlockParts) { |
| 160 |
STK_ThrowRequireMsg(stk::io::is_part_io_part(*elemBlock),"element-block-part "<<elemBlock->name()<<" is not an IO part."); |
| 161 |
unsigned numElems = stk::mesh::count_entities(*bulkData, stk::topology::ELEM_RANK, *elemBlock); |
| 162 |
Ioss::ElementBlock *output_element_block = new Ioss::ElementBlock(db_io, elemBlock->name(), elemBlock->topology().name(), numElems); |
| 163 |
|
| 164 |
output_element_block->property_add(Ioss::Property("original_topology_type", elemBlock->topology().name())); |
| 165 |
output_element_block->property_add(Ioss::Property("id", elemBlock->id())); |
| 166 |
output_region.add(output_element_block); |
| 167 |
|
| 168 |
// how about attributes? |
| 169 |
} |
| 170 |
|
| 171 |
output_region.end_mode(Ioss::STATE_DEFINE_MODEL); |
| 172 |
|
| 173 |
//////////////////////////////////////////////////////////// |
| 174 |
|
| 175 |
output_region.begin_mode(Ioss::STATE_MODEL); |
| 176 |
|
| 177 |
Ioss::NodeBlock *node_block = output_region.get_node_blocks()[0]; |
| 178 |
|
| 179 |
std::vector<double> coordinates(spatial_dim*num_nodes); |
| 180 |
std::vector<int64_t> node_ids(num_nodes); |
| 181 |
|
| 182 |
fill_node_ids_and_coords(*bulkData, node_ids, coordinates); |
| 183 |
|
| 184 |
node_block->put_field_data("mesh_model_coordinates", coordinates); |
| 185 |
node_block->put_field_data("ids", node_ids); |
| 186 |
|
| 187 |
for(const stk::mesh::Part* elemBlock : elemBlockParts) { |
| 188 |
std::vector<int64_t> elem_ids; |
| 189 |
std::vector<int64_t> connectivity; |
| 190 |
|
| 191 |
fill_elem_ids_and_connectivity(*bulkData, elemBlock, elem_ids, connectivity); |
| 192 |
|
| 193 |
Ioss::ElementBlock *output_element_block = output_region.get_element_block(elemBlock->id()); |
| 194 |
output_element_block->put_field_data("connectivity_raw", connectivity); |
| 195 |
output_element_block->put_field_data("ids", elem_ids); |
| 196 |
} |
| 197 |
|
| 198 |
output_region.end_mode(Ioss::STATE_MODEL); |
| 199 |
//////////////////////////////////////////////////////////// |
| 200 |
} |
| 201 |
|
| 202 |
verify_num_nodes_in_file(comm, file_written, 27); |
| 203 |
unlink(file_written.c_str()); |
| 204 |
} |
| 205 |
|
| 206 |
class StkIoResultsOutput : public stk::unit_test_util::MeshFixture |
| 207 |
{ |
| 208 |
protected: |
| 209 |
void setup_mesh(const std::string & meshSpec, |
| 210 |
stk::mesh::BulkData::AutomaticAuraOption auraOption, |
| 211 |
unsigned initialBucketCapacity = stk::mesh::get_default_initial_bucket_capacity(), |
| 212 |
unsigned maximumBucketCapacity = stk::mesh::get_default_maximum_bucket_capacity()) override |
| 213 |
{ |
| 214 |
setup_empty_mesh(auraOption, initialBucketCapacity, maximumBucketCapacity); |
| 215 |
|
| 216 |
stk::mesh::Field<int> & field = get_meta().declare_field<int>(stk::topology::NODE_RANK, "nodal_field"); |
| 217 |
stk::mesh::put_field_on_mesh(field, get_meta().universal_part(), nullptr); |
| 218 |
|
| 219 |
stk::io::fill_mesh(meshSpec, get_bulk()); |
| 220 |
} |
| 221 |
|
| 222 |
const stk::mesh::Part& setup_mesh_with_part(const std::string & meshSpec, stk::mesh::BulkData::AutomaticAuraOption auraOption) |
| 223 |
{ |
| 224 |
setup_empty_mesh(auraOption); |
| 225 |
|
| 226 |
stk::mesh::Part& surface_part = get_meta().declare_part_with_topology("surface_1", stk::topology::QUADRILATERAL_4); |
| 227 |
stk::io::put_io_part_attribute(surface_part); |
| 228 |
|
| 229 |
stk::mesh::Field<int> & field = get_meta().declare_field<int>(stk::topology::NODE_RANK, "nodal_field"); |
| 230 |
stk::mesh::put_field_on_mesh(field, get_meta().universal_part(), nullptr); |
| 231 |
|
| 232 |
stk::io::fill_mesh(meshSpec, get_bulk()); |
| 233 |
|
| 234 |
return surface_part; |
| 235 |
} |
| 236 |
}; |
| 237 |
|
| 238 |
|
| 239 |
TEST_F(StkIoResultsOutput, close_output_mesh_makes_it_invalid) { |
| 240 |
if (stk::parallel_machine_size(MPI_COMM_WORLD) != 2) return; |
| 241 |
|
| 242 |
std::string meshSpec = stk::unit_test_util::get_option("--mesh-spec", "generated:1x1x2|sideset:z"); |
| 243 |
|
| 244 |
setup_mesh(meshSpec, stk::mesh::BulkData::NO_AUTO_AURA); |
| 245 |
|
| 246 |
stk::io::StkMeshIoBroker stkIo; |
| 247 |
stkIo.set_bulk_data(get_bulk()); |
| 248 |
|
| 249 |
std::string fileName1 = "output1.e"; |
| 250 |
|
| 251 |
stk::mesh::FieldBase * nodalField = get_meta().get_field(stk::topology::NODE_RANK, "nodal_field"); |
| 252 |
|
| 253 |
size_t outputFileIndex = stkIo.create_output_mesh(fileName1, stk::io::WRITE_RESULTS); |
| 254 |
stkIo.add_field(outputFileIndex, *nodalField, stk::topology::NODE_RANK, "nodal_field"); |
| 255 |
|
| 256 |
stkIo.close_output_mesh(outputFileIndex); |
| 257 |
unlink((fileName1).c_str()); |
| 258 |
|
| 259 |
EXPECT_THROW(stkIo.add_field(outputFileIndex, *nodalField, stk::topology::NODE_RANK, "nodal_field2"), std::runtime_error); |
| 260 |
|
| 261 |
|
| 262 |
} |
| 263 |
|
| 264 |
TEST_F(StkIoResultsOutput, write_nodal_face_variable_multiple_procs) |
| 265 |
{ |
| 266 |
if (stk::parallel_machine_size(MPI_COMM_WORLD) != 2) return; |
| 267 |
|
| 268 |
std::string meshSpec = stk::unit_test_util::get_option("--mesh-spec", "generated:1x1x2|sideset:z"); |
| 269 |
|
| 270 |
setup_mesh(meshSpec, stk::mesh::BulkData::NO_AUTO_AURA); |
| 271 |
|
| 272 |
const std::string fileName = "nodal_field_as_face_variable.e"; |
| 273 |
stk::io::StkMeshIoBroker stkIo; |
| 274 |
stkIo.set_bulk_data(get_bulk()); |
| 275 |
size_t outputFileIndex = stkIo.create_output_mesh(fileName, stk::io::WRITE_RESULTS); |
| 276 |
stkIo.use_nodeset_for_sideset_nodes_fields(outputFileIndex, true); |
| 277 |
stkIo.check_field_existence_when_creating_nodesets(outputFileIndex, false); |
| 278 |
|
| 279 |
stk::mesh::FieldBase * nodalField = get_meta().get_field(stk::topology::NODE_RANK, "nodal_field"); |
| 280 |
ASSERT_TRUE(nodalField != nullptr); |
| 281 |
stkIo.add_field(outputFileIndex, *nodalField, stk::topology::FACE_RANK, "nodal_field"); |
| 282 |
stkIo.write_output_mesh(outputFileIndex); |
| 283 |
stkIo.begin_output_step(outputFileIndex, 0.0); |
| 284 |
stkIo.write_defined_output_fields(outputFileIndex); |
| 285 |
stkIo.end_output_step(outputFileIndex); |
| 286 |
|
| 287 |
const unsigned spatialDim = 3; |
| 288 |
std::shared_ptr<stk::mesh::BulkData> bulk = build_mesh(spatialDim, MPI_COMM_WORLD, stk::mesh::BulkData::NO_AUTO_AURA); |
| 289 |
|
| 290 |
EXPECT_NO_THROW(stk::io::fill_mesh(fileName, *bulk)); |
| 291 |
unlink(fileName.c_str()); |
| 292 |
} |
| 293 |
|
| 294 |
TEST_F(StkIoResultsOutput, no_reconstruct_on_input) |
| 295 |
{ |
| 296 |
if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) return; |
| 297 |
|
| 298 |
std::string meshSpec = stk::unit_test_util::get_option("--mesh-spec", "generated:1x1x2|sideset:z"); |
| 299 |
|
| 300 |
setup_mesh(meshSpec, stk::mesh::BulkData::NO_AUTO_AURA); |
| 301 |
|
| 302 |
const stk::mesh::Part* surface_1 = get_meta().get_part("surface_1"); |
| 303 |
EXPECT_TRUE(surface_1 != nullptr); |
| 304 |
|
| 305 |
const stk::mesh::BulkData& bulk = get_bulk(); |
| 306 |
EXPECT_TRUE(bulk.does_sideset_exist(*surface_1)); |
| 307 |
|
| 308 |
EXPECT_FALSE( stk::mesh::should_reconstruct_sideset(bulk, *surface_1) ); |
| 309 |
} |
| 310 |
|
| 311 |
TEST_F(StkIoResultsOutput, reconstruct_on_creating_sideset) |
| 312 |
{ |
| 313 |
if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) return; |
| 314 |
|
| 315 |
std::string meshSpec = stk::unit_test_util::get_option("--mesh-spec", "generated:1x1x1"); |
| 316 |
|
| 317 |
const stk::mesh::Part& surface_part = setup_mesh_with_part(meshSpec, stk::mesh::BulkData::NO_AUTO_AURA); |
| 318 |
const stk::mesh::Part& block_1 = *get_meta().get_part("block_1"); |
| 319 |
get_meta().set_surface_to_block_mapping(&surface_part, {&block_1}); |
| 320 |
|
| 321 |
stk::mesh::BulkData& bulk = get_bulk(); |
| 322 |
EXPECT_FALSE(bulk.does_sideset_exist(surface_part)); |
| 323 |
|
| 324 |
EXPECT_TRUE( stk::mesh::should_reconstruct_sideset(bulk, surface_part) ); |
| 325 |
|
| 326 |
bulk.create_sideset(surface_part); |
| 327 |
|
| 328 |
EXPECT_FALSE( stk::mesh::should_reconstruct_sideset(bulk, surface_part) ); |
| 329 |
|
| 330 |
stk::mesh::Entity elem = bulk.get_entity(stk::topology::ELEM_RANK, 1); |
| 331 |
EXPECT_TRUE(bulk.is_valid(elem)); |
| 332 |
|
| 333 |
bulk.modification_begin(); |
| 334 |
bulk.declare_element_side(elem, 1, stk::mesh::ConstPartVector{&surface_part}); |
| 335 |
bulk.modification_end(); |
| 336 |
|
| 337 |
EXPECT_TRUE(bulk.does_sideset_exist(surface_part)); |
| 338 |
stk::mesh::SideSet& ss = bulk.get_sideset(surface_part); |
| 339 |
EXPECT_EQ(1u, ss.size()); |
| 340 |
stk::mesh::SideSetEntry entry = ss[0]; |
| 341 |
EXPECT_EQ(elem, entry.element); |
| 342 |
EXPECT_EQ(1, entry.side); |
| 343 |
|
| 344 |
EXPECT_FALSE( stk::mesh::should_reconstruct_sideset(bulk, surface_part) ); |
| 345 |
} |
| 346 |
|
| 347 |
TEST(TestStkIo, readWrite) |
| 348 |
{ |
| 349 |
std::string meshSpec = stk::unit_test_util::get_option("--mesh", "none specified"); |
| 350 |
if (meshSpec == "none specified") { |
| 351 |
if (stk::parallel_machine_rank(MPI_COMM_WORLD) == 0) { |
| 352 |
std::cout<<"No mesh specified, exiting."<<std::endl; |
| 353 |
} |
| 354 |
return; |
| 355 |
} |
| 356 |
|
| 357 |
std::string autoDecomp = stk::unit_test_util::get_option("--auto-decomp", "false"); |
| 358 |
|
| 359 |
std::shared_ptr<stk::mesh::BulkData> bulk = build_mesh(MPI_COMM_WORLD, stk::mesh::BulkData::NO_AUTO_AURA); |
| 360 |
|
| 361 |
if (autoDecomp == "false") { |
| 362 |
stk::io::fill_mesh(meshSpec, *bulk); |
| 363 |
} |
| 364 |
else { |
| 365 |
stk::io::fill_mesh_with_auto_decomp(meshSpec, *bulk); |
| 366 |
} |
| 367 |
|
| 368 |
stk::io::write_mesh("readWriteTest.exo", *bulk); |
| 369 |
} |
| 370 |
|
| 371 |
} |