| 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, etc |
| 36 |
#include <stddef.h> // for size_t |
| 37 |
#include <unistd.h> // for unlink |
| 38 |
#include <iomanip> // for operator<< |
| 39 |
#include <stk_io/StkMeshIoBroker.hpp> // for StkMeshIoBroker |
| 40 |
#include <stk_mesh/base/Field.hpp> // for Field |
| 41 |
#include <stk_mesh/base/GetEntities.hpp> // for get_entities |
| 42 |
#include <stk_mesh/base/MetaData.hpp> // for MetaData, put_field |
| 43 |
#include <string> // for string |
| 44 |
#include <vector> // for vector |
| 45 |
#include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc |
| 46 |
#include "stk_io/MeshField.hpp" // for MeshField, etc |
| 47 |
#include "stk_mesh/base/Entity.hpp" // for Entity |
| 48 |
#include "stk_mesh/base/FieldBase.hpp" // for field_data |
| 49 |
#include "stk_mesh/base/FieldBLAS.hpp" |
| 50 |
#include "stk_mesh/base/ForEachEntity.hpp" |
| 51 |
#include "stk_topology/topology.hpp" // for topology, etc |
| 52 |
#include "stk_util/parallel/Parallel.hpp" |
| 53 |
|
| 54 |
namespace { |
| 55 |
|
| 56 |
TEST(StkMeshIoBrokerHowTo, readInitialConditionSpecifiedTime) |
| 57 |
{ |
| 58 |
std::string ic_name = "input_field_example.e"; |
| 59 |
MPI_Comm communicator = MPI_COMM_WORLD; |
| 60 |
int numProcs = stk::parallel_machine_size(communicator); |
| 61 |
if (numProcs != 1) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
{ |
| 66 |
// ============================================================ |
| 67 |
//+ INITIALIZATION: |
| 68 |
//+ Create a mesh with the 2 nodal fields "temp" and "flux" |
| 69 |
//+ for 3 timesteps. |
| 70 |
//+ The value of the fields at each node is 0.0 at time 0.0, |
| 71 |
//+ 1.0 at time 1.0, and 2.0 at time 2.0 |
| 72 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 73 |
|
| 74 |
const std::string generatedFileName = "generated:8x8x8|nodeset:xyz"; |
| 75 |
stkIo.add_mesh_database(generatedFileName, stk::io::READ_MESH); |
| 76 |
stkIo.create_input_mesh(); |
| 77 |
|
| 78 |
stk::mesh::Field<double> &temperature = |
| 79 |
stkIo.meta_data().declare_field<double>(stk::topology::NODE_RANK, "temperature", 1); |
| 80 |
stk::mesh::put_field_on_mesh(temperature, stkIo.meta_data().universal_part(), nullptr); |
| 81 |
|
| 82 |
stk::mesh::Field<double> &heat_flux = |
| 83 |
stkIo.meta_data().declare_field<double>(stk::topology::NODE_RANK, "heat_flux", 1); |
| 84 |
stk::mesh::put_field_on_mesh(heat_flux, stkIo.meta_data().universal_part(), nullptr); |
| 85 |
stkIo.populate_bulk_data(); |
| 86 |
|
| 87 |
size_t fh = stkIo.create_output_mesh(ic_name, stk::io::WRITE_RESULTS); |
| 88 |
|
| 89 |
stkIo.add_field(fh, temperature, "temp"); |
| 90 |
stkIo.add_field(fh, heat_flux, "flux"); |
| 91 |
|
| 92 |
// Add three steps to the database |
| 93 |
// For each step, the value of the field is the value 'time' |
| 94 |
for (size_t i=0; i < 3; i++) { |
| 95 |
double time = i; |
| 96 |
|
| 97 |
stk::mesh::field_fill(time, temperature); |
| 98 |
stk::mesh::field_fill(time, heat_flux); |
| 99 |
|
| 100 |
stkIo.begin_output_step(fh, time); |
| 101 |
stkIo.write_defined_output_fields(fh); |
| 102 |
stkIo.end_output_step(fh); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
{ |
| 107 |
//-BEGIN |
| 108 |
// ============================================================ |
| 109 |
//+ EXAMPLE: |
| 110 |
//+ Register the reading of database fields "temp" and "flux" to |
| 111 |
//+ populate the stk nodal fields "temperature" and "heat_flux" |
| 112 |
//+ for use as initial conditionss. |
| 113 |
//+ Specify that the "temp" field should be read from database |
| 114 |
//+ time 2.0 no matter what time is specified in the read_defined_input_fields |
| 115 |
//+ call. |
| 116 |
//+ The "flux" field will be read at the database time corresponding |
| 117 |
//+ to the analysis time passed in to read_defined_input_fields. |
| 118 |
|
| 119 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 120 |
size_t index = stkIo.add_mesh_database(ic_name, stk::io::READ_MESH); |
| 121 |
stkIo.set_active_mesh(index); |
| 122 |
stkIo.create_input_mesh(); |
| 123 |
|
| 124 |
stk::mesh::Field<double> &temperature = |
| 125 |
stkIo.meta_data().declare_field<double>(stk::topology::NODE_RANK, "temperature", 1); |
| 126 |
stk::mesh::put_field_on_mesh(temperature, stkIo.meta_data().universal_part(), nullptr); |
| 127 |
|
| 128 |
stk::mesh::Field<double> &heat_flux = |
| 129 |
stkIo.meta_data().declare_field<double>(stk::topology::NODE_RANK, "heat_flux", 1); |
| 130 |
stk::mesh::put_field_on_mesh(heat_flux, stkIo.meta_data().universal_part(), nullptr); |
| 131 |
stkIo.populate_bulk_data(); |
| 132 |
|
| 133 |
// The name of the field on the database is "temp" |
| 134 |
stk::io::MeshField temp_field(temperature, "temp", stk::io::MeshField::CLOSEST); |
| 135 |
temp_field.set_read_time(2.0);/*@\label{io:input:specifiedtime}*/ |
| 136 |
stkIo.add_input_field(temp_field); |
| 137 |
|
| 138 |
// The name of the field on the database is "flux" |
| 139 |
stk::io::MeshField flux_field(heat_flux, "flux", stk::io::MeshField::CLOSEST); |
| 140 |
stkIo.add_input_field(flux_field); |
| 141 |
|
| 142 |
//+ Read the field values from the database at time 1.0 |
| 143 |
//+ The value of "flux" will be the values from database time 1.0 |
| 144 |
//+ However, the value of "temp" will be the values from database time 2.0 |
| 145 |
stkIo.read_defined_input_fields(1.0); |
| 146 |
|
| 147 |
// ============================================================ |
| 148 |
//+ VERIFICATION |
| 149 |
stk::mesh::for_each_entity_run(stkIo.bulk_data(), stk::topology::NODE_RANK, |
| 150 |
[&](const stk::mesh::BulkData& /*bulk*/, stk::mesh::Entity node) { |
| 151 |
//+ The value of the "temperature" field at all nodes should be 2.0 |
| 152 |
double *fieldDataForNode = stk::mesh::field_data(temperature, node); |
| 153 |
EXPECT_DOUBLE_EQ(2.0, *fieldDataForNode); |
| 154 |
|
| 155 |
//+ The value of the "heat_flux" field at all nodes should be 1.0 |
| 156 |
fieldDataForNode = stk::mesh::field_data(heat_flux, node); |
| 157 |
EXPECT_DOUBLE_EQ(1.0, *fieldDataForNode); |
| 158 |
}); |
| 159 |
//-END |
| 160 |
} |
| 161 |
// ============================================================ |
| 162 |
// Cleanup |
| 163 |
unlink(ic_name.c_str()); |
| 164 |
} |
| 165 |
} |