Debian Codegraph @

packages/stk/stk_unit_tests/stk_mesh/UnitTestAuraToShared.cpp

raw
1 // Copyright 2002 - 2008, 2010, 2011 National Technology Engineering
2 // Solutions of Sandia, LLC (NTESS). Under the terms of Contract
3 // DE-NA0003525 with NTESS, the U.S. Government retains certain rights
4 // in this software.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
8 // met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 //
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following
15 // disclaimer in the documentation and/or other materials provided
16 // with the distribution.
17 //
18 // * Neither the name of NTESS nor the names of its contributors
19 // may be used to endorse or promote products derived from this
20 // software without specific prior written permission.
21 //
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 //
34
35 #include <gtest/gtest.h> // for AssertHelper, EXPECT_TRUE, etc
36 #include <stk_util/parallel/Parallel.hpp>
37 #include <stk_unit_test_utils/MeshFixture.hpp>
38 #include "stk_mesh/base/Types.hpp" // for PartVector, EntityId, etc
39 #include "stk_mesh/base/MetaData.hpp" // for MetaData
40 #include <stk_mesh/base/BulkData.hpp> // for BulkData
41 #include <stk_mesh/baseImpl/elementGraph/ElemElemGraph.hpp> // for BulkData
42 #include <stk_mesh/base/GetEntities.hpp>
43 #include <stk_mesh/base/FEMHelpers.hpp> // for declare_element
44 #include "stk_mesh/base/Bucket.hpp" // for Bucket
45 #include "stk_mesh/base/Entity.hpp" // for Entity
46 #include "stk_mesh/base/Comm.hpp"
47 #include "stk_mesh/base/DestroyElements.hpp"
48 #include "stk_topology/topology.hpp" // for topology, etc
49 #include "stk_io/FillMesh.hpp"
50 #include "stk_mesh/base/MeshBuilder.hpp"
51 #include "stk_unit_test_utils/BuildMesh.hpp"
52 #include "UnitTestTextMeshFixture.hpp"
53
54 namespace
55 {
56 using stk::unit_test_util::build_mesh;
57
58 void verify_local_num_aura_entities(const stk::mesh::BulkData& mesh, stk::mesh::EntityRank rank, unsigned expectedNumAuraEntities)
59 {
60 stk::mesh::EntityVector auraEntities;
61 stk::mesh::get_entities(mesh, rank, mesh.mesh_meta_data().aura_part(), auraEntities);
62 EXPECT_EQ(expectedNumAuraEntities, auraEntities.size());
63 }
64
65 class TestAura2D : public TestTextMeshAura2d
66 {
67 public:
68 TestAura2D() {}
69
70 void delete_elem5_on_p1()
71 {
72 get_bulk().modification_begin();
73
74 if (get_bulk().parallel_rank() == 1) {
75 stk::mesh::Entity elem5 = get_bulk().get_entity(stk::topology::ELEM_RANK,5);
76 ASSERT_TRUE(get_bulk().is_valid(elem5));
77 EXPECT_TRUE(get_bulk().destroy_entity(elem5));
78 }
79
80 get_bulk().modification_end();
81 }
82
83 void destroy_elems(const std::vector<std::pair<int,stk::mesh::EntityId>>& procElemIds)
84 {
85 stk::mesh::EntityVector elemsToDestroy;
86 for(const std::pair<int,stk::mesh::EntityId>& procElemId : procElemIds) {
87 if (procElemId.first == get_bulk().parallel_rank()) {
88 stk::mesh::Entity elem = get_bulk().get_entity(stk::topology::ELEM_RANK, procElemId.second);
89 ASSERT_TRUE(get_bulk().is_valid(elem));
90 elemsToDestroy.push_back(elem);
91 }
92 }
93
94 get_bulk().modification_begin();
95 stk::mesh::destroy_elements_no_mod_cycle(get_bulk(), elemsToDestroy, get_meta().universal_part());
96 get_bulk().modification_end();
97 }
98 };
99
100 TEST_F(TestAura2D, quadKeyhole)
101 {
102 if (get_parallel_size() != 2) { GTEST_SKIP(); }
103
104 std::string meshDesc = "0, 1, QUAD_4_2D, 1,2,6,5\n"
105 "0, 2, QUAD_4_2D, 2,3,7,6\n"
106 "0, 3, QUAD_4_2D, 3,4,8,7\n"
107 "1, 4, QUAD_4_2D, 5,6,10,9\n"
108 "1, 5, QUAD_4_2D, 6,7,11,10\n"
109 "1, 6, QUAD_4_2D, 7,8,12,11\n"
110 "1, 7, QUAD_4_2D, 9,10,14,13\n"
111 "1, 8, QUAD_4_2D, 10,11,15,14\n"
112 "1, 9, QUAD_4_2D, 11,12,16,15\n";
113
114 setup_text_mesh(meshDesc);
115
116 delete_elem5_on_p1();
117
118 if (get_bulk().parallel_rank() == 0) {
119 verify_local_num_aura_entities(get_bulk(), stk::topology::ELEM_RANK, 2);
120 verify_local_num_aura_entities(get_bulk(), stk::topology::NODE_RANK, 4);
121 }
122 else {
123 verify_local_num_aura_entities(get_bulk(), stk::topology::ELEM_RANK, 3);
124 verify_local_num_aura_entities(get_bulk(), stk::topology::NODE_RANK, 4);
125 }
126 }
127
128 TEST_F(TestAura2D, triSharingGhosting)
129 {
130 if (get_parallel_size() != 4) { GTEST_SKIP(); }
131
132 std::string meshDesc = "0, 1, TRI_3_2D, 1,2,3\n"
133 "0, 2, TRI_3_2D, 3,2,4\n"
134 "0, 8, TRI_3_2D, 3,1,10\n"
135 "1, 3, TRI_3_2D, 2,5,4\n"
136 "1, 4, TRI_3_2D, 2,6,5\n"
137 "1, 9, TRI_3_2D, 4,5,11\n"
138 "2, 5, TRI_3_2D, 2,7,6\n"
139 "2, 6, TRI_3_2D, 1,7,2\n"
140 "3, 7, TRI_3_2D, 6,8,5|sideset:data=3,1,4,1,5,1\n";
141
142 //This test provides coverage for some dark corners of BulkData::modification_end.
143 //Specifically, it sets up a combination of sharing and aura-ghosting such that
144 //proc 3 has an aura-ghost of node 2, and doesn't know that procs 1 and 2 share
145 //that node. Correspondingly, procs 1 and 2 know about each other, but don't know
146 //that proc 3 has a recv-ghost of node 2.
147 //The handling of this type of situation will get easier if/when we make ghosting
148 //info symmetric in BulkData.
149
150 setup_text_mesh(meshDesc);
151
152 destroy_elems({ {0,8}, {1,9} });
153 }
154
155 class AuraToSharedToAura : public TestTextMeshAura2d
156 {
157 public:
158 AuraToSharedToAura() {}
159
160 void create_elem3_p1_and_delete_elem1_p0()
161 {
162 const stk::mesh::MetaData& meta = get_meta();
163 stk::mesh::PartVector triParts = {&meta.get_topology_root_part(stk::topology::TRI_3_2D)};
164
165 const stk::mesh::EntityId elemId = 3;
166 stk::mesh::EntityIdVector nodeIds = {1,2,4};
167
168 const int otherProc = 1 - get_bulk().parallel_rank();
169 stk::mesh::Entity node1 = get_bulk().get_entity(stk::topology::NODE_RANK, 1);
170
171 EXPECT_TRUE(get_bulk().is_valid(node1));
172 EXPECT_TRUE(get_bulk().in_ghost(get_bulk().aura_ghosting(), node1));
173 EXPECT_TRUE(0 == get_bulk().parallel_owner_rank(node1));
174
175 get_bulk().modification_begin();
176
177 get_bulk().add_node_sharing(node1, otherProc);
178
179 if (get_bulk().parallel_rank() == 1) {
180 stk::mesh::declare_element(get_bulk(), triParts, elemId, nodeIds);
181 }
182
183 bool destroyedOwnedNode = false;
184 if (get_bulk().parallel_rank() == 0) {
185 stk::mesh::Entity elem1 = get_bulk().get_entity(stk::topology::ELEM_RANK, 1);
186 get_bulk().destroy_entity(elem1);
187 destroyedOwnedNode = get_bulk().destroy_entity(node1);
188 }
189
190 get_bulk().modification_end();
191
192 if (destroyedOwnedNode) {
193 //On P0, have to re-get node1 since it was owned, then destroyed,
194 //now exists again locally as a recv-aura node.
195 node1 = get_bulk().get_entity(stk::topology::NODE_RANK, 1);
196 }
197 else {
198 //On P1, node1 is still valid: it transitioned from recv-aura to
199 //shared, to owned send-aura
200 }
201 EXPECT_TRUE(get_bulk().is_valid(node1));
202 EXPECT_TRUE(get_bulk().in_ghost(get_bulk().aura_ghosting(), node1));
203 EXPECT_FALSE(get_bulk().in_shared(node1));
204 EXPECT_TRUE(1 == get_bulk().parallel_owner_rank(node1));
205
206 const stk::mesh::ElemElemGraph& eeGraph = get_bulk().get_face_adjacent_element_graph();
207
208 stk::mesh::Entity elem3 = get_bulk().get_entity(stk::topology::ELEM_RANK, elemId);
209 ASSERT_TRUE(get_bulk().is_valid(elem3));
210
211 if (get_bulk().parallel_rank() == 0) {
212 EXPECT_TRUE(get_bulk().bucket(elem3).in_aura());
213 constexpr bool requireValidId = false;
214 stk::mesh::impl::LocalId elem3LocalId = eeGraph.get_local_element_id(elem3, requireValidId);
215 EXPECT_EQ(stk::mesh::impl::INVALID_LOCAL_ID, elem3LocalId);
216 }
217
218 if (get_bulk().parallel_rank() == 1) {
219 EXPECT_TRUE(get_bulk().bucket(elem3).owned());
220 constexpr bool requireValidId = true;
221 stk::mesh::Entity elem2 = get_bulk().get_entity(stk::topology::ELEM_RANK, 2);
222 stk::mesh::impl::LocalId elem2LocalId = eeGraph.get_local_element_id(elem2, requireValidId);
223 stk::mesh::impl::LocalId elem3LocalId = eeGraph.get_local_element_id(elem3, requireValidId);
224 constexpr size_t numConnectedElems = 1;
225 EXPECT_EQ(numConnectedElems, eeGraph.get_num_connected_elems(elem3));
226 stk::mesh::GraphEdgesForElement graphEdges = eeGraph.get_edges_for_element(elem3LocalId);
227 ASSERT_EQ(1u, graphEdges.size());
228 const stk::mesh::GraphEdge& graphEdge = graphEdges[0];
229 EXPECT_EQ(elem3LocalId, graphEdge.elem1());
230 EXPECT_EQ(elem2LocalId, graphEdge.elem2());
231 }
232 }
233 };
234
235 TEST_F(AuraToSharedToAura, makeAuraNodeSharedThenDelete)
236 {
237 if (get_parallel_size() != 2) { GTEST_SKIP(); }
238
239 std::string meshDesc = "0, 1, TRI_3_2D, 1,3,2\n"
240 "1, 2, TRI_3_2D, 2,3,4\n";
241 setup_text_mesh(meshDesc);
242 create_elem3_p1_and_delete_elem1_p0();
243 }
244
245 class Aura2DTri : public TestTextMeshAura2d
246 {
247 public:
248 Aura2DTri() {}
249
250 void disconnect_node3_on_p1()
251 {
252 get_bulk().modification_begin();
253
254 if (get_bulk().parallel_rank() == 1) {
255 stk::mesh::Entity node3 = get_bulk().get_entity(stk::topology::NODE_RANK, 3);
256 ASSERT_TRUE(get_bulk().is_valid(node3));
257
258 stk::mesh::Entity node6 = get_bulk().declare_node(6);
259 stk::mesh::ConnectivityOrdinal ord = 0;
260
261 stk::mesh::Entity elem2 = get_bulk().get_entity(stk::topology::ELEM_RANK, 2);
262 ASSERT_TRUE(get_bulk().is_valid(elem2));
263
264 EXPECT_TRUE(get_bulk().destroy_relation(elem2, node3, ord));
265 get_bulk().declare_relation(elem2, node6, ord);
266 }
267 get_bulk().modification_end();
268 }
269 };
270
271 TEST_F(Aura2DTri, destroyRelation_correctAura)
272 {
273 if (get_parallel_size() != 2) { GTEST_SKIP(); }
274
275 std::string meshDesc = "0, 1, TRI_3_2D, 1,2,3\n"
276 "1, 2, TRI_3_2D, 3,4,5\n";
277 setup_text_mesh(meshDesc);
278
279 verify_local_num_aura_entities(get_bulk(), stk::topology::ELEM_RANK, 1);
280 verify_local_num_aura_entities(get_bulk(), stk::topology::NODE_RANK, 2);
281
282 disconnect_node3_on_p1();
283
284 verify_local_num_aura_entities(get_bulk(), stk::topology::ELEM_RANK, 0);
285 verify_local_num_aura_entities(get_bulk(), stk::topology::NODE_RANK, 0);
286 }
287
288 class Aura2DTri4Procs : public TestTextMeshAura2d
289 {
290 public:
291 Aura2DTri4Procs() {}
292
293 void p1_starts_sharing_node2()
294 {
295 get_bulk().modification_begin();
296
297 if (get_bulk().parallel_rank() == 1) {
298 stk::mesh::Entity elem4 = get_bulk().get_entity(stk::topology::ELEM_RANK, 4);
299 stk::mesh::Entity node8 = get_bulk().get_entity(stk::topology::NODE_RANK, 8);
300 stk::mesh::Entity node2 = get_bulk().get_entity(stk::topology::NODE_RANK, 2);
301 stk::mesh::ConnectivityOrdinal ord = 2;
302 get_bulk().destroy_relation(elem4, node8, ord);
303 get_bulk().declare_relation(elem4, node2, ord);
304 }
305
306 get_bulk().modification_end();
307 }
308
309 void p2_starts_sharing_node2()
310 {
311 get_bulk().modification_begin();
312
313 if (get_bulk().parallel_rank() == 2) {
314 stk::mesh::Entity elem4 = get_bulk().get_entity(stk::topology::ELEM_RANK, 4);
315 stk::mesh::Entity node10 = get_bulk().get_entity(stk::topology::NODE_RANK, 10);
316 stk::mesh::Entity node2 = get_bulk().get_entity(stk::topology::NODE_RANK, 2);
317 stk::mesh::ConnectivityOrdinal ord = 2;
318 get_bulk().destroy_relation(elem4, node10, ord);
319 get_bulk().declare_relation(elem4, node2, ord);
320 }
321
322 get_bulk().modification_end();
323 }
324 };
325
326 TEST_F(Aura2DTri4Procs, addSharerToAlreadySharedAuraNode)
327 {
328 if (get_parallel_size() != 4) { GTEST_SKIP(); }
329
330 std::string meshDesc = "0, 1, TRI_3_2D, 1,2,3\n"
331 "1, 3, TRI_3_2D, 1,6,7\n"
332 "1, 4, TRI_3_2D, 1,7,8\n"
333 "2, 5, TRI_3_2D, 9,10,4\n"
334 "3, 2, TRI_3_2D, 2,4,5\n";
335 setup_text_mesh(meshDesc);
336
337 stk::mesh::Entity node2 = get_bulk().get_entity(stk::topology::NODE_RANK, 2);
338 EXPECT_TRUE(get_bulk().is_valid(node2));
339 const int proc = get_bulk().parallel_rank();
340 EXPECT_EQ(0, get_bulk().parallel_owner_rank(node2));
341 if (proc == 1 || proc == 2) {
342 EXPECT_TRUE(get_bulk().bucket(node2).in_aura());
343 EXPECT_FALSE(get_bulk().bucket(node2).shared());
344 }
345 else {
346 EXPECT_TRUE(get_bulk().bucket(node2).shared());
347 EXPECT_FALSE(get_bulk().bucket(node2).in_aura());
348 }
349
350 p1_starts_sharing_node2();
351
352 node2 = get_bulk().get_entity(stk::topology::NODE_RANK, 2);
353 EXPECT_TRUE(get_bulk().is_valid(node2));
354 EXPECT_EQ(0, get_bulk().parallel_owner_rank(node2));
355
356 if (proc == 2) {
357 EXPECT_TRUE(get_bulk().bucket(node2).in_aura());
358 EXPECT_FALSE(get_bulk().bucket(node2).shared());
359 }
360 else {
361 EXPECT_TRUE(get_bulk().bucket(node2).shared());
362 EXPECT_FALSE(get_bulk().bucket(node2).in_aura());
363 }
364
365 if (proc == 1 || proc == 3) {
366 int ownerProc = 0;
367 int otherSharingProc = proc==1 ? 3 : 1;
368 EXPECT_TRUE(get_bulk().in_shared(node2, ownerProc));
369 EXPECT_TRUE(get_bulk().in_shared(node2, otherSharingProc));
370 }
371 }
372
373 TEST_F(Aura2DTri4Procs, addSharerToAlreadySharedAuraNode_addsRemoteGhostingRequestForAlreadyShared)
374 {
375 if (get_parallel_size() != 3) { GTEST_SKIP(); }
376
377 std::string meshDesc = "0, 1, TRI_3_2D, 1,2,3\n"
378 "1, 2, TRI_3_2D, 2,4,5\n"
379 "2, 4, TRI_3_2D, 8,9,10\n"
380 "2, 3, TRI_3_2D, 6,7,4\n";
381 setup_text_mesh(meshDesc);
382
383 stk::mesh::Entity node2 = get_bulk().get_entity(stk::topology::NODE_RANK, 2);
384 EXPECT_TRUE(get_bulk().is_valid(node2));
385 const int proc = get_bulk().parallel_rank();
386 EXPECT_EQ(0, get_bulk().parallel_owner_rank(node2));
387 if (proc == 2) {
388 EXPECT_TRUE(get_bulk().bucket(node2).in_aura());
389 EXPECT_FALSE(get_bulk().bucket(node2).shared());
390 }
391 else {
392 EXPECT_TRUE(get_bulk().bucket(node2).shared());
393 EXPECT_FALSE(get_bulk().bucket(node2).in_aura());
394 }
395
396 p2_starts_sharing_node2();
397
398 node2 = get_bulk().get_entity(stk::topology::NODE_RANK, 2);
399 EXPECT_TRUE(get_bulk().is_valid(node2));
400 EXPECT_EQ(0, get_bulk().parallel_owner_rank(node2));
401
402 EXPECT_TRUE(get_bulk().bucket(node2).shared());
403 EXPECT_FALSE(get_bulk().bucket(node2).in_aura());
404
405 if (proc == 1 || proc == 2) {
406 int ownerProc = 0;
407 int otherSharingProc = proc==1 ? 2 : 1;
408 EXPECT_TRUE(get_bulk().in_shared(node2, ownerProc));
409 EXPECT_TRUE(get_bulk().in_shared(node2, otherSharingProc));
410 }
411 }
412
413 } // empty namespace
414