Debian Codegraph @

packages/stk/stk_unit_tests/stk_io/UnitTestReadWriteFacesForFieldIO.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 <stk_io/FillMesh.hpp>
35 #include "UnitTestReadWriteFaces.hpp"
36 #include "UnitTestReadWriteUtils.hpp"
37 #include <stk_unit_test_utils/BuildMesh.hpp>
38
39 using stk::unit_test_util::build_mesh;
40 using stk::unit_test_util::build_mesh_no_simple_fields;
41
42 class StkFaceIoTestForResultOutput : public StkFaceIoTest
43 {
44 public:
45
46 StkFaceIoTestForResultOutput() : StkFaceIoTest() { }
47
48 void setup_mesh_with_face_field(unsigned numBlocks, unsigned numStates = 1)
49 {
50 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
51 stk::mesh::Field<double>& faceField = create_field<double>(get_meta(), stk::topology::FACE_RANK, faceFieldName, numStates);
52 setup_face_mesh(numBlocks);
53
54 stk::mesh::EntityVector faces;
55 stk::mesh::get_selected_entities(get_meta().universal_part(), get_bulk().buckets(stk::topology::FACE_RANK), faces);
56
57 initialize_face_field(&faceField, faces);
58 }
59
60 void initialize_face_field(stk::mesh::FieldBase* faceField, stk::mesh::EntityVector& faces)
61 {
62 for(auto face : faces) {
63 for(unsigned i = 0; i < faceField->number_of_states(); i++) {
64 stk::mesh::FieldState state = stk::mesh::FieldState(i+stk::mesh::StateNP1);
65 stk::mesh::FieldBase* field = faceField->field_state(state);
66 double* data = reinterpret_cast<double*>(stk::mesh::field_data(*field, face));
67
68 *data = get_bulk().identifier(face) + i*100;
69 }
70 }
71 }
72
73 void write_mesh(stk::io::DatabasePurpose purpose)
74 {
75 stk::mesh::FieldBase* faceField = get_meta().get_field(stk::topology::FACE_RANK, faceFieldName);
76 stkIoOutput.set_bulk_data(get_bulk());
77 size_t outputFileIndex = stkIoOutput.create_output_mesh(fileName, purpose);
78
79 ASSERT_TRUE(faceField != nullptr);
80 stkIoOutput.add_field(outputFileIndex, *faceField);
81 stkIoOutput.write_output_mesh(outputFileIndex);
82 stkIoOutput.begin_output_step(outputFileIndex, 0.0);
83 stkIoOutput.write_defined_output_fields(outputFileIndex);
84 stkIoOutput.end_output_step(outputFileIndex);
85 stkIoOutput.flush_output();
86 }
87
88 void test_output_mesh_and_face_field()
89 {
90 std::shared_ptr<stk::mesh::BulkData> bulk = build_mesh(MPI_COMM_WORLD);
91
92 load_output_mesh(*bulk);
93 test_output_mesh(*bulk);
94 test_face_field(*bulk);
95 }
96
97 void load_output_mesh(stk::mesh::BulkData& bulk) override
98 {
99 stk::io::fill_mesh_with_fields(fileName, stkIoInput, bulk);
100 int numSteps = stkIoInput.get_num_time_steps();
101 EXPECT_EQ(1, numSteps);
102 }
103
104 virtual void test_face_field(const stk::mesh::BulkData& bulk)
105 {
106 stk::mesh::FieldBase* field = bulk.mesh_meta_data().get_field(stk::topology::FACE_RANK, faceFieldName);
107 ASSERT_TRUE(field != nullptr);
108
109 std::vector<const stk::mesh::FieldBase*> fieldVector{field};
110
111 stk::mesh::communicate_field_data(bulk, fieldVector);
112
113 stk::mesh::EntityVector faces;
114 stk::mesh::get_entities(bulk, stk::topology::FACE_RANK, faces);
115
116 for(stk::mesh::Entity& face : faces) {
117 double* data = reinterpret_cast<double*>(stk::mesh::field_data(*field, face));
118 EXPECT_EQ((double)bulk.identifier(face), *data);
119 }
120 }
121
122 template <typename T>
123 stk::mesh::Field<T> & create_field(stk::mesh::MetaData& meta, stk::topology::rank_t rank, const std::string & name,
124 unsigned numStates = 1, unsigned numComponent = 1)
125 {
126 const std::vector<T> init(numComponent, 1);
127 stk::mesh::Field<T> & field = meta.declare_field<T>(rank, name, numStates);
128 stk::mesh::put_field_on_mesh(field, meta.universal_part(), numComponent, init.data());
129 return field;
130 }
131
132 protected:
133 std::string faceFieldName = "faceField";
134 };
135
136 class StkFaceIoTestForRestart : public StkFaceIoTestForResultOutput
137 {
138 public:
139 StkFaceIoTestForRestart() : StkFaceIoTestForResultOutput() { }
140
141 void load_output_mesh(stk::mesh::BulkData& bulk) override
142 {
143 stk::io::fill_mesh_preexisting(stkIoInput, fileName, bulk, stk::io::READ_RESTART);
144 int numSteps = stkIoInput.get_num_time_steps();
145 EXPECT_EQ(1, numSteps);
146
147 stk::mesh::FieldBase* faceField = bulk.mesh_meta_data().get_field(stk::topology::FACE_RANK, faceFieldName);
148 stk::io::MeshField mf(faceField, faceField->name());
149 mf.set_read_time(0.0);
150
151 stkIoInput.read_input_field(mf);
152 bulk.update_field_data_states(faceField);
153 }
154
155 void test_output_mesh_and_face_field(unsigned numStates)
156 {
157 std::shared_ptr<stk::mesh::BulkData> bulk = build_mesh(MPI_COMM_WORLD, stk::mesh::BulkData::NO_AUTO_AURA);
158 stk::mesh::MetaData& meta = bulk->mesh_meta_data();
159
160 create_field<double>(meta, stk::topology::FACE_RANK, faceFieldName, numStates);
161 load_output_mesh(*bulk);
162 test_output_mesh(*bulk);
163 test_face_field(*bulk);
164 }
165
166 void test_selected_face_field(const stk::mesh::BulkData& bulk, stk::mesh::Selector selector)
167 {
168 const stk::mesh::MetaData& meta = bulk.mesh_meta_data();
169 stk::mesh::FieldBase* field = meta.get_field(stk::topology::FACE_RANK, faceFieldName);
170 ASSERT_TRUE(field != nullptr);
171
172 stk::mesh::EntityVector faces;
173 stk::mesh::get_selected_entities(selector, bulk.buckets(stk::topology::EDGE_RANK), faces);
174
175 stk::mesh::FieldBase* fieldStateN = field->field_state(stk::mesh::StateN);
176 stk::mesh::FieldBase* fieldStateNM1 = field->field_state(stk::mesh::StateNM1);
177
178 for(auto face : faces) {
179 double* dataN = reinterpret_cast<double*>(stk::mesh::field_data(*fieldStateN, face));
180 double* dataNM1 = reinterpret_cast<double*>(stk::mesh::field_data(*fieldStateNM1, face));
181
182 double expectedDataNValue = bulk.identifier(face);
183 double expectedDataNM1Value = bulk.identifier(face) + 100;
184 EXPECT_EQ(expectedDataNValue, *dataN);
185 EXPECT_EQ(expectedDataNM1Value, *dataNM1);
186 }
187 }
188
189 void test_face_field(const stk::mesh::BulkData& bulk) override
190 {
191 const stk::mesh::MetaData& meta = bulk.mesh_meta_data();
192 test_selected_face_field(bulk, meta.locally_owned_part());
193
194 stk::mesh::FieldBase* field = meta.get_field(stk::topology::FACE_RANK, faceFieldName);
195 std::vector<const stk::mesh::FieldBase*> fieldVector;
196 for(unsigned i = 0; i < field->number_of_states(); i++) {
197 stk::mesh::FieldState state = (stk::mesh::FieldState)i;
198 fieldVector.push_back(field->field_state(state));
199 }
200 stk::mesh::communicate_field_data(bulk, fieldVector);
201
202 test_selected_face_field(bulk, meta.universal_part() & !meta.locally_owned_part());
203 }
204 };
205
206 TEST_F(StkFaceIoTestForResultOutput, SerialWriteMeshWithFaceField)
207 {
208 if(stk::parallel_machine_size(MPI_COMM_WORLD) != 1) { return; }
209
210 set_file_name("SerialWriteMeshWithFaceField.exo");
211
212 io_test_utils::ExpectedValues expectedTestValues;
213 expectedTestValues.numEdgesPerProc = std::vector<unsigned>{0};
214 expectedTestValues.numLocalEdgesPerProc = std::vector<unsigned>{0};
215 expectedTestValues.numFacesPerProc = std::vector<unsigned>{1};
216 expectedTestValues.numLocalFacesPerProc = std::vector<unsigned>{1};
217 expectedTestValues.numConnectedEdges = 0;
218 expectedTestValues.globalEdgeCount = 0;
219 expectedTestValues.globalElemCount = 2;
220
221 unsigned numBlocks = 2;
222 setup_mesh_with_face_field(numBlocks);
223
224 set_expected_values(expectedTestValues);
225 test_faces(get_bulk());
226 write_mesh(stk::io::WRITE_RESULTS);
227
228 test_output_mesh_and_face_field();
229 }
230
231 TEST_F(StkFaceIoTestForResultOutput, ParallelWriteMeshWithFaceField)
232 {
233 if(stk::parallel_machine_size(MPI_COMM_WORLD) != 2) { return; }
234
235 io_test_utils::ExpectedValues expectedTestValues;
236 expectedTestValues.numEdgesPerProc = std::vector<unsigned>{0, 0};
237 expectedTestValues.numLocalEdgesPerProc = std::vector<unsigned>{0, 0};
238 expectedTestValues.numFacesPerProc = std::vector<unsigned>{1, 1};
239 expectedTestValues.numLocalFacesPerProc = std::vector<unsigned>{1, 0};
240 expectedTestValues.numConnectedEdges = 0;
241 expectedTestValues.globalEdgeCount = 0;
242 expectedTestValues.globalElemCount = 2;
243
244 unsigned numBlocks = 2;
245 setup_mesh_with_face_field(numBlocks);
246
247 set_expected_values(expectedTestValues);
248 test_faces(get_bulk());
249 write_mesh(stk::io::WRITE_RESULTS);
250
251 test_output_mesh_and_face_field();
252 }
253
254 TEST_F(StkFaceIoTestForRestart, SerialWriteMeshWithFaceField)
255 {
256 if(stk::parallel_machine_size(MPI_COMM_WORLD) != 1) { return; }
257
258 io_test_utils::ExpectedValues expectedTestValues;
259 expectedTestValues.numEdgesPerProc = std::vector<unsigned>{0};
260 expectedTestValues.numLocalEdgesPerProc = std::vector<unsigned>{0};
261 expectedTestValues.numFacesPerProc = std::vector<unsigned>{1};
262 expectedTestValues.numLocalFacesPerProc = std::vector<unsigned>{1};
263 expectedTestValues.numConnectedEdges = 0;
264 expectedTestValues.globalEdgeCount = 0;
265 expectedTestValues.globalElemCount = 2;
266
267 unsigned numStates = 3;
268 unsigned numBlocks = 2;
269
270 setup_mesh_with_face_field(numBlocks, numStates);
271
272 set_expected_values(expectedTestValues);
273 test_faces(get_bulk());
274 write_mesh(stk::io::WRITE_RESTART);
275
276 test_output_mesh_and_face_field(numStates);
277 }
278
279 TEST_F(StkFaceIoTestForRestart, ParallelWriteMeshWithFaceField)
280 {
281 if(stk::parallel_machine_size(MPI_COMM_WORLD) != 3) { return; }
282
283 io_test_utils::ExpectedValues expectedTestValues;
284 expectedTestValues.numEdgesPerProc = std::vector<unsigned>{0, 0, 0};
285 expectedTestValues.numLocalEdgesPerProc = std::vector<unsigned>{0, 0, 0};
286 expectedTestValues.numFacesPerProc = std::vector<unsigned>{1, 2, 1};
287 expectedTestValues.numLocalFacesPerProc = std::vector<unsigned>{1, 1, 0};
288 expectedTestValues.numConnectedEdges = 0;
289 expectedTestValues.globalEdgeCount = 0;
290 expectedTestValues.globalElemCount = 3;
291
292 unsigned numStates = 3;
293 unsigned numBlocks = 3;
294
295 setup_mesh_with_face_field(numBlocks, numStates);
296
297 set_expected_values(expectedTestValues);
298 test_faces(get_bulk());
299 write_mesh(stk::io::WRITE_RESTART);
300
301 test_output_mesh_and_face_field(numStates);
302 }