| 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 |
|
| 36 |
#include <gtest/gtest.h> // for AssertHelper, EXPECT_EQ, etc |
| 37 |
#include <ostream> // for basic_ostream::operator<< |
| 38 |
#include <stk_mesh/base/BulkData.hpp> // for BulkData |
| 39 |
#include <stk_mesh/base/MeshBuilder.hpp> |
| 40 |
#include <stk_mesh/base/Field.hpp> // for Field |
| 41 |
#include <stk_mesh/base/MetaData.hpp> // for MetaData, entity_rank_names, etc |
| 42 |
#include "stk_mesh/base/Entity.hpp" // for Entity |
| 43 |
#include "stk_mesh/base/FieldBase.hpp" // for field_data |
| 44 |
#include "stk_mesh/base/FieldState.hpp" // for FieldState, etc |
| 45 |
#include "stk_mesh/base/Types.hpp" // for EntityId |
| 46 |
#include "stk_topology/topology.hpp" // for topology, etc |
| 47 |
|
| 48 |
namespace { |
| 49 |
|
| 50 |
//BEGINUseMultistateField |
| 51 |
TEST(stkMeshHowTo, useMultistateField) |
| 52 |
{ |
| 53 |
const unsigned spatialDimension = 3; |
| 54 |
stk::mesh::MeshBuilder builder(MPI_COMM_WORLD); |
| 55 |
builder.set_spatial_dimension(spatialDimension); |
| 56 |
builder.set_entity_rank_names(stk::mesh::entity_rank_names()); |
| 57 |
std::shared_ptr<stk::mesh::BulkData> bulkPtr = builder.create(); |
| 58 |
stk::mesh::MetaData& metaData = bulkPtr->mesh_meta_data(); |
| 59 |
|
| 60 |
typedef stk::mesh::Field<double> ScalarField; |
| 61 |
const unsigned numStates = 2; |
| 62 |
ScalarField& temperatureFieldStateNp1 = metaData.declare_field<double>(stk::topology::NODE_RANK, "temperature", numStates); |
| 63 |
|
| 64 |
double initialTemperatureValue = 1.0; |
| 65 |
stk::mesh::put_field_on_entire_mesh_with_initial_value(temperatureFieldStateNp1, &initialTemperatureValue); |
| 66 |
|
| 67 |
metaData.commit(); |
| 68 |
stk::mesh::BulkData& mesh = *bulkPtr; |
| 69 |
mesh.modification_begin(); |
| 70 |
stk::mesh::EntityId nodeId = 1; |
| 71 |
stk::mesh::Entity node = mesh.declare_node(nodeId); |
| 72 |
mesh.modification_end(); |
| 73 |
|
| 74 |
EXPECT_EQ(stk::mesh::StateNP1, temperatureFieldStateNp1.state()); |
| 75 |
double* temperatureStateNp1 = stk::mesh::field_data(temperatureFieldStateNp1, node); |
| 76 |
EXPECT_EQ(initialTemperatureValue, *temperatureStateNp1); |
| 77 |
double newTemperatureValue = 2.0; |
| 78 |
*temperatureStateNp1 = newTemperatureValue; |
| 79 |
|
| 80 |
ScalarField& temperatureFieldStateN = temperatureFieldStateNp1.field_of_state(stk::mesh::StateN); |
| 81 |
double* temperatureStateN = stk::mesh::field_data(temperatureFieldStateN, node); |
| 82 |
EXPECT_EQ(initialTemperatureValue, *temperatureStateN); |
| 83 |
|
| 84 |
mesh.update_field_data_states(); |
| 85 |
|
| 86 |
temperatureStateN = stk::mesh::field_data(temperatureFieldStateN, node); |
| 87 |
EXPECT_EQ(newTemperatureValue, *temperatureStateN); |
| 88 |
} |
| 89 |
//ENDUseMultistateField |
| 90 |
|
| 91 |
} |