Debian Codegraph @

packages/stk/stk_unit_tests/stk_mesh/UnitTestBulkData.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 <stddef.h> // for size_t
35 #include <stdlib.h> // for exit
36 #include <exception> // for exception
37 #include <iostream> // for ostringstream, etc
38 #include <iterator> // for distance
39 #include <map> // for _Rb_tree_const_iterator, etc
40 #include <stdexcept> // for logic_error, runtime_error
41 #include <algorithm> // for sort
42 #include <stk_mesh/base/BulkData.hpp> // for BulkData, etc
43 #include <stk_mesh/base/FindPermutation.hpp>
44 #include <stk_mesh/base/GetEntities.hpp> // for count_entities, etc
45 #include <stk_mesh/base/FieldParallel.hpp>
46 #include <stk_mesh/base/CreateEdges.hpp>
47 #include <stk_mesh/base/MeshBuilder.hpp>
48
49 #include "Setup8Quad4ProcMesh.hpp"
50 #include "UnitTestCEO2Elem.hpp"
51 #include "UnitTestCEO3Elem.hpp"
52 #include "UnitTestCEO4ElemEdge.hpp"
53 #include "UnitTestCEO4ElemRotate.hpp"
54 #include "UnitTestCEO8Elem.hpp"
55 #include "UnitTestCEOCommonUtils.hpp"
56 #include "UnitTestRingFixture.hpp" // for test_shift_ring
57 #include "stk_io/StkMeshIoBroker.hpp"
58 #include "stk_mesh/base/Bucket.hpp" // for Bucket, has_superset
59 #include "stk_mesh/base/Entity.hpp" // for Entity
60 #include "stk_mesh/base/EntityKey.hpp" // for EntityKey
61 #include "stk_mesh/base/Field.hpp" // for Field
62 #include "stk_mesh/base/FieldBase.hpp" // for field_data, etc
63 #include "stk_mesh/base/Ghosting.hpp" // for Ghosting
64 #include "stk_mesh/base/MetaData.hpp" // for MetaData, entity_rank_names, etc
65 #include "stk_mesh/base/Part.hpp" // for Part
66 #include "stk_mesh/base/Relation.hpp"
67 #include "stk_mesh/base/Selector.hpp" // for Selector, operator|
68 #include "stk_mesh/base/Types.hpp" // for EntityProc, EntityVector, etc
69 #include "stk_mesh/baseImpl/MeshImplUtils.hpp"
70 #include "stk_mesh/baseImpl/MeshCommImplUtils.hpp"
71 #include "stk_mesh/baseImpl/EntityGhostData.hpp"
72 #include "stk_topology/topology.hpp" // for topology, etc
73 #include "stk_unit_test_utils/stk_mesh_fixtures/BoxFixture.hpp" // for BoxFixture
74 #include "stk_unit_test_utils/stk_mesh_fixtures/TestHexFixture.hpp"
75 #include "stk_unit_test_utils/stk_mesh_fixtures/HexFixture.hpp" // for HexFixture, etc
76 #include "stk_unit_test_utils/stk_mesh_fixtures/QuadFixture.hpp" // for QuadFixture
77 #include "stk_unit_test_utils/stk_mesh_fixtures/RingFixture.hpp" // for RingFixture
78 #include "stk_util/util/PairIter.hpp" // for PairIter
79 #include <gtest/gtest.h>
80 #include <stk_mesh/base/Comm.hpp>
81 #include <stk_mesh/base/FEMHelpers.hpp> // for declare_element
82 #include <stk_mesh/base/FieldBLAS.hpp> // for stk::mesh::field_fill
83 #include <stk_mesh/base/MeshUtils.hpp>
84 #include <stk_unit_test_utils/BulkDataTester.hpp>
85 #include <stk_unit_test_utils/FaceTestingUtils.hpp>
86 #include <stk_unit_test_utils/getOption.h>
87 #include <stk_unit_test_utils/ioUtils.hpp>
88 #include <stk_util/parallel/CommSparse.hpp>
89 #include <stk_util/parallel/Parallel.hpp> // for ParallelMachine, etc
90 #include <stk_util/parallel/ParallelReduce.hpp> // for Reduce, ReduceSum, etc
91 #include <string> // for string, basic_string, etc
92 #include <utility> // for pair
93 #include <vector> // for vector, etc
94
95 using stk::mesh::Part;
96 using stk::mesh::MetaData;
97 using stk::mesh::BulkData;
98 using stk::mesh::MeshBuilder;
99 using stk::mesh::Selector;
100 using stk::mesh::PartVector;
101 using stk::mesh::PairIterRelation;
102 using stk::mesh::EntityProc;
103 using stk::mesh::Entity;
104 using stk::mesh::EntityId;
105 using stk::mesh::EntityKey;
106 using stk::mesh::EntityVector;
107 using stk::mesh::EntityRank;
108 using stk::mesh::fixtures::RingFixture;
109 using stk::mesh::fixtures::BoxFixture;
110
111 //====================
112
113 namespace
114 {
115
116 void donate_one_element(stk::unit_test_util::BulkDataTester & mesh)
117 {
118 const int p_rank = mesh.parallel_rank();
119
120 Selector select_owned(mesh.mesh_meta_data().locally_owned_part());
121
122 std::vector<size_t> before_count;
123 std::vector<size_t> after_count;
124
125 count_entities(select_owned, mesh, before_count);
126
127 // Change owner of an element on a process boundary
128 // from P0 to P1, and then recount to confirm ownership change
129
130 std::vector<EntityProc> change;
131
132 // A shared node:
133 EntityKey node_key;
134 Entity elem = Entity();
135
136 for(stk::mesh::EntityCommListInfoVector::const_iterator i = mesh.my_internal_comm_list().begin(); i != mesh.my_internal_comm_list().end(); ++i)
137 {
138 if(mesh.in_shared(i->key) && i->key.rank() == stk::topology::NODE_RANK)
139 {
140 node_key = i->key;
141 break;
142 }
143 }
144
145 ASSERT_TRUE( node_key.is_valid());
146
147 Entity node = mesh.get_entity(node_key);
148 ASSERT_TRUE( mesh.is_valid(node));
149
150 const stk::mesh::ConnectedEntities node_elems = mesh.get_connected_entities(node, stk::topology::ELEM_RANK);
151 for(unsigned i=0; i<node_elems.size() && !mesh.is_valid(elem); ++i)
152 {
153 elem = node_elems[i];
154 if(mesh.parallel_owner_rank(elem) != p_rank)
155 {
156 elem = Entity();
157 }
158 }
159
160 ASSERT_TRUE( mesh.is_valid(elem));
161
162 unsigned donated_nodes = 0;
163
164 // Only process #0 donates an element and its owned nodes:
165 if(0 == p_rank)
166 {
167 EntityProc entry;
168 entry.first = elem;
169 std::vector<int> shared_procs;
170 mesh.comm_shared_procs(mesh.entity_key(node),shared_procs);
171 entry.second = shared_procs[0];
172 change.push_back(entry);
173
174 Entity const *elem_nodes_i = mesh.begin_nodes(elem);
175 Entity const *elem_nodes_e = mesh.end_nodes(elem);
176 for(; elem_nodes_i != elem_nodes_e; ++elem_nodes_i)
177 {
178 if(mesh.parallel_owner_rank(*elem_nodes_i) == p_rank)
179 {
180 entry.first = *elem_nodes_i;
181 change.push_back(entry);
182 ++donated_nodes;
183 }
184 }
185 }
186
187 const bool someEntitiesMoved = mesh.change_entity_owner(change);
188 ASSERT_TRUE(someEntitiesMoved);
189
190 count_entities(select_owned, mesh, after_count);
191
192 if(0 == p_rank)
193 {
194 ASSERT_EQ( before_count[3] - 1, after_count[3]);
195 ASSERT_EQ( before_count[0] - donated_nodes, after_count[0]);
196 }
197 }
198
199 void donate_all_shared_nodes(stk::unit_test_util::BulkDataTester & mesh)
200 {
201 const int p_rank = mesh.parallel_rank();
202
203 const MetaData& meta = mesh.mesh_meta_data();
204 const Selector select_used = meta.locally_owned_part() | meta.globally_shared_part();
205
206 std::vector<size_t> before_count;
207 std::vector<size_t> after_count;
208
209 count_entities(select_used, mesh, before_count);
210
211 // Donate owned shared nodes to first sharing process.
212
213 const stk::mesh::EntityCommListInfoVector & entity_comm = mesh.my_internal_comm_list();
214
215 ASSERT_TRUE( ! entity_comm.empty());
216
217 std::vector<EntityProc> change;
218
219 for(stk::mesh::EntityCommListInfoVector::const_iterator i = entity_comm.begin();
220 i != entity_comm.end() && i->key.rank() == stk::topology::NODE_RANK; ++i)
221 {
222 Entity const node = i->entity;
223 std::vector<int> shared_procs;
224 mesh.comm_shared_procs(i->key,shared_procs);
225
226 if(mesh.parallel_owner_rank(node) == p_rank && !shared_procs.empty())
227 {
228 change.push_back(EntityProc(node, shared_procs[0]));
229 }
230 }
231
232 mesh.change_entity_owner(change);
233
234 count_entities(select_used, mesh, after_count);
235
236 ASSERT_TRUE( 3 <= after_count.size());
237 ASSERT_EQ( before_count[0], after_count[0]);
238 ASSERT_EQ( before_count[1], after_count[1]);
239 ASSERT_EQ( before_count[2], after_count[2]);
240 ASSERT_EQ( before_count[3], after_count[3]);
241 }
242
243 //----------------------------------------------------------------------
244 // Testing for mesh entities without relations
245
246 TEST(BulkData, testChangeOwner_nodes)
247 {
248 stk::ParallelMachine pm = MPI_COMM_WORLD;
249 MPI_Barrier( pm);
250
251 enum
252 {
253 nPerProc = 10
254 };
255 const int p_rank = stk::parallel_machine_rank(pm);
256 const int p_size = stk::parallel_machine_size(pm);
257 const unsigned id_total = nPerProc * p_size;
258 const unsigned id_begin = nPerProc * p_rank;
259 const unsigned id_end = nPerProc * (p_rank + 1);
260
261 const int spatial_dimension = 3;
262 std::shared_ptr<BulkData> bulkPtr = stk::unit_test_util::build_mesh(spatial_dimension, pm);
263 BulkData& bulk = *bulkPtr;
264 MetaData& meta = bulk.mesh_meta_data();
265
266 const stk::mesh::ConstPartVector no_parts;
267
268 meta.commit();
269 bulk.modification_begin();
270
271 // Ids for all entities (all entities have type 0):
272
273 std::vector<EntityId> ids(id_total);
274
275 for(unsigned i = 0; i < id_total; ++i)
276 {
277 ids[i] = i + 1;
278 }
279
280 // Declare just those entities in my range of ids:
281
282 for(unsigned i = id_begin; i < id_end; ++i)
283 {
284 bulk.declare_node(ids[i], no_parts);
285 }
286
287 ASSERT_TRUE( bulk.modification_end());
288
289 // Verify that I only have entities in my range:
290
291 for(unsigned i = 0; i < id_total; ++i)
292 {
293 Entity e = bulk.get_entity(stk::topology::NODE_RANK, ids[i]);
294 if(id_begin <= i && i < id_end)
295 {
296 ASSERT_TRUE( bulk.is_valid(e));
297 }
298 else
299 {
300 ASSERT_TRUE( !bulk.is_valid(e));
301 }
302 }
303
304 // Test change owner no-op first:
305
306 std::vector<EntityProc> change;
307
308 const bool anyEntitiesMoved = bulk.change_entity_owner(change);
309 ASSERT_FALSE(anyEntitiesMoved);
310
311 for(unsigned i = 0; i < id_total; ++i)
312 {
313 Entity e = bulk.get_entity(stk::topology::NODE_RANK, ids[i]);
314 if(id_begin <= i && i < id_end)
315 {
316 ASSERT_TRUE( bulk.is_valid(e));
317 }
318 else
319 {
320 ASSERT_TRUE( !bulk.is_valid(e));
321 }
322 }
323
324 // Can only test changing owner in parallel.
325
326 if(1 < p_size)
327 {
328 // Give my last two ids to the next process
329 // Get the previous process' last two ids
330
331 const int p_give = (p_rank + 1) % p_size;
332 const unsigned id_give = id_end - 2;
333 const unsigned id_get = (id_begin + id_total - 2) % id_total;
334
335 ASSERT_TRUE( bulk.is_valid(bulk.get_entity( stk::topology::NODE_RANK, ids[id_give] )));
336 ASSERT_TRUE( bulk.is_valid(bulk.get_entity( stk::topology::NODE_RANK, ids[id_give+1] )));
337 ASSERT_TRUE( !bulk.is_valid(bulk.get_entity( stk::topology::NODE_RANK, ids[id_get] )));
338 ASSERT_TRUE( !bulk.is_valid(bulk.get_entity( stk::topology::NODE_RANK, ids[id_get+1] )));
339
340 change.resize(2);
341 change[0].first = bulk.get_entity(stk::topology::NODE_RANK, ids[id_give]);
342 change[0].second = p_give;
343 change[1].first = bulk.get_entity(stk::topology::NODE_RANK, ids[id_give + 1]);
344 change[1].second = p_give;
345
346 bulk.change_entity_owner(change);
347
348 ASSERT_TRUE( bulk.is_valid(bulk.get_entity( stk::topology::NODE_RANK, ids[id_get] )));
349 ASSERT_TRUE( bulk.is_valid(bulk.get_entity( stk::topology::NODE_RANK, ids[id_get+1] )));
350
351 // Entities given away are destroyed until the next modification cycle
352 {
353 Entity const e0 = bulk.get_entity(stk::topology::NODE_RANK, ids[id_give]);
354 Entity const e1 = bulk.get_entity(stk::topology::NODE_RANK, ids[id_give + 1]);
355 ASSERT_TRUE( !bulk.is_valid(e0));
356 ASSERT_TRUE( !bulk.is_valid(e1));
357 }
358
359 ASSERT_TRUE( bulk.modification_begin());
360 ASSERT_TRUE( bulk.modification_end());
361
362 ASSERT_TRUE( !bulk.is_valid(bulk.get_entity( stk::topology::NODE_RANK, ids[id_give] )));
363 ASSERT_TRUE( !bulk.is_valid(bulk.get_entity( stk::topology::NODE_RANK, ids[id_give+1] )));
364 }
365 }
366
367 //----------------------------------------------------------------------
368 // Testing for creating existing mesh entities.
369
370 TEST(BulkData, testCreateMoreExistingOwnershipIsKept)
371 {
372 stk::ParallelMachine pm = MPI_COMM_WORLD;
373 MPI_Barrier( pm );
374
375 enum { nPerProc = 10 };
376
377 const int p_size = stk::parallel_machine_size( pm );
378 const int p_rank = stk::parallel_machine_rank( pm );
379
380 if (p_size != 2) { GTEST_SKIP(); }
381
382 const unsigned id_total = nPerProc * p_size ;
383 const unsigned id_begin = nPerProc * p_rank ;
384 const unsigned id_end = nPerProc * ( p_rank + 1 );
385
386 const int spatial_dimension = 3;
387 std::shared_ptr<BulkData> bulkPtr = stk::unit_test_util::build_mesh(spatial_dimension, pm);
388
389 BulkData& bulk = *bulkPtr;
390 MetaData& meta = bulk.mesh_meta_data();
391
392 const stk::mesh::ConstPartVector no_parts ;
393 Part* edge_part = &meta.declare_part_with_topology("edge_part", stk::topology::LINE_2);
394 Part* tri_part = &meta.declare_part_with_topology("tri_part", stk::topology::TRIANGLE_3);
395 Part* shell_part = &meta.declare_part_with_topology("shell_part", stk::topology::SHELL_TRI_3);
396
397 meta.commit();
398
399 bulk.modification_begin();
400
401 // Ids for all entities (all entities have type 0):
402
403 std::vector<EntityId> ids( id_total );
404
405 for ( unsigned i = 0 ; i < id_total ; ++i ) { ids[i] = i + 1; }
406
407 // Declare just those entities in my range of ids:
408 // P0: node ids 1,...,10
409 // P1: node ids 11,...,20
410 for ( unsigned i = id_begin ; i < id_end ; ++i ) {
411 bulk.declare_node(ids[i], no_parts );
412 }
413
414 ASSERT_TRUE( bulk.modification_end() );
415
416 // Quick check that the ranks have different sets.
417 const unsigned id_get = ( id_begin + id_total - 2 ) % id_total ;
418 ASSERT_TRUE( !bulk.is_valid(bulk.get_entity( stk::topology::NODE_RANK , ids[id_get] )) );
419 ASSERT_TRUE( !bulk.is_valid(bulk.get_entity( stk::topology::NODE_RANK , ids[id_get+1] )) );
420
421 {
422 ASSERT_TRUE( bulk.modification_begin() );
423
424 int otherProc = 1 - p_rank;
425 stk::mesh::EntityProcVec sharedNodesAndProcs;
426
427 if (0 == p_rank) {
428 Entity n10 = bulk.get_entity( stk::topology::NODE_RANK , 10 );
429 sharedNodesAndProcs.push_back(EntityProc(n10, otherProc));
430 Entity n11 = bulk.declare_node(11, no_parts);
431 sharedNodesAndProcs.push_back(EntityProc(n11, otherProc));
432 }
433 else if ( 1 == p_rank ) {
434 Entity n10 = bulk.declare_node(10, no_parts);
435 sharedNodesAndProcs.push_back(EntityProc(n10, otherProc));
436 Entity n11 = bulk.get_entity( stk::topology::NODE_RANK , 11 );
437 sharedNodesAndProcs.push_back(EntityProc(n11, otherProc));
438 }
439
440 bulk.add_node_sharing(sharedNodesAndProcs);
441
442 ASSERT_TRUE(bulk.modification_end());
443
444 {
445 Entity n10 = bulk.get_entity( stk::topology::NODE_RANK , 10 );
446 Entity n11 = bulk.get_entity( stk::topology::NODE_RANK , 11);
447 ASSERT_TRUE( bulk.in_shared(bulk.entity_key(n10)));
448 ASSERT_TRUE( bulk.in_shared(bulk.entity_key(n11)));
449 ASSERT_TRUE( 0 == bulk.parallel_owner_rank(n10) );
450 ASSERT_TRUE( 1 == bulk.parallel_owner_rank(n11) );
451 }
452 }
453
454 {
455 ASSERT_TRUE(bulk.modification_begin());
456
457 int otherProc = 1 - p_rank;
458 stk::mesh::EntityProcVec sharedNodesAndProcs;
459
460 if ( 0 == p_rank ) {
461
462 Entity n8 = bulk.get_entity( stk::topology::NODE_RANK , 8 );
463 Entity n9 = bulk.get_entity( stk::topology::NODE_RANK , 9 );
464 sharedNodesAndProcs.push_back(EntityProc(n9, otherProc));
465 Entity n20 = bulk.declare_node(20, no_parts);
466 sharedNodesAndProcs.push_back(EntityProc(n20, otherProc));
467
468 Entity e_9_20 = bulk.declare_edge(1, stk::mesh::ConstPartVector{edge_part});
469 bulk.declare_relation( e_9_20 , n9 , 0 );
470 bulk.declare_relation( e_9_20 , n20 , 1 );
471
472 Entity tri_shell = bulk.declare_element(1, stk::mesh::ConstPartVector{shell_part});
473 bulk.declare_relation( tri_shell , n8 , 0 );
474 bulk.declare_relation( tri_shell , n9 , 1 );
475 bulk.declare_relation( tri_shell , n20 , 2 );
476 bulk.declare_relation( tri_shell , e_9_20, 1 );
477 bulk.declare_element_side( tri_shell , 0 , stk::mesh::ConstPartVector{tri_part} );
478 }
479 else if (1 == p_rank) {
480
481 Entity n18 = bulk.get_entity( stk::topology::NODE_RANK , 18 );
482 Entity n9 = bulk.declare_node(9, no_parts);
483 sharedNodesAndProcs.push_back(EntityProc(n9, otherProc));
484 Entity n20 = bulk.get_entity( stk::topology::NODE_RANK , 20 );
485 sharedNodesAndProcs.push_back(EntityProc(n20, otherProc));
486
487 Entity e_9_20 = bulk.declare_edge(1, stk::mesh::ConstPartVector{edge_part});
488 bulk.declare_relation( e_9_20 , n9 , 0 );
489 bulk.declare_relation( e_9_20 , n20 , 1 );
490
491 Entity tri_shell = bulk.declare_element(11, stk::mesh::ConstPartVector{shell_part});
492 bulk.declare_relation( tri_shell , n18 , 0 );
493 bulk.declare_relation( tri_shell , n9 , 1 );
494 bulk.declare_relation( tri_shell , n20 , 2 );
495 bulk.declare_relation( tri_shell , e_9_20, 1 );
496 bulk.declare_element_side( tri_shell, 0, stk::mesh::ConstPartVector{tri_part} );
497 }
498
499 bulk.add_node_sharing(sharedNodesAndProcs);
500
501 ASSERT_TRUE(bulk.modification_end());
502
503 {
504 Entity n9 = bulk.get_entity( stk::topology::NODE_RANK , 9 );
505 Entity n20 = bulk.get_entity( stk::topology::NODE_RANK , 20 );
506 Entity e_9_20 = bulk.get_entity( stk::topology::EDGE_RANK , 1);
507 ASSERT_TRUE(bulk.in_shared(bulk.entity_key(n9)));
508 ASSERT_TRUE(bulk.in_shared(bulk.entity_key(n20)));
509 ASSERT_TRUE(bulk.in_shared(bulk.entity_key(e_9_20)));
510 ASSERT_TRUE( 0 == bulk.parallel_owner_rank(n9) );
511 ASSERT_TRUE( 1 == bulk.parallel_owner_rank(n20) );
512 ASSERT_TRUE( 0 == bulk.parallel_owner_rank(e_9_20) );
513 }
514 }
515
516 {
517 // Trip an error condition: an edge or face can only be shared
518 // if there is a downward relation to it from a locally owned entity.
519 ASSERT_TRUE(bulk.modification_begin());
520 if ( 0 == p_rank ) {
521
522 Entity n6 = bulk.get_entity( stk::topology::NODE_RANK , 6 );
523 Entity n17 = bulk.declare_node(17, no_parts);
524 bulk.add_node_sharing(n6, 1);
525 bulk.add_node_sharing(n17, 1);
526
527 Entity e_6_17 = bulk.declare_edge(2, stk::mesh::ConstPartVector{edge_part});
528 bulk.declare_relation( e_6_17 , n6 , 0 );
529 bulk.declare_relation( e_6_17 , n17 , 1 );
530 }
531 else if (1 == p_rank) {
532
533 Entity n6 = bulk.declare_node(6, no_parts );
534 Entity n17 = bulk.get_entity( stk::topology::NODE_RANK , 17 );
535 bulk.add_node_sharing(n6, 0);
536 bulk.add_node_sharing(n17, 0);
537
538 Entity e_6_17 = bulk.declare_edge(2, stk::mesh::ConstPartVector{edge_part});
539 bulk.declare_relation( e_6_17 , n6 , 0 );
540 bulk.declare_relation( e_6_17 , n17 , 1 );
541 }
542 ASSERT_THROW(bulk.modification_end(), std::runtime_error);
543 }
544 }
545
546 TEST(BulkData, inducedPartsOnFacesWorks)
547 {
548 MPI_Comm comm = MPI_COMM_WORLD;
549 int numProcs = -1;
550 MPI_Comm_size(comm, &numProcs);
551
552 if(numProcs == 2)
553 {
554 const int spatial_dimension = 3;
555 std::shared_ptr<BulkData> bulkPtr = stk::unit_test_util::build_mesh(spatial_dimension, comm);
556 BulkData& bulkData = *bulkPtr;
557 MetaData& meta = bulkData.mesh_meta_data();
558 //Part& node_part = meta.declare_part("node_part", stk::topology::NODE_RANK);
559 Part& face_part = meta.declare_part_with_topology("quad", stk::topology::QUAD_4);
560 Part& shell_part = meta.declare_part_with_topology("shell", stk::topology::SHELL_QUAD_4);
561 Part& messyPart = meta.declare_part_with_topology("messyPart", stk::topology::QUAD_4);
562 meta.commit();
563
564 bulkData.modification_begin();
565 stk::mesh::Entity face;
566 if (bulkData.parallel_rank() == 0)
567 {
568 stk::mesh::Entity node1 = bulkData.declare_node(1);
569 bulkData.add_node_sharing(node1, 1);
570 }
571 else
572 {
573 stk::mesh::Entity node1 = bulkData.declare_node(1);
574 stk::mesh::Entity node2 = bulkData.declare_node(2);
575 stk::mesh::Entity node3 = bulkData.declare_node(3);
576 stk::mesh::Entity node4 = bulkData.declare_node(4);
577
578 stk::mesh::Entity shell = bulkData.declare_element(1, stk::mesh::ConstPartVector{&shell_part});
579 bulkData.declare_relation(shell, node1, 0);
580 bulkData.declare_relation(shell, node2, 1);
581 bulkData.declare_relation(shell, node3, 2);
582 bulkData.declare_relation(shell, node4, 3);
583 bulkData.add_node_sharing(node1, 0);
584 face = bulkData.declare_element_side(shell, 0, stk::mesh::ConstPartVector{&face_part});
585 }
586
587 bulkData.modification_end();
588
589 stk::mesh::Entity node1 = bulkData.get_entity(stk::topology::NODE_RANK, 1);
590 EXPECT_TRUE(bulkData.bucket(node1).shared());
591
592 bulkData.modification_begin();
593
594 if ( bulkData.parallel_rank() == 1 )
595 {
596 stk::mesh::ConstPartVector addParts;
597 addParts.push_back(&messyPart);
598 bulkData.change_entity_parts(face, addParts, stk::mesh::ConstPartVector());
599 }
600
601 bulkData.modification_end();
602
603 EXPECT_TRUE(bulkData.bucket(node1).member(messyPart));
604
605 bulkData.modification_begin();
606
607 if ( bulkData.parallel_rank() == 1 )
608 {
609 stk::mesh::ConstPartVector rmParts;
610 rmParts.push_back(&messyPart);
611 bulkData.change_entity_parts(face, stk::mesh::ConstPartVector(), rmParts);
612 }
613
614 bulkData.modification_end();
615
616 EXPECT_TRUE(!bulkData.bucket(node1).member(messyPart));
617 }
618 }
619
620 TEST(BulkData, inducedPartsOnFacesThrowsTicket12896)
621 {
622 MPI_Comm comm = MPI_COMM_WORLD;
623 int numProcs = -1;
624 MPI_Comm_size(comm, &numProcs);
625
626 if(numProcs == 2)
627 {
628 const int spatial_dimension = 3;
629 std::shared_ptr<BulkData> bulkPtr = stk::unit_test_util::build_mesh(spatial_dimension, comm);
630 BulkData& bulkData = *bulkPtr;
631 MetaData& meta = bulkData.mesh_meta_data();
632 Part& shell_part = meta.declare_part_with_topology("shell", stk::topology::SHELL_QUAD_4);
633 Part& face_part = meta.declare_part_with_topology("quad", stk::topology::QUAD_4);
634 Part& messyPart = meta.declare_part_with_topology("messyPart", stk::topology::QUAD_4);
635 meta.commit();
636
637 bulkData.modification_begin();
638 stk::mesh::Entity face;
639 if (bulkData.parallel_rank() == 0)
640 {
641 stk::mesh::Entity node1 = bulkData.declare_node(1);
642 bulkData.add_node_sharing(node1, 1);
643 }
644 else
645 {
646 stk::mesh::Entity node1 = bulkData.declare_node(1);
647 stk::mesh::Entity node2 = bulkData.declare_node(2);
648 stk::mesh::Entity node3 = bulkData.declare_node(3);
649 stk::mesh::Entity node4 = bulkData.declare_node(4);
650
651 stk::mesh::Entity shell = bulkData.declare_element(1, stk::mesh::ConstPartVector{&shell_part});
652 bulkData.declare_relation(shell, node1, 0);
653 bulkData.declare_relation(shell, node2, 1);
654 bulkData.declare_relation(shell, node3, 2);
655 bulkData.declare_relation(shell, node4, 3);
656 bulkData.add_node_sharing(node1, 0);
657 face = bulkData.declare_element_side(shell, 0, stk::mesh::ConstPartVector{&face_part});
658 }
659
660 bulkData.modification_end();
661
662 stk::mesh::Entity node1 = bulkData.get_entity(stk::topology::NODE_RANK, 1);
663 EXPECT_TRUE(bulkData.bucket(node1).shared());
664
665 bulkData.modification_begin();
666
667 if ( bulkData.parallel_rank() == 1 )
668 {
669 stk::mesh::ConstPartVector addParts;
670 addParts.push_back(&messyPart);
671 bulkData.change_entity_parts(face, addParts, stk::mesh::ConstPartVector());
672 }
673
674 if ( bulkData.parallel_rank() == 1 )
675 {
676 stk::mesh::ConstPartVector rmParts;
677 rmParts.push_back(&messyPart);
678 bulkData.change_entity_parts(face, stk::mesh::ConstPartVector(), rmParts);
679 }
680
681 EXPECT_NO_THROW(bulkData.modification_end());
682
683 EXPECT_TRUE(!bulkData.bucket(node1).member(messyPart));
684 }
685 }
686
687 //----------------------------------------------------------------------
688 TEST(BulkData, testBulkDataRankBeginEnd)
689 {
690 stk::ParallelMachine pm = MPI_COMM_WORLD;
691 MPI_Barrier( pm);
692
693 const int p_size = stk::parallel_machine_size(pm);
694 if(p_size != 1)
695 {
696 return;
697 }
698
699 const size_t spatial_dim = 3;
700 std::shared_ptr<BulkData> bulkPtr = stk::unit_test_util::build_mesh(spatial_dim, pm);
701 BulkData& bulk = *bulkPtr;
702 bulk.modification_begin();
703 stk::mesh::const_entity_iterator iter = bulk.begin_entities(stk::topology::NODE_RANK);
704 stk::mesh::const_entity_iterator end = bulk.end_entities(stk::topology::NODE_RANK);
705
706 ASSERT_TRUE(iter == end);
707
708 EntityId node_id = 1;
709 bulk.declare_node(node_id);
710
711 iter = bulk.begin_entities(stk::topology::NODE_RANK);
712 end = bulk.end_entities(stk::topology::NODE_RANK);
713
714 //insist that there is 1 node:
715 ASSERT_TRUE(iter != end);
716 ASSERT_TRUE(std::distance(iter,end) == 1u);
717
718 //now declare an edge...
719 EntityId edge_id = 1;
720 bulk.declare_edge(edge_id);
721
722 iter = bulk.begin_entities(stk::topology::NODE_RANK);
723 end = bulk.end_entities(stk::topology::NODE_RANK);
724
725 //insist that there is still 1 node:
726 ASSERT_TRUE(iter != end);
727 ASSERT_TRUE(std::distance(iter,end) == 1u);
728
729 iter = bulk.begin_entities(stk::topology::EDGE_RANK);
730 end = bulk.end_entities(stk::topology::EDGE_RANK);
731
732 //insist that there is 1 edge:
733 ASSERT_TRUE(iter != end);
734 ASSERT_TRUE(std::distance(iter,end) == 1u);
735
736 node_id = 2;
737 bulk.declare_node(node_id);
738
739 iter = bulk.begin_entities(stk::topology::NODE_RANK);
740 end = bulk.end_entities(stk::topology::NODE_RANK);
741
742 //insist that there are 2 nodes:
743 ASSERT_TRUE(iter != end);
744 ASSERT_TRUE(std::distance(iter,end) == 2u);
745
746 iter = bulk.begin_entities(stk::topology::EDGE_RANK);
747 end = bulk.end_entities(stk::topology::EDGE_RANK);
748
749 //insist that there is still 1 edge:
750 ASSERT_TRUE(iter != end);
751 ASSERT_TRUE(std::distance(iter,end) == 1u);
752
753 iter = bulk.begin_entities(stk::topology::FACE_RANK);
754 end = bulk.end_entities(stk::topology::FACE_RANK);
755
756 //insist that there are no faces:
757 ASSERT_TRUE(iter == end);
758 }
759 //----------------------------------------------------------------------
760
761 TEST(BulkData, testChangeOwner_ring)
762 {
763 stk::ParallelMachine pm = MPI_COMM_WORLD;
764 MPI_Barrier( pm);
765
766 enum
767 {
768 nPerProc = 10
769 };
770 const int p_rank = stk::parallel_machine_rank(pm);
771 const int p_size = stk::parallel_machine_size(pm);
772 const unsigned nLocalNode = nPerProc + (1 < p_size ? 1 : 0);
773 const unsigned nLocalElement = nPerProc;
774
775 std::vector<size_t> local_count;
776
777 //------------------------------
778 {
779 RingFixture ring_mesh(pm, nPerProc, false /* no element parts */, stk::mesh::BulkData::NO_AUTO_AURA);
780 BulkData & bulk = ring_mesh.m_bulk_data;
781 ring_mesh.m_meta_data.commit();
782
783 bulk.modification_begin();
784 ring_mesh.generate_mesh();
785 ASSERT_TRUE(bulk.modification_end());
786
787 ring_mesh.fixup_node_ownership();
788
789 const Selector select_used = ring_mesh.m_meta_data.locally_owned_part() | ring_mesh.m_meta_data.globally_shared_part();
790 const Selector select_all = ring_mesh.m_meta_data.universal_part();
791
792 count_entities(select_used, ring_mesh.m_bulk_data, local_count);
793 ASSERT_EQ( local_count[stk::topology::NODE_RANK], nLocalNode);
794 ASSERT_EQ( local_count[stk::topology::ELEMENT_RANK], nLocalElement);
795
796 count_entities(select_all, ring_mesh.m_bulk_data, local_count);
797 ASSERT_EQ( local_count[stk::topology::NODE_RANK], nLocalNode);
798 ASSERT_EQ( local_count[stk::topology::ELEMENT_RANK], nLocalElement);
799
800 if(1 < p_size)
801 {
802 // Shift ring by two nodes and elements.
803
804 stk::unit_test::test_shift_ring(ring_mesh);
805
806 count_entities(select_used, ring_mesh.m_bulk_data, local_count);
807 ASSERT_TRUE( local_count[stk::topology::NODE_RANK] == nLocalNode);
808 ASSERT_TRUE( local_count[stk::topology::ELEMENT_RANK] == nLocalElement);
809
810 count_entities(select_all, ring_mesh.m_bulk_data, local_count);
811 ASSERT_TRUE( local_count[stk::topology::NODE_RANK] == nLocalNode);
812 ASSERT_TRUE( local_count[stk::topology::ELEMENT_RANK] == nLocalElement);
813 }
814 }
815
816 //------------------------------
817 // Test shift starting with ghosting but not regenerated ghosting.
818 {
819 RingFixture ring_mesh(pm, nPerProc, false /* no element parts */, stk::mesh::BulkData::NO_AUTO_AURA);
820 BulkData& bulk = ring_mesh.m_bulk_data;
821 ring_mesh.m_meta_data.commit();
822
823 bulk.modification_begin();
824 ring_mesh.generate_mesh();
825 ASSERT_TRUE(bulk.modification_end());
826
827 ring_mesh.fixup_node_ownership();
828
829 const Selector select_owned(ring_mesh.m_meta_data.locally_owned_part());
830 const Selector select_used = ring_mesh.m_meta_data.locally_owned_part() | ring_mesh.m_meta_data.globally_shared_part();
831 const Selector select_all(ring_mesh.m_meta_data.universal_part());
832
833 count_entities(select_used, ring_mesh.m_bulk_data, local_count);
834 ASSERT_EQ( local_count[stk::topology::NODE_RANK], nLocalNode);
835 ASSERT_EQ( local_count[stk::topology::ELEMENT_RANK], nLocalElement);
836
837 if(1 < p_size)
838 {
839 stk::unit_test::test_shift_ring(ring_mesh);
840
841 count_entities(select_owned, ring_mesh.m_bulk_data, local_count);
842 ASSERT_TRUE( local_count[stk::topology::NODE_RANK] == nPerProc);
843 ASSERT_TRUE( local_count[stk::topology::ELEMENT_RANK] == nPerProc);
844
845 count_entities(select_used, ring_mesh.m_bulk_data, local_count);
846 ASSERT_TRUE( local_count[stk::topology::NODE_RANK] == nLocalNode);
847 ASSERT_TRUE( local_count[stk::topology::ELEMENT_RANK] == nLocalElement);
848
849 // All of my ghosts were disrupted and therefore deleted:
850 count_entities(select_all, ring_mesh.m_bulk_data, local_count);
851 ASSERT_EQ( nLocalElement, local_count[stk::topology::ELEMENT_RANK]);
852 ASSERT_EQ( nLocalNode, local_count[stk::topology::NODE_RANK]);
853 }
854 }
855 //------------------------------
856 // Test shift starting with ghosting and regenerating ghosting.
857 {
858 RingFixture ring_mesh(pm, nPerProc, false /* no element parts */);
859 BulkData& bulk = ring_mesh.m_bulk_data;
860 ring_mesh.m_meta_data.commit();
861
862 bulk.modification_begin();
863 ring_mesh.generate_mesh();
864 ASSERT_TRUE(bulk.modification_end());
865
866 ring_mesh.fixup_node_ownership();
867
868 const Selector select_owned(ring_mesh.m_meta_data.locally_owned_part());
869 const Selector select_used = ring_mesh.m_meta_data.locally_owned_part() | ring_mesh.m_meta_data.globally_shared_part();
870 const Selector select_all(ring_mesh.m_meta_data.universal_part());
871
872 count_entities(select_used, ring_mesh.m_bulk_data, local_count);
873 ASSERT_TRUE( local_count[stk::topology::NODE_RANK] == nLocalNode);
874 ASSERT_TRUE( local_count[stk::topology::ELEMENT_RANK] == nLocalElement);
875
876 count_entities(select_all, ring_mesh.m_bulk_data, local_count);
877 const unsigned n_extra = 1 < p_size ? 2 : 0;
878 ASSERT_TRUE( local_count[stk::topology::NODE_RANK] == nLocalNode + n_extra);
879 ASSERT_TRUE( local_count[stk::topology::ELEMENT_RANK] == nLocalElement + n_extra);
880
881 if(1 < p_size)
882 {
883 stk::unit_test::test_shift_ring(ring_mesh);
884
885 count_entities(select_owned, ring_mesh.m_bulk_data, local_count);
886 ASSERT_TRUE( local_count[stk::topology::NODE_RANK] == nPerProc);
887 ASSERT_TRUE( local_count[stk::topology::ELEMENT_RANK] == nPerProc);
888
889 count_entities(select_used, ring_mesh.m_bulk_data, local_count);
890 ASSERT_TRUE( local_count[stk::topology::NODE_RANK] == nLocalNode);
891 ASSERT_TRUE( local_count[stk::topology::ELEMENT_RANK] == nLocalElement);
892
893 // All of my ghosts were regenerated:
894 count_entities(select_all, ring_mesh.m_bulk_data, local_count);
895 ASSERT_TRUE( local_count[stk::topology::NODE_RANK] == nLocalNode + n_extra);
896 ASSERT_TRUE( local_count[stk::topology::ELEMENT_RANK] == nLocalElement + n_extra);
897 }
898 }
899 //------------------------------
900 // Test bad owner change catching:
901 if(1 < p_size)
902 {
903 RingFixture ring_mesh(pm, nPerProc, false /* no element parts */);
904 BulkData& bulk = ring_mesh.m_bulk_data;
905 ring_mesh.m_meta_data.commit();
906
907 bulk.modification_begin();
908 ring_mesh.generate_mesh();
909 ASSERT_TRUE(bulk.modification_end());
910
911 ring_mesh.fixup_node_ownership();
912
913 std::vector<EntityProc> change;
914
915 if(0 == p_rank)
916 {
917 change.resize(4);
918 // Error to change to bad owner:
919 change[0].first = ring_mesh.m_bulk_data.get_entity(stk::topology::NODE_RANK, ring_mesh.m_node_ids[1]);
920 change[0].second = p_size;
921 // Error to change a ghost:
922 for(stk::mesh::EntityCommListInfoVector::const_iterator ec = ring_mesh.m_bulk_data.my_internal_comm_list().begin();
923 ec != ring_mesh.m_bulk_data.my_internal_comm_list().end(); ++ec)
924 {
925 if(bulk.in_receive_ghost(ec->key))
926 {
927 change[1].first = ec->entity;
928 break;
929 }
930 }
931 change[1].second = p_rank;
932 // Error to change to multiple owners:
933 change[2].first = ring_mesh.m_bulk_data.get_entity(stk::topology::NODE_RANK, ring_mesh.m_node_ids[1]);
934 change[2].second = (p_rank + 1) % p_size;
935 change[3].first = change[2].first;
936 change[3].second = (p_rank + 2) % p_size;
937 }
938
939 ASSERT_THROW( ring_mesh.m_bulk_data.change_entity_owner( change ), std::runtime_error);
940 }
941 //------------------------------
942 // Test move one element with initial ghosting but not regenerated ghosting:
943 // last processor give its shared node to P0
944 if(1 < p_size)
945 {
946 RingFixture ring_mesh(pm, nPerProc, false /* no element parts */, stk::mesh::BulkData::NO_AUTO_AURA);
947 BulkData& bulk = ring_mesh.m_bulk_data;
948 ring_mesh.m_meta_data.commit();
949
950 bulk.modification_begin();
951 ring_mesh.generate_mesh();
952 ASSERT_TRUE(bulk.modification_end());
953
954 ring_mesh.fixup_node_ownership();
955
956 const Selector select_owned(ring_mesh.m_meta_data.locally_owned_part());
957 const Selector select_used = ring_mesh.m_meta_data.locally_owned_part() | ring_mesh.m_meta_data.globally_shared_part();
958 const Selector select_all(ring_mesh.m_meta_data.universal_part());
959
960 std::vector<EntityProc> change;
961
962 if(p_rank + 1 == p_size)
963 {
964 EntityProc entry;
965 entry.first = ring_mesh.m_bulk_data.get_entity(stk::topology::NODE_RANK, ring_mesh.m_node_ids[0]);
966 entry.second = 0;
967 ASSERT_EQ( p_rank, bulk.parallel_owner_rank(entry.first));
968 change.push_back(entry);
969 }
970
971 ring_mesh.m_bulk_data.change_entity_owner(change);
972
973 count_entities(select_owned, ring_mesh.m_bulk_data, local_count);
974 const unsigned n_node = p_rank == 0 ? nPerProc + 1 : (p_rank + 1 == p_size ? nPerProc - 1 : nPerProc );
975
976 ASSERT_EQ( n_node, local_count[stk::topology::NODE_RANK]);
977 ASSERT_EQ( static_cast<unsigned>(nPerProc), local_count[stk::topology::ELEMENT_RANK]);
978
979 count_entities(select_used, ring_mesh.m_bulk_data, local_count);
980 ASSERT_EQ( nLocalNode, local_count[stk::topology::NODE_RANK]);
981 ASSERT_EQ( nLocalElement, local_count[stk::topology::ELEMENT_RANK]);
982 }
983 }
984
985 //----------------------------------------------------------------------
986 //----------------------------------------------------------------------
987 // Testing for collection of boxes
988
989 TEST(BulkData, testChangeOwner_box)
990 {
991 stk::ParallelMachine pm = MPI_COMM_WORLD;
992 MPI_Barrier( pm);
993
994 const int root_box[3][2] = { {0, 4}, {0, 5}, {0, 6}};
995
996 const int p_size = stk::parallel_machine_size(pm);
997
998 const int spatial_dimension = 3;
999 MetaData meta(spatial_dimension);
1000
1001 meta.commit();
1002
1003 //------------------------------
1004 {
1005 BoxFixture fixture(pm, stk::mesh::BulkData::AUTO_AURA);
1006 fixture.fem_meta().commit();
1007 stk::unit_test_util::BulkDataTester & bulk = fixture.bulk_data();
1008 int local_box[3][2] = { {0, 0}, {0, 0}, {0, 0}};
1009
1010 bulk.modification_begin();
1011 fixture.generate_boxes(root_box, local_box);
1012 ASSERT_TRUE(bulk.modification_end());
1013
1014 if(1 < p_size)
1015 {
1016 donate_one_element(bulk);
1017 }
1018 }
1019
1020 if(1 < p_size)
1021 {
1022 BoxFixture fixture(pm, stk::mesh::BulkData::AUTO_AURA);
1023 fixture.fem_meta().commit();
1024 stk::unit_test_util::BulkDataTester & bulk = fixture.bulk_data();
1025 int local_box[3][2] = { {0, 0}, {0, 0}, {0, 0}};
1026
1027 bulk.modification_begin();
1028 fixture.generate_boxes(root_box, local_box);
1029 ASSERT_TRUE(bulk.modification_end());
1030
1031 donate_all_shared_nodes(bulk);
1032 }
1033 //------------------------------
1034 if(1 < p_size)
1035 {
1036 BoxFixture fixture(pm, stk::mesh::BulkData::AUTO_AURA);
1037 fixture.fem_meta().commit();
1038 stk::unit_test_util::BulkDataTester & bulk = fixture.bulk_data();
1039 int local_box[3][2] = { {0, 0}, {0, 0}, {0, 0}};
1040
1041 bulk.modification_begin();
1042 fixture.generate_boxes(root_box, local_box);
1043 ASSERT_TRUE(bulk.modification_end());
1044
1045 donate_one_element(bulk);
1046 }
1047 //------------------------------
1048 // Introduce ghosts:
1049 if(1 < p_size)
1050 {
1051 BoxFixture fixture(pm, stk::mesh::BulkData::NO_AUTO_AURA);
1052 stk::unit_test_util::BulkDataTester & bulk = fixture.bulk_data();
1053 MetaData & box_meta = fixture.fem_meta();
1054 box_meta.commit();
1055 int local_box[3][2] = { {0, 0}, {0, 0}, {0, 0}};
1056
1057 bulk.modification_begin();
1058 fixture.generate_boxes(root_box, local_box);
1059 ASSERT_TRUE(bulk.modification_end());
1060
1061 std::vector<size_t> used_count;
1062 std::vector<size_t> all_count;
1063
1064 const Selector select_owned(box_meta.locally_owned_part());
1065 const Selector select_used = box_meta.locally_owned_part() | box_meta.globally_shared_part();
1066 const Selector select_all(box_meta.universal_part());
1067
1068 count_entities(select_all, bulk, all_count);
1069 count_entities(select_used, bulk, used_count);
1070
1071 ASSERT_EQ( used_count[0], all_count[0]);
1072 ASSERT_EQ( used_count[3], all_count[3]);
1073
1074 donate_all_shared_nodes(bulk);
1075
1076 count_entities(select_all, bulk, all_count);
1077 count_entities(select_used, bulk, used_count);
1078
1079 ASSERT_EQ( used_count[0], all_count[0]);
1080 ASSERT_EQ( used_count[3], all_count[3]);
1081 }
1082 }
1083
1084 TEST(BulkData, testModifyPropagation)
1085 {
1086 // Our new modification model makes it so the modified status
1087 // of an entity is propagated up to higher-ranked entities
1088 // that have relations to the modified entity. We test this
1089 // by grabbing a node off of a ring mesh, modifying it, and
1090 // checking that its element also gets marked as modified.
1091
1092 stk::ParallelMachine pm = MPI_COMM_WORLD;
1093 MPI_Barrier( pm);
1094
1095 const unsigned nPerProc = 2;
1096 const int p_size = stk::parallel_machine_size(pm);
1097
1098 // this test only needs to be run w/ one processor
1099 if(p_size > 1)
1100 return;
1101
1102 // Make a ring_mesh and add an extra part
1103 RingFixture ring_mesh(pm, nPerProc, false /* don't use element parts */);
1104 stk::mesh::Part& special_part = ring_mesh.m_meta_data.declare_part("special_node_part", stk::topology::NODE_RANK);
1105 ring_mesh.m_meta_data.commit();
1106 BulkData& bulk = ring_mesh.m_bulk_data;
1107
1108 bulk.modification_begin();
1109 ring_mesh.generate_mesh();
1110 ASSERT_TRUE(bulk.modification_end());
1111
1112 ring_mesh.fixup_node_ownership();
1113
1114 // grab the first element
1115 EntityVector elements;
1116 const stk::mesh::EntityRank element_rank = stk::topology::ELEMENT_RANK;
1117 stk::mesh::get_entities(ring_mesh.m_bulk_data, element_rank, elements);
1118 stk::mesh::Entity element = elements.front();
1119
1120 // get one of the nodes related to this element
1121 ASSERT_TRUE(bulk.num_nodes(element) > 0);
1122 stk::mesh::Entity node = *bulk.begin_nodes(element);
1123 ASSERT_EQ( bulk.entity_rank(node), stk::topology::NODE_RANK);
1124
1125 // make a modification to the node by changing its parts
1126 ring_mesh.m_bulk_data.modification_begin();
1127 stk::mesh::ConstPartVector parts;
1128 parts.push_back(&special_part);
1129 bulk.change_entity_parts(node, parts);
1130
1131 // check that the node AND it's element are marked as modified
1132 ASSERT_EQ( bulk.state(node), stk::mesh::Modified);
1133 ASSERT_EQ( bulk.state(element), stk::mesh::Modified);
1134
1135 ASSERT_TRUE( bulk.modification_end());
1136 }
1137
1138 TEST(BulkData, testChangeEntityOwnerFromSelfToSelf)
1139 {
1140 // It should be legal to "change" entity ownership from yourself to yourself.
1141 //
1142 // 1---3---5
1143 // | 1 | 2 |
1144 // 2---4---6
1145 //
1146 // To test this, we use the mesh above, with elem 1 going on rank 0 and
1147 // elem 2 going on rank 1. Nodes 3,4 are shared. After the mesh is set up
1148 // we change the ownership of a few nodes to the same proc that already
1149 // owns them.
1150
1151 stk::ParallelMachine pm = MPI_COMM_WORLD;
1152
1153 // Set up meta and bulk data
1154 const unsigned spatial_dim = 2;
1155 std::shared_ptr<BulkData> bulkPtr = stk::unit_test_util::build_mesh(spatial_dim, pm);
1156 BulkData& mesh = *bulkPtr;
1157 MetaData& meta_data = mesh.mesh_meta_data();
1158 meta_data.commit();
1159 int p_rank = mesh.parallel_rank();
1160 int p_size = mesh.parallel_size();
1161
1162 // Bail if we only have one proc
1163 if(p_size == 1)
1164 {
1165 return;
1166 }
1167
1168 // Begin modification cycle so we can create the entities and relations
1169 mesh.modification_begin();
1170
1171 EntityVector nodes;
1172 const unsigned nodes_per_elem = 4, nodes_per_side = 2;
1173
1174 if(p_rank < 2)
1175 {
1176 // We're just going to add everything to the universal part
1177 stk::mesh::ConstPartVector node_parts, elem_parts;
1178 stk::mesh::Part &node_part = meta_data.get_topology_root_part(stk::topology::NODE);
1179 node_parts.push_back(&node_part);
1180 stk::mesh::Part &quad4_part = meta_data.get_topology_root_part(stk::topology::QUAD_4_2D);
1181 elem_parts.push_back(&quad4_part);
1182
1183 // Create element
1184 Entity elem = mesh.declare_element(p_rank + 1, elem_parts);
1185
1186 // Create nodes
1187 const unsigned starting_node_id = p_rank * nodes_per_side + 1;
1188 for(unsigned id = starting_node_id; id < starting_node_id + nodes_per_elem; ++id)
1189 {
1190 nodes.push_back(mesh.declare_node(id, node_parts));
1191 }
1192
1193 // Add relations to nodes
1194 unsigned rel_id = 0;
1195 for(EntityVector::iterator itr = nodes.begin(); itr != nodes.end(); ++itr, ++rel_id)
1196 {
1197 mesh.declare_relation(elem, *itr, rel_id);
1198 }
1199 if (p_rank == 0)
1200 {
1201 Entity shared_node0 = nodes[2];
1202 Entity shared_node1 = nodes[3];
1203 mesh.add_node_sharing(shared_node0, 1);
1204 mesh.add_node_sharing(shared_node1, 1);
1205 }
1206 else
1207 {
1208 Entity shared_node0 = nodes[0];
1209 Entity shared_node1 = nodes[1];
1210 mesh.add_node_sharing(shared_node0, 0);
1211 mesh.add_node_sharing(shared_node1, 0);
1212 }
1213 }
1214
1215 mesh.modification_end();
1216
1217 std::vector<EntityProc> change;
1218 if(p_rank < 2)
1219 {
1220 // Change ownership of some nodes to the same proc that owns them
1221
1222 // Add a non-shared node to change list
1223 if(p_rank == 0)
1224 {
1225 EntityProc entry(nodes.front(), p_rank);
1226 change.push_back(entry);
1227 }
1228 else
1229 {
1230 EntityProc entry(nodes.back(), p_rank);
1231 change.push_back(entry);
1232 }
1233
1234 // Add a shared node to change list
1235 Entity shared_node = nodes[p_rank == 0 ? nodes_per_side : 0];
1236 EntityId expected_id = 3;
1237 Part& shared_part = meta_data.globally_shared_part();
1238 ASSERT_TRUE( has_superset(mesh.bucket(shared_node), shared_part));
1239 ASSERT_EQ(mesh.identifier(shared_node), expected_id);
1240 if(mesh.parallel_owner_rank(shared_node) == p_rank)
1241 {
1242 EntityProc entry(shared_node, p_rank);
1243 change.push_back(entry);
1244 }
1245 }
1246
1247 mesh.change_entity_owner(change);
1248 }
1249
1250 TEST(BulkData, test_internal_clean_and_verify_parallel_change_trivial)
1251 {
1252 stk::ParallelMachine pm = MPI_COMM_WORLD;
1253
1254 unsigned spatialDim = 2;
1255 std::shared_ptr<BulkData> mesh = stk::unit_test_util::build_mesh(spatialDim, pm);
1256 std::vector<EntityProc> local_change;
1257 stk::mesh::impl::internal_clean_and_verify_parallel_change(*mesh,local_change);
1258 EXPECT_TRUE(local_change.size() == 0);
1259 }
1260 TEST(BulkData, test_internal_clean_and_verify_parallel_change_sort_unique)
1261 {
1262 stk::ParallelMachine pm = MPI_COMM_WORLD;
1263 int numProcs = stk::parallel_machine_size(pm);
1264 int myRank = stk::parallel_machine_rank(pm);
1265 if (numProcs < 2) { return; }
1266
1267 unsigned spatialDim = 2;
1268 std::shared_ptr<BulkData> meshPtr = stk::unit_test_util::build_mesh(spatialDim, pm);
1269 stk::mesh::BulkData& mesh = *meshPtr;
1270 mesh.modification_begin();
1271 Entity node1, node2, node3, node4;
1272 if (myRank == 0) {
1273 node1 = mesh.declare_node(1);
1274 node2 = mesh.declare_node(2);
1275 node3 = mesh.declare_node(3);
1276 node4 = mesh.declare_node(4);
1277 }
1278 mesh.modification_end();
1279 std::vector<EntityProc> local_change;
1280 if (myRank == 0)
1281 {
1282 local_change.push_back(EntityProc(node4,0));
1283 local_change.push_back(EntityProc(node3,1));
1284 local_change.push_back(EntityProc(node2,1));
1285 local_change.push_back(EntityProc(node2,1));
1286 local_change.push_back(EntityProc(node1,1));
1287 }
1288 stk::mesh::impl::internal_clean_and_verify_parallel_change(mesh,local_change);
1289 if (myRank == 0)
1290 {
1291 EXPECT_EQ(3u,local_change.size());
1292 EXPECT_TRUE( EntityProc(node1,1) == local_change[0] );
1293 EXPECT_TRUE( EntityProc(node2,1) == local_change[1] );
1294 EXPECT_TRUE( EntityProc(node3,1) == local_change[2] );
1295 }
1296 }
1297 TEST(BulkData, test_internal_clean_and_verify_parallel_change_bad_null)
1298 {
1299 stk::ParallelMachine pm = MPI_COMM_WORLD;
1300
1301 unsigned spatialDim = 2;
1302 std::shared_ptr<BulkData> mesh = stk::unit_test_util::build_mesh(spatialDim, pm);
1303 std::vector<EntityProc> local_change;
1304 local_change.push_back(EntityProc(Entity(),1));
1305 EXPECT_THROW( stk::mesh::impl::internal_clean_and_verify_parallel_change(*mesh,local_change),
1306 std::runtime_error );
1307 }
1308 TEST(BulkData, test_internal_clean_and_verify_parallel_change_not_owner)
1309 {
1310 stk::ParallelMachine pm = MPI_COMM_WORLD;
1311 int numProcs = stk::parallel_machine_size(pm);
1312 int myRank = stk::parallel_machine_rank(pm);
1313 if (numProcs < 2) { return; }
1314
1315 unsigned spatialDim = 2;
1316 std::shared_ptr<BulkData> meshPtr = stk::unit_test_util::build_mesh(spatialDim, pm);
1317 stk::mesh::BulkData& mesh = *meshPtr;
1318 stk::mesh::MetaData& meta = mesh.mesh_meta_data();
1319 stk::mesh::Part& block_1 = meta.declare_part_with_topology("block_1", stk::topology::QUAD_4_2D);
1320 mesh.modification_begin();
1321 Entity node1, node2, node3, node4, node5, node6;
1322 if (myRank == 0)
1323 {
1324 Entity element = mesh.declare_element(1, stk::mesh::ConstPartVector{&block_1});
1325 node1 = mesh.declare_node(1);
1326 node2 = mesh.declare_node(2); // shared
1327 node3 = mesh.declare_node(3); // shared
1328 node4 = mesh.declare_node(4);
1329 mesh.declare_relation(element,node1,0);
1330 mesh.declare_relation(element,node2,1);
1331 mesh.declare_relation(element,node3,2);
1332 mesh.declare_relation(element,node4,3);
1333 mesh.add_node_sharing(node2,1);
1334 mesh.add_node_sharing(node3,1);
1335 }
1336 if (myRank == 1)
1337 {
1338 Entity element = mesh.declare_element(2, stk::mesh::ConstPartVector{&block_1});
1339 node2 = mesh.declare_node(2); // shared
1340 node5 = mesh.declare_node(5);
1341 node6 = mesh.declare_node(6);
1342 node3 = mesh.declare_node(3); // shared
1343 mesh.declare_relation(element,node2,0);
1344 mesh.declare_relation(element,node5,1);
1345 mesh.declare_relation(element,node6,2);
1346 mesh.declare_relation(element,node3,3);
1347 mesh.add_node_sharing(node2,0);
1348 mesh.add_node_sharing(node3,0);
1349 }
1350 mesh.modification_end();
1351
1352 std::vector<EntityProc> local_change;
1353 if (myRank == 1)
1354 {
1355 EXPECT_FALSE( mesh.bucket(node2).owned() );
1356 local_change.push_back(EntityProc(node2,1));
1357 }
1358 EXPECT_THROW( stk::mesh::impl::internal_clean_and_verify_parallel_change(mesh,local_change),
1359 std::runtime_error );
1360 }
1361 TEST(BulkData, test_internal_clean_and_verify_parallel_change_invalid_owner)
1362 {
1363 stk::ParallelMachine pm = MPI_COMM_WORLD;
1364 int numProcs = stk::parallel_machine_size(pm);
1365 int myRank = stk::parallel_machine_rank(pm);
1366 if (numProcs < 2) { return; }
1367
1368 unsigned spatialDim = 2;
1369 std::shared_ptr<BulkData> meshPtr = stk::unit_test_util::build_mesh(spatialDim, pm);
1370 stk::mesh::BulkData& mesh = *meshPtr;
1371 mesh.modification_begin();
1372 Entity node1;
1373 if (myRank == 0)
1374 {
1375 node1 = mesh.declare_node(1);
1376 }
1377 mesh.modification_end();
1378 std::vector<EntityProc> local_change;
1379 if (myRank == 0)
1380 {
1381 local_change.push_back(EntityProc(node1,numProcs));
1382 }
1383 EXPECT_THROW( stk::mesh::impl::internal_clean_and_verify_parallel_change(mesh,local_change),
1384 std::runtime_error );
1385 }
1386 TEST(BulkData, test_internal_clean_and_verify_parallel_change_send_to_2_owners)
1387 {
1388 stk::ParallelMachine pm = MPI_COMM_WORLD;
1389 int numProcs = stk::parallel_machine_size(pm);
1390 int myRank = stk::parallel_machine_rank(pm);
1391 if (numProcs < 3) { return; }
1392
1393 unsigned spatialDim = 2;
1394 std::shared_ptr<BulkData> bulkPtr = stk::unit_test_util::build_mesh(spatialDim, pm);
1395 stk::mesh::BulkData& mesh = *bulkPtr;
1396 mesh.modification_begin();
1397 Entity node1;
1398 if (myRank == 0) {
1399 node1 = mesh.declare_node(1);
1400 }
1401 mesh.modification_end();
1402 std::vector<EntityProc> local_change;
1403 if (myRank == 0)
1404 {
1405 local_change.push_back(EntityProc(node1,1));
1406 local_change.push_back(EntityProc(node1,2));
1407 }
1408 EXPECT_THROW( stk::mesh::impl::internal_clean_and_verify_parallel_change(mesh,local_change),
1409 std::runtime_error );
1410 }
1411
1412 TEST(BulkData, test_internal_generate_parallel_change_lists_trivial)
1413 {
1414 stk::ParallelMachine pm = MPI_COMM_WORLD;
1415
1416 unsigned spatialDim = 2;
1417 std::shared_ptr<BulkData> bulkPtr = stk::unit_test_util::build_mesh(spatialDim, pm);
1418 stk::mesh::BulkData& mesh = *bulkPtr;
1419
1420 std::vector<EntityProc> local_change;
1421 std::vector<EntityProc> ghosted_change;
1422 std::vector<EntityProc> shared_change;
1423 stk::mesh::impl::internal_generate_parallel_change_lists(mesh, local_change,
1424 shared_change, ghosted_change);
1425 EXPECT_TRUE(shared_change.empty());
1426 EXPECT_TRUE(ghosted_change.empty());
1427 }
1428
1429 TEST(BulkData, test_internal_generate_parallel_change_lists_2EltsChown1ChownItsNodes)
1430 {
1431 stk::ParallelMachine pm = MPI_COMM_WORLD;
1432 const int p_rank = stk::parallel_machine_rank( pm );
1433 const int p_size = stk::parallel_machine_size( pm );
1434
1435 if (p_size != 2) {
1436 return;
1437 }
1438
1439 const int spatial_dimension = 2;
1440 std::shared_ptr<BulkData> bulkPtr = stk::unit_test_util::build_mesh(spatial_dimension, pm);
1441 stk::mesh::MetaData& meta = bulkPtr->mesh_meta_data();
1442 stk::mesh::BulkData& bulk = *bulkPtr;
1443
1444 // id/owner_proc
1445 //
1446 // 1/0---4/0---5/0 1/0---4/1---5/1
1447 // | | | | | |
1448 // | 1/0 | 2/0 | => | 1/0 | 2/1 |
1449 // | | | | | |
1450 // 2/0---3/0---6/0 2/0---3/1---6/1
1451
1452 stk::mesh::EntityId element_ids [2] = {1, 2};
1453 stk::mesh::EntityIdVector elem_node_ids[] {
1454 {1, 2, 3, 4},
1455 {4, 3, 6, 5}
1456 };
1457
1458 stk::mesh::Part &elem_part = meta.declare_part_with_topology("elem_part",stk::topology::QUAD_4_2D);
1459 meta.commit();
1460
1461 // Start with all entities on proc 0
1462 std::vector<stk::mesh::Entity> elems;
1463 bulk.modification_begin();
1464 if (p_rank == 0) {
1465 elems.push_back(stk::mesh::declare_element(bulk, elem_part ,element_ids[0], elem_node_ids[0] ) );
1466 elems.push_back(stk::mesh::declare_element(bulk, elem_part ,element_ids[1], elem_node_ids[1] ) );
1467 }
1468 bulk.modification_end();
1469
1470 stk::mesh::EntityProcVec local_change;
1471 if (p_rank == 0) {
1472 local_change.push_back(stk::mesh::EntityProc(bulk.get_entity(stk::topology::NODE_RANK, 3), 1));
1473 local_change.push_back(stk::mesh::EntityProc(bulk.get_entity(stk::topology::NODE_RANK, 4), 1));
1474 local_change.push_back(stk::mesh::EntityProc(bulk.get_entity(stk::topology::NODE_RANK, 5), 1));
1475 local_change.push_back(stk::mesh::EntityProc(bulk.get_entity(stk::topology::NODE_RANK, 6), 1));
1476 local_change.push_back(stk::mesh::EntityProc(elems[1], 1));
1477 }
1478
1479 std::vector<EntityProc> shared_change;
1480 std::vector<EntityProc> ghosted_change;
1481 stk::mesh::impl::internal_generate_parallel_change_lists(bulk, local_change, shared_change, ghosted_change);
1482
1483 EXPECT_TRUE(shared_change.empty());
1484 EXPECT_TRUE(ghosted_change.empty());
1485 }
1486
1487 TEST(BulkData, test_internal_generate_parallel_change_lists_2EltsFlip)
1488 {
1489 stk::ParallelMachine pm = MPI_COMM_WORLD;
1490 const int p_rank = stk::parallel_machine_rank( pm );
1491 const int p_size = stk::parallel_machine_size( pm );
1492
1493 if (p_size != 2) {
1494 return;
1495 }
1496
1497 const int spatial_dimension = 2;
1498 std::shared_ptr<BulkData> bulkPtr = stk::unit_test_util::build_mesh(spatial_dimension, pm);
1499 stk::mesh::MetaData& meta = bulkPtr->mesh_meta_data();
1500 stk::mesh::BulkData& bulk = *bulkPtr;
1501
1502 // id/owner_proc
1503 //
1504 // 1/0---4/0---5/1 1/1---4/1---5/0
1505 // | | | | | |
1506 // | 1/0 | 2/1 | => | 1/1 | 2/0 |
1507 // | | | | | |
1508 // 2/0---3/0---6/1 2/1---3/0---6/0
1509
1510 stk::mesh::EntityId element_ids [2] = {1, 2};
1511 stk::mesh::EntityIdVector elem_node_ids [] = {
1512 {1, 2, 3, 4},
1513 {4, 3, 6, 5}
1514 };
1515
1516 stk::mesh::Part &elem_part = meta.declare_part_with_topology("elem_part",stk::topology::QUAD_4_2D);
1517 meta.commit();
1518
1519 // Start with all entities on proc 0
1520 Entity elem;
1521 bulk.modification_begin();
1522 if (p_rank == 0) {
1523 elem = stk::mesh::declare_element(bulk, elem_part ,element_ids[0], elem_node_ids[0] );
1524 bulk.add_node_sharing(bulk.get_entity(stk::topology::NODE_RANK, 3), 1);
1525 bulk.add_node_sharing(bulk.get_entity(stk::topology::NODE_RANK, 4), 1);
1526 }
1527 else if (p_rank == 1) {
1528 elem = stk::mesh::declare_element(bulk, elem_part ,element_ids[1], elem_node_ids[1] );
1529 bulk.add_node_sharing(bulk.get_entity(stk::topology::NODE_RANK, 3), 0);
1530 bulk.add_node_sharing(bulk.get_entity(stk::topology::NODE_RANK, 4), 0);
1531 }
1532 bulk.modification_end();
1533
1534 stk::mesh::EntityProcVec local_change;
1535 if (p_rank == 0) {
1536 local_change.push_back(stk::mesh::EntityProc(bulk.get_entity(stk::topology::NODE_RANK, 1), 1));
1537 local_change.push_back(stk::mesh::EntityProc(bulk.get_entity(stk::topology::NODE_RANK, 2), 1));
1538 local_change.push_back(stk::mesh::EntityProc(bulk.get_entity(stk::topology::NODE_RANK, 4), 1));
1539 local_change.push_back(stk::mesh::EntityProc(elem, 1));
1540 }
1541 else if (p_rank == 1) {
1542 local_change.push_back(stk::mesh::EntityProc(bulk.get_entity(stk::topology::NODE_RANK, 5), 0));
1543 local_change.push_back(stk::mesh::EntityProc(bulk.get_entity(stk::topology::NODE_RANK, 6), 0));
1544 local_change.push_back(stk::mesh::EntityProc(elem, 0));
1545 }
1546
1547 std::vector<EntityProc> shared_change;
1548 std::vector<EntityProc> ghosted_change;
1549 stk::mesh::impl::internal_generate_parallel_change_lists(bulk, local_change, shared_change, ghosted_change);
1550
1551
1552 if (p_rank == 0) {
1553 ASSERT_EQ(0u, shared_change.size());
1554 ASSERT_EQ(3u, ghosted_change.size());
1555 EntityProc node5_ghosted = ghosted_change[0];
1556 EntityProc node6_ghosted = ghosted_change[1];
1557 EntityProc elt2_ghosted = ghosted_change[2];
1558 EXPECT_EQ(stk::mesh::EntityKey(stk::topology::NODE_RANK, 5), bulk.entity_key(node5_ghosted.first));
1559 EXPECT_EQ(stk::mesh::EntityKey(stk::topology::NODE_RANK, 6), bulk.entity_key(node6_ghosted.first));
1560 EXPECT_EQ(stk::mesh::EntityKey(stk::topology::ELEMENT_RANK, 2), bulk.entity_key(elt2_ghosted.first));
1561 EXPECT_EQ(0, node5_ghosted.second);
1562 EXPECT_EQ(0, node6_ghosted.second);
1563 EXPECT_EQ(0, elt2_ghosted.second);
1564 }
1565 else if (p_rank == 1) {
1566 ASSERT_EQ(1u, shared_change.size());
1567 ASSERT_EQ(3u, ghosted_change.size());
1568 EntityProc node4_shared = shared_change[0];
1569 EntityProc node1_ghosted = ghosted_change[0];
1570 EntityProc node2_ghosted = ghosted_change[1];
1571 EntityProc elt1_ghosted = ghosted_change[2];
1572 EXPECT_EQ(stk::mesh::EntityKey(stk::topology::NODE_RANK, 4), bulk.entity_key(node4_shared.first));
1573 EXPECT_EQ(stk::mesh::EntityKey(stk::topology::NODE_RANK, 1), bulk.entity_key(node1_ghosted.first));
1574 EXPECT_EQ(stk::mesh::EntityKey(stk::topology::NODE_RANK, 2), bulk.entity_key(node2_ghosted.first));
1575 EXPECT_EQ(stk::mesh::EntityKey(stk::topology::ELEMENT_RANK, 1), bulk.entity_key(elt1_ghosted.first));
1576 EXPECT_EQ(1, node4_shared.second);
1577 EXPECT_EQ(1, node1_ghosted.second);
1578 EXPECT_EQ(1, node2_ghosted.second);
1579 EXPECT_EQ(1, elt1_ghosted.second);
1580 }
1581 }
1582
1583 TEST(BulkData, testFamilyTreeGhosting)
1584 {
1585 // A family tree is a higher-rank entity (rank = element_rank() + 1) that
1586 // has down-relations to elements used, for example, to hold parent/child
1587 // relations in an adapted mesh.
1588 //
1589 // 1---3---5---7
1590 // | 1 | 2 | 3 | ...
1591 // 2---4---6---8
1592 //
1593 // To test this, we use the mesh above, with each elem going on a separate
1594 // proc, one elem per proc.
1595 // After the mesh is set up we add rank-3 (family tree) entities and have them point down to
1596 // just the single rank-2 elements. Then we check that they are properly
1597 // ghosted after modification_end.
1598
1599 stk::ParallelMachine pm = MPI_COMM_WORLD;
1600
1601 // Set up meta and bulk data
1602 const unsigned spatial_dim = 2;
1603
1604 stk::mesh::MeshBuilder builder(pm);
1605 builder.set_spatial_dimension(spatial_dim);
1606 std::vector<std::string> entity_rank_names = stk::mesh::entity_rank_names();
1607 entity_rank_names.push_back("FAMILY_TREE");
1608 builder.set_entity_rank_names(entity_rank_names);
1609 std::shared_ptr<BulkData> bulkPtr = builder.create();
1610 MetaData& meta_data = bulkPtr->mesh_meta_data();
1611 const unsigned nodes_per_elem = 4, nodes_per_side = 2;
1612 Part &elem_part = meta_data.declare_part_with_topology("elem_part", stk::topology::QUAD_4_2D);
1613 meta_data.commit();
1614 BulkData& mesh = *bulkPtr;
1615
1616 int p_rank = mesh.parallel_rank();
1617 int p_size = mesh.parallel_size();
1618
1619 Part& owned = meta_data.locally_owned_part();
1620 Part& shared = meta_data.globally_shared_part();
1621
1622 //
1623 // Begin modification cycle so we can create the entities and relations
1624 //
1625
1626 mesh.modification_begin();
1627
1628 EntityVector nodes;
1629 const EntityRank family_tree_rank = static_cast<EntityRank>(stk::topology::ELEMENT_RANK + 1);
1630 const EntityId my_family_tree_id = p_rank + 1;
1631
1632 // We're just going to add everything to the universal part
1633 stk::mesh::ConstPartVector empty_parts;
1634 stk::mesh::ConstPartVector elem_parts;
1635 elem_parts.push_back(&elem_part);
1636
1637 // Create element
1638 Entity elem = mesh.declare_element(p_rank + 1, elem_parts);
1639
1640 // Create nodes
1641 const unsigned starting_node_id = p_rank * nodes_per_side + 1;
1642 for(unsigned id = starting_node_id; id < starting_node_id + nodes_per_elem; ++id)
1643 {
1644 nodes.push_back(mesh.declare_node(id, empty_parts));
1645 }
1646 if(p_rank > 0)
1647 {
1648 mesh.add_node_sharing(nodes[0], p_rank - 1);
1649 mesh.add_node_sharing(nodes[1], p_rank - 1);
1650 }
1651 if(p_rank < (p_size - 1))
1652 {
1653 mesh.add_node_sharing(nodes[2], p_rank + 1);
1654 mesh.add_node_sharing(nodes[3], p_rank + 1);
1655 }
1656
1657 // Add relations to nodes
1658 unsigned rel_id = 0;
1659 for(EntityVector::iterator itr = nodes.begin(); itr != nodes.end(); ++itr, ++rel_id)
1660 {
1661 mesh.declare_relation(elem, *itr, rel_id);
1662 }
1663
1664 // Create family tree
1665 Entity family_tree = mesh.declare_entity(stk::topology::CONSTRAINT_RANK, my_family_tree_id, empty_parts);
1666 // Add relation to element
1667 unsigned downward_ordinal = 0; // we only have 1 down relation, it has ordinal 0
1668 mesh.declare_relation(family_tree, elem, downward_ordinal);
1669
1670 mesh.modification_end();
1671
1672 //
1673 // Test correctness of ghosting: Check that adjacent family-trees are ghosted on this proc
1674 //
1675
1676 // Compute and store ids of adjacent family-trees
1677 std::vector<EntityId> family_tree_ghost_ids;
1678 if(p_rank > 0)
1679 {
1680 family_tree_ghost_ids.push_back(my_family_tree_id - 1);
1681 }
1682 if(p_rank < p_size - 1)
1683 {
1684 family_tree_ghost_ids.push_back(my_family_tree_id + 1);
1685 }
1686
1687 // Check that my_family_tree exists and I own it
1688 Entity my_family_tree = mesh.get_entity(family_tree_rank, my_family_tree_id);
1689 ASSERT_TRUE(mesh.is_valid(my_family_tree));
1690 ASSERT_TRUE( (p_rank) == mesh.parallel_owner_rank(my_family_tree));
1691
1692 // Check that adjacent family-trees exist and are ghosted
1693 for(std::vector<EntityId>::const_iterator itr = family_tree_ghost_ids.begin(); itr != family_tree_ghost_ids.end(); ++itr)
1694 {
1695 int expected_ghosted_family_tree_id = *itr;
1696
1697 Entity expected_ghosted_family_tree = mesh.get_entity(family_tree_rank, expected_ghosted_family_tree_id);
1698 ASSERT_TRUE(mesh.is_valid(expected_ghosted_family_tree));
1699 ASSERT_TRUE(expected_ghosted_family_tree_id - 1 == mesh.parallel_owner_rank(expected_ghosted_family_tree));
1700
1701 stk::mesh::Bucket& bucket = mesh.bucket(expected_ghosted_family_tree);
1702 ASSERT_TRUE(!bucket.member(owned) && !bucket.member(shared));
1703 }
1704 }
1705
1706 TEST(BulkData, testChangeEntityPartsOfShared)
1707 {
1708 //
1709 // This unit-test is designed to test what happens when a shared entity
1710 // is moved on one processor during the same modification cycle in which
1711 // it was declared.
1712 //
1713 // p0 p1
1714 // 1---3---5
1715 // | 1 | 2 |
1716 // 2---4---6
1717 //
1718 // To test this, we use the mesh above, with each elem going on a separate
1719 // proc, one elem per proc. Node 3 is the node we'll be testing.
1720 //
1721
1722 stk::ParallelMachine pm = MPI_COMM_WORLD;
1723
1724 // Set up meta and bulk data
1725 const unsigned spatial_dim = 2;
1726 std::shared_ptr<BulkData> bulkPtr = stk::unit_test_util::build_mesh(spatial_dim, pm);
1727 stk::mesh::MetaData& meta_data = bulkPtr->mesh_meta_data();
1728 stk::mesh::BulkData& mesh = *bulkPtr;
1729 const EntityRank node_rank = stk::topology::NODE_RANK;
1730
1731 stk::mesh::Part& extra_node_part = meta_data.declare_part("extra_node_part", node_rank);
1732 meta_data.commit();
1733
1734 int p_rank = mesh.parallel_rank();
1735 int p_size = mesh.parallel_size();
1736
1737 // Bail unless in parallel
1738 if(p_size == 1)
1739 {
1740 return;
1741 }
1742
1743 // Begin modification cycle so we can create the entities and relations
1744 if(p_rank < 2)
1745 {
1746 mesh.modification_begin();
1747
1748 const unsigned nodes_per_elem = 4, nodes_per_side = 2;
1749 EntityKey node_key_to_move(node_rank, 3 /*id*/);
1750
1751 // We're just going to add everything to the universal part
1752 stk::mesh::ConstPartVector empty_parts, elem_parts;
1753 stk::mesh::Part &quad4_part = meta_data.get_topology_root_part(stk::topology::QUAD_4_2D);
1754 elem_parts.push_back(&quad4_part);
1755
1756 // Create element
1757 Entity elem = mesh.declare_element(p_rank + 1, elem_parts);
1758
1759 // Create nodes
1760 EntityVector nodes;
1761 const unsigned starting_node_id = p_rank * nodes_per_side + 1;
1762 for(unsigned id = starting_node_id; id < starting_node_id + nodes_per_elem; ++id)
1763 {
1764 nodes.push_back(mesh.declare_node(id, empty_parts));
1765 }
1766
1767 // Add relations to nodes
1768 unsigned rel_id = 0;
1769 for(EntityVector::iterator itr = nodes.begin(); itr != nodes.end(); ++itr, ++rel_id)
1770 {
1771 mesh.declare_relation(elem, *itr, rel_id);
1772 }
1773
1774 // On the processor that does *not* end up as the owner of the node, change its parts
1775 Entity changing_node = mesh.get_entity(node_key_to_move);
1776 if(p_rank == 1)
1777 {
1778 PartVector add_parts(1, &extra_node_part);
1779 mesh.change_entity_parts(changing_node, add_parts);
1780 }
1781 if (p_rank == 0)
1782 {
1783 Entity shared_node0 = nodes[2];
1784 Entity shared_node1 = nodes[3];
1785 mesh.add_node_sharing(shared_node0, 1);
1786 mesh.add_node_sharing(shared_node1, 1);
1787 }
1788 else
1789 {
1790 Entity shared_node0 = nodes[0];
1791 Entity shared_node1 = nodes[1];
1792 mesh.add_node_sharing(shared_node0, 0);
1793 mesh.add_node_sharing(shared_node1, 0);
1794 }
1795 mesh.modification_end();
1796
1797 // Expect that this is a shared node
1798 {
1799 std::vector<int> shared_procs;
1800 mesh.comm_shared_procs(mesh.entity_key(changing_node),shared_procs);
1801 EXPECT_FALSE(shared_procs.empty());
1802 }
1803
1804 EXPECT_TRUE(mesh.bucket(changing_node).member(extra_node_part));
1805
1806 mesh.modification_begin();
1807
1808 // On the processor that owns the node, change its parts
1809 if(p_rank == 0)
1810 {
1811 PartVector add_parts(1, &extra_node_part);
1812 mesh.change_entity_parts(changing_node, add_parts);
1813 }
1814
1815 mesh.modification_end();
1816
1817 // Expect that the part change *did* have an impact
1818 EXPECT_TRUE(mesh.bucket(changing_node).member(extra_node_part));
1819 }
1820 else
1821 {
1822 // On extra procs, do bare minimum
1823 mesh.modification_begin();
1824 mesh.modification_end();
1825 mesh.modification_begin();
1826 mesh.modification_end();
1827 }
1828 }
1829
1830 bool is_entity_key_shared(const stk::mesh::BulkData & mesh, stk::mesh::EntityKey key) {
1831 std::vector<int> shared_procs;
1832 mesh.comm_shared_procs(key,shared_procs);
1833 return !shared_procs.empty();
1834 }
1835
1836 void testParallelSideCreation(stk::mesh::BulkData::AutomaticAuraOption autoAuraOption)
1837 {
1838 //
1839 // This unit-test is designed to test what happens when a shared sides are created on
1840 // both processors that share the side with different global ids. Then synchonization
1841 // is done as a second step.
1842 //
1843 // 1---4---5
1844 // | 1 | 2 |
1845 // 2---3---6
1846
1847 // element 1 conn = { 1, 2, 3, 4 }
1848 // element 2 conn = { 3, 4, 5, 6 }
1849
1850 // edge is nodes 3-4 (element 1, side id 2, perm 0)
1851 // edge is nodes 3-4 (element 2, side id 0, perm 0)
1852 //
1853 // To test this, we use the mesh above, with each elem going on a separate
1854 // proc, one elem per proc. Node 3 is the node we'll be testing.
1855 //
1856
1857 stk::ParallelMachine pm = MPI_COMM_WORLD;
1858
1859 // Set up meta and bulk data
1860 const unsigned spatial_dim = 2;
1861 std::shared_ptr<BulkData> bulkPtr = stk::unit_test_util::build_mesh(spatial_dim, pm, autoAuraOption);
1862 stk::mesh::MetaData& meta_data = bulkPtr->mesh_meta_data();
1863 stk::mesh::BulkData& mesh = *bulkPtr;
1864 const EntityRank node_rank = stk::topology::NODE_RANK;
1865 const EntityRank side_rank = stk::topology::EDGE_RANK;
1866
1867 stk::mesh::Part& side_part = meta_data.declare_part_with_topology("side_part", stk::topology::LINE_2);
1868 stk::mesh::Part& elem_part = meta_data.declare_part_with_topology("elem_part", stk::topology::QUAD_4_2D);
1869
1870 meta_data.commit();
1871
1872 int p_rank = mesh.parallel_rank();
1873 int p_size = mesh.parallel_size();
1874
1875 // Bail unless in parallel
1876 if(p_size == 1)
1877 {
1878 return;
1879 }
1880
1881 // Begin modification cycle so we can create the entities and relations
1882 if(p_rank < 2)
1883 {
1884 mesh.modification_begin();
1885
1886 const unsigned nodes_per_elem = 4, nodes_per_side = 2;
1887 EntityKey node_key_to_move(node_rank, 3 /*id*/);
1888
1889 // We're just going to add everything to the universal part
1890 stk::mesh::ConstPartVector empty_parts;
1891
1892 // Create nodes
1893 EntityVector nodes;
1894 const unsigned starting_node_id = p_rank * nodes_per_side + 1;
1895 for(unsigned id = starting_node_id; id < starting_node_id + nodes_per_elem; ++id)
1896 {
1897 nodes.push_back(mesh.declare_node(id, empty_parts));
1898 }
1899
1900 // 1-2, 1
1901 // 2-3, 2
1902 // 3-4, 3
1903 // 4-1, 4
1904
1905 // 3-4, 1
1906 // 4-5, 2
1907 // 5-6, 3
1908 // 6-3, 4
1909
1910 // Create element
1911 const EntityId elem_id = p_rank + 1;
1912 Entity elem = mesh.declare_element(elem_id, stk::mesh::ConstPartVector{&elem_part});
1913
1914 // Add element relations to nodes
1915 unsigned elem_rel_id = 0;
1916 for(EntityVector::iterator itr = nodes.begin(); itr != nodes.end(); ++itr, ++elem_rel_id)
1917 {
1918 mesh.declare_relation(elem, *itr, elem_rel_id);
1919 }
1920
1921 // Add side relations to nodes and element
1922 EntityVector side_nodes;
1923 side_nodes.push_back(mesh.get_entity(stk::topology::NODE_RANK, 3));
1924 side_nodes.push_back(mesh.get_entity(stk::topology::NODE_RANK, 4));
1925 mesh.add_node_sharing(side_nodes[0], (p_rank == 0 ? 1 : 0));
1926 mesh.add_node_sharing(side_nodes[1], (p_rank == 0 ? 1 : 0));
1927 stk::topology elem_top = mesh.bucket(elem).topology();
1928 unsigned local_side_ordinal = 2;
1929 if (p_rank == 1)
1930 {
1931 local_side_ordinal = 0;
1932 }
1933
1934 // Create local version of side on each proc
1935 Entity side = mesh.declare_element_side(elem, local_side_ordinal, stk::mesh::ConstPartVector{&side_part});
1936
1937 stk::mesh::Permutation perm1 = stk::mesh::find_permutation(mesh,
1938 elem_top, nodes.data(), elem_top.side_topology(local_side_ordinal), side_nodes.data(), local_side_ordinal);
1939 ASSERT_TRUE(perm1 != stk::mesh::Permutation::INVALID_PERMUTATION);
1940 mesh.modification_end();
1941
1942 // Expect that the side is not shared, but the nodes of side are shared
1943 EXPECT_TRUE(is_entity_key_shared(mesh,mesh.entity_key(side)));
1944 EXPECT_TRUE(is_entity_key_shared(mesh,mesh.entity_key(side_nodes[0])));
1945 EXPECT_TRUE(is_entity_key_shared(mesh,mesh.entity_key(side_nodes[1])));
1946
1947 // Now "detect" that there is a duplicate aura side using the side nodes
1948 EntityVector sides;
1949 get_entities_through_relations(mesh, side_nodes, side_rank, sides);
1950 EXPECT_EQ(1u, sides.size());
1951
1952 mesh.modification_begin();
1953
1954 // Delete the local side and create new, shared side
1955 side = sides[0];
1956 bool destroyrelationship = mesh.destroy_relation(elem, side, local_side_ordinal);
1957 EXPECT_TRUE(destroyrelationship);
1958 mesh.modification_end();
1959 //must call this here to delete ghosts, kills relationship between side and ghost of elem on other proc, allows side to be deleted in next phase
1960 mesh.modification_begin();
1961 bool successfully_destroyed = mesh.destroy_entity(side);
1962 if (p_rank == 0) {
1963 EXPECT_TRUE(successfully_destroyed);
1964 }
1965 else {
1966 EXPECT_FALSE(successfully_destroyed);
1967 }
1968 side = mesh.declare_element_side(elem, local_side_ordinal, stk::mesh::ConstPartVector{&side_part});
1969
1970 stk::mesh::Permutation perm2 = stk::mesh::find_permutation(mesh,
1971 elem_top, nodes.data(), elem_top.side_topology(local_side_ordinal), side_nodes.data(), local_side_ordinal);
1972 ASSERT_TRUE(perm2 != stk::mesh::Permutation::INVALID_PERMUTATION);
1973
1974 mesh.modification_end();
1975
1976 // Expect that the side is shared, and nodes of side are shared
1977 EXPECT_TRUE(is_entity_key_shared(mesh,mesh.entity_key(side)));
1978 EXPECT_TRUE(is_entity_key_shared(mesh,mesh.entity_key(side_nodes[0])));
1979 EXPECT_TRUE(is_entity_key_shared(mesh,mesh.entity_key(side_nodes[1])));
1980
1981 // Check that there is only a single side using the side nodes
1982 get_entities_through_relations(mesh, side_nodes, side_rank, sides);
1983 EXPECT_EQ(1u, sides.size());
1984 }
1985 else
1986 {
1987 // On extra procs, do bare minimum, collective calls must be made on each proc
1988 mesh.modification_begin();
1989 mesh.modification_end();
1990 mesh.modification_begin();
1991 mesh.modification_end();
1992 mesh.modification_begin();
1993 mesh.modification_end();
1994 }
1995 }
1996
1997 TEST(BulkData, testParallelSideCreationWithAura)
1998 {
1999 testParallelSideCreation(stk::mesh::BulkData::AUTO_AURA);
2000 }
2001
2002 TEST(BulkData, testParallelSideCreationWithoutAura)
2003 {
2004 testParallelSideCreation(stk::mesh::BulkData::NO_AUTO_AURA);
2005 }
2006
2007 //----------------------------------------------------------------------
2008 //----------------------------------------------------------------------
2009 class BulkDataWithHexes : public stk::mesh::fixtures::TestHexFixture {};
2010
2011 // Testing of field_data_footprint(.)
2012 TEST_F(BulkDataWithHexes, test_total_field_data_footprint )
2013 {
2014 // Test 3x1x1 HexFixture structure
2015 const unsigned NX = 3;
2016 const unsigned NY = 1;
2017 const unsigned NZ = 1;
2018 setup_mesh(NX, NY, NZ);
2019
2020 const stk::mesh::BulkData &mesh = get_bulk();
2021
2022 // Call function we're testing
2023 size_t field_data_footprint = stk::mesh::get_total_ngp_field_allocation_bytes(get_coord_field());
2024
2025 // Alternative computation explicitly gathers buckets.
2026 size_t node_fields_footprint = 0;
2027 const stk::mesh::BucketVector &node_buckets = mesh.buckets(stk::topology::NODE_RANK);
2028 for(size_t i = 0; i < node_buckets.size(); ++i)
2029 {
2030 node_fields_footprint += mesh.get_maximum_bucket_capacity() * field_bytes_per_entity(get_coord_field(), *node_buckets[i]);
2031 }
2032
2033 EXPECT_EQ(node_fields_footprint, field_data_footprint);
2034 }
2035
2036 //----------------------------------------------------------------------
2037 //----------------------------------------------------------------------
2038 // Testing of get_buckets and get_entities functions
2039 TEST_F(BulkDataWithHexes, test_get_entities )
2040 {
2041 // Test 3x4x4 HexFixture structure
2042 const unsigned NX = 3;
2043 const unsigned NY = 4;
2044 const unsigned NZ = 40;
2045 setup_mesh(NX, NY, NZ);
2046
2047 const stk::mesh::BulkData &mesh = get_bulk();
2048
2049 Selector select_owned(mesh.mesh_meta_data().locally_owned_part());
2050 const stk::mesh::BucketVector &bucket_ptrs = mesh.get_buckets(stk::topology::NODE_RANK, select_owned);
2051 stk::mesh::EntityVector entities;
2052 stk::mesh::get_selected_entities(select_owned, mesh.buckets(stk::topology::NODE_RANK), entities);
2053 //
2054 // Confirm that the number of entities exracted by either bucket or entity access is identical
2055 //
2056 int numBucketEntities = 0;
2057 std::map<stk::mesh::Entity, int> entityMap;
2058 for(unsigned int ibucket = 0; ibucket < bucket_ptrs.size(); ++ibucket)
2059 {
2060 numBucketEntities += bucket_ptrs[ibucket]->size();
2061 for(unsigned iobj = 0; iobj < bucket_ptrs[ibucket]->size(); ++iobj)
2062 {
2063 entityMap[(*bucket_ptrs[ibucket])[iobj]] = 1;
2064 }
2065 }
2066 int numEntities = entities.size();
2067 EXPECT_EQ(numBucketEntities, numEntities);
2068 //
2069 // Confirm that the actual contents of the entity lists are identical
2070 //
2071 for(unsigned int iobj = 0; iobj < entities.size(); ++iobj)
2072 {
2073 ASSERT_TRUE(entityMap.find(entities[iobj]) != entityMap.end());
2074 }
2075 //
2076 // confirm the total number of entities is the expected (41*5*4) = 820, the total number of unique nodes in the mesh
2077 //
2078 int globalNumEntities = numEntities;
2079 stk::all_reduce(MPI_COMM_WORLD, stk::ReduceSum<1>(&globalNumEntities));
2080
2081 EXPECT_EQ(globalNumEntities, 820);
2082 }
2083
2084 //----------------------------------------------------------------------
2085 //----------------------------------------------------------------------
2086 // Testing of communicate_field_data
2087
2088 typedef stk::mesh::Field<int> PressureFieldType;
2089
2090 static void test_sync_1(stk::mesh::BulkData& eMesh, PressureFieldType& pressure_field, bool sync_shared, bool sync_aura)
2091 {
2092 unsigned p_rank = eMesh.parallel_rank();
2093
2094 const stk::mesh::BucketVector & buckets = eMesh.buckets(stk::topology::NODE_RANK);
2095
2096 enum Type
2097 {
2098 Owned, Shared, Ghost
2099 };
2100
2101 for(stk::mesh::BucketVector::const_iterator k = buckets.begin(); k != buckets.end(); ++k)
2102 {
2103 {
2104 stk::mesh::Bucket & bucket = **k;
2105
2106 const unsigned num_elements_in_bucket = bucket.size();
2107
2108 for(unsigned iEntity = 0; iEntity < num_elements_in_bucket; iEntity++)
2109 {
2110 stk::mesh::Entity entity = bucket[iEntity];
2111 int * const p = stk::mesh::field_data(pressure_field, entity);
2112 stk::mesh::EntityId id = eMesh.identifier(entity);
2113
2114 if(bucket.owned())
2115 {
2116 p[0] = (p_rank + 1) * 100 + id;
2117 }
2118 else if(bucket.shared())
2119 {
2120 p[0] = -((eMesh.parallel_owner_rank(entity) + 1) * 100 + id);
2121 }
2122 else
2123 {
2124 p[0] = ((p_rank + 1) * 1000 + id);
2125 }
2126
2127 }
2128 }
2129 }
2130
2131 {
2132 std::vector<const stk::mesh::FieldBase *> fields;
2133 fields.push_back(&pressure_field);
2134
2135 // only the aura = !locally_owned_part && !globally_shared_part (outer layer)
2136 if(sync_aura)
2137 stk::mesh::communicate_field_data(eMesh.aura_ghosting(), fields);
2138
2139 // the shared part (just the shared boundary)
2140 if(sync_shared)
2141 stk::mesh::copy_owned_to_shared(eMesh, fields);
2142 }
2143
2144 for(stk::mesh::BucketVector::const_iterator k = buckets.begin(); k != buckets.end(); ++k)
2145 {
2146 {
2147 stk::mesh::Bucket & bucket = **k;
2148
2149 const unsigned num_elements_in_bucket = bucket.size();
2150
2151 for(unsigned iEntity = 0; iEntity < num_elements_in_bucket; iEntity++)
2152 {
2153 stk::mesh::Entity entity = bucket[iEntity];
2154 stk::mesh::EntityId id = eMesh.identifier(entity);
2155 int * const p = stk::mesh::field_data(pressure_field, entity);
2156 double p_e = (p_rank + 1) * 100 + id;
2157 if(bucket.owned())
2158 {
2159 ASSERT_EQ(p[0], p_e);
2160 }
2161 else if(bucket.shared())
2162 {
2163 p_e = ((eMesh.parallel_owner_rank(entity) + 1) * 100 + id);
2164 if(sync_shared)
2165 {
2166 ASSERT_EQ(p[0], p_e);
2167 }
2168 }
2169 else
2170 {
2171 p_e = ((eMesh.parallel_owner_rank(entity) + 1) * 100 + id);
2172 if(sync_aura)
2173 {
2174 ASSERT_EQ(p[0], p_e);
2175 }
2176 }
2177 }
2178 }
2179 }
2180 }
2181
2182 TEST(BulkData, testFieldComm)
2183 {
2184 stk::ParallelMachine pm = MPI_COMM_WORLD;
2185 MPI_Barrier( pm);
2186
2187 // run this with exercise_field_sync_bug = true, and 3 <= nprocs <= 4 to show the possible bug
2188 bool exercise_field_sync_bug = true;
2189
2190 const unsigned p_size = stk::parallel_machine_size(pm);
2191
2192 const int spatial_dimension = 3;
2193 MetaData meta(spatial_dimension);
2194
2195 meta.commit();
2196
2197 //------------------------------
2198 // 3d seems to be fine...
2199 if(p_size <= 4)
2200 {
2201 const int root_box[3][2] = { {0, 2}, {0, 2}, {0, 1}}; // emulate 2d box
2202
2203 BoxFixture fixture(pm, stk::mesh::BulkData::AUTO_AURA, 100);
2204 PressureFieldType& p_field = fixture.fem_meta().declare_field<int>(stk::topology::NODE_RANK, "p");
2205 stk::mesh::put_field_on_mesh(p_field, fixture.fem_meta().universal_part(), nullptr);
2206 fixture.fem_meta().commit();
2207 BulkData & bulk = fixture.bulk_data();
2208 int local_box[3][2] = { {0, 0}, {0, 0}, {0, 0}};
2209
2210 bulk.modification_begin();
2211 fixture.generate_boxes(root_box, local_box);
2212 bulk.modification_end();
2213
2214 {
2215 bool shared_aura = false;
2216 bool shared = false;
2217 test_sync_1(bulk, p_field, shared, shared_aura);
2218 test_sync_1(bulk, p_field, false, true);
2219 if(exercise_field_sync_bug || p_size <= 2)
2220 {
2221 test_sync_1(bulk, p_field, true, false);
2222 test_sync_1(bulk, p_field, true, true);
2223 }
2224 }
2225 }
2226
2227 //------------------------------
2228 // 2d, not so much
2229 if(p_size <= 4)
2230 {
2231 stk::mesh::fixtures::QuadFixture fixture(pm, 2 /*nx*/, 2 /*ny*/);
2232 PressureFieldType& p_field = fixture.m_meta.declare_field<int>(stk::topology::NODE_RANK, "p");
2233 stk::mesh::put_field_on_mesh(p_field, fixture.m_meta.universal_part(), nullptr);
2234 fixture.m_meta.commit();
2235 fixture.generate_mesh();
2236 stk::mesh::BulkData & bulk = fixture.m_bulk_data;
2237
2238 {
2239 bool shared_aura = false;
2240 bool shared = false;
2241 test_sync_1(bulk, p_field, shared, shared_aura);
2242 test_sync_1(bulk, p_field, false, true);
2243 if(exercise_field_sync_bug || p_size <= 2)
2244 {
2245 test_sync_1(bulk, p_field, true, false);
2246 test_sync_1(bulk, p_field, true, true);
2247 }
2248 }
2249 }
2250 }
2251
2252 // testing comm lists and custom ghosting
2253
2254 TEST(BulkData, testCommList)
2255 {
2256 /**
2257 * This is a boiled-down version of a stk_adapt situation that is failing
2258 * where a custom ghosted node is later shared because it becomes part
2259 * of an element (this occurs during hanging-node refinement). It is
2260 * a definite edge case, but exposed an assumption in the comm_mesh_verify_parallel_consistency()
2261 * function, that a comm list can't have both a shared and custom ghosted node.
2262 * This test currently just verifies the issue exists. When comm_mesh_verify_parallel_consistency()
2263 * is fixed, this test should be modified to enforce no failure under debug mode.
2264 *
2265 * Mesh
2266 * 7---8---9 P0 owns nodes 1,2,4,5; P, elem 1
2267 * | 3 | 4 | P1 : 3,6, elem 2
2268 * 4---5---6 P2 : 7,8, elem 3
2269 * | 1 | 2 | P3 : 9, elem 4
2270 * 1---2---3
2271 *
2272 * node 5 ghosted to proc 3, node 9 ghosted to proc 0
2273 *
2274 * Comm list looks like this after the ghosting operations (obtained from print_comm_list()):
2275 * legend: (ghost_id, proc)
2276 * P3: NODE[9] owner(3) (1,0) (1,1) (1,2) (2,0)
2277 * P0: NODE[9] owner(3) (1,3) (2,3)
2278 *
2279 * elem 1 modified to replace relation elem[1] -> node[1] with node[9] (previously ghosted)
2280 *
2281 * This last step induces a comm list like this
2282 *
2283 * P3: NODE[9] owner(3) mod (0,0) (1,1) (1,2) (2,0)
2284 * P0: NODE[9] owner(3) mod (0,3) (2,3)
2285 *
2286 * Note the repetition of proc 3 in both the shared (ghost_id=0) and ghosted (id=2) list
2287 * for P0 (and repetition of proc 0 in P3's list). This causes the
2288 * comm_mesh_verify_parallel_consistency() test to fail, although
2289 * this should be a valid mesh, e.g., for periodic b.c.'s we might want this situation.
2290 *
2291 */
2292
2293 stk::ParallelMachine pm = MPI_COMM_WORLD;
2294 MPI_Barrier( pm);
2295
2296 const unsigned p_size = stk::parallel_machine_size(pm);
2297 const unsigned p_rank = stk::parallel_machine_rank(pm);
2298
2299 //------------------------------
2300 if(p_size != 4)
2301 return;
2302
2303 //------------------------------
2304 // test begin/end pair
2305 {
2306 stk::mesh::fixtures::QuadFixture fixture(pm, 2 /*nx*/, 2 /*ny*/);
2307 fixture.m_meta.commit();
2308 fixture.generate_mesh();
2309 stk::mesh::BulkData & bulk = fixture.m_bulk_data;
2310 bulk.modification_begin();
2311 bulk.modification_end();
2312 }
2313
2314 //------------------------------
2315 // test begin/end pair with mesh mods
2316 {
2317 stk::mesh::fixtures::QuadFixture fixture(pm, 2 /*nx*/, 2 /*ny*/);
2318 fixture.m_meta.commit();
2319 fixture.generate_mesh();
2320 stk::mesh::BulkData & bulk = fixture.m_bulk_data;
2321 bulk.modification_begin();
2322 bulk.modification_end();
2323
2324 bulk.modification_begin();
2325
2326 // add some custom ghosting
2327 stk::mesh::Ghosting & ghosting = bulk.create_ghosting(std::string("new_nodes"));
2328 std::vector<stk::mesh::EntityKey> receive;
2329 ghosting.receive_list(receive);
2330
2331 std::vector<stk::mesh::EntityProc> nodes_to_ghost;
2332
2333 if(p_rank == 0)
2334 {
2335 stk::mesh::Entity node_5 = bulk.get_entity(stk::topology::NODE_RANK, 5);
2336 EXPECT_TRUE(bulk.is_valid(node_5));
2337 nodes_to_ghost.push_back(stk::mesh::EntityProc(node_5, 3));
2338 }
2339
2340 if(p_rank == 3)
2341 {
2342 stk::mesh::Entity node = bulk.get_entity(stk::topology::NODE_RANK, 9);
2343 EXPECT_TRUE(bulk.is_valid(node));
2344 nodes_to_ghost.push_back(stk::mesh::EntityProc(node, 0));
2345 }
2346
2347 bulk.change_ghosting(ghosting, nodes_to_ghost, receive);
2348
2349 bulk.modification_end();
2350
2351 bulk.modification_begin();
2352
2353 stk::mesh::Entity node9 = bulk.get_entity(stk::topology::NODE_RANK, 9);
2354 EXPECT_TRUE(bulk.is_valid(node9));
2355 if(p_rank == 0)
2356 {
2357 stk::mesh::Entity node1 = bulk.get_entity(stk::topology::NODE_RANK, 1);
2358 EXPECT_TRUE(bulk.is_valid(node1));
2359 stk::mesh::Entity elem = bulk.get_entity(stk::topology::ELEMENT_RANK, 1);
2360 EXPECT_TRUE(bulk.is_valid(elem));
2361 bulk.destroy_relation(elem, node1, 0);
2362 bulk.declare_relation(elem, node9, 0);
2363 bulk.add_node_sharing(node9, 3);
2364 }
2365
2366 if (p_rank == 3)
2367 {
2368 bulk.add_node_sharing(node9, 0);
2369 }
2370
2371 bool failed = false;
2372 try
2373 {
2374 bulk.modification_end();
2375 }
2376 catch(const std::exception & X)
2377 {
2378 std::cout<<X.what()<<std::endl;
2379 failed = true;
2380 }
2381 EXPECT_FALSE(failed);
2382
2383 }
2384
2385 }
2386
2387 std::string printGhostData(stk::mesh::BulkData & bulkData, stk::mesh::Entity entity)
2388 {
2389 std::ostringstream oss;
2390 std::vector<stk::mesh::impl::EntityGhostData> egd;
2391 stk::mesh::impl::get_ghost_data(bulkData, entity, egd);
2392 for(size_t z = 0; z < egd.size(); ++z)
2393 {
2394 oss << "P" << bulkData.parallel_rank() << ": " << egd[z] << std::endl;
2395 }
2396 return oss.str();
2397 }
2398
2399 std::string printGhostDataByRank(stk::mesh::BulkData & bulkData, stk::topology::rank_t rank)
2400 {
2401 const stk::mesh::BucketVector & buckets = bulkData.buckets(rank);
2402 std::ostringstream oss;
2403 oss << "P" << bulkData.parallel_rank() << ": rank=" << rank << std::endl;
2404 for(size_t k = 0; k < buckets.size(); ++k)
2405 {
2406 const stk::mesh::Bucket::iterator begin = buckets[k]->begin();
2407 const stk::mesh::Bucket::iterator end = buckets[k]->end();
2408 for(stk::mesh::Bucket::iterator it = begin; it != end; ++it)
2409 {
2410 oss << printGhostData(bulkData, *it);
2411 }
2412 }
2413 return oss.str();
2414 }
2415
2416 TEST(BulkData, EntityGhostData)
2417 {
2418 std::string gold_result = "(Entity_lid=0, direction=SEND, processor=128, ghosting level=LOCALLY_OWNED)";
2419 stk::mesh::impl::EntityGhostData data;
2420 data.direction = stk::mesh::impl::EntityGhostData::SEND;
2421 data.ghostingLevel = stk::mesh::impl::EntityGhostData::LOCALLY_OWNED;
2422 data.processor = 128;
2423 std::ostringstream oss;
2424 oss << data;
2425 EXPECT_EQ( gold_result, oss.str());
2426 }
2427
2428 TEST(BulkData, get_ghost_data)
2429 {
2430 using std::string;
2431 MPI_Comm communicator = MPI_COMM_WORLD;
2432 int psize = stk::parallel_machine_size(communicator);
2433
2434 if(psize == 3)
2435 { // Skip unless we're on 3 processors
2436 stk::io::StkMeshIoBroker stkMeshIoBroker(communicator);
2437 const string generatedMeshSpecification = "generated:1x1x3";
2438 stkMeshIoBroker.add_mesh_database(generatedMeshSpecification, stk::io::READ_MESH);
2439 stkMeshIoBroker.create_input_mesh();
2440 stkMeshIoBroker.populate_bulk_data();
2441
2442 stk::mesh::BulkData &stkMeshBulkData = stkMeshIoBroker.bulk_data();
2443
2444 if(stkMeshBulkData.parallel_rank() == 0)
2445 {
2446 std::ostringstream oss;
2447 for(stk::topology::rank_t rank = stk::topology::NODE_RANK; rank <= stk::topology::ELEMENT_RANK; ++rank)
2448 {
2449 oss << printGhostDataByRank(stkMeshBulkData, rank);
2450 }
2451 string gold_result =
2452 string("P0: rank=NODE_RANK\n")
2453 + string("P0: (Entity_gid=1, rank=0, direction=NONE, processor=0, ghosting level=LOCALLY_OWNED)\n")
2454 + string("P0: (Entity_gid=1, rank=0, direction=SEND, processor=1, ghosting level=AURA)\n")
2455 + string("P0: (Entity_gid=2, rank=0, direction=NONE, processor=0, ghosting level=LOCALLY_OWNED)\n")
2456 + string("P0: (Entity_gid=2, rank=0, direction=SEND, processor=1, ghosting level=AURA)\n")
2457 + string("P0: (Entity_gid=3, rank=0, direction=NONE, processor=0, ghosting level=LOCALLY_OWNED)\n")
2458 + string("P0: (Entity_gid=3, rank=0, direction=SEND, processor=1, ghosting level=AURA)\n")
2459 + string("P0: (Entity_gid=4, rank=0, direction=NONE, processor=0, ghosting level=LOCALLY_OWNED)\n")
2460 + string("P0: (Entity_gid=4, rank=0, direction=SEND, processor=1, ghosting level=AURA)\n")
2461 + string("P0: (Entity_gid=9, rank=0, direction=RECEIVE, processor=1, ghosting level=AURA)\n")
2462 + string("P0: (Entity_gid=10, rank=0, direction=RECEIVE, processor=1, ghosting level=AURA)\n")
2463 + string("P0: (Entity_gid=11, rank=0, direction=RECEIVE, processor=1, ghosting level=AURA)\n")
2464 + string("P0: (Entity_gid=12, rank=0, direction=RECEIVE, processor=1, ghosting level=AURA)\n")
2465 + string("P0: (Entity_gid=5, rank=0, direction=NONE, processor=0, ghosting level=LOCALLY_OWNED)\n")
2466 + string("P0: (Entity_gid=5, rank=0, direction=SEND, processor=1, ghosting level=SHARED)\n")
2467 + string("P0: (Entity_gid=5, rank=0, direction=SEND, processor=2, ghosting level=AURA)\n")
2468 + string("P0: (Entity_gid=6, rank=0, direction=NONE, processor=0, ghosting level=LOCALLY_OWNED)\n")
2469 + string("P0: (Entity_gid=6, rank=0, direction=SEND, processor=1, ghosting level=SHARED)\n")
2470 + string("P0: (Entity_gid=6, rank=0, direction=SEND, processor=2, ghosting level=AURA)\n")
2471 + string("P0: (Entity_gid=7, rank=0, direction=NONE, processor=0, ghosting level=LOCALLY_OWNED)\n")
2472 + string("P0: (Entity_gid=7, rank=0, direction=SEND, processor=1, ghosting level=SHARED)\n")
2473 + string("P0: (Entity_gid=7, rank=0, direction=SEND, processor=2, ghosting level=AURA)\n")
2474 + string("P0: (Entity_gid=8, rank=0, direction=NONE, processor=0, ghosting level=LOCALLY_OWNED)\n")
2475 + string("P0: (Entity_gid=8, rank=0, direction=SEND, processor=1, ghosting level=SHARED)\n")
2476 + string("P0: (Entity_gid=8, rank=0, direction=SEND, processor=2, ghosting level=AURA)\n")
2477 + string("P0: rank=EDGE_RANK\n")
2478 + string("P0: rank=FACE_RANK\n")
2479 + string("P0: rank=ELEMENT_RANK\n")
2480 + string("P0: (Entity_gid=1, rank=3, direction=NONE, processor=0, ghosting level=LOCALLY_OWNED)\n")
2481 + string("P0: (Entity_gid=1, rank=3, direction=SEND, processor=1, ghosting level=AURA)\n")
2482 + string("P0: (Entity_gid=2, rank=3, direction=RECEIVE, processor=1, ghosting level=AURA)\n");
2483 EXPECT_EQ( gold_result, oss.str());
2484 }
2485 else if(stkMeshBulkData.parallel_rank() == 1)
2486 {
2487 std::ostringstream oss;
2488 for(stk::topology::rank_t rank = stk::topology::NODE_RANK; rank <= stk::topology::ELEMENT_RANK; ++rank)
2489 {
2490 oss << printGhostDataByRank(stkMeshBulkData, rank);
2491 }
2492 std::string gold_result =
2493 string("P1: rank=NODE_RANK\n")
2494 + string("P1: (Entity_gid=5, rank=0, direction=RECEIVE, processor=0, ghosting level=SHARED)\n")
2495 + string("P1: (Entity_gid=6, rank=0, direction=RECEIVE, processor=0, ghosting level=SHARED)\n")
2496 + string("P1: (Entity_gid=7, rank=0, direction=RECEIVE, processor=0, ghosting level=SHARED)\n")
2497 + string("P1: (Entity_gid=8, rank=0, direction=RECEIVE, processor=0, ghosting level=SHARED)\n")
2498 + string("P1: (Entity_gid=1, rank=0, direction=RECEIVE, processor=0, ghosting level=AURA)\n")
2499 + string("P1: (Entity_gid=2, rank=0, direction=RECEIVE, processor=0, ghosting level=AURA)\n")
2500 + string("P1: (Entity_gid=3, rank=0, direction=RECEIVE, processor=0, ghosting level=AURA)\n")
2501 + string("P1: (Entity_gid=4, rank=0, direction=RECEIVE, processor=0, ghosting level=AURA)\n")
2502 + string("P1: (Entity_gid=13, rank=0, direction=RECEIVE, processor=2, ghosting level=AURA)\n")
2503 + string("P1: (Entity_gid=14, rank=0, direction=RECEIVE, processor=2, ghosting level=AURA)\n")
2504 + string("P1: (Entity_gid=15, rank=0, direction=RECEIVE, processor=2, ghosting level=AURA)\n")
2505 + string("P1: (Entity_gid=16, rank=0, direction=RECEIVE, processor=2, ghosting level=AURA)\n")
2506 + string("P1: (Entity_gid=9, rank=0, direction=NONE, processor=1, ghosting level=LOCALLY_OWNED)\n")
2507 + string("P1: (Entity_gid=9, rank=0, direction=SEND, processor=2, ghosting level=SHARED)\n")
2508 + string("P1: (Entity_gid=9, rank=0, direction=SEND, processor=0, ghosting level=AURA)\n")
2509 + string("P1: (Entity_gid=10, rank=0, direction=NONE, processor=1, ghosting level=LOCALLY_OWNED)\n")
2510 + string("P1: (Entity_gid=10, rank=0, direction=SEND, processor=2, ghosting level=SHARED)\n")
2511 + string("P1: (Entity_gid=10, rank=0, direction=SEND, processor=0, ghosting level=AURA)\n")
2512 + string("P1: (Entity_gid=11, rank=0, direction=NONE, processor=1, ghosting level=LOCALLY_OWNED)\n")
2513 + string("P1: (Entity_gid=11, rank=0, direction=SEND, processor=2, ghosting level=SHARED)\n")
2514 + string("P1: (Entity_gid=11, rank=0, direction=SEND, processor=0, ghosting level=AURA)\n")
2515 + string("P1: (Entity_gid=12, rank=0, direction=NONE, processor=1, ghosting level=LOCALLY_OWNED)\n")
2516 + string("P1: (Entity_gid=12, rank=0, direction=SEND, processor=2, ghosting level=SHARED)\n")
2517 + string("P1: (Entity_gid=12, rank=0, direction=SEND, processor=0, ghosting level=AURA)\n")
2518 + string("P1: rank=EDGE_RANK\n")
2519 + string("P1: rank=FACE_RANK\n")
2520 + string("P1: rank=ELEMENT_RANK\n")
2521 + string("P1: (Entity_gid=2, rank=3, direction=NONE, processor=1, ghosting level=LOCALLY_OWNED)\n")
2522 + string("P1: (Entity_gid=2, rank=3, direction=SEND, processor=0, ghosting level=AURA)\n")
2523 + string("P1: (Entity_gid=2, rank=3, direction=SEND, processor=2, ghosting level=AURA)\n")
2524 + string("P1: (Entity_gid=1, rank=3, direction=RECEIVE, processor=0, ghosting level=AURA)\n")
2525 + string("P1: (Entity_gid=3, rank=3, direction=RECEIVE, processor=2, ghosting level=AURA)\n");
2526 EXPECT_EQ( gold_result, oss.str());
2527 }
2528 else
2529 { // if (stkMeshBulkData.parallel_rank() == 2)
2530 std::ostringstream oss;
2531 for(stk::topology::rank_t rank = stk::topology::NODE_RANK; rank <= stk::topology::ELEMENT_RANK; ++rank)
2532 {
2533 oss << printGhostDataByRank(stkMeshBulkData, rank);
2534 }
2535 std::string gold_result =
2536 string("P2: rank=NODE_RANK\n")
2537 + string("P2: (Entity_gid=13, rank=0, direction=NONE, processor=2, ghosting level=LOCALLY_OWNED)\n")
2538 + string("P2: (Entity_gid=13, rank=0, direction=SEND, processor=1, ghosting level=AURA)\n")
2539 + string("P2: (Entity_gid=14, rank=0, direction=NONE, processor=2, ghosting level=LOCALLY_OWNED)\n")
2540 + string("P2: (Entity_gid=14, rank=0, direction=SEND, processor=1, ghosting level=AURA)\n")
2541 + string("P2: (Entity_gid=15, rank=0, direction=NONE, processor=2, ghosting level=LOCALLY_OWNED)\n")
2542 + string("P2: (Entity_gid=15, rank=0, direction=SEND, processor=1, ghosting level=AURA)\n")
2543 + string("P2: (Entity_gid=16, rank=0, direction=NONE, processor=2, ghosting level=LOCALLY_OWNED)\n")
2544 + string("P2: (Entity_gid=16, rank=0, direction=SEND, processor=1, ghosting level=AURA)\n")
2545 + string("P2: (Entity_gid=9, rank=0, direction=RECEIVE, processor=1, ghosting level=SHARED)\n")
2546 + string("P2: (Entity_gid=10, rank=0, direction=RECEIVE, processor=1, ghosting level=SHARED)\n")
2547 + string("P2: (Entity_gid=11, rank=0, direction=RECEIVE, processor=1, ghosting level=SHARED)\n")
2548 + string("P2: (Entity_gid=12, rank=0, direction=RECEIVE, processor=1, ghosting level=SHARED)\n")
2549 + string("P2: (Entity_gid=5, rank=0, direction=RECEIVE, processor=0, ghosting level=AURA)\n")
2550 + string("P2: (Entity_gid=6, rank=0, direction=RECEIVE, processor=0, ghosting level=AURA)\n")
2551 + string("P2: (Entity_gid=7, rank=0, direction=RECEIVE, processor=0, ghosting level=AURA)\n")
2552 + string("P2: (Entity_gid=8, rank=0, direction=RECEIVE, processor=0, ghosting level=AURA)\n")
2553 + string("P2: rank=EDGE_RANK\n")
2554 + string("P2: rank=FACE_RANK\n")
2555 + string("P2: rank=ELEMENT_RANK\n")
2556 + string("P2: (Entity_gid=3, rank=3, direction=NONE, processor=2, ghosting level=LOCALLY_OWNED)\n")
2557 + string("P2: (Entity_gid=3, rank=3, direction=SEND, processor=1, ghosting level=AURA)\n")
2558 + string("P2: (Entity_gid=2, rank=3, direction=RECEIVE, processor=1, ghosting level=AURA)\n");
2559 EXPECT_EQ( gold_result, oss.str());
2560 }
2561 }
2562 }
2563
2564 // Add to documentation tests for modification_end FAQ:
2565 // 1. How does change owner work? Can I change the owner of a shared entity when I'm not the owner?
2566 // A: Only the owner can give ownership to another processor
2567 // 2. Can I delete a shared and not-locally owned entity just on one processor?
2568 // 3. Can I delete a shared and locally owned entity just on one processor?
2569 // 4. Can I change parts on a shared entity differently on different sharing processors and will modification_end figure out the right parts?
2570 // A: Only the owner can change parts on the entity.
2571 // 5. Can I add relations to shared entities differently on different sharing processors?
2572 // 6. Is there a difference in any of these between "creation" and "modification" cycles of the modification?
2573 // For example, can I create shared entities with different parts and it works? vs. Modifying shared entities to produce different parts?
2574 TEST(DocTestBulkData, onlyTheOwnerCanChangeEntityParts)
2575 {
2576 stk::ParallelMachine communicator = MPI_COMM_WORLD;
2577 int numProcs = stk::parallel_machine_size(communicator);
2578 if(numProcs != 2)
2579 {
2580 return;
2581 }
2582
2583 stk::io::StkMeshIoBroker stkMeshIoBroker(communicator);
2584 const std::string generatedMeshSpecification = "generated:1x1x2";
2585 stkMeshIoBroker.add_mesh_database(generatedMeshSpecification, stk::io::READ_MESH);
2586 stkMeshIoBroker.create_input_mesh();
2587 stk::mesh::MetaData &stkMeshMetaData = stkMeshIoBroker.meta_data();
2588 stk::mesh::Part & myPart = stkMeshMetaData.declare_part("new_part");
2589 stkMeshIoBroker.populate_bulk_data();
2590 stk::mesh::BulkData &stkMeshBulkData = stkMeshIoBroker.bulk_data();
2591 stk::mesh::Selector allSharedSelector = stkMeshMetaData.globally_shared_part();
2592 std::vector<stk::mesh::Entity> allSharedNodes;
2593 stk::mesh::get_selected_entities(allSharedSelector, stkMeshBulkData.buckets(stk::topology::NODE_RANK), allSharedNodes);
2594 stkMeshBulkData.modification_begin();
2595 stk::mesh::PartVector addParts;
2596 addParts.push_back(&myPart);
2597 const int myRank = stk::parallel_machine_rank(communicator);
2598 for(size_t i = 0; i < allSharedNodes.size(); ++i)
2599 {
2600 if(stkMeshBulkData.parallel_owner_rank(allSharedNodes[i]) == myRank)
2601 {
2602 stkMeshBulkData.change_entity_parts(allSharedNodes[i], addParts);
2603 }
2604 else
2605 {
2606 EXPECT_THROW(stkMeshBulkData.change_entity_parts(allSharedNodes[i],addParts), std::logic_error);
2607 }
2608 }EXPECT_NO_THROW( stkMeshBulkData.modification_end());
2609
2610 // Verify parts are correct on all processors.
2611 stk::mesh::Selector shared_selector = stkMeshMetaData.globally_shared_part();
2612 std::vector<stk::mesh::Entity> shared_nodes;
2613 stk::mesh::get_selected_entities(shared_selector, stkMeshBulkData.buckets(stk::topology::NODE_RANK), shared_nodes);
2614 for(size_t i = 0; i < shared_nodes.size(); ++i)
2615 {
2616 EXPECT_TRUE(stkMeshBulkData.bucket(shared_nodes[i]).member(myPart));
2617 }
2618 }
2619
2620 TEST(BulkData, onlyKeepTheOwnersParts)
2621 {
2622 stk::ParallelMachine communicator = MPI_COMM_WORLD;
2623 int numProcs = stk::parallel_machine_size(communicator);
2624 if(numProcs != 2)
2625 {
2626 return;
2627 }
2628
2629 stk::io::StkMeshIoBroker stkMeshIoBroker(communicator);
2630 const std::string generatedMeshSpecification = "generated:1x1x2";
2631 stkMeshIoBroker.add_mesh_database(generatedMeshSpecification, stk::io::READ_MESH);
2632 stkMeshIoBroker.create_input_mesh();
2633 stk::mesh::MetaData &stkMeshMetaData = stkMeshIoBroker.meta_data();
2634 const int myRank = stk::parallel_machine_rank(communicator);
2635 stk::mesh::Part & partA = stkMeshMetaData.declare_part("PartA", stk::topology::NODE_RANK);
2636 stk::mesh::Part & partB = stkMeshMetaData.declare_part("PartB", stk::topology::NODE_RANK);
2637 stkMeshIoBroker.populate_bulk_data();
2638 stk::mesh::BulkData &stkMeshBulkData = stkMeshIoBroker.bulk_data();
2639
2640 stk::topology elem_topo = (stkMeshMetaData.spatial_dimension() == 2 ? stk::topology::QUAD_4_2D : stk::topology::HEX_8);
2641 stk::mesh::Part &elem_part = stkMeshMetaData.get_topology_root_part(elem_topo);
2642 stk::mesh::PartVector elem_parts;
2643 elem_parts.push_back(&elem_part);
2644
2645 stk::mesh::EntityKey node0Key;
2646 const stk::mesh::EntityId node_id = 2000;
2647 stkMeshBulkData.modification_begin();
2648 const stk::mesh::EntityId element_id_0 = 1000;
2649 const stk::mesh::EntityId element_id_1 = 1001;
2650 if(myRank == 0)
2651 {
2652 stk::mesh::Entity element0 = stkMeshBulkData.declare_element(element_id_0, elem_parts);
2653 stk::mesh::Entity node0 = stkMeshBulkData.declare_node(node_id, stk::mesh::ConstPartVector{&partA});
2654 int sharingProc = 1;
2655 stkMeshBulkData.add_node_sharing(node0, sharingProc);
2656 node0Key = stkMeshBulkData.entity_key(node0);
2657 for(stk::mesh::RelationIdentifier node_rel_id = 0; node_rel_id < elem_topo.num_nodes(); ++node_rel_id)
2658 {
2659 stkMeshBulkData.declare_relation(element0, node0, node_rel_id);
2660 }
2661 }
2662 else // myRank == 1
2663 {
2664 stk::mesh::Entity element1 = stkMeshBulkData.declare_element(element_id_1, elem_parts);
2665 stk::mesh::Entity node0 = stkMeshBulkData.declare_node(node_id, stk::mesh::ConstPartVector{&partB});
2666 int sharingProc = 0;
2667 stkMeshBulkData.add_node_sharing(node0, sharingProc);
2668 node0Key = stkMeshBulkData.entity_key(node0);
2669 for(stk::mesh::RelationIdentifier node_rel_id = 0; node_rel_id < elem_topo.num_nodes(); ++node_rel_id)
2670 {
2671 stkMeshBulkData.declare_relation(element1, node0, node_rel_id);
2672 }
2673 }
2674 stkMeshBulkData.modification_end();
2675
2676 stk::mesh::Entity node0 = stkMeshBulkData.get_entity(node0Key);
2677 stk::mesh::Bucket & nodeBucket = stkMeshBulkData.bucket(node0);
2678
2679 const int nodeOwner = 0;
2680 EXPECT_EQ( nodeOwner, stkMeshBulkData.parallel_owner_rank(node0));
2681 EXPECT_TRUE( nodeBucket.member(stkMeshMetaData.globally_shared_part()));
2682
2683 EXPECT_TRUE( nodeBucket.member(partA));
2684 EXPECT_TRUE( nodeBucket.member(partB));
2685 EXPECT_TRUE( stk::mesh::has_superset(nodeBucket,partA));
2686 EXPECT_TRUE( stk::mesh::has_superset(nodeBucket,partB));
2687
2688 stk::mesh::Entity element0 = stkMeshBulkData.get_entity(stk::topology::ELEMENT_RANK, element_id_0);
2689 stk::mesh::Entity element1 = stkMeshBulkData.get_entity(stk::topology::ELEMENT_RANK, element_id_1);
2690 const int element0Owner = 0;
2691 const int element1Owner = 1;
2692 EXPECT_EQ( element0Owner, stkMeshBulkData.parallel_owner_rank(element0));
2693 EXPECT_EQ( element1Owner, stkMeshBulkData.parallel_owner_rank(element1));
2694 }
2695
2696 TEST(BulkData, newSharedNodeGetMergedPartsFromElements)
2697 {
2698 stk::ParallelMachine communicator = MPI_COMM_WORLD;
2699 int numProcs = stk::parallel_machine_size(communicator);
2700 if(numProcs != 2)
2701 {
2702 return;
2703 }
2704
2705 stk::io::StkMeshIoBroker stkMeshIoBroker(communicator);
2706 const std::string generatedMeshSpecification = "generated:1x1x2";
2707 stkMeshIoBroker.add_mesh_database(generatedMeshSpecification, stk::io::READ_MESH);
2708 stkMeshIoBroker.create_input_mesh();
2709 stk::mesh::MetaData &stkMeshMetaData = stkMeshIoBroker.meta_data();
2710
2711 stk::topology elem_topo = (stkMeshMetaData.spatial_dimension() == 2 ? stk::topology::QUAD_4_2D : stk::topology::HEX_8);
2712 stk::mesh::Part &elem_part = stkMeshMetaData.get_topology_root_part(elem_topo);
2713 stk::mesh::PartVector elem_parts, empty_parts;
2714 elem_parts.push_back(&elem_part);
2715
2716 const int myRank = stk::parallel_machine_rank(communicator);
2717 stk::mesh::Part & partA = stkMeshMetaData.declare_part("PartA", stk::topology::ELEMENT_RANK);
2718 stk::mesh::Part & partB = stkMeshMetaData.declare_part("PartB", stk::topology::ELEMENT_RANK);
2719 stkMeshIoBroker.populate_bulk_data();
2720 stk::mesh::BulkData &stkMeshBulkData = stkMeshIoBroker.bulk_data();
2721
2722 stk::mesh::EntityKey node0Key;
2723 const stk::mesh::EntityId node_id = 2000;
2724 stkMeshBulkData.modification_begin();
2725 if(myRank == 0)
2726 {
2727 const stk::mesh::EntityId element_id = 1000;
2728 stk::mesh::Entity element0 = stkMeshBulkData.declare_element(element_id, stk::mesh::ConstPartVector{&partA});
2729 stk::mesh::Entity node0 = stkMeshBulkData.declare_node(node_id);
2730 int sharingProc = 1;
2731 stkMeshBulkData.add_node_sharing(node0, sharingProc);
2732 node0Key = stkMeshBulkData.entity_key(node0);
2733 stkMeshBulkData.change_entity_parts(element0, elem_parts, empty_parts);
2734 for(stk::mesh::RelationIdentifier node_rel_id = 0; node_rel_id < elem_topo.num_nodes(); ++node_rel_id)
2735 {
2736 stkMeshBulkData.declare_relation(element0, node0, node_rel_id);
2737 }
2738 }
2739 else // myRank == 1
2740 {
2741 const stk::mesh::EntityId element_id = 1001;
2742 stk::mesh::Entity element1 = stkMeshBulkData.declare_element(element_id, stk::mesh::ConstPartVector{&partB});
2743 stk::mesh::Entity node0 = stkMeshBulkData.declare_node(node_id);
2744 int sharingProc = 0;
2745 stkMeshBulkData.add_node_sharing(node0, sharingProc);
2746 node0Key = stkMeshBulkData.entity_key(node0);
2747 stkMeshBulkData.change_entity_parts(element1, elem_parts, empty_parts);
2748 for(stk::mesh::RelationIdentifier node_rel_id = 0; node_rel_id < elem_topo.num_nodes(); ++node_rel_id)
2749 {
2750 stkMeshBulkData.declare_relation(element1, node0, node_rel_id);
2751 }
2752 }
2753 stkMeshBulkData.modification_end();
2754
2755 stk::mesh::Entity node0 = stkMeshBulkData.get_entity(node0Key);
2756 stk::mesh::Bucket & node0Bucket = stkMeshBulkData.bucket(node0);
2757
2758 const int node0Owner = 0;
2759 EXPECT_EQ( node0Owner, stkMeshBulkData.parallel_owner_rank(node0));
2760 EXPECT_TRUE( node0Bucket.member(stkMeshMetaData.globally_shared_part()));
2761
2762 EXPECT_TRUE( node0Bucket.member(partA));
2763 EXPECT_TRUE( node0Bucket.member(partB));
2764 EXPECT_TRUE( stk::mesh::has_superset(node0Bucket,partA));
2765 EXPECT_TRUE( stk::mesh::has_superset(node0Bucket,partB));
2766 }
2767
2768 TEST(BulkData, mayCreateRelationsToNodesDifferently)
2769 {
2770 stk::ParallelMachine communicator = MPI_COMM_WORLD;
2771 const int numProcs = stk::parallel_machine_size(communicator);
2772 if(numProcs != 2) { GTEST_SKIP(); }
2773
2774 stk::io::StkMeshIoBroker stkMeshIoBroker(communicator);
2775 const std::string generatedMeshSpecification = "generated:1x1x2";
2776 stkMeshIoBroker.add_mesh_database(generatedMeshSpecification, stk::io::READ_MESH);
2777 stkMeshIoBroker.create_input_mesh();
2778 stk::mesh::MetaData &stkMeshMetaData = stkMeshIoBroker.meta_data();
2779 const int myRank = stk::parallel_machine_rank(communicator);
2780 stk::mesh::Part & partA = stkMeshMetaData.declare_part("PartA", stk::topology::ELEMENT_RANK);
2781 stk::mesh::Part & partB = stkMeshMetaData.declare_part("PartB", stk::topology::ELEMENT_RANK);
2782 stkMeshIoBroker.populate_bulk_data();
2783 stk::mesh::BulkData &stkMeshBulkData = stkMeshIoBroker.bulk_data();
2784
2785 stk::topology elem_topo = (stkMeshMetaData.spatial_dimension() == 2 ? stk::topology::QUAD_4_2D : stk::topology::HEX_8);
2786 stk::mesh::Part &elem_part = stkMeshMetaData.get_topology_root_part(elem_topo);
2787 stk::mesh::PartVector elem_parts, empty_parts;
2788 elem_parts.push_back(&elem_part);
2789
2790 stk::mesh::Selector allSharedSelector = stkMeshMetaData.globally_shared_part();
2791 std::vector<stk::mesh::Entity> allSharedNodes;
2792 stk::mesh::get_selected_entities(allSharedSelector, stkMeshBulkData.buckets(stk::topology::NODE_RANK), allSharedNodes);
2793
2794 std::sort(allSharedNodes.begin(), allSharedNodes.end(), stk::mesh::EntityLess(stkMeshBulkData));
2795 stk::mesh::Entity sharedNode0 = allSharedNodes[0];
2796 stk::mesh::Entity sharedNode1 = allSharedNodes[1];
2797
2798 stkMeshBulkData.modification_begin();
2799 const stk::mesh::EntityId element_id_0 = 1000;
2800 const stk::mesh::EntityId element_id_1 = 1001;
2801 stk::mesh::Entity filler_node = stkMeshBulkData.declare_node(123456);
2802 if(myRank == 0)
2803 {
2804 int sharingProc = 1;
2805 stkMeshBulkData.add_node_sharing(filler_node, sharingProc);
2806 stk::mesh::Entity element0 = stkMeshBulkData.declare_element(element_id_0, stk::mesh::ConstPartVector{&partA});
2807 stkMeshBulkData.change_entity_parts(element0, elem_parts, empty_parts);
2808 stk::mesh::RelationIdentifier node_rel_id = 0;
2809 stkMeshBulkData.declare_relation(element0, sharedNode0, node_rel_id);
2810 for(stk::mesh::RelationIdentifier filler_rel_id = node_rel_id + 1; filler_rel_id < elem_topo.num_nodes(); ++filler_rel_id)
2811 {
2812 stkMeshBulkData.declare_relation(element0, filler_node, filler_rel_id);
2813 }
2814 }
2815 else // myRank == 1
2816 {
2817 int sharingProc = 0;
2818 stkMeshBulkData.add_node_sharing(filler_node, sharingProc);
2819 stk::mesh::Entity element1 = stkMeshBulkData.declare_element(element_id_1, stk::mesh::ConstPartVector{&partB});
2820 stkMeshBulkData.change_entity_parts(element1, elem_parts, empty_parts);
2821 const stk::mesh::RelationIdentifier node_rel_id = 0;
2822 stkMeshBulkData.declare_relation(element1, sharedNode0, node_rel_id);
2823 for(stk::mesh::RelationIdentifier filler_rel_id = node_rel_id + 1; filler_rel_id < elem_topo.num_nodes(); ++filler_rel_id)
2824 {
2825 stkMeshBulkData.declare_relation(element1, filler_node, filler_rel_id);
2826 }
2827 }
2828 EXPECT_NO_THROW( stkMeshBulkData.modification_end());
2829
2830 {
2831 stk::mesh::Bucket & nodeBucket = stkMeshBulkData.bucket(sharedNode0);
2832 EXPECT_TRUE( nodeBucket.member(partA));
2833 EXPECT_TRUE( nodeBucket.member(partB));
2834 EXPECT_EQ( 4u, stkMeshBulkData.num_elements(sharedNode0));
2835 }
2836 stk::mesh::Entity element0 = stkMeshBulkData.get_entity(stk::topology::ELEMENT_RANK, element_id_0);
2837 stk::mesh::Entity element1 = stkMeshBulkData.get_entity(stk::topology::ELEMENT_RANK, element_id_1);
2838 stkMeshBulkData.modification_begin();
2839 if(myRank == 0)
2840 {
2841 stk::mesh::RelationIdentifier node_rel_id = 1;
2842 stkMeshBulkData.declare_relation(element0, sharedNode1, node_rel_id);
2843 }
2844 else // myRank == 1
2845 {
2846 stk::mesh::RelationIdentifier node_rel_id = 1;
2847 stkMeshBulkData.declare_relation(element1, sharedNode1, node_rel_id);
2848 }
2849 stkMeshBulkData.modification_end();
2850
2851 {
2852 stk::mesh::Bucket & nodeBucket = stkMeshBulkData.bucket(sharedNode1);
2853 EXPECT_TRUE( nodeBucket.member(partA));
2854 EXPECT_TRUE( nodeBucket.member(partB));
2855 EXPECT_EQ( 4u, stkMeshBulkData.num_elements(sharedNode1));
2856 }
2857 }
2858
2859 stk::mesh::PartVector setupFixture(stk::io::StkMeshIoBroker & io)
2860 {
2861 const std::string generatedMeshSpecification = "generated:1x1x2";
2862 io.add_mesh_database(generatedMeshSpecification, stk::io::READ_MESH);
2863 io.create_input_mesh();
2864 stk::mesh::Part & partA = io.meta_data().declare_part("PartA", stk::topology::ELEMENT_RANK);
2865 stk::mesh::Part & partB = io.meta_data().declare_part("PartB", stk::topology::ELEMENT_RANK);
2866 io.populate_bulk_data();
2867 stk::mesh::PartVector pv;
2868 pv.push_back(&partA);
2869 pv.push_back(&partB);
2870 return pv;
2871 }
2872
2873 stk::mesh::EntityVector getSortedNodes(stk::mesh::BulkData& bulk, stk::mesh::Selector selector)
2874 {
2875 stk::mesh::EntityVector nodes;
2876 stk::mesh::get_selected_entities(selector, bulk.buckets(stk::topology::NODE_RANK), nodes);
2877 std::sort(nodes.begin(), nodes.end(), stk::mesh::EntityLess(bulk));
2878 return nodes;
2879 }
2880
2881 bool no_aura_declare_relation(stk::mesh::BulkData &bulk,
2882 stk::mesh::Entity e_from,
2883 stk::mesh::Entity e_to,
2884 const stk::mesh::RelationIdentifier local_id)
2885 {
2886 stk::mesh::Bucket const *b_from = bulk.bucket_ptr(e_from);
2887 stk::mesh::Bucket const *b_to = bulk.bucket_ptr(e_to);
2888 const bool ownership_ok = (b_from && b_to && !b_from->in_aura() && b_to->in_aura());
2889
2890 if(ownership_ok)
2891 {
2892 bulk.declare_relation(e_from, e_to, local_id);
2893 return true;
2894 }
2895 else
2896 {
2897 return false;
2898 }
2899 }
2900
2901 TEST(DocTestBulkData, inducedPartMembershipIgnoredForNonOwnedHigherRankedEntities)
2902 {
2903 stk::ParallelMachine communicator = MPI_COMM_WORLD;
2904 int numProcs = stk::parallel_machine_size(communicator);
2905 if(numProcs != 2)
2906 {
2907 return;
2908 }
2909 const int myRank = stk::parallel_machine_rank(communicator);
2910
2911 stk::io::StkMeshIoBroker stkMeshIoBroker(communicator);
2912 stk::mesh::PartVector pv = setupFixture(stkMeshIoBroker);
2913 stk::mesh::Part & partA = *pv[0];
2914 stk::mesh::Part & partB = *pv[1];
2915
2916 stk::mesh::MetaData &meta = stkMeshIoBroker.meta_data();
2917 stk::mesh::BulkData &bulk = stkMeshIoBroker.bulk_data();
2918
2919 stk::topology elem_topo = (meta.spatial_dimension() == 2 ? stk::topology::QUAD_4_2D : stk::topology::HEX_8);
2920 stk::mesh::Part &elem_part = meta.get_topology_root_part(elem_topo);
2921 stk::mesh::PartVector elem_parts, empty_parts;
2922 elem_parts.push_back(&elem_part);
2923
2924 stk::mesh::EntityVector ev = getSortedNodes(bulk, stk::mesh::Selector(meta.globally_shared_part()));
2925 ASSERT_TRUE( ev.size() >= 2);
2926 stk::mesh::Entity sharedNodeA(ev[0]);
2927 stk::mesh::Entity sharedNodeB(ev[1]);
2928
2929 stk::mesh::Bucket & nodeABucket = bulk.bucket(sharedNodeA);
2930 EXPECT_FALSE( nodeABucket.member(partA));
2931 EXPECT_FALSE( nodeABucket.member(partB));
2932 EXPECT_EQ( 2u, bulk.num_elements(sharedNodeA));
2933 EXPECT_EQ( 2u, bulk.num_elements(sharedNodeB));
2934
2935 // First we create off processor elements
2936 bulk.modification_begin();
2937 const stk::mesh::EntityId element_id_0 = 1000;
2938 const stk::mesh::EntityId element_id_1 = 1001;
2939
2940 const stk::mesh::EntityId filler_node_id = 123456;
2941 stk::mesh::Entity filler_node = bulk.declare_node(filler_node_id);
2942
2943 bulk.add_node_sharing(filler_node, (myRank == 1 ? 0 : 1));
2944
2945 if(myRank == 0)
2946 {
2947 const stk::mesh::RelationIdentifier node_rel_id = 0;
2948 stk::mesh::Entity element = bulk.declare_element(element_id_0, stk::mesh::ConstPartVector{&partA});
2949 bulk.change_entity_parts(element, elem_parts, empty_parts);
2950 bulk.declare_relation(element, sharedNodeA, node_rel_id);
2951 for(stk::mesh::RelationIdentifier filler_rel_id = node_rel_id + 1; filler_rel_id < elem_topo.num_nodes(); ++filler_rel_id)
2952 {
2953 bulk.declare_relation(element, filler_node, filler_rel_id);
2954 }
2955 }
2956 else // myRank == 1
2957 {
2958 const stk::mesh::RelationIdentifier node_rel_id = 0;
2959 stk::mesh::Entity element = bulk.declare_element(element_id_1, stk::mesh::ConstPartVector{&partB});
2960 bulk.change_entity_parts(element, elem_parts, empty_parts);
2961 bulk.declare_relation(element, sharedNodeA, node_rel_id);
2962 for(stk::mesh::RelationIdentifier filler_rel_id = node_rel_id + 1; filler_rel_id < elem_topo.num_nodes(); ++filler_rel_id)
2963 {
2964 bulk.declare_relation(element, filler_node, filler_rel_id);
2965 }
2966 }
2967
2968 EXPECT_NO_THROW( bulk.modification_end());
2969 {
2970 stk::mesh::Bucket & nodeABkt = bulk.bucket(sharedNodeA);
2971 EXPECT_TRUE( nodeABkt.member(partA));
2972 EXPECT_TRUE( nodeABkt.member(partB));
2973 EXPECT_EQ( 4u, bulk.num_elements(sharedNodeA));
2974 }
2975
2976 stk::mesh::Entity element0 = bulk.get_entity(stk::topology::ELEMENT_RANK, element_id_0);
2977 stk::mesh::Entity element1 = bulk.get_entity(stk::topology::ELEMENT_RANK, element_id_1);
2978
2979 stk::mesh::Part &locally_owned_part = meta.locally_owned_part();
2980 stk::mesh::Bucket &element0_bucket = bulk.bucket(element0);
2981 stk::mesh::Bucket &element1_bucket = bulk.bucket(element1);
2982
2983 if(myRank == 0)
2984 {
2985 EXPECT_TRUE(element0_bucket.member(locally_owned_part));
2986 EXPECT_FALSE(element1_bucket.member(locally_owned_part));
2987 }
2988 else // myRank == 1
2989 {
2990 EXPECT_FALSE(element0_bucket.member(locally_owned_part));
2991 EXPECT_TRUE(element1_bucket.member(locally_owned_part));
2992 }
2993
2994 // Disallowing declaring relations to/from an entity in the aura can make it
2995 // easier to keep the mesh consistent.
2996 bulk.modification_begin();
2997 if(myRank == 0)
2998 {
2999 const stk::mesh::RelationIdentifier node_rel_id = 1;
3000 EXPECT_FALSE(no_aura_declare_relation(bulk, element1, sharedNodeB, node_rel_id));
3001 }
3002 else // myRank == 1
3003 {
3004 const stk::mesh::RelationIdentifier node_rel_id = 1;
3005 EXPECT_FALSE(no_aura_declare_relation(bulk, element0, sharedNodeB, node_rel_id));
3006 }
3007
3008 EXPECT_NO_THROW( bulk.modification_end());
3009
3010 {
3011 stk::mesh::Bucket & nodeBBucket = bulk.bucket(sharedNodeB);
3012 EXPECT_FALSE( nodeBBucket.member(partA));
3013 EXPECT_FALSE( nodeBBucket.member(partB));
3014 EXPECT_EQ( 2u, bulk.num_elements(sharedNodeB));
3015
3016 const int element0Owner = 0;
3017 EXPECT_EQ( element0Owner, bulk.parallel_owner_rank(element0));
3018 const int element1Owner = 1;
3019 EXPECT_EQ( element1Owner, bulk.parallel_owner_rank(element1));
3020
3021 if(myRank == 0)
3022 {
3023 EXPECT_TRUE( sharedNodeA == bulk.begin_nodes(element0)[0]);
3024
3025 EXPECT_TRUE( sharedNodeA == bulk.begin_nodes(element1)[0]);
3026 EXPECT_TRUE( filler_node == bulk.begin_nodes(element1)[1]);
3027 }
3028 else // myRank == 1
3029 {
3030 EXPECT_TRUE( sharedNodeA == bulk.begin_nodes(element0)[0]);
3031 EXPECT_TRUE( filler_node == bulk.begin_nodes(element0)[1]);
3032
3033 EXPECT_TRUE( sharedNodeA == bulk.begin_nodes(element1)[0]);
3034 }
3035 }
3036
3037 // Again we try to create relations from ghost element to shared node B.
3038 // BulkData::declare_relation(..) will do this, but the owner relations
3039 // will reset the ghost's relations in modification_end().
3040 //
3041 // Thus, more work needs to be done to enforce the relationship reciprocity
3042 // aspect of mesh consistency.
3043 bulk.modification_begin();
3044 if(myRank == 0)
3045 {
3046 const stk::mesh::RelationIdentifier node_rel_id = 1;
3047 bulk.declare_relation(element1, sharedNodeB, node_rel_id);
3048 EXPECT_TRUE( sharedNodeB == bulk.begin_nodes(element1)[1]);
3049 }
3050 else // myRank == 1
3051 {
3052 const stk::mesh::RelationIdentifier node_rel_id = 1;
3053 bulk.declare_relation(element0, sharedNodeB, node_rel_id);
3054 EXPECT_TRUE( sharedNodeB == bulk.begin_nodes(element0)[1]);
3055 }
3056
3057 {
3058 stk::parallel_machine_barrier(bulk.parallel());
3059 std::ostringstream os;
3060 os<<"P"<<bulk.parallel_rank()<<" about to call mod-end"<<std::endl;
3061 std::cerr<<os.str();
3062 stk::parallel_machine_barrier(bulk.parallel());
3063 }
3064 bulk.modification_end();
3065
3066 {
3067 stk::mesh::Bucket & nodeBBucket = bulk.bucket(sharedNodeB);
3068 EXPECT_FALSE( nodeBBucket.member(partA));
3069 EXPECT_FALSE( nodeBBucket.member(partB));
3070 EXPECT_EQ( 3u, bulk.num_elements(sharedNodeB));
3071
3072 if(myRank == 0)
3073 {
3074 EXPECT_TRUE( sharedNodeA == bulk.begin_nodes(element0)[0]);
3075
3076 EXPECT_TRUE( sharedNodeA == bulk.begin_nodes(element1)[0]);
3077 EXPECT_TRUE( filler_node == bulk.begin_nodes(element1)[1]);
3078 }
3079 else // myRank == 1
3080 {
3081 EXPECT_TRUE( sharedNodeA == bulk.begin_nodes(element0)[0]);
3082 EXPECT_TRUE( filler_node == bulk.begin_nodes(element0)[1]);
3083
3084 EXPECT_TRUE( sharedNodeA == bulk.begin_nodes(element1)[0]);
3085 }
3086 }
3087 }
3088
3089 TEST(BulkData, ModificationEnd)
3090 {
3091 MPI_Comm communicator = MPI_COMM_WORLD;
3092 int numProcs = stk::parallel_machine_size(communicator);
3093
3094 if(numProcs == 2)
3095 {
3096 const int spatialDim = 3;
3097 stk::mesh::MetaData stkMeshMetaData(spatialDim);
3098 stk::unit_test_util::BulkDataTester *stkMeshBulkData = new stk::unit_test_util::BulkDataTester(stkMeshMetaData, communicator);
3099
3100 std::string exodusFileName = stk::unit_test_util::get_option("-i", "generated:1x1x4");
3101
3102 // STK IO module will be described in separate chapter.
3103 // It is used here to read the mesh data from the Exodus file and populate an STK Mesh.
3104 // The order of the following lines in {} are important
3105 {
3106 stk::io::StkMeshIoBroker exodusFileReader(communicator);
3107
3108 // Inform STK IO which STK Mesh objects to populate later
3109 exodusFileReader.set_bulk_data(*stkMeshBulkData);
3110
3111 exodusFileReader.add_mesh_database(exodusFileName, stk::io::READ_MESH);
3112
3113 // Populate the MetaData which has the descriptions of the Parts and Fields.
3114 exodusFileReader.create_input_mesh();
3115
3116 // Populate entities in STK Mesh from Exodus file
3117 exodusFileReader.populate_bulk_data();
3118 }
3119
3120 stk::mesh::EntityId elementToDestroy = 3;
3121 stk::mesh::EntityId nodeToCheck = 9;
3122
3123 stk::mesh::EntityKey nodeEntityKey(stk::topology::NODE_RANK, nodeToCheck);
3124 stk::mesh::EntityKey entityToDestroyKey(stk::topology::ELEMENT_RANK, elementToDestroy);
3125
3126 stk::mesh::EntityCommListInfoVector::const_iterator iter = std::lower_bound(stkMeshBulkData->my_internal_comm_list().begin(),
3127 stkMeshBulkData->my_internal_comm_list().end(),
3128 nodeEntityKey);
3129
3130 ASSERT_TRUE(iter != stkMeshBulkData->my_internal_comm_list().end());
3131 EXPECT_EQ(nodeEntityKey, iter->key);
3132 EXPECT_TRUE(stkMeshBulkData->is_valid(iter->entity));
3133
3134 stkMeshBulkData->modification_begin();
3135
3136 ASSERT_TRUE( stkMeshBulkData->is_valid(stkMeshBulkData->get_entity(entityToDestroyKey)));
3137
3138 if(stkMeshBulkData->parallel_rank() == 1)
3139 {
3140 stkMeshBulkData->destroy_entity(stkMeshBulkData->get_entity(entityToDestroyKey));
3141 }
3142
3143 stkMeshBulkData->modification_end();
3144
3145 stk::mesh::Entity nodeEntity = stkMeshBulkData->get_entity(nodeEntityKey);
3146 if (stkMeshBulkData->parallel_rank() == 0) {
3147 EXPECT_TRUE(stkMeshBulkData->is_valid(nodeEntity));
3148 EXPECT_FALSE(stkMeshBulkData->in_shared(nodeEntity));
3149 }
3150 else {
3151 EXPECT_FALSE(stkMeshBulkData->is_valid(nodeEntity));
3152 }
3153
3154 std::vector<size_t> globalCounts;
3155 stk::mesh::comm_mesh_counts(*stkMeshBulkData, globalCounts);
3156
3157 delete stkMeshBulkData;
3158 }
3159 }
3160
3161 TEST(BulkData, resolve_ownership_of_modified_entities_trivial)
3162 {
3163 MPI_Comm communicator = MPI_COMM_WORLD;
3164 int numProcs = stk::parallel_machine_size(communicator);
3165 const int myRank = stk::parallel_machine_rank(communicator);
3166
3167 if(numProcs != 3)
3168 {
3169 return;
3170 }
3171
3172 const int spatialDim = 3;
3173 stk::mesh::MetaData stkMeshMetaData(spatialDim);
3174 stk::unit_test_util::BulkDataTester mesh(stkMeshMetaData, communicator);
3175 std::string exodusFileName = stk::unit_test_util::get_option("-i", "generated:1x1x3");
3176 {
3177 stk::io::StkMeshIoBroker exodusFileReader(communicator);
3178 exodusFileReader.set_bulk_data(mesh);
3179 exodusFileReader.add_mesh_database(exodusFileName, stk::io::READ_MESH);
3180 exodusFileReader.create_input_mesh();
3181 exodusFileReader.populate_bulk_data();
3182 }
3183 std::vector<Entity> modified_entities;
3184 if (myRank == 0) {
3185 modified_entities.push_back(mesh.get_entity(stk::topology::NODE_RANK, 1));
3186 }
3187 else if (myRank == 1) {
3188 modified_entities.push_back(mesh.get_entity(stk::topology::NODE_RANK, 9));
3189 }
3190 else {
3191 modified_entities.push_back(mesh.get_entity(stk::topology::NODE_RANK, 13));
3192 }
3193 mesh.my_resolve_ownership_of_modified_entities(modified_entities);
3194 if (myRank == 0) {
3195 EXPECT_TRUE(check_state(mesh, EntityKey(stk::topology::NODE_RANK, 1), CEOUtils::STATE_OWNED, 0));
3196 EXPECT_TRUE(check_state(mesh, EntityKey(stk::topology::NODE_RANK, 9), CEOUtils::STATE_OWNED, 1));
3197 }
3198 else if (myRank == 1) {
3199 EXPECT_TRUE(check_state(mesh, EntityKey(stk::topology::NODE_RANK, 1), CEOUtils::STATE_OWNED, 0));
3200 EXPECT_TRUE(check_state(mesh, EntityKey(stk::topology::NODE_RANK, 9), CEOUtils::STATE_OWNED, 1));
3201 EXPECT_TRUE(check_state(mesh, EntityKey(stk::topology::NODE_RANK, 13), CEOUtils::STATE_OWNED, 2));
3202 }
3203 else {
3204 EXPECT_TRUE(check_state(mesh, EntityKey(stk::topology::NODE_RANK, 9), CEOUtils::STATE_OWNED, 1));
3205 EXPECT_TRUE(check_state(mesh, EntityKey(stk::topology::NODE_RANK, 13), CEOUtils::STATE_OWNED, 2));
3206 }
3207 }
3208
3209 TEST(BulkData, verify_closure_count_is_correct)
3210 {
3211 MPI_Comm communicator = MPI_COMM_WORLD;
3212 int numProcs = stk::parallel_machine_size(communicator);
3213 const int myRank = stk::parallel_machine_rank(communicator);
3214
3215 if(numProcs == 2)
3216 {
3217 const int spatialDim = 3;
3218 stk::mesh::MetaData stkMeshMetaData(spatialDim);
3219 stk::unit_test_util::BulkDataTester *stkMeshBulkData = new stk::unit_test_util::BulkDataTester(stkMeshMetaData, communicator);
3220
3221 std::string exodusFileName = stk::unit_test_util::get_option("-i", "generated:1x1x2");
3222
3223 // STK IO module will be described in separate chapter.
3224 // It is used here to read the mesh data from the Exodus file and populate an STK Mesh.
3225 // The order of the following lines in {} are important
3226 {
3227 stk::io::StkMeshIoBroker exodusFileReader(communicator);
3228
3229 // Inform STK IO which STK Mesh objects to populate later
3230 exodusFileReader.set_bulk_data(*stkMeshBulkData);
3231
3232 exodusFileReader.add_mesh_database(exodusFileName, stk::io::READ_MESH);
3233
3234 // Populate the MetaData which has the descriptions of the Parts and Fields.
3235 exodusFileReader.create_input_mesh();
3236
3237 // Populate entities in STK Mesh from Exodus file
3238 exodusFileReader.populate_bulk_data();
3239 }
3240
3241 stk::mesh::EntityKey element_1_key(stk::topology::ELEMENT_RANK, 1);
3242 stk::mesh::EntityKey element_2_key(stk::topology::ELEMENT_RANK, 2);
3243
3244 stk::mesh::EntityKey node_1_key(stk::topology::NODE_RANK, 1);
3245 stk::mesh::EntityKey node_2_key(stk::topology::NODE_RANK, 2);
3246 stk::mesh::EntityKey node_3_key(stk::topology::NODE_RANK, 3);
3247 stk::mesh::EntityKey node_4_key(stk::topology::NODE_RANK, 4);
3248 stk::mesh::EntityKey node_5_key(stk::topology::NODE_RANK, 5);
3249 stk::mesh::EntityKey node_6_key(stk::topology::NODE_RANK, 6);
3250 stk::mesh::EntityKey node_7_key(stk::topology::NODE_RANK, 7);
3251 stk::mesh::EntityKey node_8_key(stk::topology::NODE_RANK, 8);
3252 stk::mesh::EntityKey node_9_key(stk::topology::NODE_RANK, 9);
3253 stk::mesh::EntityKey node_10_key(stk::topology::NODE_RANK, 10);
3254 stk::mesh::EntityKey node_11_key(stk::topology::NODE_RANK, 11);
3255 stk::mesh::EntityKey node_12_key(stk::topology::NODE_RANK, 12);
3256
3257 if(myRank == 0)
3258 {
3259 EXPECT_EQ( 1u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(element_1_key)));
3260 EXPECT_EQ( 0u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(element_2_key)));
3261
3262 EXPECT_EQ( 2u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_1_key)));
3263 EXPECT_EQ( 2u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_2_key)));
3264 EXPECT_EQ( 2u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_3_key)));
3265 EXPECT_EQ( 2u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_4_key)));
3266 EXPECT_EQ( 2u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_5_key)));
3267 EXPECT_EQ( 2u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_6_key)));
3268 EXPECT_EQ( 2u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_7_key)));
3269 EXPECT_EQ( 2u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_8_key)));
3270 EXPECT_EQ( 0u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_9_key)));
3271 EXPECT_EQ( 0u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_10_key)));
3272 EXPECT_EQ( 0u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_11_key)));
3273 EXPECT_EQ( 0u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_12_key)));
3274 }
3275 else // myRank == 1
3276 {
3277 EXPECT_EQ( 0u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(element_1_key)));
3278 EXPECT_EQ( 1u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(element_2_key)));
3279
3280 EXPECT_EQ( 0u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_1_key)));
3281 EXPECT_EQ( 0u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_2_key)));
3282 EXPECT_EQ( 0u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_3_key)));
3283 EXPECT_EQ( 0u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_4_key)));
3284 EXPECT_EQ( 1u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_5_key)));
3285 EXPECT_EQ( 1u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_6_key)));
3286 EXPECT_EQ( 1u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_7_key)));
3287 EXPECT_EQ( 1u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_8_key)));
3288 EXPECT_EQ( 2u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_9_key)));
3289 EXPECT_EQ( 2u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_10_key)));
3290 EXPECT_EQ( 2u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_11_key)));
3291 EXPECT_EQ( 2u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_12_key)));
3292 }
3293
3294 std::vector<EntityProc> change_owner_vector;
3295 if(myRank == 1)
3296 {
3297 const int target_processor = 0;
3298 stk::mesh::Selector my_entities_selector = stkMeshBulkData->mesh_meta_data().locally_owned_part();
3299 std::vector<Entity> nodes;
3300 stk::mesh::get_selected_entities(my_entities_selector, stkMeshBulkData->buckets(stk::topology::NODE_RANK), nodes);
3301 for(size_t i = 0; i < nodes.size(); ++i)
3302 {
3303 change_owner_vector.push_back(EntityProc(nodes[i], target_processor));
3304 }
3305 }
3306 stkMeshBulkData->change_entity_owner(change_owner_vector);
3307
3308 if(myRank == 0)
3309 {
3310 EXPECT_EQ( 1u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(element_1_key)));
3311 EXPECT_EQ( 0u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(element_2_key)));
3312
3313 EXPECT_EQ( 2u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_1_key)));
3314 EXPECT_EQ( 2u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_2_key)));
3315 EXPECT_EQ( 2u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_3_key)));
3316 EXPECT_EQ( 2u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_4_key)));
3317 EXPECT_EQ( 2u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_5_key)));
3318 EXPECT_EQ( 2u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_6_key)));
3319 EXPECT_EQ( 2u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_7_key)));
3320 EXPECT_EQ( 2u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_8_key)));
3321 EXPECT_EQ( 1u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_9_key)));
3322 EXPECT_EQ( 1u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_10_key)));
3323 EXPECT_EQ( 1u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_11_key)));
3324 EXPECT_EQ( 1u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_12_key)));
3325 }
3326 else // myRank == 1
3327 {
3328 EXPECT_EQ( 0u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(element_1_key)));
3329 EXPECT_EQ( 1u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(element_2_key)));
3330
3331 EXPECT_EQ( 0u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_1_key)));
3332 EXPECT_EQ( 0u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_2_key)));
3333 EXPECT_EQ( 0u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_3_key)));
3334 EXPECT_EQ( 0u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_4_key)));
3335 EXPECT_EQ( 1u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_5_key)));
3336 EXPECT_EQ( 1u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_6_key)));
3337 EXPECT_EQ( 1u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_7_key)));
3338 EXPECT_EQ( 1u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_8_key)));
3339 EXPECT_EQ( 1u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_9_key)));
3340 EXPECT_EQ( 1u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_10_key)));
3341 EXPECT_EQ( 1u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_11_key)));
3342 EXPECT_EQ( 1u, stkMeshBulkData->closure_count(stkMeshBulkData->get_entity(node_12_key)));
3343 }
3344
3345 delete stkMeshBulkData;
3346 }
3347
3348 }
3349
3350 TEST(BulkData, orphaned_node_closure_count_shared_nodes_non_owner_adds_element)
3351 {
3352 MPI_Comm communicator = MPI_COMM_WORLD;
3353 int numProcs = stk::parallel_machine_size(communicator);
3354 const int myRank = stk::parallel_machine_rank(communicator);
3355
3356 if (numProcs != 2) { return; }
3357
3358 const int spatial_dimension = 2;
3359 stk::mesh::MetaData meta(spatial_dimension);
3360 stk::unit_test_util::BulkDataTester bulk(meta,communicator);
3361
3362 stk::mesh::Part& element_part = meta.declare_part_with_topology("Beam2Part", stk::topology::BEAM_2);
3363
3364 bulk.modification_begin();
3365 stk::mesh::Entity node1 = bulk.declare_node(1);
3366 stk::mesh::Entity node2 = bulk.declare_node(2);
3367 int other_proc = 1-myRank;
3368 bulk.add_node_sharing(node1,other_proc);
3369 bulk.add_node_sharing(node2,other_proc);
3370 EXPECT_EQ(bulk.my_orphaned_node_marking()+1,bulk.closure_count(node1));
3371 EXPECT_EQ(bulk.my_orphaned_node_marking()+1,bulk.closure_count(node2));
3372
3373 ASSERT_NO_THROW(bulk.modification_end());
3374
3375 if (myRank == 0)
3376 {
3377 EXPECT_EQ(1u,bulk.closure_count(node1));
3378 EXPECT_EQ(1u,bulk.closure_count(node2));
3379 }
3380 else
3381 {
3382 EXPECT_EQ(bulk.my_orphaned_node_marking()+0,bulk.closure_count(node1));
3383 EXPECT_EQ(bulk.my_orphaned_node_marking()+0,bulk.closure_count(node2));
3384 }
3385
3386 bulk.modification_begin();
3387 if (myRank == 1)
3388 {
3389 stk::mesh::Entity element = bulk.declare_element(1, stk::mesh::ConstPartVector{&element_part});
3390 bulk.declare_relation(element,node1,0);
3391 EXPECT_EQ(1u,bulk.closure_count(node1));
3392 bulk.declare_relation(element,node2,1);
3393 EXPECT_EQ(1u,bulk.closure_count(node2));
3394 }
3395 EXPECT_NO_THROW(bulk.modification_end());
3396
3397 EXPECT_EQ(1u,bulk.closure_count(node1));
3398 EXPECT_EQ(1u,bulk.closure_count(node2));
3399
3400 bulk.modification_begin();
3401 if (myRank == 0)
3402 {
3403 std::vector<Entity> element_vector;
3404 stk::mesh::get_entities(bulk, stk::topology::ELEMENT_RANK, element_vector);
3405 ASSERT_EQ(1u, element_vector.size());
3406 EXPECT_TRUE(bulk.destroy_entity(element_vector[0]));
3407 EXPECT_TRUE(bulk.destroy_entity(node1));
3408 EXPECT_TRUE(bulk.destroy_entity(node2));
3409 EXPECT_FALSE(bulk.is_valid(node1));
3410 EXPECT_FALSE(bulk.is_valid(node2));
3411 }
3412 EXPECT_NO_THROW(bulk.modification_end());
3413 if (myRank == 0)
3414 {
3415 EXPECT_FALSE(bulk.is_valid(node1));
3416 EXPECT_FALSE(bulk.is_valid(node2));
3417 }
3418 else // myRank == 1
3419 {
3420 EXPECT_TRUE(bulk.bucket(node1).owned());
3421 EXPECT_TRUE(bulk.bucket(node2).owned());
3422 EXPECT_FALSE(bulk.bucket(node1).shared());
3423 EXPECT_FALSE(bulk.bucket(node2).shared());
3424 }
3425 }
3426
3427 TEST(BulkData, orphaned_node_closure_count_shared_nodes_owner_deletes)
3428 {
3429 MPI_Comm communicator = MPI_COMM_WORLD;
3430 int numProcs = stk::parallel_machine_size(communicator);
3431 const int myRank = stk::parallel_machine_rank(communicator);
3432
3433 if (numProcs != 2) { return; }
3434
3435 const int spatial_dimension = 2;
3436 stk::mesh::MetaData meta(spatial_dimension);
3437 stk::unit_test_util::BulkDataTester bulk(meta,communicator);
3438
3439 bulk.modification_begin();
3440 stk::mesh::Entity node1 = bulk.declare_node(1);
3441 int other_proc = 1-myRank;
3442 bulk.add_node_sharing(node1,other_proc);
3443
3444 bulk.modification_end();
3445
3446 bulk.modification_begin();
3447
3448 if (myRank == 0)
3449 {
3450 EXPECT_TRUE(bulk.destroy_entity(node1));
3451 EXPECT_FALSE(bulk.is_valid(node1));
3452 }
3453 EXPECT_NO_THROW(bulk.modification_end());
3454 if (myRank == 0)
3455 {
3456 EXPECT_FALSE(bulk.is_valid(node1));
3457 }
3458 else // myRank == 1
3459 {
3460 EXPECT_EQ(1u,bulk.closure_count(node1));
3461 EXPECT_TRUE(bulk.bucket(node1).owned());
3462 EXPECT_FALSE(bulk.bucket(node1).shared());
3463 }
3464 }
3465
3466 TEST(BulkData, orphaned_node_closure_count_shared_nodes_change_entity_owner_3proc)
3467 {
3468 MPI_Comm communicator = MPI_COMM_WORLD;
3469 int numProcs = stk::parallel_machine_size(communicator);
3470 const int myRank = stk::parallel_machine_rank(communicator);
3471
3472 if (numProcs != 3) { return; }
3473
3474 const int spatial_dimension = 2;
3475 stk::mesh::MetaData meta(spatial_dimension);
3476 stk::unit_test_util::BulkDataTester bulk(meta,communicator);
3477
3478 bulk.modification_begin();
3479 stk::mesh::Entity node1;
3480 if (myRank != 2) {
3481 node1 = bulk.declare_node(1);
3482 int other_proc = 1-myRank;
3483 bulk.add_node_sharing(node1,other_proc);
3484 }
3485 bulk.modification_end();
3486 if (myRank == 0)
3487 {
3488 EXPECT_EQ(1u, bulk.closure_count(node1));
3489 }
3490 else if (myRank == 1)
3491 {
3492 EXPECT_EQ(bulk.my_orphaned_node_marking(), bulk.closure_count(node1));
3493 }
3494 else // myRank == 2
3495 {
3496 EXPECT_FALSE(bulk.is_valid(node1));
3497 }
3498
3499 std::vector<EntityProc> new_owners;
3500 if (myRank == 0)
3501 {
3502 new_owners.push_back(stk::mesh::EntityProc(node1,2));
3503 }
3504 EXPECT_NO_THROW(bulk.change_entity_owner(new_owners));
3505
3506 if (myRank == 0)
3507 {
3508 EXPECT_FALSE(bulk.is_valid(node1));
3509 }
3510 else if (myRank == 1)
3511 {
3512 node1 = bulk.get_entity(stk::mesh::EntityKey(stk::topology::NODE_RANK,1));
3513 EXPECT_EQ(bulk.my_orphaned_node_marking()+0,bulk.closure_count(node1));
3514 EXPECT_FALSE(bulk.bucket(node1).owned());
3515 EXPECT_TRUE(bulk.bucket(node1).shared());
3516 }
3517 else // myRank == 2
3518 {
3519 node1 = bulk.get_entity(stk::mesh::EntityKey(stk::topology::NODE_RANK,1));
3520 EXPECT_EQ(1u,bulk.closure_count(node1));
3521 EXPECT_TRUE(bulk.bucket(node1).owned());
3522 EXPECT_TRUE(bulk.bucket(node1).shared());
3523 }
3524 }
3525
3526 TEST(BulkData, orphaned_node_closure_count_shared_nodes_change_entity_owner_2proc)
3527 {
3528 MPI_Comm communicator = MPI_COMM_WORLD;
3529 int numProcs = stk::parallel_machine_size(communicator);
3530 const int myRank = stk::parallel_machine_rank(communicator);
3531
3532 if (numProcs != 2) { return; }
3533
3534 const int spatial_dimension = 2;
3535 stk::mesh::MetaData meta(spatial_dimension);
3536 stk::unit_test_util::BulkDataTester bulk(meta,communicator);
3537
3538 bulk.modification_begin();
3539 stk::mesh::Entity node1 = bulk.declare_node(1);
3540 int other_proc = 1-myRank;
3541 bulk.add_node_sharing(node1,other_proc);
3542 bulk.modification_end();
3543 if (myRank == 0)
3544 {
3545 EXPECT_EQ(1u,bulk.closure_count(node1));
3546 }
3547 else // myRank == 1
3548 {
3549 EXPECT_EQ(bulk.my_orphaned_node_marking(),bulk.closure_count(node1));
3550 }
3551
3552 std::vector<EntityProc> new_owners;
3553 if (myRank == 0)
3554 {
3555 new_owners.push_back(stk::mesh::EntityProc(node1,1));
3556 }
3557 EXPECT_NO_THROW(bulk.change_entity_owner(new_owners));
3558
3559 if (myRank == 0)
3560 {
3561 EXPECT_EQ(0, bulk.closure_count(node1));
3562 EXPECT_FALSE(bulk.is_valid(node1));
3563 }
3564 else if (myRank == 1)
3565 {
3566 EXPECT_EQ(1u,bulk.closure_count(node1));
3567 EXPECT_TRUE(bulk.bucket(node1).owned());
3568 EXPECT_FALSE(bulk.bucket(node1).shared());
3569 }
3570 }
3571
3572 TEST(BulkData, orphaned_node_closure_count_shared_nodes_owner_adds_element)
3573 {
3574 MPI_Comm communicator = MPI_COMM_WORLD;
3575 int numProcs = stk::parallel_machine_size(communicator);
3576 const int myRank = stk::parallel_machine_rank(communicator);
3577
3578 if (numProcs != 2) { return; }
3579
3580 const int spatial_dimension = 2;
3581 stk::mesh::MetaData meta(spatial_dimension);
3582 stk::unit_test_util::BulkDataTester bulk(meta,communicator);
3583
3584 stk::mesh::Part& element_part = meta.declare_part_with_topology("Beam2Part", stk::topology::BEAM_2);
3585
3586 bulk.modification_begin();
3587 stk::mesh::Entity node1 = bulk.declare_node(1);
3588 stk::mesh::Entity node2 = bulk.declare_node(2);
3589 int other_proc = 1-myRank;
3590 bulk.add_node_sharing(node1,other_proc);
3591 bulk.add_node_sharing(node2,other_proc);
3592
3593 bulk.modification_end();
3594
3595 bulk.modification_begin();
3596 if (myRank == 0)
3597 {
3598 stk::mesh::Entity element = bulk.declare_element(1, stk::mesh::ConstPartVector{&element_part});
3599 bulk.declare_relation(element,node1,0);
3600 EXPECT_EQ(2u,bulk.closure_count(node1));
3601 bulk.declare_relation(element,node2,1);
3602 EXPECT_EQ(2u,bulk.closure_count(node2));
3603 }
3604 EXPECT_NO_THROW(bulk.modification_end());
3605 if (myRank == 1)
3606 {
3607 EXPECT_EQ(bulk.my_orphaned_node_marking(),bulk.closure_count(node1));
3608 EXPECT_EQ(bulk.my_orphaned_node_marking(),bulk.closure_count(node2));
3609 }
3610 }
3611
3612 TEST(BulkData, change_entity_owner_no_aura_check)
3613 {
3614 // id/owner_proc
3615 //
3616 // 1/0---4/0---5/0 1/0---4/1---5/1
3617 // | | | | | |
3618 // | 1/0 | 2/0 | => | 1/0 | 2/1 |
3619 // | | | | | |
3620 // 2/0---3/0---6/0 2/0---3/0---6/1
3621
3622 stk::ParallelMachine pm = MPI_COMM_WORLD;
3623 const int p_rank = stk::parallel_machine_rank( pm );
3624 const int p_size = stk::parallel_machine_size( pm );
3625
3626 if (p_size != 2) {
3627 return;
3628 }
3629
3630 const int spatial_dimension = 2;
3631 stk::mesh::MetaData meta( spatial_dimension );
3632 stk::unit_test_util::BulkDataTester bulk( meta, pm, stk::mesh::BulkData::NO_AUTO_AURA);
3633
3634 std::vector<stk::mesh::Entity> elems;
3635 CEOUtils::fillMeshfor2Elem2ProcMoveAndTest(bulk, meta, elems);
3636
3637 stk::mesh::EntityProcVec entity_procs;
3638 if (p_rank == 0) {
3639 entity_procs.push_back(stk::mesh::EntityProc(elems[1], 1));
3640 entity_procs.push_back(stk::mesh::EntityProc(bulk.get_entity(stk::topology::NODE_RANK, 4), 1));
3641 entity_procs.push_back(stk::mesh::EntityProc(bulk.get_entity(stk::topology::NODE_RANK, 5), 1));
3642 entity_procs.push_back(stk::mesh::EntityProc(bulk.get_entity(stk::topology::NODE_RANK, 6), 1));
3643 }
3644 bulk.change_entity_owner(entity_procs);
3645
3646 CEOUtils::checkStatesAfterCEOME_2Elem2ProcMove_no_ghost(bulk);
3647 }
3648
3649 TEST(BulkData, modification_end_and_change_entity_owner_no_aura_check)
3650 {
3651 // id/owner_proc
3652 //
3653 // 1/0---4/0---5/1 1/1---4/0---5/0
3654 // | | | | | |
3655 // | 1/0 | 2/1 | => | 1/1 | 2/0 |
3656 // | | | | | |
3657 // 2/0---3/0---6/1 2/1---3/0---6/0
3658
3659 stk::ParallelMachine pm = MPI_COMM_WORLD;
3660 const int p_rank = stk::parallel_machine_rank( pm );
3661 const int p_size = stk::parallel_machine_size( pm );
3662
3663 if (p_size != 2) {
3664 return;
3665 }
3666
3667 const int spatial_dimension = 2;
3668 stk::mesh::MetaData meta( spatial_dimension );
3669 stk::unit_test_util::BulkDataTester mesh( meta, pm, stk::mesh::BulkData::NO_AUTO_AURA);
3670
3671 CEOUtils::fillMeshfor2Elem2ProcFlipAndTest_no_ghost(mesh, meta);
3672
3673 stk::mesh::EntityProcVec entity_procs_flip;
3674 if (p_rank == 0) {
3675 entity_procs_flip.push_back(stk::mesh::EntityProc(mesh.get_entity(stk::topology::ELEM_RANK, 1), 1));
3676 entity_procs_flip.push_back(stk::mesh::EntityProc(mesh.get_entity(stk::topology::NODE_RANK, 1), 1));
3677 entity_procs_flip.push_back(stk::mesh::EntityProc(mesh.get_entity(stk::topology::NODE_RANK, 2), 1));
3678 } else {
3679 entity_procs_flip.push_back(stk::mesh::EntityProc(mesh.get_entity(stk::topology::ELEM_RANK, 2), 0));
3680 entity_procs_flip.push_back(stk::mesh::EntityProc(mesh.get_entity(stk::topology::NODE_RANK, 5), 0));
3681 entity_procs_flip.push_back(stk::mesh::EntityProc(mesh.get_entity(stk::topology::NODE_RANK, 6), 0));
3682 }
3683 mesh.change_entity_owner(entity_procs_flip);
3684
3685 CEOUtils::checkStatesAfterCEOME_2Elem2ProcFlip_no_ghost(mesh);
3686 }
3687
3688 //==============================================================================
3689 //These 8 tests thoroughly test change_entity_owner() and consequently:
3690 //in_receive_ghost()
3691 //is_valid()
3692 //parallel_owner_rank() : assuming entity you pass is valid
3693 //entity_comm_map_shared(): this is called for every possible state
3694 //entity_comm_map_owner(): haven't explicitly tested for invalid processor condition
3695 //aura_ghosting: trivial - returns 1 - no obvious need to unit test
3696 //declare_element: tested ubiquitously throughout the code - including stk mesh unit tests
3697 //add_node_sharing: tested ubiquitously throughout the code - including specific add node sharing test in UnitTestBulkDataSharing
3698 //connectEntityToEdge: exercised through create_edges() and directly within 2 change_entity_owner acceptance tests - not certain what happens if you pass it garbage
3699 //declare_part_with_topology: exercised ubiquitously and directly throughout the unit tests
3700
3701
3702 TEST(BulkData, change_entity_owner_2Elem2ProcMove)
3703 {
3704 // id/owner_proc
3705 //
3706 // 1/0---4/0---5/0 1/0---4/1---5/1
3707 // | | | | | |
3708 // | 1/0 | 2/0 | => | 1/0 | 2/1 |
3709 // | | | | | |
3710 // 2/0---3/0---6/0 2/0---3/0---6/1
3711
3712 stk::ParallelMachine pm = MPI_COMM_WORLD;
3713 const int p_rank = stk::parallel_machine_rank( pm );
3714 const int p_size = stk::parallel_machine_size( pm );
3715
3716 if (p_size != 2) {
3717 return;
3718 }
3719
3720 const int spatial_dimension = 2;
3721 stk::mesh::MetaData meta( spatial_dimension );
3722 stk::unit_test_util::BulkDataTester bulk( meta, pm);
3723
3724 std::vector<stk::mesh::Entity> elems;
3725 CEOUtils::fillMeshfor2Elem2ProcMoveAndTest(bulk, meta, elems);
3726
3727 stk::mesh::EntityProcVec entity_procs;
3728 if (p_rank == 0) {
3729 entity_procs.push_back(stk::mesh::EntityProc(elems[1], 1));
3730 entity_procs.push_back(stk::mesh::EntityProc(bulk.get_entity(stk::topology::NODE_RANK, 4), 1));
3731 entity_procs.push_back(stk::mesh::EntityProc(bulk.get_entity(stk::topology::NODE_RANK, 5), 1));
3732 entity_procs.push_back(stk::mesh::EntityProc(bulk.get_entity(stk::topology::NODE_RANK, 6), 1));
3733 }
3734 bulk.change_entity_owner(entity_procs);
3735
3736 CEOUtils::checkStatesAfterCEOME_2Elem2ProcMove(bulk);
3737 }
3738
3739 TEST(BulkData, change_entity_owner_2Elem2ProcFlip)
3740 {
3741 // id/owner_proc
3742 //
3743 // 1/0---4/0---5/1 1/1---4/0---5/0
3744 // | | | | | |
3745 // | 1/0 | 2/1 | => | 1/1 | 2/0 |
3746 // | | | | | |
3747 // 2/0---3/0---6/1 2/1---3/0---6/0
3748
3749 stk::ParallelMachine pm = MPI_COMM_WORLD;
3750 const int p_rank = stk::parallel_machine_rank( pm );
3751 const int p_size = stk::parallel_machine_size( pm );
3752
3753 if (p_size != 2) {
3754 return;
3755 }
3756
3757 const int spatial_dimension = 2;
3758 stk::mesh::MetaData meta( spatial_dimension );
3759 stk::unit_test_util::BulkDataTester mesh( meta, pm);
3760
3761 CEOUtils::fillMeshfor2Elem2ProcFlipAndTest(mesh, meta);
3762
3763 stk::mesh::EntityProcVec entity_procs_flip;
3764 if (p_rank == 0) {
3765 entity_procs_flip.push_back(stk::mesh::EntityProc(mesh.get_entity(stk::topology::ELEM_RANK, 1), 1));
3766 entity_procs_flip.push_back(stk::mesh::EntityProc(mesh.get_entity(stk::topology::NODE_RANK, 1), 1));
3767 entity_procs_flip.push_back(stk::mesh::EntityProc(mesh.get_entity(stk::topology::NODE_RANK, 2), 1));
3768 } else {
3769 entity_procs_flip.push_back(stk::mesh::EntityProc(mesh.get_entity(stk::topology::ELEM_RANK, 2), 0));
3770 entity_procs_flip.push_back(stk::mesh::EntityProc(mesh.get_entity(stk::topology::NODE_RANK, 5), 0));
3771 entity_procs_flip.push_back(stk::mesh::EntityProc(mesh.get_entity(stk::topology::NODE_RANK, 6), 0));
3772 }
3773 mesh.change_entity_owner(entity_procs_flip);
3774
3775 CEOUtils::checkStatesAfterCEOME_2Elem2ProcFlip(mesh);
3776 }
3777
3778 TEST(BulkData, change_entity_owner_3Elem2ProcMoveRight)
3779 {
3780 // id/owner_proc
3781 //
3782 // 1/0---3/0---5/0---7/1 1/0---3/0---5/1---7/1
3783 // | | | | | | | |
3784 // | 1/0 | 2/0 | 3/1 | => | 1/0 | 2/1 | 3/1 |
3785 // | | | | | | | |
3786 // 2/0---4/0---6/0---8/1 2/0---4/0---6/1---8/1
3787
3788 stk::ParallelMachine pm = MPI_COMM_WORLD;
3789
3790 // Set up meta and bulk data
3791 const unsigned spatial_dim = 2;
3792 MetaData meta_data(spatial_dim);
3793 stk::unit_test_util::BulkDataTester mesh(meta_data, pm);
3794 int p_rank = mesh.parallel_rank();
3795 int p_size = mesh.parallel_size();
3796
3797 if(p_size != 2)
3798 {
3799 return;
3800 }
3801
3802 EntityVector nodes;
3803 EntityVector elements;
3804
3805 CEOUtils::fillMeshfor3Elem2ProcMoveRightAndTest(mesh, meta_data, nodes, elements);
3806
3807 std::vector<EntityProc> change;
3808 if(p_rank == 0)
3809 {
3810 change.push_back(EntityProc(elements[1], 1));
3811 change.push_back(EntityProc(nodes[4], 1));
3812 change.push_back(EntityProc(nodes[5], 1));
3813 }
3814
3815 mesh.change_entity_owner(change);
3816
3817 CEOUtils::checkStatesAfterCEOME_3Elem2ProcMoveRight(mesh);
3818 }
3819
3820 TEST(BulkData, change_entity_owner_3Elem2ProcMoveLeft)
3821 {
3822 // id/owner_proc
3823 //
3824 // 1/0---3/0---5/1---7/1 1/0---3/0---5/0---7/1
3825 // | | | | | | | |
3826 // | 1/0 | 2/1 | 3/1 | => | 1/0 | 2/0 | 3/1 |
3827 // | | | | | | | |
3828 // 2/0---4/0---6/1---8/1 2/0---4/0---6/0---8/1
3829
3830 stk::ParallelMachine pm = MPI_COMM_WORLD;
3831
3832 // Set up meta and bulk data
3833 const unsigned spatial_dim = 2;
3834 MetaData meta_data(spatial_dim);
3835 stk::unit_test_util::BulkDataTester mesh(meta_data, pm);
3836 int p_rank = mesh.parallel_rank();
3837 int p_size = mesh.parallel_size();
3838
3839 if(p_size != 2)
3840 {
3841 return;
3842 }
3843
3844 EntityVector nodes;
3845 EntityVector elements;
3846
3847 CEOUtils::fillMeshfor3Elem2ProcMoveLeftAndTest(mesh, meta_data, nodes, elements);
3848
3849 std::vector<EntityProc> change;
3850 if(p_rank == 1)
3851 {
3852 change.push_back(EntityProc(elements[0], 0));
3853 change.push_back(EntityProc(nodes[2], 0));
3854 change.push_back(EntityProc(nodes[3], 0));
3855 }
3856
3857 mesh.change_entity_owner(change);
3858
3859 CEOUtils::checkStatesAfterCEOME_3Elem2ProcMoveLeft(mesh);
3860 }
3861
3862 TEST(BulkData, change_entity_owner_4Elem4ProcEdge)
3863 {
3864 // This unit-test is designed to test the conditions that results that
3865 // resulted in the difficult-to-fix rebalance use-case bug. Specifically,
3866 // it will test the changing-of-ownership of a shared edge to a proc that
3867 // either ghosted it or did not know about it.
3868 //
3869 // id/proc id/proc
3870 // 1/0---3/0---5/1---7/2---9/3 1/0---3/0---5/1---7/0---9/3
3871 // | | | || | | | | || |
3872 // | 1/0 | 2/1 | 3/2|| 4/3 | | 1/0 | 2/1 | 3/0|| 4/3 |
3873 // | | | || | | | | || |
3874 // 2/0---4/0---6/1---8/2---10/3 2/0---4/0---6/1---8/0---10/3
3875 // this edge moves to p0 --^
3876 // element 3 moves to proc 0.
3877 // nodes 7&8 move to proc 0.
3878 // proc 2 forgets everything.
3879 //
3880 // To test this, we use the mesh above, with each elem going on a separate
3881 // proc, one elem per proc. We will take the edge shared by the last
3882 // two (rightmost) elements and change the ownership to proc 0.
3883
3884 stk::ParallelMachine pm = MPI_COMM_WORLD;
3885
3886 // Set up meta and bulk data
3887 const unsigned spatial_dim = 2;
3888 MetaData meta_data(spatial_dim);
3889 stk::unit_test_util::BulkDataTester mesh(meta_data, pm);
3890 int p_rank = mesh.parallel_rank();
3891 int p_size = mesh.parallel_size();
3892
3893 if(p_size != 4)
3894 {
3895 return;
3896 }
3897
3898 stk::mesh::EntityKey elem_key_chg_own;
3899 CEOUtils::fillMeshfor4Elem4ProcEdgeAndTest(mesh, meta_data, elem_key_chg_own);
3900
3901 std::vector<EntityProc> change;
3902 if(p_rank == 2)
3903 {
3904 // Change ownership of changing elem and all entities in it's closure that
3905 // we own to proc 0.
3906
3907 Entity changing_elem = mesh.get_entity(elem_key_chg_own);
3908 ASSERT_TRUE( mesh.is_valid(changing_elem));
3909 EntityProc eproc(changing_elem, 0 /*new owner*/);
3910 change.push_back(eproc);
3911
3912 const stk::mesh::EntityRank end_rank = static_cast<stk::mesh::EntityRank>(mesh.mesh_meta_data().entity_rank_count());
3913 for (stk::mesh::EntityRank irank = stk::topology::BEGIN_RANK; irank < end_rank; ++irank)
3914 {
3915 stk::mesh::Entity const *to_i = mesh.begin(changing_elem, irank);
3916 stk::mesh::Entity const *to_e = mesh.end(changing_elem, irank);
3917 for (; to_i != to_e; ++to_i)
3918 {
3919 if (mesh.parallel_owner_rank(*to_i) == p_rank)
3920 {
3921 EntityProc eproc_new(*to_i, 0 /*new owner*/);
3922 change.push_back(eproc_new);
3923 }
3924 }
3925 }
3926 }
3927
3928 mesh.change_entity_owner(change);
3929
3930 CEOUtils::checkStatesAfterCEOME_4Elem4ProcEdge(mesh);
3931 }
3932
3933 TEST(BulkData, change_entity_owner_8Elem4ProcMoveTop)
3934 {
3935 //
3936 // id/proc id/proc
3937 // 11/0--12/0--13/1--14/2--15/3 11/0--12/0--13/3--14/0--15/3
3938 // | | | | | | | | | |
3939 // | 5/0 | 6/1 | 7/2 | 8/3 | | 5/0 | 6/3 | 7/0 | 8/3 |
3940 // | | | | | | | | | |
3941 // 6/0---7/0---8/1---9/2--10/3 --> 6/0---7/0---8/3---9/0--10/3
3942 // | | | | | | | | | |
3943 // | 1/0 | 2/1 | 3/2 | 4/3 | | 1/0 | 2/1 | 3/2 | 4/3 |
3944 // | | | | | | | | | |
3945 // 1/0---2/0---3/1---4/2---5/3 1/0---2/0---3/1---4/2---5/3
3946 //
3947 // This test moves ownership of elements 6 and 7 (as well as their locally-owned
3948 // nodes) to procs 3 and 0, respectively.
3949 //
3950
3951 stk::ParallelMachine pm = MPI_COMM_WORLD;
3952 int numProcs = stk::parallel_machine_size(pm);
3953 if (numProcs != 4)
3954 {
3955 return;
3956 }
3957
3958 unsigned spatialDim = 2;
3959 stk::mesh::MetaData meta(spatialDim);
3960 stk::unit_test_util::BulkDataTester mesh(meta, pm);
3961
3962 CEOUtils::fillMeshfor8Elem4ProcMoveTopAndTest(mesh, meta);
3963
3964 std::vector<stk::mesh::EntityProc> entities_to_move;
3965 if(mesh.parallel_rank() == 1)
3966 {
3967 stk::mesh::Entity elem = mesh.get_entity(stk::topology::ELEMENT_RANK, 6);
3968 int dest_proc = 3;
3969 entities_to_move.push_back(stk::mesh::EntityProc(elem, dest_proc));
3970 CEOUtils::add_nodes_to_move(mesh, elem, dest_proc, entities_to_move);
3971 }
3972 if(mesh.parallel_rank() == 2)
3973 {
3974 stk::mesh::Entity elem = mesh.get_entity(stk::topology::ELEMENT_RANK, 7);
3975 int dest_proc = 0;
3976 entities_to_move.push_back(stk::mesh::EntityProc(elem, dest_proc));
3977 CEOUtils::add_nodes_to_move(mesh, elem, dest_proc, entities_to_move);
3978 }
3979
3980 mesh.change_entity_owner(entities_to_move);
3981
3982 CEOUtils::checkStatesAfterCEOME_8Elem4ProcMoveTop(mesh);
3983 }
3984
3985 TEST(BulkData, change_entity_owner_4Elem4ProcRotate)
3986 {
3987 //
3988 // id/proc id/proc
3989 // 7/3---8/2---9/2 7/2---8/1---9/1
3990 // | | | | | |
3991 // | 4/3 | 3/2 | | 4/2 | 3/1 |
3992 // | | | | | |
3993 // 4/0---5/0---6/1 --> 4/3---5/3---6/0
3994 // | | | | | |
3995 // | 1/0 | 2/1 | | 1/3 | 2/0 |
3996 // | | | | | |
3997 // 1/0---2/0---3/1 1/3---2/3---3/0
3998
3999 stk::ParallelMachine pm = MPI_COMM_WORLD;
4000 int numProcs = stk::parallel_machine_size(pm);
4001 if (numProcs != 4)
4002 {
4003 return;
4004 }
4005
4006 unsigned spatialDim = 2;
4007 stk::mesh::MetaData meta(spatialDim);
4008 stk::unit_test_util::BulkDataTester mesh(meta, pm);
4009 const int p_rank = mesh.parallel_rank();
4010
4011 CEOUtils::fillMeshfor4Elem4ProcRotateAndTest(mesh, meta);
4012
4013 std::vector<stk::mesh::EntityProc> entities_to_move;
4014 if (p_rank == 0) {
4015 stk::mesh::Entity elem = mesh.get_entity(stk::topology::ELEMENT_RANK, 1);
4016 int dest_proc = 3;
4017 entities_to_move.push_back(stk::mesh::EntityProc(elem, dest_proc));
4018 CEOUtils::add_nodes_to_move(mesh, elem, dest_proc, entities_to_move);
4019 }
4020 else if (p_rank == 1) {
4021 stk::mesh::Entity elem = mesh.get_entity(stk::topology::ELEMENT_RANK, 2);
4022 int dest_proc = 0;
4023 entities_to_move.push_back(stk::mesh::EntityProc(elem, dest_proc));
4024 CEOUtils::add_nodes_to_move(mesh, elem, dest_proc, entities_to_move);
4025 }
4026 else if (p_rank == 2) {
4027 stk::mesh::Entity elem = mesh.get_entity(stk::topology::ELEMENT_RANK, 3);
4028 int dest_proc = 1;
4029 entities_to_move.push_back(stk::mesh::EntityProc(elem, dest_proc));
4030 CEOUtils::add_nodes_to_move(mesh, elem, dest_proc, entities_to_move);
4031 }
4032 else if (p_rank == 3) {
4033 stk::mesh::Entity elem = mesh.get_entity(stk::topology::ELEMENT_RANK, 4);
4034 int dest_proc = 2;
4035 entities_to_move.push_back(stk::mesh::EntityProc(elem, dest_proc));
4036 CEOUtils::add_nodes_to_move(mesh, elem, dest_proc, entities_to_move);
4037 }
4038
4039 mesh.change_entity_owner(entities_to_move);
4040
4041 CEOUtils::checkStatesAfterCEOME_4Elem4ProcRotate(mesh, meta);
4042 }
4043
4044 TEST(BulkData, change_entity_owner_3Elem4Proc1Edge3D)
4045 {
4046 // ID.proc
4047 // 15.2--------16.2 15.1--------16.1
4048 // /| /| /| /|
4049 // / | / | / | / |
4050 // 7.2---------8.2 | 7.1---------8.1 |
4051 // | | 3.2 | | | | 3.1 | |
4052 // | | | | | | | |
4053 // 12.0-------|13.0-------|14.1 12.3-------|13.3-------|14.0
4054 // /| | *| | /| --> /| | *| | /|
4055 // / | |* | |/ | / | |* | |/ |
4056 // 4.0---------5.0---------6.1 | 4.3---------5.3---------6.0 |
4057 // | | 1.0 | | 2.1 | | | | 1.3 | | 2.0 | |
4058 // | | | | | | | | | | | |
4059 // | 9.0-------|10.0-------|11.1 | 9.3-------|10.3-------|11.0
4060 // | / | / | / | / | / | /
4061 // |/ |/ |/ |/ |/ |/
4062 // 1.0---------2.0---------3.1 1.3---------2.3---------3.0
4063 //
4064 // (*)edge: 1.0 (*)edge: 1.1
4065
4066 stk::ParallelMachine pm = MPI_COMM_WORLD;
4067 int numProcs = stk::parallel_machine_size(pm);
4068 if (numProcs != 4)
4069 {
4070 return;
4071 }
4072
4073 unsigned spatialDim = 3;
4074 stk::mesh::MetaData meta(spatialDim);
4075 stk::unit_test_util::BulkDataTester mesh(meta, pm);
4076 const int p_rank = mesh.parallel_rank();
4077 CEOUtils::fillMeshfor3Elem4Proc1Edge3DAndTest(mesh, meta);
4078
4079 std::vector<stk::mesh::EntityProc> entities_to_move;
4080 if (p_rank == 0) {
4081 Entity elem = mesh.get_entity(stk::topology::ELEMENT_RANK, 1);
4082 int dest_proc = 3;
4083 entities_to_move.push_back(stk::mesh::EntityProc(elem, dest_proc));
4084 CEOUtils::add_nodes_to_move(mesh, elem, dest_proc, entities_to_move);
4085
4086 elem = mesh.get_entity(stk::topology::EDGE_RANK, 1);
4087 dest_proc = 1;
4088 entities_to_move.push_back(stk::mesh::EntityProc(elem, dest_proc));
4089 }
4090 else if (p_rank == 1) {
4091 Entity elem = mesh.get_entity(stk::topology::ELEMENT_RANK, 2);
4092 int dest_proc = 0;
4093 entities_to_move.push_back(stk::mesh::EntityProc(elem, dest_proc));
4094 CEOUtils::add_nodes_to_move(mesh, elem, dest_proc, entities_to_move);
4095 }
4096 else if (p_rank == 2) {
4097 Entity elem = mesh.get_entity(stk::topology::ELEMENT_RANK, 3);
4098 int dest_proc = 1;
4099 entities_to_move.push_back(stk::mesh::EntityProc(elem, dest_proc));
4100 CEOUtils::add_nodes_to_move(mesh, elem, dest_proc, entities_to_move);
4101 }
4102
4103 mesh.change_entity_owner(entities_to_move);
4104
4105 CEOUtils::checkStatesAfterCEOME_3Elem4Proc1Edge3D(mesh);
4106 }
4107
4108 TEST(BulkData, test_find_ghosted_nodes_that_need_to_be_shared)
4109 {
4110 unsigned spatialDim = 3;
4111 stk::mesh::MetaData meta(spatialDim);
4112 stk::mesh::Part& elem_part = meta.declare_part_with_topology("beam2", stk::topology::BEAM_2);
4113 meta.commit();
4114
4115 stk::unit_test_util::BulkDataTester bulk(meta, MPI_COMM_WORLD);
4116 if ( bulk.parallel_size() == 2 )
4117 {
4118 bulk.modification_begin();
4119 stk::mesh::Ghosting &ghosting = bulk.create_ghosting("Ghost Both nodes");
4120 stk::mesh::Part& ghost_part = bulk.ghosting_part(ghosting);
4121
4122 std::vector< std::pair<stk::mesh::Entity, int> > ghostingStruct;
4123
4124 if ( bulk.parallel_rank() == 0 )
4125 {
4126 stk::mesh::EntityId nodeId1 = 1;
4127 stk::mesh::Entity node1 = bulk.declare_node(nodeId1);
4128
4129 stk::mesh::EntityId nodeId2 = 2;
4130 stk::mesh::Entity node2 = bulk.declare_node(nodeId2);
4131
4132 ghostingStruct.push_back(std::make_pair(node1, 1));
4133 ghostingStruct.push_back(std::make_pair(node2, 1));
4134 }
4135
4136 bulk.change_ghosting(ghosting, ghostingStruct);
4137
4138 stk::mesh::EntityVector ghosted_nodes_that_need_to_be_shared;
4139 stk::mesh::find_ghosted_nodes_that_need_to_be_shared(bulk, ghosted_nodes_that_need_to_be_shared);
4140
4141 EXPECT_EQ(0u, ghosted_nodes_that_need_to_be_shared.size());
4142
4143 ASSERT_NO_THROW(bulk.modification_end());
4144
4145 std::vector<size_t> counts;
4146 stk::mesh::comm_mesh_counts(bulk, counts);
4147
4148 size_t validNumberOfNodes = 2;
4149 EXPECT_EQ(validNumberOfNodes,counts[stk::topology::NODE_RANK]);
4150
4151 bulk.modification_begin();
4152
4153 if ( bulk.parallel_rank() == 1 )
4154 {
4155 stk::mesh::EntityId elementId = 1;
4156 stk::mesh::Entity element1 = bulk.declare_element(elementId, stk::mesh::ConstPartVector{&elem_part});
4157 stk::mesh::Entity ghostedNode1 = bulk.get_entity(stk::topology::NODE_RANK, 1);
4158 stk::mesh::Entity ghostedNode2 = bulk.get_entity(stk::topology::NODE_RANK, 2);
4159
4160 ASSERT_FALSE(bulk.bucket(ghostedNode1).in_aura());
4161 ASSERT_FALSE(bulk.bucket(ghostedNode2).in_aura());
4162
4163 ASSERT_TRUE(bulk.bucket(ghostedNode1).member(ghost_part));
4164 ASSERT_TRUE(bulk.bucket(ghostedNode2).member(ghost_part));
4165
4166 bulk.declare_relation( element1, ghostedNode1, 0 );
4167 bulk.declare_relation( element1, ghostedNode2, 1 );
4168 }
4169
4170 stk::mesh::find_ghosted_nodes_that_need_to_be_shared(bulk, ghosted_nodes_that_need_to_be_shared);
4171
4172 if ( bulk.parallel_rank() == 1 )
4173 {
4174 EXPECT_EQ(2u, ghosted_nodes_that_need_to_be_shared.size());
4175
4176 stk::mesh::Entity ghostedNode1 = bulk.get_entity(stk::topology::NODE_RANK, 1);
4177 EXPECT_EQ(ghostedNode1, ghosted_nodes_that_need_to_be_shared[0]);
4178
4179 stk::mesh::Entity ghostedNode2 = bulk.get_entity(stk::topology::NODE_RANK, 2);
4180 EXPECT_EQ(ghostedNode2, ghosted_nodes_that_need_to_be_shared[1]);
4181 }
4182 else
4183 {
4184 EXPECT_EQ(0u, ghosted_nodes_that_need_to_be_shared.size());
4185 }
4186
4187 stk::mesh::fixup_ghosted_to_shared_nodes(bulk);
4188 ASSERT_NO_THROW(bulk.modification_end());
4189
4190 stk::mesh::Entity shared_node1 = bulk.get_entity(stk::topology::NODE_RANK, 1);
4191 ASSERT_TRUE(bulk.bucket(shared_node1).shared());
4192
4193 stk::mesh::Entity shared_node2 = bulk.get_entity(stk::topology::NODE_RANK, 2);
4194 ASSERT_TRUE(bulk.bucket(shared_node2).shared());
4195
4196 stk::mesh::comm_mesh_counts(bulk, counts);
4197 EXPECT_EQ(validNumberOfNodes,counts[stk::topology::NODE_RANK]);
4198 }
4199 }
4200
4201
4202 TEST(BulkData, show_how_one_could_add_a_shared_node)
4203 {
4204 unsigned spatialDim = 3;
4205 stk::mesh::MetaData meta(spatialDim);
4206 stk::mesh::Part& elem_part = meta.declare_part_with_topology("triangle", stk::topology::SHELL_TRIANGLE_3);
4207 meta.commit();
4208
4209 stk::unit_test_util::BulkDataTester bulk(meta, MPI_COMM_WORLD);
4210
4211 if ( bulk.parallel_size() == 2 )
4212 {
4213 bulk.modification_begin();
4214
4215 stk::mesh::EntityId nodeId1 = 1;
4216 bulk.declare_node(nodeId1);
4217
4218 stk::mesh::EntityId nodeId2 = 2;
4219 bulk.declare_node(nodeId2);
4220
4221 if ( bulk.parallel_rank() == 0 )
4222 {
4223 stk::mesh::EntityId nodeId3 = 3;
4224 bulk.declare_node(nodeId3);
4225 }
4226 else
4227 {
4228 stk::mesh::EntityId nodeId4 = 4;
4229 bulk.declare_node(nodeId4);
4230 }
4231
4232 ASSERT_NO_THROW(bulk.modification_end());
4233
4234 /// Proc 0 has nodes 1, 2, 3
4235 /// Proc 1 has nodes 1, 2, 4
4236
4237 std::vector<size_t> counts;
4238 stk::mesh::comm_mesh_counts(bulk, counts);
4239
4240 size_t inValidNumberOfNodes = 6;
4241 EXPECT_EQ(inValidNumberOfNodes,counts[stk::topology::NODE_RANK]);
4242
4243 stk::mesh::Entity shared_node1 = bulk.get_entity(stk::topology::NODE_RANK, nodeId1);
4244 ASSERT_FALSE(bulk.bucket(shared_node1).shared());
4245 ASSERT_TRUE(bulk.bucket(shared_node1).owned());
4246
4247 stk::mesh::Entity shared_node2 = bulk.get_entity(stk::topology::NODE_RANK, nodeId2);
4248 ASSERT_FALSE(bulk.bucket(shared_node2).shared());
4249 ASSERT_TRUE(bulk.bucket(shared_node2).owned());
4250
4251 bulk.modification_begin();
4252
4253 int otherProc = 1-bulk.parallel_rank();
4254 std::vector<Entity> shared_modified ;
4255
4256 if ( bulk.parallel_rank() == 0 )
4257 {
4258 stk::mesh::EntityId elementId = 1;
4259 stk::mesh::Entity element = bulk.declare_element(elementId, stk::mesh::ConstPartVector{&elem_part});
4260
4261 stk::mesh::Entity node1 = bulk.get_entity(stk::topology::NODE_RANK, 1);
4262 stk::mesh::Entity node2 = bulk.get_entity(stk::topology::NODE_RANK, 2);
4263 stk::mesh::Entity node3 = bulk.get_entity(stk::topology::NODE_RANK, 3);
4264
4265 ASSERT_TRUE(bulk.is_valid(node1));
4266 ASSERT_TRUE(bulk.is_valid(node2));
4267 ASSERT_TRUE(bulk.is_valid(node3));
4268 ASSERT_TRUE(bulk.is_valid(element));
4269
4270 bulk.declare_relation( element, node1, 0 );
4271 bulk.declare_relation( element, node2, 1 );
4272 bulk.declare_relation( element, node3, 2 );
4273
4274 bulk.add_node_sharing(node1, otherProc);
4275 bulk.add_node_sharing(node2, otherProc);
4276 shared_modified.push_back(node1);
4277 shared_modified.push_back(node2);
4278 }
4279 else
4280 {
4281 stk::mesh::EntityId elementId = 2;
4282 stk::mesh::Entity element = bulk.declare_element(elementId, stk::mesh::ConstPartVector{&elem_part});
4283
4284 stk::mesh::Entity node1 = bulk.get_entity(stk::topology::NODE_RANK, 1);
4285 stk::mesh::Entity node2 = bulk.get_entity(stk::topology::NODE_RANK, 2);
4286 stk::mesh::Entity node4 = bulk.get_entity(stk::topology::NODE_RANK, 4);
4287
4288 ASSERT_TRUE(bulk.is_valid(node1));
4289 ASSERT_TRUE(bulk.is_valid(node2));
4290 ASSERT_TRUE(bulk.is_valid(node4));
4291 ASSERT_TRUE(bulk.is_valid(element));
4292
4293 bulk.declare_relation( element, node1, 0 );
4294 bulk.declare_relation( element, node2, 1 );
4295 bulk.declare_relation( element, node4, 2 );
4296
4297 bulk.add_node_sharing(node1, otherProc);
4298 bulk.add_node_sharing(node2, otherProc);
4299 shared_modified.push_back(node1);
4300 shared_modified.push_back(node2);
4301 }
4302
4303
4304 for (size_t i=0;i<shared_modified.size();i++)
4305 {
4306 std::vector<int> procs;
4307 bulk.comm_shared_procs(bulk.entity_key(shared_modified[i]), procs);
4308 int owner = bulk.parallel_rank();
4309 for (size_t j=0;j<procs.size();j++)
4310 {
4311 owner = std::min(owner, procs[j]);
4312 }
4313 bulk.my_internal_change_owner_in_comm_data(shared_modified[i], owner);
4314 }
4315
4316 ASSERT_NO_THROW(bulk.modification_end());
4317
4318 stk::mesh::comm_mesh_counts(bulk, counts);
4319 EXPECT_EQ(4u,counts[stk::topology::NODE_RANK]);
4320 }
4321 }
4322
4323
4324 void write_mesh(const std::string& filename, stk::mesh::BulkData& mesh)
4325 {
4326 stk::io::StkMeshIoBroker writer(mesh.parallel());
4327 writer.set_bulk_data(mesh);
4328 size_t output_handle = writer.create_output_mesh(filename, stk::io::WRITE_RESULTS);
4329 writer.write_output_mesh(output_handle);
4330 }
4331
4332 void test_nodes(stk::mesh::BulkData &mesh)
4333 {
4334 stk::mesh::EntityId node1Id = 1;
4335 stk::mesh::EntityId node2Id = 2;
4336 stk::mesh::EntityId node3Id = 3;
4337 stk::mesh::EntityId node4Id = 4;
4338
4339 stk::mesh::Entity node1 = mesh.get_entity(stk::topology::NODE_RANK, node1Id);
4340 stk::mesh::Entity node2 = mesh.get_entity(stk::topology::NODE_RANK, node2Id);
4341 stk::mesh::Entity node3 = mesh.get_entity(stk::topology::NODE_RANK, node3Id);
4342 stk::mesh::Entity node4 = mesh.get_entity(stk::topology::NODE_RANK, node4Id);
4343
4344 stk::mesh::Entity element1 = mesh.get_entity(stk::topology::ELEMENT_RANK, 1);
4345
4346 if ( mesh.parallel_rank() == 0)
4347 {
4348 EXPECT_TRUE(mesh.bucket(node1).owned());
4349 EXPECT_TRUE(mesh.bucket(node2).owned());
4350 EXPECT_TRUE(mesh.bucket(node3).owned());
4351 EXPECT_FALSE(mesh.is_valid(node4));
4352
4353 EXPECT_TRUE(mesh.bucket(node1).shared());
4354 EXPECT_TRUE(mesh.bucket(node2).shared());
4355 EXPECT_FALSE(mesh.bucket(node3).shared());
4356
4357 EXPECT_FALSE(mesh.bucket(node1).in_aura());
4358 EXPECT_FALSE(mesh.bucket(node2).in_aura());
4359 EXPECT_FALSE(mesh.bucket(node3).in_aura());
4360
4361 EXPECT_TRUE(mesh.bucket(element1).owned());
4362 }
4363 else if ( mesh.parallel_rank() == 1)
4364 {
4365 EXPECT_FALSE(mesh.bucket(node1).owned());
4366 EXPECT_FALSE(mesh.bucket(node2).owned());
4367 EXPECT_FALSE(mesh.bucket(node3).owned());
4368 EXPECT_TRUE(mesh.bucket(node4).owned());
4369
4370 EXPECT_TRUE(mesh.bucket(node1).shared());
4371 EXPECT_TRUE(mesh.bucket(node2).shared());
4372 EXPECT_FALSE(mesh.bucket(node3).shared());
4373 EXPECT_FALSE(mesh.bucket(node4).shared());
4374
4375 EXPECT_FALSE(mesh.bucket(node1).in_aura());
4376 EXPECT_FALSE(mesh.bucket(node2).in_aura());
4377 EXPECT_TRUE(mesh.bucket(node3).in_aura());
4378 EXPECT_FALSE(mesh.bucket(node4).in_aura());
4379
4380 EXPECT_FALSE(mesh.bucket(element1).owned());
4381 EXPECT_TRUE(mesh.bucket(element1).in_aura());
4382 }
4383 }
4384
4385 TEST(BulkData, can_we_create_shared_nodes)
4386 {
4387 int numProcs = -1;
4388 MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
4389
4390 if ( numProcs == 2 )
4391 {
4392 std::string filename = "test.exo";
4393
4394 {
4395 unsigned spatialDim = 3;
4396 stk::mesh::MetaData meta(spatialDim);
4397
4398 stk::mesh::Selector all_nodes = meta.universal_part();
4399 typedef stk::mesh::Field<double> CoordFieldType;
4400 CoordFieldType& coordField = meta.declare_field<double>(stk::topology::NODE_RANK, "model_coordinates");
4401 stk::mesh::put_field_on_mesh(coordField, all_nodes, 3, nullptr);
4402
4403 stk::mesh::Part& elem_part = meta.declare_part_with_topology("block_1", stk::topology::BEAM_2);
4404 stk::io::put_io_part_attribute(elem_part);
4405
4406 typedef stk::mesh::Field<double> ScalarField;
4407 ScalarField& temperatureField = meta.declare_field<double>(stk::topology::NODE_RANK, "temperature");
4408
4409 double initialTemperatureValue = 25.0;
4410 stk::mesh::put_field_on_entire_mesh_with_initial_value(temperatureField, &initialTemperatureValue);
4411
4412 meta.commit();
4413
4414 //////////////////////////////////////////
4415 /*
4416 Proc 0 | Proc 1
4417 |
4418 3 1 | 1 4
4419 o----o | o o
4420 E1 |
4421 |
4422 2 | 2
4423 o | o
4424 |
4425 */
4426
4427 stk::unit_test_util::BulkDataTester bulk(meta, MPI_COMM_WORLD);
4428
4429 bulk.modification_begin();
4430
4431 double xCoords[4] = { 0., 1., 1., 1. };
4432 double yCoords[4] = { 0., 0., 1., 1. };
4433 double zCoords[4] = { 0., 0., 0., 1. };
4434
4435 stk::mesh::EntityId nodeId1 = 1;
4436 stk::mesh::Entity node1 = bulk.declare_node(nodeId1);
4437
4438 stk::mesh::EntityId nodeId2 = 2;
4439 stk::mesh::Entity node2 = bulk.declare_node(nodeId2);
4440
4441 stk::mesh::Entity thirdNode;
4442 if ( bulk.parallel_rank() == 0 )
4443 {
4444 stk::mesh::EntityId nodeId3 = 3;
4445 thirdNode = bulk.declare_node(nodeId3);
4446 }
4447 else
4448 {
4449 stk::mesh::EntityId nodeId4 = 4;
4450 thirdNode = bulk.declare_node(nodeId4);
4451 }
4452
4453 int otherProc = 1 - bulk.parallel_rank();
4454 bulk.add_node_sharing(node1, otherProc);
4455 bulk.add_node_sharing(node2, otherProc);
4456
4457 if ( bulk.parallel_rank() == 0 )
4458 {
4459 stk::mesh::EntityId elementId = 1 + bulk.parallel_rank();
4460 stk::mesh::Entity element = bulk.declare_element(elementId, stk::mesh::ConstPartVector{&elem_part});
4461 bulk.declare_relation(element, node1, 0);
4462 bulk.declare_relation(element, thirdNode, 1);
4463 }
4464
4465 ASSERT_NO_THROW(bulk.modification_end());
4466
4467 test_nodes(bulk);
4468
4469 ////////////////////////
4470 // Made it here. nodes 2 and 3 are shared, but are not connected to any higher
4471 // ranked entities on processor 1
4472
4473 // Add coordinates so mesh can be written out to file
4474 int node_id = bulk.identifier(node1);
4475 double* coords = stk::mesh::field_data(coordField, node1);
4476 coords[0] = xCoords[node_id-1];
4477 coords[1] = yCoords[node_id-1];
4478 coords[2] = zCoords[node_id-1];
4479
4480 node_id = bulk.identifier(node2);
4481 coords = stk::mesh::field_data(coordField, node2);
4482 coords[0] = xCoords[node_id-1];
4483 coords[1] = yCoords[node_id-1];
4484 coords[2] = zCoords[node_id-1];
4485
4486 node_id = bulk.identifier(thirdNode);
4487 coords = stk::mesh::field_data(coordField, thirdNode);
4488 coords[0] = xCoords[node_id-1];
4489 coords[1] = yCoords[node_id-1];
4490 coords[2] = zCoords[node_id-1];
4491
4492 std::vector<size_t> counts;
4493 stk::mesh::comm_mesh_counts(bulk, counts);
4494 size_t goldNumberNodes = 4;
4495 EXPECT_EQ(goldNumberNodes, counts[stk::topology::NODE_RANK]);
4496 write_mesh(filename, bulk);
4497
4498 // Add temperature field to make sure parallel field communications work
4499
4500 double owned_value = 10;
4501 double shared_value = 1;
4502
4503 double *tempField = stk::mesh::field_data(temperatureField, node1);
4504 if ( bulk.bucket(node1).owned() )
4505 {
4506 *tempField = owned_value;
4507 }
4508 else
4509 {
4510 *tempField = shared_value;
4511 }
4512
4513 tempField = stk::mesh::field_data(temperatureField, node2);
4514 if ( bulk.bucket(node2).owned() )
4515 {
4516 *tempField = owned_value;
4517 }
4518 else
4519 {
4520 *tempField = shared_value;
4521 }
4522
4523 tempField = stk::mesh::field_data(temperatureField, thirdNode);
4524 if ( bulk.bucket(thirdNode).owned() )
4525 {
4526 *tempField = owned_value;
4527 }
4528 else
4529 {
4530 *tempField = shared_value;
4531 }
4532
4533 std::vector<const stk::mesh::FieldBase*> fields(1, &temperatureField);
4534 // send data, owned to shared
4535 stk::mesh::communicate_field_data(bulk.shared_ghosting(), fields); /* IMPORTANT PART TO TEST */
4536
4537 tempField = stk::mesh::field_data(temperatureField, node1);
4538 EXPECT_NEAR(owned_value, *tempField, 1e-6);
4539 tempField = stk::mesh::field_data(temperatureField, node2);
4540 EXPECT_NEAR(owned_value, *tempField, 1e-6);
4541 tempField = stk::mesh::field_data(temperatureField, thirdNode);
4542 EXPECT_NEAR(owned_value, *tempField, 1e-6);
4543
4544 // node 3 on processor 1 is ghosted, so it still has initial value
4545 if ( bulk.parallel_rank() == 1 )
4546 {
4547 stk::mesh::Entity ghostedNode = bulk.get_entity(stk::topology::NODE_RANK, 3);
4548 tempField = stk::mesh::field_data(temperatureField, ghostedNode);
4549 EXPECT_NEAR(initialTemperatureValue, *tempField, 1e-6);
4550 }
4551
4552 // send data, owned to ghosted
4553 stk::mesh::communicate_field_data(bulk.aura_ghosting(), fields); /* IMPORTANT PART TO TEST */
4554
4555 if ( bulk.parallel_rank() == 1 )
4556 {
4557 stk::mesh::Entity ghostedNode = bulk.get_entity(stk::topology::NODE_RANK, 3);
4558 tempField = stk::mesh::field_data(temperatureField, ghostedNode);
4559 EXPECT_NEAR(owned_value, *tempField, 1e-6);
4560 }
4561
4562 std::vector<const stk::mesh::FieldBase*> fields1(1, &temperatureField);
4563 stk::mesh::parallel_sum(bulk, fields1); /* IMPORTANT PART TO TEST */
4564
4565 double summed_value = 2*owned_value;
4566 tempField = stk::mesh::field_data(temperatureField, node1);
4567 EXPECT_NEAR(summed_value, *tempField, 1e-6);
4568 tempField = stk::mesh::field_data(temperatureField, node2);
4569 EXPECT_NEAR(summed_value, *tempField, 1e-6);
4570
4571 double min_value = 1;
4572 if ( bulk.parallel_rank() == 1 )
4573 {
4574 tempField = stk::mesh::field_data(temperatureField, node1);
4575 *tempField = min_value;
4576 }
4577
4578 stk::mesh::parallel_min(bulk, fields1); /* IMPORTANT PART TO TEST */
4579 tempField = stk::mesh::field_data(temperatureField, node1);
4580 EXPECT_NEAR(min_value, *tempField, 1e-6);
4581
4582 double max_value = 100;
4583 if ( bulk.parallel_rank() == 1 )
4584 {
4585 tempField = stk::mesh::field_data(temperatureField, node1);
4586 *tempField = max_value;
4587 }
4588
4589 stk::mesh::parallel_max(bulk, fields1); /* IMPORTANT PART TO TEST */
4590 tempField = stk::mesh::field_data(temperatureField, node1);
4591 EXPECT_NEAR(max_value, *tempField, 1e-6);
4592
4593 std::vector<EntityProc> entities_to_change;
4594 if ( bulk.parallel_rank() == 0 )
4595 {
4596 entities_to_change.push_back(std::make_pair(node2, 1));
4597 }
4598
4599 bulk.change_entity_owner(entities_to_change);
4600
4601 if (bulk.parallel_rank() == 0)
4602 {
4603 EXPECT_FALSE(bulk.is_valid(node2));
4604 }
4605 else // bulk.parallel_rank() == 1
4606 {
4607 ASSERT_TRUE(bulk.is_valid(node2));
4608 EXPECT_FALSE(bulk.bucket(node2).shared());
4609 EXPECT_EQ(1u,bulk.closure_count(node2));
4610 }
4611
4612 }
4613
4614 // Now read in the mesh and see that it passes same tests
4615
4616 {
4617 unsigned spatialDim = 3;
4618 std::shared_ptr<BulkData> bulkPtr = stk::unit_test_util::build_mesh(spatialDim, MPI_COMM_WORLD);
4619 stk::mesh::BulkData& mesh = *bulkPtr;
4620 stk::io::StkMeshIoBroker reader(mesh.parallel());
4621 reader.set_bulk_data(mesh);
4622 reader.add_mesh_database(filename, stk::io::READ_MESH);
4623 reader.create_input_mesh();
4624 reader.populate_bulk_data();
4625
4626 test_nodes(mesh);
4627
4628 std::vector<size_t> counts;
4629 stk::mesh::comm_mesh_counts(mesh, counts);
4630 EXPECT_EQ(4u,counts[stk::topology::NODE_RANK]);
4631
4632 std::ostringstream os;
4633 os << filename << ".2." << mesh.parallel_rank();
4634 std::string fileToRemove = os.str();
4635 unlink(fileToRemove.c_str());
4636 }
4637 }
4638 }
4639
4640 typedef std::pair< std::vector<const stk::mesh::Entity*>, stk::mesh::Entity* > ChildNodeRequest;
4641
4642 struct edgeToBeRefined
4643 {
4644 stk::mesh::EntityId m_node1;
4645 stk::mesh::EntityId m_node2;
4646 std::vector<int> m_sharing_procs;
4647 std::vector<std::pair<int, stk::mesh::EntityId> > m_id_proc_pairs_from_all_procs;
4648 stk::mesh::BulkData *m_stk_mesh;
4649 bool m_id_procs_pairs_have_been_sorted;
4650 stk::mesh::Entity **m_node_entity;
4651
4652 edgeToBeRefined(stk::mesh::BulkData& bulkData, stk::mesh::EntityId nodeA, stk::mesh::EntityId nodeB, stk::mesh::Entity **entity_place_holder=NULL)
4653 : m_node1(0), m_node2(0), m_sharing_procs(), m_id_proc_pairs_from_all_procs(), m_stk_mesh(&bulkData),
4654 m_id_procs_pairs_have_been_sorted(false), m_node_entity(entity_place_holder)
4655 {
4656 if ( nodeA < nodeB )
4657 {
4658 m_node1 = nodeA;
4659 m_node2 = nodeB;
4660 }
4661 else
4662 {
4663 m_node1 = nodeB;
4664 m_node2 = nodeA;
4665 }
4666 }
4667
4668 void add_proc_id_pair(int proc_id, stk::mesh::EntityId id)
4669 {
4670 m_id_proc_pairs_from_all_procs.push_back(std::make_pair(proc_id, id));
4671 }
4672
4673 void calculate_sharing_procs()
4674 {
4675 std::vector<int> sharingProcsA;
4676 stk::mesh::EntityKey keyA(stk::topology::NODE_RANK, m_node1);
4677 m_stk_mesh->comm_shared_procs(keyA, sharingProcsA);
4678
4679 std::vector<int> sharingProcsB;
4680 stk::mesh::EntityKey keyB(stk::topology::NODE_RANK, m_node2);
4681 m_stk_mesh->comm_shared_procs(keyB, sharingProcsB);
4682
4683 std::sort(sharingProcsA.begin(), sharingProcsA.end());
4684 std::sort(sharingProcsB.begin(), sharingProcsB.end());
4685
4686 std::set_intersection(sharingProcsA.begin(),sharingProcsA.end(),sharingProcsB.begin(),sharingProcsB.end(),std::back_inserter(m_sharing_procs));
4687 }
4688
4689 size_t num_sharing_procs() const
4690 {
4691 return m_sharing_procs.size();
4692 }
4693
4694 int sharing_proc(int index) const
4695 {
4696 return m_sharing_procs[index];
4697 }
4698
4699 stk::mesh::EntityId suggested_node_id() const
4700 {
4701 STK_ThrowRequireMsg(!m_id_procs_pairs_have_been_sorted, "Invalid use of edge calculation. Contact sierra-help");
4702 return m_id_proc_pairs_from_all_procs[0].second;
4703 }
4704
4705 stk::mesh::EntityId node1() const
4706 {
4707 return m_node1;
4708 }
4709
4710 stk::mesh::EntityId node2() const
4711 {
4712 return m_node2;
4713 }
4714
4715 void sort_id_proc_pairs()
4716 {
4717 m_id_procs_pairs_have_been_sorted = true;
4718 std::sort(m_id_proc_pairs_from_all_procs.begin(), m_id_proc_pairs_from_all_procs.end());
4719 }
4720
4721 stk::mesh::EntityId get_id_for_edge() const
4722 {
4723 STK_ThrowRequireMsg(m_id_procs_pairs_have_been_sorted, "Invalid use of edge calculation. Contact sierra-help");
4724 return m_id_proc_pairs_from_all_procs[0].second;
4725 }
4726
4727 void set_node_entity_for_edge()
4728 {
4729 this->sort_id_proc_pairs();
4730 stk::mesh::EntityId id_for_edge = this->get_id_for_edge();
4731 *(*m_node_entity) = m_stk_mesh->declare_node(id_for_edge);
4732 for (size_t i=0;i<m_id_proc_pairs_from_all_procs.size();++i)
4733 {
4734 if ( m_id_proc_pairs_from_all_procs[i].first != m_stk_mesh->parallel_rank() )
4735 {
4736 m_stk_mesh->add_node_sharing(**m_node_entity, m_id_proc_pairs_from_all_procs[i].first);
4737 }
4738 }
4739 }
4740
4741 bool operator==(const edgeToBeRefined& otherEdge) const
4742 {
4743 if ( this->node1() == otherEdge.node1() &&
4744 this->node2() == otherEdge.node2() )
4745 {
4746 return true;
4747 }
4748 return false;
4749 }
4750 };
4751
4752 struct edge_finder
4753 {
4754 bool operator()(const edgeToBeRefined& edge1, const edgeToBeRefined& edge2) const
4755 {
4756 if ( edge1.node1() != edge2.node1() )
4757 {
4758 return edge1.node1() < edge2.node1();
4759 }
4760 else
4761 {
4762 return edge1.node2() < edge2.node2();
4763 }
4764 }
4765 };
4766
4767 void batch_create_child_nodes_new(BulkData & mesh, std::vector< ChildNodeRequest > & child_node_requests)
4768 {
4769 bool communicate_nodes = true;
4770 std::vector<bool> communicate_edge(child_node_requests.size(), false);
4771
4772 unsigned num_nodes_requested = child_node_requests.size();
4773 std::vector<stk::mesh::EntityId> available_node_ids;
4774 mesh.generate_new_ids(stk::topology::NODE_RANK, num_nodes_requested, available_node_ids);
4775
4776 while ( communicate_nodes )
4777 {
4778 communicate_nodes = false;
4779
4780 std::vector<edgeToBeRefined> edgesToBeRefined;
4781
4782 for (unsigned it_req=0; it_req<child_node_requests.size(); ++it_req)
4783 {
4784 ChildNodeRequest & request = child_node_requests[it_req];
4785 std::vector<const stk::mesh::Entity*> & request_parents = request.first;
4786
4787 STK_ThrowRequireMsg(request_parents.size() == 2, "Invalid size of request, needed exactly 2 parents, found " << request_parents.size() << ". Contact sierra-help.");
4788 if (mesh.is_valid(*request_parents[0]) && mesh.is_valid(*request_parents[1]) && communicate_edge[it_req] == false )
4789 {
4790 stk::mesh::Entity **request_child = &request.second;
4791
4792 communicate_edge[it_req] = true;
4793 communicate_nodes = true;
4794 stk::mesh::EntityId node1 = mesh.identifier(*request_parents[0]);
4795 stk::mesh::EntityId node2 = mesh.identifier(*request_parents[1]);
4796
4797 edgesToBeRefined.push_back(edgeToBeRefined(mesh, node1, node2, request_child));
4798 edgesToBeRefined.back().add_proc_id_pair(mesh.parallel_rank(), available_node_ids[it_req]);
4799 edgesToBeRefined.back().calculate_sharing_procs();
4800 }
4801 }
4802
4803 int more_work_to_be_done = communicate_nodes ? 1 : 0;
4804 int global_result = 0;
4805 stk::all_reduce_max(mesh.parallel(), &more_work_to_be_done, &global_result, 1);
4806 if ( global_result == 0 ) break;
4807
4808 std::sort(edgesToBeRefined.begin(), edgesToBeRefined.end(), edge_finder());
4809
4810 // By this point, I have list of edges that I need to communicate
4811
4812 stk::CommSparse comm_spec(mesh.parallel());
4813
4814 for (int phase=0;phase<2;++phase)
4815 {
4816 for (size_t edge_index=0;edge_index<edgesToBeRefined.size();++edge_index)
4817 {
4818 for (size_t proc_index=0;proc_index<edgesToBeRefined[edge_index].num_sharing_procs();++proc_index)
4819 {
4820 int other_proc = edgesToBeRefined[edge_index].sharing_proc(proc_index);
4821 stk::mesh::EntityId node_1 = edgesToBeRefined[edge_index].node1();
4822 stk::mesh::EntityId node_2 = edgesToBeRefined[edge_index].node2();
4823 stk::mesh::EntityId this_procs_suggested_id = edgesToBeRefined[edge_index].suggested_node_id();
4824 comm_spec.send_buffer(other_proc).pack<stk::mesh::EntityId>(node_1).pack<stk::mesh::EntityId>(node_2);
4825 comm_spec.send_buffer(other_proc).pack<stk::mesh::EntityId>(this_procs_suggested_id);
4826 }
4827 }
4828
4829 if ( phase == 0 )
4830 {
4831 comm_spec.allocate_buffers();
4832 }
4833 else
4834 {
4835 comm_spec.communicate();
4836 }
4837 }
4838
4839 for(int i = 0; i < mesh.parallel_size(); ++i)
4840 {
4841 if(i != mesh.parallel_rank())
4842 {
4843 while(comm_spec.recv_buffer(i).remaining())
4844 {
4845 stk::mesh::EntityId node1;
4846 stk::mesh::EntityId node2;
4847 stk::mesh::EntityId suggested_node_id;
4848 comm_spec.recv_buffer(i).unpack<stk::mesh::EntityId>(node1);
4849 comm_spec.recv_buffer(i).unpack<stk::mesh::EntityId>(node2);
4850 comm_spec.recv_buffer(i).unpack<stk::mesh::EntityId>(suggested_node_id);
4851
4852 edgeToBeRefined from_other_proc(mesh, node1, node2);
4853 std::vector<edgeToBeRefined>::iterator iter = std::lower_bound(edgesToBeRefined.begin(), edgesToBeRefined.end(),
4854 from_other_proc, edge_finder());
4855
4856 if ( iter != edgesToBeRefined.end() && *iter == from_other_proc)
4857 {
4858 iter->add_proc_id_pair(i, suggested_node_id);
4859 }
4860 }
4861 }
4862 }
4863
4864 for (size_t edge_index=0;edge_index<edgesToBeRefined.size();++edge_index)
4865 {
4866 edgesToBeRefined[edge_index].set_node_entity_for_edge();
4867 }
4868 }
4869
4870 std::vector<bool>::iterator iter = std::find(communicate_edge.begin(), communicate_edge.end(), false);
4871 STK_ThrowRequireMsg(iter == communicate_edge.end(), "Invalid edge requests. Contact sierra-help.");
4872 }
4873
4874 void set_coords_on_new_node(stk::mesh::MetaData& meta, stk::mesh::Entity nodeA, stk::mesh::Entity nodeB, stk::mesh::Entity new_node)
4875 {
4876 stk::mesh::FieldBase const * coord = meta.coordinate_field();
4877
4878 double x = 0, y = 0, z = 0;
4879
4880 double *fieldValue = static_cast<double *>(stk::mesh::field_data(*coord, nodeA));
4881 x += fieldValue[0]*0.5;
4882 y += fieldValue[1]*0.5;
4883 z += fieldValue[2]*0.5;
4884
4885 fieldValue = static_cast<double *>(stk::mesh::field_data(*coord, nodeB));
4886 x += fieldValue[0]*0.5;
4887 y += fieldValue[1]*0.5;
4888 z += fieldValue[2]*0.5;
4889
4890 fieldValue = static_cast<double *>(stk::mesh::field_data(*coord, new_node));
4891 fieldValue[0] = x;
4892 fieldValue[1] = y;
4893 fieldValue[2] = z;
4894 }
4895
4896 TEST(BulkData, create_vigilante_nodes_along_shared_edge)
4897 {
4898 unsigned spatialDim = 3;
4899 std::shared_ptr<BulkData> bulkPtr = stk::unit_test_util::build_mesh(spatialDim, MPI_COMM_WORLD);
4900 stk::mesh::MetaData& meta= bulkPtr->mesh_meta_data();
4901 stk::mesh::BulkData& mesh = *bulkPtr;
4902 stk::mesh::Part& node_part = meta.declare_part_with_topology("nodelist_1", stk::topology::NODE);
4903 stk::io::put_io_part_attribute(node_part);
4904
4905 stk::io::StkMeshIoBroker reader(mesh.parallel());
4906
4907 if ( mesh.parallel_size() == 2 )
4908 {
4909 reader.set_bulk_data(mesh);
4910 reader.add_mesh_database("generated:2x2x2", stk::io::READ_MESH);
4911 reader.create_input_mesh();
4912 reader.populate_bulk_data();
4913
4914 std::vector<size_t> counts;
4915 stk::mesh::comm_mesh_counts(mesh, counts);
4916
4917 EXPECT_EQ(27u,counts[stk::topology::NODE_RANK]);
4918 EXPECT_EQ(8u,counts[stk::topology::ELEMENT_RANK]);
4919
4920 // nodes 16, 17, 18
4921 // nodes 13, 14, 15
4922 // nodes 10, 11, 12
4923
4924 stk::mesh::Entity node_10 = mesh.get_entity(stk::topology::NODE_RANK, 10);
4925 stk::mesh::Entity node_11 = mesh.get_entity(stk::topology::NODE_RANK, 11);
4926
4927 ASSERT_TRUE(mesh.bucket(node_10).shared());
4928 ASSERT_TRUE(mesh.bucket(node_11).shared());
4929
4930 {
4931 std::vector<ChildNodeRequest> child_node_requests;
4932
4933 stk::mesh::Entity node_between_10_and_11= stk::mesh::Entity();
4934 {
4935 std::vector<const stk::mesh::Entity*> node_parents;
4936 node_parents.push_back(&node_10);
4937 node_parents.push_back(&node_11);
4938 ChildNodeRequest node_request(node_parents, &node_between_10_and_11);
4939 child_node_requests.push_back(node_request);
4940 }
4941
4942 stk::mesh::Entity node_between_10_and_new_node = stk::mesh::Entity();
4943 {
4944 std::vector<const stk::mesh::Entity*> node_parents;
4945 node_parents.push_back(&node_10);
4946 node_parents.push_back(&node_between_10_and_11);
4947 ChildNodeRequest node_request(node_parents, &node_between_10_and_new_node);
4948 child_node_requests.push_back(node_request);
4949 }
4950
4951 stk::mesh::Entity node_between_10_and_second_new_node = stk::mesh::Entity();
4952 {
4953 std::vector<const stk::mesh::Entity*> node_parents;
4954 node_parents.push_back(&node_10);
4955 node_parents.push_back(&node_between_10_and_new_node);
4956 ChildNodeRequest node_request(node_parents, &node_between_10_and_second_new_node);
4957 child_node_requests.push_back(node_request);
4958 }
4959
4960
4961 mesh.modification_begin();
4962
4963 batch_create_child_nodes_new(mesh, child_node_requests);
4964
4965 stk::mesh::PartVector add_parts_vec;
4966 stk::mesh::PartVector rem_parts_vec;
4967 add_parts_vec.push_back(&node_part);
4968 mesh.change_entity_parts(node_between_10_and_11, add_parts_vec, rem_parts_vec);
4969 mesh.change_entity_parts(node_between_10_and_new_node, add_parts_vec, rem_parts_vec);
4970 mesh.change_entity_parts(node_between_10_and_second_new_node, add_parts_vec, rem_parts_vec);
4971
4972
4973 ASSERT_NO_THROW(mesh.modification_end());
4974
4975 for (size_t i=0;i<child_node_requests.size();++i)
4976 {
4977 ChildNodeRequest & request = child_node_requests[i];
4978 std::vector<const stk::mesh::Entity*> & request_parents = request.first;
4979 stk::mesh::Entity *new_node = request.second;
4980 set_coords_on_new_node(mesh.mesh_meta_data(), *request_parents[0], *request_parents[1], *new_node);
4981 }
4982 }
4983
4984 stk::mesh::comm_mesh_counts(mesh, counts);
4985
4986 EXPECT_EQ(30u,counts[stk::topology::NODE_RANK]);
4987 EXPECT_EQ(8u,counts[stk::topology::ELEMENT_RANK]);
4988
4989 //std::string filename = "test.exo";
4990 //write_mesh(filename, mesh);
4991 }
4992 }
4993
4994 TEST(BulkData, renegade_nodes_along_every_edge)
4995 {
4996 unsigned spatialDim = 3;
4997 std::shared_ptr<BulkData> bulkPtr = stk::unit_test_util::build_mesh(spatialDim, MPI_COMM_WORLD);
4998 stk::mesh::MetaData& meta= bulkPtr->mesh_meta_data();
4999 stk::mesh::BulkData& mesh = *bulkPtr;
5000 stk::mesh::Part& node_part = meta.declare_part_with_topology("nodelist_1", stk::topology::NODE);
5001 stk::io::put_io_part_attribute(node_part);
5002
5003 stk::io::StkMeshIoBroker reader(mesh.parallel());
5004
5005 if(mesh.parallel_size() == 2)
5006 {
5007 reader.set_bulk_data(mesh);
5008 reader.add_mesh_database("generated:2x2x2", stk::io::READ_MESH);
5009 reader.create_input_mesh();
5010 reader.populate_bulk_data();
5011
5012 std::vector<size_t> counts;
5013 stk::mesh::comm_mesh_counts(mesh, counts);
5014
5015 size_t num_original_nodes = counts[stk::topology::NODE_RANK];
5016
5017 EXPECT_EQ(27u, num_original_nodes);
5018 EXPECT_EQ(8u, counts[stk::topology::ELEMENT_RANK]);
5019
5020 stk::mesh::create_edges(mesh);
5021
5022 stk::mesh::comm_mesh_counts(mesh, counts);
5023 size_t num_edges = counts[stk::topology::EDGE_RANK];
5024 EXPECT_EQ(54u, num_edges);
5025 std::vector<stk::mesh::Entity> newNodes(3*num_edges, stk::mesh::Entity());
5026 size_t new_node_counter = 0;
5027
5028 std::vector<ChildNodeRequest> child_node_requests;
5029
5030 const stk::mesh::BucketVector &buckets = mesh.buckets(stk::topology::EDGE_RANK);
5031
5032 for(size_t b = 0; b < buckets.size(); ++b)
5033 {
5034 const stk::mesh::Bucket &bucket = *buckets[b];
5035 if(!bucket.in_aura())
5036 {
5037 for (size_t e=0;e<bucket.size();++e)
5038 {
5039 const stk::mesh::Entity* pnodes = mesh.begin_nodes(bucket[e]);
5040
5041 int child_index = 0;
5042 {
5043 std::vector<const stk::mesh::Entity*> node_parents;
5044 node_parents.push_back(pnodes);
5045 node_parents.push_back(pnodes+1);
5046 ChildNodeRequest node_request(node_parents, &newNodes[new_node_counter]);
5047 child_index = new_node_counter;
5048 ++new_node_counter;
5049 child_node_requests.push_back(node_request);
5050 }
5051 {
5052 std::vector<const stk::mesh::Entity*> node_parents;
5053 node_parents.push_back(&newNodes[child_index]);
5054 node_parents.push_back(pnodes+1);
5055 ChildNodeRequest node_request(node_parents, &newNodes[new_node_counter]);
5056 ++new_node_counter;
5057 child_node_requests.push_back(node_request);
5058 }
5059 {
5060 std::vector<const stk::mesh::Entity*> node_parents;
5061 node_parents.push_back(pnodes);
5062 node_parents.push_back(&newNodes[child_index]);
5063 ChildNodeRequest node_request(node_parents, &newNodes[new_node_counter]);
5064 ++new_node_counter;
5065 child_node_requests.push_back(node_request);
5066 }
5067 ASSERT_TRUE(new_node_counter <= 3*num_edges);
5068 }
5069 }
5070 }
5071
5072 {
5073 mesh.modification_begin();
5074
5075 batch_create_child_nodes_new(mesh, child_node_requests);
5076
5077 stk::mesh::PartVector add_parts_vec;
5078 stk::mesh::PartVector rem_parts_vec;
5079 add_parts_vec.push_back(&node_part);
5080 for (size_t e=0;e<new_node_counter;++e)
5081 {
5082 mesh.change_entity_parts(newNodes[e], add_parts_vec, rem_parts_vec);
5083 }
5084
5085 ASSERT_NO_THROW(mesh.modification_end());
5086
5087 for(size_t i = 0; i < child_node_requests.size(); ++i)
5088 {
5089 ChildNodeRequest & request = child_node_requests[i];
5090 std::vector<const stk::mesh::Entity*> & request_parents = request.first;
5091 stk::mesh::Entity *new_node = request.second;
5092 set_coords_on_new_node(mesh.mesh_meta_data(), *request_parents[0], *request_parents[1], *new_node);
5093 }
5094 }
5095
5096 stk::mesh::comm_mesh_counts(mesh, counts);
5097
5098 size_t gold_number_nodes = num_original_nodes + 3*num_edges;
5099 EXPECT_EQ(gold_number_nodes, counts[stk::topology::NODE_RANK]);
5100 EXPECT_EQ(8u, counts[stk::topology::ELEMENT_RANK]);
5101
5102 //std::string filename = "test.exo";
5103 //write_mesh(filename, mesh);
5104 }
5105 }
5106
5107
5108 TEST(BulkData, show_API_for_batch_create_child_nodes)
5109 {
5110 unsigned spatialDim = 2;
5111 stk::mesh::MetaData meta(spatialDim);
5112 stk::mesh::Part& elem_part = meta.declare_part_with_topology("triangle", stk::topology::TRIANGLE_3_2D);
5113
5114 meta.commit();
5115
5116 stk::unit_test_util::BulkDataTester bulk(meta, MPI_COMM_WORLD);
5117
5118 if ( bulk.parallel_size() != 2 ) return;
5119
5120 bulk.modification_begin();
5121
5122 stk::mesh::Entity node1 = bulk.declare_node(1);
5123 stk::mesh::Entity node2 = bulk.declare_node(2);
5124
5125 int otherProc = 1-bulk.parallel_rank();
5126 bulk.add_node_sharing(node1, otherProc);
5127 bulk.add_node_sharing(node2, otherProc);
5128
5129 if ( bulk.parallel_rank() == 0 )
5130 {
5131 bulk.declare_node(3);
5132
5133 stk::mesh::EntityIdVector connected_nodes {1, 2, 3 };
5134 stk::mesh::declare_element(bulk, elem_part, 1, connected_nodes);
5135 }
5136 else
5137 {
5138 bulk.declare_node(4);
5139
5140 stk::mesh::EntityIdVector connected_nodes {2, 1, 4 };
5141 stk::mesh::declare_element(bulk, elem_part, 2, connected_nodes);
5142 }
5143
5144 ASSERT_NO_THROW(bulk.modification_end());
5145
5146 std::vector<size_t> counts;
5147 stk::mesh::comm_mesh_counts(bulk, counts);
5148
5149 EXPECT_EQ(4u,counts[stk::topology::NODE_RANK]);
5150 EXPECT_EQ(2u,counts[stk::topology::ELEMENT_RANK]);
5151
5152 ASSERT_TRUE(bulk.bucket(node1).shared());
5153 ASSERT_TRUE(bulk.bucket(node2).shared());
5154
5155 // Nodes 3 and 4 accessible on both processors via aura
5156 stk::mesh::Entity node3 = bulk.get_entity(stk::topology::NODE_RANK, 3);
5157 stk::mesh::Entity node4 = bulk.get_entity(stk::topology::NODE_RANK, 4);
5158
5159 ASSERT_FALSE(bulk.bucket(node3).shared());
5160 ASSERT_FALSE(bulk.bucket(node4).shared());
5161
5162 std::vector<ChildNodeRequest> child_node_requests;
5163
5164 stk::mesh::Entity node5 = stk::mesh::Entity(); // "child" of 1 and 2 is 5
5165 std::vector<const stk::mesh::Entity*> node5_parents;
5166 node5_parents.push_back(&node1);
5167 node5_parents.push_back(&node2);
5168 ChildNodeRequest node5_request(node5_parents, &node5);
5169 child_node_requests.push_back(node5_request);
5170
5171 stk::mesh::Entity node6 = stk::mesh::Entity(); // "child" of 1 and 5 is 6
5172 std::vector<const stk::mesh::Entity*> node6_parents;
5173 node6_parents.push_back(&node1);
5174 node6_parents.push_back(&node5);
5175 ChildNodeRequest node6_request(node6_parents, &node6);
5176 child_node_requests.push_back(node6_request);
5177
5178 // It would be nice if child node creation was processed a batch of requests and didn't have to
5179 // be part of the modification cycle where the elements are attached. Currently, this would throw
5180 // an error because of the shared orphan nodes.
5181 bulk.modification_begin();
5182 batch_create_child_nodes_new(bulk, child_node_requests);
5183
5184 if ( bulk.parallel_rank() == 0 )
5185 {
5186 stk::mesh::EntityIdVector connected_nodes(3);
5187 connected_nodes[0] = 1;
5188 connected_nodes[1] = bulk.identifier(node6);
5189 connected_nodes[2] = 3;
5190 stk::mesh::declare_element(bulk, elem_part, 3, connected_nodes);
5191
5192 connected_nodes[0] = bulk.identifier(node6);
5193 connected_nodes[1] = bulk.identifier(node5);
5194 connected_nodes[2] = 3;
5195 stk::mesh::declare_element(bulk, elem_part, 4, connected_nodes);
5196
5197 connected_nodes[0] = bulk.identifier(node5);
5198 connected_nodes[1] = 2;
5199 connected_nodes[2] = 3;
5200 stk::mesh::declare_element(bulk, elem_part, 5, connected_nodes);
5201 }
5202 else
5203 {
5204 stk::mesh::EntityIdVector connected_nodes(3);
5205 connected_nodes[0] = bulk.identifier(node6);
5206 connected_nodes[1] = 1;
5207 connected_nodes[2] = 4;
5208 stk::mesh::declare_element(bulk, elem_part, 6, connected_nodes);
5209
5210 connected_nodes[0] = bulk.identifier(node5);
5211 connected_nodes[1] = bulk.identifier(node6);
5212 connected_nodes[2] = 4;
5213 stk::mesh::declare_element(bulk, elem_part, 7, connected_nodes);
5214
5215 connected_nodes[0] = 2;
5216 connected_nodes[1] = bulk.identifier(node5);
5217 connected_nodes[2] = 4;
5218 stk::mesh::declare_element(bulk, elem_part, 8, connected_nodes);
5219 }
5220
5221 ASSERT_NO_THROW(bulk.modification_end());
5222
5223
5224 ASSERT_TRUE(bulk.is_valid(node5));
5225 ASSERT_TRUE(bulk.is_valid(node6));
5226 ASSERT_TRUE(bulk.bucket(node5).shared());
5227 ASSERT_TRUE(bulk.bucket(node6).shared());
5228
5229 stk::mesh::comm_mesh_counts(bulk, counts);
5230
5231 EXPECT_EQ(6u,counts[stk::topology::NODE_RANK]);
5232 EXPECT_EQ(8u,counts[stk::topology::ELEMENT_RANK]);
5233
5234 //std::string filename = "test.exo";
5235 //write_mesh(filename, bulk);
5236 }
5237
5238 void Test_STK_ParallelPartConsistency_ChangeBlock(stk::mesh::BulkData::AutomaticAuraOption autoAuraOption)
5239 {
5240 stk::ParallelMachine pm = MPI_COMM_WORLD;
5241 const int parallel_size = stk::parallel_machine_size(pm);
5242 const int parallel_rank = stk::parallel_machine_rank(pm);
5243
5244 if (parallel_size != 2) return;
5245
5246 // This test will create a two-element mesh (quad4 elements)
5247 // in 2 blocks in parallel. Both elements will start out in block_1
5248 // and then one of the elements will be moved to block_2.
5249
5250 unsigned spatialDim = 2;
5251 std::shared_ptr<BulkData> bulkPtr = stk::unit_test_util::build_mesh(spatialDim, pm, autoAuraOption);
5252 stk::mesh::MetaData& meta= bulkPtr->mesh_meta_data();
5253 stk::mesh::BulkData& mesh = *bulkPtr;
5254
5255 //declare 'block_1' which will hold element 1
5256 stk::mesh::Part& block_1 = meta.declare_part("block_1", stk::topology::ELEMENT_RANK);
5257 stk::mesh::set_topology(block_1, stk::topology::QUAD_4_2D);
5258 stk::mesh::Part& block_2 = meta.declare_part("block_2", stk::topology::ELEMENT_RANK);
5259 stk::mesh::set_topology(block_2, stk::topology::QUAD_4_2D);
5260
5261 stk::mesh::Selector all_nodes = meta.universal_part();
5262
5263 //declare a field for coordinates
5264 typedef stk::mesh::Field<double> CoordFieldType;
5265 CoordFieldType& coordField = meta.declare_field<double>(stk::topology::NODE_RANK, "model_coordinates");
5266 stk::mesh::put_field_on_mesh(coordField, all_nodes, 2, nullptr);
5267 stk::mesh::Field<double>& oneField = meta.declare_field<double>(stk::topology::NODE_RANK, "field_of_one");
5268 stk::mesh::put_field_on_mesh(oneField, block_1, nullptr);
5269
5270 meta.commit();
5271 mesh.modification_begin();
5272
5273 const size_t nodesPerElem = 4;
5274 const size_t numNodes = 6;
5275
5276 double xCoords[numNodes] = { -1., 0., 1., 1., 0., -1. };
5277 double yCoords[numNodes] = { 0., 0., 0., 1., 1., 1. };
5278 int elem_nodes0[] = {0, 1, 4, 5};
5279 int elem_nodes1[] = {1, 2, 3, 4};
5280 int * elem_nodes[] = { elem_nodes0, elem_nodes1 };
5281
5282 //Next create nodes and set up connectivity to use later for creating the element.
5283 stk::mesh::EntityIdVector connected_nodes(nodesPerElem);
5284 for(size_t n=0; n<nodesPerElem; ++n) {
5285 size_t e = parallel_rank;
5286 stk::mesh::EntityId nodeGlobalId = elem_nodes[e][n]+1;
5287
5288 stk::mesh::Entity node = mesh.get_entity(stk::topology::NODE_RANK, nodeGlobalId);
5289 if (!mesh.is_valid(node))
5290 {
5291 node = mesh.declare_node(nodeGlobalId);
5292 }
5293
5294 connected_nodes[n] = nodeGlobalId;
5295 }
5296
5297 stk::mesh::Entity node2 = mesh.get_entity(stk::topology::NODE_RANK, 2);
5298 stk::mesh::Entity node5 = mesh.get_entity(stk::topology::NODE_RANK, 5);
5299
5300 int otherProc = 1 - parallel_rank;
5301 mesh.add_node_sharing(node2, otherProc);
5302 mesh.add_node_sharing(node5, otherProc);
5303
5304 std::vector<stk::mesh::Entity> nodes;
5305 stk::mesh::get_entities(mesh, stk::topology::NODE_RANK, nodes);
5306 for (size_t n=0; n<nodes.size(); ++n)
5307 {
5308 stk::mesh::Entity node = nodes[n];
5309 int node_id = mesh.identifier(node);
5310
5311 double* coords = stk::mesh::field_data(coordField, node);
5312 coords[0] = xCoords[node_id-1];
5313 coords[1] = yCoords[node_id-1];
5314 }
5315
5316 //create 1 element per processor
5317 stk::mesh::EntityId elemId = parallel_rank + 1;
5318 stk::mesh::Entity element = stk::mesh::declare_element(mesh, block_1, elemId, connected_nodes );
5319
5320 mesh.modification_end();
5321
5322 stk::mesh::field_fill(1.0, oneField);
5323
5324 EXPECT_TRUE(mesh.is_valid(node2));
5325 EXPECT_TRUE(mesh.is_valid(node5));
5326
5327 // check that shared nodes are members of block_1
5328 EXPECT_TRUE(mesh.bucket(node2).member(block_1));
5329 EXPECT_TRUE(mesh.bucket(node5).member(block_1));
5330
5331 // check that all nodes of block_1 have the correct value
5332 std::vector<stk::mesh::Entity> block_1_nodes;
5333 stk::mesh::get_selected_entities(stk::mesh::Selector(block_1), mesh.buckets( stk::topology::NODE_RANK ), block_1_nodes);
5334 for(size_t n=0; n<block_1_nodes.size(); ++n)
5335 {
5336 double* data_ptr = stk::mesh::field_data(oneField, block_1_nodes[n]);
5337 EXPECT_TRUE(data_ptr != NULL);
5338 EXPECT_DOUBLE_EQ(1.0, *data_ptr);
5339 }
5340
5341 //
5342 // now switch the element on proc0 to block_2
5343 //
5344 mesh.modification_begin();
5345
5346 if (0 == parallel_rank)
5347 {
5348 stk::mesh::PartVector add_parts(1, &block_2);
5349 stk::mesh::PartVector remove_parts(1, &block_1);
5350
5351 element = mesh.get_entity(stk::topology::ELEMENT_RANK, elemId);
5352 mesh.change_entity_parts(element, add_parts, remove_parts);
5353 }
5354
5355 mesh.modification_end();
5356
5357 // check that shared nodes are now members of both blocks
5358 EXPECT_TRUE(mesh.bucket(node2).member(block_1));
5359 EXPECT_TRUE(mesh.bucket(node5).member(block_1));
5360 EXPECT_TRUE(mesh.bucket(node2).member(block_2));
5361 EXPECT_TRUE(mesh.bucket(node5).member(block_2));
5362
5363 // check that all nodes of block_1 have the correct value
5364 stk::mesh::get_selected_entities(stk::mesh::Selector(block_1), mesh.buckets( stk::topology::NODE_RANK ), block_1_nodes);
5365 for(size_t n=0; n<block_1_nodes.size(); ++n)
5366 {
5367 double* data_ptr = stk::mesh::field_data(oneField, block_1_nodes[n]);
5368 EXPECT_TRUE(data_ptr != NULL);
5369 EXPECT_DOUBLE_EQ(1.0, *data_ptr)<<"node "<<mesh.identifier(block_1_nodes[n]);
5370 }
5371 }
5372
5373 TEST(BulkData, STK_ParallelPartConsistency_ChangeBlock_WithAura)
5374 {
5375 Test_STK_ParallelPartConsistency_ChangeBlock(stk::mesh::BulkData::AUTO_AURA);
5376 }
5377
5378 TEST(BulkData, STK_ParallelPartConsistency_ChangeBlock_WithoutAura)
5379 {
5380 Test_STK_ParallelPartConsistency_ChangeBlock(stk::mesh::BulkData::NO_AUTO_AURA);
5381 }
5382
5383 TEST(BulkData, STK_Deimprint)
5384 {
5385 stk::ParallelMachine pm = MPI_COMM_WORLD;
5386 const int parallel_size = stk::parallel_machine_size(pm);
5387
5388 if (parallel_size != 1) return;
5389
5390 // This test will create a two-element mesh (quad4 elements)
5391 // in 2 blocks in parallel. Element 1 is in block_1, element 2
5392 // is in block_2. The part block_2 is a subset of a part that
5393 // has no rank (and is therefore non-imprintable). This test
5394 // examines the behavior of the parts on the shared nodes when
5395 // element 2 is destroyed.
5396
5397 unsigned spatialDim = 2;
5398 std::shared_ptr<BulkData> bulkPtr = stk::unit_test_util::build_mesh(spatialDim, pm);
5399 stk::mesh::MetaData& meta= bulkPtr->mesh_meta_data();
5400 stk::mesh::BulkData& mesh = *bulkPtr;
5401
5402 stk::mesh::Part& block_1 = meta.declare_part_with_topology("block_1", stk::topology::QUAD_4_2D);
5403 stk::mesh::Part& block_2 = meta.declare_part_with_topology("block_2", stk::topology::QUAD_4_2D);
5404 stk::mesh::Part& block_2_superset_with_rank = meta.declare_part("block_2_superset_with_rank", stk::topology::ELEMENT_RANK);
5405 stk::mesh::Part& block_2_superset_without_rank = meta.declare_part("block_2_superset_without_rank");
5406
5407 meta.declare_part_subset(block_2_superset_with_rank, block_2);
5408 meta.declare_part_subset(block_2_superset_without_rank, block_2);
5409
5410 stk::mesh::Selector all_nodes = meta.universal_part();
5411
5412 meta.commit();
5413 mesh.modification_begin();
5414
5415 const size_t nodesPerElem = 4;
5416
5417 stk::mesh::EntityIdVector elem_nodes[] {
5418 {1, 2, 5, 6},
5419 {2, 3, 4, 5}
5420 };
5421
5422 const size_t numElem = 2;
5423
5424 // Create nodes
5425 for (size_t e=0; e<numElem; ++e)
5426 {
5427 for(size_t n=0; n<nodesPerElem; ++n)
5428 {
5429 stk::mesh::EntityId nodeGlobalId = elem_nodes[e][n];
5430
5431 stk::mesh::Entity node = mesh.get_entity(stk::topology::NODE_RANK, nodeGlobalId);
5432 if (!mesh.is_valid(node))
5433 {
5434 node = mesh.declare_node(nodeGlobalId);
5435 }
5436 }
5437 }
5438
5439 stk::mesh::Entity node2 = mesh.get_entity(stk::topology::NODE_RANK, 2);
5440 stk::mesh::Entity node5 = mesh.get_entity(stk::topology::NODE_RANK, 5);
5441
5442 // Create elements
5443 for (size_t e=0; e<numElem; ++e)
5444 {
5445 stk::mesh::Part& block = (e == 0) ? block_1 : block_2;
5446 stk::mesh::declare_element(mesh, block, e+1, elem_nodes[e]);
5447 }
5448
5449 stk::mesh::Entity element1 = mesh.get_entity(stk::topology::ELEMENT_RANK, 1);
5450 stk::mesh::Entity element2 = mesh.get_entity(stk::topology::ELEMENT_RANK, 2);
5451
5452 mesh.modification_end();
5453
5454 EXPECT_TRUE(mesh.is_valid(node2));
5455 EXPECT_TRUE(mesh.is_valid(node5));
5456 EXPECT_TRUE(mesh.is_valid(element1));
5457 EXPECT_TRUE(mesh.is_valid(element2));
5458
5459 // These two flags should definitely be the same. Otherwise, creation and deletion
5460 // are not symmetric, and parts of lower rank entities will be corrupted by mesh modification.
5461 // The code might be simpler and performance might be better if both were false.
5462 const bool stk_induces_unranked_supersets = false;
5463 const bool stk_deinduces_unranked_supersets = false;
5464
5465 // check block membership of shared nodes
5466 EXPECT_TRUE(mesh.bucket(node2).member(block_1));
5467 EXPECT_TRUE(mesh.bucket(node5).member(block_1));
5468 EXPECT_TRUE(mesh.bucket(node2).member(block_2));
5469 EXPECT_TRUE(mesh.bucket(node5).member(block_2));
5470 EXPECT_TRUE(mesh.bucket(node2).member(block_2_superset_with_rank));
5471 EXPECT_TRUE(mesh.bucket(node5).member(block_2_superset_with_rank));
5472 if (stk_induces_unranked_supersets)
5473 {
5474 EXPECT_TRUE(mesh.bucket(node2).member(block_2_superset_without_rank));
5475 EXPECT_TRUE(mesh.bucket(node5).member(block_2_superset_without_rank));
5476 }
5477 else
5478 {
5479 EXPECT_FALSE(mesh.bucket(node2).member(block_2_superset_without_rank));
5480 EXPECT_FALSE(mesh.bucket(node5).member(block_2_superset_without_rank));
5481 }
5482
5483 //
5484 // now delete element 2
5485 //
5486 mesh.modification_begin();
5487
5488 mesh.destroy_entity(element2);
5489
5490 mesh.modification_end();
5491
5492 // check block membership of shared nodes
5493 EXPECT_TRUE(mesh.bucket(node2).member(block_1));
5494 EXPECT_TRUE(mesh.bucket(node5).member(block_1));
5495 EXPECT_FALSE(mesh.bucket(node2).member(block_2));
5496 EXPECT_FALSE(mesh.bucket(node5).member(block_2));
5497 EXPECT_FALSE(mesh.bucket(node2).member(block_2_superset_with_rank));
5498 EXPECT_FALSE(mesh.bucket(node5).member(block_2_superset_with_rank));
5499 if (stk_induces_unranked_supersets && !stk_deinduces_unranked_supersets)
5500 {
5501 EXPECT_TRUE(mesh.bucket(node2).member(block_2_superset_without_rank));
5502 EXPECT_TRUE(mesh.bucket(node5).member(block_2_superset_without_rank));
5503 }
5504 else
5505 {
5506 EXPECT_FALSE(mesh.bucket(node2).member(block_2_superset_without_rank));
5507 EXPECT_FALSE(mesh.bucket(node5).member(block_2_superset_without_rank));
5508 }
5509 }
5510
5511 TEST(BulkData, ChangeAuraElementPart)
5512 {
5513 stk::ParallelMachine pm = MPI_COMM_WORLD;
5514 const int parallel_size = stk::parallel_machine_size(pm);
5515 const int parallel_rank = stk::parallel_machine_rank(pm);
5516
5517 if (parallel_size != 4) return;
5518
5519 // This test will create a four element mesh (quad4 elements)
5520 // in parallel.
5521
5522 /* Mesh
5523 * 7---8---9 P0 owns nodes 1,2,4,5; P, elem 1
5524 * | 3 | 4 | P1 : 3,6, elem 2
5525 * 4---5---6 P2 : 7,8, elem 3
5526 * | 1 | 2 | P3 : 9, elem 4
5527 * 1---2---3
5528 */
5529
5530 unsigned spatialDim = 2;
5531 std::shared_ptr<BulkData> bulkPtr = stk::unit_test_util::build_mesh(spatialDim, pm);
5532 stk::mesh::MetaData& meta= bulkPtr->mesh_meta_data();
5533 stk::mesh::BulkData& mesh = *bulkPtr;
5534
5535 stk::mesh::Part& block_1 = meta.declare_part_with_topology("block_1", stk::topology::QUAD_4_2D);
5536 stk::mesh::Part& block_2 = meta.declare_part_with_topology("block_2", stk::topology::QUAD_4_2D);
5537
5538 stk::mesh::Selector all_nodes = meta.universal_part();
5539
5540 meta.commit();
5541 mesh.modification_begin();
5542
5543 const size_t nodesPerElem = 4;
5544
5545 stk::mesh::EntityIdVector elem_nodes[] {
5546 {1, 2, 5, 4},
5547 {2, 3, 6, 5},
5548 {4, 5, 8, 7},
5549 {5, 6, 9, 8}
5550 };
5551
5552 int node_sharing[9][4] = { {0}, {0,1}, {1}, {0,2}, {0,1,2,3}, {1,3}, {2}, {2,3}, {3} };
5553 int num_node_sharing[] = {1, 2, 1, 2, 4, 2, 1, 2, 1};
5554
5555 // Create nodes
5556 size_t e = parallel_rank;
5557 {
5558 for(size_t n=0; n<nodesPerElem; ++n)
5559 {
5560 stk::mesh::EntityId nodeGlobalId = elem_nodes[e][n];
5561
5562 stk::mesh::Entity node = mesh.get_entity(stk::topology::NODE_RANK, nodeGlobalId);
5563 if (!mesh.is_valid(node))
5564 {
5565 node = mesh.declare_node(nodeGlobalId);
5566 }
5567 }
5568 }
5569
5570 // Create elements
5571 stk::mesh::Entity local_elem = stk::mesh::declare_element(mesh, block_1, e+1, elem_nodes[e]);
5572
5573 // declare node sharing
5574 for(size_t n=0; n<nodesPerElem; ++n)
5575 {
5576 stk::mesh::EntityId nodeGlobalId = elem_nodes[e][n];
5577 stk::mesh::Entity node = mesh.get_entity(stk::topology::NODE_RANK, nodeGlobalId);
5578 const int node_index = nodeGlobalId-1;
5579 for (int share=0; share<num_node_sharing[node_index]; ++share)
5580 {
5581 const int share_proc = node_sharing[node_index][share];
5582 if (parallel_rank != share_proc) mesh.add_node_sharing(node, share_proc);
5583 }
5584 }
5585
5586 mesh.modification_end();
5587
5588 // All nodes appear everywhere via aura
5589 stk::mesh::Entity node5 = mesh.get_entity(stk::topology::NODE_RANK, 5);
5590 stk::mesh::Entity node6 = mesh.get_entity(stk::topology::NODE_RANK, 6);
5591 EXPECT_TRUE(mesh.is_valid(node5));
5592 EXPECT_TRUE(mesh.is_valid(node6));
5593
5594 mesh.modification_begin();
5595 if (parallel_rank == 1 || parallel_rank == 3)
5596 {
5597 stk::mesh::PartVector add_parts(1, &block_2);
5598 stk::mesh::PartVector remove_parts(1, &block_1);
5599 mesh.change_entity_parts(local_elem, add_parts, remove_parts);
5600 }
5601 mesh.modification_end();
5602
5603 stk::mesh::Entity elem2 = mesh.get_entity(stk::topology::ELEMENT_RANK, 2);
5604 stk::mesh::Entity elem4 = mesh.get_entity(stk::topology::ELEMENT_RANK, 4);
5605 EXPECT_TRUE(mesh.is_valid(elem2));
5606 EXPECT_TRUE(mesh.is_valid(elem4));
5607
5608 const bool expect_consistent_parts_on_aura_entities = true;
5609
5610 if (expect_consistent_parts_on_aura_entities)
5611 {
5612 EXPECT_FALSE(mesh.bucket(elem2).member(block_1));
5613 EXPECT_TRUE(mesh.bucket(elem2).member(block_2));
5614 EXPECT_FALSE(mesh.bucket(elem4).member(block_1));
5615 EXPECT_TRUE(mesh.bucket(elem4).member(block_2));
5616
5617 EXPECT_TRUE(mesh.bucket(node5).member(block_1));
5618 EXPECT_TRUE(mesh.bucket(node5).member(block_2));
5619 EXPECT_FALSE(mesh.bucket(node6).member(block_1));
5620 EXPECT_TRUE(mesh.bucket(node6).member(block_2));
5621 }
5622 }
5623
5624 TEST(BulkData, generate_new_ids)
5625 {
5626 MPI_Comm communicator = MPI_COMM_WORLD;
5627 int psize = stk::parallel_machine_size(communicator);
5628
5629 stk::io::StkMeshIoBroker stkMeshIoBroker(communicator);
5630 std::ostringstream os;
5631 os << "generated:10x10x" << psize;
5632 const std::string generatedMeshSpec = os.str();
5633 stkMeshIoBroker.add_mesh_database(generatedMeshSpec, stk::io::READ_MESH);
5634 stkMeshIoBroker.create_input_mesh();
5635 stkMeshIoBroker.populate_bulk_data();
5636
5637 stk::mesh::BulkData &stkMeshBulkData = stkMeshIoBroker.bulk_data();
5638 std::vector<size_t> counts;
5639 stk::mesh::comm_mesh_counts(stkMeshBulkData, counts);
5640
5641 std::vector<stk::mesh::EntityId> requestedIds;
5642 size_t numIdsNeeded = counts[stk::topology::NODE_RANK] / 10 + 1;
5643 ASSERT_TRUE(numIdsNeeded>0);
5644 stkMeshBulkData.generate_new_ids(stk::topology::NODE_RANK, numIdsNeeded, requestedIds);
5645
5646 std::sort(requestedIds.begin(), requestedIds.end());
5647 std::vector<stk::mesh::EntityId>::iterator iter = std::unique(requestedIds.begin(), requestedIds.end());
5648
5649 bool ids_better_be_unique_on_this_proc = ( iter == requestedIds.end() );
5650 ASSERT_TRUE(ids_better_be_unique_on_this_proc);
5651
5652 stk::CommSparse comm(stkMeshBulkData.parallel());
5653
5654 for(int phase = 0; phase < 2; ++phase)
5655 {
5656 for(int i = 0; i < stkMeshBulkData.parallel_size(); ++i)
5657 {
5658 if(i != stkMeshBulkData.parallel_rank())
5659 {
5660 for(size_t j = 0; j < requestedIds.size(); ++j)
5661 {
5662 comm.send_buffer(i).pack<stk::mesh::EntityId>(requestedIds[j]);
5663 }
5664 }
5665 }
5666
5667 if(phase == 0)
5668 {
5669 comm.allocate_buffers();
5670 }
5671 else
5672 {
5673 comm.communicate();
5674 }
5675 }
5676
5677 for(int i = 0; i < stkMeshBulkData.parallel_size(); ++i)
5678 {
5679 if(i != stkMeshBulkData.parallel_rank())
5680 {
5681 while(comm.recv_buffer(i).remaining())
5682 {
5683 stk::mesh::EntityId entity_id;
5684 comm.recv_buffer(i).unpack<stk::mesh::EntityId>(entity_id);
5685 bool is_other_procs_id_on_this_proc = std::binary_search(requestedIds.begin(), requestedIds.end(), entity_id);
5686 ASSERT_FALSE(is_other_procs_id_on_this_proc);
5687 }
5688 }
5689 }
5690
5691 }
5692
5693 TEST(BulkData, test_generate_new_entities)
5694 {
5695 MPI_Comm communicator = MPI_COMM_WORLD;
5696 int psize = stk::parallel_machine_size(communicator);
5697
5698 stk::io::StkMeshIoBroker stkMeshIoBroker(communicator);
5699 std::ostringstream os;
5700 os << "generated:10x10x" << psize;
5701 const std::string generatedMeshSpec = os.str();
5702 stkMeshIoBroker.add_mesh_database(generatedMeshSpec, stk::io::READ_MESH);
5703 stkMeshIoBroker.create_input_mesh();
5704 stkMeshIoBroker.populate_bulk_data();
5705
5706 stk::mesh::BulkData &stkMeshBulkData = stkMeshIoBroker.bulk_data();
5707 std::vector<size_t> counts;
5708 stk::mesh::comm_mesh_counts(stkMeshBulkData, counts);
5709
5710 std::vector<stk::mesh::Entity> requestedEntities;
5711 size_t numIdsNeeded = counts[stk::topology::NODE_RANK] / 10 + 1;
5712 ASSERT_TRUE(numIdsNeeded>0);
5713
5714 std::vector<size_t> requests(stkMeshBulkData.mesh_meta_data().entity_rank_count(),0);
5715 requests[0] = numIdsNeeded;
5716
5717 stkMeshBulkData.modification_begin();
5718 stkMeshBulkData.generate_new_entities(requests, requestedEntities);
5719 stkMeshBulkData.modification_end();
5720
5721 stk::CommSparse comm(stkMeshBulkData.parallel());
5722
5723 for(int phase = 0; phase < 2; ++phase)
5724 {
5725 for(int i = 0; i < stkMeshBulkData.parallel_size(); ++i)
5726 {
5727 if(i != stkMeshBulkData.parallel_rank())
5728 {
5729 for(size_t j = 0; j < requestedEntities.size(); ++j)
5730 {
5731 comm.send_buffer(i).pack<stk::mesh::EntityKey>(stkMeshBulkData.entity_key(requestedEntities[j]));
5732 }
5733 }
5734 }
5735
5736 if(phase == 0)
5737 {
5738 comm.allocate_buffers();
5739 }
5740 else
5741 {
5742 comm.communicate();
5743 }
5744 }
5745
5746 for(int i = 0; i < stkMeshBulkData.parallel_size(); ++i)
5747 {
5748 if(i != stkMeshBulkData.parallel_rank())
5749 {
5750 while(comm.recv_buffer(i).remaining())
5751 {
5752 stk::mesh::EntityKey key;
5753 comm.recv_buffer(i).unpack<stk::mesh::EntityKey>(key);
5754 ASSERT_FALSE(stkMeshBulkData.is_valid(stkMeshBulkData.get_entity(key)));
5755 }
5756 }
5757 }
5758
5759 }
5760
5761 TEST(BulkData, test_destroy_ghosted_entity_then_create_locally_owned_entity_with_same_identifier)
5762 {
5763 MPI_Comm communicator = MPI_COMM_WORLD;
5764 int psize = stk::parallel_machine_size(communicator);
5765
5766 if ( psize == 2 )
5767 {
5768 stk::io::StkMeshIoBroker stkMeshIoBroker(communicator);
5769 std::ostringstream os;
5770 os << "generated:2x2x2";
5771 const std::string generatedMeshSpec = os.str();
5772 stkMeshIoBroker.add_mesh_database(generatedMeshSpec, stk::io::READ_MESH);
5773 stkMeshIoBroker.create_input_mesh();
5774 stkMeshIoBroker.populate_bulk_data();
5775
5776 stk::mesh::BulkData &stkMeshBulkData = stkMeshIoBroker.bulk_data();
5777
5778 stk::mesh::EntityKey key(stk::topology::ELEMENT_RANK,3);
5779
5780 stkMeshBulkData.modification_begin();
5781 stkMeshBulkData.destroy_entity(stkMeshBulkData.get_entity(key), false);
5782 stkMeshBulkData.modification_end();
5783
5784 stk::mesh::Part *part = stkMeshBulkData.mesh_meta_data().get_part("block_1");
5785
5786 stkMeshBulkData.modification_begin();
5787 if ( stkMeshBulkData.parallel_rank() == 1 )
5788 {
5789 stk::mesh::Entity element = stkMeshBulkData.declare_element(3, stk::mesh::ConstPartVector{part});
5790 stk::mesh::EntityId nodes[] = { 100, 101, 102, 103, 104, 105, 106, 107 };
5791 const int num_nodes = 8;
5792 for (int i=0;i<num_nodes;++i)
5793 {
5794 stk::mesh::Entity node = stkMeshBulkData.declare_node(nodes[i]);
5795 stkMeshBulkData.declare_relation(element, node, i);
5796 }
5797 }
5798 stkMeshBulkData.modification_end();
5799
5800 stkMeshBulkData.modification_begin();
5801 if ( stkMeshBulkData.parallel_rank() == 1 )
5802 {
5803 EXPECT_NO_THROW(stkMeshBulkData.destroy_entity(stkMeshBulkData.get_entity(key), false));
5804 }
5805 stkMeshBulkData.modification_end();
5806
5807 }
5808 }
5809
5810 TEST(FaceCreation, test_face_creation_2Hexes_2procs)
5811 {
5812 int numProcs = stk::parallel_machine_size(MPI_COMM_WORLD);
5813 if (numProcs==2)
5814 {
5815 stk::mesh::MetaData meta(3);
5816 stk::unit_test_util::BulkDataFaceSharingTester mesh(meta, MPI_COMM_WORLD);
5817
5818 stk::mesh::fixtures::HexFixture::fill_mesh(1,1,2, mesh);
5819
5820 int procId = stk::parallel_machine_rank(MPI_COMM_WORLD);
5821
5822 unsigned elem_id = procId+1;
5823
5824 stk::mesh::Entity elem = mesh.get_entity(stk::topology::ELEM_RANK, elem_id);
5825
5826 unsigned face_node_ids[] = { 5, 6, 8, 7 };
5827 size_t num_nodes_on_entity = 4;
5828 stk::mesh::EntityVector nodes(num_nodes_on_entity);
5829 if (procId==0)
5830 {
5831 for(size_t n = 0; n < num_nodes_on_entity; ++n)
5832 {
5833 nodes[n] = mesh.get_entity(stk::topology::NODE_RANK, face_node_ids[n]);
5834 }
5835 }
5836 else
5837 {
5838 for(size_t n = 0; n < num_nodes_on_entity; ++n)
5839 {
5840 unsigned index = num_nodes_on_entity - n - 1;
5841 nodes[n] = mesh.get_entity(stk::topology::NODE_RANK, face_node_ids[index]);
5842 }
5843 }
5844
5845 mesh.modification_begin();
5846
5847 stk::mesh::Entity side = stk::unit_test_util::declare_element_side_with_nodes(mesh, elem, nodes, 1+procId, meta.get_topology_root_part(stk::topology::QUAD_4));
5848
5849 EXPECT_TRUE(mesh.is_valid(side));
5850
5851 std::vector<size_t> counts;
5852 stk::mesh::comm_mesh_counts(mesh, counts);
5853 EXPECT_EQ(2u, counts[stk::topology::FACE_RANK]);
5854
5855 std::vector<stk::mesh::shared_entity_type> potentially_shared_sides;
5856 mesh.my_markEntitiesForResolvingSharingInfoUsingNodes(stk::topology::FACE_RANK, potentially_shared_sides);
5857
5858 ASSERT_EQ(1u, potentially_shared_sides.size());
5859
5860 std::sort(potentially_shared_sides.begin(), potentially_shared_sides.end());
5861
5862 EXPECT_EQ(side, potentially_shared_sides[0].entity);
5863
5864 std::vector<std::vector<stk::mesh::shared_entity_type> > shared_entities_by_proc(mesh.parallel_size());
5865 mesh.my_fillSharedEntities(potentially_shared_sides, shared_entities_by_proc);
5866
5867 int otherProc = 1 - procId;
5868 EXPECT_TRUE(shared_entities_by_proc[procId].empty());
5869 EXPECT_EQ(1u, shared_entities_by_proc[otherProc].size());
5870
5871 stk::CommSparse comm(mesh.parallel());
5872 stk::mesh::impl::communicate_shared_entity_info(mesh, comm, shared_entities_by_proc);
5873 mesh.my_unpackEntityInfromFromOtherProcsAndMarkEntitiesAsSharedAndTrackProcessorsThatNeedAlsoHaveEntity(comm, potentially_shared_sides);
5874
5875 EXPECT_TRUE(mesh.my_internal_is_entity_marked(side) == stk::mesh::BulkData::IS_SHARED);
5876
5877 mesh.change_entity_key_and_update_sharing_info(potentially_shared_sides);
5878
5879 mesh.my_modification_end_for_entity_creation({stk::topology::FACE_RANK});
5880
5881 stk::mesh::comm_mesh_counts(mesh, counts);
5882 EXPECT_EQ(1u, counts[stk::topology::FACE_RANK]);
5883 }
5884 }
5885
5886 //
5887 TEST(BulkData, test_parallel_entity_sharing)
5888 {
5889
5890 stk::mesh::Entity entity;
5891 stk::mesh::EntityKey quad(stk::mesh::EntityKey(stk::topology::ELEM_RANK, 1));
5892
5893 entity.set_local_offset(1);
5894 size_t num_nodes_on_entity = 4;
5895 std::vector<stk::mesh::EntityKey> keys;
5896 keys.push_back(stk::mesh::EntityKey(stk::topology::NODE_RANK, 1));
5897 keys.push_back(stk::mesh::EntityKey(stk::topology::NODE_RANK, 2));
5898 keys.push_back(stk::mesh::EntityKey(stk::topology::NODE_RANK, 3));
5899 keys.push_back(stk::mesh::EntityKey(stk::topology::NODE_RANK, 4));
5900
5901 stk::topology topo = stk::topology::QUAD_4;
5902
5903 stk::mesh::shared_entity_type sentity(quad, entity, topo);
5904 sentity.nodes.resize(num_nodes_on_entity);
5905 for(size_t n = 0; n < num_nodes_on_entity; ++n)
5906 {
5907 sentity.nodes[n]=keys[n];
5908 }
5909
5910 std::vector<stk::mesh::shared_entity_type> shared_entity_map;
5911 shared_entity_map.push_back(sentity);
5912
5913 stk::mesh::shared_entity_type entity_from_other_proc(quad, entity, topo);
5914
5915 entity_from_other_proc.nodes.resize(num_nodes_on_entity);
5916 for(size_t n = 0; n < num_nodes_on_entity; ++n)
5917 {
5918 int index = num_nodes_on_entity - n - 1;
5919 entity_from_other_proc.nodes[n]=keys[index];
5920 }
5921
5922 int matching_index = stk::unit_test_util::does_entity_exist_in_list(shared_entity_map, entity_from_other_proc);
5923 EXPECT_TRUE(matching_index >= 0);
5924 }
5925
5926 TEST(BulkData, makeElementWithConflictingTopologies)
5927 {
5928 stk::ParallelMachine pm = MPI_COMM_WORLD;
5929 const int p_size = stk::parallel_machine_size(pm);
5930
5931 if(p_size != 1)
5932 {
5933 return;
5934 }
5935 const int spatial_dimension = 2;
5936 std::shared_ptr<BulkData> bulkPtr = stk::unit_test_util::build_mesh(spatial_dimension, pm);
5937 stk::mesh::MetaData& meta= bulkPtr->mesh_meta_data();
5938 stk::mesh::BulkData& mesh = *bulkPtr;
5939
5940 stk::mesh::EntityId element_ids[1] = {1};
5941 stk::mesh::EntityIdVector elem_node_ids[] { {1, 2, 3, 4} };
5942
5943 stk::mesh::Part * quad_part = &meta.declare_part_with_topology("quad_part", stk::topology::QUAD_4_2D);
5944 stk::mesh::Part * tri_part = &meta.declare_part_with_topology( "tri_part", stk::topology::TRI_3_2D);
5945 meta.commit();
5946
5947 stk::mesh::PartVector parts;
5948 parts.push_back(quad_part);
5949 parts.push_back(tri_part);
5950
5951 mesh.modification_begin();
5952
5953 EXPECT_THROW(stk::mesh::declare_element(mesh, parts, element_ids[0], elem_node_ids[0]), std::logic_error);
5954
5955 mesh.modification_end();
5956 }
5957
5958 TEST( BulkData, AddSharedNodesInTwoSteps)
5959 {
5960 stk::ParallelMachine pm = MPI_COMM_WORLD;
5961 unsigned p_size = stk::parallel_machine_size(pm);
5962 unsigned p_rank = stk::parallel_machine_rank(pm);
5963
5964 if(p_size != 3u) {
5965 return;
5966 }
5967
5968 stk::mesh::EntityId nodeId = 1;
5969 const unsigned spatialDim = 3;
5970 std::shared_ptr<BulkData> bulkPtr = stk::unit_test_util::build_mesh(spatialDim, pm);
5971 stk::mesh::MetaData& meta= bulkPtr->mesh_meta_data();
5972 stk::mesh::BulkData& mesh = *bulkPtr;
5973
5974 meta.commit();
5975
5976 mesh.modification_begin();
5977 Entity node;
5978 if (0 == p_rank || 1 == p_rank) {
5979 node = mesh.declare_node(nodeId);
5980 }
5981 if (0 == p_rank) {
5982 mesh.add_node_sharing(node, 1);
5983 }
5984 if (1 == p_rank) {
5985 mesh.add_node_sharing(node, 0);
5986 }
5987 mesh.modification_end();
5988
5989
5990
5991 mesh.modification_begin();
5992 if (2 == p_rank) {
5993 node = mesh.declare_node(nodeId);
5994 }
5995 if (0 == p_rank) {
5996 mesh.add_node_sharing(node, 2);
5997 }
5998 if (1 == p_rank) {
5999 mesh.add_node_sharing(node, 2);
6000 }
6001 if (2 == p_rank) {
6002 mesh.add_node_sharing(node, 0);
6003 mesh.add_node_sharing(node, 1);
6004 }
6005
6006 // EXPECT_THROW(mesh.modification_end(), std::logic_error);
6007 //this only throws on processor 2, but not in a parallel consistent way
6008 //this is because you apparently can't create the node on a new processor where it didn't exist before
6009
6010 }
6011
6012 TEST(ChangeEntityId, test_throw_on_shared_node)
6013 {
6014 if (stk::parallel_machine_size(MPI_COMM_WORLD)!=2) { GTEST_SKIP(); }
6015
6016 const unsigned spatialDim = 3;
6017 std::shared_ptr<BulkData> bulkPtr = stk::unit_test_util::build_mesh(spatialDim, MPI_COMM_WORLD);
6018 stk::mesh::BulkData& mesh = *bulkPtr;
6019
6020 stk::mesh::fixtures::HexFixture::fill_mesh(1,1,2, mesh);
6021
6022 stk::mesh::Entity sharedNode5 = mesh.get_entity(stk::topology::NODE_RANK, 5);
6023
6024 EXPECT_TRUE(mesh.bucket(sharedNode5).shared());
6025
6026 mesh.modification_begin();
6027
6028 EXPECT_THROW(mesh.change_entity_id(99, sharedNode5), std::logic_error);
6029
6030 mesh.modification_end();
6031 }
6032
6033 TEST(AmbiguousTopology, hexRedefinedAsShell)
6034 {
6035 if (stk::parallel_machine_size(MPI_COMM_WORLD)!=1) { GTEST_SKIP(); }
6036
6037 const unsigned spatialDim = 3;
6038 std::shared_ptr<BulkData> bulkPtr = stk::unit_test_util::build_mesh(spatialDim, MPI_COMM_WORLD);
6039 stk::mesh::MetaData& meta= bulkPtr->mesh_meta_data();
6040 stk::mesh::BulkData& mesh = *bulkPtr;
6041
6042 stk::mesh::fixtures::HexFixture::fill_mesh(1,1,1, mesh);
6043
6044 stk::mesh::Part& shellPart = meta.get_topology_root_part(stk::topology::SHELL_QUAD_4);
6045 stk::mesh::EntityId elemId = 1;
6046 mesh.modification_begin();
6047 ASSERT_THROW(mesh.declare_element(elemId, stk::mesh::ConstPartVector{&shellPart}), std::runtime_error);
6048 }
6049
6050 }// empty namespace
6051