Debian Codegraph @

packages/stk/stk_unit_tests/stk_io/UnitTestDynamicTopology.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 #include "gtest/gtest.h" // for AssertHelper, ASSERT_TRUE
35
36 #include <stddef.h> // for size_t
37 #include <string> // for string
38 #include <vector> // for vector, etc
39 #include <numeric>
40 #include <algorithm>
41 #include <fstream>
42 #include <functional>
43 #include <iostream>
44 #include <unistd.h> // for unlink
45
46 #include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc
47 #include <stk_io/IossBridge.hpp> // for is_part_io_part
48 #include <stk_io/StkMeshIoBroker.hpp> // for StkMeshIoBroker
49 #include <stk_io/FillMesh.hpp>
50 #include <stk_io/WriteMesh.hpp>
51
52 #include <stk_mesh/base/BulkData.hpp> // for BulkData
53 #include <stk_mesh/base/GetEntities.hpp> // for get_selected_entities
54 #include <stk_mesh/base/MetaData.hpp> // for MetaData
55 #include "stk_mesh/base/Entity.hpp" // for Entity
56 #include "stk_mesh/base/Part.hpp" // for Part
57 #include "stk_mesh/base/Selector.hpp" // for Selector, operator&
58 #include "stk_mesh/base/Types.hpp" // for PartVector, BucketVector, etc
59 #include "stk_mesh/base/FEMHelpers.hpp"
60 #include "stk_mesh/base/Field.hpp"
61
62 #include "stk_topology/topology.hpp" // for topology, etc
63
64 #include "stk_util/parallel/Parallel.hpp" // for ParallelMachine
65 #include <stk_unit_test_utils/GetMeshSpec.hpp>
66 #include <stk_unit_test_utils/BuildMesh.hpp>
67 #include <stk_unit_test_utils/TextMesh.hpp>
68
69 #include <Ionit_Initializer.h>
70
71 namespace {
72
73 auto setup_mesh(const std::string & meshSpec,
74 stk::ParallelMachine comm,
75 stk::mesh::BulkData::AutomaticAuraOption auraOption = stk::mesh::BulkData::NO_AUTO_AURA,
76 unsigned spatialDim = 3,
77 unsigned initialBucketCapacity = stk::mesh::get_default_initial_bucket_capacity(),
78 unsigned maximumBucketCapacity = stk::mesh::get_default_maximum_bucket_capacity())
79 {
80 stk::mesh::MeshBuilder builder(comm);
81 builder.set_spatial_dimension(spatialDim);
82 builder.set_aura_option(auraOption);
83 builder.set_initial_bucket_capacity(initialBucketCapacity);
84 builder.set_maximum_bucket_capacity(maximumBucketCapacity);
85
86 std::shared_ptr<stk::mesh::BulkData> bulk = builder.create();
87
88 stk::mesh::Field<int> & field = bulk->mesh_meta_data().declare_field<int>(stk::topology::NODE_RANK, "nodal_field");
89 stk::io::set_field_role(field, Ioss::Field::TRANSIENT);
90 stk::mesh::put_field_on_mesh(field, bulk->mesh_meta_data().universal_part(), nullptr);
91
92 stk::io::fill_mesh("textmesh:"+meshSpec, *bulk);
93
94 return bulk;
95 }
96
97 std::string get_output_filename(const std::string &fileBase, const int modCount = 1)
98 {
99 Ioss::ParallelUtils util(Ioss::ParallelUtils::comm_world());
100 std::string file = fileBase;
101
102 if(modCount > 1) {
103 std::ostringstream oss;
104 oss << fileBase;
105 oss << "-s" << std::setw(4) << std::setfill('0') << modCount;
106 file = oss.str();
107 }
108
109 return file;
110 }
111
112 std::string get_decoded_output_filename(const std::string &fileBase, const int modCount = 1)
113 {
114 Ioss::ParallelUtils util(Ioss::ParallelUtils::comm_world());
115 std::string file = get_output_filename(fileBase, modCount);
116 return Ioss::Utils::decode_filename(file, util.parallel_rank(), util.parallel_size());
117 }
118
119 void cleanup_files(const std::string &outFile, const int numOutputs = 1)
120 {
121 for(int i=1; i<=numOutputs; i++) {
122 std::string file = get_decoded_output_filename(outFile, i);
123 unlink(file.c_str());
124 }
125 }
126
127 void setup_dynamic_topology(stk::ParallelMachine comm,
128 const std::string& outFile,
129 const int numSteps,
130 const stk::io::FileOption fileOption)
131 {
132 int numBlocks = stk::parallel_machine_size(comm);
133
134 cleanup_files(outFile, numSteps);
135
136 std::vector<double> coords = stk::unit_test_util::get_many_block_coordinates(numBlocks);
137 std::string meshDesc = stk::unit_test_util::get_many_block_mesh_desc(numBlocks, stk::parallel_machine_size(comm));
138 std::string fullMeshDesc = stk::unit_test_util::get_full_text_mesh_desc(meshDesc, coords);
139 std::shared_ptr<stk::mesh::BulkData> outBulk = setup_mesh(fullMeshDesc, comm);
140
141 stk::io::StkMeshIoBroker outBroker;
142 outBroker.set_bulk_data(outBulk);
143 outBroker.enable_dynamic_topology(fileOption);
144
145 size_t outputFileIndex = outBroker.create_output_mesh(outFile, stk::io::WRITE_RESULTS);
146 EXPECT_EQ(Ioss::TOPOLOGY_SAME, outBroker.get_topology_modification(outputFileIndex));
147
148 const stk::mesh::FieldVector fields = outBulk->mesh_meta_data().get_fields();
149 for(stk::mesh::FieldBase* field : fields)
150 {
151 const Ioss::Field::RoleType* fieldRole = stk::io::get_field_role(*field);
152 if(fieldRole == nullptr || *fieldRole == Ioss::Field::TRANSIENT)
153 outBroker.add_field(outputFileIndex, *field);
154 }
155
156 outBroker.write_output_mesh(outputFileIndex);
157
158 for(int i=0; i<numSteps; i++) {
159 double time = i;
160
161 outBroker.begin_output_step(outputFileIndex, time);
162 outBroker.write_defined_output_fields(outputFileIndex);
163 outBroker.end_output_step(outputFileIndex);
164 EXPECT_EQ(Ioss::TOPOLOGY_SAME, outBroker.get_topology_modification(outputFileIndex));
165 outBroker.set_topology_modification(outputFileIndex, Ioss::TOPOLOGY_UNKNOWN);
166 }
167
168 outBroker.close_output_mesh(outputFileIndex);
169 }
170
171 void test_legacy_file(stk::ParallelMachine comm,
172 const std::string& outFile,
173 const int numSteps)
174 {
175 stk::mesh::MeshBuilder builder(comm);
176 std::shared_ptr<stk::mesh::BulkData> inBulk = builder.create();
177
178 stk::io::StkMeshIoBroker inBroker;
179 stk::io::fill_mesh_with_fields(outFile, inBroker, *inBulk, stk::io::READ_MESH);
180
181 EXPECT_EQ(0, inBroker.num_mesh_groups());
182
183 for(int i=1; i<=numSteps; i++) {
184 double goldTime = i-1;
185 double time = inBroker.read_defined_input_fields(i);
186 EXPECT_NEAR(goldTime, time, 1.0e-6);
187 }
188
189 cleanup_files(outFile);
190 }
191
192 TEST(StkIoDynamicTopology, legacyOutputFileBehavior)
193 {
194 stk::ParallelMachine comm = MPI_COMM_WORLD;
195 std::string outFile("legacyOutputFile.g");
196
197 int numSteps = 5;
198 setup_dynamic_topology(comm, outFile, numSteps, stk::io::FileOption::NO_DYNAMIC_TOPOLOGY_FILE_CONTROL);
199 test_legacy_file(comm, outFile, numSteps);
200 }
201
202 void test_multi_file(stk::ParallelMachine comm,
203 const std::string& outFile,
204 const int numSteps)
205 {
206 for(int i=1; i<=numSteps; i++) {
207 std::string inFile = get_output_filename(outFile, i);
208
209 double goldTime = i-1;
210
211 stk::mesh::MeshBuilder builder(comm);
212 std::shared_ptr<stk::mesh::BulkData> inBulk = builder.create();
213
214 stk::io::StkMeshIoBroker inBroker;
215 stk::io::fill_mesh_with_fields(inFile, inBroker, *inBulk, stk::io::READ_MESH);
216
217 EXPECT_EQ(0, inBroker.num_mesh_groups());
218
219 int stepsPerFile = 1;
220 double time = inBroker.read_defined_input_fields(stepsPerFile);
221 EXPECT_NEAR(goldTime, time, 1.0e-6);
222 }
223
224 cleanup_files(outFile, numSteps);
225 }
226
227 TEST(StkIoDynamicTopology, multiFileOutputFileBehavior)
228 {
229 stk::ParallelMachine comm = MPI_COMM_WORLD;
230 std::string outFile("multiFileOutputFile.g");
231
232 int numSteps = 5;
233 setup_dynamic_topology(comm, outFile, numSteps, stk::io::FileOption::USE_DYNAMIC_TOPOLOGY_MULTI_FILE);
234 test_multi_file(comm, outFile, numSteps);
235 }
236
237 void test_dynamic_file(stk::ParallelMachine comm,
238 const std::string& outFile,
239 const int numSteps)
240 {
241 stk::mesh::MeshBuilder builder(comm);
242 std::shared_ptr<stk::mesh::BulkData> inBulk = builder.create();
243
244 stk::io::StkMeshIoBroker inBroker;
245 stk::io::fill_mesh_with_fields(outFile, inBroker, *inBulk, stk::io::READ_MESH);
246
247 EXPECT_EQ(numSteps, inBroker.num_mesh_groups());
248 std::vector<std::string> names = inBroker.mesh_group_names();
249
250 for(int i=0; i<numSteps; i++) {
251 EXPECT_TRUE(inBroker.load_mesh_group(names[i]));
252
253 int stepsPerFile = 1;
254 double goldTime = i;
255 double time = inBroker.read_defined_input_fields(stepsPerFile);
256 EXPECT_NEAR(goldTime, time, 1.0e-6);
257 }
258
259 cleanup_files(outFile);
260 }
261
262 TEST(StkIoDynamicTopology, dynamicOutputFileBehavior)
263 {
264 stk::ParallelMachine comm = MPI_COMM_WORLD;
265 std::string outFile("dynamicOutputFile.g");
266
267 int numSteps = 5;
268 setup_dynamic_topology(comm, outFile, numSteps, stk::io::FileOption::USE_DYNAMIC_TOPOLOGY_GROUP_FILE);
269 test_dynamic_file(comm, outFile, numSteps);
270 }
271
272 }