Debian Codegraph @

packages/stk/stk_unit_tests/stk_tools/mesh_clone/UnitTestMeshClone.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_unit_test_utils/MeshFixture.hpp>
35 #include <stk_mesh/base/Field.hpp>
36 #include <stk_mesh/base/FieldBase.hpp>
37 #include <stk_mesh/base/Selector.hpp>
38 #include <stk_mesh/base/BulkData.hpp>
39 #include <stk_mesh/base/GetEntities.hpp>
40 #include <stk_mesh/base/MetaData.hpp>
41 #include <stk_tools/mesh_clone/MeshClone.hpp>
42 #include <stk_tools/mesh_clone/MeshCloneUtils.hpp>
43 #include <stk_io/IossBridge.hpp>
44 #include <stk_util/util/ReportHandler.hpp>
45 #include <stk_unit_test_utils/BuildMesh.hpp>
46
47 namespace
48 {
49 using stk::unit_test_util::build_mesh;
50
51 class NoDeleteAttribute {
52 public:
53 NoDeleteAttribute()
54 : m_value(123)
55 {}
56 int value() const { return m_value; }
57
58 private:
59 int m_value;
60 };
61
62 class WithDeleteAttribute {
63 public:
64 WithDeleteAttribute()
65 : m_value(456)
66 {}
67 int value() const { return m_value; }
68
69 private:
70 int m_value;
71 };
72
73 void expect_equal_parts(const stk::mesh::Part &oldPart, const stk::mesh::Part &newPart)
74 {
75 EXPECT_EQ(oldPart.name(), newPart.name());
76 EXPECT_EQ(oldPart.id(), newPart.id()) << oldPart.name();
77 EXPECT_EQ(oldPart.topology(), newPart.topology()) << oldPart.name();
78 EXPECT_EQ(oldPart.primary_entity_rank(), newPart.primary_entity_rank()) << oldPart.name();
79 EXPECT_EQ(oldPart.force_no_induce(), newPart.force_no_induce());
80
81 EXPECT_EQ(stk::io::is_part_io_part(oldPart), stk::io::is_part_io_part(newPart));
82 if (oldPart.attribute<NoDeleteAttribute>()) {
83 EXPECT_EQ(oldPart.attribute<NoDeleteAttribute>()->value(), newPart.attribute<NoDeleteAttribute>()->value());
84 }
85 if (oldPart.attribute<WithDeleteAttribute>()) {
86 EXPECT_EQ(oldPart.attribute<WithDeleteAttribute>()->value(), newPart.attribute<WithDeleteAttribute>()->value());
87 }
88
89 const stk::mesh::FieldBase *oldDistFactField = stk::io::get_distribution_factor_field(oldPart);
90 if (oldDistFactField != nullptr) {
91 const stk::mesh::FieldBase *newDistFactField = stk::io::get_distribution_factor_field(newPart);
92 ASSERT_TRUE(newDistFactField != nullptr);
93 EXPECT_EQ(oldDistFactField->mesh_meta_data_ordinal(), newDistFactField->mesh_meta_data_ordinal());
94 EXPECT_NE(oldDistFactField, newDistFactField) << "Distribution factor fields must be different for different meshes";
95 }
96
97 stk::mesh::OrdinalVector oldSupersets = stk::tools::get_part_supersets(oldPart);
98 stk::mesh::OrdinalVector newSupersets = stk::tools::get_part_supersets(newPart);
99 EXPECT_TRUE(oldSupersets == newSupersets) << oldPart.name();
100 }
101
102 void expect_all_parts_equal(stk::mesh::MetaData &oldMeta, stk::mesh::MetaData &newMeta)
103 {
104 const stk::mesh::PartVector &oldParts = oldMeta.get_parts();
105 const stk::mesh::PartVector &newParts = newMeta.get_parts();
106 ASSERT_EQ(oldParts.size(), newParts.size());
107 for(size_t i=0; i<oldParts.size(); i++)
108 expect_equal_parts(*oldParts[i], *newParts[i]);
109 }
110
111 void expect_part_pointers_in_new_meta(stk::mesh::MetaData &newMeta, const stk::mesh::FieldRestriction &newRestriction)
112 {
113 stk::mesh::PartVector selectorParts;
114 newRestriction.selector().get_parts(selectorParts);
115 for(stk::mesh::Part *part : selectorParts)
116 {
117 stk::mesh::Part &metaPart = newMeta.get_part(part->mesh_meta_data_ordinal());
118 EXPECT_EQ(&metaPart, part);
119 }
120 }
121
122 void expect_equal_selectors(stk::mesh::Selector oldSel, stk::mesh::Selector newSel)
123 {
124 std::ostringstream oldS;
125 oldS << oldSel;
126 std::ostringstream newS;
127 newS << newSel;
128 EXPECT_EQ(oldS.str(), newS.str());
129 }
130
131 void expect_equal_field_restrictions(stk::mesh::MetaData &newMeta, stk::mesh::FieldBase &oldField, stk::mesh::FieldBase &newField)
132 {
133 const stk::mesh::FieldRestrictionVector &oldRestrictions = oldField.restrictions();
134 const stk::mesh::FieldRestrictionVector &newRestrictions = newField.restrictions();
135 ASSERT_EQ(oldRestrictions.size(), newRestrictions.size());
136 for(unsigned j=0; j<oldRestrictions.size(); j++)
137 {
138 expect_equal_selectors(oldRestrictions[j].selector(), newRestrictions[j].selector());
139 expect_part_pointers_in_new_meta(newMeta, newRestrictions[j]);
140
141 EXPECT_EQ(oldRestrictions[j].num_scalars_per_entity(), newRestrictions[j].num_scalars_per_entity());
142 EXPECT_EQ(oldRestrictions[j].dimension(), newRestrictions[j].dimension());
143 }
144 }
145
146 void expect_equal_field_initial_values(stk::mesh::FieldBase &oldField, stk::mesh::FieldBase &newField)
147 {
148 ASSERT_EQ(oldField.get_initial_value_num_bytes(), newField.get_initial_value_num_bytes());
149 const char *oldInitValPtr = static_cast<const char *>(oldField.get_initial_value());
150 const char *newInitValPtr = static_cast<const char *>(newField.get_initial_value());
151 for(unsigned j=0; j<oldField.get_initial_value_num_bytes(); j++)
152 EXPECT_EQ(oldInitValPtr[j], newInitValPtr[j]);
153 }
154
155 void expect_equal_data_traits(const stk::mesh::DataTraits &oldTraits, const stk::mesh::DataTraits &newTraits)
156 {
157 EXPECT_EQ(oldTraits.type_info, newTraits.type_info);
158 EXPECT_EQ(oldTraits.size_of, newTraits.size_of);
159 EXPECT_EQ(oldTraits.is_void, newTraits.is_void);
160 EXPECT_EQ(oldTraits.is_integral, newTraits.is_integral);
161 EXPECT_EQ(oldTraits.is_floating_point, newTraits.is_floating_point);
162 EXPECT_EQ(oldTraits.is_array, newTraits.is_array);
163 EXPECT_EQ(oldTraits.is_pointer, newTraits.is_pointer);
164 EXPECT_EQ(oldTraits.is_enum, newTraits.is_enum);
165 EXPECT_EQ(oldTraits.is_class, newTraits.is_class);
166 EXPECT_EQ(oldTraits.is_pod, newTraits.is_pod);
167 EXPECT_EQ(oldTraits.is_signed, newTraits.is_signed);
168 EXPECT_EQ(oldTraits.is_unsigned, newTraits.is_unsigned);
169 EXPECT_EQ(oldTraits.alignment_of, newTraits.alignment_of);
170 EXPECT_EQ(oldTraits.stride_of, newTraits.stride_of);
171 EXPECT_EQ(oldTraits.name, newTraits.name);
172 }
173
174 void expect_equal_fields(stk::mesh::MetaData &newMeta, stk::mesh::FieldBase &oldField, stk::mesh::FieldBase &newField)
175 {
176 EXPECT_EQ(oldField.mesh_meta_data_ordinal(), newField.mesh_meta_data_ordinal());
177 EXPECT_EQ(oldField.name(), newField.name());
178 EXPECT_EQ(oldField.entity_rank(), newField.entity_rank());
179 EXPECT_EQ(oldField.number_of_states(), newField.number_of_states());
180 EXPECT_EQ(oldField.state(), newField.state());
181 EXPECT_EQ(oldField.type_is<int>(), newField.type_is<int>());
182 EXPECT_EQ(oldField.type_is<double>(), newField.type_is<double>());
183
184 if(oldField.attribute<Ioss::Field::RoleType>() != nullptr)
185 {
186 EXPECT_EQ(*oldField.attribute<Ioss::Field::RoleType>(), *newField.attribute<Ioss::Field::RoleType>());
187 }
188
189 for(stk::mesh::EntityRank rank=stk::topology::NODE_RANK; rank<oldField.entity_rank(); rank++)
190 {
191 EXPECT_EQ(oldField.max_size(), newField.max_size());
192 }
193
194 ASSERT_EQ(stk::io::has_field_output_type(oldField), stk::io::has_field_output_type(newField));
195 if (stk::io::has_field_output_type(oldField)) {
196 EXPECT_EQ(stk::io::get_field_output_type(oldField), stk::io::get_field_output_type(newField));
197 }
198
199 expect_equal_data_traits(oldField.data_traits(), newField.data_traits());
200 expect_equal_field_initial_values(oldField, newField);
201 expect_equal_field_restrictions(newMeta, oldField, newField);
202 }
203
204 void expect_all_fields_equal(stk::mesh::MetaData &oldMeta, stk::mesh::MetaData &newMeta)
205 {
206 const stk::mesh::FieldVector &oldFields = oldMeta.get_fields();
207 const stk::mesh::FieldVector &newFields = newMeta.get_fields();
208 ASSERT_EQ(oldFields.size(), newFields.size());
209 for(size_t i=0; i<oldFields.size(); i++) {
210 expect_equal_fields(newMeta, *oldFields[i], *newFields[i]);
211 }
212 }
213
214 void expect_surface_to_block_mappings_equal(stk::mesh::MetaData &oldMeta, stk::mesh::MetaData &newMeta)
215 {
216 std::vector<const stk::mesh::Part *> oldSurfacesInMap = oldMeta.get_surfaces_in_surface_to_block_map();
217 std::vector<const stk::mesh::Part *> newSurfacesInMap = newMeta.get_surfaces_in_surface_to_block_map();
218
219 ASSERT_EQ(oldSurfacesInMap.size(), newSurfacesInMap.size());
220
221 for(size_t i=0;i<oldSurfacesInMap.size();++i)
222 {
223 std::vector<const stk::mesh::Part*> oldBlocks = oldMeta.get_blocks_touching_surface(oldSurfacesInMap[i]);
224 std::vector<const stk::mesh::Part*> newBlocks = newMeta.get_blocks_touching_surface(newSurfacesInMap[i]);
225 ASSERT_EQ(oldBlocks.size(), newBlocks.size());
226 for(size_t j=0; j<oldBlocks.size(); ++j)
227 {
228 EXPECT_EQ(oldBlocks[j]->name(), newBlocks[j]->name());
229 }
230 }
231 }
232
233 void expect_equal_meta_datas(stk::mesh::MetaData& oldMeta, stk::mesh::MetaData& newMeta)
234 {
235 EXPECT_TRUE(newMeta.is_initialized());
236 EXPECT_EQ(oldMeta.spatial_dimension(), newMeta.spatial_dimension());
237 EXPECT_EQ(oldMeta.entity_rank_count(), newMeta.entity_rank_count());
238 EXPECT_EQ(oldMeta.entity_rank_names(), newMeta.entity_rank_names());
239 EXPECT_EQ(oldMeta.coordinate_field()->name(), newMeta.coordinate_field()->name());
240 expect_all_parts_equal(oldMeta, newMeta);
241 expect_all_fields_equal(oldMeta, newMeta);
242 expect_surface_to_block_mappings_equal(oldMeta, newMeta);
243 }
244
245 void expect_equal_entity_counts(stk::mesh::BulkData& oldBulk, stk::mesh::BulkData& newBulk)
246 {
247 std::vector<unsigned> oldCount;
248 std::vector<unsigned> newCount;
249
250 const size_t oldNranks = oldBulk.mesh_meta_data().entity_rank_count();
251 const size_t newNranks = newBulk.mesh_meta_data().entity_rank_count();
252
253 EXPECT_EQ(oldNranks, newNranks);
254
255 oldCount.resize( oldNranks );
256 newCount.resize( newNranks );
257
258 for ( size_t i = 0 ; i < oldNranks ; ++i )
259 {
260 oldCount[i] = stk::mesh::count_selected_entities(oldBulk.mesh_meta_data().locally_owned_part(), oldBulk.buckets( static_cast<stk::mesh::EntityRank>(i) ));
261 newCount[i] = stk::mesh::count_selected_entities(newBulk.mesh_meta_data().locally_owned_part(), newBulk.buckets( static_cast<stk::mesh::EntityRank>(i) ));
262 }
263
264 EXPECT_EQ(oldCount, newCount);
265 }
266
267 class MeshClone : public stk::unit_test_util::MeshFixture
268 {
269 public:
270 MeshClone()
271 : m_coordsField(nullptr),
272 m_noDeleteAttribute()
273 {}
274
275 protected:
276 const char *get_mesh_spec() const {return "generated:1x1x8|sideset:x";}
277 stk::mesh::BulkData::AutomaticAuraOption get_aura_option() const {return stk::mesh::BulkData::NO_AUTO_AURA;}
278
279 void initialize_mesh_with_parts_and_fields()
280 {
281 setup_empty_mesh(get_aura_option());
282 setup_parts();
283 setup_fields();
284 stk::io::fill_mesh(get_mesh_spec(), get_bulk());
285 get_meta().set_coordinate_field(m_coordsField);
286 add_distribution_factor();
287 add_part_attributes({"block_1", "surface_1"});
288 }
289
290 void initialize_multiproc_multiblock_mesh()
291 {
292 const std::string meshSpec = "textmesh:0,1,HEX_8,1,2,3,4,5,6,7,8,block_1\n"
293 "1,2,HEX_8,5,6,7,8,9,10,11,12,block_2\n";
294 setup_mesh(meshSpec, stk::mesh::BulkData::AUTO_AURA);
295 }
296
297 void add_part_attributes(const std::vector<std::string> & partNames)
298 {
299 for (const std::string & partName : partNames) {
300 stk::mesh::Part * part = get_meta().get_part(partName);
301 STK_ThrowRequire(part != nullptr);
302 get_meta().declare_attribute_no_delete(*part, &m_noDeleteAttribute);
303 get_meta().declare_attribute_with_delete(*part, new WithDeleteAttribute);
304 }
305 }
306
307 void add_distribution_factor() {
308 stk::io::set_distribution_factor_field(*m_block1Part, *m_distFactField);
309 }
310
311 void setup_parts()
312 {
313 m_superPart = &get_meta().declare_part("superPart");
314 m_subPart = &get_meta().declare_part("subPart");
315 get_meta().declare_part_subset(*m_superPart, *m_subPart);
316
317 m_block1Part = &get_meta().declare_part("block_1");
318 m_surface1Part = &get_meta().declare_part("surface_1");
319 }
320
321 void setup_fields()
322 {
323 m_coordsField = &get_meta().declare_field<double>(stk::topology::NODE_RANK, "Coordinates", 1);
324 m_surfaceField = &get_meta().declare_field<double>(stk::topology::NODE_RANK, "surfaceField", 2);
325 m_distFactField = &get_meta().declare_field<double>(stk::topology::NODE_RANK, "dist_fact", 1);
326
327 const double vectInitValue[] = {13, 14, 15};
328 const double scalarInitValue = 3;
329 stk::mesh::put_field_on_mesh(*m_coordsField, *m_block1Part, 3, vectInitValue);
330 stk::mesh::put_field_on_mesh(*m_surfaceField, *m_surface1Part, 3, vectInitValue);
331 stk::mesh::put_field_on_mesh(*m_distFactField, *m_block1Part, 1, &scalarInitValue);
332 stk::io::set_field_output_type(*m_coordsField, stk::io::FieldOutputType::VECTOR_3D);
333 stk::io::set_field_output_type(*m_surfaceField, stk::io::FieldOutputType::VECTOR_3D);
334 }
335
336 void add_orphan_nodes(const unsigned numOrphansPerProc)
337 {
338 std::vector<stk::mesh::EntityId> newIds;
339 get_bulk().generate_new_ids(stk::topology::NODE_RANK, numOrphansPerProc, newIds);
340
341 get_bulk().modification_begin();
342 for(unsigned i=0; i<numOrphansPerProc; i++)
343 {
344 stk::mesh::EntityId id = newIds[i];
345 get_bulk().declare_entity(stk::topology::NODE_RANK, id, *m_block1Part);
346 }
347 get_bulk().modification_end();
348 }
349
350 stk::mesh::Part* m_superPart;
351 stk::mesh::Part* m_subPart;
352 stk::mesh::Part* m_block1Part;
353 stk::mesh::Part* m_surface1Part;
354 stk::mesh::Field<double>* m_coordsField;
355 stk::mesh::Field<double>* m_surfaceField;
356 stk::mesh::Field<double>* m_distFactField;
357 NoDeleteAttribute m_noDeleteAttribute;
358 };
359
360 TEST_F(MeshClone, ifSameMesh_throws)
361 {
362 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
363 ASSERT_THROW(stk::tools::copy_mesh(get_bulk(), get_meta().universal_part(), get_bulk()), std::exception);
364 }
365
366 TEST_F(MeshClone, copyOnlyMeta)
367 {
368 initialize_mesh_with_parts_and_fields();
369
370 stk::mesh::MetaData newMeta;
371 stk::tools::copy_meta_with_io_attributes(get_meta(), newMeta);
372
373 expect_equal_meta_datas(get_meta(), newMeta);
374 }
375
376 TEST_F(MeshClone, copyMetaAndBulk)
377 {
378 initialize_mesh_with_parts_and_fields();
379
380 std::shared_ptr<stk::mesh::BulkData> newBulkPtr = build_mesh(get_comm());
381 stk::mesh::BulkData& newBulk = *newBulkPtr;
382 stk::mesh::MetaData& newMeta = newBulk.mesh_meta_data();
383
384 stk::tools::copy_mesh(get_bulk(), get_meta().universal_part(), newBulk);
385
386 expect_equal_meta_datas(get_meta(), newMeta);
387 expect_equal_entity_counts(get_bulk(), newBulk);
388 }
389
390 TEST_F(MeshClone, copyMeshWithPreExistingOrphanNodes)
391 {
392 initialize_mesh_with_parts_and_fields();
393
394 unsigned numOrphansPerProc = 5;
395 add_orphan_nodes(numOrphansPerProc);
396
397 std::shared_ptr<stk::mesh::BulkData> newBulkPtr = build_mesh(get_comm());
398 stk::mesh::BulkData& newBulk = *newBulkPtr;
399 stk::mesh::MetaData& newMeta = newBulk.mesh_meta_data();
400 stk::tools::copy_mesh(get_bulk(), get_meta().universal_part(), newBulk);
401
402 expect_equal_meta_datas(get_meta(), newMeta);
403 expect_equal_entity_counts(get_bulk(), newBulk);
404 }
405
406 TEST_F(MeshClone, copyMeshWithOrphanNodesOnSharedSide)
407 {
408 if (get_parallel_size() != 2) return;
409
410 initialize_multiproc_multiblock_mesh();
411
412 std::shared_ptr<stk::mesh::BulkData> newBulkPtr = build_mesh(get_comm());
413 stk::mesh::BulkData& newBulk = *newBulkPtr;
414
415 stk::tools::copy_mesh(get_bulk(), *get_meta().get_part("block_1"), newBulk);
416
417 std::vector<size_t> count;
418 stk::mesh::count_entities(get_meta().locally_owned_part() | get_meta().globally_shared_part(), newBulk, count);
419 if (get_parallel_rank() == 0) {
420 EXPECT_EQ(count[stk::topology::NODE_RANK], 8u);
421 }
422 else {
423 EXPECT_EQ(count[stk::topology::NODE_RANK], 0u);
424 }
425 }
426
427 TEST_F(MeshClone, copyMeshWithOrphanNodesOnOwnedSide)
428 {
429 if (get_parallel_size() != 2) return;
430
431 initialize_multiproc_multiblock_mesh();
432
433 std::shared_ptr<stk::mesh::BulkData> newBulkPtr = build_mesh(get_comm());
434 stk::mesh::BulkData& newBulk = *newBulkPtr;
435
436 stk::tools::copy_mesh(get_bulk(), *get_meta().get_part("block_2"), newBulk);
437
438 std::vector<size_t> count;
439 stk::mesh::count_entities(get_meta().locally_owned_part() | get_meta().globally_shared_part(), newBulk, count);
440 if (get_parallel_rank() == 0) {
441 EXPECT_EQ(count[stk::topology::NODE_RANK], 0u);
442 }
443 else {
444 EXPECT_EQ(count[stk::topology::NODE_RANK], 8u);
445 }
446 }
447
448 #if defined(__GNUC__) && (__GNUC__ > 4) && !defined(__INTEL_COMPILER)
449 TEST(MetaDataSize, sizeChanges_needToUpdateCopyMesh)
450 {
451 stk::mesh::MetaData meta;
452 EXPECT_GE(632u, sizeof(meta)) << "Size of MetaData changed. Does mesh copying capability need to be updated?";
453 }
454 #endif
455
456 }