| 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 <gtest/gtest.h> // for AssertHelper, EXPECT_EQ, etc |
| 36 |
#include <stddef.h> // for size_t |
| 37 |
#include <unistd.h> // for unlink |
| 38 |
#include <ostream> // for basic_ostream::operator<< |
| 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 <string> // for string |
| 43 |
#include "Ioss_DBUsage.h" // for DatabaseUsage::READ_RESTART |
| 44 |
#include "Ioss_DatabaseIO.h" // for DatabaseIO |
| 45 |
#include "Ioss_IOFactory.h" // for IOFactory |
| 46 |
#include "Ioss_Property.h" // for Property |
| 47 |
#include "Ioss_Region.h" // for Region |
| 48 |
#include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc |
| 49 |
#include "stk_topology/topology.hpp" // for topology, etc |
| 50 |
|
| 51 |
namespace { |
| 52 |
TEST(StkMeshIoBrokerHowTo, singleStepOnRestart) |
| 53 |
{ |
| 54 |
// ============================================================ |
| 55 |
// INITIALIZATION... |
| 56 |
|
| 57 |
std::string filename = "single_step.restart"; |
| 58 |
MPI_Comm comm = MPI_COMM_WORLD; |
| 59 |
{ |
| 60 |
stk::io::StkMeshIoBroker stkIo(comm); |
| 61 |
const std::string exodusFileName = "generated:1x1x8"; |
| 62 |
stkIo.add_mesh_database(exodusFileName, stk::io::READ_MESH); |
| 63 |
stkIo.create_input_mesh(); |
| 64 |
|
| 65 |
stk::mesh::Field<double> &field = stkIo.meta_data().declare_field<double>(stk::topology::NODE_RANK, "disp", 3); |
| 66 |
stk::mesh::put_field_on_mesh(field, stkIo.meta_data().universal_part(), nullptr); |
| 67 |
|
| 68 |
stkIo.populate_bulk_data(); |
| 69 |
|
| 70 |
{ |
| 71 |
//-BEGIN |
| 72 |
// ... Setup deleted |
| 73 |
// ============================================================ |
| 74 |
// EXAMPLE USAGE... |
| 75 |
// Create a restart file, |
| 76 |
size_t fh = stkIo.create_output_mesh(filename, |
| 77 |
stk::io::WRITE_RESTART); |
| 78 |
stkIo.add_field(fh, field); |
| 79 |
|
| 80 |
//+ Set the cycle count to 1. This will result in a maximum |
| 81 |
//+ of one step on the output database -- when a new step is |
| 82 |
//+ added, it will overwrite the existing step. |
| 83 |
stkIo.get_output_ioss_region(fh)->get_database()->set_cycle_count(1); /*@\label{io:cycle}*/ |
| 84 |
|
| 85 |
// Write multiple steps to the restart file. |
| 86 |
for (size_t step=0; step < 3; step++) { |
| 87 |
double time = step; |
| 88 |
stkIo.begin_output_step(fh, time); |
| 89 |
stkIo.write_defined_output_fields(fh); |
| 90 |
stkIo.end_output_step(fh); |
| 91 |
} |
| 92 |
|
| 93 |
//+ At this point, there should only be a single state on the |
| 94 |
//+ restart database. The time of this state should be 2.0. |
| 95 |
// ... Verification deleted |
| 96 |
//-END |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
{ |
| 101 |
// ============================================================ |
| 102 |
// VERIFICATION: |
| 103 |
//+ There should only be a single state on the restart database. |
| 104 |
//+ The time of this state should be 2.0. |
| 105 |
Ioss::DatabaseIO *iossDb = |
| 106 |
Ioss::IOFactory::create("exodus", filename, |
| 107 |
Ioss::READ_RESTART, comm); |
| 108 |
Ioss::Region region(iossDb); |
| 109 |
|
| 110 |
EXPECT_EQ(region.get_property("state_count").get_int(), 1); |
| 111 |
EXPECT_EQ(region.get_state_time(1), 2.0); |
| 112 |
} |
| 113 |
|
| 114 |
// ============================================================= |
| 115 |
// CLEANUP: |
| 116 |
unlink(filename.c_str()); |
| 117 |
} |
| 118 |
} |