| 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_EQ, etc |
| 36 |
#include <stk_mesh/base/BulkData.hpp> // for BulkData |
| 37 |
#include <stk_mesh/base/Field.hpp> // for Field |
| 38 |
#include <stk_mesh/base/MetaData.hpp> // for MetaData, put_field |
| 39 |
#include <stk_util/parallel/Parallel.hpp> // for ParallelMachine, etc |
| 40 |
#include <vector> // for operator!= |
| 41 |
#include "mpi.h" // for MPI_COMM_WORLD, MPI_Barrier, etc |
| 42 |
#include "stk_mesh/base/Bucket.hpp" // for Bucket |
| 43 |
#include "stk_mesh/base/Entity.hpp" // for Entity |
| 44 |
#include "stk_mesh/base/FieldBase.hpp" // for field_data |
| 45 |
#include "stk_mesh/base/FieldState.hpp" // for FieldState::StateNew, etc |
| 46 |
#include "stk_mesh/base/Types.hpp" // for PartVector, EntityId, etc |
| 47 |
#include "stk_topology/topology.hpp" // for topology, etc |
| 48 |
#include "stk_unit_test_utils/BuildMesh.hpp" |
| 49 |
namespace stk { namespace mesh { class Part; } } |
| 50 |
|
| 51 |
|
| 52 |
using stk::mesh::Entity; |
| 53 |
using stk::mesh::EntityRank; |
| 54 |
using stk::mesh::Part; |
| 55 |
using stk::mesh::Field; |
| 56 |
using stk::mesh::BulkData; |
| 57 |
using stk::mesh::EntityId; |
| 58 |
using stk::mesh::MetaData; |
| 59 |
using stk::unit_test_util::build_mesh; |
| 60 |
|
| 61 |
namespace { |
| 62 |
|
| 63 |
TEST(UnitTestFieldDataInitVal, test_scalar_field) |
| 64 |
{ |
| 65 |
// Test that if an initial-value is set on a scalar field, that value is |
| 66 |
// present the first time field-data is referenced for that field. |
| 67 |
// |
| 68 |
|
| 69 |
stk::ParallelMachine pm = MPI_COMM_WORLD; |
| 70 |
MPI_Barrier( MPI_COMM_WORLD ); |
| 71 |
|
| 72 |
// Set up meta and bulk data |
| 73 |
const unsigned spatial_dim = 2; |
| 74 |
std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatial_dim, pm); |
| 75 |
stk::mesh::MetaData& meta_data = bulkPtr->mesh_meta_data(); |
| 76 |
stk::mesh::BulkData& mesh = *bulkPtr; |
| 77 |
|
| 78 |
const unsigned num_states = 1; |
| 79 |
Field<double>& dfield = meta_data.declare_field<double>(stk::topology::NODE_RANK, "double_scalar", num_states); |
| 80 |
|
| 81 |
const double initial_value = 99.9; |
| 82 |
|
| 83 |
stk::mesh::put_field_on_mesh(dfield, meta_data.universal_part(), &initial_value); |
| 84 |
|
| 85 |
meta_data.commit(); |
| 86 |
|
| 87 |
unsigned p_rank = mesh.parallel_rank(); |
| 88 |
|
| 89 |
// Begin modification cycle so we can create stuff |
| 90 |
mesh.modification_begin(); |
| 91 |
|
| 92 |
// Node will be automatically added to the universal part |
| 93 |
stk::mesh::PartVector empty_parts; |
| 94 |
|
| 95 |
EntityId node_id = p_rank+1; |
| 96 |
// Create node |
| 97 |
Entity node = mesh.declare_node(node_id, empty_parts); |
| 98 |
|
| 99 |
mesh.modification_end(); |
| 100 |
|
| 101 |
//now insist that data for dfield on node is equal to the initial-value specified above: |
| 102 |
|
| 103 |
double* data_ptr = stk::mesh::field_data( dfield, node); |
| 104 |
|
| 105 |
ASSERT_EQ( *data_ptr, initial_value ); |
| 106 |
} |
| 107 |
|
| 108 |
TEST(UnitTestFieldDataInitVal, test_scalar_field_part_change) |
| 109 |
{ |
| 110 |
// Test that if an initial-value is set on a scalar field, that value is |
| 111 |
// present the first time field-data is referenced for that field. |
| 112 |
// |
| 113 |
|
| 114 |
stk::ParallelMachine pm = MPI_COMM_WORLD; |
| 115 |
int numProcs = stk::parallel_machine_size(pm); |
| 116 |
if (numProcs == 1) { |
| 117 |
// Set up meta and bulk data |
| 118 |
const unsigned spatial_dim = 2; |
| 119 |
std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatial_dim, pm); |
| 120 |
stk::mesh::MetaData& meta_data = bulkPtr->mesh_meta_data(); |
| 121 |
stk::mesh::BulkData& mesh = *bulkPtr; |
| 122 |
|
| 123 |
stk::mesh::Part& newPart = meta_data.declare_part("new part"); |
| 124 |
|
| 125 |
const unsigned num_states = 1; |
| 126 |
Field<double>& dfield = meta_data.declare_field<double>(stk::topology::NODE_RANK, "double_scalar", num_states); |
| 127 |
|
| 128 |
const double initial_value = 99.9; |
| 129 |
|
| 130 |
stk::mesh::put_field_on_mesh(dfield, newPart, &initial_value); |
| 131 |
|
| 132 |
meta_data.commit(); |
| 133 |
|
| 134 |
unsigned p_rank = mesh.parallel_rank(); |
| 135 |
|
| 136 |
// Begin modification cycle so we can create stuff |
| 137 |
mesh.modification_begin(); |
| 138 |
|
| 139 |
//declare a node that is not in newPart: |
| 140 |
EntityId node_id = p_rank+1; |
| 141 |
Entity node = mesh.declare_node(node_id, stk::mesh::ConstPartVector{&meta_data.universal_part()}); |
| 142 |
|
| 143 |
//declare two nodes that *are* in newPart: |
| 144 |
node_id = p_rank+2; |
| 145 |
Entity node_tmp = mesh.declare_node(node_id, stk::mesh::ConstPartVector{&newPart}); |
| 146 |
node_id = p_rank+3; |
| 147 |
Entity node_tmp2 = mesh.declare_node(node_id, stk::mesh::ConstPartVector{&newPart}); |
| 148 |
|
| 149 |
mesh.modification_end(); |
| 150 |
|
| 151 |
//zero the field-data for the nodes that have dfield: |
| 152 |
double* data_ptr = stk::mesh::field_data( dfield, node_tmp); |
| 153 |
ASSERT_TRUE( nullptr != data_ptr); |
| 154 |
*data_ptr = 0.0; |
| 155 |
data_ptr = stk::mesh::field_data( dfield, node_tmp2); |
| 156 |
ASSERT_TRUE( nullptr != data_ptr); |
| 157 |
*data_ptr = 0.0; |
| 158 |
|
| 159 |
mesh.modification_begin(); |
| 160 |
//delete one of the nodes in newPart, add the node that wasn't previously in newPart: |
| 161 |
mesh.destroy_entity(node_tmp); |
| 162 |
mesh.change_entity_parts(node, stk::mesh::ConstPartVector{&newPart}); |
| 163 |
mesh.modification_end(); |
| 164 |
|
| 165 |
//now expect that data for dfield on node matches initial value |
| 166 |
data_ptr = stk::mesh::field_data( dfield, node); |
| 167 |
|
| 168 |
ASSERT_EQ( initial_value, *data_ptr); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
TEST(UnitTestFieldDataInitVal, test_vector_field) |
| 173 |
{ |
| 174 |
// Test that if an initial-value is set on a vector field, that value is |
| 175 |
// present the first time field-data is referenced for that field. |
| 176 |
// |
| 177 |
|
| 178 |
typedef stk::mesh::Field<double> VectorField; |
| 179 |
|
| 180 |
stk::ParallelMachine pm = MPI_COMM_WORLD; |
| 181 |
MPI_Barrier( MPI_COMM_WORLD ); |
| 182 |
// Set up meta and bulk data |
| 183 |
const unsigned spatial_dim = 2; |
| 184 |
std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatial_dim, pm); |
| 185 |
stk::mesh::MetaData& meta_data = bulkPtr->mesh_meta_data(); |
| 186 |
stk::mesh::BulkData& mesh = *bulkPtr; |
| 187 |
|
| 188 |
const unsigned num_states = 1; |
| 189 |
VectorField& vfield = meta_data.declare_field<double>(stk::topology::NODE_RANK, "double_vector", num_states); |
| 190 |
|
| 191 |
const double initial_value[2] = { 50.0, 99.0 }; |
| 192 |
|
| 193 |
stk::mesh::put_field_on_mesh(vfield, meta_data.universal_part(), 2, initial_value); |
| 194 |
|
| 195 |
meta_data.commit(); |
| 196 |
|
| 197 |
unsigned p_rank = mesh.parallel_rank(); |
| 198 |
|
| 199 |
// Begin modification cycle so we can create stuff |
| 200 |
mesh.modification_begin(); |
| 201 |
|
| 202 |
// Node will be automatically added to the universal part |
| 203 |
stk::mesh::PartVector empty_parts; |
| 204 |
|
| 205 |
EntityId node_id = p_rank+1; |
| 206 |
// Create node |
| 207 |
Entity node = mesh.declare_node(node_id, empty_parts); |
| 208 |
|
| 209 |
mesh.modification_end(); |
| 210 |
|
| 211 |
//now insist that data for vfield on node is equal to the initial-value specified above: |
| 212 |
|
| 213 |
double* data_ptr = stk::mesh::field_data( vfield, node); |
| 214 |
|
| 215 |
ASSERT_EQ( data_ptr[0], initial_value[0] ); |
| 216 |
ASSERT_EQ( data_ptr[1], initial_value[1] ); |
| 217 |
} |
| 218 |
|
| 219 |
TEST(UnitTestFieldDataInitVal, test_vector_field_move_bucket) |
| 220 |
{ |
| 221 |
// Test that if an initial-value is set on a vector field, that value is |
| 222 |
// present the first time field-data is referenced for that field, and |
| 223 |
// that the value is present for a node that is moved from a bucket without |
| 224 |
// the field to a new bucket that does have the field. |
| 225 |
// |
| 226 |
|
| 227 |
typedef stk::mesh::Field<double> VectorField; |
| 228 |
|
| 229 |
stk::ParallelMachine pm = MPI_COMM_WORLD; |
| 230 |
MPI_Barrier( MPI_COMM_WORLD ); |
| 231 |
|
| 232 |
// Set up meta and bulk data |
| 233 |
const unsigned spatial_dim = 2; |
| 234 |
std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatial_dim, pm); |
| 235 |
stk::mesh::MetaData& meta_data = bulkPtr->mesh_meta_data(); |
| 236 |
stk::mesh::BulkData& mesh = *bulkPtr; |
| 237 |
|
| 238 |
const unsigned num_states = 1; |
| 239 |
VectorField& vfield = meta_data.declare_field<double>(stk::topology::NODE_RANK, "double_vector", num_states); |
| 240 |
|
| 241 |
const double initial_value[2] = { 50.0, 99.0 }; |
| 242 |
|
| 243 |
Part& node_part = meta_data.declare_part_with_topology("node_part", stk::topology::NODE); |
| 244 |
|
| 245 |
stk::mesh::put_field_on_mesh(vfield, node_part, 2, initial_value); |
| 246 |
|
| 247 |
meta_data.commit(); |
| 248 |
|
| 249 |
unsigned p_rank = mesh.parallel_rank(); |
| 250 |
|
| 251 |
// Begin modification cycle so we can create stuff |
| 252 |
mesh.modification_begin(); |
| 253 |
|
| 254 |
// Node will be automatically added to the universal part |
| 255 |
stk::mesh::PartVector empty_parts; |
| 256 |
|
| 257 |
EntityId node_id = p_rank+1; |
| 258 |
// Create node |
| 259 |
Entity node = mesh.declare_node(node_id, empty_parts); |
| 260 |
|
| 261 |
// need to copy since bucket is going to be deleted during mesh modification |
| 262 |
const stk::mesh::PartVector old_parts = mesh.bucket(node).supersets(); |
| 263 |
|
| 264 |
//Now move the node to the "node_part": |
| 265 |
stk::mesh::ConstPartVector node_part_vec; |
| 266 |
node_part_vec.push_back(&node_part); |
| 267 |
mesh.change_entity_parts(node, node_part_vec); |
| 268 |
|
| 269 |
mesh.modification_end(); |
| 270 |
|
| 271 |
const stk::mesh::PartVector& new_parts = mesh.bucket(node).supersets(); |
| 272 |
|
| 273 |
//Insist that the node is now in a different bucket: |
| 274 |
bool in_different_bucket = old_parts != new_parts; |
| 275 |
ASSERT_TRUE(in_different_bucket); |
| 276 |
|
| 277 |
//now insist that data for vfield on node is equal to the initial-value specified above: |
| 278 |
|
| 279 |
double* data_ptr = stk::mesh::field_data( vfield, node); |
| 280 |
|
| 281 |
ASSERT_EQ( data_ptr[0], initial_value[0] ); |
| 282 |
ASSERT_EQ( data_ptr[1], initial_value[1] ); |
| 283 |
} |
| 284 |
|
| 285 |
TEST(UnitTestFieldDataInitVal, test_multi_state_vector_field) |
| 286 |
{ |
| 287 |
// Test that if an initial-value is set on a multi-state vector field, that value is |
| 288 |
// present the first time field-data is referenced for that field. |
| 289 |
// |
| 290 |
|
| 291 |
typedef stk::mesh::Field<double> VectorField; |
| 292 |
|
| 293 |
stk::ParallelMachine pm = MPI_COMM_WORLD; |
| 294 |
MPI_Barrier( MPI_COMM_WORLD ); |
| 295 |
|
| 296 |
// Set up meta and bulk data |
| 297 |
const unsigned spatial_dim = 2; |
| 298 |
std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatial_dim, pm); |
| 299 |
stk::mesh::MetaData& meta_data = bulkPtr->mesh_meta_data(); |
| 300 |
stk::mesh::BulkData& mesh = *bulkPtr; |
| 301 |
|
| 302 |
const unsigned num_states = 2; |
| 303 |
VectorField& vfield = meta_data.declare_field<double>(stk::topology::NODE_RANK, "double_vector", num_states); |
| 304 |
|
| 305 |
const double initial_value[2] = { 50.0, 99.0 }; |
| 306 |
|
| 307 |
stk::mesh::put_field_on_mesh(vfield, meta_data.universal_part(), 2, initial_value); |
| 308 |
|
| 309 |
meta_data.commit(); |
| 310 |
|
| 311 |
unsigned p_rank = mesh.parallel_rank(); |
| 312 |
|
| 313 |
// Begin modification cycle so we can create stuff |
| 314 |
mesh.modification_begin(); |
| 315 |
|
| 316 |
// Node will be automatically added to the universal part |
| 317 |
stk::mesh::PartVector empty_parts; |
| 318 |
|
| 319 |
EntityId node_id = p_rank+1; |
| 320 |
// Create node |
| 321 |
Entity node = mesh.declare_node(node_id, empty_parts); |
| 322 |
|
| 323 |
mesh.modification_end(); |
| 324 |
|
| 325 |
//now insist that data for vfield on node is equal to the initial-value specified above: |
| 326 |
|
| 327 |
ASSERT_EQ( vfield.number_of_states(), num_states); |
| 328 |
|
| 329 |
VectorField& vfield_new = vfield.field_of_state(stk::mesh::StateNew); |
| 330 |
VectorField& vfield_old = vfield.field_of_state(stk::mesh::StateOld); |
| 331 |
|
| 332 |
{ |
| 333 |
double* data_ptr_new = stk::mesh::field_data( vfield_new, node); |
| 334 |
double* data_ptr_old = stk::mesh::field_data( vfield_old, node); |
| 335 |
|
| 336 |
ASSERT_EQ( data_ptr_new[0], initial_value[0] ); |
| 337 |
ASSERT_EQ( data_ptr_new[1], initial_value[1] ); |
| 338 |
|
| 339 |
ASSERT_EQ( data_ptr_old[0], initial_value[0] ); |
| 340 |
ASSERT_EQ( data_ptr_old[1], initial_value[1] ); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
} |