| 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, etc |
| 36 |
#include <stddef.h> // for size_t |
| 37 |
#include <unistd.h> // for unlink |
| 38 |
#include <iomanip> // for operator<< |
| 39 |
#include <stk_io/StkMeshIoBroker.hpp> // for StkMeshIoBroker |
| 40 |
#include <stk_mesh/base/Field.hpp> // for Field |
| 41 |
#include <stk_mesh/base/GetEntities.hpp> // for get_entities |
| 42 |
#include <stk_mesh/base/MetaData.hpp> // for MetaData, put_field |
| 43 |
#include <string> // for string |
| 44 |
#include <vector> // for vector |
| 45 |
#include "Ioss_Field.h" // for Field, Field::RoleType, etc |
| 46 |
#include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc |
| 47 |
#include "stk_io/IossBridge.hpp" // for get_field_role |
| 48 |
#include "stk_io/MeshField.hpp" // for MeshField, etc |
| 49 |
#include "stk_mesh/base/Entity.hpp" // for Entity |
| 50 |
#include "stk_mesh/base/FieldBase.hpp" // for field_data |
| 51 |
#include "stk_mesh/base/Types.hpp" // for FieldVector |
| 52 |
#include "stk_topology/topology.hpp" // for topology, etc |
| 53 |
#include "stk_util/parallel/Parallel.hpp" |
| 54 |
namespace { |
| 55 |
|
| 56 |
TEST(StkMeshIoBrokerHowTo, restartInterpolatedField) |
| 57 |
{ |
| 58 |
std::string rs_name = "restart_interpolate_field.rs"; |
| 59 |
std::string ic_name = "interpolate_field.e"; |
| 60 |
MPI_Comm communicator = MPI_COMM_WORLD; |
| 61 |
int numProcs = stk::parallel_machine_size(communicator); |
| 62 |
if (numProcs != 1) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
// ============================================================ |
| 67 |
//+ INITIALIZATION: |
| 68 |
{ |
| 69 |
//+ Create a "restart database" with several nodal and element fields, |
| 70 |
//+ and some timesteps... |
| 71 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 72 |
|
| 73 |
const std::string generatedFileName = "generated:8x8x8|shell:XYZ|" |
| 74 |
"nodeset:xyz|times:3|variables:nodal,4,element,3,nodeset,2"; |
| 75 |
stkIo.add_mesh_database(generatedFileName, stk::io::READ_MESH); |
| 76 |
stkIo.create_input_mesh(); |
| 77 |
stkIo.add_all_mesh_fields_as_input_fields(); |
| 78 |
stkIo.populate_bulk_data(); |
| 79 |
|
| 80 |
size_t fh = stkIo.create_output_mesh(rs_name, stk::io::WRITE_RESTART); |
| 81 |
|
| 82 |
// Add all fields as restart fields... |
| 83 |
const stk::mesh::FieldVector &fields = stkIo.meta_data().get_fields(); |
| 84 |
for (size_t i=0; i < fields.size(); i++) { |
| 85 |
const Ioss::Field::RoleType* role = stk::io::get_field_role(*fields[i]); |
| 86 |
if ( role && *role == Ioss::Field::TRANSIENT ) { |
| 87 |
stkIo.add_field(fh, *fields[i]); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
// Output the field data. |
| 92 |
for (size_t i=0; i < 3; i++) { |
| 93 |
double time = i; |
| 94 |
stkIo.begin_output_step(fh, time); |
| 95 |
stkIo.write_defined_output_fields(fh); |
| 96 |
stkIo.end_output_step(fh); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
{ |
| 101 |
//+ Create an "initial condition database" with the nodal field |
| 102 |
//+ "temp" for 10 timesteps - 0.0, 1.0, ..., 9.0. |
| 103 |
//+ The value of the field at each node is the 'time' value. |
| 104 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 105 |
|
| 106 |
const std::string generatedFileName = "generated:8x8x8|nodeset:xyz"; |
| 107 |
stkIo.add_mesh_database(generatedFileName, stk::io::READ_MESH); |
| 108 |
stkIo.create_input_mesh(); |
| 109 |
|
| 110 |
stk::mesh::Field<double> &temperature = |
| 111 |
stkIo.meta_data().declare_field<double>(stk::topology::NODE_RANK, "temperature", 1); |
| 112 |
stk::mesh::put_field_on_mesh(temperature, stkIo.meta_data().universal_part(), nullptr); |
| 113 |
stkIo.populate_bulk_data(); |
| 114 |
|
| 115 |
size_t fh = stkIo.create_output_mesh(ic_name, stk::io::WRITE_RESULTS); |
| 116 |
|
| 117 |
//+ The name of the field on the database will be "temp" |
| 118 |
stkIo.add_field(fh, temperature, "temp"); |
| 119 |
|
| 120 |
std::vector<stk::mesh::Entity> nodes; |
| 121 |
stk::mesh::get_entities(stkIo.bulk_data(), |
| 122 |
stk::topology::NODE_RANK, nodes); |
| 123 |
|
| 124 |
// Add three steps to the database |
| 125 |
// For each step, the value of the field is the value 'time' |
| 126 |
for (size_t i=0; i < 10; i++) { |
| 127 |
double time = i; |
| 128 |
|
| 129 |
for(size_t inode=0; inode<nodes.size(); inode++) { |
| 130 |
double *fieldDataForNode = |
| 131 |
stk::mesh::field_data(temperature, nodes[inode]); |
| 132 |
*fieldDataForNode = time; |
| 133 |
} |
| 134 |
|
| 135 |
stkIo.begin_output_step(fh, time); |
| 136 |
stkIo.write_defined_output_fields(fh); |
| 137 |
stkIo.end_output_step(fh); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
{ |
| 142 |
//-BEGIN |
| 143 |
// ============================================================ |
| 144 |
//+ EXAMPLE: |
| 145 |
//+ The restart mesh database has 3 timesteps with times 0.0, 1.0, 2.0, |
| 146 |
//+ and several fields. |
| 147 |
//+ |
| 148 |
//+ The initial condition database has 10 timesteps with times |
| 149 |
//+ 0.0, 1.0, ..., 9.0 and a nodal variable "temp" |
| 150 |
//+ The value of the field "temp" is equal to the time |
| 151 |
//+ |
| 152 |
//+ The example will read the restart database at time 1.0 |
| 153 |
//+ and then simulate continuing the analysis at that time |
| 154 |
//+ reading the initial condition data from the other database |
| 155 |
//+ interpolating this data. |
| 156 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 157 |
size_t ic = stkIo.add_mesh_database(ic_name, stk::io::READ_MESH); |
| 158 |
size_t rs = stkIo.add_mesh_database(rs_name, stk::io::READ_RESTART); |
| 159 |
|
| 160 |
//+ "Restart" the calculation... |
| 161 |
double time = 1.0; |
| 162 |
stkIo.set_active_mesh(rs); |
| 163 |
stkIo.create_input_mesh(); |
| 164 |
|
| 165 |
stkIo.add_all_mesh_fields_as_input_fields(); |
| 166 |
|
| 167 |
stk::mesh::Field<double> &temperature = |
| 168 |
stkIo.meta_data().declare_field<double>(stk::topology::NODE_RANK, "temperature", 1); |
| 169 |
stk::mesh::put_field_on_mesh(temperature, stkIo.meta_data().universal_part(), nullptr); |
| 170 |
|
| 171 |
// The name of the field on the initial condition database is "temp" |
| 172 |
stkIo.add_input_field(ic, stk::io::MeshField(temperature, "temp", |
| 173 |
stk::io::MeshField::LINEAR_INTERPOLATION)); |
| 174 |
stkIo.populate_bulk_data(); |
| 175 |
|
| 176 |
std::vector<stk::mesh::Entity> nodes; |
| 177 |
stk::mesh::get_entities(stkIo.bulk_data(), stk::topology::NODE_RANK, nodes); |
| 178 |
|
| 179 |
//+ Read restart data |
| 180 |
stkIo.read_defined_input_fields(time); |
| 181 |
|
| 182 |
//+ Switch active mesh to "initial condition" database |
| 183 |
stkIo.set_active_mesh(ic); |
| 184 |
|
| 185 |
double delta_time = 1.0 / 4.0; |
| 186 |
while (time <= 9.0) { |
| 187 |
//+ Read the field values from the database and verify that they |
| 188 |
//+ are interpolated correctly. |
| 189 |
stkIo.read_defined_input_fields(time); |
| 190 |
|
| 191 |
// ============================================================ |
| 192 |
//+ VERIFICATION |
| 193 |
// The value of the "temperature" field at all nodes should be 'time' |
| 194 |
for(size_t i=0; i<nodes.size(); i++) { |
| 195 |
double *fieldDataForNode = stk::mesh::field_data(temperature, nodes[i]); |
| 196 |
EXPECT_DOUBLE_EQ(time, *fieldDataForNode); |
| 197 |
} |
| 198 |
time += delta_time; |
| 199 |
} |
| 200 |
//-END |
| 201 |
} |
| 202 |
// ============================================================ |
| 203 |
// Cleanup |
| 204 |
unlink(rs_name.c_str()); |
| 205 |
unlink(ic_name.c_str()); |
| 206 |
} |
| 207 |
} |