| 1 |
// Copyright 2002 - 2008, 2010, 2011 National Technology Engineering |
| 2 |
// Solutions of Sandia, LLC (NTESS). Under the terms of Contract |
| 3 |
// DE-NA0003525 with NTESS, the U.S. Government retains certain rights |
| 4 |
// in this software. |
| 5 |
// |
| 6 |
// Redistribution and use in source and binary forms, with or without |
| 7 |
// modification, are permitted provided that the following conditions are |
| 8 |
// met: |
| 9 |
// |
| 10 |
// * Redistributions of source code must retain the above copyright |
| 11 |
// notice, this list of conditions and the following disclaimer. |
| 12 |
// |
| 13 |
// * Redistributions in binary form must reproduce the above |
| 14 |
// copyright notice, this list of conditions and the following |
| 15 |
// disclaimer in the documentation and/or other materials provided |
| 16 |
// with the distribution. |
| 17 |
// |
| 18 |
// * Neither the name of NTESS nor the names of its contributors |
| 19 |
// may be used to endorse or promote products derived from this |
| 20 |
// software without specific prior written permission. |
| 21 |
// |
| 22 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
// |
| 34 |
|
| 35 |
#include <stddef.h> // for size_t |
| 36 |
#include <stdlib.h> // for rand, srand, RAND_MAX |
| 37 |
#include <stk_io/IossBridge.hpp> // for is_part_io_part |
| 38 |
#include <stk_io/StkMeshIoBroker.hpp> // for StkMeshIoBroker |
| 39 |
#include <stk_mesh/base/BulkData.hpp> // for BulkData |
| 40 |
#include <stk_mesh/base/MetaData.hpp> // for MetaData |
| 41 |
#include <stk_unit_test_utils/BuildMesh.hpp> |
| 42 |
#include <gtest/gtest.h> |
| 43 |
#include <string> // for string |
| 44 |
#include <vector> // for vector, etc |
| 45 |
#include "gtest/gtest.h" // for AssertHelper, ASSERT_TRUE |
| 46 |
#include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc |
| 47 |
#include "stk_mesh/base/Bucket.hpp" // for Bucket |
| 48 |
#include "stk_mesh/base/Entity.hpp" // for Entity |
| 49 |
#include "stk_mesh/base/Part.hpp" // for Part |
| 50 |
#include "stk_mesh/base/Selector.hpp" // for Selector, operator& |
| 51 |
#include "stk_mesh/base/Types.hpp" // for PartVector, BucketVector, etc |
| 52 |
#include "stk_topology/topology.hpp" // for topology, etc |
| 53 |
#include "stk_util/parallel/Parallel.hpp" // for ParallelMachine |
| 54 |
#include "stk_mesh/base/Comm.hpp" |
| 55 |
#include "stk_unit_test_utils/ioUtils.hpp" |
| 56 |
#include "stk_unit_test_utils/getOption.h" |
| 57 |
#include "Ioss_Region.h" |
| 58 |
#include "Ioss_ElementBlock.h" |
| 59 |
#include "Ioss_NodeSet.h" |
| 60 |
|
| 61 |
namespace { |
| 62 |
|
| 63 |
using stk::unit_test_util::build_mesh; |
| 64 |
using stk::unit_test_util::build_mesh_no_simple_fields; |
| 65 |
|
| 66 |
//BeginDocTest1 |
| 67 |
TEST(StkMeshIoBroker, outputEqualsInput) |
| 68 |
{ |
| 69 |
// A simple test for reading and writing an exodus file, to make sure |
| 70 |
// that the output file is the same as the input file. |
| 71 |
|
| 72 |
std::string input_filename = stk::unit_test_util::get_option("--input-mesh", "no-mesh-specified"); |
| 73 |
|
| 74 |
if (input_filename != "no-mesh-specified") |
| 75 |
{ |
| 76 |
stk::ParallelMachine comm = MPI_COMM_WORLD; |
| 77 |
std::shared_ptr<stk::mesh::BulkData> bulk = build_mesh(comm); |
| 78 |
|
| 79 |
stk::io::StkMeshIoBroker ioBroker(comm); |
| 80 |
ioBroker.set_bulk_data(bulk); |
| 81 |
ioBroker.add_mesh_database(input_filename, stk::io::READ_MESH); |
| 82 |
ioBroker.create_input_mesh(); |
| 83 |
ioBroker.populate_bulk_data(); |
| 84 |
|
| 85 |
std::vector<size_t> counts; |
| 86 |
stk::mesh::comm_mesh_counts(*bulk, counts); |
| 87 |
|
| 88 |
if (bulk->parallel_rank() == 0) |
| 89 |
{ |
| 90 |
std::cerr << "input-mesh = " << input_filename << std::endl; |
| 91 |
std::cerr << " num-nodes = " << counts[stk::topology::NODE_RANK] |
| 92 |
<< ", num-elems = " << counts[stk::topology::ELEM_RANK] << std::endl; |
| 93 |
} |
| 94 |
|
| 95 |
std::cerr << "writing mesh with same io-broker as input" << std::endl; |
| 96 |
std::string same_iobroker_output_filename = std::string("output-same-iobroker-")+input_filename; |
| 97 |
size_t output_file_index = ioBroker.create_output_mesh(same_iobroker_output_filename, stk::io::WRITE_RESULTS); |
| 98 |
ioBroker.write_output_mesh(output_file_index); |
| 99 |
|
| 100 |
std::string new_iobroker_output_filename = std::string("output-")+input_filename; |
| 101 |
std::cerr << "writing mesh with new io-broker" << std::endl; |
| 102 |
|
| 103 |
stk::io::StkMeshIoBroker new_iobroker(comm); |
| 104 |
|
| 105 |
new_iobroker.set_bulk_data(bulk); |
| 106 |
size_t new_iobroker_output_file_index = new_iobroker.create_output_mesh(new_iobroker_output_filename, stk::io::WRITE_RESULTS); |
| 107 |
new_iobroker.write_output_mesh(new_iobroker_output_file_index); |
| 108 |
|
| 109 |
//Ideally we could use exodiff to compare the two output meshes to |
| 110 |
//verify they are the same. |
| 111 |
//But exodiff won't say two mesh files are different if the difference is only |
| 112 |
//a block-ordering difference. So let's instead verify that the two ioss-regions |
| 113 |
//have the same element-block grouping-entities. |
| 114 |
const Ioss::ElementBlockContainer& input_broker_elem_blocks = ioBroker.get_output_ioss_region(output_file_index)->get_element_blocks(); |
| 115 |
const Ioss::ElementBlockContainer& new_iobroker_elem_blocks = new_iobroker.get_output_ioss_region(new_iobroker_output_file_index)->get_element_blocks(); |
| 116 |
|
| 117 |
EXPECT_EQ(input_broker_elem_blocks.size(), new_iobroker_elem_blocks.size()); |
| 118 |
for(size_t i=0; i<input_broker_elem_blocks.size(); ++i) |
| 119 |
{ |
| 120 |
std::string input_name = input_broker_elem_blocks[i]->name(); |
| 121 |
std::string new_name = new_iobroker_elem_blocks[i]->name(); |
| 122 |
EXPECT_EQ(input_name, new_name); |
| 123 |
} |
| 124 |
|
| 125 |
//now also compare names/ids of nodesets |
| 126 |
const Ioss::NodeSetContainer input_broker_node_sets = ioBroker.get_output_ioss_region(output_file_index)->get_nodesets(); |
| 127 |
const Ioss::NodeSetContainer new_iobroker_node_sets = new_iobroker.get_output_ioss_region(output_file_index)->get_nodesets(); |
| 128 |
EXPECT_EQ(input_broker_node_sets.size(), new_iobroker_node_sets.size()); |
| 129 |
for(size_t i=0; i<input_broker_node_sets.size(); ++i) |
| 130 |
{ |
| 131 |
std::string input_name = input_broker_node_sets[i]->name(); |
| 132 |
std::string new_name = new_iobroker_node_sets[i]->name(); |
| 133 |
EXPECT_EQ(input_name, new_name); |
| 134 |
int input_id = input_broker_node_sets[i]->get_property("id").get_int(); |
| 135 |
int new_id = new_iobroker_node_sets[i]->get_property("id").get_int(); |
| 136 |
EXPECT_EQ(input_id, new_id); |
| 137 |
} |
| 138 |
} |
| 139 |
else |
| 140 |
{ |
| 141 |
std::cout << "test StkMeshIoBroker.outputEqualsInput, no mesh specified" << std::endl; |
| 142 |
} |
| 143 |
} |
| 144 |
//BeginDocTest1 |
| 145 |
|
| 146 |
void assert_read_mesh_has_original_topology(const std::string& fileName, |
| 147 |
const std::string& expectedTopologyType) |
| 148 |
{ |
| 149 |
const unsigned spatialDim = 3; |
| 150 |
std::shared_ptr<stk::mesh::BulkData> bulk = build_mesh(spatialDim, MPI_COMM_WORLD); |
| 151 |
stk::io::fill_mesh(fileName, *bulk); |
| 152 |
|
| 153 |
stk::mesh::Part* block1 = bulk->mesh_meta_data().get_part("block_1"); |
| 154 |
ASSERT_TRUE(block1 != nullptr); |
| 155 |
ASSERT_TRUE(stk::io::is_part_io_part(*block1)); |
| 156 |
ASSERT_TRUE(stk::io::has_original_topology_type(*block1)); |
| 157 |
ASSERT_EQ(expectedTopologyType, stk::io::get_original_topology_type(*block1)); |
| 158 |
} |
| 159 |
|
| 160 |
void read_then_write_mesh(const std::string& inputFileName, |
| 161 |
const std::string& outputFileName) |
| 162 |
{ |
| 163 |
const unsigned spatialDim = 3; |
| 164 |
std::shared_ptr<stk::mesh::BulkData> bulk = build_mesh(spatialDim, MPI_COMM_WORLD); |
| 165 |
stk::io::fill_mesh(inputFileName, *bulk); |
| 166 |
stk::io::write_mesh(outputFileName, *bulk); |
| 167 |
} |
| 168 |
|
| 169 |
TEST(StkMeshIoBroker, originalTopologyType) |
| 170 |
{ |
| 171 |
if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) { return; } |
| 172 |
|
| 173 |
std::string fileName = stk::unit_test_util::get_option("--mesh", "no-mesh-specified"); |
| 174 |
if (fileName == "no-mesh-specified") { return; } |
| 175 |
|
| 176 |
const bool isBar = (fileName == "bar.exo"); |
| 177 |
const bool isBeam = (fileName == "beam.exo"); |
| 178 |
ASSERT_TRUE(isBar || isBeam); |
| 179 |
std::string expectedTopologyType("beam2"); |
| 180 |
if (isBar) { |
| 181 |
expectedTopologyType = "bar2"; |
| 182 |
} |
| 183 |
|
| 184 |
assert_read_mesh_has_original_topology(fileName, expectedTopologyType); |
| 185 |
|
| 186 |
std::string tmpFileName("tmpMesh.exo"); |
| 187 |
read_then_write_mesh(fileName, tmpFileName); |
| 188 |
|
| 189 |
assert_read_mesh_has_original_topology(tmpFileName, expectedTopologyType); |
| 190 |
|
| 191 |
unlink(tmpFileName.c_str()); |
| 192 |
} |
| 193 |
|
| 194 |
} |