| 1 |
|
| 2 |
|
| 3 |
// Copyright 2002 - 2008, 2010, 2011 National Technology Engineering |
| 4 |
// Solutions of Sandia, LLC (NTESS). Under the terms of Contract |
| 5 |
// DE-NA0003525 with NTESS, the U.S. Government retains certain rights |
| 6 |
// in this software. |
| 7 |
// |
| 8 |
// Redistribution and use in source and binary forms, with or without |
| 9 |
// modification, are permitted provided that the following conditions are |
| 10 |
// met: |
| 11 |
// |
| 12 |
// * Redistributions of source code must retain the above copyright |
| 13 |
// notice, this list of conditions and the following disclaimer. |
| 14 |
// |
| 15 |
// * Redistributions in binary form must reproduce the above |
| 16 |
// copyright notice, this list of conditions and the following |
| 17 |
// disclaimer in the documentation and/or other materials provided |
| 18 |
// with the distribution. |
| 19 |
// |
| 20 |
// * Neither the name of NTESS nor the names of its contributors |
| 21 |
// may be used to endorse or promote products derived from this |
| 22 |
// software without specific prior written permission. |
| 23 |
// |
| 24 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 25 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 26 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 27 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 28 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 29 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 30 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 31 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 32 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 33 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 34 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 |
// |
| 36 |
|
| 37 |
#include <gtest/gtest.h> |
| 38 |
#include <stk_util/diag/StringUtil.hpp> // for make_lower |
| 39 |
#include <stk_io/StkMeshIoBroker.hpp> // for StkMeshIoBroker |
| 40 |
#include <stk_mesh/base/Field.hpp> // for Field |
| 41 |
#include <stk_mesh/base/MetaData.hpp> // for MetaData, put_field |
| 42 |
#include "Ioss_DBUsage.h" // for DatabaseUsage::READ_MODEL, etc |
| 43 |
#include "Ioss_ElementTopology.h" // for NameList |
| 44 |
#include "Ioss_Field.h" // for Field, etc |
| 45 |
#include "Ioss_IOFactory.h" // for IOFactory |
| 46 |
#include "Ioss_NodeBlock.h" // for NodeBlock |
| 47 |
#include "Ioss_Region.h" // for Region, NodeBlockContainer |
| 48 |
#include "Ioss_Utils.h" // for Utils |
| 49 |
#include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc |
| 50 |
#include "stk_io/WriteMesh.hpp" |
| 51 |
#include "stk_topology/topology.hpp" // for topology, etc |
| 52 |
#include "stk_unit_test_utils/BuildMesh.hpp" |
| 53 |
#include <stk_unit_test_utils/getOption.h> |
| 54 |
#include <stk_unit_test_utils/TextMesh.hpp> |
| 55 |
#include <stddef.h> |
| 56 |
#include <unistd.h> |
| 57 |
#include <string> |
| 58 |
#include <algorithm> |
| 59 |
#include <cctype> |
| 60 |
#include <cstdio> |
| 61 |
|
| 62 |
namespace { |
| 63 |
|
| 64 |
void test_read_corrupt_restart(MPI_Comm communicator, const std::string& fileName, |
| 65 |
const std::string& fieldName, double expectedMaxTime, double restartTime) |
| 66 |
{ |
| 67 |
std::shared_ptr<stk::mesh::BulkData> bulk = stk::unit_test_util::build_mesh(communicator); |
| 68 |
stk::mesh::MetaData& meta = bulk->mesh_meta_data(); |
| 69 |
|
| 70 |
stk::io::StkMeshIoBroker stkIo; |
| 71 |
stkIo.set_bulk_data(*bulk); |
| 72 |
stkIo.add_mesh_database(fileName, stk::io::READ_RESTART); |
| 73 |
stkIo.create_input_mesh(); |
| 74 |
stkIo.add_all_mesh_fields_as_input_fields(); |
| 75 |
|
| 76 |
stk::mesh::FieldBase* field0 = meta.get_field(stk::topology::NODE_RANK, fieldName); |
| 77 |
ASSERT_TRUE(field0 != nullptr); |
| 78 |
stkIo.populate_bulk_data(); |
| 79 |
|
| 80 |
stk::io::MeshField meshField(*field0, field0->name()); |
| 81 |
stk::io::FieldReadStatus readStatus; |
| 82 |
|
| 83 |
meshField.set_read_time(restartTime); |
| 84 |
meshField.set_read_once(false); |
| 85 |
stkIo.read_input_field(meshField, readStatus); |
| 86 |
|
| 87 |
EXPECT_NE(expectedMaxTime, stkIo.get_max_time()); |
| 88 |
EXPECT_TRUE(readStatus.possiblyCorrupt); |
| 89 |
|
| 90 |
bool saveFile = stk::unit_test_util::has_option("-s"); |
| 91 |
if(!saveFile) { |
| 92 |
std::string parallelFilename = stkIo.get_input_ioss_region()->get_database()->decoded_filename(); |
| 93 |
unlink(parallelFilename.c_str()); |
| 94 |
} else { |
| 95 |
if(stk::parallel_machine_rank(communicator) == 0) { |
| 96 |
std::cout << "Saving corrupt restart files" << std::endl; |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
std::string get_corrupt_restart_mesh_filename(MPI_Comm communicator) |
| 102 |
{ |
| 103 |
std::ostringstream oss; |
| 104 |
oss << "generated:1x1x"; |
| 105 |
oss << stk::parallel_machine_size(communicator); |
| 106 |
return oss.str(); |
| 107 |
} |
| 108 |
|
| 109 |
void create_corrupt_restart(MPI_Comm communicator, |
| 110 |
const std::string& restartFilename, |
| 111 |
const std::string& internalClientFieldName, |
| 112 |
const int nSteps, const int skipStep) |
| 113 |
{ |
| 114 |
std::string parallelFilename; |
| 115 |
|
| 116 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 117 |
const std::string exodusFileName = get_corrupt_restart_mesh_filename(communicator); |
| 118 |
size_t index = stkIo.add_mesh_database(exodusFileName, stk::io::READ_MESH); |
| 119 |
stkIo.set_active_mesh(index); |
| 120 |
stkIo.create_input_mesh(); |
| 121 |
|
| 122 |
stk::mesh::MetaData &stkMeshMetaData = stkIo.meta_data(); |
| 123 |
const int numberOfStates = 2; |
| 124 |
stk::mesh::Field<double> &field0 = stkMeshMetaData.declare_field<double>(stk::topology::NODE_RANK, |
| 125 |
internalClientFieldName, |
| 126 |
numberOfStates); |
| 127 |
|
| 128 |
stk::mesh::put_field_on_mesh(field0, stkMeshMetaData.universal_part(), nullptr); |
| 129 |
|
| 130 |
stkIo.populate_bulk_data(); |
| 131 |
|
| 132 |
stkIo.property_add(Ioss::Property("FLUSH_INTERVAL", 1)); |
| 133 |
size_t fileIndex = stkIo.create_output_mesh(restartFilename, stk::io::WRITE_RESTART); |
| 134 |
stkIo.add_field(fileIndex, field0); |
| 135 |
|
| 136 |
std::shared_ptr<Ioss::Region> region = stkIo.get_output_ioss_region(fileIndex); |
| 137 |
|
| 138 |
for(int i=0; i<=nSteps; i++) { |
| 139 |
if(stk::parallel_machine_rank(communicator) == 1 && (i == skipStep)) { |
| 140 |
continue; |
| 141 |
} |
| 142 |
|
| 143 |
double time = i; |
| 144 |
stkIo.begin_output_step(fileIndex, time); |
| 145 |
stkIo.write_defined_output_fields(fileIndex); |
| 146 |
stkIo.end_output_step(fileIndex); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
TEST(StkIO, CorruptRestart) |
| 151 |
{ |
| 152 |
MPI_Comm communicator = MPI_COMM_WORLD; |
| 153 |
if (stk::parallel_machine_size(communicator) == 1) { GTEST_SKIP(); } |
| 154 |
|
| 155 |
#ifndef NDEBUG |
| 156 |
// Skip debug due to parallel collective calls we want to avoid |
| 157 |
GTEST_SKIP(); |
| 158 |
#endif |
| 159 |
|
| 160 |
std::string restartFilename = "output.rst"; |
| 161 |
const std::string internalClientFieldName = "Field0"; |
| 162 |
|
| 163 |
int nSteps = 5; |
| 164 |
int skipStep = 5; |
| 165 |
|
| 166 |
nSteps = stk::unit_test_util::get_command_line_option("-nSteps", nSteps); |
| 167 |
skipStep = stk::unit_test_util::get_command_line_option("-skipStep", skipStep); |
| 168 |
|
| 169 |
if(stk::parallel_machine_rank(communicator) == 0) { |
| 170 |
std::cout << "Creating corrupt restart files" << std::endl; |
| 171 |
} |
| 172 |
create_corrupt_restart(communicator, restartFilename, internalClientFieldName, nSteps, skipStep); |
| 173 |
|
| 174 |
if(stk::parallel_machine_rank(communicator) == 0) { |
| 175 |
std::cout << "Testing corrupt restart" << std::endl; |
| 176 |
} |
| 177 |
|
| 178 |
double expectedMaxTime = std::min(nSteps, skipStep); |
| 179 |
double restartTime = double(skipStep); |
| 180 |
test_read_corrupt_restart(communicator, restartFilename, internalClientFieldName, expectedMaxTime, restartTime); |
| 181 |
} |
| 182 |
|
| 183 |
TEST(StkIo, EmptyLocalBlock_beam2) |
| 184 |
{ |
| 185 |
if (stk::parallel_machine_size(MPI_COMM_WORLD) != 2) { GTEST_SKIP(); } |
| 186 |
|
| 187 |
std::unique_ptr<stk::mesh::BulkData> bulk = stk::mesh::MeshBuilder(MPI_COMM_WORLD).set_spatial_dimension(3).create(); |
| 188 |
|
| 189 |
const std::string meshDesc = |
| 190 |
"0,1,SHELL_QUAD_4, 1,4,5,2, block_1\n\ |
| 191 |
1,2,SHELL_QUAD_4, 2,5,6,3, block_1\n\ |
| 192 |
1,3,BEAM_2, 6,7, block_nodes"; |
| 193 |
std::vector<double> coords = {0,0,0, 0,1,0, 0,2,0, |
| 194 |
1,0,0, 1,1,0, 1,2,0, |
| 195 |
0,3,0}; |
| 196 |
|
| 197 |
stk::unit_test_util::setup_text_mesh(*bulk, stk::unit_test_util::get_full_text_mesh_desc(meshDesc, coords)); |
| 198 |
|
| 199 |
stk::io::write_mesh("shellq4_beam.g", *bulk, stk::io::WRITE_RESTART); |
| 200 |
stk::parallel_machine_barrier(MPI_COMM_WORLD); |
| 201 |
|
| 202 |
stk::io::StkMeshIoBroker ioBroker(MPI_COMM_SELF); |
| 203 |
ioBroker.set_mesh_builder(std::make_shared<stk::mesh::MeshBuilder>()); |
| 204 |
std::string pllFileName = "shellq4_beam.g.2." + std::to_string(stk::parallel_machine_rank(MPI_COMM_WORLD)); |
| 205 |
|
| 206 |
ioBroker.add_mesh_database(pllFileName, stk::io::READ_MESH); |
| 207 |
EXPECT_NO_THROW(ioBroker.create_input_mesh()); |
| 208 |
ioBroker.populate_bulk_data(); |
| 209 |
|
| 210 |
std::remove(pllFileName.c_str()); |
| 211 |
} |
| 212 |
|
| 213 |
} |