Debian Codegraph @

packages/stk/stk_unit_tests/stk_mesh/UnitTestPartitions.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 <algorithm> // for copy, reverse
36 #include <iostream> // for operator<<, basic_ostream, etc
37 #include <stk_mesh/base/Bucket.hpp> // for Bucket, Bucket::iterator
38 #include <stk_mesh/base/Part.hpp> // for Part
39 #include <stk_mesh/base/Selector.hpp> // for Selector, operator&, etc
40 #include <stk_mesh/base/Types.hpp> // for PartOrdinal, BucketVector, etc
41 #include <stk_mesh/baseImpl/Partition.hpp> // for Partition
42 #include <gtest/gtest.h>
43 #include <vector> // for vector, vector<>::iterator, etc
44
45 #include "stk_mesh/base/BulkData.hpp" // for BulkData
46 #include "stk_mesh/base/Entity.hpp" // for Entity
47 #include "stk_mesh/base/Field.hpp" // for Field
48 #include "stk_mesh/base/FieldBase.hpp" // for field_data
49 #include "stk_mesh/base/MetaData.hpp" // for MetaData
50 #include "stk_mesh/baseImpl/BucketRepository.hpp" // for BucketRepository, etc
51 #include "stk_mesh/baseImpl/GlobalIdEntitySorter.hpp" // for BucketRepository, etc
52 #include "stk_topology/topology.hpp" // for topology, etc
53 #include "stk_unit_test_utils/stk_mesh_fixtures/SelectorFixture.hpp" // for SelectorFixture
54
55 namespace {
56
57 struct ReverseSorter : public stk::mesh::EntitySorterBase
58 {
59 virtual void sort(stk::mesh::BulkData &bulk, stk::mesh::EntityVector& entityVector) const
60 {
61 std::sort(entityVector.begin(), entityVector.end(),
62 [&bulk](const stk::mesh::Entity lhs, const stk::mesh::Entity rhs)->bool
63 { return bulk.entity_key(lhs) > bulk.entity_key(rhs); });
64 }
65 };
66
67 using stk::mesh::fixtures::SelectorFixture;
68
69
70 // Borrow a lot from UnitTestSelector. Bulk up the SelectorFixture to have parts
71 // with enough entities so that each partition (bucket family) comprises multiple
72 // buckets.
73
74 stk::mesh::EntityId
75 addEntitiesToFixture(SelectorFixture& fixture, stk::mesh::EntityId start_id, size_t num_to_add,
76 const std::vector<stk::mesh::Part*> &partMembership,
77 std::vector<stk::mesh::Entity> &collector)
78 {
79 stk::mesh::EntityId ent_id = start_id;
80 for (size_t i = 0; i < num_to_add; ++i)
81 {
82 stk::mesh::Entity ent = fixture.m_bulk_data.declare_node(ent_id, partMembership);
83 collector.push_back(ent);
84 ++ent_id;
85 }
86 return ent_id;
87 }
88
89 void initializeFiveEntityCollections(SelectorFixture& fixture,
90 std::vector<stk::mesh::Entity> &ec1_arg,
91 std::vector<stk::mesh::Entity> &ec2_arg,
92 std::vector<stk::mesh::Entity> &ec3_arg,
93 std::vector<stk::mesh::Entity> &ec4_arg,
94 std::vector<stk::mesh::Entity> &ec5_arg
95 )
96 {
97 fixture.m_meta_data.commit();
98 fixture.m_bulk_data.modification_begin();
99 fixture.generate_mesh();
100
101 const size_t bucket_size = 1000; // Default value for BucketRepository constructor.
102 const size_t lb_num_buckets_per_partition = 3;
103
104 const size_t bf_size = bucket_size * lb_num_buckets_per_partition;
105 stk::mesh::EntityId ent_id = 1001; // Want to keep numerical alignment.
106 std::vector<stk::mesh::Part*> partMembership;
107
108 // Note that the loop variables start at 1 because SelectorFixture::generate_mesh() has
109 // already created an Entity in each partition.
110
111 // Entities in collection 1 are contained in PartA
112 partMembership.clear();
113 partMembership.push_back( & fixture.m_partA );
114 ent_id = addEntitiesToFixture(fixture, ent_id, bf_size - 1, partMembership, ec1_arg);
115
116 // Entities in collection 2 are in PartA and PartB
117 ++ent_id; // For numerical alignment.
118 partMembership.clear();
119 partMembership.push_back( & fixture.m_partA );
120 partMembership.push_back( & fixture.m_partB );
121 ent_id = addEntitiesToFixture(fixture, ent_id, bf_size - 1, partMembership, ec2_arg);
122
123 // Entities in collection 3 are in PartB and PartC
124 ++ent_id;
125 partMembership.clear();
126 partMembership.push_back( & fixture.m_partB );
127 partMembership.push_back( & fixture.m_partC );
128 ent_id = addEntitiesToFixture(fixture, ent_id, bf_size - 1, partMembership, ec3_arg);
129
130 // Entities in collection 4 are in PartC
131 ++ent_id;
132 partMembership.clear();
133 partMembership.push_back( & fixture.m_partC );
134 ent_id = addEntitiesToFixture(fixture, ent_id, bf_size - 1, partMembership, ec4_arg);
135
136 // Entities in collection 5 are not contained in any Part
137 ++ent_id;
138 partMembership.clear();
139 ent_id = addEntitiesToFixture(fixture, ent_id, bf_size - 1, partMembership, ec5_arg);
140
141 bool me_result = fixture.m_bulk_data.modification_end();
142 ASSERT_TRUE(me_result);
143 }
144
145 void initializeFivePartitionsWithSixBucketsEach(SelectorFixture& fixture)
146 {
147 std::vector<stk::mesh::Entity> ec1;
148 std::vector<stk::mesh::Entity> ec2;
149 std::vector<stk::mesh::Entity> ec3;
150 std::vector<stk::mesh::Entity> ec4;
151 std::vector<stk::mesh::Entity> ec5;
152
153 initializeFiveEntityCollections(fixture, ec1, ec2, ec3, ec4, ec5);
154 }
155
156 void initialize_unsorted(SelectorFixture& fixture)
157 {
158 initializeFivePartitionsWithSixBucketsEach(fixture);
159
160 stk::mesh::impl::BucketRepository &bucket_repository = fixture.m_bulk_data.my_get_bucket_repository();
161 bucket_repository.sync_from_partitions();
162
163 std::vector<stk::mesh::impl::Partition *> partitions =
164 bucket_repository.get_partitions(stk::topology::NODE_RANK);
165 size_t num_partitions = partitions.size();
166 EXPECT_EQ(num_partitions, 5u);
167
168 for (size_t i = 0; i < num_partitions; ++i)
169 {
170 stk::mesh::impl::Partition &partition = *partitions[i];
171 partition.sort(ReverseSorter());
172 }
173 }
174
175 // Initialize field data in the fixture to correspond to the EntityIds of the entities.
176 void setFieldDataUsingEntityIDs(SelectorFixture& fix)
177 {
178 stk::mesh::impl::BucketRepository &bucket_repository = fix.m_bulk_data.my_get_bucket_repository();
179 bucket_repository.sync_from_partitions();
180
181 stk::mesh::BulkData &mesh = fix.m_bulk_data;
182
183 std::vector<stk::mesh::impl::Partition *> partitions = bucket_repository.get_partitions(stk::topology::NODE_RANK);
184 size_t num_partitions = partitions.size();
185
186 for (size_t i = 0; i < num_partitions; ++i)
187 {
188 stk::mesh::impl::Partition &partition = *partitions[i];
189 for (stk::mesh::BucketVector::iterator bkt_i= partition.begin(); bkt_i != partition.end(); ++bkt_i)
190 {
191 stk::mesh::Bucket &bkt = **bkt_i;
192 double *field_data = stk::mesh::field_data(*fix.m_fieldABC, bkt, 0);
193 if (!field_data)
194 {
195 continue;
196 }
197
198 size_t bkt_size = bkt.size();
199 for (size_t k = 0; k < bkt_size; ++k)
200 {
201 stk::mesh::Entity curr_ent = bkt[k];
202 field_data[k] = mesh.identifier(curr_ent);
203 }
204 }
205 }
206 }
207
208
209 bool areAllEntitiesSelected(const stk::mesh::BulkData& mesh, const stk::mesh::Selector selects,
210 const std::vector<stk::mesh::Entity> &cands)
211 {
212 for (size_t i = 0; i < cands.size(); ++i)
213 {
214 if (!selects(mesh.bucket(cands[i])))
215 return false;
216 }
217 return true;
218 }
219
220 bool check_bucket_ptrs(const stk::mesh::Bucket &bucket)
221 {
222 if (bucket.size() == 0 )
223 {
224 std::cout << "Bucket has size zero!" << std::endl;
225 return false;
226 }
227
228 stk::mesh::Bucket::iterator e_i = bucket.begin();
229 stk::mesh::Bucket::iterator e_e = bucket.end();
230 for(; e_i != e_e; ++e_i)
231 {
232 if (&bucket != bucket.mesh().bucket_ptr(*e_i))
233 return false;
234 }
235 return true;
236 }
237
238 template <typename Data_T>
239 bool check_nonempty_strictly_ordered(Data_T data[], size_t sz, bool reject_0_lt_0 = true )
240 {
241 if (sz == 0)
242 return false;
243
244 for (size_t i = 0; i < sz - 1; ++i)
245 {
246 if ((data[i] >= data[i + 1]) && reject_0_lt_0)
247 {
248 std::cout << "i = " << i << ": data[i] = " << data[i]
249 << ", data[i + 1] = " << data[i + 1] << std::endl;
250 return false;
251 }
252 }
253 return true;
254 }
255
256 void check_test_partition_invariant(SelectorFixture& fix,
257 const stk::mesh::impl::Partition &partition)
258 {
259 const std::vector<unsigned> &partition_key = partition.get_legacy_partition_id();
260 for (stk::mesh::BucketVector::const_iterator bkt_i= partition.begin();
261 bkt_i != partition.end(); ++bkt_i)
262 {
263 const stk::mesh::Bucket &bkt = **bkt_i;
264 EXPECT_EQ(&partition, bkt.getPartition() );
265 EXPECT_TRUE(check_bucket_ptrs(bkt));
266
267 double *field_data = stk::mesh::field_data(*fix.m_fieldABC, bkt, 0);
268 if (field_data)
269 {
270 EXPECT_TRUE(check_nonempty_strictly_ordered(field_data, bkt.size()));
271 }
272 const unsigned *bucket_key = bkt.key();
273 for (size_t k = 0; k < partition_key.size(); ++k)
274 {
275 EXPECT_EQ(partition_key[k], bucket_key[k]);
276 }
277
278 stk::mesh::impl::BucketRepository &bucket_repository = fix.m_bulk_data.my_get_bucket_repository();
279 EXPECT_EQ(bucket_repository.get_bucket(bkt.entity_rank(), bkt.bucket_id()), &bkt);
280 }
281 }
282
283 unsigned checkGetBucketAndCountNonEmpty(stk::mesh::impl::BucketRepository &bucket_repo,
284 stk::mesh::EntityRank entity_rank, unsigned max_id_to_try)
285 {
286 unsigned count = 0;
287 for (unsigned i = 0; i < max_id_to_try; ++i)
288 {
289 stk::mesh::Bucket *b = bucket_repo.get_bucket(entity_rank, i);
290 if (b)
291 {
292 ++count;
293 EXPECT_EQ(i, b->bucket_id());
294 }
295 }
296 return count;
297 }
298
299 void check_bucket_ids_testset_A(stk::mesh::impl::BucketRepository &bucket_repository)
300 {
301 const unsigned num_node_buckets = bucket_repository.buckets(stk::topology::NODE_RANK).size();
302 unsigned numNonEmptyNodeBucketsInRepository = checkGetBucketAndCountNonEmpty(bucket_repository, stk::topology::NODE_RANK, num_node_buckets);
303 EXPECT_EQ(numNonEmptyNodeBucketsInRepository, num_node_buckets);
304 const unsigned num_face_buckets = bucket_repository.buckets(stk::topology::FACE_RANK).size();
305 unsigned numNonEmptyFaceBucketsInRepository = checkGetBucketAndCountNonEmpty(bucket_repository, stk::topology::FACE_RANK, num_face_buckets);
306 EXPECT_EQ(numNonEmptyFaceBucketsInRepository, num_face_buckets);
307 }
308
309
310 /** \defgroup stk_mesh_partition_unit "stk::mesh::Partition Unit Testing"
311 * \addtogroup stk_mesh_partition_unit
312 * \{
313 *
314 * Selector unit testing environment. <br>
315 * A special set of mesh parts and entities are set up in the
316 * following configuration for the Selector unit testing.<br>
317 * Parts: PartA, PartB, PartC, PartD, PartU <br>
318 * PartU = MetaData.universal_part() <br>
319 * Entities: Entity1, Entity2, Entity3, Entity4, Entity5 <br>
320 *
321 * PartA contains Entity1, Entity2 <br>
322 * PartB contains Entity2, Entity3 <br>
323 * PartC contains Entity3, Entity4 <br>
324 * PartD contains no entities <br>
325 * Entity5 is not contained in any Part <br>
326 *
327 * <PRE>
328 * |----------|--|-------|--|----------| |-------------|
329 * |<--PartA---->| |<--PartC---->| | PartD |
330 * | |<---PartB--->| | | |
331 * | 1 |2 | |3 | 4 | 5 | |
332 * | | | | | | | |
333 * | | | | | | | |
334 * |----------|--|-------|--|----------| |-------------|
335 * </PRE>
336 *
337 * Note: The unit test names use the convention of "i" for
338 * intersection, "u" for union, and "c" for complement.
339 *
340 * */
341
342
343 /// Verify we can construct the unit testing fixture.
344 TEST( UnitTestPartition, Partition_testInitialize )
345 {
346 std::vector<stk::mesh::Entity> ec1;
347 std::vector<stk::mesh::Entity> ec2;
348 std::vector<stk::mesh::Entity> ec3;
349 std::vector<stk::mesh::Entity> ec4;
350 std::vector<stk::mesh::Entity> ec5;
351
352 SelectorFixture fix;
353
354 if (fix.m_bulk_data.parallel_size() > 1)
355 {
356 return;
357 }
358
359 initializeFiveEntityCollections(fix, ec1, ec2, ec3, ec4, ec5);
360 setFieldDataUsingEntityIDs(fix);
361
362 stk::mesh::impl::BucketRepository &bucket_repository = fix.m_bulk_data.my_get_bucket_repository();
363 bucket_repository.sync_from_partitions();
364
365 std::vector<stk::mesh::impl::Partition *> partitions =
366 bucket_repository.get_partitions(stk::topology::NODE_RANK);
367 size_t num_partitions = partitions.size();
368 EXPECT_EQ(num_partitions, 5u);
369
370 for (size_t i = 0; i < num_partitions; ++i)
371 {
372 const stk::mesh::impl::Partition &partition = *partitions[i];
373 check_test_partition_invariant(fix, partition);
374 }
375
376 stk::mesh::Selector selector;
377
378 selector = fix.m_partA & !fix.m_partB;
379 EXPECT_TRUE(areAllEntitiesSelected(fix.m_bulk_data, selector, ec1));
380
381 selector = fix.m_partA & fix.m_partB;
382 EXPECT_TRUE(areAllEntitiesSelected(fix.m_bulk_data, selector, ec2));
383
384 selector = fix.m_partB & fix.m_partC;
385 EXPECT_TRUE(areAllEntitiesSelected(fix.m_bulk_data, selector, ec3));
386
387 selector = (!fix.m_partB) & fix.m_partC;
388 EXPECT_TRUE(areAllEntitiesSelected(fix.m_bulk_data, selector, ec4));
389
390 selector = !(fix.m_partA | fix.m_partB | fix.m_partC | fix.m_partD);
391 EXPECT_TRUE(areAllEntitiesSelected(fix.m_bulk_data, selector, ec5));
392
393 check_bucket_ids_testset_A(bucket_repository);
394 }
395
396 /// Test Partition::sort()
397 TEST( UnitTestPartition, Partition_testSort)
398 {
399 SelectorFixture fix;
400
401 if (fix.m_bulk_data.parallel_size() > 1)
402 {
403 return;
404 }
405 initialize_unsorted(fix);
406 setFieldDataUsingEntityIDs(fix);
407
408 stk::mesh::impl::BucketRepository &bucket_repository = fix.m_bulk_data.my_get_bucket_repository();
409 bucket_repository.sync_from_partitions();
410
411 std::vector<stk::mesh::impl::Partition *> partitions =
412 bucket_repository.get_partitions(stk::topology::NODE_RANK);
413 size_t num_partitions = partitions.size();
414 EXPECT_EQ(num_partitions, 5u);
415
416 for (size_t i = 0; i < num_partitions; ++i)
417 {
418 stk::mesh::impl::Partition &partition = *partitions[i];
419 // check_test_partition_invariant(fix, partition);
420 partition.sort(stk::mesh::impl::GlobalIdEntitySorter());
421 check_test_partition_invariant(fix, partition);
422 }
423
424 check_bucket_ids_testset_A(bucket_repository);
425 }
426
427 /// Test Partition::remove(.)
428 TEST( UnitTestPartition, Partition_testRemove)
429 {
430 SelectorFixture fix;
431
432 if (fix.m_bulk_data.parallel_size() > 1)
433 {
434 return;
435 }
436 initializeFivePartitionsWithSixBucketsEach(fix);
437 setFieldDataUsingEntityIDs(fix);
438
439 stk::mesh::impl::BucketRepository &bucket_repository = fix.m_bulk_data.my_get_bucket_repository();
440 bucket_repository.sync_from_partitions();
441
442 std::vector<stk::mesh::impl::Partition *> partitions =
443 bucket_repository.get_partitions(stk::topology::NODE_RANK);
444 size_t num_partitions = partitions.size();
445 EXPECT_EQ(num_partitions, 5u);
446
447 for (size_t i = 0; i < num_partitions; ++i)
448 {
449 stk::mesh::impl::Partition &partition = *partitions[i];
450
451 size_t old_size = partition.size();
452 size_t num_removed = 0;
453
454 // Remove non-last entity in a bucket.
455 stk::mesh::Bucket &bkt_0 = **partition.begin();
456 stk::mesh::Entity entity = bkt_0[0];
457 partition.remove(entity);
458 // fix.m_bulk_data.my_set_mesh_index(entity, 0, 0);
459 ++num_removed;
460
461 // Remove last entity in a bucket.
462 stk::mesh::Bucket &bkt_1 = **partition.begin();
463 stk::mesh::Entity e_last_in_1 = bkt_1[bkt_1.size() - 1];
464 partition.remove(e_last_in_1);
465 // fix.m_bulk_data.my_set_mesh_index(e_last_in_1, 0, 0);
466 ++num_removed;
467
468 // Need to sort before checking whether the invariant holds.
469 partition.default_sort_if_needed();
470
471 EXPECT_EQ(old_size, partition.size() + num_removed);
472 check_test_partition_invariant(fix, partition);
473 }
474
475 check_bucket_ids_testset_A(bucket_repository);
476
477 //
478 // Now's a good time to exercise BucketRepository::sync_from_partitions().
479 //
480 bucket_repository.sync_from_partitions();
481 partitions = bucket_repository.get_partitions(stk::topology::NODE_RANK);
482 num_partitions = partitions.size();
483 EXPECT_EQ(num_partitions, 5u);
484
485 for (size_t i = 0; i < num_partitions; ++i)
486 {
487 stk::mesh::impl::Partition &partition = *partitions[i];
488 check_test_partition_invariant(fix, partition);
489 }
490
491 check_bucket_ids_testset_A(bucket_repository);
492 }
493
494 /// Test Partition::add(.).
495 TEST( UnitTestPartition, Partition_testAdd)
496 {
497 SelectorFixture fix;
498
499 if (fix.m_bulk_data.parallel_size() > 1)
500 {
501 return;
502 }
503 initializeFivePartitionsWithSixBucketsEach(fix);
504 setFieldDataUsingEntityIDs(fix);
505
506 stk::mesh::impl::BucketRepository &bucket_repository = fix.m_bulk_data.my_get_bucket_repository();
507 bucket_repository.sync_from_partitions();
508
509 std::vector<stk::mesh::impl::Partition *> partitions =
510 bucket_repository.get_partitions(stk::topology::NODE_RANK);
511 size_t num_partitions = partitions.size();
512 EXPECT_EQ(num_partitions, 5u);
513
514 std::vector<stk::mesh::Entity> first_entities(num_partitions);
515 std::vector<size_t> old_sizes(num_partitions);
516
517 // Get the first entity in each partition.
518 for (size_t i = 0; i < num_partitions; ++i)
519 {
520 stk::mesh::impl::Partition &partition = *partitions[i];
521 stk::mesh::Bucket &bkt = **partition.begin();
522 first_entities[i] = bkt[0];
523 old_sizes[i] = partition.size();
524 }
525
526 // Now remove them from those partitions.
527 for (size_t i = 0; i < num_partitions; ++i)
528 {
529 stk::mesh::Entity entity = first_entities[i];
530 partitions[i]->remove(entity);
531 fix.m_bulk_data.my_set_mesh_index(entity, 0, 0);
532 }
533
534 const stk::mesh::BulkData& mesh = fix.m_bulk_data;
535
536 // "Rotate" each former first entity to another partition.
537 for (size_t i = 0; i < num_partitions; ++i)
538 {
539 size_t dst_partition_idx = (i + 1) % num_partitions;
540 stk::mesh::impl::Partition &dst_partition = *partitions[dst_partition_idx];
541 stk::mesh::Entity entity = first_entities[i];
542 dst_partition.add(entity);
543 stk::mesh::Bucket &bkt = mesh.bucket(entity);
544 double *field_data = stk::mesh::field_data(*fix.m_fieldABC, bkt, 0);
545 if (field_data)
546 {
547 field_data[mesh.bucket_ordinal(entity)] = mesh.identifier(entity);
548 }
549 }
550
551 // Because the each first entity had a low identifier, it will again be
552 // the first entity in its new partition.
553 for (size_t i = 0; i < num_partitions; ++i)
554 {
555 stk::mesh::impl::Partition &partition = *partitions[i];
556 partition.default_sort_if_needed();
557 EXPECT_EQ(old_sizes[i], partition.size());
558 check_test_partition_invariant(fix, partition);
559 }
560
561 check_bucket_ids_testset_A(bucket_repository);
562 }
563
564 /// Test Partition::move_to(..)
565 TEST( UnitTestPartition, Partition_testMoveTo)
566 {
567 SelectorFixture fix;
568
569 if (fix.m_bulk_data.parallel_size() > 1)
570 {
571 return;
572 }
573 initializeFivePartitionsWithSixBucketsEach(fix);
574 setFieldDataUsingEntityIDs(fix);
575
576 stk::mesh::impl::BucketRepository &bucket_repository = fix.m_bulk_data.my_get_bucket_repository();
577 bucket_repository.sync_from_partitions();
578
579 std::vector<stk::mesh::impl::Partition *> all_partitions =
580 bucket_repository.get_partitions(stk::topology::NODE_RANK);
581 const size_t num_all_partitions = all_partitions.size();
582 EXPECT_EQ(num_all_partitions, 5u);
583
584 const size_t num_data_partitions = num_all_partitions - 1;
585 std::vector<stk::mesh::impl::Partition *> data_partitions(num_data_partitions);
586 std::copy(all_partitions.begin() + 1, all_partitions.end(), &data_partitions[0]);
587
588 std::vector<stk::mesh::Entity> first_entities(num_data_partitions);
589 std::vector<size_t> old_sizes(num_data_partitions);
590
591 // Get the first entity in each partition.
592 for (size_t i = 0; i < num_data_partitions; ++i)
593 {
594 stk::mesh::impl::Partition &partition = *data_partitions[i];
595 stk::mesh::Bucket &bkt = **partition.begin();
596 first_entities[i] = bkt[0];
597 old_sizes[i] = partition.size();
598 }
599
600 // "Rotate" each first entity to another partition.
601 for (size_t i = 0; i < num_data_partitions; ++i)
602 {
603 size_t dst_partition_idx = (i + 1) % num_data_partitions;
604 stk::mesh::impl::Partition &dst_partition = *data_partitions[dst_partition_idx];
605 data_partitions[i]->move_to(first_entities[i], dst_partition);
606 }
607
608 // Because the each first entity had a low identifier, it will again be
609 // the first entity in its new partition.
610 for (size_t i = 0; i < num_data_partitions; ++i)
611 {
612 stk::mesh::impl::Partition &partition = *data_partitions[i];
613 partition.default_sort_if_needed();
614
615 EXPECT_EQ(old_sizes[i], partition.size());
616 check_test_partition_invariant(fix, partition);
617 }
618
619 check_bucket_ids_testset_A(bucket_repository);
620 }
621
622 // Test the OrdinalVector version of get_or_create_partition(..).
623 TEST( UnitTestPartition, Partition_testGetOrCreateOV)
624 {
625 SelectorFixture fix;
626
627 if (fix.m_bulk_data.parallel_size() > 1)
628 {
629 return;
630 }
631 initializeFivePartitionsWithSixBucketsEach(fix);
632 setFieldDataUsingEntityIDs(fix);
633
634 stk::mesh::impl::BucketRepository &bucket_repository = fix.m_bulk_data.my_get_bucket_repository();
635 bucket_repository.sync_from_partitions();
636
637 stk::mesh::OrdinalVector scratch;
638 std::vector<stk::mesh::PartOrdinal> parts;
639 parts.push_back(fix.m_meta_data.universal_part().mesh_meta_data_ordinal());
640 parts.push_back(fix.m_meta_data.locally_owned_part().mesh_meta_data_ordinal());
641 parts.push_back(fix.m_partA.mesh_meta_data_ordinal() );
642 stk::mesh::impl::Partition *partitionA =
643 bucket_repository.get_or_create_partition(stk::topology::NODE_RANK, parts);
644 ASSERT_TRUE(0 != partitionA);
645 size_t numEntitiesPerPartition = 3000;
646 size_t bucketCapacity = stk::mesh::get_default_bucket_capacity();
647 size_t expectedNumBucketsPerPartition = (numEntitiesPerPartition + (bucketCapacity - 1u)) / bucketCapacity;
648 EXPECT_EQ(expectedNumBucketsPerPartition, partitionA->num_buckets());
649
650 parts.push_back(fix.m_partC.mesh_meta_data_ordinal());
651 stk::mesh::impl::Partition *partitionAC =
652 bucket_repository.get_or_create_partition(stk::topology::NODE_RANK, parts);
653 ASSERT_TRUE(0 != partitionAC);
654 EXPECT_EQ(0u, partitionAC->num_buckets());
655
656 stk::mesh::impl::Partition *partitionAC_again =
657 bucket_repository.get_or_create_partition(stk::topology::NODE_RANK, parts);
658 ASSERT_TRUE(partitionAC == partitionAC_again);
659
660 check_bucket_ids_testset_A(bucket_repository);
661 }
662
663 /** \} */
664
665 } // namespace
666
667 /// Test Partition::move_to(..) more rigorously
668 TEST( UnitTestPartition, Partition_testMoveToBetter)
669 {
670 SelectorFixture fix;
671
672 if (fix.m_bulk_data.parallel_size() > 1)
673 {
674 return;
675 }
676 initializeFivePartitionsWithSixBucketsEach(fix);
677 setFieldDataUsingEntityIDs(fix);
678
679 stk::mesh::impl::BucketRepository &bucket_repository = fix.m_bulk_data.my_get_bucket_repository();
680 bucket_repository.sync_from_partitions();
681
682 std::vector<stk::mesh::impl::Partition *> all_partitions =
683 bucket_repository.get_partitions(stk::topology::NODE_RANK);
684 size_t num_all_partitions = all_partitions.size();
685 EXPECT_EQ(num_all_partitions, 5u);
686
687 const size_t num_data_partitions = num_all_partitions - 1;
688 std::vector<stk::mesh::impl::Partition *> data_partitions(num_data_partitions);
689 std::copy(all_partitions.begin() + 1, all_partitions.end(), &data_partitions[0]);
690
691 std::vector<std::vector<stk::mesh::Entity> > entities_to_move(num_data_partitions);
692 std::vector<size_t> old_sizes(num_data_partitions);
693
694 // Get the entities from the first bucket in each partition.
695 for (size_t i = 0; i < num_data_partitions; ++i)
696 {
697 stk::mesh::impl::Partition &partition = *data_partitions[i];
698 stk::mesh::Bucket &bkt = **partition.begin();
699
700 size_t bkt_sz = bkt.size();
701 const size_t default_bucket_capacity = stk::mesh::get_default_bucket_capacity();
702 EXPECT_EQ(bkt_sz, default_bucket_capacity);
703 for (size_t j = 0; j < bkt_sz; ++j)
704 {
705 entities_to_move[i].push_back(bkt[j]);
706 }
707 old_sizes[i] = partition.size();
708 EXPECT_EQ(old_sizes[i], 3000u);
709 }
710
711 // "Rotate" the entities to another partition.
712 for (size_t i = 0; i < num_data_partitions; ++i)
713 {
714 size_t dst_partition_idx = (i + 1) % num_data_partitions;
715 stk::mesh::impl::Partition &dst_partition = *data_partitions[dst_partition_idx];
716
717 size_t num_to_move = entities_to_move[i].size();
718 // std::cout << "Moving entities from " << *data_partitions[i] << " to " << dst_partition
719 // << "\n starting with " << entities_to_move[i][0] << " and ending with "
720 // << entities_to_move[i][num_to_move - 1] << std::endl;
721 for (size_t j = 0; j < num_to_move; ++j)
722 {
723 data_partitions[i]->move_to(entities_to_move[i][j], dst_partition);
724 }
725 }
726
727 for (size_t i = 0; i < num_data_partitions; ++i)
728 {
729 stk::mesh::impl::Partition &partition = *data_partitions[i];
730 partition.default_sort_if_needed();
731
732 EXPECT_EQ(partition.size(), old_sizes[i]);
733
734 // std::cout << "Check " << partition << std::endl;
735 // std::cout << "source partition was " << src_partition << std::endl;
736
737 EXPECT_EQ(old_sizes[i], partition.size());
738 check_test_partition_invariant(fix, partition);
739 }
740 }