Debian Codegraph @

packages/stk/stk_doc_tests/stk_io/usingHeartbeatOverrideSeparator.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 #include "Ioss_Property.h" // for Property
47 #include "Ioss_PropertyManager.h" // for PropertyManager
48
49 namespace
50 {
51 TEST(StkMeshIoBrokerHowTo, writeHeartbeatOverrideSeparator)
52 {
53
54 const std::string file_name = "HeartbeatBarSep.txt";
55 MPI_Comm communicator = MPI_COMM_WORLD;
56 int numProcs = stk::parallel_machine_size(communicator);
57
58 if (numProcs != 1) {
59 return;
60 }
61
62 stk::util::ParameterList parameters;
63
64 {
65 // ========================================================================
66 // INITIALIZATION...
67 // Add some parameters to write and read...
68 parameters.set_param("PI", -3.14159); // Double
69 parameters.set_param("Answer", 42); // Integer
70
71 std::vector<double> my_vector;
72 my_vector.push_back(2.78);
73 my_vector.push_back(5.30);
74 my_vector.push_back(6.21);
75 parameters.set_param("dbl_vec", my_vector); // Vector of doubles...
76
77 std::vector<int> ages;
78 ages.push_back(55);
79 ages.push_back(49);
80 ages.push_back(21);
81 ages.push_back(19);
82
83 parameters.set_param("Ages", ages); // Vector of integers...
84 }
85
86 {
87 // ========================================================================
88 // EXAMPLE USAGE...
89 // Begin use of stk io heartbeat file...
90 stk::io::StkMeshIoBroker stkIo(communicator);
91
92 //-BEGIN
93 //+ Use vertical bar as field separator
94 Ioss::PropertyManager hb_props;
95 hb_props.add(Ioss::Property("FIELD_SEPARATOR", " | "));
96 size_t hb =
97 stkIo.add_heartbeat_output(file_name, stk::io::TEXT, hb_props);
98 //-END
99
100 stk::util::ParameterMapType::const_iterator i = parameters.begin();
101 stk::util::ParameterMapType::const_iterator iend = parameters.end();
102 for (; i != iend; ++i) {
103 const std::string parameterName = (*i).first;
104 stk::util::Parameter &parameter = parameters.get_param(parameterName);
105
106 // Tell heartbeat database which global variables should be output at each step...
107 stkIo.add_heartbeat_global(hb, parameterName, parameter);
108 }
109
110 // Now output the global variables...
111 int timestep_count = 1;
112 double time = 0.0;
113 for (int step=1; step <= timestep_count; step++) {
114 stkIo.process_heartbeat_output(hb, step, time);
115 }
116 }
117
118 {
119 // ========================================================================
120 // VERIFICATION:
121 // open the heartbeat file...
122 std::ifstream heartbeat(file_name.c_str());
123 std::string header_line;
124 std::string data_line;
125
126 std::string expected_header_line = " Time | Ages_1 | Ages_2 | Ages_3 | Ages_4 | Answer | PI | dbl_vec_1 | dbl_vec_2 | dbl_vec_3";
127 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";
128
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 }