Debian Codegraph @

packages/stk/stk_doc_tests/stk_io/writingAndReadingGlobalParameters.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 ASSERT_EQ, AssertHelper, etc
36 #include <stddef.h> // for size_t
37 #include <unistd.h> // for unlink
38 #include <map> // for _Rb_tree_const_iterator, etc
39 #include <stk_io/StkMeshIoBroker.hpp> // for StkMeshIoBroker
40 #include "parameterTestUtils.hpp"
41 #include <stk_util/util/ParameterList.hpp> // for ParameterList, etc
42 #include <string> // for string
43 #include <utility> // for pair
44 #include <vector> // for vector
45 #include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc
46
47 namespace
48 {
49 //-BEGIN
50 TEST(StkMeshIoBrokerHowTo, writeAndReadGlobalParameters)
51 {
52 // ============================================================
53 //+ INITIALIZATION
54 const std::string file_name = "GlobalParameters.e";
55 MPI_Comm communicator = MPI_COMM_WORLD;
56
57 // Add some parameters to write and read...
58 stk::util::ParameterList params;
59 params.set_param("PI", 3.14159); // Double
60 params.set_param("Answer", 42); // Integer
61
62 std::vector<double> my_vector = { 2.78, 5.30, 6.21 };
63 params.set_param("doubles", my_vector); // Vector of doubles...
64
65 std::vector<int> ages = { 55, 49, 21, 19};
66 params.set_param("Ages", ages); // Vector of integers...
67
68 {
69 stk::io::StkMeshIoBroker stkIo(communicator);
70 const std::string exodusFileName = "generated:1x1x8";
71 size_t index = stkIo.add_mesh_database(exodusFileName, stk::io::READ_MESH);
72 stkIo.set_active_mesh(index);
73 stkIo.create_input_mesh();
74 stkIo.populate_bulk_data();
75
76 // ============================================================
77 //+ EXAMPLE
78 //+ Write output file with all parameters in params list...
79 size_t idx = stkIo.create_output_mesh(file_name,
80 stk::io::WRITE_RESTART);
81
82 stk::util::ParameterMapType::const_iterator i = params.begin();
83 stk::util::ParameterMapType::const_iterator ie = params.end();
84 for (; i != ie; ++i) {
85 const std::string parameterName = (*i).first;
86 stk::util::Parameter &param = params.get_param(parameterName);
87 stkIo.add_global(idx, parameterName, param);
88 }
89
90 stkIo.begin_output_step(idx, 0.0);/*@\label{io:global:write_begin}*/
91
92 for (i = params.begin(); i != ie; ++i) {
93 const std::string parameterName = (*i).first;
94 stk::util::Parameter &param = params.get_param(parameterName);
95 stkIo.write_global(idx, parameterName, param);
96 }
97
98 stkIo.end_output_step(idx);/*@\label{io:global:write_end}*/
99 }
100
101 {
102 // ============================================================
103 //+ EXAMPLE
104 //+ Read parameters from file...
105 stk::io::StkMeshIoBroker stkIo(communicator);
106 stkIo.add_mesh_database(file_name, stk::io::READ_MESH);
107 stkIo.create_input_mesh();
108 stkIo.populate_bulk_data();
109
110 stkIo.read_defined_input_fields(0.0);
111
112 stk::util::ParameterMapType::const_iterator i = params.begin();
113 stk::util::ParameterMapType::const_iterator ie = params.end();
114 for (; i != ie; ++i) {
115 const std::string parameterName = (*i).first;
116 stk::util::Parameter &param = params.get_param(parameterName);
117 stkIo.get_global(parameterName, param);
118 }
119
120 // ============================================================
121 //+ VALIDATION
122 stk::util::ParameterList gold_params; // To compare values read
123 gold_params.set_param("PI", 3.14159); // Double
124 gold_params.set_param("Answer", 42); // Integer
125 gold_params.set_param("doubles", my_vector); // Vector of doubles
126 gold_params.set_param("Ages", ages); // Vector of integers...
127
128 size_t param_count = 0;
129 for (i = params.begin(); i != ie; ++i) {
130 param_count++;
131 const std::string parameterName = (*i).first;
132 stk::util::Parameter &param = params.get_param(parameterName);
133 stk::util::Parameter &gold_parameter =
134 gold_params.get_param(parameterName);
135 validate_parameters_equal_value(param, gold_parameter);
136 }
137
138 std::vector<std::string> globalNamesOnFile;
139 stkIo.get_global_variable_names(globalNamesOnFile);
140 ASSERT_EQ(param_count, globalNamesOnFile.size());
141 }
142 // ============================================================
143 // CLEAN UP
144 unlink(file_name.c_str());
145 }
146 //-END
147 }