| 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 <stddef.h> // for size_t |
| 36 |
#include <stdlib.h> // for rand, srand, RAND_MAX |
| 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_mesh/base/GetEntities.hpp> // for get_selected_entities |
| 41 |
#include <stk_mesh/base/MetaData.hpp> // for MetaData |
| 42 |
#include <gtest/gtest.h> |
| 43 |
#include <string> // for string |
| 44 |
#include <vector> // for vector, etc |
| 45 |
#include "gtest/gtest.h" // for AssertHelper, ASSERT_TRUE |
| 46 |
#include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc |
| 47 |
#include "stk_mesh/base/Bucket.hpp" // for Bucket |
| 48 |
#include "stk_mesh/base/Entity.hpp" // for Entity |
| 49 |
#include "stk_mesh/base/Part.hpp" // for Part |
| 50 |
#include "stk_mesh/base/Selector.hpp" // for Selector, operator& |
| 51 |
#include "stk_mesh/base/Types.hpp" // for PartVector, BucketVector, etc |
| 52 |
#include "stk_topology/topology.hpp" // for topology, etc |
| 53 |
#include "stk_util/parallel/Parallel.hpp" // for ParallelMachine |
| 54 |
#include "stk_mesh/baseImpl/MeshImplUtils.hpp" |
| 55 |
#include "stk_mesh/base/FEMHelpers.hpp" |
| 56 |
#include "stk_mesh/base/Field.hpp" |
| 57 |
#include <stk_unit_test_utils/MeshFixture.hpp> |
| 58 |
#include <stk_unit_test_utils/BuildMesh.hpp> |
| 59 |
|
| 60 |
namespace { |
| 61 |
|
| 62 |
using stk::unit_test_util::build_mesh; |
| 63 |
using stk::unit_test_util::build_mesh_no_simple_fields; |
| 64 |
|
| 65 |
void create_one_element_mesh_with_nodeset(stk::mesh::BulkData& bulk, const std::string & /*filename*/) |
| 66 |
{ |
| 67 |
if(bulk.parallel_size() == 1) { |
| 68 |
stk::mesh::MetaData& meta = bulk.mesh_meta_data(); |
| 69 |
unsigned spatialDim = meta.spatial_dimension(); |
| 70 |
|
| 71 |
stk::mesh::Part * hexPart = &meta.declare_part_with_topology("block_1", stk::topology::HEX_8); |
| 72 |
stk::io::put_io_part_attribute(*hexPart); |
| 73 |
|
| 74 |
stk::mesh::Part * nodesetPart = &meta.declare_part_with_topology("nodelist_1", stk::topology::NODE); |
| 75 |
stk::io::put_io_part_attribute(*nodesetPart); |
| 76 |
|
| 77 |
stk::mesh::Field<double> & coordsField = meta.declare_field<double>(stk::topology::NODE_RANK, "coordinates", 1); |
| 78 |
stk::mesh::put_field_on_mesh(coordsField, meta.universal_part(), spatialDim, nullptr); |
| 79 |
|
| 80 |
stk::mesh::Field<double> & nodeSetField = meta.declare_field<double>(stk::topology::NODE_RANK, "nodeSetField", 1); |
| 81 |
stk::mesh::put_field_on_mesh(nodeSetField, *nodesetPart, 1, nullptr); |
| 82 |
|
| 83 |
meta.commit(); |
| 84 |
|
| 85 |
std::vector<stk::mesh::EntityIdVector> hexNodeIDs { |
| 86 |
{ 1, 2, 3, 4, 5, 6, 7, 8 } |
| 87 |
}; |
| 88 |
stk::mesh::EntityId hexElemIDs[] = { 1 }; |
| 89 |
|
| 90 |
// Build the base hex mesh |
| 91 |
bulk.modification_begin(); |
| 92 |
for (size_t i = 0; i < hexNodeIDs.size(); ++i) { |
| 93 |
stk::mesh::declare_element(bulk, *hexPart, hexElemIDs[i], hexNodeIDs[i]); |
| 94 |
} |
| 95 |
bulk.modification_end(); |
| 96 |
|
| 97 |
stk::mesh::Entity node_1 = bulk.get_entity(stk::topology::NODE_RANK, 1); |
| 98 |
EXPECT_TRUE(bulk.is_valid(node_1)); |
| 99 |
|
| 100 |
bulk.modification_begin(); |
| 101 |
bulk.change_entity_parts(node_1, stk::mesh::PartVector{nodesetPart}, stk::mesh::PartVector{}); |
| 102 |
|
| 103 |
stk::mesh::EntityVector nodes; |
| 104 |
stk::mesh::get_entities(bulk, stk::topology::NODE_RANK, nodes); |
| 105 |
EXPECT_EQ(8u, nodes.size()); |
| 106 |
|
| 107 |
std::vector<double> coordinates = {0,0,0, |
| 108 |
1,0,0, |
| 109 |
1,1,0, |
| 110 |
0,1,0, |
| 111 |
0,0,1, |
| 112 |
1,0,1, |
| 113 |
1,1,1, |
| 114 |
0,1,1}; |
| 115 |
|
| 116 |
for(size_t nodeIndex=0; nodeIndex < nodes.size(); nodeIndex++) |
| 117 |
{ |
| 118 |
double * nodalCoords = stk::mesh::field_data(coordsField, nodes[nodeIndex]); |
| 119 |
for(unsigned coordIndex=0; coordIndex < spatialDim; coordIndex++) |
| 120 |
nodalCoords[coordIndex] = coordinates[nodeIndex*spatialDim+coordIndex]; |
| 121 |
} |
| 122 |
|
| 123 |
stk::mesh::EntityVector elements; |
| 124 |
stk::mesh::get_selected_entities(*hexPart, bulk.buckets(stk::topology::ELEM_RANK), elements); |
| 125 |
EXPECT_EQ(1u, elements.size()); |
| 126 |
|
| 127 |
stk::mesh::Part * part = meta.get_part("nodelist_1"); |
| 128 |
EXPECT_TRUE(stk::io::is_part_io_part(*part)); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
void create_and_write_one_hex_mesh_with_nodeset(const std::string& filename) |
| 133 |
{ |
| 134 |
const unsigned spatialDim = 3; |
| 135 |
std::shared_ptr<stk::mesh::BulkData> bulk = build_mesh(spatialDim, MPI_COMM_WORLD, stk::mesh::BulkData::NO_AUTO_AURA); |
| 136 |
|
| 137 |
create_one_element_mesh_with_nodeset(*bulk, filename); |
| 138 |
|
| 139 |
stk::io::write_mesh_with_fields(filename, *bulk, 1, 1.0); |
| 140 |
} |
| 141 |
|
| 142 |
TEST(StkMeshIoBroker, readNodesetWithDistributionFactor) { |
| 143 |
if(stk::parallel_machine_size(MPI_COMM_WORLD) != 1) { return; } |
| 144 |
|
| 145 |
std::string inputFile = "readNodesetWithDistributionFactor.g"; |
| 146 |
create_and_write_one_hex_mesh_with_nodeset(inputFile); |
| 147 |
|
| 148 |
const unsigned spatialDim = 3; |
| 149 |
std::shared_ptr<stk::mesh::BulkData> bulk = build_mesh(spatialDim, MPI_COMM_WORLD, stk::mesh::BulkData::NO_AUTO_AURA); |
| 150 |
stk::mesh::MetaData& meta = bulk->mesh_meta_data(); |
| 151 |
|
| 152 |
stk::io::StkMeshIoBroker ioBroker; |
| 153 |
stk::io::fill_mesh_preexisting(ioBroker, inputFile, *bulk); |
| 154 |
|
| 155 |
stk::mesh::Part* nodeSet = meta.get_part("nodelist_1"); |
| 156 |
EXPECT_TRUE(nodeSet != nullptr); |
| 157 |
|
| 158 |
stk::mesh::FieldBase* nodeSetDf = meta.get_field(stk::topology::NODE_RANK, "distribution_factors_nodelist_1"); |
| 159 |
EXPECT_TRUE(nodeSetDf != nullptr); |
| 160 |
unlink(inputFile.c_str()); |
| 161 |
} |
| 162 |
|
| 163 |
TEST(StkMeshIoBroker, readNodesetWithoutDistributionFactor) { |
| 164 |
if(stk::parallel_machine_size(MPI_COMM_WORLD) != 1) { return; } |
| 165 |
|
| 166 |
std::string inputFile = "readNodesetWithoutDistributionFactor.g"; |
| 167 |
create_and_write_one_hex_mesh_with_nodeset(inputFile); |
| 168 |
|
| 169 |
const unsigned spatialDim = 3; |
| 170 |
std::shared_ptr<stk::mesh::BulkData> bulk = build_mesh(spatialDim, MPI_COMM_WORLD, stk::mesh::BulkData::NO_AUTO_AURA); |
| 171 |
stk::mesh::MetaData& meta = bulk->mesh_meta_data(); |
| 172 |
|
| 173 |
stk::io::StkMeshIoBroker ioBroker; |
| 174 |
ioBroker.set_auto_load_distribution_factor_per_nodeset(false); |
| 175 |
stk::io::fill_mesh_preexisting(ioBroker, inputFile, *bulk); |
| 176 |
|
| 177 |
stk::mesh::Part* nodeSet = meta.get_part("nodelist_1"); |
| 178 |
EXPECT_TRUE(nodeSet != nullptr); |
| 179 |
|
| 180 |
stk::mesh::FieldBase* nodeSetDf = meta.get_field(stk::topology::NODE_RANK, "distribution_factors_nodelist_1"); |
| 181 |
EXPECT_TRUE(nodeSetDf == nullptr); |
| 182 |
unlink(inputFile.c_str()); |
| 183 |
} |
| 184 |
|
| 185 |
void setup_field_data(const std::string& inputFile, const std::string& nodeSetFieldName, stk::mesh::BulkData& bulk) |
| 186 |
{ |
| 187 |
stk::mesh::MetaData& meta = bulk.mesh_meta_data(); |
| 188 |
create_and_write_one_hex_mesh_with_nodeset(inputFile); |
| 189 |
|
| 190 |
meta.declare_part_with_topology("nodelist_1", stk::topology::NODE); |
| 191 |
meta.declare_field<double>(stk::topology::NODE_RANK, nodeSetFieldName, 1); |
| 192 |
} |
| 193 |
|
| 194 |
void load_field_data(const std::string& inputFile, stk::mesh::BulkData& bulk, stk::io::StkMeshIoBroker& ioBroker, stk::io::MeshField& meshField) |
| 195 |
{ |
| 196 |
ioBroker.set_bulk_data(bulk); |
| 197 |
ioBroker.add_mesh_database(inputFile, stk::io::READ_MESH); |
| 198 |
ioBroker.create_input_mesh(); |
| 199 |
ioBroker.populate_bulk_data(); |
| 200 |
|
| 201 |
ioBroker.add_input_field(meshField); |
| 202 |
} |
| 203 |
|
| 204 |
void test_field_data_no_throw(const std::string& inputFile, stk::mesh::BulkData& bulk, stk::io::MeshField& meshField) |
| 205 |
{ |
| 206 |
stk::io::StkMeshIoBroker ioBroker; |
| 207 |
load_field_data(inputFile, bulk, ioBroker, meshField); |
| 208 |
|
| 209 |
EXPECT_NO_THROW(ioBroker.read_defined_input_fields(0.0)); |
| 210 |
unlink(inputFile.c_str()); |
| 211 |
} |
| 212 |
|
| 213 |
void test_field_data_throw(const std::string& inputFile, stk::mesh::BulkData& bulk, stk::io::MeshField& meshField) |
| 214 |
{ |
| 215 |
stk::io::StkMeshIoBroker ioBroker; |
| 216 |
load_field_data(inputFile, bulk, ioBroker, meshField); |
| 217 |
|
| 218 |
EXPECT_THROW(ioBroker.read_defined_input_fields(0.0), std::runtime_error); |
| 219 |
unlink(inputFile.c_str()); |
| 220 |
} |
| 221 |
|
| 222 |
TEST(StkMeshIoBroker, readSubsetFieldData) { |
| 223 |
if(stk::parallel_machine_size(MPI_COMM_WORLD) != 1) { return; } |
| 224 |
|
| 225 |
std::string inputFile = "readSubsetFieldData.g"; |
| 226 |
std::string nodeSetString = "nodeSetField"; |
| 227 |
|
| 228 |
const unsigned spatialDim = 3; |
| 229 |
std::shared_ptr<stk::mesh::BulkData> bulk = build_mesh(spatialDim, MPI_COMM_WORLD, stk::mesh::BulkData::NO_AUTO_AURA); |
| 230 |
stk::mesh::MetaData& meta = bulk->mesh_meta_data(); |
| 231 |
|
| 232 |
setup_field_data(inputFile, nodeSetString, *bulk); |
| 233 |
|
| 234 |
stk::mesh::FieldBase* nodeSetField = meta.get_field(stk::topology::NODE_RANK, nodeSetString); |
| 235 |
stk::mesh::Part* nodesetPart = meta.get_part("nodelist_1"); |
| 236 |
|
| 237 |
stk::mesh::put_field_on_mesh(*nodeSetField, *nodesetPart, 1, nullptr); |
| 238 |
|
| 239 |
stk::io::MeshField meshField(nodeSetField, nodeSetString); |
| 240 |
meshField.add_subset(*nodesetPart); |
| 241 |
|
| 242 |
test_field_data_no_throw(inputFile, *bulk, meshField); |
| 243 |
} |
| 244 |
|
| 245 |
TEST(StkMeshIoBroker, readFieldDataOnUniversalSetButNotDefinedOnSubset) { |
| 246 |
if(stk::parallel_machine_size(MPI_COMM_WORLD) != 1) { return; } |
| 247 |
|
| 248 |
std::string inputFile = "readFieldDataOnUniversalSetButNotDefinedOnSubset.g"; |
| 249 |
std::string nodeSetString = "nodeSetField"; |
| 250 |
|
| 251 |
const unsigned spatialDim = 3; |
| 252 |
std::shared_ptr<stk::mesh::BulkData> bulk = build_mesh(spatialDim, MPI_COMM_WORLD, stk::mesh::BulkData::NO_AUTO_AURA); |
| 253 |
stk::mesh::MetaData& meta = bulk->mesh_meta_data(); |
| 254 |
|
| 255 |
setup_field_data(inputFile, nodeSetString, *bulk); |
| 256 |
|
| 257 |
stk::mesh::FieldBase* nodeSetField = meta.get_field(stk::topology::NODE_RANK, nodeSetString); |
| 258 |
stk::mesh::put_field_on_mesh(*nodeSetField, meta.universal_part(), 1, nullptr); |
| 259 |
|
| 260 |
stk::io::MeshField meshField(nodeSetField, nodeSetString); |
| 261 |
|
| 262 |
test_field_data_throw(inputFile, *bulk, meshField); |
| 263 |
} |
| 264 |
|
| 265 |
TEST(StkMeshIoBroker, readFieldDataOnUniversalSetAndDefinedOnSubset) { |
| 266 |
if(stk::parallel_machine_size(MPI_COMM_WORLD) != 1) { return; } |
| 267 |
|
| 268 |
std::string inputFile = "readFieldDataOnUniversalSetAndDefinedOnSubset.g"; |
| 269 |
std::string nodeSetString = "nodeSetField"; |
| 270 |
|
| 271 |
const unsigned spatialDim = 3; |
| 272 |
std::shared_ptr<stk::mesh::BulkData> bulk = build_mesh(spatialDim, MPI_COMM_WORLD, stk::mesh::BulkData::NO_AUTO_AURA); |
| 273 |
stk::mesh::MetaData& meta = bulk->mesh_meta_data(); |
| 274 |
|
| 275 |
setup_field_data(inputFile, nodeSetString, *bulk); |
| 276 |
|
| 277 |
stk::mesh::FieldBase* nodeSetField = meta.get_field(stk::topology::NODE_RANK, nodeSetString); |
| 278 |
stk::mesh::Part* nodesetPart = meta.get_part("nodelist_1"); |
| 279 |
stk::mesh::put_field_on_mesh(*nodeSetField, meta.universal_part(), 1, nullptr); |
| 280 |
|
| 281 |
stk::io::MeshField meshField(nodeSetField, nodeSetString); |
| 282 |
meshField.add_subset(*nodesetPart); |
| 283 |
|
| 284 |
test_field_data_no_throw(inputFile, *bulk, meshField); |
| 285 |
} |
| 286 |
|
| 287 |
} |