Debian Codegraph @

packages/stk/stk_unit_tests/stk_mesh/UnitTestBulkModification.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 <stdexcept>
40 #include <stk_mesh/base/BulkData.hpp>
41 #include <stk_mesh/base/BulkModification.hpp>
42 #include <stk_mesh/base/Entity.hpp> // for Entity
43 #include <stk_mesh/base/MetaData.hpp>
44 #include <stk_mesh/base/Selector.hpp>
45 #include <stk_unit_test_utils/BulkDataTester.hpp>
46 #include <stk_util/parallel/Parallel.hpp>
47 #include <vector> // for vector, etc
48 #include "mpi.h"
49
50 #include "stk_unit_test_utils/stk_mesh_fixtures/RingFixture.hpp"
51 #include "stk_mesh/base/Bucket.hpp" // for Bucket, etc
52 #include "stk_mesh/base/EntityKey.hpp"
53 #include "stk_mesh/base/EntityLess.hpp"
54 #include "stk_mesh/base/Part.hpp" // for Part
55 #include "stk_mesh/base/Types.hpp"
56 #include "stk_topology/topology.hpp"
57 #include "stk_util/util/ReportHandler.hpp"
58 #include "stk_util/parallel/ParallelComm.hpp"
59 #include "stk_util/util/SortAndUnique.hpp"
60 // clang-format on
61 // ####################### End Clang Header Tool Managed Headers ########################
62
63 using stk::mesh::MetaData;
64 using stk::mesh::BulkData;
65 using stk::mesh::Bucket;
66 using stk::mesh::BucketIterator;
67 using stk::mesh::Entity;
68 using stk::mesh::EntityRank;
69 using stk::mesh::BucketVector;
70 using stk::mesh::fixtures::RingFixture;
71
72 class UnitTestStkMeshBulkModification {
73 public:
74 UnitTestStkMeshBulkModification(stk::ParallelMachine pm)
75 : m_comm(pm),
76 m_num_procs(stk::parallel_machine_size( m_comm )),
77 m_rank(stk::parallel_machine_rank( m_comm )),
78 m_ring_mesh(pm)
79 { }
80
81 void test_bulkdata_not_synchronized();
82 void test_all_local_nodes();
83 void test_all_local_elements();
84 void test_parallel_consistency();
85
86 BulkData& initialize_ring_fixture()
87 {
88 m_ring_mesh.m_meta_data.commit();
89 BulkData& bulk_data = m_ring_mesh.m_bulk_data;
90
91 bulk_data.modification_begin();
92 m_ring_mesh.generate_mesh( );
93 STK_ThrowRequire(bulk_data.modification_end());
94
95 m_ring_mesh.fixup_node_ownership( );
96
97 return bulk_data;
98 }
99
100 stk::ParallelMachine m_comm;
101 int m_num_procs;
102 int m_rank;
103 RingFixture m_ring_mesh;
104 };
105
106 namespace {
107
108 const EntityRank NODE_RANK = stk::topology::NODE_RANK;
109
110 TEST( UnitTestBulkDataNotSynchronized , testUnit )
111 {
112 UnitTestStkMeshBulkModification unit(MPI_COMM_WORLD);
113 unit.test_bulkdata_not_synchronized();
114 }
115
116 TEST( UnitTestAllLocalNodes , testUnit )
117 {
118 UnitTestStkMeshBulkModification unit(MPI_COMM_WORLD);
119 unit.test_all_local_nodes();
120 }
121
122 TEST( UnitTestAllLocalElements , testUnit )
123 {
124 UnitTestStkMeshBulkModification unit(MPI_COMM_WORLD);
125 unit.test_all_local_elements();
126 }
127
128 TEST( UnitTestParallelConsistency , testUnit )
129 {
130 UnitTestStkMeshBulkModification unit(MPI_COMM_WORLD);
131 unit.test_parallel_consistency();
132 }
133
134 } //end namespace
135
136 void UnitTestStkMeshBulkModification::test_bulkdata_not_synchronized()
137 {
138 BulkData& bulk_data = initialize_ring_fixture();
139
140 bulk_data.modification_begin(); // Intentionally make things unsynced
141
142 std::vector< Entity> entities;
143 std::vector< Entity> entities_closure;
144 ASSERT_THROW(stk::mesh::find_closure(bulk_data, entities, entities_closure), std::logic_error);
145 }
146
147 void UnitTestStkMeshBulkModification::test_all_local_nodes()
148 {
149 BulkData& bulk_data = initialize_ring_fixture();
150
151 {
152 std::vector< Entity> entities;
153 std::vector< Entity> entities_closure;
154 find_closure(bulk_data, entities, entities_closure);
155
156 // the closure of the an empty set of entities on all procs should be empty
157 EXPECT_TRUE(entities_closure.empty());
158 }
159
160 {
161 // Get a selector for the univeral part (contains local, shared, and ghosted)
162 const stk::mesh::Part& universal = m_ring_mesh.m_meta_data.universal_part();
163 stk::mesh::Selector universal_selector(universal);
164
165 // Get the buckets that will give us the universal nodes
166 BucketVector buckets = bulk_data.get_buckets(NODE_RANK, universal_selector);
167
168 // Get the universal nodes
169 std::vector< Entity> universal_entities;
170 for (BucketVector::iterator itr = buckets.begin();
171 itr != buckets.end(); ++itr) {
172 Bucket& b = **itr;
173 for (BucketIterator bitr = b.begin(); bitr != b.end(); ++bitr) {
174 universal_entities.push_back(*bitr);
175 }
176 }
177 buckets.clear();
178
179 stk::util::sort_and_unique(universal_entities, stk::mesh::EntityLess(bulk_data));
180
181 // Get the buckets that will give us the locally used nodes
182 stk::mesh::Selector locally_used_selector =
183 m_ring_mesh.m_meta_data.locally_owned_part() |
184 m_ring_mesh.m_meta_data.globally_shared_part();
185
186 buckets = bulk_data.get_buckets(stk::topology::NODE_RANK, locally_used_selector);
187
188 // Get the locally used nodes
189 std::vector< Entity> entities;
190 for (BucketVector::iterator itr = buckets.begin();
191 itr != buckets.end(); ++itr) {
192 Bucket& b = **itr;
193 for (BucketIterator bitr = b.begin(); bitr != b.end(); ++bitr) {
194 entities.push_back(*bitr);
195 }
196 }
197
198 // Get the closure, passing in the locally used nodes on each proc
199 std::vector< Entity> entities_closure;
200 stk::mesh::find_closure(bulk_data, entities, entities_closure);
201
202 // The ghosted nodes on this part will be locally used on one of the other
203 // procs, so we expect that they will be part of the closure. In other
204 // words, the set of nodes returned by find_closure should exactly match
205 // the set of universal nodes.
206 ASSERT_TRUE(universal_entities.size() == entities_closure.size());
207 for (size_t i = 0; i < entities_closure.size(); ++i) {
208 EXPECT_TRUE(universal_entities[i] == entities_closure[i]);
209 }
210 }
211 }
212
213 void UnitTestStkMeshBulkModification::test_all_local_elements()
214 {
215 BulkData& bulk_data = initialize_ring_fixture();
216 {
217 const stk::mesh::Part& universal = m_ring_mesh.m_meta_data.universal_part();
218 stk::mesh::Selector universal_selector(universal);
219
220 BucketVector buckets = bulk_data.get_buckets(stk::topology::NODE_RANK, universal_selector);
221
222 // get all the nodes that this process knows about
223 std::vector< Entity> universal_entities;
224 for (BucketVector::iterator itr = buckets.begin();
225 itr != buckets.end(); ++itr) {
226 Bucket& b = **itr;
227 for (BucketIterator bitr = b.begin(); bitr != b.end(); ++bitr) {
228 universal_entities.push_back(*bitr);
229 }
230 }
231 buckets.clear();
232
233 buckets = bulk_data.get_buckets(stk::topology::ELEMENT_RANK, universal_selector);
234
235 // get all the elements that this process knows about
236 for (BucketVector::iterator itr = buckets.begin();
237 itr != buckets.end(); ++itr) {
238 Bucket& b = **itr;
239 for (BucketIterator bitr = b.begin(); bitr != b.end(); ++bitr) {
240 universal_entities.push_back(*bitr);
241 }
242 }
243 buckets.clear();
244
245 // universal entities should now have all the universal nodes and elements
246 stk::util::sort_and_unique(universal_entities, stk::mesh::EntityLess(bulk_data));
247
248 // get the buckets that we need to traverse to get the locally used elements
249 stk::mesh::Selector locally_used_selector =
250 m_ring_mesh.m_meta_data.locally_owned_part() |
251 m_ring_mesh.m_meta_data.globally_shared_part();
252
253 buckets = bulk_data.get_buckets(stk::topology::ELEMENT_RANK, locally_used_selector);
254
255 // get the locally used elements and store them in entities
256 std::vector< Entity> entities;
257 for (BucketVector::iterator itr = buckets.begin();
258 itr != buckets.end(); ++itr) {
259 Bucket& b = **itr;
260 for (BucketIterator bitr = b.begin(); bitr != b.end(); ++bitr) {
261 entities.push_back(*bitr);
262 }
263 }
264
265 // call find_closure, passing in the locally used elements
266 std::vector< Entity> entities_closure;
267 stk::mesh::find_closure(bulk_data, entities, entities_closure);
268
269 // The ghosted entities on this proc (element or node) should be contained
270 // in the closure of the locally-used element on some other proc, so we
271 // expect that they will be part of the closure. In other
272 // words, the set of entities returned by find_closure should exactly match
273 // the set of universal entities (nodes and elements).
274 ASSERT_TRUE(universal_entities.size() == entities_closure.size());
275 for (size_t i = 0; i < entities_closure.size(); ++i) {
276 EXPECT_TRUE(universal_entities[i] == entities_closure[i]);
277 }
278 }
279 }
280
281 void UnitTestStkMeshBulkModification::test_parallel_consistency()
282 {
283 BulkData& bulk_data = initialize_ring_fixture();
284
285 std::vector< Entity> entities;
286 std::vector< Entity> entities_closure;
287
288 // For proc 0 only, add locally used nodes to entities, for all other
289 // procs, leave entities empty.
290 if (m_rank == 0) {
291 stk::mesh::Selector locally_used_selector =
292 m_ring_mesh.m_meta_data.locally_owned_part() |
293 m_ring_mesh.m_meta_data.globally_shared_part();
294
295 BucketVector const& buckets = bulk_data.get_buckets(stk::topology::NODE_RANK, locally_used_selector);
296
297 for (BucketVector::const_iterator itr = buckets.begin();
298 itr != buckets.end(); ++itr) {
299 Bucket& b = **itr;
300 for (BucketIterator bitr = b.begin(); bitr != b.end(); ++bitr) {
301 entities.push_back(*bitr);
302 }
303 }
304 }
305
306 // Call find_closure with proc 0 passing in locally-used nodes
307 stk::mesh::find_closure(bulk_data, entities, entities_closure);
308
309 // Proc 0 will broadcast the global ids of the nodes it passed to
310 // find_closure
311
312 stk::CommBroadcast all(bulk_data.parallel(), 0);
313
314 stk::pack_and_communicate(all, [&](){
315 for (Entity entity : entities) {
316 all.send_buffer().pack<stk::mesh::EntityKey>(bulk_data.entity_key(entity));
317 }
318 });
319
320 // clear-out entities and put the nodes that correspond to the keys
321 // broadcast by proc 0 into entities.
322 entities.clear();
323 stk::CommBuffer& buf = all.recv_buffer();
324 stk::mesh::EntityKey k ;
325 while ( buf.remaining() ) {
326 buf.unpack<stk::mesh::EntityKey>(k);
327 Entity e = bulk_data.get_entity(k);
328 // If a proc is not aware of a key, that means it has no relationship
329 // with that entity, so it can ignore it.
330 if (bulk_data.is_valid(e)) {
331 entities.push_back(e);
332 }
333 }
334
335 stk::util::sort_and_unique(entities, stk::mesh::EntityLess(bulk_data));
336
337 // If any processor had ghosted nodes that were local to proc 0, those
338 // nodes should be in the closure because proc 0 passed them in to
339 // find_closure.
340 ASSERT_TRUE(entities.size() == entities_closure.size());
341 for (size_t i = 0; i < entities_closure.size(); ++i) {
342 EXPECT_TRUE(entities[i] == entities_closure[i]);
343 }
344 }