Debian Codegraph @

packages/stk/stk_doc_tests/stk_io/usingHeartbeatCSVChangePrecision.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 <string> // for string, getline
43 #include <utility> // for pair
44 #include <vector> // for vector
45 #include "Ioss_Property.h" // for Property
46 #include "Ioss_PropertyManager.h" // for PropertyManager
47 #include "stk_util/parallel/Parallel.hpp"
48
49 namespace
50 {
51 TEST(StkMeshIoBrokerHowTo, writeHeartbeatCSVChangePrecision)
52 {
53
54 const std::string file_name = "Heartbeat-CSV.txt";
55 MPI_Comm communicator = MPI_COMM_WORLD;
56 int num_procs = stk::parallel_machine_size(communicator);
57 if (num_procs != 1) {
58 return;
59 }
60 int my_processor = stk::parallel_machine_rank(communicator);
61
62 stk::util::ParameterList params;
63
64 {
65 // ============================================================
66 // INITIALIZATION...
67 // Add some params to write and read...
68 params.set_param("PI", -3.14159); // Double
69 params.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 params.set_param("some_doubles", 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 params.set_param("Ages", ages); // Vector of integers...
84 }
85
86 {
87 // ============================================================
88 // EXAMPLE USAGE...
89 // Define the heartbeat output...
90 stk::io::StkMeshIoBroker stkIo(communicator);
91
92 //-BEGIN
93 //+ Output should have 10 digits of precision (1.0123456789e+00)
94 //+ default precision is 5 digits (1.012345e+00)
95 Ioss::PropertyManager hb_props;
96 hb_props.add(Ioss::Property("PRECISION", 10));
97
98 //+ Define the heartbeat output and the format (CSV)
99 size_t hb = stkIo.add_heartbeat_output(file_name, stk::io::CSV, hb_props);
100 //-END
101
102 stk::util::ParameterMapType::const_iterator i = params.begin();
103 stk::util::ParameterMapType::const_iterator iend = params.end();
104 for (; i != iend; ++i) {
105 const std::string parameterName = (*i).first;
106 stk::util::Parameter &parameter = params.get_param(parameterName);
107
108 //+ Tell heartbeat which variables to output at each step...
109 stkIo.add_heartbeat_global(hb, parameterName, parameter);
110 }
111
112 int timestep_count = 1;
113 double time = 0.0;
114 for (int step=1; step <= timestep_count; step++) {
115 //+ Now output the global variables...
116 stkIo.process_heartbeat_output(hb, step, time);
117 }
118 }
119
120 if (my_processor == 0) { // Heartbeat is only output on processor 0.
121 // ============================================================
122 // VERIFICATION:
123 // Expected output:
124 std::string expected_header_line =
125 " Time, Ages_1, Ages_2, Ages_3, Ages_4, Answer, PI, some_doubles_1, some_doubles_2, some_doubles_3";
126 std::string expected_data_line =
127 " 0.0000000000e+00, 55, 49, 21, 19, 42, -3.1415900000e+00, 2.7800000000e+00, 5.3000000000e+00, 6.2100000000e+00";
128
129 // open the heartbeat file...
130 std::ifstream heartbeat(file_name.c_str());
131 std::string header_line;
132 std::string data_line;
133
134 EXPECT_TRUE(!std::getline(heartbeat, header_line).fail());
135 EXPECT_STREQ(header_line.c_str(), expected_header_line.c_str());
136 EXPECT_TRUE(!std::getline(heartbeat, data_line).fail());
137 EXPECT_STREQ(data_line.c_str(), expected_data_line.c_str());
138
139 // ============================================================
140 // CLEANUP:
141 unlink(file_name.c_str());
142 }
143 }
144 }