| 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_THROW, etc |
| 36 |
#include <stddef.h> // for size_t |
| 37 |
#include <unistd.h> // for unlink |
| 38 |
#include <exception> // for exception |
| 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 "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc |
| 46 |
#include "stk_io/MeshField.hpp" // for MeshField |
| 47 |
#include "stk_mesh/base/Entity.hpp" // for Entity |
| 48 |
#include "stk_mesh/base/FieldBase.hpp" // for field_data |
| 49 |
#include "stk_topology/topology.hpp" // for topology, etc |
| 50 |
#include "stk_util/parallel/Parallel.hpp" |
| 51 |
namespace { |
| 52 |
|
| 53 |
TEST(StkMeshIoBrokerHowTo, handleMissingFieldOnReadThrow) |
| 54 |
{ |
| 55 |
std::string ic_name = "input_field_example.e"; |
| 56 |
MPI_Comm communicator = MPI_COMM_WORLD; |
| 57 |
int numProcs = stk::parallel_machine_size(communicator); |
| 58 |
if (numProcs != 1) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
{ |
| 63 |
// ============================================================ |
| 64 |
//+ INITIALIZATION: |
| 65 |
//+ Create a mesh with the nodal field "temp" for 3 timesteps. |
| 66 |
//+ The value of the field at each node is 0.0 at time 0.0, |
| 67 |
//+ 1.0 at time 1.0, and 2.0 at time 2.0 |
| 68 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 69 |
|
| 70 |
const std::string generatedFileName = "generated:8x8x8|nodeset:xyz"; |
| 71 |
size_t index = stkIo.add_mesh_database(generatedFileName, stk::io::READ_MESH); |
| 72 |
stkIo.set_active_mesh(index); // Optional if only a single input database |
| 73 |
stkIo.create_input_mesh(); |
| 74 |
|
| 75 |
stk::mesh::Field<double> &temperature = |
| 76 |
stkIo.meta_data().declare_field<double>(stk::topology::NODE_RANK, "temperature", 1); |
| 77 |
stk::mesh::put_field_on_mesh(temperature, stkIo.meta_data().universal_part(), nullptr); |
| 78 |
stkIo.populate_bulk_data(); |
| 79 |
|
| 80 |
size_t fh = stkIo.create_output_mesh(ic_name, stk::io::WRITE_RESULTS); |
| 81 |
|
| 82 |
//+ The name of the field on the database will be "temp" |
| 83 |
stkIo.add_field(fh, temperature, "temp"); |
| 84 |
|
| 85 |
std::vector<stk::mesh::Entity> nodes; |
| 86 |
stk::mesh::get_entities(stkIo.bulk_data(), |
| 87 |
stk::topology::NODE_RANK, nodes); |
| 88 |
|
| 89 |
// Add three steps to the database |
| 90 |
// For each step, the value of the field is the value 'time' |
| 91 |
for (size_t i=0; i < 3; i++) { |
| 92 |
double time = i; |
| 93 |
|
| 94 |
for(size_t inode=0; inode<nodes.size(); inode++) { |
| 95 |
double *fieldDataForNode = stk::mesh::field_data(temperature, nodes[inode]); |
| 96 |
*fieldDataForNode = time; |
| 97 |
} |
| 98 |
|
| 99 |
stkIo.begin_output_step(fh, time); |
| 100 |
stkIo.write_defined_output_fields(fh); |
| 101 |
stkIo.end_output_step(fh); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
{ |
| 106 |
// ============================================================ |
| 107 |
//+ EXAMPLE: |
| 108 |
//+ Demonstrate what happens when application requests the |
| 109 |
//+ reading of a field that does not exist on the input |
| 110 |
//+ mesh database. The nodal field "displacement" is |
| 111 |
//+ requested for input from the database field "disp" which |
| 112 |
//+ does not exist. |
| 113 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 114 |
size_t index = stkIo.add_mesh_database(ic_name, stk::io::READ_MESH); |
| 115 |
stkIo.set_active_mesh(index); |
| 116 |
stkIo.create_input_mesh(); |
| 117 |
|
| 118 |
stk::mesh::Field<double> &temperature = |
| 119 |
stkIo.meta_data().declare_field<double>(stk::topology::NODE_RANK, "temperature", 1); |
| 120 |
stk::mesh::put_field_on_mesh(temperature, stkIo.meta_data().universal_part(), nullptr); |
| 121 |
|
| 122 |
stk::mesh::Field<double> &displacement = |
| 123 |
stkIo.meta_data().declare_field<double>(stk::topology::NODE_RANK, "displacement", 1); |
| 124 |
stk::mesh::put_field_on_mesh(displacement, stkIo.meta_data().universal_part(), nullptr); |
| 125 |
stkIo.populate_bulk_data(); |
| 126 |
|
| 127 |
// The name of the field on the database is "temp" |
| 128 |
// This field does exist and should be read correctly |
| 129 |
stkIo.add_input_field(stk::io::MeshField(temperature, "temp")); |
| 130 |
|
| 131 |
//+ The name of the field on the database is "disp" |
| 132 |
//+ This field does not exist and will not be found. |
| 133 |
stkIo.add_input_field(stk::io::MeshField(displacement, "disp")); |
| 134 |
|
| 135 |
//-BEGIN |
| 136 |
//+ If read the fields, but don't pass in the 'missing_fields' |
| 137 |
//+ vector, the code will print an error message and throw an |
| 138 |
//+ exception if it doesn't find all of the requested fields. |
| 139 |
EXPECT_ANY_THROW(stkIo.read_defined_input_fields(2.0)); |
| 140 |
|
| 141 |
//+ If code throws due to missing field(s), it will NOT read |
| 142 |
//+ even the fields that exist. |
| 143 |
//-END |
| 144 |
} |
| 145 |
// ============================================================ |
| 146 |
// Cleanup |
| 147 |
unlink(ic_name.c_str()); |
| 148 |
} |
| 149 |
} |