| 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 <unistd.h> // for unlink |
| 37 |
#include <stk_util/diag/StringUtil.hpp> // for make_lower |
| 38 |
#include <stk_io/StkMeshIoBroker.hpp> // for StkMeshIoBroker |
| 39 |
#include <stk_mesh/base/Field.hpp> // for Field |
| 40 |
#include <stk_mesh/base/MetaData.hpp> // for MetaData, put_field |
| 41 |
#include <gtest/gtest.h> |
| 42 |
#include <string> // for string, basic_string |
| 43 |
#include <algorithm> |
| 44 |
#include <cctype> |
| 45 |
#include "Ioss_DBUsage.h" // for DatabaseUsage::READ_MODEL, etc |
| 46 |
#include "Ioss_ElementTopology.h" // for NameList |
| 47 |
#include "Ioss_Field.h" // for Field, etc |
| 48 |
#include "Ioss_IOFactory.h" // for IOFactory |
| 49 |
#include "Ioss_NodeBlock.h" // for NodeBlock |
| 50 |
#include "Ioss_Region.h" // for Region, NodeBlockContainer |
| 51 |
#include "Ioss_Utils.h" // for Utils |
| 52 |
#include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc |
| 53 |
#include "stk_topology/topology.hpp" // for topology, etc |
| 54 |
|
| 55 |
|
| 56 |
namespace Ioss { class DatabaseIO; } |
| 57 |
namespace stk { namespace mesh { class FieldBase; } } |
| 58 |
namespace { |
| 59 |
|
| 60 |
void createNamedFieldOnMesh(stk::mesh::MetaData &stkMeshMetaData, const std::string &internalClientFieldName) |
| 61 |
{ |
| 62 |
const int numberOfStates = 1; |
| 63 |
stk::mesh::Field<double> &field0 = stkMeshMetaData.declare_field<double>(stk::topology::NODE_RANK, internalClientFieldName, numberOfStates); |
| 64 |
stk::mesh::put_field_on_mesh(field0, stkMeshMetaData.universal_part(), nullptr); |
| 65 |
} |
| 66 |
|
| 67 |
void testFieldNamedCorrectly(Ioss::Region &ioRegion, MPI_Comm /*communicator*/, std::vector<std::string> goldFieldNames) |
| 68 |
{ |
| 69 |
Ioss::NodeBlock *nodeBlockAssociatedWithField0 = ioRegion.get_node_blocks()[0]; |
| 70 |
Ioss::NameList fieldNames; |
| 71 |
nodeBlockAssociatedWithField0->field_describe(Ioss::Field::TRANSIENT, &fieldNames); |
| 72 |
for (std::string & name : goldFieldNames) { |
| 73 |
for (char & c : name) { |
| 74 |
c = tolower(c); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
ASSERT_EQ(goldFieldNames.size(), fieldNames.size()); |
| 79 |
for (auto goldFieldName : goldFieldNames) { |
| 80 |
auto entry = std::find(fieldNames.begin(), fieldNames.end(), goldFieldName.c_str()); |
| 81 |
if (entry == fieldNames.end()) { |
| 82 |
EXPECT_TRUE(false) << "Field " << goldFieldName << " not found in file" << std::endl; |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
TEST(FieldNamesTest, FieldNameRenameTwice) |
| 88 |
{ |
| 89 |
const std::string outputFilename = "ourSillyOwlput.exo"; |
| 90 |
MPI_Comm communicator = MPI_COMM_WORLD; |
| 91 |
const std::string internalClientFieldName = "Field0"; |
| 92 |
std::string requestedFieldNameForResultsOutput("NotjeSSe"); |
| 93 |
std::vector<std::string> outputFieldNames; |
| 94 |
{ |
| 95 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 96 |
const std::string exodusFileName = "generated:1x1x8"; |
| 97 |
size_t index = stkIo.add_mesh_database(exodusFileName, stk::io::READ_MESH); |
| 98 |
stkIo.set_active_mesh(index); |
| 99 |
stkIo.create_input_mesh(); |
| 100 |
|
| 101 |
stk::mesh::MetaData &stkMeshMetaData = stkIo.meta_data(); |
| 102 |
createNamedFieldOnMesh(stkMeshMetaData, internalClientFieldName); |
| 103 |
stkIo.populate_bulk_data(); |
| 104 |
|
| 105 |
size_t results_output_index = stkIo.create_output_mesh(outputFilename, stk::io::WRITE_RESULTS); |
| 106 |
|
| 107 |
stk::mesh::FieldBase *field0 = stkMeshMetaData.get_field(stk::topology::NODE_RANK, internalClientFieldName); |
| 108 |
stkIo.add_field(results_output_index, *field0, requestedFieldNameForResultsOutput); |
| 109 |
outputFieldNames.push_back(requestedFieldNameForResultsOutput); |
| 110 |
|
| 111 |
|
| 112 |
requestedFieldNameForResultsOutput = "jeSSe"; |
| 113 |
stkIo.add_field(results_output_index, *field0, requestedFieldNameForResultsOutput); |
| 114 |
outputFieldNames.push_back(requestedFieldNameForResultsOutput); |
| 115 |
|
| 116 |
stkIo.process_output_request(results_output_index, 0.0); |
| 117 |
} |
| 118 |
|
| 119 |
Ioss::DatabaseIO *iossDb = Ioss::IOFactory::create("exodus", outputFilename, Ioss::READ_MODEL, communicator); |
| 120 |
Ioss::Region ioRegion(iossDb); |
| 121 |
testFieldNamedCorrectly(ioRegion, communicator, outputFieldNames); |
| 122 |
|
| 123 |
unlink(outputFilename.c_str()); |
| 124 |
} |
| 125 |
|
| 126 |
TEST(FieldNamesTest, FieldNameWithRestart) |
| 127 |
{ |
| 128 |
std::string restartFilename = "output.restart"; |
| 129 |
MPI_Comm communicator = MPI_COMM_WORLD; |
| 130 |
const std::string internalClientFieldName = "Field0"; |
| 131 |
{ |
| 132 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 133 |
const std::string exodusFileName = "generated:1x1x8"; |
| 134 |
size_t index = stkIo.add_mesh_database(exodusFileName, stk::io::READ_MESH); |
| 135 |
stkIo.set_active_mesh(index); |
| 136 |
stkIo.create_input_mesh(); |
| 137 |
|
| 138 |
stk::mesh::MetaData &stkMeshMetaData = stkIo.meta_data(); |
| 139 |
createNamedFieldOnMesh(stkMeshMetaData, internalClientFieldName); |
| 140 |
stkIo.populate_bulk_data(); |
| 141 |
|
| 142 |
stk::mesh::FieldBase *field0 = stkMeshMetaData.get_field(stk::topology::NODE_RANK, internalClientFieldName); |
| 143 |
|
| 144 |
size_t fileIndex = stkIo.create_output_mesh(restartFilename, stk::io::WRITE_RESTART); |
| 145 |
stkIo.add_field(fileIndex, *field0); |
| 146 |
|
| 147 |
double time = 0.0; |
| 148 |
stkIo.begin_output_step(fileIndex, time); |
| 149 |
stkIo.write_defined_output_fields(fileIndex); |
| 150 |
stkIo.end_output_step(fileIndex); |
| 151 |
} |
| 152 |
|
| 153 |
Ioss::DatabaseIO *iossDb = Ioss::IOFactory::create("exodus", restartFilename, Ioss::READ_RESTART, communicator); |
| 154 |
Ioss::Region iossRegion(iossDb); |
| 155 |
std::string goldFieldName = sierra::make_lower(internalClientFieldName); |
| 156 |
testFieldNamedCorrectly(iossRegion, communicator, {goldFieldName}); |
| 157 |
|
| 158 |
unlink(restartFilename.c_str()); |
| 159 |
} |
| 160 |
|
| 161 |
TEST(FieldNamesTest, FieldNameWithResultsAndRestart) |
| 162 |
{ |
| 163 |
const std::string restartFilename = "FieldNameWithResultsAndRestart.restart"; |
| 164 |
const std::string outputFileName = "FieldNameWithResultsAndRestart.exo"; |
| 165 |
MPI_Comm communicator = MPI_COMM_WORLD; |
| 166 |
const std::string internalClientFieldName = "Field0"; |
| 167 |
{ |
| 168 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 169 |
const std::string exodusFileName = "generated:1x1x8"; |
| 170 |
size_t index = stkIo.add_mesh_database(exodusFileName, stk::io::READ_MESH); |
| 171 |
stkIo.set_active_mesh(index); |
| 172 |
stkIo.create_input_mesh(); |
| 173 |
|
| 174 |
stk::mesh::MetaData &stkMeshMetaData = stkIo.meta_data(); |
| 175 |
createNamedFieldOnMesh(stkMeshMetaData, internalClientFieldName); |
| 176 |
stkIo.populate_bulk_data(); |
| 177 |
|
| 178 |
size_t results_output_index = stkIo.create_output_mesh(outputFileName, stk::io::WRITE_RESULTS); |
| 179 |
stk::mesh::FieldBase *field0 = stkMeshMetaData.get_field(stk::topology::NODE_RANK, internalClientFieldName); |
| 180 |
std::string requestedFieldNameForResultsOutput("jeSSe"); |
| 181 |
stkIo.add_field(results_output_index, *field0, requestedFieldNameForResultsOutput); |
| 182 |
|
| 183 |
size_t restartFileIndex = stkIo.create_output_mesh(restartFilename, stk::io::WRITE_RESTART); |
| 184 |
stkIo.add_field(restartFileIndex, *field0); |
| 185 |
|
| 186 |
double time = 0.0; |
| 187 |
stkIo.begin_output_step(results_output_index, time); |
| 188 |
stkIo.write_defined_output_fields(results_output_index); |
| 189 |
stkIo.end_output_step(results_output_index); |
| 190 |
|
| 191 |
stkIo.begin_output_step(restartFileIndex, time); |
| 192 |
stkIo.write_defined_output_fields(restartFileIndex); |
| 193 |
stkIo.end_output_step(restartFileIndex); |
| 194 |
} |
| 195 |
Ioss::DatabaseIO *iossResultDb = Ioss::IOFactory::create("exodus", restartFilename, Ioss::READ_MODEL, communicator); |
| 196 |
Ioss::Region resultRegion(iossResultDb); |
| 197 |
std::string goldFieldName = sierra::make_lower(internalClientFieldName); |
| 198 |
testFieldNamedCorrectly(resultRegion, communicator, {goldFieldName}); |
| 199 |
|
| 200 |
Ioss::DatabaseIO *iossRestartDb = Ioss::IOFactory::create("exodus", restartFilename, Ioss::READ_RESTART, communicator); |
| 201 |
Ioss::Region restartRegion(iossRestartDb); |
| 202 |
testFieldNamedCorrectly(restartRegion, communicator, {goldFieldName}); |
| 203 |
|
| 204 |
unlink(outputFileName.c_str()); |
| 205 |
unlink(restartFilename.c_str()); |
| 206 |
} |
| 207 |
|
| 208 |
} |