| 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_NE, etc |
| 36 |
#include <stddef.h> // for size_t, NULL |
| 37 |
#include <unistd.h> // for unlink |
| 38 |
#include <stk_io/StkMeshIoBroker.hpp> // for StkMeshIoBroker |
| 39 |
#include <stk_mesh/base/MetaData.hpp> // for MetaData |
| 40 |
#include <stk_mesh/base/GetEntities.hpp> |
| 41 |
#include <string> // for string |
| 42 |
#include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc |
| 43 |
#include "stk_util/parallel/Parallel.hpp" |
| 44 |
#include "stk_unit_test_utils/MeshFixture.hpp" |
| 45 |
#include "stk_io/WriteMesh.hpp" |
| 46 |
|
| 47 |
#include <stk_mesh/base/Field.hpp> |
| 48 |
#include <stk_unit_test_utils/BuildMesh.hpp> |
| 49 |
|
| 50 |
namespace stk { namespace mesh { class Part; } } |
| 51 |
namespace { |
| 52 |
using stk::unit_test_util::build_mesh; |
| 53 |
|
| 54 |
void compare_fields(const stk::mesh::BulkData& bulk1, |
| 55 |
const stk::mesh::BulkData& bulk2, |
| 56 |
const stk::topology::rank_t rank, |
| 57 |
const std::string& fieldName) |
| 58 |
{ |
| 59 |
const stk::mesh::MetaData &meta1 = bulk1.mesh_meta_data(); |
| 60 |
const stk::mesh::MetaData &meta2 = bulk2.mesh_meta_data(); |
| 61 |
|
| 62 |
stk::mesh::FieldBase* field1 = meta1.get_field(rank, fieldName); |
| 63 |
stk::mesh::FieldBase* field2 = meta2.get_field(rank, fieldName); |
| 64 |
|
| 65 |
ASSERT_TRUE(nullptr != field1); |
| 66 |
ASSERT_TRUE(nullptr != field2); |
| 67 |
|
| 68 |
stk::mesh::EntityVector entityList1; |
| 69 |
stk::mesh::EntityVector entityList2; |
| 70 |
|
| 71 |
const stk::mesh::Selector s1(*field1); |
| 72 |
const stk::mesh::Selector s2(*field2); |
| 73 |
|
| 74 |
stk::mesh::get_entities(bulk1, rank, s1, entityList1); |
| 75 |
stk::mesh::get_entities(bulk2, rank, s2, entityList2); |
| 76 |
|
| 77 |
ASSERT_EQ(entityList1.size(), entityList2.size()); |
| 78 |
|
| 79 |
for(unsigned i=0; i<entityList1.size(); ++i) |
| 80 |
{ |
| 81 |
stk::mesh::Entity entity1 = entityList1[i]; |
| 82 |
stk::mesh::Entity entity2 = entityList2[i]; |
| 83 |
|
| 84 |
ASSERT_EQ(bulk1.identifier(entity1), bulk2.identifier(entity2)); |
| 85 |
|
| 86 |
double* data1 = static_cast<double*>(stk::mesh::field_data(*field1, entity1) ); |
| 87 |
double* data2 = static_cast<double*>(stk::mesh::field_data(*field2, entity2) ); |
| 88 |
|
| 89 |
ASSERT_EQ(*data1, *data2); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
void verify_field_is_valid(const stk::mesh::MetaData &meta, |
| 94 |
stk::mesh::Entity entity, |
| 95 |
double *initialValue, |
| 96 |
const unsigned fieldLength, |
| 97 |
const std::string &fieldName) |
| 98 |
{ |
| 99 |
stk::topology::rank_t rank = meta.mesh_bulk_data().entity_rank(entity); |
| 100 |
stk::mesh::FieldBase* field = meta.get_field(rank, fieldName); |
| 101 |
ASSERT_TRUE(field != nullptr); |
| 102 |
const double* fieldDataForEntity = static_cast<double*>(stk::mesh::field_data(*field, entity) ); |
| 103 |
ASSERT_EQ(fieldLength, stk::mesh::field_scalars_per_entity(*field, entity)); |
| 104 |
ASSERT_EQ(initialValue[0], fieldDataForEntity[0]); |
| 105 |
} |
| 106 |
|
| 107 |
void verify_entity_from_file(stk::mesh::BulkData& input_bulk, stk::mesh::Entity input_entity, stk::mesh::BulkData &bulk, const std::string& bulkEntitySetName) |
| 108 |
{ |
| 109 |
stk::mesh::Part* bulkEntitySetPart = bulk.mesh_meta_data().get_part(bulkEntitySetName); |
| 110 |
ASSERT_TRUE(bulkEntitySetPart != nullptr); |
| 111 |
|
| 112 |
stk::topology::rank_t rank = input_bulk.entity_rank(input_entity); |
| 113 |
stk::mesh::EntityVector entities; |
| 114 |
stk::mesh::get_entities(bulk, rank, *bulkEntitySetPart, entities); |
| 115 |
ASSERT_EQ(1u, entities.size()); |
| 116 |
EXPECT_EQ(input_bulk.identifier(input_entity), bulk.identifier(entities[0])); |
| 117 |
} |
| 118 |
|
| 119 |
void verify_write_and_read_mesh(stk::mesh::BulkData& input_bulk, const std::string &filename, stk::mesh::BulkData &bulkFromFile) |
| 120 |
{ |
| 121 |
int step = 1; |
| 122 |
double time = 1.0; |
| 123 |
stk::io::write_mesh_with_fields(filename, input_bulk, step, time); |
| 124 |
|
| 125 |
int numSteps; |
| 126 |
double maxTime; |
| 127 |
stk::io::fill_mesh_save_step_info(filename, bulkFromFile, numSteps, maxTime); |
| 128 |
unlink(filename.c_str()); |
| 129 |
|
| 130 |
ASSERT_EQ(step, numSteps); |
| 131 |
ASSERT_EQ(time, maxTime); |
| 132 |
} |
| 133 |
|
| 134 |
void verify_field_exists(stk::mesh::BulkData& bulk, |
| 135 |
stk::topology::rank_t rank, |
| 136 |
const std::string& fieldName) |
| 137 |
{ |
| 138 |
stk::mesh::FieldBase* field = bulk.mesh_meta_data().get_field(rank, fieldName); |
| 139 |
ASSERT_TRUE(field != nullptr); |
| 140 |
} |
| 141 |
|
| 142 |
void verify_field_in_file(stk::mesh::BulkData& input_bulk, |
| 143 |
stk::mesh::Entity input_entity, |
| 144 |
const std::string& entitySetName, |
| 145 |
const std::string& fieldName, |
| 146 |
const std::string &filename) |
| 147 |
{ |
| 148 |
std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(input_bulk.parallel()); |
| 149 |
stk::mesh::BulkData& bulk = *bulkPtr; |
| 150 |
verify_write_and_read_mesh(input_bulk, filename, bulk); |
| 151 |
|
| 152 |
stk::topology::rank_t rank = input_bulk.entity_rank(input_entity); |
| 153 |
verify_entity_from_file(input_bulk, input_entity, bulk, entitySetName); |
| 154 |
verify_field_exists(bulk, rank, fieldName); |
| 155 |
compare_fields(input_bulk, bulk, rank, fieldName); |
| 156 |
} |
| 157 |
|
| 158 |
void verify_nodesetField_in_file(stk::mesh::BulkData& input_bulk, stk::mesh::Entity node, const std::string& nodesetName, const std::string& fieldName) |
| 159 |
{ |
| 160 |
std::string filename("nodelist.exo"); |
| 161 |
ASSERT_TRUE(input_bulk.entity_rank(node) == stk::topology::NODE_RANK); |
| 162 |
|
| 163 |
verify_field_in_file(input_bulk, node, nodesetName, fieldName, filename); |
| 164 |
} |
| 165 |
|
| 166 |
class MeshWithNodeset : public stk::unit_test_util::MeshFixture |
| 167 |
{ |
| 168 |
}; |
| 169 |
|
| 170 |
//BEGINTEST1 |
| 171 |
TEST_F(MeshWithNodeset, createAndWriteNodesetWithField) |
| 172 |
{ |
| 173 |
if (stk::parallel_machine_size(get_comm()) == 1) |
| 174 |
{ |
| 175 |
setup_empty_mesh(stk::mesh::BulkData::AUTO_AURA); |
| 176 |
std::string nodesetName("nodelist_1"); |
| 177 |
stk::mesh::Part& nodesetPart = get_meta().declare_part(nodesetName, stk::topology::NODE_RANK); |
| 178 |
|
| 179 |
const std::string fieldName = "nodesetField"; |
| 180 |
const unsigned fieldLength = 3; |
| 181 |
double initialValue[fieldLength] {0., 0., 0.}; |
| 182 |
const int numStates = 1; |
| 183 |
stk::mesh::Field<double> &newField = |
| 184 |
get_meta().declare_field<double>(stk::topology::NODE_RANK, fieldName, numStates); |
| 185 |
|
| 186 |
stk::mesh::put_field_on_mesh(newField, nodesetPart, fieldLength, initialValue); |
| 187 |
stk::io::set_field_output_type(newField, stk::io::FieldOutputType::VECTOR_3D); |
| 188 |
|
| 189 |
stk::io::fill_mesh("generated:1x1x1", get_bulk()); |
| 190 |
|
| 191 |
stk::mesh::Entity node1 = get_bulk().get_entity(stk::topology::NODE_RANK, 1); |
| 192 |
|
| 193 |
get_bulk().modification_begin(); |
| 194 |
get_bulk().change_entity_parts(node1, stk::mesh::ConstPartVector{&nodesetPart}); |
| 195 |
get_bulk().modification_end(); |
| 196 |
|
| 197 |
stk::io::put_io_part_attribute(nodesetPart); |
| 198 |
|
| 199 |
verify_field_is_valid(get_meta(), node1, initialValue, fieldLength, fieldName); |
| 200 |
verify_nodesetField_in_file(get_bulk(), node1, nodesetName, fieldName); |
| 201 |
} |
| 202 |
} |
| 203 |
//ENDTEST1 |
| 204 |
|
| 205 |
|
| 206 |
void verify_sidesetField_in_file(stk::mesh::BulkData& input_bulk, stk::mesh::Entity side, const std::string& sidesetName, const std::string& fieldName) |
| 207 |
{ |
| 208 |
std::string filename("surface.exo"); |
| 209 |
ASSERT_TRUE(input_bulk.entity_rank(side) == input_bulk.mesh_meta_data().side_rank()); |
| 210 |
|
| 211 |
verify_field_in_file(input_bulk, side, sidesetName, fieldName, filename); |
| 212 |
} |
| 213 |
|
| 214 |
class MeshWithSideset : public stk::unit_test_util::MeshFixture |
| 215 |
{ |
| 216 |
}; |
| 217 |
|
| 218 |
//BEGINTEST2 |
| 219 |
TEST_F(MeshWithSideset, createAndWriteSidesetWithField) |
| 220 |
{ |
| 221 |
if (stk::parallel_machine_size(get_comm()) == 1) |
| 222 |
{ |
| 223 |
setup_empty_mesh(stk::mesh::BulkData::AUTO_AURA); |
| 224 |
std::string sidesetName("surface_1"); |
| 225 |
stk::mesh::Part& sidesetPart = get_meta().declare_part(sidesetName, get_meta().side_rank()); |
| 226 |
|
| 227 |
const std::string fieldName = "sidesetField"; |
| 228 |
const unsigned fieldLength = 3; |
| 229 |
double initialValue[fieldLength] {1., 1., 1.}; |
| 230 |
const int numStates = 1; |
| 231 |
stk::mesh::Field<double> &newField = |
| 232 |
get_meta().declare_field<double>(get_meta().side_rank(), fieldName, numStates); |
| 233 |
|
| 234 |
stk::mesh::put_field_on_mesh(newField, sidesetPart, fieldLength, initialValue); |
| 235 |
stk::io::set_field_output_type(newField, stk::io::FieldOutputType::VECTOR_3D); |
| 236 |
|
| 237 |
stk::io::fill_mesh("generated:1x1x1", get_bulk()); |
| 238 |
|
| 239 |
stk::mesh::Entity elem1 = get_bulk().get_entity(stk::topology::ELEM_RANK, 1); |
| 240 |
unsigned sideOrdinal = 0; |
| 241 |
|
| 242 |
get_bulk().modification_begin(); |
| 243 |
stk::mesh::Entity side = get_bulk().declare_element_side(elem1, sideOrdinal, stk::mesh::PartVector{&sidesetPart}); |
| 244 |
get_bulk().modification_end(); |
| 245 |
|
| 246 |
stk::io::put_io_part_attribute(sidesetPart); |
| 247 |
|
| 248 |
verify_field_is_valid(get_meta(), side, initialValue, fieldLength, fieldName); |
| 249 |
verify_sidesetField_in_file(get_bulk(), side, sidesetName, fieldName); |
| 250 |
} |
| 251 |
} |
| 252 |
//ENDTEST2 |
| 253 |
|
| 254 |
} |