| 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, ASSERT_TRUE |
| 36 |
#include <stk_io/InputQuery.hpp> |
| 37 |
#include <stk_io/IossBridge.hpp> // for is_part_io_part |
| 38 |
#include <stk_io/StkMeshIoBroker.hpp> // for StkMeshIoBroker |
| 39 |
#include <stk_mesh/base/BulkData.hpp> // for BulkData |
| 40 |
#include "stk_unit_test_utils/GeneratedMeshToFile.hpp" |
| 41 |
#include "stk_unit_test_utils/ioUtils.hpp" |
| 42 |
#include <stddef.h> // for size_t |
| 43 |
#include <stdlib.h> // for rand, srand, RAND_MAX |
| 44 |
|
| 45 |
namespace { |
| 46 |
|
| 47 |
void write_mesh_with_transient_field_data(const std::string & fileName, |
| 48 |
const std::vector<double> & transientTimeSteps, |
| 49 |
const std::string & transientFieldName) |
| 50 |
{ |
| 51 |
std::string globalVariableName = "global_variable"; |
| 52 |
stk::unit_test_util::GeneratedMeshToFileWithTransientFields gMesh(MPI_COMM_WORLD, |
| 53 |
stk::mesh::BulkData::AUTO_AURA, |
| 54 |
transientFieldName, |
| 55 |
stk::topology::NODE_RANK); |
| 56 |
gMesh.setup_mesh("1x1x8", fileName); |
| 57 |
gMesh.write_mesh_with_field(transientTimeSteps, |
| 58 |
stk::unit_test_util::IdAndTimeFieldValueSetter(), |
| 59 |
globalVariableName); |
| 60 |
} |
| 61 |
|
| 62 |
void verify_transient_field_data(stk::unit_test_util::MeshFromFile & mesh, |
| 63 |
const std::vector<double> & transientTimeSteps, |
| 64 |
const std::string & transientFieldName) |
| 65 |
{ |
| 66 |
stk::unit_test_util::TransientVerifier verifier(MPI_COMM_WORLD); |
| 67 |
verifier.verify_time_steps(mesh, transientTimeSteps); |
| 68 |
verifier.verify_num_transient_fields(mesh, 2); |
| 69 |
verifier.verify_transient_field_names(mesh, transientFieldName); |
| 70 |
verifier.verify_transient_fields(mesh); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
TEST(StkMeshIoBroker, readTransientFieldData) { |
| 75 |
const std::string fieldDataFile = "meshWithFieldData.e"; |
| 76 |
std::vector<double> transientTimeSteps = {0.0, 1.0, 2.0}; |
| 77 |
std::string transientFieldName = "transient_field"; |
| 78 |
|
| 79 |
write_mesh_with_transient_field_data(fieldDataFile, transientTimeSteps, transientFieldName); |
| 80 |
|
| 81 |
stk::unit_test_util::MeshFromFile meshWithFieldData(MPI_COMM_WORLD); |
| 82 |
meshWithFieldData.fill_from_parallel(fieldDataFile); |
| 83 |
|
| 84 |
verify_transient_field_data(meshWithFieldData, transientTimeSteps, transientFieldName); |
| 85 |
} |
| 86 |
|
| 87 |
TEST(StkMeshIoBroker, readTransientFieldData_withCache) { |
| 88 |
const std::string fieldDataFile = "meshWithFieldData.e"; |
| 89 |
std::vector<double> transientTimeSteps = {0.0, 1.0, 2.0}; |
| 90 |
std::string transientFieldName = "transient_field"; |
| 91 |
|
| 92 |
write_mesh_with_transient_field_data(fieldDataFile, transientTimeSteps, transientFieldName); |
| 93 |
|
| 94 |
stk::unit_test_util::MeshFromFile meshWithFieldData(MPI_COMM_WORLD); |
| 95 |
meshWithFieldData.broker.cache_entity_list_for_transient_steps(true); |
| 96 |
meshWithFieldData.fill_from_parallel(fieldDataFile); |
| 97 |
|
| 98 |
verify_transient_field_data(meshWithFieldData, transientTimeSteps, transientFieldName); |
| 99 |
} |
| 100 |
|
| 101 |
TEST(StkMeshIoBroker, missingInputField) { |
| 102 |
const std::string fieldDataFile = "meshWithMissingFieldData.e"; |
| 103 |
std::vector<double> transientTimeSteps = {0.0, 1.0, 2.0}; |
| 104 |
std::string transientFieldName = "transient_field"; |
| 105 |
|
| 106 |
write_mesh_with_transient_field_data(fieldDataFile, transientTimeSteps, transientFieldName); |
| 107 |
|
| 108 |
std::unique_ptr<stk::mesh::BulkData> bulk = stk::mesh::MeshBuilder(MPI_COMM_WORLD).set_spatial_dimension(3).create(); |
| 109 |
stk::mesh::MetaData& meta = bulk->mesh_meta_data(); |
| 110 |
|
| 111 |
const stk::mesh::EntityRank rank = stk::topology::NODE_RANK; |
| 112 |
const std::string fieldName = transientFieldName+"_scalar"; |
| 113 |
const std::string dbFieldName = fieldName + "_missingField"; |
| 114 |
|
| 115 |
stk::mesh::Field<double> &scalarField = meta.declare_field<double>(rank, fieldName, 1); |
| 116 |
stk::mesh::put_field_on_mesh(scalarField, meta.universal_part(), nullptr); |
| 117 |
|
| 118 |
stk::io::MeshField meshField(&scalarField, dbFieldName); |
| 119 |
stk::io::StkMeshIoBroker broker(MPI_COMM_WORLD); |
| 120 |
|
| 121 |
broker.set_throw_on_missing_input_fields(true); |
| 122 |
broker.set_bulk_data(*bulk); |
| 123 |
broker.add_mesh_database(fieldDataFile, stk::io::READ_MESH); |
| 124 |
broker.create_input_mesh(); |
| 125 |
broker.add_input_field(meshField); |
| 126 |
broker.populate_bulk_data(); |
| 127 |
|
| 128 |
std::vector<stk::io::MeshField> missingFields; |
| 129 |
EXPECT_THROW(broker.read_defined_input_fields(0.0, &missingFields), std::logic_error); |
| 130 |
|
| 131 |
unlink(fieldDataFile.c_str()); |
| 132 |
} |
| 133 |
|
| 134 |
TEST(StkMeshIoBroker, testMissingInputField) { |
| 135 |
const std::string fieldDataFile = "testMeshWithMissingFieldData.e"; |
| 136 |
std::vector<double> transientTimeSteps = {0.0, 1.0, 2.0}; |
| 137 |
std::string transientFieldName = "transient_field"; |
| 138 |
|
| 139 |
write_mesh_with_transient_field_data(fieldDataFile, transientTimeSteps, transientFieldName); |
| 140 |
|
| 141 |
std::unique_ptr<stk::mesh::BulkData> bulk = stk::mesh::MeshBuilder(MPI_COMM_WORLD).set_spatial_dimension(3).create(); |
| 142 |
stk::mesh::MetaData& meta = bulk->mesh_meta_data(); |
| 143 |
|
| 144 |
const stk::mesh::EntityRank rank = stk::topology::NODE_RANK; |
| 145 |
|
| 146 |
const std::string fieldNameBad = transientFieldName+"_scalar_bad_field"; |
| 147 |
stk::mesh::Field<double> &scalarFieldBad = meta.declare_field<double>(rank, fieldNameBad, 1); |
| 148 |
stk::mesh::put_field_on_mesh(scalarFieldBad, meta.universal_part(), nullptr); |
| 149 |
stk::io::MeshField meshFieldBad(&scalarFieldBad, fieldNameBad); |
| 150 |
|
| 151 |
const std::string fieldNameGood = transientFieldName+"_scalar"; |
| 152 |
stk::mesh::Field<double> &scalarFieldGood = meta.declare_field<double>(rank, fieldNameGood, 1); |
| 153 |
stk::mesh::put_field_on_mesh(scalarFieldGood, meta.universal_part(), nullptr); |
| 154 |
stk::io::MeshField meshFieldGood(&scalarFieldGood, fieldNameGood); |
| 155 |
|
| 156 |
stk::io::StkMeshIoBroker broker(MPI_COMM_WORLD); |
| 157 |
|
| 158 |
broker.set_bulk_data(*bulk); |
| 159 |
broker.add_mesh_database(fieldDataFile, stk::io::READ_MESH); |
| 160 |
broker.create_input_mesh(); |
| 161 |
|
| 162 |
EXPECT_FALSE(stk::io::verify_field_request(broker, meshFieldBad)); |
| 163 |
EXPECT_TRUE(stk::io::verify_field_request(broker, meshFieldGood)); |
| 164 |
|
| 165 |
broker.populate_bulk_data(); |
| 166 |
|
| 167 |
unlink(fieldDataFile.c_str()); |
| 168 |
} |
| 169 |
|
| 170 |
} |