| 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_STREQ, etc |
| 36 |
#include <stddef.h> // for size_t |
| 37 |
#include <unistd.h> // for unlink |
| 38 |
#include <istream> // for basic_istream, ifstream |
| 39 |
#include <map> // for _Rb_tree_const_iterator, etc |
| 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, getline |
| 44 |
#include <utility> // for pair |
| 45 |
#include <vector> // for vector |
| 46 |
|
| 47 |
namespace |
| 48 |
{ |
| 49 |
TEST(StkMeshIoBrokerHowTo, writeHeartbeat) |
| 50 |
{ |
| 51 |
|
| 52 |
const std::string file_name = "Heartbeat.txt"; |
| 53 |
MPI_Comm communicator = MPI_COMM_WORLD; |
| 54 |
int num_procs = stk::parallel_machine_size(communicator); |
| 55 |
if (num_procs != 1) { |
| 56 |
return; |
| 57 |
} |
| 58 |
int my_processor = stk::parallel_machine_rank(communicator); |
| 59 |
|
| 60 |
//-BEGIN |
| 61 |
stk::util::ParameterList params; |
| 62 |
|
| 63 |
{ |
| 64 |
// ============================================================ |
| 65 |
//+ INITIALIZATION... |
| 66 |
// Add some params to write and read... |
| 67 |
params.set_param("PI", -3.14159); // Double |
| 68 |
params.set_param("Answer", 42); // Integer |
| 69 |
|
| 70 |
std::vector<double> my_vector; |
| 71 |
my_vector.push_back(2.78); |
| 72 |
my_vector.push_back(5.30); |
| 73 |
my_vector.push_back(6.21); |
| 74 |
params.set_param("some_doubles", my_vector); // Vector of doubles |
| 75 |
|
| 76 |
std::vector<int> ages; |
| 77 |
ages.push_back(55); |
| 78 |
ages.push_back(49); |
| 79 |
ages.push_back(21); |
| 80 |
ages.push_back(19); |
| 81 |
params.set_param("Ages", ages); // Vector of integers |
| 82 |
} |
| 83 |
|
| 84 |
{ |
| 85 |
// ============================================================ |
| 86 |
//+ EXAMPLE USAGE... |
| 87 |
//+ Begin use of stk io heartbeat file... |
| 88 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 89 |
|
| 90 |
//+ Define the heartbeat output to be in TEXT format. |
| 91 |
size_t hb = stkIo.add_heartbeat_output(file_name, stk::io::TEXT); /*@\label{io:hb:add_heartbeat_output}*/ |
| 92 |
|
| 93 |
stk::util::ParameterMapType::const_iterator i = params.begin(); /*@\label{io:hb:begin_add}*/ |
| 94 |
stk::util::ParameterMapType::const_iterator iend = params.end(); |
| 95 |
for (; i != iend; ++i) { |
| 96 |
const std::string paramName = (*i).first; |
| 97 |
//+ NOTE: A reference to the param is needed here. |
| 98 |
stk::util::Parameter ¶m = params.get_param(paramName); |
| 99 |
|
| 100 |
//+ Tell heartbeat which variables to output at each step... |
| 101 |
//+ NOTE: The address of the value to be output is needed since the |
| 102 |
//+ value is output in the process_heartbeat_output call. |
| 103 |
stkIo.add_heartbeat_global(hb,paramName, param); |
| 104 |
}/*@\label{io:hb:end_add}*/ |
| 105 |
|
| 106 |
// Application's "Execution Loop" |
| 107 |
int timestep_count = 1; |
| 108 |
double time = 0.0; |
| 109 |
for (int step=1; step <= timestep_count; step++) { |
| 110 |
//+ Now output the global variables... |
| 111 |
//+ NOTE: All registered global values automatically output. |
| 112 |
stkIo.process_heartbeat_output(hb, step, time);/*@\label{io:hb:output}*/ |
| 113 |
} |
| 114 |
} //-END |
| 115 |
|
| 116 |
if (my_processor == 0) { // Heartbeat is only output on processor 0. |
| 117 |
// ============================================================ |
| 118 |
// VERIFICATION: |
| 119 |
// open the heartbeat file... |
| 120 |
std::ifstream heartbeat(file_name.c_str()); |
| 121 |
std::string header_line; |
| 122 |
std::string data_line; |
| 123 |
|
| 124 |
std::string expected_header_line = " Time Ages_1 Ages_2 Ages_3 Ages_4 Answer PI some_doubles_1 some_doubles_2 some_doubles_3"; |
| 125 |
std::string expected_data_line = " 0.00000e+00 55 49 21 19 42 -3.14159e+00 2.78000e+00 5.30000e+00 6.21000e+00"; |
| 126 |
|
| 127 |
EXPECT_TRUE(!std::getline(heartbeat, header_line).fail()); |
| 128 |
EXPECT_STREQ(header_line.c_str(), expected_header_line.c_str()); |
| 129 |
EXPECT_TRUE(!std::getline(heartbeat, data_line).fail()); |
| 130 |
EXPECT_STREQ(data_line.c_str(), expected_data_line.c_str()); |
| 131 |
|
| 132 |
// ============================================================ |
| 133 |
// CLEANUP: |
| 134 |
unlink(file_name.c_str()); |
| 135 |
} |
| 136 |
} |
| 137 |
} |