Debian Codegraph @

packages/stk/stk_doc_tests/stk_io/howToWriteOutputWithDTIO.cpp

raw
1
2 // Copyright 2002 - 2008, 2010, 2011 National Technology Engineering
3 // Solutions of Sandia, LLC (NTESS). Under the terms of Contract
4 // DE-NA0003525 with NTESS, the U.S. Government retains certain rights
5 // in this software.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are
9 // met:
10 //
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //
14 // * Redistributions in binary form must reproduce the above
15 // copyright notice, this list of conditions and the following
16 // disclaimer in the documentation and/or other materials provided
17 // with the distribution.
18 //
19 // * Neither the name of NTESS nor the names of its contributors
20 // may be used to endorse or promote products derived from this
21 // software without specific prior written permission.
22 //
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 //
35
36 #include "Ioss_DBUsage.h" // for DatabaseUsage::READ_MODEL
37 #include "Ioss_Field.h" // for Field, etc
38 #include "Ioss_ElementBlock.h" // for ElementBlock
39 #include "Ioss_Property.h" // for Property
40 #include "Ioss_Region.h" // for Region, ElementBlockContainer
41 #include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc
42 #include "stk_io/FillMesh.hpp"
43 #include "stk_io/WriteMesh.hpp"
44 #include "stk_mesh/base/MeshBuilder.hpp"
45 #include "stk_mesh/base/Entity.hpp" // for Entity
46 #include "stk_mesh/base/FieldBase.hpp" // for field_data
47 #include "stk_mesh/base/FieldBLAS.hpp"
48 #include "stk_topology/topology.hpp" // for topology, etc
49 #include <gtest/gtest.h> // for AssertHelper, EXPECT_EQ, etc
50 #include <ostream> // for basic_ostream::operator<<
51 #include <stddef.h> // for size_t
52 #include <stk_io/StkMeshIoBroker.hpp> // for StkMeshIoBroker
53 #include <stk_mesh/base/Field.hpp> // for Field
54 #include <stk_mesh/base/MetaData.hpp> // for MetaData, put_field
55 #include "stk_util/parallel/ParallelReduce.hpp"
56 #include <string> // for string
57 #include <unistd.h> // for unlink
58 #include <vector> // for vector
59 namespace Ioss { class DatabaseIO; }
60
61 namespace {
62
63 TEST(StkMeshIoBrokerHowTo, writeResultsWithDTIO)
64 {
65 std::string mesh_name = "input_mesh_example.e";
66 std::string results_name = "output.e";
67 MPI_Comm communicator = MPI_COMM_WORLD;
68
69 {
70 // ============================================================
71 //+ INITIALIZATION:
72 //+ Create a basic mesh with a hex block, 3 shell blocks, 3 nodesets, and 3 sidesets.
73 std::unique_ptr<stk::mesh::BulkData> mesh = stk::mesh::MeshBuilder(communicator).create();
74
75 const std::string generatedFileName = "generated:2x2x2|shell:xyz|nodeset:xyz|sideset:XYZ";
76 stk::io::fill_mesh(generatedFileName, *mesh);
77 stk::io::write_mesh(mesh_name, *mesh);
78 }
79
80 {
81 //-BEGIN
82 // ============================================================
83 //+ EXAMPLE:
84 //+ Read mesh data from the specified file.
85 stk::io::StkMeshIoBroker stkIo(communicator);
86
87 //+ Enable automatic IOSS control of dynamic topology IO (DTIO) to create a single group file
88 stkIo.enable_dynamic_topology(stk::io::FileOption::USE_DYNAMIC_TOPOLOGY_GROUP_FILE);
89
90 stkIo.add_mesh_database(mesh_name, stk::io::READ_MESH);
91
92 //+ Creates meta data; creates parts
93 stkIo.create_input_mesh();
94
95 //+ Declare a field
96 //+ NOTE: Fields must be declared before "populate_bulk_data()" is called
97 //+ since it commits the meta data.
98 const std::string fieldName = "density";
99 stk::mesh::Field<double> &field = stkIo.meta_data().declare_field<double>(stk::topology::ELEM_RANK, fieldName, 1);
100 stk::mesh::put_field_on_mesh(field, stkIo.meta_data().universal_part(), nullptr);
101
102 //+ commit the meta data and create the bulk data.
103 //+ populate the bulk data with data from the mesh file.
104 stkIo.populate_bulk_data();
105
106 // ============================================================
107 //+ Create results file. By default, all parts created from the input
108 //+ mesh will be written to the results output file.
109 size_t fh = stkIo.create_output_mesh(results_name, stk::io::WRITE_RESULTS);
110
111 //+ The field will be output to the results file with the default field name.
112 stkIo.add_field(fh, field); /*@\label{io:results:add_field}*/
113
114 //+ Iterate the application's execute loop nine times and output
115 //+ field data each iteration (mimicing time steps). The last eight
116 //+ "time steps" involve a dynamic topology change where an element is
117 //+ destroyed and would normally necessitate the creation of a new
118 //+ file. DTIO allows the automatic creation of a single database
119 //+ that contains all the files
120 for (int step=0; step < 9; step++) {
121 double time = step;
122
123 //simulate time-varying result field...
124 double value = 10.0 * time;
125 stk::mesh::field_fill(value, field);
126
127 if(step != 0) {
128 // Destroy an element
129 stk::mesh::BulkData& bulk = stkIo.bulk_data();
130
131 bulk.modification_begin();
132 stk::mesh::EntityId entityId = step;
133 stk::mesh::Entity entity = bulk.get_entity(stk::topology::ELEM_RANK, entityId);
134 EXPECT_TRUE(bulk.is_valid(entity));
135 EXPECT_TRUE(bulk.destroy_entity(entity));
136 bulk.modification_end();
137
138 // Notify STKIO that a mesh change occured
139 stkIo.set_topology_modification(fh, Ioss::TOPOLOGY_CREATEELEM);
140 }
141 //+ Output the field data calculated by the application.
142 stkIo.begin_output_step(fh, time);
143 stkIo.write_defined_output_fields(fh);
144 stkIo.end_output_step(fh);
145 }
146 //-END
147 }
148 // ============================================================
149 //+ VERIFICATION
150 {
151 std::shared_ptr<stk::mesh::BulkData> bulkPtr = stk::mesh::MeshBuilder(communicator)
152 .set_spatial_dimension(3).create();
153 stk::io::StkMeshIoBroker stkIo;
154
155 // Make sure IOSS retains empty element blocks ... final output has no HEX elements
156 stkIo.property_add(Ioss::Property("RETAIN_EMPTY_BLOCKS", "YES"));
157
158 stk::io::fill_mesh_preexisting(stkIo, results_name, *bulkPtr);
159
160 // Should be 9 mesh groups in database...one per output time step
161 constexpr int goldNumMeshGroups = 9;
162 EXPECT_EQ(goldNumMeshGroups, stkIo.num_mesh_groups());
163
164 for(int i=0; i<goldNumMeshGroups; i++) {
165 // Load each individual mesh group
166 // First one is actually a no-op since group 0 is loaded by default
167 EXPECT_TRUE(stkIo.load_mesh_group(i));
168
169 // One time step per mesh group was written
170 constexpr int goldNumTimeSteps = 1;
171 EXPECT_EQ(goldNumTimeSteps, stkIo.get_num_time_steps());
172
173 const auto& region = stkIo.get_input_ioss_region();
174 const auto& eBlocks = region->get_element_blocks();
175
176 // 1 HEX and 3 SHELL blocks
177 EXPECT_EQ(4u, eBlocks.size());
178
179 // Look at HEX block
180 Ioss::ElementBlock* eb = region->get_element_block("BLOCK_1");
181 EXPECT_TRUE(nullptr != eb);
182
183 int64_t localElementCount = eb->entity_count();
184 int64_t globalElementCount = stk::get_global_sum(communicator, localElementCount);
185
186 int64_t goldGlobalElementCount = 8 - i;
187 EXPECT_EQ(goldGlobalElementCount, globalElementCount);
188
189 // Should be 1 element field on database named "density";
190 EXPECT_EQ(1u, eb->field_count(Ioss::Field::TRANSIENT));
191 EXPECT_TRUE(eb->field_exists("density"));
192
193 // Verify that the correct data was written for each step per mesh group.
194 double time = i;
195
196 double db_time = region->begin_state(1);
197 EXPECT_EQ(time, db_time);
198
199 std::vector<double> field_data;
200 eb->get_field_data("density", field_data);
201 const double expectedValue = 10.0 * time;
202 for (size_t elem = 0; elem < field_data.size(); elem++) {
203 EXPECT_NEAR(expectedValue, field_data[elem], 1.0e-6);
204 }
205 region->end_state(1);
206 }
207 }
208
209 // ============================================================
210 // Cleanup
211 unlink(mesh_name.c_str());
212 unlink(results_name.c_str());
213 }
214
215 }