Debian Codegraph @

packages/stk/stk_doc_tests/stk_io/writingAndReadingGlobalParametersAuto.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 TEST(StkMeshIoBrokerHowTo, writeAndReadGlobalParametersAuto)
50 {
51 const std::string file_name = "GlobalParameters.e";
52 MPI_Comm communicator = MPI_COMM_WORLD;
53
54 stk::util::ParameterList params;
55
56 // Add some parameters to write and read...
57 params.set_param("PI", 3.14159); // Double
58 params.set_param("Answer", 42); // Integer
59
60 std::vector<double> my_vector;
61 my_vector.push_back(2.78);
62 my_vector.push_back(5.30);
63 my_vector.push_back(6.21);
64 params.set_param("some_doubles", my_vector); // Vector of doubles...
65
66 std::vector<int> ages;
67 ages.push_back(55);
68 ages.push_back(49);
69 ages.push_back(21);
70 ages.push_back(19);
71
72 params.set_param("Ages", ages); // Vector of integers...
73
74 //-BEGIN
75 // ... Setup is the same as in the previous example
76 // Write output file with all parameters in params list...
77 {
78 stk::io::StkMeshIoBroker stkIo(communicator);
79 const std::string exodusFileName = "generated:1x1x8";
80 size_t input_index = stkIo.add_mesh_database(exodusFileName, stk::io::READ_MESH);
81 stkIo.set_active_mesh(input_index);
82 stkIo.create_input_mesh();
83 stkIo.populate_bulk_data();
84
85 size_t idx = stkIo.create_output_mesh(file_name,
86 stk::io::WRITE_RESTART);
87
88 stk::util::ParameterMapType::const_iterator i = params.begin();
89 stk::util::ParameterMapType::const_iterator iend = params.end();
90 for (; i != iend; ++i) {
91 const std::string paramName = (*i).first;
92 //+ NOTE: Need a reference to the parameter.
93 stk::util::Parameter &param = params.get_param(paramName);
94 stkIo.add_global_ref(idx, paramName, param);/*@\label{io:global:autoparam}*/
95 }
96
97 //+ All writing of the values is handled automatically,
98 //+ do not need to call write_global
99 stkIo.process_output_request(idx, 0.0);/*@\label{io:global:autowrite}*/
100 }
101 // ... Reading is the same as in previous example
102 //-END
103
104 // Read params from file...
105 {
106 stk::util::ParameterList gold_params; // To compare values read
107 gold_params.set_param("PI", 3.14159); // Double
108 gold_params.set_param("Answer", 42); // Integer
109 gold_params.set_param("some_doubles", my_vector);
110 gold_params.set_param("Ages", ages); // Vector of integers...
111
112 stk::io::StkMeshIoBroker stkIo(communicator);
113 stkIo.add_mesh_database(file_name, stk::io::READ_MESH);
114 stkIo.create_input_mesh();
115 stkIo.populate_bulk_data();
116
117 stkIo.read_defined_input_fields(0.0);
118
119 size_t param_count = 0;
120 stk::util::ParameterMapType::const_iterator i = params.begin();
121 stk::util::ParameterMapType::const_iterator iend = params.end();
122 for (; i != iend; ++i) {
123 param_count++;
124 const std::string paramName = (*i).first;
125 stk::util::Parameter &param = params.get_param(paramName);
126 stk::util::Parameter &gold_param
127 = gold_params.get_param(paramName);
128 stkIo.get_global(paramName, param);
129 validate_parameters_equal_value(param, gold_param);
130 }
131
132 std::vector<std::string> globalNamesOnFile;
133 stkIo.get_global_variable_names(globalNamesOnFile);
134 ASSERT_EQ(param_count, globalNamesOnFile.size());
135
136 }
137 unlink(file_name.c_str());
138 }
139 }