Debian Codegraph @

packages/stk/stk_unit_tests/stk_mesh/UnitTestBulkDataSharing.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 // ####################### Start Clang Header Tool Managed Headers ########################
36 // clang-format off
37 #include <gtest/gtest.h> // for AssertHelper, etc
38 #include <stddef.h> // for size_t
39 #include <iostream> // for operator<<, etc
40 #include <stk_mesh/base/BulkData.hpp> // for BulkData
41 #include <stk_mesh/base/MeshBuilder.hpp>
42 #include <stk_util/parallel/Parallel.hpp> // for ParallelMachine
43 #include <string> // for string, etc
44 #include <vector> // for vector
45 #include "mpi.h" // for MPI_COMM_WORLD, etc
46 #include "stk_mesh/base/Entity.hpp" // for Entity, etc
47 #include "stk_mesh/base/EntityKey.hpp" // for operator<<, etc
48 #include "stk_mesh/base/GetEntities.hpp" // for count_entities
49 #include "stk_mesh/base/MetaData.hpp" // for MetaData, etc
50 #include "stk_mesh/base/Types.hpp" // for EntityVector, etc
51 #include "stk_topology/topology.hpp" // for topology, etc
52 #include "stk_io/FillMesh.hpp"
53
54 namespace stk { namespace mesh { class Part; } }
55 namespace stk { namespace mesh { class Selector; } }
56 // clang-format on
57 // ####################### End Clang Header Tool Managed Headers ########################
58
59 using stk::mesh::MetaData;
60 using stk::mesh::BulkData;
61 using stk::mesh::MeshBuilder;
62 using stk::mesh::Selector;
63 using stk::mesh::PartVector;
64 using stk::mesh::Entity;
65 using stk::mesh::EntityId;
66 using stk::mesh::EntityKey;
67 using stk::mesh::EntityVector;
68 using stk::mesh::EntityRank;
69
70 TEST(UnitTestingOfBulkData, node_sharing)
71 {
72 //
73 // Test to make sure user-provided node sharing information is propagated
74 // correctly. This test manually builds up a 2D mesh of 2x2 quad elements,
75 // and only runs on 4 processors (one element per processor). This creates
76 // the special case of a central node that has 4-way sharing between all
77 // processors.
78 //
79 // O-----------O-----------O
80 // |10 |20 |31
81 // | | |
82 // p0 | 100 | 101 | p1
83 // | | |
84 // | | |
85 // O-----------O-----------O
86 // |40 |50 |61
87 // | | |
88 // p2 | 102 | 103 | p3
89 // | | |
90 // | | |
91 // O-----------O-----------O
92 // 72 82 93
93 //
94 // Numbering Scheme:
95 // Element ID: 103 Node ID: 61
96 // ^ ^^
97 // | ||
98 // \--- Owning proc (3) |\--- Owning proc (1)
99 // \---- Node number (6)
100 //
101 // Connectivity information for each processor:
102 // {elem, node0, node1, node2, node3}
103 EntityId connInfo[][5] = { {100, 10, 40, 50, 20}, // p0
104 {101, 20, 50, 61, 31}, // p1
105 {102, 40, 72, 82, 50}, // p2
106 {103, 50, 82, 93, 61} }; // p3
107
108 // Sharing information for each created node for each processor. Data
109 // is ordered in pairs as:
110 // {node, sharingProc, node, sharingProc, ...}
111 const unsigned numSharings = 5u;
112 EntityId sharingInfo[][10] = { {20, 1, 40, 2, 50, 1, 50, 2, 50, 3}, // p0
113 {20, 0, 61, 3, 50, 0, 50, 2, 50, 3}, // p1
114 {40, 0, 82, 3, 50, 0, 50, 1, 50, 3}, // p2
115 {61, 1, 82, 2, 50, 0, 50, 1, 50, 2} }; // p3
116
117 // Set up meta and bulk data
118 const unsigned spatial_dim = 2;
119 stk::ParallelMachine pm = MPI_COMM_WORLD;
120 MeshBuilder builder(pm);
121 builder.set_spatial_dimension(spatial_dim);
122 std::shared_ptr<BulkData> meshPtr = builder.create();
123 BulkData& mesh = *meshPtr;
124 MetaData& meta_data = mesh.mesh_meta_data();
125 stk::mesh::Part& elem_part = meta_data.declare_part_with_topology("elem_part", stk::topology::QUAD_4_2D);
126 stk::mesh::Part& node_part = meta_data.declare_part_with_topology("node_part", stk::topology::NODE);
127 meta_data.commit();
128
129 int p_rank = mesh.parallel_rank();
130 int p_size = mesh.parallel_size();
131
132 // Only run in 4-processor configuration
133 if (p_size != 4) return;
134
135
136 mesh.modification_begin();
137
138 // Create the single element and four nodes for this processor. Nodes
139 // on the boundary with other processors will be shared.
140 //
141 Entity createdElem = mesh.declare_element(connInfo[p_rank][0], stk::mesh::ConstPartVector{&elem_part});
142
143 // Create all 4 nodes for this element
144 EntityVector createdNodes;
145 createdNodes.push_back( mesh.declare_node(connInfo[p_rank][1], stk::mesh::ConstPartVector{&node_part}) );
146 createdNodes.push_back( mesh.declare_node(connInfo[p_rank][2], stk::mesh::ConstPartVector{&node_part}) );
147 createdNodes.push_back( mesh.declare_node(connInfo[p_rank][3], stk::mesh::ConstPartVector{&node_part}) );
148 createdNodes.push_back( mesh.declare_node(connInfo[p_rank][4], stk::mesh::ConstPartVector{&node_part}) );
149
150 // Add relations to nodes
151 mesh.declare_relation( createdElem, createdNodes[0], 0 );
152 mesh.declare_relation( createdElem, createdNodes[1], 1 );
153 mesh.declare_relation( createdElem, createdNodes[2], 2 );
154 mesh.declare_relation( createdElem, createdNodes[3], 3 );
155
156 // Declare all processors that share any of our nodes
157 std::ostringstream oss;
158 for (unsigned i = 0; i < numSharings; ++i)
159 {
160 Entity node = mesh.get_entity(stk::topology::NODE_RANK, sharingInfo[p_rank][i*2]);
161 oss << "P" << p_rank << ": entity " << node << " = " << mesh.entity_key(node) << std::endl;
162 int sharingProc = sharingInfo[p_rank][i*2+1];
163 mesh.add_node_sharing(node, sharingProc);
164 }
165 std::cout << oss.str() << std::flush;
166
167 mesh.modification_end();
168
169 // Make sure we know about all nodes and elements (including aura, which
170 // includes *all* entities in our small mesh)
171 //
172 std::vector<size_t> countsAll;
173 count_entities(meta_data.universal_part(), mesh, countsAll);
174
175 EXPECT_EQ( 9u, countsAll[stk::topology::NODE_RANK] );
176 EXPECT_EQ( 4u, countsAll[stk::topology::ELEM_RANK] );
177
178 // Count how many entities each proc owns, which will be different for each
179 // proc (because of the lower parallel rank owning shared nodes on the
180 // bondaries). Check values in the processor-specific sections below.
181 //
182 std::vector<size_t> countsOwned;
183 count_entities(meta_data.locally_owned_part(), mesh, countsOwned);
184
185 std::vector<int> sharingProcs;
186
187 if (p_rank == 0)
188 {
189 EXPECT_EQ( 4u, countsOwned[stk::topology::NODE_RANK] );
190
191 mesh.comm_shared_procs(mesh.entity_key(createdNodes[0]), sharingProcs);
192 EXPECT_TRUE( sharingProcs.empty() );
193
194 mesh.comm_shared_procs(mesh.entity_key(createdNodes[1]), sharingProcs);
195 EXPECT_EQ( 1u, sharingProcs.size() );
196 EXPECT_EQ( 2, sharingProcs[0] );
197
198 mesh.comm_shared_procs(mesh.entity_key(createdNodes[2]), sharingProcs);
199 EXPECT_EQ( 3u, sharingProcs.size() );
200 EXPECT_EQ( 1, sharingProcs[0] );
201 EXPECT_EQ( 2, sharingProcs[1] );
202 EXPECT_EQ( 3, sharingProcs[2] );
203
204 mesh.comm_shared_procs(mesh.entity_key(createdNodes[3]), sharingProcs);
205 EXPECT_EQ( 1u, sharingProcs.size() );
206 EXPECT_EQ( 1, sharingProcs[0] );
207 }
208 else if (p_rank == 1)
209 {
210 EXPECT_EQ( 2u, countsOwned[stk::topology::NODE_RANK] );
211
212 mesh.comm_shared_procs(mesh.entity_key(createdNodes[0]), sharingProcs);
213 EXPECT_EQ( 1u, sharingProcs.size() );
214 EXPECT_EQ( 0, sharingProcs[0] );
215
216 mesh.comm_shared_procs(mesh.entity_key(createdNodes[1]), sharingProcs);
217 EXPECT_EQ( 3u, sharingProcs.size() );
218 EXPECT_EQ( 0, sharingProcs[0] );
219 EXPECT_EQ( 2, sharingProcs[1] );
220 EXPECT_EQ( 3, sharingProcs[2] );
221
222 mesh.comm_shared_procs(mesh.entity_key(createdNodes[2]), sharingProcs);
223 EXPECT_EQ( 1u, sharingProcs.size() );
224 EXPECT_EQ( 3, sharingProcs[0] );
225
226 mesh.comm_shared_procs(mesh.entity_key(createdNodes[3]), sharingProcs);
227 EXPECT_TRUE( sharingProcs.empty() );
228 }
229 else if (p_rank == 2)
230 {
231 EXPECT_EQ( 2u, countsOwned[stk::topology::NODE_RANK] );
232
233 mesh.comm_shared_procs(mesh.entity_key(createdNodes[0]), sharingProcs);
234 EXPECT_EQ( 1u, sharingProcs.size() );
235 EXPECT_EQ( 0, sharingProcs[0] );
236
237 mesh.comm_shared_procs(mesh.entity_key(createdNodes[1]), sharingProcs);
238 EXPECT_TRUE( sharingProcs.empty() );
239
240 mesh.comm_shared_procs(mesh.entity_key(createdNodes[2]), sharingProcs);
241 EXPECT_EQ( 1u, sharingProcs.size() );
242 EXPECT_EQ( 3, sharingProcs[0] );
243
244 mesh.comm_shared_procs(mesh.entity_key(createdNodes[3]), sharingProcs);
245 EXPECT_EQ( 3u, sharingProcs.size() );
246 EXPECT_EQ( 0, sharingProcs[0] );
247 EXPECT_EQ( 1, sharingProcs[1] );
248 EXPECT_EQ( 3, sharingProcs[2] );
249 }
250 else if (p_rank == 3)
251 {
252 EXPECT_EQ( 1u, countsOwned[stk::topology::NODE_RANK] );
253
254 mesh.comm_shared_procs(mesh.entity_key(createdNodes[0]), sharingProcs);
255 EXPECT_EQ( 3u, sharingProcs.size());
256 EXPECT_EQ( 0, sharingProcs[0] );
257 EXPECT_EQ( 1, sharingProcs[1] );
258 EXPECT_EQ( 2, sharingProcs[2] );
259
260 mesh.comm_shared_procs(mesh.entity_key(createdNodes[1]), sharingProcs);
261 EXPECT_EQ( 1u, sharingProcs.size() );
262 EXPECT_EQ( 2, sharingProcs[0] );
263
264 mesh.comm_shared_procs(mesh.entity_key(createdNodes[2]), sharingProcs);
265 EXPECT_TRUE( sharingProcs.empty() );
266
267 mesh.comm_shared_procs(mesh.entity_key(createdNodes[3]), sharingProcs);
268 EXPECT_EQ( 1u, sharingProcs.size() );
269 EXPECT_EQ( 1, sharingProcs[0] );
270 }
271 }
272
273 TEST(UnitTestingOfBulkData, sharedProcsIntersection)
274 {
275 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 2) {
276 return;
277 }
278
279 unsigned spatialDim = 3;
280 MeshBuilder builder(MPI_COMM_WORLD);
281 builder.set_spatial_dimension(spatialDim);
282 std::shared_ptr<BulkData> bulkPtr = builder.create();
283 stk::mesh::BulkData& bulk = *bulkPtr;
284 stk::io::fill_mesh("generated:1x1x4", bulk);
285
286 stk::mesh::Entity sharedNode9 = bulk.get_entity(stk::topology::NODE_RANK, 9);
287 stk::mesh::Entity sharedNode10 = bulk.get_entity(stk::topology::NODE_RANK, 10);
288 stk::mesh::EntityId unsharedNodeId = bulk.parallel_rank()==0 ? 1 : 13;
289 stk::mesh::Entity unsharedNode = bulk.get_entity(stk::topology::NODE_RANK, unsharedNodeId);
290
291 EXPECT_TRUE(bulk.bucket(sharedNode9).shared());
292 EXPECT_TRUE(bulk.bucket(sharedNode10).shared());
293 EXPECT_FALSE(bulk.bucket(unsharedNode).shared());
294 EXPECT_FALSE(bulk.bucket(unsharedNode).in_aura());
295
296 stk::mesh::EntityVector nodes;
297 std::vector<int> sharingProcs;
298
299 bulk.shared_procs_intersection(nodes, sharingProcs);
300 EXPECT_TRUE(sharingProcs.empty());
301
302 nodes = {sharedNode9};
303 bulk.shared_procs_intersection(nodes, sharingProcs);
304 EXPECT_EQ(1u, sharingProcs.size());
305 const int otherProc = 1 - bulk.parallel_rank();
306 EXPECT_EQ(otherProc, sharingProcs[0]);
307
308 nodes = {unsharedNode};
309 bulk.shared_procs_intersection(nodes, sharingProcs);
310 EXPECT_TRUE(sharingProcs.empty());
311
312 nodes = {unsharedNode, sharedNode9};
313 bulk.shared_procs_intersection(nodes, sharingProcs);
314 EXPECT_TRUE(sharingProcs.empty());
315
316 nodes = {sharedNode9, unsharedNode};
317 bulk.shared_procs_intersection(nodes, sharingProcs);
318 EXPECT_TRUE(sharingProcs.empty());
319
320 nodes = {sharedNode9, sharedNode10};
321 bulk.shared_procs_intersection(nodes, sharingProcs);
322 EXPECT_EQ(1u, sharingProcs.size());
323 EXPECT_EQ(otherProc, sharingProcs[0]);
324 }
325
326 TEST(UnitTestingOfBulkData, node_sharing_with_dangling_nodes)
327 {
328 //
329 // Test to make sure user-provided node sharing information is propagated
330 // correctly. This test manually builds up a 2D mesh of 2x2 quad elements,
331 // and only runs on 4 processors (one element per processor). This creates
332 // the special case of a central node that has 4-way sharing between all
333 // processors.
334 //
335 // Test is same as node_sharing, but creates dangling nodes emulating
336 // what happens in Percept adaptivity - extra nodes 100 and 113 created
337 //
338 //
339 // O-----------O-----------O
340 // |10 |20 |31
341 // | | |
342 // p0 | 100 | 101 | p1
343 // | | |
344 // | | |
345 // O---100-----O----113----O
346 // |40 |50 |61
347 // | | |
348 // p2 | 102 | 103 | p3
349 // | | |
350 // | | |
351 // O-----------O-----------O
352 // 72 82 93
353 //
354 // Numbering Scheme:
355 // Element ID: 103 Node ID: 61
356 // ^ ^^
357 // | ||
358 // \--- Owning proc (3) |\--- Owning proc (1)
359 // \---- Node number (6)
360 //
361 // Connectivity information for each processor:
362 // {elem, node0, node1, node2, node3}
363 EntityId connInfo[][5] = { {100, 10, 40, 50, 20}, // p0
364 {101, 20, 50, 61, 31}, // p1
365 {102, 40, 72, 82, 50}, // p2
366 {103, 50, 82, 93, 61} }; // p3
367
368 // Sharing information for each created node for each processor. Data
369 // is ordered in pairs as:
370 // {node, sharingProc, node, sharingProc, ...}
371 const unsigned numSharings = 5u;
372 EntityId sharingInfo[][10] = { {20, 1, 40, 2, 50, 1, 50, 2, 50, 3}, // p0
373 {20, 0, 61, 3, 50, 0, 50, 2, 50, 3}, // p1
374 {40, 0, 82, 3, 50, 0, 50, 1, 50, 3}, // p2
375 {61, 1, 82, 2, 50, 0, 50, 1, 50, 2} }; // p3
376
377 const unsigned numSharings1 = 1u;
378 EntityId sharingInfo1[][10] = { {100, 2} , // p0
379 {113, 3} , // p1
380 {100, 0} , // p2
381 {113, 1} }; // p3
382
383 // Set up meta and bulk data
384 const unsigned spatial_dim = 2;
385 stk::ParallelMachine pm = MPI_COMM_WORLD;
386 MeshBuilder builder(pm);
387 builder.set_spatial_dimension(spatial_dim);
388 std::shared_ptr<BulkData> meshPtr = builder.create();
389 BulkData& mesh = *meshPtr;
390 MetaData& meta_data = mesh.mesh_meta_data();
391 stk::mesh::Part& elem_part = meta_data.declare_part_with_topology("elem_part", stk::topology::QUAD_4_2D);
392 stk::mesh::Part& node_part = meta_data.declare_part_with_topology("node_part", stk::topology::NODE);
393 meta_data.commit();
394
395 int p_rank = mesh.parallel_rank();
396 int p_size = mesh.parallel_size();
397
398 // Only run in 4-processor configuration
399 if (p_size != 4) return;
400
401
402 mesh.modification_begin();
403
404 // Create the single element and four nodes for this processor. Nodes
405 // on the boundary with other processors will be shared.
406 //
407 Entity createdElem = mesh.declare_element(connInfo[p_rank][0], stk::mesh::ConstPartVector{&elem_part});
408
409 // Create all 4 nodes for this element
410 EntityVector createdNodes;
411 createdNodes.push_back( mesh.declare_node(connInfo[p_rank][1], stk::mesh::ConstPartVector{&node_part}) );
412 createdNodes.push_back( mesh.declare_node(connInfo[p_rank][2], stk::mesh::ConstPartVector{&node_part}) );
413 createdNodes.push_back( mesh.declare_node(connInfo[p_rank][3], stk::mesh::ConstPartVector{&node_part}) );
414 createdNodes.push_back( mesh.declare_node(connInfo[p_rank][4], stk::mesh::ConstPartVector{&node_part}) );
415
416
417 bool doNew = true;
418 if (doNew)
419 {
420 mesh.declare_node(sharingInfo1[p_rank][0], stk::mesh::ConstPartVector{&node_part}) ;
421 std::cout << "P[" << p_rank << "] declared node= " << sharingInfo1[p_rank][0] << std::endl;
422 }
423
424 // Add relations to nodes
425 mesh.declare_relation( createdElem, createdNodes[0], 0 );
426 mesh.declare_relation( createdElem, createdNodes[1], 1 );
427 mesh.declare_relation( createdElem, createdNodes[2], 2 );
428 mesh.declare_relation( createdElem, createdNodes[3], 3 );
429
430 // Declare all processors that share any of our nodes
431 for (unsigned i = 0; i < numSharings; ++i)
432 {
433 Entity node = mesh.get_entity(stk::topology::NODE_RANK, sharingInfo[p_rank][i*2]);
434 int sharingProc = sharingInfo[p_rank][i*2+1];
435 mesh.add_node_sharing(node, sharingProc);
436 }
437
438 if (doNew)
439 {
440 for (unsigned i = 0; i < numSharings1; ++i)
441 {
442 int sharingProc = sharingInfo1[p_rank][i*2+1];
443 Entity node = mesh.get_entity(stk::topology::NODE_RANK, sharingInfo1[p_rank][i*2]);
444 std::cout << "P[" << p_rank << "] add_node_sharing node= " << sharingInfo1[p_rank][i*2] << " sharingProc= " << sharingProc << " is_valid= " << mesh.is_valid(node) << std::endl;
445 mesh.add_node_sharing(node, sharingProc);
446 }
447 }
448
449 EXPECT_NO_THROW(mesh.modification_end());
450
451 // Make sure we know about all nodes and elements (including aura, which
452 // includes *all* entities in our small mesh)
453 //
454 std::vector<size_t> countsAll;
455 count_entities(meta_data.universal_part(), mesh, countsAll);
456
457 EXPECT_EQ( 10u, countsAll[stk::topology::NODE_RANK] ); //
458 EXPECT_EQ( 4u, countsAll[stk::topology::ELEM_RANK] );
459
460 // Count how many entities each proc owns, which will be different for each
461 // proc (because of the lower parallel rank owning shared nodes on the
462 // bondaries). Check values in the processor-specific sections below.
463 //
464 std::vector<size_t> countsOwned;
465 count_entities(meta_data.locally_owned_part(), mesh, countsOwned);
466
467 std::vector<int> sharingProcs;
468
469 if (p_rank == 0)
470 {
471 EXPECT_EQ( 5u, countsOwned[stk::topology::NODE_RANK] ); //
472
473 mesh.comm_shared_procs(mesh.entity_key(createdNodes[0]), sharingProcs);
474 EXPECT_TRUE( sharingProcs.empty() );
475
476 mesh.comm_shared_procs(mesh.entity_key(createdNodes[1]), sharingProcs);
477 EXPECT_EQ( 1u, sharingProcs.size() );
478 EXPECT_EQ( 2, sharingProcs[0] );
479
480 mesh.comm_shared_procs(mesh.entity_key(createdNodes[2]), sharingProcs);
481 EXPECT_EQ( 3u, sharingProcs.size() );
482 EXPECT_EQ( 1, sharingProcs[0] );
483 EXPECT_EQ( 2, sharingProcs[1] );
484 EXPECT_EQ( 3, sharingProcs[2] );
485
486 mesh.comm_shared_procs(mesh.entity_key(createdNodes[3]), sharingProcs);
487 EXPECT_EQ( 1u, sharingProcs.size() );
488 EXPECT_EQ( 1, sharingProcs[0] );
489 }
490 else if (p_rank == 1)
491 {
492 EXPECT_EQ( 3u, countsOwned[stk::topology::NODE_RANK] ); //
493
494 mesh.comm_shared_procs(mesh.entity_key(createdNodes[0]), sharingProcs);
495 EXPECT_EQ( 1u, sharingProcs.size() );
496 EXPECT_EQ( 0, sharingProcs[0] );
497
498 mesh.comm_shared_procs(mesh.entity_key(createdNodes[1]), sharingProcs);
499 EXPECT_EQ( 3u, sharingProcs.size() );
500 EXPECT_EQ( 0, sharingProcs[0] );
501 EXPECT_EQ( 2, sharingProcs[1] );
502 EXPECT_EQ( 3, sharingProcs[2] );
503
504 mesh.comm_shared_procs(mesh.entity_key(createdNodes[2]), sharingProcs);
505 EXPECT_EQ( 1u, sharingProcs.size() );
506 EXPECT_EQ( 3, sharingProcs[0] );
507
508 mesh.comm_shared_procs(mesh.entity_key(createdNodes[3]), sharingProcs);
509 EXPECT_TRUE( sharingProcs.empty() );
510 }
511 else if (p_rank == 2)
512 {
513 EXPECT_EQ( 2u, countsOwned[stk::topology::NODE_RANK] );
514
515 mesh.comm_shared_procs(mesh.entity_key(createdNodes[0]), sharingProcs);
516 EXPECT_EQ( 1u, sharingProcs.size() );
517 EXPECT_EQ( 0, sharingProcs[0] );
518
519 mesh.comm_shared_procs(mesh.entity_key(createdNodes[1]), sharingProcs);
520 EXPECT_TRUE( sharingProcs.empty() );
521
522 mesh.comm_shared_procs(mesh.entity_key(createdNodes[2]), sharingProcs);
523 EXPECT_EQ( 1u, sharingProcs.size() );
524 EXPECT_EQ( 3, sharingProcs[0] );
525
526 mesh.comm_shared_procs(mesh.entity_key(createdNodes[3]), sharingProcs);
527 EXPECT_EQ( 3u, sharingProcs.size() );
528 EXPECT_EQ( 0, sharingProcs[0] );
529 EXPECT_EQ( 1, sharingProcs[1] );
530 EXPECT_EQ( 3, sharingProcs[2] );
531 }
532 else if (p_rank == 3)
533 {
534 EXPECT_EQ( 1u, countsOwned[stk::topology::NODE_RANK] );
535
536 mesh.comm_shared_procs(mesh.entity_key(createdNodes[0]), sharingProcs);
537 EXPECT_EQ( 3u, sharingProcs.size());
538 EXPECT_EQ( 0, sharingProcs[0] );
539 EXPECT_EQ( 1, sharingProcs[1] );
540 EXPECT_EQ( 2, sharingProcs[2] );
541
542 mesh.comm_shared_procs(mesh.entity_key(createdNodes[1]), sharingProcs);
543 EXPECT_EQ( 1u, sharingProcs.size() );
544 EXPECT_EQ( 2, sharingProcs[0] );
545
546 mesh.comm_shared_procs(mesh.entity_key(createdNodes[2]), sharingProcs);
547 EXPECT_TRUE( sharingProcs.empty() );
548
549 mesh.comm_shared_procs(mesh.entity_key(createdNodes[3]), sharingProcs);
550 EXPECT_EQ( 1u, sharingProcs.size() );
551 EXPECT_EQ( 1, sharingProcs[0] );
552 }
553 }
554
555 void move_elem(stk::mesh::BulkData& bulk, stk::mesh::EntityId id,
556 stk::mesh::Part& addPart, stk::mesh::Part& rmPart)
557 {
558 bulk.modification_begin();
559 stk::mesh::PartVector addParts = {&addPart};
560 stk::mesh::PartVector rmParts = {&rmPart};
561
562 stk::mesh::Entity elem = bulk.get_entity(stk::topology::ELEM_RANK, id);
563 if (bulk.is_valid(elem) && bulk.bucket(elem).owned()) {
564 bulk.change_entity_parts(elem, addParts, rmParts);
565 }
566 bulk.modification_end();
567 }
568
569 void create_faces(stk::mesh::BulkData& bulk)
570 {
571 bulk.modification_begin();
572
573 if (bulk.parallel_rank() == 0) {
574 stk::mesh::Entity elem1 = bulk.get_entity(stk::topology::ELEM_RANK, 1);
575 stk::mesh::Entity elem2 = bulk.get_entity(stk::topology::ELEM_RANK, 2);
576 stk::mesh::Entity elem3 = bulk.get_entity(stk::topology::ELEM_RANK, 3);
577 ASSERT_TRUE(bulk.is_valid(elem1) && bulk.is_valid(elem2) && bulk.is_valid(elem3));
578 stk::mesh::ConnectivityOrdinal sideOrdinal = 5;
579 bulk.declare_element_side(elem1, sideOrdinal, stk::mesh::ConstPartVector{});
580 bulk.declare_element_side(elem2, sideOrdinal, stk::mesh::ConstPartVector{});
581 bulk.declare_element_side(elem3, sideOrdinal, stk::mesh::ConstPartVector{});
582 }
583
584 bulk.modification_end();
585 }
586
587 void detach_face(stk::mesh::BulkData& bulk, stk::mesh::EntityId elemId)
588 {
589 bulk.modification_begin();
590 stk::mesh::Entity elem = bulk.get_entity(stk::topology::ELEM_RANK, elemId);
591 if (bulk.is_valid(elem) && bulk.bucket(elem).owned()) {
592 unsigned numFaces = bulk.num_faces(elem);
593 ASSERT_EQ(1u, numFaces);
594 const stk::mesh::Entity* faces = bulk.begin_faces(elem);
595 stk::mesh::Entity face = faces[0];
596 const stk::mesh::ConnectivityOrdinal* ords = bulk.begin_face_ordinals(elem);
597 stk::mesh::ConnectivityOrdinal ord = ords[0];
598 bulk.destroy_relation(elem, face, ord);
599 }
600 bulk.modification_end();
601 }
602
603 void assert_shared_faces(const stk::mesh::BulkData& bulk, size_t expectedNumSharedFaces)
604 {
605 stk::mesh::Selector shared = bulk.mesh_meta_data().globally_shared_part();
606 ASSERT_TRUE(expectedNumSharedFaces == stk::mesh::count_entities(bulk, stk::topology::FACE_RANK, shared));
607 }
608
609 TEST(UnitTestBulkData, resolveSharedModifiedFaceNextToUnmodifiedFaces)
610 {
611 stk::ParallelMachine comm = MPI_COMM_WORLD;
612 if (stk::parallel_machine_size(comm) != 2) { GTEST_SKIP(); }
613
614 MeshBuilder builder(comm);
615 builder.set_aura_option(stk::mesh::BulkData::NO_AUTO_AURA);
616 builder.set_spatial_dimension(3);
617 std::shared_ptr<BulkData> bulkPtr = builder.create();
618 stk::mesh::BulkData& bulk = *bulkPtr;
619 stk::mesh::MetaData& meta = bulk.mesh_meta_data();
620 stk::mesh::Part& block2 = meta.declare_part_with_topology("block_2", stk::topology::HEX_8);
621 stk::io::fill_mesh("generated:3x1x2", bulk);
622 stk::mesh::Part& block1 = *meta.get_part("block_1");
623
624 move_elem(bulk, 2, block2, block1);
625
626 create_faces(bulk);
627 assert_shared_faces(bulk, 3);
628
629 detach_face(bulk, 2);
630 assert_shared_faces(bulk, 3);
631 }
632