Debian Codegraph @

packages/stk/stk_unit_tests/stk_mesh/UnitTestBulkData_Destroy.cpp

raw
1 // Copyright 2002 - 2008, 2010, 2011 National Technology Engineering
2 // Solutions of Sandia, LLC (NTESS). Under the terms of Contract
3 // DE-NA0003525 with NTESS, the U.S. Government retains certain rights
4 // in this software.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
8 // met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 //
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following
15 // disclaimer in the documentation and/or other materials provided
16 // with the distribution.
17 //
18 // * Neither the name of NTESS nor the names of its contributors
19 // may be used to endorse or promote products derived from this
20 // software without specific prior written permission.
21 //
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 //
34
35 // ####################### Start Clang Header Tool Managed Headers ########################
36 // clang-format off
37 #include <gtest/gtest.h>
38 #include <stddef.h> // for size_t
39 #include <stk_mesh/base/BulkData.hpp>
40 #include <stk_unit_test_utils/BulkDataTester.hpp>
41 #include <stk_util/parallel/Parallel.hpp>
42 #include <vector> // for vector
43 #include "mpi.h"
44
45 #include "stk_unit_test_utils/stk_mesh_fixtures/RingFixture.hpp"
46 #include "stk_mesh/base/Bucket.hpp" // for Bucket
47 #include "stk_mesh/base/Entity.hpp" // for Entity
48 #include "stk_mesh/base/EntityCommListInfo.hpp"
49 #include "stk_mesh/base/MetaData.hpp"
50 #include "stk_mesh/base/Part.hpp" // for Part
51 #include "stk_mesh/base/Selector.hpp"
52 #include "stk_mesh/base/Types.hpp"
53 #include "stk_topology/topology.hpp"
54 #include "stk_unit_test_utils/BuildMesh.hpp"
55 // clang-format on
56 // ####################### End Clang Header Tool Managed Headers ########################
57
58 using stk::mesh::Part;
59 using stk::mesh::MetaData;
60 using stk::mesh::BulkData;
61 using stk::mesh::Entity;
62 using stk::mesh::Selector;
63 using stk::mesh::PartVector;
64 using stk::mesh::EntityId;
65 using stk::mesh::fixtures::RingFixture;
66 using stk::unit_test_util::build_mesh;
67
68 //----------------------------------------------------------------------
69 // Testing for mesh entities without relations
70
71 void testDestroy_nodes(stk::mesh::BulkData::AutomaticAuraOption autoAuraOption)
72 {
73 stk::ParallelMachine pm = MPI_COMM_WORLD;
74 MPI_Barrier( pm );
75
76 enum { nPerProc = 10 };
77 const int p_rank = stk::parallel_machine_rank( pm );
78 const int p_size = stk::parallel_machine_size( pm );
79 const unsigned id_total = nPerProc * p_size ;
80 const unsigned id_begin = nPerProc * p_rank ;
81 const unsigned id_end = nPerProc * ( p_rank + 1 );
82
83 const int spatial_dimension = 3;
84 std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatial_dimension, pm, autoAuraOption);
85 MetaData& meta = bulkPtr->mesh_meta_data();
86
87 const PartVector no_parts ;
88
89 meta.commit();
90
91 BulkData& bulk = *bulkPtr;
92
93 // Ids for all entities (all entities have type 0):
94
95 std::vector<EntityId> ids( id_total );
96
97 for ( unsigned i = 0 ; i < id_total ; ++i ) {
98 ids[i] = i + 1;
99 }
100
101 // Declare just those entities in my range of ids:
102
103 ASSERT_TRUE( bulk.modification_begin() );
104 for ( unsigned i = id_begin ; i < id_end ; ++i ) {
105 bulk.declare_node(ids[i] , no_parts );
106 }
107 ASSERT_TRUE( bulk.modification_end() );
108
109 // Verify that I only have entities in my range:
110
111 for ( unsigned i = 0 ; i < id_total ; ++i ) {
112 Entity e = bulk.get_entity( stk::topology::NODE_RANK , ids[ i ] );
113 if ( id_begin <= i && i < id_end ) {
114 ASSERT_TRUE( bulk.is_valid(e) );
115 ASSERT_TRUE( p_rank == bulk.parallel_owner_rank(e) );
116 }
117 else {
118 ASSERT_TRUE( !bulk.is_valid(e) );
119 }
120 }
121
122 // Delete one entity at a time.
123
124 for ( unsigned i = id_begin ; i < id_end ; ++i ) {
125 Entity e = bulk.get_entity( stk::topology::NODE_RANK , ids[ i ] );
126
127 ASSERT_TRUE( bulk.is_valid(e) );
128
129 bulk.modification_begin();
130 ASSERT_TRUE( bulk.destroy_entity( e ) );
131 bulk.modification_end();
132
133 if ( id_begin < i ) {
134 ASSERT_TRUE( !bulk.is_valid(bulk.get_entity( stk::topology::NODE_RANK , ids[ i - 1 ] )) );
135 }
136
137 e = bulk.get_entity( stk::topology::NODE_RANK , ids[ i ] );
138 ASSERT_TRUE( !bulk.is_valid(e) );
139 }
140 }
141
142 TEST(UnitTestingOfBulkData, testDestroy_nodes_with_aura)
143 {
144 testDestroy_nodes(stk::mesh::BulkData::AUTO_AURA);
145 }
146
147 TEST(UnitTestingOfBulkData, testDestroy_nodes_without_aura)
148 {
149 testDestroy_nodes(stk::mesh::BulkData::NO_AUTO_AURA);
150 }
151
152 //----------------------------------------------------------------------
153
154 void assert_is_destroyed(const BulkData& mesh, const Entity entity )
155 {
156 ASSERT_TRUE( !mesh.is_valid(entity) || mesh.bucket(entity).capacity() == 0 );
157 }
158
159 TEST(UnitTestingOfBulkData, testDestroy_ring)
160 {
161 stk::ParallelMachine pm = MPI_COMM_WORLD;
162 MPI_Barrier( pm );
163
164 enum { nPerProc = 10 };
165 const int p_rank = stk::parallel_machine_rank( pm );
166 const int p_size = stk::parallel_machine_size( pm );
167 // const unsigned nLocalNode = nPerProc + ( 1 < p_size ? 1 : 0 );
168 const unsigned nLocalElement = nPerProc ;
169
170 const int spatial_dimension = 3;
171 MetaData meta( spatial_dimension );
172
173 meta.commit();
174
175 Selector select_owned( meta.locally_owned_part() );
176 Selector select_used = meta.locally_owned_part() | meta.globally_shared_part();
177 Selector select_all( meta.universal_part() );
178
179 PartVector no_parts ;
180
181 std::vector<unsigned> local_count ;
182
183 //------------------------------
184 { // No ghosting
185 RingFixture mesh( pm , nPerProc , false /* No element parts */, stk::mesh::BulkData::NO_AUTO_AURA );
186 mesh.m_meta_data.commit();
187 BulkData& bulk = mesh.m_bulk_data;
188
189 bulk.modification_begin();
190 mesh.generate_mesh( );
191 ASSERT_TRUE(bulk.modification_end());
192
193 mesh.fixup_node_ownership();
194
195 // This process' first element in the loop
196 // if a parallel mesh has a shared node
197 Entity element = bulk.get_entity( stk::topology::ELEMENT_RANK , mesh.m_element_ids[ nLocalElement * p_rank ] );
198 Entity const *element_nodes = bulk.begin_nodes(element);
199 Entity node0 = element_nodes[0];
200 Entity node1 = element_nodes[1];
201
202 const size_t node0_elements = bulk.count_relations(node0);
203 const size_t node1_elements = bulk.count_relations(node1);
204
205 ASSERT_TRUE( 1 <= node0_elements && node0_elements <= 2 );
206 ASSERT_TRUE( 1 <= node1_elements && node1_elements <= 2 );
207
208 ASSERT_TRUE( bulk.begin_elements(node0)[0] == element ||
209 bulk.begin_elements(node0)[1] == element );
210
211 ASSERT_TRUE( bulk.begin_elements(node1)[0] == element ||
212 bulk.begin_elements(node1)[1] == element );
213
214 bulk.modification_begin();
215
216 // Destroy the element:
217 bool result = bulk.destroy_entity( element );
218 element = Entity();
219 ASSERT_TRUE( true == result );
220
221 // Destroy orphanned node:
222 if ( bulk.has_no_relations(node0) ) {
223 ASSERT_TRUE( bulk.destroy_entity( node0 ) );
224 node0 = Entity();
225 }
226 if ( bulk.has_no_relations(node1) ) {
227 ASSERT_TRUE( bulk.destroy_entity( node1 ) );
228 node1 = Entity();
229 }
230 ASSERT_TRUE( bulk.modification_end() );
231
232 if ( bulk.is_valid(node0) ) {
233 ASSERT_EQ( node0_elements - 1 , bulk.count_relations(node0) );
234 }
235 if ( bulk.is_valid(node1) ) {
236 ASSERT_EQ( node1_elements - 1 , bulk.count_relations(node1) );
237 }
238 }
239 //------------------------------
240 if ( 1 < p_size ) { // With ghosting
241 RingFixture mesh( pm , nPerProc , false /* No element parts */ );
242 mesh.m_meta_data.commit();
243 stk::unit_test_util::BulkDataTester& bulk = mesh.m_bulk_data;
244
245 bulk.modification_begin();
246 mesh.generate_mesh( );
247 ASSERT_TRUE( bulk.modification_end() );
248
249 mesh.fixup_node_ownership();
250
251 const unsigned nNotOwned = nPerProc * p_rank ;
252
253 // The not-owned shared entity:
254 Entity node = bulk.get_entity( stk::topology::NODE_RANK , mesh.m_node_ids[ nNotOwned ] );
255
256 ASSERT_TRUE( bulk.is_valid(node) );
257 ASSERT_NE( p_rank , bulk.parallel_owner_rank(node) );
258
259 std::vector<int> shared_procs;
260 bulk.comm_shared_procs(bulk.entity_key(node),shared_procs);
261 ASSERT_EQ( size_t(1) , shared_procs.size() );
262 ASSERT_EQ( size_t(2) , bulk.count_relations(node) )<<"node: "<<bulk.identifier(node);
263
264 EntityId node_element_ids[2] ;
265 Entity const *node_elems = bulk.begin_elements(node);
266 node_element_ids[0] = bulk.identifier(node_elems[0]);
267 node_element_ids[1] = bulk.identifier(node_elems[1]);
268
269 bulk.modification_begin();
270
271 // This process' first node in the loop is shared, destroy it
272 // First have to destroy attached elements.
273 // One will be owned and the other ghosted
274
275 const stk::mesh::EntityRank end_rank = static_cast<stk::mesh::EntityRank>(bulk.mesh_meta_data().entity_rank_count());
276 for (stk::mesh::EntityRank irank = end_rank; irank != stk::topology::BEGIN_RANK; )
277 {
278 --irank;
279 stk::mesh::Entity const *to_b = bulk.begin(node, irank);
280 stk::mesh::Entity const *to_e = bulk.end(node, irank);
281 for (; to_b != to_e;
282 to_b = bulk.begin(node, irank), to_e = bulk.end(node, irank))
283 {
284 ASSERT_TRUE( bulk.destroy_entity(*(to_e -1)) );
285 }
286 }
287 ASSERT_TRUE( bulk.destroy_entity( node ) );
288
289 ASSERT_TRUE( bulk.modification_end() );
290
291 assert_is_destroyed(bulk, bulk.get_entity(stk::topology::NODE_RANK, mesh.m_node_ids[nNotOwned] ) );
292 assert_is_destroyed(bulk, bulk.get_entity(stk::topology::ELEMENT_RANK, node_element_ids[0] ) );
293 assert_is_destroyed(bulk, bulk.get_entity(stk::topology::ELEMENT_RANK, node_element_ids[1] ) );
294
295 // assert that no entities are shared or ghosted
296 ASSERT_TRUE( bulk.my_internal_comm_list().empty() );
297 }
298 //------------------------------
299 if ( 1 < p_size ) { // With ghosting
300 RingFixture mesh( pm , nPerProc , false /* No element parts */ );
301 mesh.m_meta_data.commit();
302 stk::unit_test_util::BulkDataTester& bulk = mesh.m_bulk_data;
303
304 bulk.modification_begin();
305 mesh.generate_mesh( );
306 ASSERT_TRUE( bulk.modification_end() );
307
308 mesh.fixup_node_ownership();
309
310 // The owned shared entity:
311 const unsigned nOwned = ( nPerProc * ( p_rank + 1 ) ) % mesh.m_node_ids.size();
312 const unsigned nNotOwned = nPerProc * p_rank ;
313
314 Entity node_owned = bulk.get_entity( stk::topology::NODE_RANK , mesh.m_node_ids[ nOwned ] );
315 Entity node_not_owned = bulk.get_entity( stk::topology::NODE_RANK , mesh.m_node_ids[ nNotOwned ] );
316
317 ASSERT_TRUE( bulk.is_valid(node_owned) );
318 ASSERT_TRUE( bulk.is_valid(node_not_owned));
319 ASSERT_NE( p_rank , bulk.parallel_owner_rank(node_not_owned) );
320 ASSERT_EQ( p_rank , bulk.parallel_owner_rank(node_owned) );
321
322 std::vector<int> node_owned_shared_procs;
323 bulk.comm_shared_procs(bulk.entity_key(node_owned),node_owned_shared_procs);
324 std::vector<int> node_not_owned_shared_procs;
325 bulk.comm_shared_procs(bulk.entity_key(node_not_owned),node_not_owned_shared_procs);
326 ASSERT_EQ( 1u , node_owned_shared_procs.size() );
327 ASSERT_EQ( 1u , node_not_owned_shared_procs.size() );
328 ASSERT_EQ( 2u , bulk.count_relations(node_owned) );
329
330 EntityId node_element_ids[2] ;
331 stk::mesh::Entity const *node_elems = bulk.begin_elements(node_owned);
332 node_element_ids[0] = bulk.identifier(node_elems[0]);
333 node_element_ids[1] = bulk.identifier(node_elems[1]);
334
335 bulk.modification_begin();
336
337 // This process' first node in the loop is shared, destroy it
338 // First have to destroy attached elements.
339 // One will be owned and the other ghosted
340 const stk::mesh::EntityRank end_rank = static_cast<stk::mesh::EntityRank>(bulk.mesh_meta_data().entity_rank_count());
341 for (stk::mesh::EntityRank irank = end_rank; irank != stk::topology::BEGIN_RANK; )
342 {
343 --irank;
344 while (bulk.num_connectivity(node_owned, irank) > 0) {
345 stk::mesh::Entity const *to_b = bulk.begin(node_owned, irank);
346 ASSERT_TRUE( bulk.destroy_entity(*to_b) );
347 }
348 }
349 ASSERT_TRUE( bulk.destroy_entity( node_owned ) );
350
351 ASSERT_TRUE( bulk.modification_end() );
352
353 // Ownership of the other process' owned, shared, and destroyed node
354 // has been transferred to this process.
355
356 ASSERT_EQ( p_rank , bulk.parallel_owner_rank(node_not_owned) );
357 assert_is_destroyed(bulk, bulk.get_entity(stk::topology::NODE_RANK, mesh.m_node_ids[ nOwned ] ) );
358 assert_is_destroyed(bulk, bulk.get_entity(stk::topology::ELEMENT_RANK, node_element_ids[0] ) );
359 assert_is_destroyed(bulk, bulk.get_entity(stk::topology::ELEMENT_RANK, node_element_ids[1] ) );
360
361 // assert that no entities are shared or ghosted
362 ASSERT_TRUE( bulk.my_internal_comm_list().empty() );
363 }
364 }
365