| 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> |
| 36 |
#include <string> |
| 37 |
#include <stk_io/StkMeshIoBroker.hpp> |
| 38 |
#include <Ioss_SubSystem.h> |
| 39 |
#include <stk_mesh/base/MetaData.hpp> |
| 40 |
#include <restartTestUtils.hpp> |
| 41 |
|
| 42 |
namespace { |
| 43 |
|
| 44 |
TEST(StkMeshIoBrokerHowTo, writingMultipleOutputFiles) |
| 45 |
{ |
| 46 |
// ============================================================ |
| 47 |
//+ INITIALIZATION |
| 48 |
const std::string resultsFilename1 = "output1.results"; |
| 49 |
const std::string resultsFilename2 = "output2.results"; |
| 50 |
const std::string displacementFieldName = "displacement"; |
| 51 |
const std::string velocityFieldName = "velocity"; |
| 52 |
|
| 53 |
std::string globalVarNameFile1 = "eigenValue"; |
| 54 |
std::string globalVarNameFile2 = "kineticEnergy"; |
| 55 |
const double globalVarValue1 = 13.0; |
| 56 |
const double globalVarValue2 = 14.0; |
| 57 |
std::string nameOnOutputFile("deformations"); |
| 58 |
|
| 59 |
MPI_Comm communicator = MPI_COMM_WORLD; |
| 60 |
{ |
| 61 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 62 |
setupMeshAndFieldsForTest(stkIo, displacementFieldName, velocityFieldName); |
| 63 |
stk::mesh::MetaData &meta_data = stkIo.meta_data(); |
| 64 |
|
| 65 |
//-BEGIN |
| 66 |
// ============================================================ |
| 67 |
//+ EXAMPLE -- Two results output files |
| 68 |
stk::mesh::FieldBase *displacementField = |
| 69 |
meta_data.get_field(stk::topology::NODE_RANK, displacementFieldName); |
| 70 |
|
| 71 |
//+ For file one, set up results and global variables |
| 72 |
size_t file1Handle = stkIo.create_output_mesh(resultsFilename1, |
| 73 |
stk::io::WRITE_RESULTS); |
| 74 |
stkIo.add_field(file1Handle, *displacementField); |
| 75 |
stkIo.add_global(file1Handle, globalVarNameFile1, Ioss::Field::REAL); |
| 76 |
|
| 77 |
//+ For file two, set up results and global variables |
| 78 |
size_t file2Handle = stkIo.create_output_mesh(resultsFilename2, |
| 79 |
stk::io::WRITE_RESULTS); |
| 80 |
stkIo.add_field(file2Handle, *displacementField, nameOnOutputFile); |
| 81 |
stk::mesh::FieldBase *velocityField = meta_data.get_field(stk::topology::NODE_RANK, velocityFieldName); |
| 82 |
stkIo.add_field(file2Handle, *velocityField); |
| 83 |
stkIo.add_global(file2Handle, globalVarNameFile2, Ioss::Field::REAL); |
| 84 |
|
| 85 |
//+ Write output |
| 86 |
double time = 0.0; |
| 87 |
stkIo.begin_output_step(file1Handle, time); |
| 88 |
stkIo.write_defined_output_fields(file1Handle); |
| 89 |
stkIo.write_global(file1Handle, globalVarNameFile1, globalVarValue1); |
| 90 |
stkIo.end_output_step(file1Handle); |
| 91 |
|
| 92 |
stkIo.begin_output_step(file2Handle, time); |
| 93 |
stkIo.write_defined_output_fields(file2Handle); |
| 94 |
stkIo.write_global(file2Handle, globalVarNameFile2, globalVarValue2); |
| 95 |
stkIo.end_output_step(file2Handle); |
| 96 |
} |
| 97 |
//-END |
| 98 |
// ============================================================ |
| 99 |
//+ Validation |
| 100 |
std::vector<std::string> goldNodalVarNamesInFile1; |
| 101 |
goldNodalVarNamesInFile1.push_back(displacementFieldName); |
| 102 |
checkFileForNodalVarNames(resultsFilename1, goldNodalVarNamesInFile1); |
| 103 |
checkFileForGlobal(resultsFilename1, globalVarNameFile1, globalVarValue1); |
| 104 |
|
| 105 |
std::vector<std::string> goldNodalVarNamesInFile2; |
| 106 |
goldNodalVarNamesInFile2.push_back(nameOnOutputFile); |
| 107 |
goldNodalVarNamesInFile2.push_back(velocityFieldName); |
| 108 |
checkFileForNodalVarNames(resultsFilename2, goldNodalVarNamesInFile2); |
| 109 |
checkFileForGlobal(resultsFilename2, globalVarNameFile2, globalVarValue2); |
| 110 |
|
| 111 |
// Cleanup |
| 112 |
unlink(resultsFilename1.c_str()); |
| 113 |
unlink(resultsFilename2.c_str()); |
| 114 |
} |
| 115 |
|
| 116 |
} |