Debian Codegraph @

packages/stk/stk_doc_tests/stk_io/usingResults.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_EQ, etc
36 #include <stddef.h> // for size_t
37 #include <unistd.h> // for unlink
38 #include <ostream> // for basic_ostream::operator<<
39 #include <stk_io/StkMeshIoBroker.hpp> // for StkMeshIoBroker
40 #include <stk_mesh/base/Field.hpp> // for Field
41 #include <stk_mesh/base/GetEntities.hpp> // for get_entities
42 #include <stk_mesh/base/MetaData.hpp> // for MetaData, put_field
43 #include <string> // for string, operator+
44 #include <vector> // for vector
45 #include "Ioss_DBUsage.h" // for DatabaseUsage::READ_MODEL
46 #include "Ioss_Field.h" // for Field, etc
47 #include "Ioss_IOFactory.h" // for IOFactory
48 #include "Ioss_NodeBlock.h" // for NodeBlock
49 #include "Ioss_Property.h" // for Property
50 #include "Ioss_Region.h" // for Region, NodeBlockContainer
51 #include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc
52 #include "stk_mesh/base/Entity.hpp" // for Entity
53 #include "stk_mesh/base/FieldBase.hpp" // for field_data, FieldBase
54 #include "stk_mesh/base/FieldState.hpp" // for FieldState::StateN, etc
55 #include "stk_topology/topology.hpp" // for topology, etc
56 #include "stk_util/parallel/Parallel.hpp"
57 namespace Ioss { class DatabaseIO; }
58
59 namespace {
60
61 TEST(StkMeshIoBrokerHowTo, writeResultsWithMultistateField)
62 {
63 std::string resultsFilename = "output.results";
64 MPI_Comm communicator = MPI_COMM_WORLD;
65 int numProcs = stk::parallel_machine_size(communicator);
66 if (numProcs != 1) {
67 return;
68 }
69
70 //-BEGIN
71 const std::string fieldName = "disp";
72 const std::string np1Name = fieldName+"NP1";
73 const std::string nName = fieldName+"N";
74 const std::string nm1Name = fieldName+"Nm1";
75 {
76 // ============================================================
77 //+ INITIALIZATION
78 const std::string exodusFileName = "generated:1x1x8";
79 stk::io::StkMeshIoBroker stkIo(communicator);
80 size_t index = stkIo.add_mesh_database(exodusFileName, stk::io::READ_MESH);
81 stkIo.set_active_mesh(index);
82 stkIo.create_input_mesh();
83
84 //+ Declare a three-state field
85 //+ NOTE: Fields must be declared before "populate_bulk_data()" is called
86 //+ since it commits the meta data.
87 stk::mesh::Field<double> &field =
88 stkIo.meta_data().declare_field<double>(stk::topology::NODE_RANK, fieldName, 3);
89 stk::mesh::put_field_on_mesh(field, stkIo.meta_data().universal_part(), nullptr);
90
91 stkIo.populate_bulk_data();
92
93 size_t fh =
94 stkIo.create_output_mesh(resultsFilename, stk::io::WRITE_RESULTS);
95
96 // ============================================================
97 //+ EXAMPLE
98 //+ Output each state of the multi-state field individually to results file
99 stk::mesh::FieldBase *statedFieldNp1 = field.field_state(stk::mesh::StateNP1);
100 stk::mesh::FieldBase *statedFieldN = field.field_state(stk::mesh::StateN);
101 stk::mesh::FieldBase *statedFieldNm1 = field.field_state(stk::mesh::StateNM1);
102
103 std::vector<stk::mesh::Entity> nodes;
104 stk::mesh::get_entities(stkIo.bulk_data(), stk::topology::NODE_RANK, nodes);
105
106 stkIo.add_field(fh, *statedFieldNp1, np1Name);
107 stkIo.add_field(fh, *statedFieldN, nName);
108 stkIo.add_field(fh, *statedFieldNm1, nm1Name);
109
110 // Iterate the application's execute loop five times and output
111 // field data each iteration.
112 for (int step=0; step < 5; step++) {
113 double time = step;
114
115 // Application execution...
116 // Generate field data... (details omitted)
117 //-END
118 double value = 10.0 * time;
119 for(size_t i=0; i<nodes.size(); i++) {
120 double *np1_data =
121 static_cast<double*>(stk::mesh::field_data(*statedFieldNp1, nodes[i]));
122 *np1_data = value;
123 double *n_data =
124 static_cast<double*>(stk::mesh::field_data(*statedFieldN, nodes[i]));
125 *n_data = value + 0.1;
126 double *nm1_data =
127 static_cast<double*>(stk::mesh::field_data(*statedFieldNm1, nodes[i]));
128 *nm1_data = value + 0.2;
129 }
130 //-BEGIN
131 //+ Results output...
132 stkIo.begin_output_step(fh, time);
133 stkIo.write_defined_output_fields(fh);
134 stkIo.end_output_step(fh);
135 }
136 //-END
137 }
138 //-END
139
140 // ============================================================
141 //+ VERIFICATION
142 {
143 Ioss::DatabaseIO *resultsDb = Ioss::IOFactory::create("exodus", resultsFilename,
144 Ioss::READ_MODEL, communicator);
145 Ioss::Region results(resultsDb);
146 // Should be 5 steps on database...
147 EXPECT_EQ(results.get_property("state_count").get_int(), 5);
148 // Should be 3 nodal fields on database named "disp.NP1", "disp.N", "disp.NM1";
149 Ioss::NodeBlock *nb = results.get_node_blocks()[0];
150 EXPECT_EQ(3u, nb->field_count(Ioss::Field::TRANSIENT));
151 EXPECT_TRUE(nb->field_exists(np1Name));
152 EXPECT_TRUE(nb->field_exists(nName));
153 EXPECT_TRUE(nb->field_exists(nm1Name));
154
155 // Iterate each step and verify that the correct data was written.
156 for (size_t step=0; step < 5; step++) {
157 double time = step;
158
159 double db_time = results.begin_state(step+1);
160 EXPECT_EQ(time, db_time);
161
162 std::vector<double> field_data_np1;
163 std::vector<double> field_data_n;
164 std::vector<double> field_data_nm1;
165 nb->get_field_data(np1Name, field_data_np1);
166 nb->get_field_data(nName, field_data_n);
167 nb->get_field_data(nm1Name, field_data_nm1);
168
169 EXPECT_EQ(field_data_np1.size(), field_data_n.size());
170 EXPECT_EQ(field_data_np1.size(), field_data_nm1.size());
171
172 double expected = 10.0 * time;
173 for (size_t node = 0; node < field_data_np1.size(); node++) {
174 EXPECT_EQ(field_data_np1[node], expected);
175 EXPECT_EQ(field_data_n[node], expected+0.1);
176 EXPECT_EQ(field_data_nm1[node], expected+0.2);
177 }
178 results.end_state(step+1);
179 }
180 }
181
182 unlink(resultsFilename.c_str());
183 }
184 }