Debian Codegraph @

packages/stk/stk_unit_tests/stk_io/UnitTestMeshData.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 <stddef.h> // for size_t
36 #include <stdlib.h> // for rand, srand, RAND_MAX
37 #include <stk_io/IossBridge.hpp> // for is_part_io_part
38 #include <stk_io/StkMeshIoBroker.hpp> // for StkMeshIoBroker
39 #include <Ioss_ConcreteVariableType.h>
40 #include <stk_mesh/base/BulkData.hpp> // for BulkData
41 #include <stk_mesh/base/GetEntities.hpp> // for get_selected_entities
42 #include <stk_mesh/base/MetaData.hpp> // for MetaData
43 #include <gtest/gtest.h>
44 #include <string> // for string
45 #include <vector> // for vector, etc
46 #include "gtest/gtest.h" // for AssertHelper, ASSERT_TRUE
47 #include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc
48 #include "stk_mesh/base/Bucket.hpp" // for Bucket
49 #include "stk_mesh/base/Entity.hpp" // for Entity
50 #include "stk_mesh/base/Part.hpp" // for Part
51 #include "stk_mesh/base/Selector.hpp" // for Selector, operator&
52 #include "stk_mesh/base/Types.hpp" // for PartVector, BucketVector, etc
53 #include "stk_topology/topology.hpp" // for topology, etc
54 #include "stk_util/parallel/Parallel.hpp" // for ParallelMachine
55 #include "stk_mesh/baseImpl/MeshImplUtils.hpp"
56 #include "stk_mesh/base/FEMHelpers.hpp"
57 #include "stk_mesh/base/Field.hpp"
58 #include <stk_unit_test_utils/MeshFixture.hpp>
59 #include <stk_unit_test_utils/BuildMesh.hpp>
60
61 using stk::unit_test_util::build_mesh;
62 using stk::unit_test_util::build_mesh_no_simple_fields;
63
64 namespace {
65
66 void activate_entities(stk::io::StkMeshIoBroker &fixture,
67 stk::mesh::Part &active_part)
68 {
69 // Seed generator so multiple calls produce same result
70 srand(999999u);
71 stk::mesh::MetaData & meta = fixture.meta_data();
72 stk::mesh::BulkData &bulk = fixture.bulk_data();
73
74 stk::mesh::EntityRank elem_rank = stk::topology::ELEMENT_RANK;
75
76 stk::mesh::PartVector add_parts(1, &active_part);
77
78 bulk.modification_begin();
79 const stk::mesh::PartVector & all_parts = meta.get_parts();
80 for ( stk::mesh::PartVector::const_iterator
81 ip = all_parts.begin(); ip != all_parts.end(); ++ip ) {
82
83 stk::mesh::Part * const part = *ip;
84 if (stk::io::is_part_io_part(*part) && part->primary_entity_rank() == elem_rank) {
85 // Get all entities (elements) on this part...
86 std::vector<stk::mesh::Entity> entities;
87 stk::mesh::Selector select = meta.locally_owned_part() & *part;
88 stk::mesh::get_selected_entities(select, bulk.buckets(elem_rank), entities);
89 for (size_t i=0; i < entities.size(); i++) {
90 if (rand() > (RAND_MAX/4)*3)
91 bulk.change_entity_parts(entities[i], add_parts);
92 }
93 }
94 }
95 bulk.modification_end();
96 }
97
98 }
99
100 TEST( StkMeshIoBroker, iofixture_externalFile )
101 {
102 // A simple test for reading and writing an exodus file using the StkMeshIoBroker
103
104 stk::ParallelMachine pm = MPI_COMM_WORLD;
105
106 stk::io::StkMeshIoBroker fixture(pm);
107
108 std::string input_base_filename = "unit_test.g";
109
110 bool ok = false;
111 try {
112 // Initialize meta data from exodus file
113 fixture.add_mesh_database(input_base_filename, stk::io::READ_MESH);
114 fixture.create_input_mesh();
115 ok = true;
116
117 stk::mesh::MetaData & meta_data = fixture.meta_data();
118
119 // Commit meta_data
120 meta_data.commit();
121
122 // bulk_data initialize (from exodus file)
123 fixture.populate_bulk_data();
124
125 // exodus file creation
126 std::string output_base_filename = "unit_test_output.e";
127 size_t output_index = fixture.create_output_mesh(output_base_filename, stk::io::WRITE_RESULTS);
128
129 // process output
130 const double time_step = 0;
131 fixture.process_output_request(output_index, time_step);
132 }
133 catch(...) {
134 ASSERT_TRUE(ok);
135 }
136 // Since correctness can only be established by running SEACAS tools, correctness
137 // checking is left to the test XML.
138 }
139
140 TEST( StkMeshIoBroker, testModifyTopology )
141 {
142 stk::ParallelMachine comm = MPI_COMM_WORLD;
143 if (stk::parallel_machine_size(comm) == 1)
144 {
145 stk::io::StkMeshIoBroker fixture(comm);
146 std::string generated_mesh_spec = "generated:1x1x2";
147 fixture.add_mesh_database(generated_mesh_spec, stk::io::READ_MESH);
148 fixture.create_input_mesh();
149
150 stk::mesh::MetaData & meta_data = fixture.meta_data();
151 stk::mesh::Part & side_set_part = meta_data.declare_part_with_topology("Side Set Part",stk::topology::QUAD_4);
152 stk::io::put_io_part_attribute(side_set_part);
153
154 stk::mesh::Part & inactive_part = meta_data.declare_part("Inactive Part");
155
156 fixture.populate_bulk_data();
157
158 {
159 stk::mesh::BulkData & mesh = fixture.bulk_data();
160 mesh.modification_begin();
161 stk::mesh::Entity element1 = mesh.get_entity(stk::topology::ELEMENT_RANK, 1);
162 stk::mesh::Entity element2 = mesh.get_entity(stk::topology::ELEMENT_RANK, 2);
163
164 stk::mesh::PartVector add_parts = { &inactive_part };
165 mesh.change_entity_parts(element2, add_parts);
166
167 const int elem1_side_ordinal = 5;
168 stk::mesh::Entity side1 = mesh.declare_element_side(element1, elem1_side_ordinal, stk::mesh::PartVector{&side_set_part});
169
170 const int elem2_side_ordinal = 4;
171 stk::mesh::Entity side2 = mesh.declare_element_side(element2, elem2_side_ordinal, stk::mesh::PartVector{&side_set_part});
172 mesh.modification_end();
173
174 EXPECT_EQ(side1, side2);
175 }
176
177 const std::string output_base_filename = "StkMeshIoBroker.testModifyTopology.e";
178 size_t output_index = 0;
179 ASSERT_NO_THROW(output_index = fixture.create_output_mesh(output_base_filename, stk::io::WRITE_RESULTS));
180 stk::mesh::Selector active_selector = !inactive_part;
181 fixture.set_subset_selector(output_index,active_selector);
182 const double time_step = 0.1;
183 ASSERT_NO_THROW(fixture.process_output_request(output_index, time_step));
184
185 unlink(output_base_filename.c_str());
186 }
187 }
188
189 TEST( StkMeshIoBroker, active_only_externalFile )
190 {
191 // A simple test for reading and writing an exodus file using the StkMeshIoBroker.
192
193 stk::ParallelMachine pm = MPI_COMM_WORLD;
194 stk::io::StkMeshIoBroker fixture(pm);
195
196 std::string input_base_filename = "unit_test.g";
197
198 bool ok = false;
199 try {
200 // Initialize meta data from exodus file
201 fixture.add_mesh_database(input_base_filename, stk::io::READ_MESH);
202
203 fixture.create_input_mesh();
204 ok = true;
205 stk::mesh::MetaData & meta_data = fixture.meta_data();
206
207 // Add an "active" part...
208 stk::mesh::Part &active = meta_data.declare_part("active", stk::topology::ELEMENT_RANK);
209 meta_data.commit();
210
211 // bulk_data initialize (from exodus file)
212 fixture.populate_bulk_data();
213
214 // Put some entities into the "active" part...
215 // This will be used to test the I/O filtering via a selector...
216 activate_entities(fixture, active);
217
218 // exodus file creation
219 std::string output_base_filename = "unit_test_output_filtered.e";
220 size_t index = fixture.create_output_mesh( output_base_filename, stk::io::WRITE_RESULTS );
221
222 // Set the output filter on the mesh_data...
223 stk::mesh::Selector active_selector(active);
224 fixture.set_subset_selector(index, active_selector);
225
226 // process output
227 const double time_step = 0;
228 fixture.process_output_request(index, time_step);
229 }
230 catch(...) {
231 ASSERT_TRUE(ok);
232 }
233
234
235 // Since correctness can only be established by running SEACAS tools, correctness
236 // checking is left to the test XML.
237 }
238
239 TEST( StkMeshIoBroker, active_and_all_externalFile )
240 {
241 // A simple test for reading and writing two exodus files using the StkMeshIoBroker.
242 stk::ParallelMachine pm = MPI_COMM_WORLD;
243 int numProcs = stk::parallel_machine_size(pm);
244 if (numProcs != 1) {
245 return;
246 }
247 stk::io::StkMeshIoBroker fixture(pm);
248
249 std::string input_base_filename = "unit_test.g";
250
251 bool ok = false;
252 try {
253 fixture.add_mesh_database(input_base_filename, stk::io::READ_MESH);
254 fixture.create_input_mesh();
255 ok = true;
256 stk::mesh::MetaData & meta_data = fixture.meta_data();
257
258 // Add an "active" part...
259 stk::mesh::Part &active = meta_data.declare_part("active", stk::topology::ELEMENT_RANK);
260 meta_data.commit();
261
262 // bulk_data initialize (from exodus file)
263 fixture.populate_bulk_data();
264
265 // Put some entities into the "active" part...
266 // This will be used to test the I/O filtering via a selector...
267 activate_entities(fixture, active);
268
269 // exodus file creation
270 std::string filtered_output_base_filename = "unit_test_output_first_of_two.e";
271 std::string unfiltered_output_base_filename = "unit_test_output_second_of_two.e";
272 size_t filtered_index = fixture.create_output_mesh( filtered_output_base_filename, stk::io::WRITE_RESULTS );
273 size_t universal_index = fixture.create_output_mesh( unfiltered_output_base_filename, stk::io::WRITE_RESULTS );
274
275 // Set the output filter on the mesh_data...
276 // Only output the part declared above as "active"
277 stk::mesh::Selector active_selector(active);
278 fixture.set_subset_selector(filtered_index, active_selector);
279
280 // process output
281 double time_step = 0;
282 fixture.process_output_request(filtered_index, time_step);
283 fixture.process_output_request(universal_index, time_step);
284
285 ++time_step;
286
287 fixture.process_output_request(filtered_index, time_step);
288 fixture.process_output_request(universal_index, time_step);
289
290 ++time_step;
291
292 fixture.process_output_request(filtered_index, time_step);
293 fixture.process_output_request(universal_index, time_step);
294 }
295 catch(...) {
296 ASSERT_TRUE(ok);
297 }
298
299 // Since correctness can only be established by running SEACAS tools, correctness
300 // checking is left to the test XML.
301 }
302
303 TEST( StkMeshIoBroker, large_mesh_test_externalFile )
304 {
305 // A simple test for reading and writing two exodus files using the StkMeshIoBroker.
306 stk::ParallelMachine pm = MPI_COMM_WORLD;
307 int numProcs = stk::parallel_machine_size(pm);
308 if (numProcs != 1) {
309 return;
310 }
311 stk::io::StkMeshIoBroker fixture(pm);
312
313 std::string input_base_filename = "1mCube_20x20x20.g";
314
315 bool ok = false;
316 try {
317 // Initialize meta data from exodus file
318 fixture.add_mesh_database(input_base_filename, stk::io::READ_MESH);
319 fixture.create_input_mesh();
320 ok = true;
321
322 stk::mesh::MetaData & meta_data = fixture.meta_data();
323
324 // Commit
325 meta_data.commit();
326
327 // bulk_data initialize (from exodus file)
328 fixture.populate_bulk_data();
329 stk::mesh::BulkData &bulk_data = fixture.bulk_data();
330
331 const stk::mesh::BucketVector & element_buckets
332 = bulk_data.buckets( stk::topology::ELEMENT_RANK);
333
334 // iterate elements and check num nodal relations
335 for ( stk::mesh::BucketVector::const_iterator ib = element_buckets.begin() ;
336 ib != element_buckets.end() ; ++ib ) {
337 stk::mesh::Bucket & b = **ib ;
338 const int length = b.size();
339 for ( int k = 0 ; k < length ; ++k ) {
340 // get element
341 stk::mesh::Entity elem = b[k];
342 size_t num_elem_node_rels = bulk_data.count_valid_connectivity(elem, stk::topology::NODE_RANK);
343 EXPECT_EQ( 8u, num_elem_node_rels);
344 }
345 }
346 }
347 catch(...) {
348 ASSERT_TRUE(ok);
349 }
350 }
351
352 TEST(StkMeshIoBroker, removeIoPartAttribute)
353 {
354 if(stk::parallel_machine_size(MPI_COMM_WORLD) != 1) { return; }
355 const unsigned spatialDim = 3;
356 std::shared_ptr<stk::mesh::BulkData> bulk = build_mesh(spatialDim, MPI_COMM_WORLD);
357 stk::mesh::MetaData& meta = bulk->mesh_meta_data();
358
359 stk::io::fill_mesh("generated:1x1x1", *bulk);
360
361 for (stk::mesh::Part *part : meta.get_mesh_parts())
362 if ( stk::io::is_part_io_part(*part))
363 stk::io::remove_io_part_attribute(*part);
364 }
365
366 class StkIoFixture : public stk::unit_test_util::MeshFixture
367 {
368 protected:
369 void setup_mesh(const std::string & meshSpec,
370 stk::mesh::BulkData::AutomaticAuraOption auraOption,
371 unsigned initialBucketCapacity = stk::mesh::get_default_initial_bucket_capacity(),
372 unsigned maximumBucketCapacity = stk::mesh::get_default_maximum_bucket_capacity()) override
373 {
374 setup_empty_mesh(auraOption, initialBucketCapacity, maximumBucketCapacity);
375
376 stk::io::fill_mesh(meshSpec, get_bulk());
377 }
378 };
379
380 TEST_F(StkIoFixture, customCoordinateName)
381 {
382 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) return;
383
384 setup_mesh("generated:1x1x2", stk::mesh::BulkData::NO_AUTO_AURA);
385 const std::string meshName = "meshWithCoordinates.g";
386 stk::io::write_mesh(meshName, get_bulk());
387
388 const unsigned spatialDim = 3;
389 std::shared_ptr<stk::mesh::BulkData> bulk = build_mesh(spatialDim, MPI_COMM_WORLD, stk::mesh::BulkData::NO_AUTO_AURA);
390 stk::mesh::MetaData& meta = bulk->mesh_meta_data();
391
392 meta.set_coordinate_field_name("custom_coordinates");
393
394 stk::io::fill_mesh(meshName, *bulk);
395
396 const stk::mesh::FieldBase * coordField = meta.coordinate_field();
397 EXPECT_EQ("custom_coordinates", coordField->name());
398
399 unlink(meshName.c_str());
400 }
401
402 stk::mesh::Part& declare_elem_part(stk::mesh::MetaData& meta, const std::string& partName)
403 {
404 stk::mesh::Part& part = meta.declare_part(partName, stk::topology::ELEM_RANK);
405 stk::io::put_io_part_attribute(part);
406 return part;
407 }
408
409 Ioss::Field* create_ioss_field(const std::string& fieldName,
410 int numScalarComponentsPerField,
411 int numFieldCopiesPerEntity)
412 {
413 const Ioss::VariableType* symTensor = Ioss::VariableType::factory("sym_tensor_33", numFieldCopiesPerEntity);
414
415 return new Ioss::Field(fieldName, Ioss::Field::REAL, symTensor, Ioss::Field::TRANSIENT, numScalarComponentsPerField);
416 }
417
418 TEST(DeclareIossField, reRegisterWithDifferentNumCopies)
419 {
420 if(stk::parallel_machine_size(MPI_COMM_WORLD) != 1) { return; }
421 stk::mesh::MetaData meta(3);
422
423 stk::mesh::Part& myPart = declare_elem_part(meta, "myPart");
424 stk::mesh::Part& myOtherPart = declare_elem_part(meta, "myOtherPart");
425
426 Ioss::Sym_Tensor_33::factory();
427
428 int numScalarComponentsPerField = 6;
429 int numFieldCopiesPerEntity = 1;
430 std::string fieldName("left_stretch");
431
432 Ioss::Field* iossField1copy = create_ioss_field(fieldName, numScalarComponentsPerField, numFieldCopiesPerEntity);
433
434 const stk::mesh::FieldBase* stkField = stk::io::impl::declare_stk_field_internal(meta, stk::topology::ELEM_RANK, myPart,
435 *iossField1copy);
436
437 unsigned expectedMaxSize = numFieldCopiesPerEntity*numScalarComponentsPerField;
438 EXPECT_EQ(expectedMaxSize, stkField->max_size());
439
440 numFieldCopiesPerEntity = 9;
441
442 Ioss::Field* iossField9copies = create_ioss_field(fieldName, numScalarComponentsPerField, numFieldCopiesPerEntity);
443
444 stkField = stk::io::impl::declare_stk_field_internal(meta, stk::topology::ELEM_RANK, myOtherPart, *iossField9copies);
445
446 expectedMaxSize = numFieldCopiesPerEntity*numScalarComponentsPerField;
447 EXPECT_EQ(expectedMaxSize, stkField->max_size());
448
449 delete iossField1copy;
450 delete iossField9copies;
451 }