Debian Codegraph @

packages/stk/stk_doc_tests/stk_io/writingAndReadingGlobalVariables.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, ASSERT_EQ, etc
36 #include <stddef.h> // for size_t
37 #include <unistd.h> // for unlink
38 #include <exception> // for exception
39 #include <stk_io/StkMeshIoBroker.hpp> // for StkMeshIoBroker
40 #include <string> // for string, basic_string
41 #include <vector> // for vector
42 #include <limits> // for std::numeric_limits
43 #include "Ioss_Field.h" // for Field, etc
44 #include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc
45 #include "stk_util/parallel/Parallel.hpp"
46
47 namespace
48 {
49 //-BEGIN
50 TEST(StkMeshIoBrokerHowTo, writeAndReadGlobalVariables)
51 {
52 MPI_Comm communicator = MPI_COMM_WORLD;
53 int numProcs = stk::parallel_machine_size(communicator);
54 if (numProcs != 1) { return; }
55
56 const std::string restartFileName = "OneGlobalDouble.restart";
57 const std::string timeStepVarName = "timeStep";
58 const double timeStepSize = 1e-6;
59 const double currentTime = 1.0;
60
61 //+ Write restart file with time step size as a global variable
62 {
63 stk::io::StkMeshIoBroker stkIo(communicator);
64 const std::string exodusFileName = "generated:1x1x8";
65 stkIo.add_mesh_database(exodusFileName, stk::io::READ_MESH);
66 stkIo.create_input_mesh();
67 stkIo.populate_bulk_data();
68
69 size_t fileIndex =
70 stkIo.create_output_mesh(restartFileName, stk::io::WRITE_RESTART);
71 stkIo.add_global(fileIndex, timeStepVarName, Ioss::Field::REAL);
72 stkIo.begin_output_step(fileIndex, currentTime);
73 stkIo.write_global(fileIndex, timeStepVarName, timeStepSize);
74 stkIo.end_output_step(fileIndex);
75 }
76
77 //+ Read restart file with time step size as a global variable
78 {
79 stk::io::StkMeshIoBroker stkIo(communicator);
80 stkIo.add_mesh_database(restartFileName, stk::io::READ_RESTART);
81 stkIo.create_input_mesh();
82 stkIo.populate_bulk_data();
83 stkIo.read_defined_input_fields(currentTime);
84 std::vector<std::string> globalNamesOnFile;
85 stkIo.get_global_variable_names(globalNamesOnFile);
86
87 ASSERT_EQ(1u, globalNamesOnFile.size());
88 EXPECT_STRCASEEQ(timeStepVarName.c_str(),
89 globalNamesOnFile[0].c_str());
90 double timeStepSizeReadFromFile = 0.0;
91 stkIo.get_global(globalNamesOnFile[0], timeStepSizeReadFromFile);
92
93 const double epsilon = std::numeric_limits<double>::epsilon();
94 EXPECT_NEAR(timeStepSize, timeStepSizeReadFromFile, epsilon);
95
96 //+ If try to get a global that does not exist, will throw
97 //+ an exception by default...
98 double value = 0.0;
99 EXPECT_ANY_THROW(stkIo.get_global("does_not_exist", value));
100
101 //+ If the application wants to handle the error instead (without a try/catch),
102 //+ can pass in an optional boolean:
103 bool abort_if_not_found = false;
104 bool found = stkIo.get_global("does_not_exist", value, abort_if_not_found);
105 ASSERT_FALSE(found);
106 }
107
108 unlink(restartFileName.c_str());
109 }
110 //-END
111 }