| 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_EQ, etc |
| 36 |
#include <stddef.h> // for size_t |
| 37 |
#include <unistd.h> // for unlink |
| 38 |
#include <map> // for _Rb_tree_const_iterator, etc |
| 39 |
#include <ostream> // for basic_ostream::operator<< |
| 40 |
#include <stk_io/StkMeshIoBroker.hpp> // for StkMeshIoBroker, etc |
| 41 |
#include <stk_util/util/ParameterList.hpp> // for ParameterList, etc |
| 42 |
#include <stk_util/parallel/Parallel.hpp> |
| 43 |
#include <string> // for string |
| 44 |
#include <utility> // for pair |
| 45 |
#include <vector> // for vector |
| 46 |
#include "Ioss_DBUsage.h" // for DatabaseUsage::READ_MODEL |
| 47 |
#include "Ioss_ElementTopology.h" // for NameList |
| 48 |
#include "Ioss_Field.h" // for Field, etc |
| 49 |
#include "Ioss_IOFactory.h" // for IOFactory |
| 50 |
#include "Ioss_Property.h" // for Property |
| 51 |
#include "Ioss_Region.h" // for Region |
| 52 |
|
| 53 |
#include <stk_mesh/base/MetaData.hpp> |
| 54 |
#include <stk_mesh/base/BulkData.hpp> |
| 55 |
#include <stk_mesh/base/MeshBuilder.hpp> |
| 56 |
#include <stk_mesh/base/Field.hpp> |
| 57 |
#include <stk_mesh/base/FieldBase.hpp> |
| 58 |
#include <stk_io/FillMesh.hpp> |
| 59 |
#include <stk_io/WriteMesh.hpp> |
| 60 |
#include <stk_mesh/base/GetEntities.hpp> |
| 61 |
|
| 62 |
namespace Ioss { class DatabaseIO; } |
| 63 |
|
| 64 |
namespace |
| 65 |
{ |
| 66 |
|
| 67 |
TEST(StkMeshIoBrokerHowTo, writeHistory) |
| 68 |
{ |
| 69 |
|
| 70 |
const std::string file_name = "History.e"; |
| 71 |
MPI_Comm communicator = MPI_COMM_WORLD; |
| 72 |
int numProcs = stk::parallel_machine_size(communicator); |
| 73 |
if (numProcs != 1) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
stk::util::ParameterList params; |
| 78 |
|
| 79 |
{ |
| 80 |
// ======================================================================== |
| 81 |
// INITIALIZATION... |
| 82 |
// Add some params to write and read... |
| 83 |
params.set_param("PI", 3.14159); // Double |
| 84 |
params.set_param("Answer", 42); // Integer |
| 85 |
|
| 86 |
std::vector<double> my_vector; |
| 87 |
my_vector.push_back(2.78); |
| 88 |
my_vector.push_back(5.30); |
| 89 |
my_vector.push_back(6.21); |
| 90 |
params.set_param("some_doubles", my_vector); // Vector of doubles... |
| 91 |
|
| 92 |
std::vector<int> ages; |
| 93 |
ages.push_back(55); |
| 94 |
ages.push_back(49); |
| 95 |
ages.push_back(21); |
| 96 |
ages.push_back(19); |
| 97 |
|
| 98 |
params.set_param("Ages", ages); // Vector of integers... |
| 99 |
} |
| 100 |
|
| 101 |
{ |
| 102 |
// ======================================================================== |
| 103 |
// EXAMPLE USAGE... |
| 104 |
// Begin use of stk io history file... |
| 105 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 106 |
|
| 107 |
//-BEGIN |
| 108 |
//+ Define the heartbeat output and the format (BINARY) |
| 109 |
size_t hb = stkIo.add_heartbeat_output(file_name, stk::io::BINARY); |
| 110 |
//-END |
| 111 |
|
| 112 |
stk::util::ParameterMapType::const_iterator i = params.begin(); |
| 113 |
stk::util::ParameterMapType::const_iterator iend = params.end(); |
| 114 |
for (; i != iend; ++i) { |
| 115 |
const std::string parameterName = (*i).first; |
| 116 |
stk::util::Parameter ¶meter = params.get_param(parameterName); |
| 117 |
|
| 118 |
// Tell history database which global variables should be output at each step... |
| 119 |
stkIo.add_heartbeat_global(hb, parameterName, parameter); |
| 120 |
} |
| 121 |
|
| 122 |
// Now output the global variables... |
| 123 |
int timestep_count = 2; |
| 124 |
double time = 0.0; |
| 125 |
for (int step=1; step <= timestep_count; step++) { |
| 126 |
time = step; |
| 127 |
stkIo.process_heartbeat_output(hb, step, time); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
{ |
| 132 |
// ======================================================================== |
| 133 |
// VERIFICATION: |
| 134 |
Ioss::DatabaseIO *iossDb = Ioss::IOFactory::create("exodus", file_name, Ioss::READ_MODEL, communicator); |
| 135 |
Ioss::Region region(iossDb); |
| 136 |
|
| 137 |
// Number steps |
| 138 |
EXPECT_EQ(region.get_property("state_count").get_int(), 2); |
| 139 |
region.begin_state(2); |
| 140 |
|
| 141 |
Ioss::NameList fields; |
| 142 |
region.field_describe(Ioss::Field::REDUCTION, &fields); |
| 143 |
EXPECT_EQ(fields.size(), 4u); |
| 144 |
|
| 145 |
std::vector<double> values; |
| 146 |
region.get_field_data("PI", values); |
| 147 |
EXPECT_NEAR(values[0], 3.14159, 1.0e-6); |
| 148 |
EXPECT_EQ(1u, values.size()); |
| 149 |
|
| 150 |
std::vector<int> ages; |
| 151 |
region.get_field_data("Ages", ages); |
| 152 |
EXPECT_EQ(ages.size(), 4u); |
| 153 |
EXPECT_EQ(ages[0], 55); |
| 154 |
EXPECT_EQ(ages[1], 49); |
| 155 |
EXPECT_EQ(ages[2], 21); |
| 156 |
EXPECT_EQ(ages[3], 19); |
| 157 |
} |
| 158 |
|
| 159 |
// ======================================================================== |
| 160 |
// CLEANUP: |
| 161 |
unlink(file_name.c_str()); |
| 162 |
} |
| 163 |
|
| 164 |
std::string getElementFieldName() |
| 165 |
{ |
| 166 |
return "SomeElementField"; |
| 167 |
} |
| 168 |
|
| 169 |
std::string getNodalFieldName() |
| 170 |
{ |
| 171 |
return "SomeNodalField"; |
| 172 |
} |
| 173 |
|
| 174 |
double initialValue() |
| 175 |
{ |
| 176 |
return -1.0; |
| 177 |
} |
| 178 |
|
| 179 |
void setUpMeshWithFieldOnBlock1(stk::mesh::BulkData& bulk, stk::mesh::Field<double>& field1, stk::mesh::Field<double>& field2) |
| 180 |
{ |
| 181 |
stk::io::StkMeshIoBroker stkIo(bulk.parallel()); |
| 182 |
stkIo.set_bulk_data(bulk); |
| 183 |
stkIo.add_mesh_database("generated:1x1x2", stk::io::READ_MESH); |
| 184 |
stkIo.create_input_mesh(); |
| 185 |
|
| 186 |
double init = initialValue(); |
| 187 |
stk::mesh::Part* block1 = bulk.mesh_meta_data().get_part("block_1"); |
| 188 |
stk::mesh::put_field_on_mesh(field1, *block1, &init); |
| 189 |
stk::mesh::put_field_on_mesh(field2, *block1, &init); |
| 190 |
|
| 191 |
stkIo.add_all_mesh_fields_as_input_fields(); |
| 192 |
stkIo.populate_bulk_data(); |
| 193 |
} |
| 194 |
|
| 195 |
void putEntityIntoHistoryPart(stk::mesh::BulkData& bulk, stk::mesh::EntityRank rank, stk::mesh::EntityId id, stk::mesh::Part& historyPart) |
| 196 |
{ |
| 197 |
stk::mesh::Entity entity = bulk.get_entity(rank, id); |
| 198 |
bulk.modification_begin(); |
| 199 |
if(bulk.is_valid(entity) && bulk.bucket(entity).owned()) |
| 200 |
{ |
| 201 |
bulk.change_entity_parts(entity, stk::mesh::ConstPartVector{&historyPart}); |
| 202 |
} |
| 203 |
bulk.modification_end(); |
| 204 |
} |
| 205 |
|
| 206 |
double getValue(stk::mesh::EntityId id, int stepNum) |
| 207 |
{ |
| 208 |
return 100*stepNum + id; |
| 209 |
} |
| 210 |
|
| 211 |
void writeHistoryFile(const std::string& historyFilename, stk::mesh::BulkData& bulk, |
| 212 |
stk::mesh::Field<double>& elementField, stk::mesh::Field<double>& nodalField, |
| 213 |
stk::mesh::Part& elementHistoryPart, stk::mesh::Part& nodeHistoryPart) |
| 214 |
{ |
| 215 |
stk::mesh::Selector subset = elementHistoryPart | nodeHistoryPart; |
| 216 |
stk::io::StkMeshIoBroker outStkIo; |
| 217 |
outStkIo.set_bulk_data(bulk); |
| 218 |
|
| 219 |
size_t outputFileIndex = outStkIo.create_output_mesh(historyFilename, stk::io::WRITE_RESULTS); |
| 220 |
outStkIo.set_subset_selector(outputFileIndex, subset); |
| 221 |
outStkIo.add_field(outputFileIndex, elementField); |
| 222 |
outStkIo.add_field(outputFileIndex, nodalField); |
| 223 |
outStkIo.write_output_mesh(outputFileIndex); |
| 224 |
|
| 225 |
stk::mesh::EntityVector elementEntities; |
| 226 |
stk::mesh::get_entities(bulk, stk::topology::ELEM_RANK, elementHistoryPart & bulk.mesh_meta_data().locally_owned_part(), elementEntities); |
| 227 |
|
| 228 |
stk::mesh::EntityVector nodeEntities; |
| 229 |
stk::mesh::get_entities(bulk, stk::topology::NODE_RANK, nodeHistoryPart & bulk.mesh_meta_data().locally_owned_part(), nodeEntities); |
| 230 |
|
| 231 |
for(int step = 1; step < 10; step++) |
| 232 |
{ |
| 233 |
for(stk::mesh::Entity entity : elementEntities) |
| 234 |
{ |
| 235 |
double *data = stk::mesh::field_data(elementField, entity); |
| 236 |
*data = getValue(bulk.identifier(entity), step); |
| 237 |
} |
| 238 |
|
| 239 |
for(stk::mesh::Entity entity : nodeEntities) |
| 240 |
{ |
| 241 |
double *data = stk::mesh::field_data(nodalField, entity); |
| 242 |
*data = getValue(bulk.identifier(entity), step); |
| 243 |
} |
| 244 |
|
| 245 |
double time = 0.4*step; |
| 246 |
outStkIo.begin_output_step(outputFileIndex, time); |
| 247 |
outStkIo.write_defined_output_fields(outputFileIndex); |
| 248 |
outStkIo.end_output_step(outputFileIndex); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
void verify_data(stk::mesh::BulkData& bulk, stk::mesh::Field<double>& field, stk::mesh::EntityRank rank, int stepNum) |
| 253 |
{ |
| 254 |
stk::mesh::EntityVector entities; |
| 255 |
stk::mesh::get_entities(bulk, rank, bulk.mesh_meta_data().locally_owned_part(), entities); |
| 256 |
|
| 257 |
for(stk::mesh::Entity entity : entities) |
| 258 |
{ |
| 259 |
double *data = stk::mesh::field_data(field, entity); |
| 260 |
double goldValue = getValue(bulk.identifier(entity), stepNum); |
| 261 |
if(*data != initialValue()) |
| 262 |
{ |
| 263 |
EXPECT_EQ(goldValue, *data); |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
void verifyHistoryFileOutput(const std::string& filename) |
| 269 |
{ |
| 270 |
std::shared_ptr<stk::mesh::BulkData> bulk = stk::mesh::MeshBuilder(MPI_COMM_WORLD).create(); |
| 271 |
stk::mesh::MetaData& meta = bulk->mesh_meta_data(); |
| 272 |
|
| 273 |
int numSteps = 0; |
| 274 |
double maxTime = 0; |
| 275 |
|
| 276 |
stk::io::fill_mesh_save_step_info(filename, *bulk, numSteps, maxTime); |
| 277 |
|
| 278 |
stk::mesh::Field<double>* field1 = static_cast<stk::mesh::Field<double>*>(meta.get_field(stk::topology::ELEM_RANK, getElementFieldName())); |
| 279 |
ASSERT_TRUE(field1!=nullptr); |
| 280 |
verify_data(*bulk, *field1, stk::topology::ELEM_RANK, numSteps); |
| 281 |
|
| 282 |
stk::mesh::Field<double>* field2 = static_cast<stk::mesh::Field<double>*>(meta.get_field(stk::topology::NODE_RANK, getNodalFieldName())); |
| 283 |
ASSERT_TRUE(field2!=nullptr); |
| 284 |
verify_data(*bulk, *field2, stk::topology::NODE_RANK, numSteps); |
| 285 |
} |
| 286 |
|
| 287 |
TEST(StkMeshIoBrokerHowTo, writeHistoryOfElementAndNode) |
| 288 |
{ |
| 289 |
stk::ParallelMachine comm = MPI_COMM_WORLD; |
| 290 |
if(stk::parallel_machine_size(comm)<=2) |
| 291 |
{ |
| 292 |
stk::mesh::EntityId elementId = 1; |
| 293 |
stk::mesh::EntityId nodeId = 12; |
| 294 |
|
| 295 |
stk::mesh::MeshBuilder builder(MPI_COMM_WORLD); |
| 296 |
builder.set_spatial_dimension(3); |
| 297 |
std::shared_ptr<stk::mesh::BulkData> bulk = builder.create(); |
| 298 |
stk::mesh::MetaData& meta = bulk->mesh_meta_data(); |
| 299 |
|
| 300 |
stk::mesh::Field<double>& elemField = meta.declare_field<double>(stk::topology::ELEM_RANK, getElementFieldName()); |
| 301 |
stk::mesh::Field<double>& nodalField = meta.declare_field<double>(stk::topology::NODE_RANK, getNodalFieldName()); |
| 302 |
|
| 303 |
stk::mesh::Part& elementHistoryPart = meta.declare_part("ElementHistoryOutput", stk::topology::ELEM_RANK); |
| 304 |
stk::mesh::Part& nodeHistoryPart = meta.declare_part("NodeHistoryOutput", stk::topology::NODE_RANK); |
| 305 |
|
| 306 |
|
| 307 |
setUpMeshWithFieldOnBlock1(*bulk, elemField, nodalField); |
| 308 |
putEntityIntoHistoryPart(*bulk, stk::topology::ELEM_RANK, elementId, elementHistoryPart); |
| 309 |
putEntityIntoHistoryPart(*bulk, stk::topology::NODE_RANK, nodeId, nodeHistoryPart); |
| 310 |
|
| 311 |
const std::string historyFilename = "history.e"; |
| 312 |
writeHistoryFile(historyFilename, *bulk, elemField, nodalField, elementHistoryPart, nodeHistoryPart); |
| 313 |
|
| 314 |
verifyHistoryFileOutput(historyFilename); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
TEST(StkMeshIoBrokerHowTo, writeEmptyHistory) |
| 320 |
{ |
| 321 |
|
| 322 |
const std::string file_name = "EmptyHistory.e"; |
| 323 |
MPI_Comm communicator = MPI_COMM_WORLD; |
| 324 |
int numProcs = stk::parallel_machine_size(communicator); |
| 325 |
if (numProcs != 1) { |
| 326 |
return; |
| 327 |
} |
| 328 |
|
| 329 |
stk::util::ParameterList params; |
| 330 |
|
| 331 |
{ |
| 332 |
// ======================================================================== |
| 333 |
// EXAMPLE USAGE... |
| 334 |
// Begin use of stk io history file... |
| 335 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 336 |
|
| 337 |
//-BEGIN |
| 338 |
//+ Define the heartbeat output and the format (BINARY) |
| 339 |
size_t hb = stkIo.add_heartbeat_output(file_name, stk::io::BINARY); |
| 340 |
//-END |
| 341 |
|
| 342 |
stkIo.begin_define_transient_for_heartbeat(hb); |
| 343 |
|
| 344 |
stk::util::ParameterMapType::const_iterator i = params.begin(); |
| 345 |
stk::util::ParameterMapType::const_iterator iend = params.end(); |
| 346 |
for (; i != iend; ++i) { |
| 347 |
const std::string parameterName = (*i).first; |
| 348 |
stk::util::Parameter ¶meter = params.get_param(parameterName); |
| 349 |
|
| 350 |
// Tell history database which global variables should be output at each step... |
| 351 |
stkIo.add_heartbeat_global(hb, parameterName, parameter); |
| 352 |
} |
| 353 |
|
| 354 |
stkIo.end_define_transient_for_heartbeat(hb); |
| 355 |
|
| 356 |
// Now output the global variables... |
| 357 |
int timestep_count = 2; |
| 358 |
double time = 0.0; |
| 359 |
for (int step=1; step <= timestep_count; step++) { |
| 360 |
time = step; |
| 361 |
EXPECT_NO_THROW(stkIo.process_heartbeat_output(hb, step, time)); |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
// ======================================================================== |
| 366 |
// CLEANUP: |
| 367 |
unlink(file_name.c_str()); |
| 368 |
} |
| 369 |
|
| 370 |
} |