Debian Codegraph @

packages/stk/stk_doc_tests/stk_io/usingHeartbeatSpyhisFormat.cpp

raw
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, writeHeartbeatSpyhisFormat)
50 {
51
52 const std::string file_name = "Heartbeat-Spyhis.txt";
53 MPI_Comm communicator = MPI_COMM_WORLD;
54 int numProcs = stk::parallel_machine_size(communicator);
55 if (numProcs != 1) {
56 return;
57 }
58
59 stk::util::ParameterList parameters;
60
61 {
62 // ========================================================================
63 // INITIALIZATION...
64 // Add some parameters to write and read...
65 parameters.set_param("PI", 3.14159); // Double
66 parameters.set_param("Answer", 42); // Integer
67
68 std::vector<double> my_vector;
69 my_vector.push_back(2.78);
70 my_vector.push_back(5.30);
71 my_vector.push_back(6.21);
72 parameters.set_param("some_doubles", my_vector); // Vector of doubles...
73
74 std::vector<int> ages;
75 ages.push_back(55);
76 ages.push_back(49);
77 ages.push_back(21);
78 ages.push_back(19);
79
80 parameters.set_param("Ages", ages); // Vector of integers...
81 }
82
83 {
84 // ========================================================================
85 // EXAMPLE USAGE...
86 // Begin use of stk io heartbeat file...
87 stk::io::StkMeshIoBroker stkIo(communicator);
88
89 // Define the heartbeat output.
90 size_t heartbeat_index = stkIo.add_heartbeat_output(file_name, stk::io::SPYHIS);
91
92 stk::util::ParameterMapType::const_iterator i = parameters.begin();
93 stk::util::ParameterMapType::const_iterator iend = parameters.end();
94 for (; i != iend; ++i) {
95 const std::string parameterName = (*i).first;
96 stk::util::Parameter &parameter = parameters.get_param(parameterName);
97
98 // Tell heartbeat database which global variables should be output at each step...
99 stkIo.add_heartbeat_global(heartbeat_index, parameterName, parameter);
100 }
101
102 // Now output the global variables...
103 int timestep_count = 1;
104 double time = 0.0;
105 for (int step=1; step <= timestep_count; step++) {
106 stkIo.process_heartbeat_output(heartbeat_index, step, time);
107 }
108 }
109
110 {
111 // ========================================================================
112 // VERIFICATION:
113 // open the heartbeat file...
114 std::ifstream heartbeat(file_name.c_str());
115 std::string format_line;
116 std::string header_line;
117 std::string data_line;
118
119 std::string expected_format_line = "% Sierra SPYHIS Output";
120 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";
121 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";
122
123 EXPECT_TRUE(!std::getline(heartbeat, format_line).fail());
124 // Remove current date from format line...
125 format_line = format_line.substr(0, expected_format_line.size());
126 EXPECT_STREQ(format_line.c_str(), expected_format_line.c_str());
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, header_line).fail());
130 EXPECT_STREQ(header_line.c_str(), expected_header_line.c_str());
131 EXPECT_TRUE(!std::getline(heartbeat, data_line).fail());
132 EXPECT_STREQ(data_line.c_str(), expected_data_line.c_str());
133 }
134
135 // ========================================================================
136 // CLEANUP:
137 // unlink(file_name.c_str());
138 }
139 }