Debian Codegraph @

packages/stk/stk_doc_tests/stk_io/writeResultsAndRestart.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
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 "Ioss_ElementBlock.h"
52 #include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc
53 #include "stk_mesh/base/Entity.hpp" // for Entity
54 #include "stk_mesh/base/FieldBase.hpp" // for field_data, FieldBase
55 #include "stk_mesh/base/FieldState.hpp" // for FieldState::StateN, etc
56 #include "stk_topology/topology.hpp" // for topology, etc
57 namespace Ioss { class DatabaseIO; }
58 namespace {
59
60 TEST(StkMeshIoBrokerHowTo, writeResultsAndRestart)
61 {
62 std::string mesh_name = "input_mesh_example.e";
63 std::string results_name = "output.results";
64 std::string restart_name = "output.restart";
65 MPI_Comm communicator = MPI_COMM_WORLD;
66
67 {
68 // ============================================================
69 //+ INITIALIZATION:
70 //+ Create a basic mesh with a hex block, 3 shell blocks, 3 nodesets, and 3 sidesets.
71 stk::io::StkMeshIoBroker stkIo(communicator);
72
73 const std::string generatedFileName = "generated:8x8x8|shell:xyz|nodeset:xyz|sideset:XYZ";
74 size_t index = stkIo.add_mesh_database(generatedFileName, stk::io::READ_MESH);
75 stkIo.set_active_mesh(index);
76 stkIo.create_input_mesh();
77 stkIo.populate_bulk_data();
78
79 size_t fh = stkIo.create_output_mesh(mesh_name, stk::io::WRITE_RESULTS);
80 stkIo.write_output_mesh(fh);
81 }
82
83 {
84 //-BEGIN
85 // ============================================================
86 //+ EXAMPLE:
87 //+ Read mesh data from the specified file.
88 stk::io::StkMeshIoBroker stkIo(communicator);
89 stkIo.add_mesh_database(mesh_name, stk::io::READ_MESH);
90
91 //+ Creates meta data; creates parts
92 stkIo.create_input_mesh();
93
94 //+ Declare a three-state field
95 //+ NOTE: Fields must be declared before "populate_bulk_data()" is called
96 //+ since it commits the meta data.
97 const std::string fieldName = "disp";
98 stk::mesh::Field<double> &field = stkIo.meta_data().declare_field<double>(stk::topology::NODE_RANK, fieldName, 3);
99 stk::mesh::put_field_on_mesh(field, stkIo.meta_data().universal_part(), nullptr);
100
101 const stk::mesh::Part& block_1 = *stkIo.meta_data().get_part("block_1");
102 //+ create a two-state field
103 stk::mesh::Field<double> &fooSubset = stkIo.meta_data().declare_field<double>(stk::topology::NODE_RANK, "fooSubset", 2);
104 stk::mesh::put_field_on_mesh(fooSubset, block_1, nullptr);
105
106 //+ commit the meta data and create the bulk data.
107 //+ populate the bulk data with data from the mesh file.
108 stkIo.populate_bulk_data();
109
110 // ============================================================
111 //+ Create results file. By default, all parts created from the input
112 //+ mesh will be written to the results output file.
113 size_t results_fh = stkIo.create_output_mesh(results_name, stk::io::WRITE_RESULTS);
114
115 //+ Create restart file. By default, all parts created from the input
116 //+ mesh will be written to the results output file.
117 size_t restart_fh = stkIo.create_output_mesh(restart_name, stk::io::WRITE_RESTART);
118
119 //+ The field will be output to the results file with the default field name.
120 //+ Only the newest state will be output.
121 stkIo.add_field(results_fh, field);
122
123 //+ Output the field to the restart database also.
124 //+ The two newest states will be output.
125 stkIo.add_field(restart_fh, field);
126 stkIo.add_field(restart_fh, fooSubset);
127
128 std::vector<stk::mesh::Entity> nodes;
129 stk::mesh::get_entities(stkIo.bulk_data(), stk::topology::NODE_RANK, nodes);
130
131 stk::mesh::FieldBase *statedFieldNp1 = field.field_state(stk::mesh::StateNP1);
132 stk::mesh::FieldBase *statedFieldN = field.field_state(stk::mesh::StateN);
133 stk::mesh::FieldBase *statedFieldNm1 = field.field_state(stk::mesh::StateNM1);
134
135 // Iterate the application's execute loop five times and output
136 // field data each iteration.
137 for (int step=0; step < 5; step++) {
138 double time = step;
139
140 // Application execution...
141 double value = 10.0 * time;
142 for(size_t i=0; i<nodes.size(); i++) {
143 double *np1_data = static_cast<double*>(stk::mesh::field_data(*statedFieldNp1, nodes[i]));
144 *np1_data = value;
145 double *n_data = static_cast<double*>(stk::mesh::field_data(*statedFieldN, nodes[i]));
146 *n_data = value + 0.1;
147 double *nm1_data = static_cast<double*>(stk::mesh::field_data(*statedFieldNm1, nodes[i]));
148 *nm1_data = value + 0.2;
149 }
150
151 //+ Results output...
152 stkIo.begin_output_step(results_fh, time);
153 stkIo.write_defined_output_fields(results_fh);
154 stkIo.end_output_step(results_fh);
155
156 //+ Restart output...
157 stkIo.begin_output_step(restart_fh, time);
158 stkIo.write_defined_output_fields(restart_fh);
159 stkIo.end_output_step(restart_fh);
160 }
161 //-END
162 }
163
164 {
165 //Demonstrate reading a restart database after adding an extra multistate field to
166 //the io-broker "foo" which we know is missing from the database. We confirm that
167 //passing the 'missingFields' argument to stkIo.read_defined_input_fields allows
168 //the code to continue without throwing an exception due to not finding the field.
169 stk::io::StkMeshIoBroker stkIo(communicator);
170 size_t rs = stkIo.add_mesh_database(restart_name, stk::io::READ_RESTART);
171
172 //+ "Restart" the calculation...
173 double time = 1.0;
174 stkIo.set_active_mesh(rs);
175 stkIo.create_input_mesh();
176
177 //create a 3-state field
178 stk::mesh::Field<double> &foo =
179 stkIo.meta_data().declare_field<double>(stk::topology::NODE_RANK, "foo", 3);
180 stk::mesh::put_field_on_mesh(foo, stkIo.meta_data().universal_part(), nullptr);
181
182 const stk::mesh::Part& block_1 = *stkIo.meta_data().get_part("block_1");
183 //create a 3-state field
184 stk::mesh::Field<double> &fooSubset = stkIo.meta_data().
185 declare_field<double>(stk::topology::NODE_RANK, "fooSubset", 3);
186 stk::mesh::put_field_on_mesh(fooSubset, block_1, nullptr);
187
188 //add the new fields to the stk-io-broker, even though we know it isn't present
189 //in the restart database. This is to test the 'missing-fields' argument below.
190 stk::io::MeshField meshfieldFoo(&foo, "FOO");
191 meshfieldFoo.set_single_state(false);
192 meshfieldFoo.add_part(stk::topology::NODE_RANK, stkIo.meta_data().universal_part(),
193 stkIo.get_input_ioss_region().get()->get_node_blocks()[0]);
194 stkIo.add_input_field(meshfieldFoo);
195
196 stk::io::MeshField meshfieldFooSubset(&fooSubset, "FOOSUBSET");
197 meshfieldFooSubset.set_single_state(false);
198 const Ioss::ElementBlock& elem_block = *stkIo.get_input_ioss_region().get()->get_element_blocks()[0];
199 Ioss::ElementBlock& nonconst_elem_block = const_cast<Ioss::ElementBlock&>(elem_block);
200 meshfieldFooSubset.add_part(stk::topology::NODE_RANK, block_1, &nonconst_elem_block);
201 meshfieldFooSubset.add_subset(block_1);
202 stkIo.add_input_field(meshfieldFooSubset);
203
204 stk::io::set_field_role(foo, Ioss::Field::TRANSIENT);
205 stk::io::set_field_role(fooSubset, Ioss::Field::TRANSIENT);
206
207 //stkIo.add_all_mesh_fields_as_input_fields();
208
209 stkIo.populate_bulk_data();
210
211 std::vector<stk::mesh::Entity> nodes;
212 stk::mesh::get_entities(stkIo.bulk_data(), stk::topology::NODE_RANK, nodes);
213
214 std::vector<stk::io::MeshField> missingFields;
215 //+ Read restart data
216 stkIo.read_defined_input_fields(time, &missingFields);
217
218 EXPECT_EQ(3u, missingFields.size());
219 const stk::io::MeshField& missingField0 = missingFields[0];
220 std::string name = missingField0.db_name();
221 std::string statedFieldName = missingField0.field()->name();
222 EXPECT_EQ("FOO", name);
223 EXPECT_EQ("foo", statedFieldName);
224 const stk::io::MeshField& missingField1 = missingFields[1];
225 name = missingField1.db_name();
226 statedFieldName = missingField1.field()->name();
227 EXPECT_EQ("FOO", name);
228 EXPECT_EQ("foo_STKFS_N", statedFieldName);
229 const stk::io::MeshField& missingField2 = missingFields[2];
230 statedFieldName = missingField2.field()->name();
231 name = missingField2.db_name();
232 EXPECT_EQ("FOOSUBSET", name);
233 EXPECT_EQ("fooSubset_STKFS_N", statedFieldName);
234 }
235
236 // ============================================================
237 //+ VERIFICATION
238 {
239 Ioss::DatabaseIO *resultsDb = Ioss::IOFactory::create("exodus", results_name,
240 Ioss::READ_MODEL, communicator);
241 Ioss::Region results(resultsDb);
242 // Should be 5 steps on database...
243 EXPECT_EQ(results.get_property("state_count").get_int(), 5);
244 // Should be 1 nodal field on database named "disp";
245 Ioss::NodeBlock *nb = results.get_node_blocks()[0];
246 EXPECT_EQ(1u, nb->field_count(Ioss::Field::TRANSIENT));
247 EXPECT_TRUE(nb->field_exists("disp"));
248
249 // Iterate each step and verify that the correct data was written.
250 for (size_t step=0; step < 5; step++) {
251 double time = step;
252
253 double db_time = results.begin_state(step+1);
254 EXPECT_EQ(time, db_time);
255
256 std::vector<double> field_data;
257 nb->get_field_data("disp", field_data);
258 double expected = 10.0 * time;
259 for (size_t node = 0; node < field_data.size(); node++) {
260 EXPECT_EQ(field_data[node], expected);
261 }
262 results.end_state(step+1);
263 }
264 }
265
266 {
267 Ioss::DatabaseIO *restartDb = Ioss::IOFactory::create("exodus", restart_name,
268 Ioss::READ_MODEL, communicator);
269 Ioss::Region restart(restartDb);
270 // Should be 5 steps on database...
271 EXPECT_EQ(restart.get_property("state_count").get_int(), 5);
272 // Should be 2 nodal field on database named "disp" and "disp.N";
273 Ioss::NodeBlock *nb = restart.get_node_blocks()[0];
274 EXPECT_EQ(3u, nb->field_count(Ioss::Field::TRANSIENT));
275 EXPECT_TRUE(nb->field_exists("disp"));
276 EXPECT_TRUE(nb->field_exists("disp.N"));
277
278 // Iterate each step and verify that the correct data was written.
279 for (size_t step=0; step < 5; step++) {
280 double time = step;
281
282 double db_time = restart.begin_state(step+1);
283 EXPECT_EQ(time, db_time);
284
285 std::vector<double> field_data_n;
286 std::vector<double> field_data_np1;
287 nb->get_field_data("disp", field_data_np1);
288 nb->get_field_data("disp.N", field_data_n);
289 double expected = 10.0 * time;
290 for (size_t node = 0; node < field_data_n.size(); node++) {
291 EXPECT_EQ(field_data_np1[node], expected);
292 EXPECT_EQ(field_data_n[node], expected+0.1);
293 }
294 restart.end_state(step+1);
295 }
296 }
297
298 // ============================================================
299 // Cleanup
300 unlink(mesh_name.c_str());
301 unlink(results_name.c_str());
302 unlink(restart_name.c_str());
303 }
304 }