Debian Codegraph @

packages/stk/stk_unit_tests/stk_mesh/UnitTestMeshImplUtils.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 "Setup2Block2HexMesh.hpp"
36 #include "stk_io/StkMeshIoBroker.hpp"
37 #include "stk_mesh/base/Bucket.hpp" // for Bucket
38 #include "stk_mesh/base/Types.hpp" // for BucketVector, EntityRank
39 #include "stk_mesh/baseImpl/Visitors.hpp"
40 #include "stk_mesh/baseImpl/MeshImplUtils.hpp"
41 #include "stk_topology/topology.hpp" // for topology, etc
42 #include <gtest/gtest.h>
43 #include <stddef.h> // for size_t
44 #include <stk_mesh/base/BulkData.hpp> // for BulkData
45 #include <stk_mesh/base/MeshBuilder.hpp>
46 #include <stk_mesh/base/Comm.hpp> // for comm_mesh_counts
47 #include <stk_mesh/base/CreateFaces.hpp>
48 #include <stk_mesh/base/GetEntities.hpp> // for count_entities
49 #include <stk_mesh/base/MetaData.hpp> // for MetaData
50 #include <stk_unit_test_utils/BulkDataTester.hpp>
51 #include <stk_unit_test_utils/getOption.h>
52 #include <stk_unit_test_utils/BuildMesh.hpp>
53 #include <vector> // for vector, vector<>::iterator
54
55 using stk::mesh::MetaData;
56 using stk::mesh::BulkData;
57 using stk::mesh::Selector;
58 using stk::mesh::Entity;
59 using stk::mesh::EntityKey;
60 using stk::mesh::EntityVector;
61 using stk::mesh::EntityRank;
62 using stk::mesh::impl::VisitClosureGeneral;
63 using stk::mesh::impl::VisitClosure;
64 using stk::mesh::impl::VisitUpwardClosureGeneral;
65 using stk::mesh::impl::VisitUpwardClosure;
66 using stk::mesh::impl::VisitAuraClosureGeneral;
67 using stk::mesh::impl::VisitAuraClosure;
68 using stk::mesh::impl::VisitUpDownClosure;
69 using stk::mesh::impl::StoreInVector;
70 using stk::mesh::impl::StoreInSet;
71 using stk::mesh::impl::AlwaysVisit;
72 using stk::mesh::impl::OnlyVisitGhostsOnce;
73 using stk::mesh::impl::OnlyVisitLocallyOwnedOnce;
74 using stk::unit_test_util::build_mesh;
75
76 TEST ( MeshImplUtils, find_element_edge_ordinal_and_equivalent_nodes )
77 {
78 stk::ParallelMachine communicator = MPI_COMM_WORLD;
79 int numProcs = stk::parallel_machine_size(communicator);
80 if (numProcs > 2) {
81 return;
82 }
83
84 const unsigned spatialDim = 3;
85 std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatialDim, communicator);
86 stk::mesh::BulkData& bulk = *bulkPtr;
87
88 setup2Block2HexMesh(bulk);
89
90 const unsigned numNodesPerEdge = 2;
91
92 //we know that edge 2-6 is edge-ordinal 9 on element 1 and
93 //edge-ordinal 8 on element 2
94 stk::mesh::EntityId edge_2_6_nodeIds[] = {2, 6};
95 stk::mesh::Entity edge_2_6_nodes[numNodesPerEdge];
96 edge_2_6_nodes[0] = bulk.get_entity(stk::topology::NODE_RANK, edge_2_6_nodeIds[0]);
97 edge_2_6_nodes[1] = bulk.get_entity(stk::topology::NODE_RANK, edge_2_6_nodeIds[1]);
98
99 stk::mesh::EntityId elem1Id = 1;
100 stk::mesh::EntityId elem2Id = 2;
101 stk::mesh::Entity elem1 = bulk.get_entity(stk::topology::ELEM_RANK, elem1Id);
102 stk::mesh::Entity elem2 = bulk.get_entity(stk::topology::ELEM_RANK, elem2Id);
103
104 stk::mesh::Entity elemEdgeNodes[numNodesPerEdge];
105 unsigned elemEdgeOrdinal = 999;
106
107 bool found_it = stk::mesh::impl::find_element_edge_ordinal_and_equivalent_nodes(bulk, elem1, numNodesPerEdge, edge_2_6_nodes,
108 elemEdgeOrdinal, elemEdgeNodes);
109
110 EXPECT_EQ(true, found_it);
111 unsigned expectedElemEdgeOrdinal = 9;
112 EXPECT_EQ(expectedElemEdgeOrdinal, elemEdgeOrdinal);
113 EXPECT_EQ(edge_2_6_nodes[0], elemEdgeNodes[0]);
114 EXPECT_EQ(edge_2_6_nodes[1], elemEdgeNodes[1]);
115
116 found_it = stk::mesh::impl::find_element_edge_ordinal_and_equivalent_nodes(bulk, elem2, numNodesPerEdge, edge_2_6_nodes,
117 elemEdgeOrdinal, elemEdgeNodes);
118
119 EXPECT_EQ(true, found_it);
120 expectedElemEdgeOrdinal = 8;
121 EXPECT_EQ(expectedElemEdgeOrdinal, elemEdgeOrdinal);
122 EXPECT_EQ(edge_2_6_nodes[0], elemEdgeNodes[0]);
123 EXPECT_EQ(edge_2_6_nodes[1], elemEdgeNodes[1]);
124 }
125
126 class ClosureFixture
127 {
128 public:
129 ClosureFixture(MPI_Comm communicator, int num_x, int num_y=1)
130 {
131 const int spatialDim = 3;
132 m_mesh = build_mesh(spatialDim, communicator);
133 m_meta = &(m_mesh->mesh_meta_data());
134 std::ostringstream oss;
135 oss << "generated:" << num_x << "x" << num_y << "x" << m_mesh->parallel_size() << "|sideset:xXyYzZ";
136 std::string exodusFileName = stk::unit_test_util::get_option("-i", oss.str());
137 stk::io::StkMeshIoBroker exodus_file_reader(communicator);
138 exodus_file_reader.set_bulk_data(*m_mesh);
139 exodus_file_reader.add_mesh_database(exodusFileName, stk::io::READ_MESH);
140 exodus_file_reader.create_input_mesh();
141 exodus_file_reader.populate_bulk_data();
142 }
143
144 ~ClosureFixture()
145 {
146 }
147
148 void WriteToExodusFile(std::string filename)
149 {
150 stk::io::StkMeshIoBroker exodus_file_reader(m_mesh->parallel());
151 exodus_file_reader.set_bulk_data(*m_mesh);
152 int index = exodus_file_reader.create_output_mesh(filename, stk::io::WRITE_RESULTS);
153 exodus_file_reader.write_output_mesh(index);
154 }
155
156 BulkData & mesh() { return *m_mesh; }
157 int prank() { return m_mesh->parallel_rank(); }
158 int psize() { return m_mesh->parallel_size(); }
159
160 private:
161 MetaData * m_meta = nullptr;
162 std::shared_ptr<BulkData> m_mesh;
163 };
164
165
166 std::string PrintEntityVector(EntityVector ev, const BulkData & mesh)
167 {
168 std::ostringstream oss;
169 int myRank = mesh.parallel_rank();
170 oss << "P" << myRank << " { ";
171 for (size_t i=0 ; i<ev.size() ; ++i) {
172 EntityKey key = mesh.entity_key(ev[i]);
173 oss << key << " ";
174 }
175 oss << "}\n";
176 return oss.str();
177 }
178
179 TEST(MeshImplUtils, visit_closure_trivial)
180 {
181 MPI_Comm communicator = MPI_COMM_WORLD;
182 const int spatialDim = 3;
183 std::shared_ptr<BulkData> mesh = build_mesh(spatialDim, communicator);
184
185 Entity entity = Entity();
186 EntityVector ev;
187 StoreInVector<EntityVector> siv(ev);
188 VisitClosure(*mesh, entity, siv);
189 EXPECT_EQ( 0u, ev.size() );
190 }
191
192 TEST(MeshImplUtils, visit_closure_nominal)
193
194 {
195 MPI_Comm communicator = MPI_COMM_WORLD;
196 ClosureFixture fix(communicator,2);
197 int numProcs = fix.psize();
198 const int myRank = fix.prank();
199 BulkData & mesh = fix.mesh();
200
201 Selector locally_owned_selector = mesh.mesh_meta_data().locally_owned_part();
202 EntityVector element_vector;
203 get_selected_entities(locally_owned_selector, mesh.buckets(stk::topology::ELEMENT_RANK),element_vector);
204 ASSERT_TRUE( !element_vector.empty() );
205 Entity element = element_vector[0];
206 EntityVector ev;
207 StoreInVector<EntityVector> siv(ev);
208 VisitClosure(mesh, element, siv);
209 if (numProcs == 1)
210 {
211 EXPECT_EQ( 14u, ev.size() );
212 }
213 else if (myRank == 0 || myRank == numProcs -1)
214 {
215 EXPECT_EQ( 13u, ev.size() );
216 }
217 else
218 {
219 EXPECT_EQ( 12u, ev.size() );
220 }
221 }
222
223 TEST(MeshImplUtils, visit_closure_face)
224 {
225 MPI_Comm communicator = MPI_COMM_WORLD;
226 ClosureFixture fix(communicator,2);
227 BulkData & mesh = fix.mesh();
228
229 EntityVector ev;
230 StoreInVector<EntityVector> siv(ev);
231 Selector locally_owned_selector = mesh.mesh_meta_data().locally_owned_part();
232 EntityVector face_vector;
233 get_selected_entities(locally_owned_selector, mesh.buckets(stk::topology::FACE_RANK),face_vector);
234 ASSERT_TRUE( !face_vector.empty() );
235 Entity face = face_vector[0];
236 VisitClosure(mesh, face, siv);
237 EXPECT_EQ( 5u, ev.size() );
238 }
239
240 TEST(MeshImplUtils, visit_closure_of_vector)
241 {
242 MPI_Comm communicator = MPI_COMM_WORLD;
243 ClosureFixture fix(communicator,2);
244 int numProcs = fix.psize();
245 const int myRank = fix.prank();
246 BulkData & mesh = fix.mesh();
247
248 EntityVector ev;
249 StoreInVector<EntityVector> siv(ev);
250 Selector locally_owned_selector = mesh.mesh_meta_data().locally_owned_part();
251 EntityVector element_vector;
252 get_selected_entities(locally_owned_selector, mesh.buckets(stk::topology::ELEMENT_RANK),element_vector);
253 VisitClosure(mesh,element_vector.begin(),element_vector.end(),siv);
254 if (numProcs == 1)
255 {
256 EXPECT_EQ( 24u, ev.size() );
257 }
258 else if (myRank == 0 || myRank == numProcs-1)
259 {
260 EXPECT_EQ( 22u, ev.size() );
261 }
262 else
263 {
264 EXPECT_EQ( 20u, ev.size() );
265 }
266 }
267
268 TEST(MeshImplUtils, visit_closure_of_vector_locally_owned)
269 {
270 MPI_Comm communicator = MPI_COMM_WORLD;
271 ClosureFixture fix(communicator,2);
272 int numProcs = fix.psize();
273 const int myRank = fix.prank();
274 BulkData & mesh = fix.mesh();
275
276 EntityVector ev;
277 StoreInVector<EntityVector> siv(ev);
278 Selector locally_owned_selector = mesh.mesh_meta_data().locally_owned_part();
279 EntityVector element_vector;
280 get_selected_entities(locally_owned_selector, mesh.buckets(stk::topology::ELEMENT_RANK),element_vector);
281 OnlyVisitLocallyOwnedOnce ovloeo(mesh);
282 VisitClosureGeneral(mesh,element_vector.begin(),element_vector.end(),siv,ovloeo);
283 if (numProcs == 1)
284 {
285 EXPECT_EQ( 24u, ev.size() );
286 }
287 else if (myRank == 0)
288 {
289 EXPECT_EQ( 22u, ev.size() );
290 }
291 else if (myRank == numProcs-1)
292 {
293 EXPECT_EQ( 16u, ev.size() );
294 }
295 else
296 {
297 EXPECT_EQ( 14u, ev.size() );
298 }
299 }
300
301 TEST(MeshImplUtils, visit_upward_closure_trivial)
302 {
303 MPI_Comm communicator = MPI_COMM_WORLD;
304 const int spatialDim = 3;
305 std::shared_ptr<BulkData> mesh = build_mesh(spatialDim, communicator);
306
307 Entity entity = Entity();
308 EntityVector ev;
309 StoreInVector<EntityVector> siv(ev);
310 VisitUpwardClosure(*mesh, entity, siv);
311 EXPECT_EQ( 0u, ev.size() );
312 }
313
314 TEST(MeshImplUtils, visit_upward_closure_nominal)
315 {
316 MPI_Comm communicator = MPI_COMM_WORLD;
317 ClosureFixture fix(communicator,2);
318 int numProcs = fix.psize();
319 const int myRank = fix.prank();
320 BulkData & mesh = fix.mesh();
321 if (numProcs != 4) { return; }
322
323 Entity node;
324 if (myRank == 0) {
325 node = mesh.get_entity(stk::topology::NODE_RANK,1);
326 }
327 else if (myRank == 1) {
328 node = mesh.get_entity(stk::topology::NODE_RANK,7);
329 }
330 else if (myRank == 2) {
331 node = mesh.get_entity(stk::topology::NODE_RANK,17);
332 }
333 else if (myRank == 3) {
334 node = mesh.get_entity(stk::topology::NODE_RANK,29);
335 }
336 EntityVector ev;
337 StoreInVector<EntityVector> siv(ev);
338 VisitUpwardClosure(mesh, node, siv);
339 if (myRank == 0) {
340 EXPECT_EQ( 5u, ev.size() );
341 }
342 else if (myRank == 1) {
343 EXPECT_EQ( 7u, ev.size() );
344 }
345 else if (myRank == 2) {
346 EXPECT_EQ( 9u, ev.size() );
347 }
348 else { // myRank == 3
349 EXPECT_EQ( 7u, ev.size() );
350 }
351 }
352
353 TEST(MeshImplUtils, visit_upward_closure_face)
354 {
355 MPI_Comm communicator = MPI_COMM_WORLD;
356 ClosureFixture fix(communicator,2);
357 BulkData & mesh = fix.mesh();
358
359 EntityVector ev;
360 StoreInVector<EntityVector> siv(ev);
361 Selector locally_owned_selector = mesh.mesh_meta_data().locally_owned_part();
362 EntityVector face_vector;
363 get_selected_entities(locally_owned_selector, mesh.buckets(stk::topology::FACE_RANK),face_vector);
364 ASSERT_TRUE( !face_vector.empty() );
365 Entity face = face_vector[0];
366 VisitUpwardClosure(mesh, face, siv);
367 EXPECT_EQ( 2u, ev.size() );
368 }
369
370 TEST(MeshImplUtils, visit_upward_closure_of_vector)
371 {
372 MPI_Comm communicator = MPI_COMM_WORLD;
373 ClosureFixture fix(communicator,2);
374 int numProcs = fix.psize();
375 const int myRank = fix.prank();
376 BulkData & mesh = fix.mesh();
377 if (numProcs != 4) { return; }
378
379 EntityVector entity_vector;
380 if (myRank == 0) {
381 entity_vector.push_back(mesh.get_entity(stk::topology::NODE_RANK,1));
382 entity_vector.push_back(mesh.get_entity(stk::topology::NODE_RANK,6));
383 }
384 else if (myRank == 1) {
385 entity_vector.push_back(mesh.get_entity(stk::topology::ELEMENT_RANK,3));
386 }
387 else if (myRank == 2) {
388 entity_vector.push_back(mesh.get_entity(stk::topology::ELEMENT_RANK,5));
389 entity_vector.push_back(mesh.get_entity(stk::topology::NODE_RANK,15));
390 }
391 else { // (myRank == 3)
392 entity_vector.push_back(mesh.get_entity(stk::topology::NODE_RANK,20));
393 entity_vector.push_back(mesh.get_entity(stk::topology::NODE_RANK,23));
394 }
395
396 EntityVector ev;
397 StoreInVector<EntityVector> siv(ev);
398 VisitUpwardClosure(mesh,entity_vector.begin(),entity_vector.end(),siv);
399 if (myRank == 0) {
400 EXPECT_EQ( 10u, ev.size() );
401 }
402 else if (myRank == 1) {
403 EXPECT_EQ( 1u, ev.size() );
404 }
405 else if (myRank == 2) {
406 EXPECT_EQ( 8u, ev.size() );
407 }
408 else { // (myRank == 3)
409 EXPECT_EQ( 14u, ev.size() );
410 }
411 }
412
413 TEST(MeshImplUtils, visit_aura_closure_trivial)
414 {
415 MPI_Comm communicator = MPI_COMM_WORLD;
416 ClosureFixture fix(communicator,2);
417 BulkData & mesh = fix.mesh();
418
419 EntityVector ev;
420 StoreInVector<EntityVector> siv(ev);
421 Entity entity = Entity();
422 VisitAuraClosure(mesh,entity,siv);
423 EXPECT_EQ( 0u, ev.size() );
424 }
425
426 TEST(MeshImplUtils, visit_aura_closure_of_element)
427 {
428 MPI_Comm communicator = MPI_COMM_WORLD;
429 ClosureFixture fix(communicator,2);
430 int numProcs = fix.psize();
431 const int myRank = fix.prank();
432 BulkData & mesh = fix.mesh();
433 if (numProcs > 4) { return; }
434
435 EntityVector ev;
436 StoreInVector<EntityVector> siv(ev);
437 Selector locally_owned_selector = mesh.mesh_meta_data().locally_owned_part();
438 EntityVector element_vector;
439 get_selected_entities(locally_owned_selector, mesh.buckets(stk::topology::ELEMENT_RANK),element_vector);
440 ASSERT_TRUE( !element_vector.empty() );
441 Entity element = element_vector[0];
442 VisitAuraClosure(mesh,element,siv);
443 if (numProcs == 1)
444 {
445 EXPECT_EQ( 24u, ev.size() );
446 }
447 else if (numProcs == 2)
448 {
449 EXPECT_EQ( 38u, ev.size() );
450 }
451 else if (myRank == 0 || myRank == numProcs-1)
452 {
453 EXPECT_EQ( 36u, ev.size() );
454 }
455 else
456 {
457 if (numProcs==3) {
458 EXPECT_EQ( 52u, ev.size() );
459 }
460 else {
461 EXPECT_EQ( 50u, ev.size() );
462 }
463 }
464
465 }
466
467 TEST(MeshImplUtils, visit_aura_closure_of_corner_node)
468 {
469 MPI_Comm communicator = MPI_COMM_WORLD;
470 ClosureFixture fix(communicator,2);
471 int numProcs = fix.psize();
472 BulkData & mesh = fix.mesh();
473 if (numProcs > 1) { return; }
474
475 Entity node;
476 Selector locally_owned_selector = mesh.mesh_meta_data().locally_owned_part();
477 EntityVector node_vector;
478 get_selected_entities(locally_owned_selector, mesh.buckets(stk::topology::NODE_RANK),node_vector);
479 ASSERT_TRUE( !node_vector.empty() );
480 node = node_vector[0];
481 EntityVector ev;
482 StoreInVector<EntityVector> siv(ev);
483 VisitAuraClosure(mesh,node,siv);
484 EXPECT_EQ( 14u, ev.size() );
485 }
486
487 TEST(MeshImplUtils, visit_aura_closure_of_center_node)
488 {
489 MPI_Comm communicator = MPI_COMM_WORLD;
490 ClosureFixture fix(communicator,2);
491 int numProcs = fix.psize();
492 BulkData & mesh = fix.mesh();
493 if (numProcs > 1) { return; }
494
495 Entity node;
496 Selector locally_owned_selector = mesh.mesh_meta_data().locally_owned_part();
497 EntityVector node_vector;
498 get_selected_entities(locally_owned_selector, mesh.buckets(stk::topology::NODE_RANK),node_vector);
499 ASSERT_TRUE( node_vector.size() > 1 );
500 node = node_vector[1];
501 EntityVector ev;
502 StoreInVector<EntityVector> siv(ev);
503 VisitAuraClosure(mesh,node,siv);
504 EXPECT_EQ( 24u, ev.size() );
505 }
506
507 TEST(MeshImplUtils, visit_aura_closure_of_corner_node_in_2procs)
508 {
509 MPI_Comm communicator = MPI_COMM_WORLD;
510 ClosureFixture fix(communicator,2);
511 int numProcs = fix.psize();
512 BulkData & mesh = fix.mesh();
513 if (numProcs != 2 ) { return; }
514
515 Selector shared_selector = mesh.mesh_meta_data().globally_shared_part();
516 EntityVector node_vector;
517 get_selected_entities(shared_selector, mesh.buckets(stk::topology::NODE_RANK),node_vector);
518 ASSERT_TRUE( !node_vector.empty() );
519 Entity node = node_vector[0];
520 EntityVector ev;
521 StoreInVector<EntityVector> siv(ev);
522 VisitAuraClosure(mesh,node,siv);
523 EXPECT_EQ( 22u, ev.size() );
524 }
525
526 TEST(MeshImplUtils, visit_aura_closure_of_center_node_in_2procs)
527 {
528 MPI_Comm communicator = MPI_COMM_WORLD;
529 ClosureFixture fix(communicator,2);
530 int numProcs = fix.psize();
531 BulkData & mesh = fix.mesh();
532 if (numProcs != 2 ) { return; }
533
534 Selector shared_selector = mesh.mesh_meta_data().globally_shared_part();
535 EntityVector node_vector;
536 get_selected_entities(shared_selector, mesh.buckets(stk::topology::NODE_RANK),node_vector);
537 ASSERT_TRUE( node_vector.size() > 1 );
538 Entity node = node_vector[1];
539 EntityVector ev;
540 StoreInVector<EntityVector> siv(ev);
541 VisitAuraClosure(mesh,node,siv);
542 EXPECT_EQ( 38u, ev.size() );
543 }
544
545 TEST(MeshImplUtils, visit_aura_closure_of_side_node_in_3procs)
546 {
547 MPI_Comm communicator = MPI_COMM_WORLD;
548 ClosureFixture fix(communicator,4,4); // 4 x 4 x 3
549 int numProcs = fix.psize();
550 const int myRank = fix.prank();
551 BulkData & mesh = fix.mesh();
552 if (numProcs != 3 ) { return; }
553
554 Entity node;
555 if (myRank == 0 || myRank == 1) {
556 node = mesh.get_entity(stk::topology::NODE_RANK,31);
557 }
558 else {
559 node = mesh.get_entity(stk::topology::NODE_RANK,56);
560 }
561 EntityVector ev;
562 StoreInVector<EntityVector> siv(ev);
563 VisitAuraClosure(mesh,node,siv);
564 EXPECT_EQ( 30u, ev.size() );
565 }
566
567 TEST(MeshImplUtils, visit_aura_closure_of_center_node_in_3procs)
568 {
569 MPI_Comm communicator = MPI_COMM_WORLD;
570 ClosureFixture fix(communicator,4,4); // 4 x 4 x 3
571 int numProcs = fix.psize();
572 const int myRank = fix.prank();
573 BulkData & mesh = fix.mesh();
574 if (numProcs != 3 ) { return; }
575
576 Entity node;
577 if (myRank == 0 || myRank == 1) {
578 node = mesh.get_entity(stk::topology::NODE_RANK,32);
579 }
580 else {
581 node = mesh.get_entity(stk::topology::NODE_RANK,57);
582 }
583 EntityVector ev;
584 StoreInVector<EntityVector> siv(ev);
585 VisitAuraClosure(mesh,node,siv);
586 EXPECT_EQ( 47u, ev.size() );
587 }
588
589 TEST(MeshImplUtils, visit_aura_closure_of_side_element_in_3procs)
590 {
591 MPI_Comm communicator = MPI_COMM_WORLD;
592 ClosureFixture fix(communicator,4,4); // 4 x 4 x 3
593 int numProcs = fix.psize();
594 const int myRank = fix.prank();
595 BulkData & mesh = fix.mesh();
596 if (numProcs != 3 ) { return; }
597
598 Entity element = mesh.get_entity(stk::topology::ELEMENT_RANK,21);
599 EntityVector ev;
600 StoreInVector<EntityVector> siv(ev);
601 VisitAuraClosure(mesh,element,siv);
602 if (myRank == 0 || myRank == 2) {
603 EXPECT_EQ( 64u, ev.size() );
604 }
605 else {
606 EXPECT_EQ( 93u, ev.size() );
607 }
608 }
609
610 TEST(MeshImplUtils, visit_aura_closure_of_center_element_in_3procs)
611 {
612 MPI_Comm communicator = MPI_COMM_WORLD;
613 ClosureFixture fix(communicator,4,4); // 4 x 4 x 3
614 int numProcs = fix.psize();
615 const int myRank = fix.prank();
616 BulkData & mesh = fix.mesh();
617 if (numProcs != 3 ) { return; }
618
619 Entity element = mesh.get_entity(stk::topology::ELEMENT_RANK,22);
620 EntityVector ev;
621 StoreInVector<EntityVector> siv(ev);
622 VisitAuraClosure(mesh,element,siv);
623 if (myRank == 0 || myRank == 2) {
624 EXPECT_EQ( 87u, ev.size() );
625 }
626 else {
627 EXPECT_EQ( 127u, ev.size() );
628 }
629 }
630
631 TEST(MeshImplUtils, visit_up_down_closure_of_center_element_in_3procs)
632 {
633 MPI_Comm communicator = MPI_COMM_WORLD;
634 ClosureFixture fix(communicator,4,4); // 4 x 4 x 3
635 int numProcs = fix.psize();
636 if (numProcs != 3 ) { return; }
637
638 BulkData & mesh = fix.mesh();
639
640 Entity element = mesh.get_entity(stk::topology::ELEMENT_RANK,22);
641 EntityVector evUpDown;
642 StoreInVector<EntityVector> sivUpDown(evUpDown);
643 VisitUpDownClosure(mesh,element,sivUpDown);
644 EXPECT_EQ( 9u, evUpDown.size() );
645 }
646
647 TEST(MeshImplUtils, visit_aura_closure_vector)
648 {
649 MPI_Comm communicator = MPI_COMM_WORLD;
650 ClosureFixture fix(communicator,4,4); // 4x4x1
651 int numProcs = fix.psize();
652 BulkData & mesh = fix.mesh();
653 if (numProcs != 1 ) { return; }
654
655 std::vector<Entity> node_vec;
656 node_vec.push_back(mesh.get_entity(stk::topology::NODE_RANK,25));
657 node_vec.push_back(mesh.get_entity(stk::topology::NODE_RANK,26));
658 EntityVector ev;
659 StoreInVector<EntityVector> siv(ev);
660 VisitAuraClosure(mesh,node_vec.begin(),node_vec.end(),siv);
661 EXPECT_EQ( 26u, ev.size() );
662 }
663
664
665 TEST(MeshImplUtils, visit_aura_closure_vector_ghost)
666 {
667 MPI_Comm communicator = MPI_COMM_WORLD;
668 ClosureFixture fix(communicator,1,1); // 1x1x4
669 const int myRank = fix.prank();
670 int numProcs = fix.psize();
671 BulkData & mesh = fix.mesh();
672 if (numProcs != 4 ) { return; }
673
674 EntityVector ev;
675 if (myRank > 0) {
676 ev.push_back(mesh.get_entity(stk::topology::NODE_RANK,13));
677 ev.push_back(mesh.get_entity(stk::topology::NODE_RANK,14));
678 ev.push_back(mesh.get_entity(stk::topology::NODE_RANK,15));
679 ev.push_back(mesh.get_entity(stk::topology::NODE_RANK,16));
680 ev.push_back(mesh.get_entity(stk::topology::ELEMENT_RANK,3));
681 std::set<Entity> es;
682 StoreInSet<std::set<Entity> > sis(es);
683 OnlyVisitGhostsOnce ovgo(mesh);
684 VisitAuraClosureGeneral(mesh,ev.begin(),ev.end(),sis,ovgo);
685 if (myRank == 1) {
686 EXPECT_EQ( 9u, es.size() );
687 }
688 else if (myRank == 2) {
689 EXPECT_EQ( 19u, es.size() );
690 }
691 else if (myRank == 3) {
692 EXPECT_EQ( 9u, es.size() );
693 }
694 }
695 }
696
697 TEST(MeshImplUtils, check_for_connected_nodes)
698 {
699 stk::ParallelMachine communicator = MPI_COMM_WORLD;
700 unsigned spatialDim = 2;
701 std::shared_ptr<BulkData> meshPtr = build_mesh(spatialDim, communicator);
702 stk::mesh::MetaData& meta = meshPtr->mesh_meta_data();
703 stk::mesh::Part& block_1 = meta.declare_part_with_topology("block_1", stk::topology::QUAD_4_2D);
704 stk::mesh::BulkData& mesh = *meshPtr;
705 mesh.modification_begin();
706 Entity node1, node2, node3, node4;
707 if (mesh.parallel_rank() == 0) {
708 Entity element = mesh.declare_element(1, stk::mesh::ConstPartVector{&block_1});
709 node1 = mesh.declare_node(1);
710 node2 = mesh.declare_node(2);
711 node3 = mesh.declare_node(3);
712 node4 = mesh.declare_node(4);
713 //before relations declared, this check should fail
714 EXPECT_EQ(-1, stk::mesh::impl::check_for_connected_nodes(mesh));
715 mesh.declare_relation(element,node1,0);
716 mesh.declare_relation(element,node2,1);
717 mesh.declare_relation(element,node3,2);
718 mesh.declare_relation(element,node4,3);
719 }
720 mesh.modification_end();
721 //nominal checking here
722 EXPECT_EQ(0, stk::mesh::impl::check_for_connected_nodes(mesh));
723 }
724
725 TEST(MeshImplUtils, check_for_connected_nodes_invalid_topology)
726 {
727 stk::ParallelMachine communicator = MPI_COMM_WORLD;
728 unsigned spatialDim = 2;
729 std::shared_ptr<BulkData> meshPtr = build_mesh(spatialDim, communicator);
730 meshPtr->modification_begin();
731 if (meshPtr->parallel_rank() == 0) {
732 meshPtr->declare_element(1);
733 //before relations declared, this check should fail
734 EXPECT_EQ(-1, stk::mesh::impl::check_for_connected_nodes(*meshPtr));
735 }
736 }
737
738 TEST(MeshImplUtils, comm_mesh_very_parallel_consistency_nominal)
739 {
740 stk::ParallelMachine communicator = MPI_COMM_WORLD;
741 unsigned spatialDim = 2;
742 stk::mesh::MetaData meta(spatialDim);
743 stk::mesh::Part& block_1 = meta.declare_part_with_topology("block_1", stk::topology::QUAD_4_2D);
744 stk::unit_test_util::BulkDataTester mesh(meta, communicator);
745 if (mesh.parallel_size() >= 1) {
746 mesh.modification_begin();
747 Entity node1, node2, node3, node4;
748 if (mesh.parallel_rank() == 0) {
749 Entity element = mesh.declare_element(1, stk::mesh::ConstPartVector{&block_1});
750 node1 = mesh.declare_node(1);
751 node2 = mesh.declare_node(2);
752 node3 = mesh.declare_node(3);
753 node4 = mesh.declare_node(4);
754 mesh.declare_relation(element, node1, 0);
755 mesh.declare_relation(element, node2, 1);
756 mesh.declare_relation(element, node3, 2);
757 mesh.declare_relation(element, node4, 3);
758 }
759 mesh.modification_end();
760 std::ostringstream msg ;
761 bool is_consistent = mesh.my_comm_mesh_verify_parallel_consistency(msg);
762 STK_ThrowErrorMsgIf(!is_consistent, msg.str());
763 }
764 }
765
766 TEST(MeshImplUtils, check_no_shared_elements_or_higher_nominal)
767 {
768 stk::ParallelMachine pm = MPI_COMM_WORLD;
769 int numProcs = stk::parallel_machine_size(pm);
770 int myRank = stk::parallel_machine_rank(pm);
771 if (numProcs >= 2) {
772 unsigned spatialDim = 2;
773 std::shared_ptr<BulkData> meshPtr = build_mesh(spatialDim, pm);
774 stk::mesh::BulkData& mesh = *meshPtr;
775 stk::mesh::MetaData& meta = meshPtr->mesh_meta_data();
776 stk::mesh::Part& block_1 = meta.declare_part_with_topology("block_1", stk::topology::QUAD_4_2D);
777 mesh.modification_begin();
778 Entity node1, node2, node3, node4, node5, node6;
779 if (myRank == 0)
780 {
781 Entity element = mesh.declare_element(1, stk::mesh::ConstPartVector{&block_1});
782 node1 = mesh.declare_node(1);
783 node2 = mesh.declare_node(2); // shared
784 node3 = mesh.declare_node(3); // shared
785 node4 = mesh.declare_node(4);
786 mesh.declare_relation(element,node1,0);
787 mesh.declare_relation(element,node2,1);
788 mesh.declare_relation(element,node3,2);
789 mesh.declare_relation(element,node4,3);
790 mesh.add_node_sharing(node2,1);
791 mesh.add_node_sharing(node3,1);
792 }
793 if (myRank == 1)
794 {
795 Entity element = mesh.declare_element(2, stk::mesh::ConstPartVector{&block_1});
796 node2 = mesh.declare_node(2); // shared
797 node5 = mesh.declare_node(5);
798 node6 = mesh.declare_node(6);
799 node3 = mesh.declare_node(3); // shared
800 mesh.declare_relation(element,node2,0);
801 mesh.declare_relation(element,node5,1);
802 mesh.declare_relation(element,node6,2);
803 mesh.declare_relation(element,node3,3);
804 mesh.add_node_sharing(node2,0);
805 mesh.add_node_sharing(node3,0);
806 }
807 mesh.modification_end();
808 //nominal test of this only, since this is enforced in many places, impossible to create shared elements
809 EXPECT_EQ(0, stk::mesh::impl::check_no_shared_elements_or_higher(mesh));
810 }
811 }
812
813 void call_get_or_create_face_at_element_side_and_check(stk::mesh::BulkData & mesh, stk::mesh::Entity element, unsigned side_ordinal, unsigned new_face_global_id, stk::mesh::Part & part) {
814 mesh.modification_begin();
815 stk::mesh::PartVector add_parts(1, &part);
816 stk::mesh::Entity new_face = stk::mesh::impl::get_or_create_face_at_element_side(mesh, element, side_ordinal, new_face_global_id, stk::mesh::PartVector(1,&part));
817 mesh.modification_end();
818 ASSERT_TRUE( mesh.is_valid(new_face) );
819 EXPECT_EQ( new_face_global_id, mesh.identifier(new_face) );
820 ASSERT_EQ( 2u, mesh.num_elements(new_face));
821 EXPECT_TRUE( mesh.bucket(new_face).member(part) );
822 }
823
824 TEST( MeshImplUtils, test_create_face_for_sideset)
825 {
826 stk::ParallelMachine pm = MPI_COMM_WORLD;
827 int numProcs = stk::parallel_machine_size(pm);
828 if (numProcs != 1) {
829 return;
830 }
831 stk::io::StkMeshIoBroker fixture(pm);
832
833 fixture.add_mesh_database("generated:1x1x2", stk::io::READ_MESH);
834 fixture.create_input_mesh();
835 stk::topology quad4_topology = stk::topology::QUAD_4;
836 stk::mesh::Part & quad4_part = fixture.meta_data().get_topology_root_part(quad4_topology);
837
838 stk::mesh::Part & new_topology_sub_part = fixture.meta_data().declare_part("My Fancy Part",stk::topology::FACE_RANK);
839 fixture.meta_data().declare_part_subset(quad4_part, new_topology_sub_part);
840 fixture.populate_bulk_data();
841
842 stk::mesh::BulkData & mesh = fixture.bulk_data();
843
844 unsigned elem_global_id = 1;
845 stk::mesh::Entity element = mesh.get_entity(stk::topology::ELEMENT_RANK,elem_global_id);
846 unsigned side_ordinal = 5;
847 unsigned new_face_global_id = 16;
848
849
850 stk::mesh::Entity new_face = mesh.get_entity(stk::topology::FACE_RANK, new_face_global_id);
851 EXPECT_FALSE( mesh.is_valid(new_face) );
852 call_get_or_create_face_at_element_side_and_check(mesh,element,side_ordinal,new_face_global_id,quad4_part);
853 call_get_or_create_face_at_element_side_and_check(mesh,element,side_ordinal,new_face_global_id,quad4_part);
854 call_get_or_create_face_at_element_side_and_check(mesh,element,side_ordinal,new_face_global_id,new_topology_sub_part);
855 }
856
857
858 TEST( MeshImplUtils, test_connect_face_to_other_elements)
859 {
860 stk::ParallelMachine pm = MPI_COMM_WORLD;
861 int numProcs = stk::parallel_machine_size(pm);
862 if (numProcs != 1) {
863 return;
864 }
865 stk::io::StkMeshIoBroker fixture(pm);
866
867 fixture.add_mesh_database("generated:1x1x2", stk::io::READ_MESH);
868 fixture.create_input_mesh();
869 fixture.populate_bulk_data();
870
871 stk::mesh::BulkData & mesh = fixture.bulk_data();
872
873 stk::topology quad4_topology = stk::topology::QUAD_4;
874 stk::mesh::Part & quad4_part = fixture.meta_data().get_topology_root_part(quad4_topology);
875
876 unsigned elem_global_id = 1;
877 stk::mesh::Entity element = mesh.get_entity(stk::topology::ELEMENT_RANK,elem_global_id);
878 unsigned side_ordinal = 5;
879 unsigned new_face_global_id = 16;
880
881 call_get_or_create_face_at_element_side_and_check(mesh,element,side_ordinal,new_face_global_id,quad4_part);
882 stk::mesh::Entity new_face = mesh.get_entity(stk::topology::FACE_RANK, new_face_global_id);
883 ASSERT_TRUE( mesh.is_valid(new_face) );
884 mesh.modification_begin();
885 stk::mesh::impl::connect_face_to_other_elements(mesh,new_face,element,side_ordinal);
886 mesh.modification_end();
887
888 ASSERT_EQ( 2u, mesh.num_elements(new_face));
889 unsigned other_element_global_id = 2;
890 stk::mesh::Entity other_element = mesh.get_entity(stk::topology::ELEMENT_RANK, other_element_global_id);
891 ASSERT_EQ( 1u, mesh.num_faces(other_element) );
892 stk::mesh::Entity attached_face = *mesh.begin_faces(other_element);
893 EXPECT_EQ( new_face, attached_face );
894
895 unsigned attached_side_ordinal = *mesh.begin_face_ordinals(other_element);
896 const unsigned expected_other_element_side_ordinal = 4;
897 EXPECT_EQ( expected_other_element_side_ordinal, attached_side_ordinal );
898 }
899
900
901 TEST( MeshImplUtils, test_create_shell_status) {
902 std::vector<stk::topology> element_topology_vector;
903 element_topology_vector.push_back(stk::topology(stk::topology::HEX_8));
904 stk::topology original_element_topology = stk::topology::HEX_8;
905 std::vector<stk::mesh::impl::ShellStatus> element_shell_status;
906 stk::mesh::impl::create_shell_status(element_topology_vector,original_element_topology,element_shell_status);
907 ASSERT_EQ( 1u, element_shell_status.size() );
908 EXPECT_EQ( stk::mesh::impl::NO_SHELLS, element_shell_status[0] );
909
910 element_topology_vector.push_back(stk::topology(stk::topology::HEX_8));
911 stk::mesh::impl::create_shell_status(element_topology_vector,original_element_topology,element_shell_status);
912 ASSERT_EQ( 2u, element_shell_status.size() );
913 EXPECT_EQ( stk::mesh::impl::NO_SHELLS, element_shell_status[0] );
914 EXPECT_EQ( stk::mesh::impl::NO_SHELLS, element_shell_status[1] );
915
916 element_topology_vector.push_back(stk::topology(stk::topology::SHELL_QUAD_4));
917 stk::mesh::impl::create_shell_status(element_topology_vector,original_element_topology,element_shell_status);
918 ASSERT_EQ( 3u, element_shell_status.size() );
919 EXPECT_EQ( stk::mesh::impl::YES_SHELLS_BOTH_SHELLS_OR_BOTH_SOLIDS, element_shell_status[0] );
920 EXPECT_EQ( stk::mesh::impl::YES_SHELLS_BOTH_SHELLS_OR_BOTH_SOLIDS, element_shell_status[1] );
921 EXPECT_EQ( stk::mesh::impl::YES_SHELLS_ONE_SHELL_ONE_SOLID, element_shell_status[2] );
922
923 original_element_topology = stk::topology::SHELL_QUAD_4;
924 stk::mesh::impl::create_shell_status(element_topology_vector,original_element_topology,element_shell_status);
925 ASSERT_EQ( 3u, element_shell_status.size() );
926 EXPECT_EQ( stk::mesh::impl::YES_SHELLS_ONE_SHELL_ONE_SOLID, element_shell_status[0] );
927 EXPECT_EQ( stk::mesh::impl::YES_SHELLS_ONE_SHELL_ONE_SOLID, element_shell_status[1] );
928 EXPECT_EQ( stk::mesh::impl::YES_SHELLS_BOTH_SHELLS_OR_BOTH_SOLIDS, element_shell_status[2] );
929
930 }
931
932 TEST( MeshImplUtils, test_connect_face_to_other_elements_2)
933 {
934 std::vector<int> face_nodes(4);
935 face_nodes[0] = 5;
936 face_nodes[1] = 6;
937 face_nodes[2] = 7;
938 face_nodes[3] = 8;
939
940 stk::topology element_side_topology = stk::topology::QUAD_4;
941
942 std::vector<int> element_side_nodes(4);
943 element_side_nodes[0] = 5;
944 element_side_nodes[1] = 6;
945 element_side_nodes[2] = 7;
946 element_side_nodes[3] = 8;
947
948 EXPECT_TRUE(stk::mesh::impl::should_face_be_connected_to_element_side(face_nodes,element_side_nodes,element_side_topology,stk::mesh::impl::NO_SHELLS));
949 EXPECT_TRUE(stk::mesh::impl::should_face_be_connected_to_element_side(face_nodes,element_side_nodes,element_side_topology,stk::mesh::impl::YES_SHELLS_BOTH_SHELLS_OR_BOTH_SOLIDS));
950 EXPECT_FALSE(stk::mesh::impl::should_face_be_connected_to_element_side(face_nodes,element_side_nodes,element_side_topology,stk::mesh::impl::YES_SHELLS_ONE_SHELL_ONE_SOLID));
951
952 element_side_nodes[0] = 5;
953 element_side_nodes[1] = 8;
954 element_side_nodes[2] = 7;
955 element_side_nodes[3] = 6;
956
957 EXPECT_TRUE(stk::mesh::impl::should_face_be_connected_to_element_side(face_nodes,element_side_nodes,element_side_topology,stk::mesh::impl::NO_SHELLS));
958 EXPECT_FALSE(stk::mesh::impl::should_face_be_connected_to_element_side(face_nodes,element_side_nodes,element_side_topology,stk::mesh::impl::YES_SHELLS_BOTH_SHELLS_OR_BOTH_SOLIDS));
959 EXPECT_TRUE(stk::mesh::impl::should_face_be_connected_to_element_side(face_nodes,element_side_nodes,element_side_topology,stk::mesh::impl::YES_SHELLS_ONE_SHELL_ONE_SOLID));
960
961 element_side_nodes[0] = 5;
962 element_side_nodes[1] = 6;
963 element_side_nodes[2] = 7;
964 element_side_nodes[3] = 9;
965
966 EXPECT_FALSE(stk::mesh::impl::should_face_be_connected_to_element_side(face_nodes,element_side_nodes,element_side_topology,stk::mesh::impl::NO_SHELLS));
967 EXPECT_FALSE(stk::mesh::impl::should_face_be_connected_to_element_side(face_nodes,element_side_nodes,element_side_topology,stk::mesh::impl::YES_SHELLS_BOTH_SHELLS_OR_BOTH_SOLIDS));
968 EXPECT_FALSE(stk::mesh::impl::should_face_be_connected_to_element_side(face_nodes,element_side_nodes,element_side_topology,stk::mesh::impl::YES_SHELLS_ONE_SHELL_ONE_SOLID));
969
970 face_nodes[0] = 5;
971 face_nodes[1] = 8;
972 face_nodes[2] = 7;
973 face_nodes[3] = 6;
974
975 element_side_nodes[0] = 5;
976 element_side_nodes[1] = 6;
977 element_side_nodes[2] = 7;
978 element_side_nodes[3] = 8;
979
980 EXPECT_TRUE(stk::mesh::impl::should_face_be_connected_to_element_side(face_nodes,element_side_nodes,element_side_topology,stk::mesh::impl::NO_SHELLS));
981 EXPECT_FALSE(stk::mesh::impl::should_face_be_connected_to_element_side(face_nodes,element_side_nodes,element_side_topology,stk::mesh::impl::YES_SHELLS_BOTH_SHELLS_OR_BOTH_SOLIDS));
982 EXPECT_TRUE(stk::mesh::impl::should_face_be_connected_to_element_side(face_nodes,element_side_nodes,element_side_topology,stk::mesh::impl::YES_SHELLS_ONE_SHELL_ONE_SOLID));
983
984 element_side_nodes[0] = 5;
985 element_side_nodes[1] = 8;
986 element_side_nodes[2] = 7;
987 element_side_nodes[3] = 6;
988
989 EXPECT_TRUE(stk::mesh::impl::should_face_be_connected_to_element_side(face_nodes,element_side_nodes,element_side_topology,stk::mesh::impl::NO_SHELLS));
990 EXPECT_TRUE(stk::mesh::impl::should_face_be_connected_to_element_side(face_nodes,element_side_nodes,element_side_topology,stk::mesh::impl::YES_SHELLS_BOTH_SHELLS_OR_BOTH_SOLIDS));
991 EXPECT_FALSE(stk::mesh::impl::should_face_be_connected_to_element_side(face_nodes,element_side_nodes,element_side_topology,stk::mesh::impl::YES_SHELLS_ONE_SHELL_ONE_SOLID));
992 }