| 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 <stddef.h> // for size_t |
| 36 |
#include <unistd.h> // for unlink |
| 37 |
#include <exception> // for exception |
| 38 |
#include <stk_io/StkMeshIoBroker.hpp> // for StkMeshIoBroker |
| 39 |
#include <stk_mesh/base/BulkData.hpp> // for BulkData |
| 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 <gtest/gtest.h> |
| 44 |
#include <string> // for string, basic_string |
| 45 |
#include <vector> // for vector |
| 46 |
#include "Ioss_DBUsage.h" // for DatabaseUsage::READ_RESTART |
| 47 |
#include "Ioss_Field.h" // for Field, etc |
| 48 |
#include "Ioss_IOFactory.h" // for IOFactory |
| 49 |
#include "Ioss_NodeBlock.h" // for NodeBlock |
| 50 |
#include "Ioss_Region.h" // for Region |
| 51 |
#include "gtest/gtest.h" // for AssertHelper, ASSERT_TRUE, etc |
| 52 |
#include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc |
| 53 |
#include "stk_mesh/base/Entity.hpp" // for Entity |
| 54 |
#include "stk_mesh/base/FieldBase.hpp" // for field_data |
| 55 |
#include "stk_topology/topology.hpp" // for topology, etc |
| 56 |
#include "stk_util/parallel/Parallel.hpp" |
| 57 |
namespace Ioss { class DatabaseIO; } |
| 58 |
|
| 59 |
namespace { |
| 60 |
|
| 61 |
Ioss::Field::BasicType iossBasicType(double) |
| 62 |
{ |
| 63 |
return Ioss::Field::REAL; |
| 64 |
} |
| 65 |
|
| 66 |
Ioss::Field::BasicType iossBasicType(int) |
| 67 |
{ |
| 68 |
return Ioss::Field::INTEGER; |
| 69 |
} |
| 70 |
|
| 71 |
template <typename DataType> |
| 72 |
void testGlobalVarOnFile(const std::string &outputFileName, const int stepNumber, const std::vector<std::string> &goldGlobalVarName, |
| 73 |
const std::vector<DataType> goldGlobalVarValue, DataType goldGlobalScale, MPI_Comm comm) |
| 74 |
{ |
| 75 |
stk::io::StkMeshIoBroker stkIo(comm); |
| 76 |
stkIo.add_mesh_database(outputFileName, stk::io::READ_MESH); |
| 77 |
stkIo.create_input_mesh(); |
| 78 |
stkIo.populate_bulk_data(); |
| 79 |
stkIo.read_defined_input_fields(stepNumber); |
| 80 |
std::vector<std::string> globalVarNames; |
| 81 |
stkIo.get_global_variable_names(globalVarNames); |
| 82 |
ASSERT_EQ(goldGlobalVarName.size(), globalVarNames.size()); |
| 83 |
for(size_t i=0; i<goldGlobalVarName.size(); i++) |
| 84 |
{ |
| 85 |
EXPECT_STRCASEEQ(goldGlobalVarName[i].c_str(), globalVarNames[i].c_str()); |
| 86 |
std::vector<DataType> globalVar; |
| 87 |
ASSERT_TRUE(stkIo.get_global(globalVarNames[i], globalVar)); |
| 88 |
for(size_t j=0; j<globalVar.size(); j++) |
| 89 |
{ |
| 90 |
EXPECT_NEAR(goldGlobalVarValue[i]+goldGlobalScale*j, globalVar[j], std::numeric_limits<double>::epsilon()); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
void testNodalFieldOnFile(const std::string &outputFileName, const int stepNumber, const std::string &goldNodalFieldName, |
| 96 |
const std::vector<double> goldNodalFieldValues, MPI_Comm comm) |
| 97 |
{ |
| 98 |
Ioss::DatabaseIO *iossDb = Ioss::IOFactory::create("exodus", outputFileName, Ioss::READ_RESTART, comm); |
| 99 |
Ioss::Region inputRegion(iossDb); |
| 100 |
Ioss::NodeBlock *nodeBlock = inputRegion.get_node_block("nodeblock_1"); |
| 101 |
ASSERT_TRUE(nodeBlock->field_exists(goldNodalFieldName)); |
| 102 |
|
| 103 |
inputRegion.begin_state(stepNumber); |
| 104 |
|
| 105 |
std::vector<double> fieldValues; |
| 106 |
nodeBlock->get_field_data(goldNodalFieldName, fieldValues); |
| 107 |
ASSERT_EQ(goldNodalFieldValues.size(), fieldValues.size()); |
| 108 |
for(size_t i=0; i<goldNodalFieldValues.size(); i++) |
| 109 |
{ |
| 110 |
EXPECT_NEAR(goldNodalFieldValues[i], fieldValues[i], std::numeric_limits<double>::epsilon()); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
TEST(GlobalVariablesTest, OneGlobalDouble) |
| 115 |
{ |
| 116 |
const std::string outputFileName = "OneGlobalDouble.exo"; |
| 117 |
// 0 1 2 3 4 5 |
| 118 |
// 12345678901234567890123456789012345678901234567890 |
| 119 |
const std::string globalVarName = "a_global_variable_with_a_longer_name_than_the_default"; |
| 120 |
const double globalVarValue = 13.0; |
| 121 |
MPI_Comm communicator = MPI_COMM_WORLD; |
| 122 |
{ |
| 123 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 124 |
stkIo.property_add(Ioss::Property("MAXIMUM_NAME_LENGTH", 64)); |
| 125 |
const std::string exodusFileName = "generated:1x1x8"; |
| 126 |
stkIo.add_mesh_database(exodusFileName, stk::io::READ_MESH); |
| 127 |
stkIo.create_input_mesh(); |
| 128 |
stkIo.populate_bulk_data(); |
| 129 |
|
| 130 |
size_t result_file_index = stkIo.create_output_mesh(outputFileName, stk::io::WRITE_RESULTS); |
| 131 |
|
| 132 |
stkIo.add_global(result_file_index, globalVarName, Ioss::Field::REAL); |
| 133 |
|
| 134 |
const double time = 1.0; |
| 135 |
stkIo.begin_output_step(result_file_index, time); |
| 136 |
|
| 137 |
stkIo.write_global(result_file_index, globalVarName, globalVarValue); |
| 138 |
|
| 139 |
stkIo.end_output_step(result_file_index); |
| 140 |
} |
| 141 |
|
| 142 |
const int stepNumber = 1; |
| 143 |
std::vector<std::string> globalVarNames(1, globalVarName); |
| 144 |
std::vector<double> globalVarValues(1,globalVarValue); |
| 145 |
double goldGlobalScaleFactor = 0.0; |
| 146 |
testGlobalVarOnFile(outputFileName, stepNumber, globalVarNames, globalVarValues, goldGlobalScaleFactor, communicator); |
| 147 |
unlink(outputFileName.c_str()); |
| 148 |
} |
| 149 |
|
| 150 |
TEST(GlobalVariablesTest, InvalidGlobalRequest) |
| 151 |
{ |
| 152 |
const std::string outputFileName = "InvalidGlobalRequest.exo"; |
| 153 |
const std::string globalVarName = "testGlobal"; |
| 154 |
const double globalVarValue = 13.0; |
| 155 |
MPI_Comm communicator = MPI_COMM_WORLD; |
| 156 |
{ |
| 157 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 158 |
const std::string exodusFileName = "generated:1x1x8"; |
| 159 |
stkIo.add_mesh_database(exodusFileName, stk::io::READ_MESH); |
| 160 |
stkIo.create_input_mesh(); |
| 161 |
stkIo.populate_bulk_data(); |
| 162 |
|
| 163 |
size_t result_file_index = stkIo.create_output_mesh(outputFileName, stk::io::WRITE_RESULTS); |
| 164 |
|
| 165 |
stkIo.add_global(result_file_index, globalVarName, Ioss::Field::REAL); |
| 166 |
|
| 167 |
const double time = 1.0; |
| 168 |
stkIo.begin_output_step(result_file_index, time); |
| 169 |
|
| 170 |
stkIo.write_global(result_file_index, globalVarName, globalVarValue); |
| 171 |
|
| 172 |
stkIo.end_output_step(result_file_index); |
| 173 |
} |
| 174 |
|
| 175 |
{ |
| 176 |
double global_value = 0.0; |
| 177 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 178 |
stkIo.add_mesh_database(outputFileName, stk::io::READ_MESH); |
| 179 |
stkIo.create_input_mesh(); |
| 180 |
stkIo.populate_bulk_data(); |
| 181 |
|
| 182 |
bool abort_if_not_exist = true; |
| 183 |
EXPECT_ANY_THROW(stkIo.get_global("does_not_exist", global_value)); |
| 184 |
EXPECT_ANY_THROW(stkIo.get_global("does_not_exist", global_value, abort_if_not_exist)); |
| 185 |
ASSERT_TRUE(stkIo.get_global(globalVarName, global_value, abort_if_not_exist)); |
| 186 |
|
| 187 |
abort_if_not_exist = false; |
| 188 |
ASSERT_FALSE(stkIo.get_global("does_not_exist", global_value, abort_if_not_exist)); |
| 189 |
ASSERT_TRUE(stkIo.get_global(globalVarName, global_value, abort_if_not_exist)); |
| 190 |
} |
| 191 |
unlink(outputFileName.c_str()); |
| 192 |
} |
| 193 |
|
| 194 |
TEST(GlobalVariablesTest, OneGlobalDoubleVector3) |
| 195 |
{ |
| 196 |
const std::string outputFileName = "OneGlobalDoubleVector3.exo"; |
| 197 |
const std::string globalVarName = "testGlobal"; |
| 198 |
double goldGlobalScaleFactor = 0.1; |
| 199 |
std::vector<double> globalVarValue; |
| 200 |
for (int i=0; i < 3; i++) { |
| 201 |
globalVarValue.push_back(13.0 + i*goldGlobalScaleFactor); |
| 202 |
} |
| 203 |
|
| 204 |
MPI_Comm communicator = MPI_COMM_WORLD; |
| 205 |
{ |
| 206 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 207 |
const std::string exodusFileName = "generated:1x1x8"; |
| 208 |
stkIo.add_mesh_database(exodusFileName, stk::io::READ_MESH); |
| 209 |
stkIo.create_input_mesh(); |
| 210 |
stkIo.populate_bulk_data(); |
| 211 |
|
| 212 |
size_t result_file_index = stkIo.create_output_mesh(outputFileName, stk::io::WRITE_RESULTS); |
| 213 |
|
| 214 |
stkIo.add_global(result_file_index, globalVarName, "vector_3d", Ioss::Field::REAL); |
| 215 |
|
| 216 |
const double time = 1.0; |
| 217 |
stkIo.begin_output_step(result_file_index, time); |
| 218 |
|
| 219 |
stkIo.write_global(result_file_index, globalVarName, globalVarValue); |
| 220 |
|
| 221 |
stkIo.end_output_step(result_file_index); |
| 222 |
} |
| 223 |
|
| 224 |
const int stepNumber = 1; |
| 225 |
std::vector<std::string> globalVarNames(1, globalVarName); |
| 226 |
testGlobalVarOnFile(outputFileName, stepNumber, globalVarNames, globalVarValue, goldGlobalScaleFactor, communicator); |
| 227 |
unlink(outputFileName.c_str()); |
| 228 |
} |
| 229 |
|
| 230 |
TEST(GlobalVariablesTest, OneGlobalIntegerVector3) |
| 231 |
{ |
| 232 |
const std::string outputFileName = "OneGlobalIntegerVector3.exo"; |
| 233 |
const std::string globalVarName = "testGlobal"; |
| 234 |
std::vector<int> globalVarValue; |
| 235 |
int goldGlobalScaleFactor = 10; |
| 236 |
for (int i=0; i < 3; i++) { |
| 237 |
globalVarValue.push_back(13 + i*goldGlobalScaleFactor); |
| 238 |
} |
| 239 |
|
| 240 |
MPI_Comm communicator = MPI_COMM_WORLD; |
| 241 |
{ |
| 242 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 243 |
const std::string exodusFileName = "generated:1x1x8"; |
| 244 |
stkIo.add_mesh_database(exodusFileName, stk::io::READ_MESH); |
| 245 |
stkIo.create_input_mesh(); |
| 246 |
stkIo.populate_bulk_data(); |
| 247 |
|
| 248 |
size_t result_file_index = stkIo.create_output_mesh(outputFileName, stk::io::WRITE_RESULTS); |
| 249 |
|
| 250 |
stkIo.add_global(result_file_index, globalVarName, "vector_3d", Ioss::Field::INTEGER); |
| 251 |
|
| 252 |
const double time = 1.0; |
| 253 |
stkIo.begin_output_step(result_file_index, time); |
| 254 |
|
| 255 |
stkIo.write_global(result_file_index, globalVarName, globalVarValue); |
| 256 |
|
| 257 |
stkIo.end_output_step(result_file_index); |
| 258 |
} |
| 259 |
|
| 260 |
const int stepNumber = 1; |
| 261 |
std::vector<std::string> globalVarNames(1, globalVarName); |
| 262 |
testGlobalVarOnFile(outputFileName, stepNumber, globalVarNames, globalVarValue, goldGlobalScaleFactor, communicator); |
| 263 |
unlink(outputFileName.c_str()); |
| 264 |
} |
| 265 |
|
| 266 |
TEST(GlobalVariablesTest, OneGlobalDouble10) |
| 267 |
{ |
| 268 |
const std::string outputFileName = "OneGlobalDouble10.exo"; |
| 269 |
const std::string globalVarName = "testGlobal"; |
| 270 |
std::vector<double> globalVarValue; |
| 271 |
double goldGlobalScaleFactor = 0.1; |
| 272 |
for (int i=0; i < 10; i++) { |
| 273 |
globalVarValue.push_back(3.14159 + i*goldGlobalScaleFactor); |
| 274 |
} |
| 275 |
|
| 276 |
MPI_Comm communicator = MPI_COMM_WORLD; |
| 277 |
{ |
| 278 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 279 |
const std::string exodusFileName = "generated:1x1x8"; |
| 280 |
stkIo.add_mesh_database(exodusFileName, stk::io::READ_MESH); |
| 281 |
stkIo.create_input_mesh(); |
| 282 |
stkIo.populate_bulk_data(); |
| 283 |
|
| 284 |
size_t result_file_index = stkIo.create_output_mesh(outputFileName, stk::io::WRITE_RESULTS); |
| 285 |
|
| 286 |
stkIo.add_global(result_file_index, globalVarName, globalVarValue.size(), Ioss::Field::REAL); |
| 287 |
|
| 288 |
const double time = 1.0; |
| 289 |
stkIo.begin_output_step(result_file_index, time); |
| 290 |
|
| 291 |
stkIo.write_global(result_file_index, globalVarName, globalVarValue); |
| 292 |
|
| 293 |
stkIo.end_output_step(result_file_index); |
| 294 |
} |
| 295 |
|
| 296 |
const int stepNumber = 1; |
| 297 |
std::vector<std::string> globalVarNames(1, globalVarName); |
| 298 |
testGlobalVarOnFile(outputFileName, stepNumber, globalVarNames, globalVarValue, goldGlobalScaleFactor, communicator); |
| 299 |
unlink(outputFileName.c_str()); |
| 300 |
} |
| 301 |
|
| 302 |
template <typename DataType> |
| 303 |
void testTwoGlobals(const std::string &outputFileName, const std::vector<std::string> &globalVarNames) |
| 304 |
{ |
| 305 |
MPI_Comm communicator = MPI_COMM_WORLD; |
| 306 |
std::vector<DataType> globalVarValues = {13, 14}; |
| 307 |
{ |
| 308 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 309 |
const std::string exodusFileName = "generated:1x1x8"; |
| 310 |
stkIo.add_mesh_database(exodusFileName, stk::io::READ_MESH); |
| 311 |
stkIo.create_input_mesh(); |
| 312 |
stkIo.populate_bulk_data(); |
| 313 |
|
| 314 |
size_t resultOuputIndex = stkIo.create_output_mesh(outputFileName, stk::io::WRITE_RESULTS); |
| 315 |
|
| 316 |
Ioss::Field::BasicType iossDataType = iossBasicType(DataType()); |
| 317 |
stkIo.add_global(resultOuputIndex, globalVarNames[0], iossDataType); |
| 318 |
stkIo.add_global(resultOuputIndex, globalVarNames[1], iossDataType); |
| 319 |
|
| 320 |
const double time = 1.0; |
| 321 |
stkIo.begin_output_step(resultOuputIndex, time); |
| 322 |
|
| 323 |
stkIo.write_global(resultOuputIndex, globalVarNames[0], globalVarValues[0]); |
| 324 |
stkIo.write_global(resultOuputIndex, globalVarNames[1], globalVarValues[1]); |
| 325 |
|
| 326 |
stkIo.end_output_step(resultOuputIndex); |
| 327 |
} |
| 328 |
|
| 329 |
const int stepNumber = 1; |
| 330 |
DataType goldGlobalScaleFactor = 0; |
| 331 |
testGlobalVarOnFile(outputFileName, stepNumber, globalVarNames, globalVarValues, goldGlobalScaleFactor, communicator); |
| 332 |
unlink(outputFileName.c_str()); |
| 333 |
} |
| 334 |
|
| 335 |
TEST(GlobalVariablesTest, TwoGlobalIntegers) |
| 336 |
{ |
| 337 |
std::vector<std::string> globalVarNames = {"testGlobal", "testGlobal2"}; |
| 338 |
testTwoGlobals<int>("TwoGlobalIntegers.exo", globalVarNames); |
| 339 |
} |
| 340 |
|
| 341 |
TEST(GlobalVariablesTest, TwoGlobalDoubles) |
| 342 |
{ |
| 343 |
std::vector<std::string> globalVarNames = {"testGlobal", "testGlobal2"}; |
| 344 |
testTwoGlobals<double>("TwoGlobalDoubles.exo", globalVarNames); |
| 345 |
} |
| 346 |
|
| 347 |
TEST(GlobalVariablesTest, TwoGlobalDoublesSameName) |
| 348 |
{ |
| 349 |
std::vector<std::string> globalVarNames = {"testGlobal", "testGlobal"}; |
| 350 |
EXPECT_ANY_THROW(testTwoGlobals<double>("TwoGlobalDoublesSameName.exo", globalVarNames)); |
| 351 |
unlink("TwoGlobalDoublesSameName.exo"); |
| 352 |
} |
| 353 |
|
| 354 |
stk::mesh::Field<double> &createNodalTestField(stk::mesh::MetaData &stkMeshMetaData, const std::string &fieldName) |
| 355 |
{ |
| 356 |
const int numberOfStates = 1; |
| 357 |
stk::mesh::Field<double> &field0 = stkMeshMetaData.declare_field<double>(stk::topology::NODE_RANK, fieldName, numberOfStates); |
| 358 |
stk::mesh::put_field_on_mesh(field0, stkMeshMetaData.universal_part(), nullptr); |
| 359 |
return field0; |
| 360 |
} |
| 361 |
void putDataOnTestField(stk::mesh::BulkData &stkMeshBulkData, stk::mesh::Field<double> &field0, std::vector<double> &nodalFieldValues) |
| 362 |
{ |
| 363 |
std::vector<stk::mesh::Entity> nodes; |
| 364 |
stk::mesh::get_entities(stkMeshBulkData, stk::topology::NODE_RANK, nodes); |
| 365 |
for(size_t i=0; i<nodes.size(); i++) |
| 366 |
{ |
| 367 |
double *fieldDataForNode = stk::mesh::field_data(field0, nodes[i]); |
| 368 |
*fieldDataForNode = static_cast<double>(stkMeshBulkData.identifier(nodes[i])); |
| 369 |
nodalFieldValues.push_back(*fieldDataForNode); |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
TEST(GlobalVariablesTest, GlobalDoubleWithFieldMultipleTimeSteps) |
| 374 |
{ |
| 375 |
const std::string outputFileName = "GlobalDoubleWithFieldMultipleTimeSteps.exo"; |
| 376 |
const std::string fieldName = "field0"; |
| 377 |
std::vector<double> nodalFieldValues; |
| 378 |
const std::string globalVarName = "testGlobal"; |
| 379 |
std::vector<double> globalVarValuesOverTime; |
| 380 |
const int numTimeSteps = 5; |
| 381 |
MPI_Comm communicator = MPI_COMM_WORLD; |
| 382 |
int numProcs = stk::parallel_machine_size(communicator); |
| 383 |
if (numProcs != 1) { |
| 384 |
return; |
| 385 |
} |
| 386 |
{ |
| 387 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 388 |
const std::string exodusFileName = "generated:1x1x8"; |
| 389 |
stkIo.add_mesh_database(exodusFileName, stk::io::READ_MESH); |
| 390 |
stkIo.create_input_mesh(); |
| 391 |
|
| 392 |
stk::mesh::Field<double> &field0 = createNodalTestField(stkIo.meta_data(), fieldName); |
| 393 |
|
| 394 |
stkIo.populate_bulk_data(); |
| 395 |
|
| 396 |
putDataOnTestField(stkIo.bulk_data(), field0, nodalFieldValues); |
| 397 |
|
| 398 |
size_t result_file_index = stkIo.create_output_mesh(outputFileName, stk::io::WRITE_RESULTS); |
| 399 |
stkIo.add_field(result_file_index, field0); |
| 400 |
stkIo.add_global(result_file_index, globalVarName, Ioss::Field::REAL); |
| 401 |
|
| 402 |
double time = 1.0; |
| 403 |
const double stepSize = 1.0; |
| 404 |
for(int i=0; i<numTimeSteps; i++) |
| 405 |
{ |
| 406 |
stkIo.begin_output_step(result_file_index, time); |
| 407 |
|
| 408 |
const double globalVarValue = time; |
| 409 |
stkIo.write_global(result_file_index, globalVarName, globalVarValue); |
| 410 |
globalVarValuesOverTime.push_back(globalVarValue); |
| 411 |
|
| 412 |
stkIo.write_defined_output_fields(result_file_index); |
| 413 |
|
| 414 |
stkIo.end_output_step(result_file_index); |
| 415 |
time += stepSize; |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
for(int i=0; i<numTimeSteps; i++) |
| 420 |
{ |
| 421 |
std::vector<std::string> globalVarNames(1, globalVarName); |
| 422 |
std::vector<double> globalVarValues(1,globalVarValuesOverTime[i]); |
| 423 |
double goldGlobalScaleFactor = 0.0; |
| 424 |
testGlobalVarOnFile(outputFileName, i+1, globalVarNames, globalVarValues, goldGlobalScaleFactor, communicator); |
| 425 |
testNodalFieldOnFile(outputFileName, i+1, fieldName, nodalFieldValues, communicator); |
| 426 |
} |
| 427 |
unlink(outputFileName.c_str()); |
| 428 |
} |
| 429 |
|
| 430 |
TEST(GlobalVariablesTest, OneGlobalDoubleRestart) |
| 431 |
{ |
| 432 |
const std::string restartFileName = "OneGlobalDouble.restart"; |
| 433 |
const std::string globalVarName = "testGlobal"; |
| 434 |
const double globalVarValue = 13.0; |
| 435 |
const double time = 1.0; |
| 436 |
MPI_Comm communicator = MPI_COMM_WORLD; |
| 437 |
{ |
| 438 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 439 |
const std::string exodusFileName = "generated:1x1x8"; |
| 440 |
stkIo.add_mesh_database(exodusFileName, stk::io::READ_MESH); |
| 441 |
stkIo.create_input_mesh(); |
| 442 |
stkIo.populate_bulk_data(); |
| 443 |
|
| 444 |
size_t fileIndex = stkIo.create_output_mesh(restartFileName, stk::io::WRITE_RESTART); |
| 445 |
|
| 446 |
stkIo.add_global(fileIndex, globalVarName, Ioss::Field::REAL); |
| 447 |
|
| 448 |
stkIo.begin_output_step(fileIndex, time); |
| 449 |
|
| 450 |
stkIo.write_global(fileIndex, globalVarName, globalVarValue); |
| 451 |
|
| 452 |
stkIo.end_output_step(fileIndex); |
| 453 |
} |
| 454 |
|
| 455 |
{ |
| 456 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 457 |
stkIo.add_mesh_database(restartFileName, stk::io::READ_RESTART); |
| 458 |
stkIo.create_input_mesh(); |
| 459 |
stkIo.populate_bulk_data(); |
| 460 |
stkIo.read_defined_input_fields(time); |
| 461 |
std::vector<std::string> globalVarNames; |
| 462 |
stkIo.get_global_variable_names(globalVarNames); |
| 463 |
ASSERT_EQ(1u, globalVarNames.size()); |
| 464 |
EXPECT_STRCASEEQ(globalVarName.c_str(), globalVarNames[0].c_str()); |
| 465 |
double globalVar = 0.0; |
| 466 |
ASSERT_TRUE(stkIo.get_global(globalVarNames[0], globalVar)); |
| 467 |
EXPECT_NEAR(globalVarValue, globalVar, std::numeric_limits<double>::epsilon()); |
| 468 |
} |
| 469 |
const int stepNumber = 1; |
| 470 |
std::vector<std::string> globalVarNames(1, globalVarName); |
| 471 |
std::vector<double> globalVarValues(1,globalVarValue); |
| 472 |
double goldGlobalScaleFactor = 0.0; |
| 473 |
testGlobalVarOnFile(restartFileName, stepNumber, globalVarNames, globalVarValues, goldGlobalScaleFactor,communicator); |
| 474 |
unlink(restartFileName.c_str()); |
| 475 |
} |
| 476 |
|
| 477 |
TEST(GlobalVariablesTest, OneGlobalDoubleWithFieldRestart) |
| 478 |
{ |
| 479 |
const std::string outputFileName = "GlobalDoubleWithFieldMultipleTimeSteps.restart"; |
| 480 |
const std::string fieldName = "field0"; |
| 481 |
std::vector<double> nodalFieldValues; |
| 482 |
const std::string globalVarName = "testGlobal"; |
| 483 |
std::vector<double> globalVarValuesOverTime; |
| 484 |
const int numTimeSteps = 5; |
| 485 |
MPI_Comm communicator = MPI_COMM_WORLD; |
| 486 |
int numProcs = stk::parallel_machine_size(communicator); |
| 487 |
if (numProcs != 1) { |
| 488 |
return; |
| 489 |
} |
| 490 |
|
| 491 |
{ |
| 492 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 493 |
const std::string exodusFileName = "generated:1x1x8"; |
| 494 |
stkIo.add_mesh_database(exodusFileName, stk::io::READ_MESH); |
| 495 |
stkIo.create_input_mesh(); |
| 496 |
|
| 497 |
stk::mesh::Field<double> &field0 = createNodalTestField(stkIo.meta_data(), fieldName); |
| 498 |
|
| 499 |
stkIo.populate_bulk_data(); |
| 500 |
|
| 501 |
putDataOnTestField(stkIo.bulk_data(), field0, nodalFieldValues); |
| 502 |
|
| 503 |
size_t fileIndex = stkIo.create_output_mesh(outputFileName, stk::io::WRITE_RESTART); |
| 504 |
stkIo.add_field(fileIndex, field0); |
| 505 |
|
| 506 |
stkIo.add_global(fileIndex, globalVarName, Ioss::Field::REAL); |
| 507 |
|
| 508 |
double time = 1.0; |
| 509 |
const double stepSize = 1.0; |
| 510 |
for(int i=0; i<numTimeSteps; i++) |
| 511 |
{ |
| 512 |
stkIo.begin_output_step(fileIndex, time); |
| 513 |
|
| 514 |
const double globalVarValue = time; |
| 515 |
stkIo.write_global(fileIndex, globalVarName, globalVarValue); |
| 516 |
globalVarValuesOverTime.push_back(globalVarValue); |
| 517 |
|
| 518 |
stkIo.write_defined_output_fields(fileIndex); |
| 519 |
|
| 520 |
stkIo.end_output_step(fileIndex); |
| 521 |
time += stepSize; |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
for(int i=0; i<numTimeSteps; i++) |
| 526 |
{ |
| 527 |
std::vector<std::string> globalVarNames(1, globalVarName); |
| 528 |
std::vector<double> globalVarValues(1,globalVarValuesOverTime[i]); |
| 529 |
double goldGlobalScaleFactor = 0.0; |
| 530 |
testGlobalVarOnFile(outputFileName, i+1, globalVarNames, globalVarValues, goldGlobalScaleFactor, communicator); |
| 531 |
testNodalFieldOnFile(outputFileName, i+1, fieldName, nodalFieldValues, communicator); |
| 532 |
} |
| 533 |
unlink(outputFileName.c_str()); |
| 534 |
} |
| 535 |
|
| 536 |
} |