Debian Codegraph @

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