| 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 "Ioss_DBUsage.h" // for DatabaseUsage::READ_MODEL |
| 36 |
#include "Ioss_Field.h" // for Field, etc |
| 37 |
#include "Ioss_IOFactory.h" // for IOFactory |
| 38 |
#include "Ioss_NodeBlock.h" // for NodeBlock |
| 39 |
#include "Ioss_Property.h" // for Property |
| 40 |
#include "Ioss_Region.h" // for Region, NodeBlockContainer |
| 41 |
#include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc |
| 42 |
#include "stk_io/FillMesh.hpp" |
| 43 |
#include "stk_io/WriteMesh.hpp" |
| 44 |
#include "stk_mesh/base/MeshBuilder.hpp" |
| 45 |
#include "stk_mesh/base/Entity.hpp" // for Entity |
| 46 |
#include "stk_mesh/base/FieldBase.hpp" // for field_data |
| 47 |
#include "stk_mesh/base/FieldBLAS.hpp" |
| 48 |
#include "stk_topology/topology.hpp" // for topology, etc |
| 49 |
#include <gtest/gtest.h> // for AssertHelper, EXPECT_EQ, etc |
| 50 |
#include <ostream> // for basic_ostream::operator<< |
| 51 |
#include <stddef.h> // for size_t |
| 52 |
#include <stk_io/StkMeshIoBroker.hpp> // for StkMeshIoBroker |
| 53 |
#include <stk_mesh/base/Field.hpp> // for Field |
| 54 |
#include <stk_mesh/base/GetEntities.hpp> // for get_entities |
| 55 |
#include <stk_mesh/base/MetaData.hpp> // for MetaData, put_field |
| 56 |
#include <string> // for string |
| 57 |
#include <unistd.h> // for unlink |
| 58 |
#include <vector> // for vector |
| 59 |
namespace Ioss { class DatabaseIO; } |
| 60 |
|
| 61 |
namespace { |
| 62 |
|
| 63 |
TEST(StkMeshIoBrokerHowTo, writeResults) |
| 64 |
{ |
| 65 |
std::string mesh_name = "input_mesh_example.e"; |
| 66 |
std::string results_name = "output.results"; |
| 67 |
MPI_Comm communicator = MPI_COMM_WORLD; |
| 68 |
|
| 69 |
{ |
| 70 |
// ============================================================ |
| 71 |
//+ INITIALIZATION: |
| 72 |
//+ Create a basic mesh with a hex block, 3 shell blocks, 3 nodesets, and 3 sidesets. |
| 73 |
std::unique_ptr<stk::mesh::BulkData> mesh = stk::mesh::MeshBuilder(communicator).create(); |
| 74 |
|
| 75 |
const std::string generatedFileName = "generated:8x8x8|shell:xyz|nodeset:xyz|sideset:XYZ"; |
| 76 |
stk::io::fill_mesh(generatedFileName, *mesh); |
| 77 |
stk::io::write_mesh(mesh_name, *mesh); |
| 78 |
} |
| 79 |
|
| 80 |
{ |
| 81 |
//-BEGIN |
| 82 |
// ============================================================ |
| 83 |
//+ EXAMPLE: |
| 84 |
//+ Read mesh data from the specified file. |
| 85 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 86 |
stkIo.add_mesh_database(mesh_name, stk::io::READ_MESH); |
| 87 |
|
| 88 |
//+ Creates meta data; creates parts |
| 89 |
stkIo.create_input_mesh(); |
| 90 |
|
| 91 |
//+ Declare a field |
| 92 |
//+ NOTE: Fields must be declared before "populate_bulk_data()" is called |
| 93 |
//+ since it commits the meta data. |
| 94 |
const std::string fieldName = "disp"; |
| 95 |
stk::mesh::Field<double> &field = stkIo.meta_data().declare_field<double>(stk::topology::NODE_RANK, fieldName, 1); |
| 96 |
stk::mesh::put_field_on_mesh(field, stkIo.meta_data().universal_part(), nullptr); |
| 97 |
|
| 98 |
//+ commit the meta data and create the bulk data. |
| 99 |
//+ populate the bulk data with data from the mesh file. |
| 100 |
stkIo.populate_bulk_data(); |
| 101 |
|
| 102 |
// ============================================================ |
| 103 |
//+ Create results file. By default, all parts created from the input |
| 104 |
//+ mesh will be written to the results output file. |
| 105 |
size_t fh = stkIo.create_output_mesh(results_name, stk::io::WRITE_RESULTS); |
| 106 |
|
| 107 |
//+ The field will be output to the results file with the default field name. |
| 108 |
stkIo.add_field(fh, field); /*@\label{io:results:add_field}*/ |
| 109 |
|
| 110 |
// Iterate the application's execute loop five times and output |
| 111 |
// field data each iteration (mimicing time steps). |
| 112 |
for (int step=0; step < 5; step++) { |
| 113 |
double time = step; |
| 114 |
|
| 115 |
//simulate time-varying result field... |
| 116 |
double value = 10.0 * time; |
| 117 |
stk::mesh::field_fill(value, field); |
| 118 |
|
| 119 |
//+ Output the field data calculated by the application. |
| 120 |
stkIo.begin_output_step(fh, time); |
| 121 |
stkIo.write_defined_output_fields(fh); |
| 122 |
stkIo.end_output_step(fh); |
| 123 |
} |
| 124 |
//-END |
| 125 |
} |
| 126 |
// ============================================================ |
| 127 |
//+ VERIFICATION |
| 128 |
{ |
| 129 |
Ioss::DatabaseIO *resultsDb = Ioss::IOFactory::create("exodus", results_name, |
| 130 |
Ioss::READ_MODEL, communicator); |
| 131 |
Ioss::Region results(resultsDb); |
| 132 |
// Should be 5 steps on database... |
| 133 |
EXPECT_EQ(results.get_property("state_count").get_int(), 5); |
| 134 |
// Should be 1 nodal field on database named "disp"; |
| 135 |
Ioss::NodeBlock *nb = results.get_node_blocks()[0]; |
| 136 |
EXPECT_EQ(1u, nb->field_count(Ioss::Field::TRANSIENT)); |
| 137 |
EXPECT_TRUE(nb->field_exists("disp")); |
| 138 |
|
| 139 |
// Iterate each step and verify that the correct data was written. |
| 140 |
for (size_t step=0; step < 5; step++) { |
| 141 |
double time = step; |
| 142 |
|
| 143 |
double db_time = results.begin_state(step+1); |
| 144 |
EXPECT_EQ(time, db_time); |
| 145 |
|
| 146 |
std::vector<double> field_data; |
| 147 |
nb->get_field_data("disp", field_data); |
| 148 |
const double expectedValue = 10.0 * time; |
| 149 |
for (size_t node = 0; node < field_data.size(); node++) { |
| 150 |
EXPECT_EQ(field_data[node], expectedValue); |
| 151 |
} |
| 152 |
results.end_state(step+1); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
// ============================================================ |
| 157 |
// Cleanup |
| 158 |
unlink(mesh_name.c_str()); |
| 159 |
unlink(results_name.c_str()); |
| 160 |
} |
| 161 |
|
| 162 |
} |