21 #include <unit_tests/stk_utest_macros.hpp> 23 #include <stk_util/parallel/Parallel.hpp> 25 #include <stk_mesh/base/BulkData.hpp> 26 #include <stk_mesh/base/GetEntities.hpp> 27 #include <stk_mesh/base/Field.hpp> 28 #include <stk_mesh/base/FieldData.hpp> 29 #include <stk_mesh/base/Comm.hpp> 30 #include <stk_mesh/base/GetBuckets.hpp> 33 #include <stk_mesh/fixtures/BoxFixture.hpp> 39 STKUNIT_UNIT_TEST(UnitTestTransaction, verifyBulkOnCreate)
47 std::vector<stk::mesh::Part *> add_part;
48 add_part.push_back ( &new_part );
54 for (
int i = 0 ; i != 100 ; i++ )
56 int new_id = size*i+rank;
57 bulk.declare_entity ( 0 , new_id+1 , add_part );
59 bulk.modification_end();
63 STKUNIT_ASSERT ( bulk.get_transaction_log().get_inserted_buckets(0).size() == 0 );
64 STKUNIT_ASSERT ( bulk.get_transaction_log().get_modified_buckets(0).size() == 0 );
65 STKUNIT_ASSERT ( bulk.get_transaction_log().get_deleted_buckets(0).size() == 0 );
69 bulk.get_transaction_log().get_parts_with_inserted_entities ( inserted_parts );
70 STKUNIT_ASSERT ( inserted_parts.size() > 0 );
78 STKUNIT_UNIT_TEST(UnitTestTransaction, verifyBulkInsert)
86 add_part.push_back ( &new_part );
89 if ( fixture.comm_size() > 1 )
return;
91 bulk.reset_transaction ( stk::mesh::Transaction::BULK );
93 bulk.
declare_entity ( 0 , fixture.comm_size()*1000 + fixture.comm_rank() , add_part );
97 STKUNIT_ASSERT ( bulk.get_transaction_log().get_inserted_buckets(0).size() == 0 );
102 bulk.get_transaction_log().get_parts_with_inserted_entities ( inserted_parts );
103 for (
size_t i = 0 ; i != inserted_parts.size() ; i++ )
105 if ( inserted_parts[i] == &new_part )
108 STKUNIT_ASSERT ( found );
113 bulk.get_transaction_log().get_parts_with_modified_entities ( modified_parts );
114 for (
size_t i = 0 ; i != modified_parts.size() ; i++ )
116 if ( modified_parts[i] == &new_part )
119 STKUNIT_ASSERT ( !found );
124 bulk.get_transaction_log().get_parts_with_deleted_entities ( deleted_parts );
125 for (
size_t i = 0 ; i != deleted_parts.size() ; i++ )
127 if ( deleted_parts[i] == &new_part )
130 STKUNIT_ASSERT ( !found );
139 STKUNIT_UNIT_TEST(UnitTestTransaction, verifyBulkModify)
147 add_part.push_back ( &new_part );
150 if ( fixture.comm_size() > 1 )
return;
152 bulk.reset_transaction ( stk::mesh::Transaction::BULK );
157 bulk.reset_transaction ( stk::mesh::Transaction::BULK );
162 STKUNIT_ASSERT ( bulk.get_transaction_log().get_modified_buckets(0).size() == 0 );
166 bulk.get_transaction_log().get_parts_with_inserted_entities ( inserted_parts );
167 for (
size_t i = 0 ; i != inserted_parts.size() ; i++ )
169 if ( inserted_parts[i] == &new_part )
172 STKUNIT_ASSERT ( !found );
176 bulk.get_transaction_log().get_parts_with_modified_entities ( modified_parts );
177 for (
size_t i = 0 ; i != modified_parts.size() ; i++ )
179 if ( modified_parts[i] == &new_part )
182 STKUNIT_ASSERT ( found );
186 bulk.get_transaction_log().get_parts_with_deleted_entities ( deleted_parts );
187 for (
size_t i = 0 ; i != deleted_parts.size() ; i++ )
189 if ( deleted_parts[i] == &new_part )
192 STKUNIT_ASSERT ( !found );
200 STKUNIT_UNIT_TEST(UnitTestTransaction, verifyBulkAddRelation)
209 const stk::mesh::Transaction &log = bulk.get_transaction_log();
210 add_part.push_back ( &new_part );
213 if ( fixture.comm_size() > 1 )
return;
215 bulk.reset_transaction ( stk::mesh::Transaction::BULK );
223 log.get_parts_with_inserted_entities ( buffer_vec );
224 STKUNIT_ASSERT ( buffer_vec.size() > 0u );
228 log.get_parts_with_modified_entities ( buffer_vec );
229 STKUNIT_ASSERT ( buffer_vec.size() > 0u );
239 STKUNIT_UNIT_TEST(UnitTestTransaction, verifyBulkDelete)
247 add_part.push_back ( &new_part );
251 if ( fixture.comm_size() > 1 )
return;
253 bulk.reset_transaction ( stk::mesh::Transaction::BULK );
258 bulk.reset_transaction ( stk::mesh::Transaction::BULK );
263 STKUNIT_ASSERT ( bulk.get_transaction_log().get_deleted_buckets(0).size() == 0 );
268 bool inserted_found =
false;
269 bulk.get_transaction_log().get_parts_with_inserted_entities ( inserted_parts );
270 for (
size_t i = 0 ; i != deleted_parts.size() ; i++ )
272 if ( inserted_parts[i] == &new_part )
273 inserted_found =
true;
275 STKUNIT_ASSERT ( !inserted_found );
277 bool modified_found =
false;
278 bulk.get_transaction_log().get_parts_with_modified_entities ( modified_parts );
279 for (
size_t i = 0 ; i != deleted_parts.size() ; i++ )
281 if ( modified_parts[i] == &new_part )
282 modified_found =
true;
284 STKUNIT_ASSERT ( !modified_found );
286 bool deleted_found =
false;
287 bulk.get_transaction_log().get_parts_with_deleted_entities ( deleted_parts );
288 for (
size_t i = 0 ; i != deleted_parts.size() ; i++ )
290 if ( deleted_parts[i] == &new_part )
291 deleted_found =
true;
293 STKUNIT_ASSERT ( deleted_found );
301 STKUNIT_UNIT_TEST(UnitTestTransaction, verifyTransactionSpanningModifications)
321 add_part.push_back ( &new_part );
324 if ( fixture.comm_size() > 1 )
return;
329 bulk.reset_transaction ( stk::mesh::Transaction::INCREMENTAL );
341 STKUNIT_ASSERT ( bulk.get_transaction_log().get_modified_buckets(0).size() == 1 );
342 STKUNIT_ASSERT ( bulk.get_transaction_log().get_inserted_buckets(1).size() == 1 );
344 bulk.reset_transaction ( stk::mesh::Transaction::INCREMENTAL );
346 STKUNIT_ASSERT ( bulk.get_transaction_log().get_inserted_buckets(0).size() == 0 );
347 STKUNIT_ASSERT ( bulk.get_transaction_log().get_inserted_buckets(1).size() == 0 );
354 STKUNIT_ASSERT_THROW ( bulk.reset_transaction () , std::runtime_error );
364 STKUNIT_UNIT_TEST(UnitTestTransaction, verifyIncrementalInsert)
372 const stk::mesh::Transaction &log = bulk.get_transaction_log();
373 add_part.push_back ( &new_part );
376 if ( fixture.comm_size() > 1 )
return;
378 bulk.reset_transaction ( stk::mesh::Transaction::INCREMENTAL );
382 entities[0] = &bulk.
declare_entity ( 0 , 123456789 , blank_part );
383 entities[1] = &bulk.
declare_entity ( 1 , 123456789 , blank_part );
384 entities[2] = &bulk.
declare_entity ( 2 , 123456789 , blank_part );
385 entities[3] = &bulk.
declare_entity ( 3 , 123456789 , blank_part );
396 for (
unsigned i = 0 ; i != 3 ; i++ )
399 STKUNIT_ASSERT_EQUAL ( log.get_inserted_buckets(i).size() , 1u );
401 STKUNIT_ASSERT_EQUAL ( log.get_inserted_buckets(i)[0]->size() , 1u );
403 stk::mesh::Entity &new_entity = *((*log.get_inserted_buckets(i).begin())->begin());
405 STKUNIT_ASSERT_EQUAL ( &new_entity , entities[i] );
407 STKUNIT_ASSERT_EQUAL ( log.get_modified_buckets(i).size() , 0u );
408 STKUNIT_ASSERT_EQUAL ( log.get_deleted_buckets(i).size() , 0u );
412 STKUNIT_ASSERT_EQUAL ( log.get_modified_buckets(3).size() , 0u );
413 STKUNIT_ASSERT_EQUAL ( log.get_deleted_buckets(3).size() , 0u );
414 STKUNIT_ASSERT_EQUAL ( log.get_inserted_buckets(3).size() , 0u );
418 STKUNIT_UNIT_TEST(UnitTestTransaction, verifyIncrementalModify)
427 const stk::mesh::Transaction &log = bulk.get_transaction_log();
428 add_part.push_back ( &new_part );
431 if ( fixture.comm_size() > 1 )
return;
434 bulk.reset_transaction ( stk::mesh::Transaction::INCREMENTAL );
437 entities[0] = &*bulk.
buckets(0)[0]->begin();
438 entities[1] = &*bulk.
buckets(3)[0]->begin();
443 for (
unsigned i = 0 ; i != 2 ; i++ )
445 unsigned enttype = i*3;
447 STKUNIT_ASSERT_EQUAL ( log.get_modified_buckets(enttype).size() , 1u );
449 STKUNIT_ASSERT_EQUAL ( log.get_modified_buckets(enttype)[0]->size() , 1u );
452 STKUNIT_ASSERT_EQUAL ( &mod_entity , entities[i] );
454 STKUNIT_ASSERT_EQUAL ( log.get_inserted_buckets(enttype).size() , 0u );
455 STKUNIT_ASSERT_EQUAL ( log.get_deleted_buckets(enttype).size() , 0u );
460 STKUNIT_ASSERT ( mod_entity.transaction_bucket() != 0 );
461 STKUNIT_ASSERT ( !mod_entity.transaction_bucket()->member ( new_part ) );
463 mod_entity.transaction_bucket()->supersets ( modified_bucket_parts );
464 STKUNIT_ASSERT ( mod_entity.bucket().member_all ( modified_bucket_parts ));
469 STKUNIT_UNIT_TEST(UnitTestTransaction, verifyIncrementalAddRelation)
478 const stk::mesh::Transaction &log = bulk.get_transaction_log();
479 add_part.push_back ( &new_part );
482 if ( fixture.comm_size() > 1 )
return;
484 bulk.reset_transaction ( stk::mesh::Transaction::INCREMENTAL );
492 STKUNIT_ASSERT_EQUAL ( log.get_inserted_buckets(0).size() , 1u );
493 STKUNIT_ASSERT_EQUAL ( log.get_inserted_buckets(0)[0]->size() , 1u );
494 STKUNIT_ASSERT_EQUAL ( log.get_modified_buckets(0).size() , 0u );
495 STKUNIT_ASSERT_EQUAL ( &*log.get_inserted_buckets(0)[0]->begin() , &new_node );
498 STKUNIT_ASSERT_EQUAL ( log.get_modified_buckets(3).size() , 1u );
499 STKUNIT_ASSERT_EQUAL ( log.get_modified_buckets(3)[0]->size() , 1u );
500 STKUNIT_ASSERT_EQUAL ( &*log.get_modified_buckets(3)[0]->begin() , &existing_cell );
504 STKUNIT_ASSERT ( existing_cell.transaction_bucket() != 0 );
505 existing_cell.transaction_bucket()->supersets ( old_parts );
508 STKUNIT_ASSERT ( existing_cell.transaction_bucket()->member_all ( new_parts ) );
513 STKUNIT_UNIT_TEST(UnitTestTransaction, verifyIncrementalDelete)
522 const stk::mesh::Transaction &log = bulk.get_transaction_log();
523 add_part.push_back ( &new_part );
526 if ( fixture.comm_size() > 1 )
return;
530 bulk.reset_transaction ( stk::mesh::Transaction::INCREMENTAL );
536 stk::mesh::EntityId deleted_cell_id = deleted_cell->
identifier();
541 STKUNIT_ASSERT_EQUAL ( log.get_deleted_buckets(3).size() , 1u );
542 STKUNIT_ASSERT_EQUAL ( log.get_deleted_buckets(3)[0]->size() , 1u );
543 STKUNIT_ASSERT_EQUAL ( (*log.get_deleted_buckets(3)[0]->begin()).identifier() , deleted_cell_id );
546 deleted_cell = &*log.get_deleted_buckets(3)[0]->begin();
547 STKUNIT_ASSERT ( deleted_cell->transaction_bucket() != 0 );
548 STKUNIT_ASSERT ( deleted_cell->transaction_bucket()->member_all ( old_parts ) );
550 deleted_cell->transaction_bucket()->supersets ( old_in_trans );
551 STKUNIT_ASSERT_EQUAL ( old_in_trans.size() , old_parts.size() );
554 STKUNIT_UNIT_TEST(UnitTestTransaction, verifyParallelChangeOwnership)
563 add_part.push_back ( &new_part );
566 if ( fixture.comm_size() < 4 )
return;
571 if ( fixture.comm_rank() < 3 )
575 bulk.reset_transaction ( stk::mesh::Transaction::INCREMENTAL );
576 std::vector <stk::mesh::EntityProc> change_owner;
578 if ( fixture.comm_rank() == entity->
owner_rank() )
580 int other_rank = fixture.comm_rank()==0?1:0;
581 change_owner.push_back ( std::make_pair ( entity , other_rank ) );
599 STKUNIT_UNIT_TEST(UnitTestTransaction, verifyParallelResolutionModify)
607 const stk::mesh::Transaction &log = bulk.get_transaction_log();
609 add_part.push_back ( &new_part );
612 if ( fixture.comm_size() == 1 )
return;
617 const std::vector<stk::mesh::EntityProc> &shared_entities = bulk.shared_entities();
619 for (
unsigned i = 0 ; i != shared_entities.size() ;i++ )
621 if ( shared_entities[i].first->entity_rank() == 0 )
624 node_to_modify = shared_entities[i].first;
632 stk::mesh::EntityId *found_node_id_list =
new stk::mesh::EntityId [ bulk.
parallel_size() ];
635 stk::mesh::EntityId node_id = node_to_modify ? node_to_modify->
identifier() : 0;
636 int found_a_node = node_to_modify ? 1 : 0;
638 MPI_Allgather ( &found_a_node , 1 , MPI_INT , found_node_list , 1 , MPI_INT , bulk.
parallel() );
639 MPI_Allgather ( &node_id , 1 , MPI_INT , found_node_id_list , 1 , MPI_INT , bulk.
parallel() );
643 bulk.reset_transaction ( stk::mesh::Transaction::INCREMENTAL );
645 if ( node_to_modify )
651 std::vector<stk::mesh::Bucket *>::const_iterator cur_modified_node_bucket = log.get_modified_buckets(0).begin();
652 while ( cur_modified_node_bucket != log.get_modified_buckets(0).end() )
654 stk::mesh::BucketIterator cur_modified_node = (*cur_modified_node_bucket)->begin();
655 while ( cur_modified_node != (*cur_modified_node_bucket)->begin() )
659 bool valid_change =
false;
661 if ( found_node_list[i] == 1 )
662 if ( cur_modified_node->identifier() == found_node_id_list[i] )
664 STKUNIT_ASSERT ( valid_change );
667 ++cur_modified_node_bucket;
670 delete [] found_node_list;
671 delete [] found_node_id_list;
void change_entity_parts(Entity &entity, const PartVector &add_parts, const PartVector &remove_parts=PartVector())
Change the parallel-locally-owned entity's part membership by adding and/or removing parts...
std::vector< Part * > PartVector
Collections of parts are frequently maintained as a vector of Part pointers.
void declare_relation(Entity &e_from, Entity &e_to, const RelationIdentifier local_id)
Declare a relation and its converse between entities in the same mesh.
unsigned owner_rank() const
Parallel processor rank of the processor which owns this entity.
void change_entity_owner(const std::vector< EntityProc > &arg_change)
Give away ownership of entities to other parallel processes.
unsigned parallel_machine_size(ParallelMachine parallel_machine)
Member function parallel_machine_size ...
A fundamental unit within the discretization of a problem domain, including but not limited to nodes...
EntityId identifier() const
Identifier for this entity which is globally unique for a given entity type.
unsigned parallel_machine_rank(ParallelMachine parallel_machine)
Member function parallel_machine_rank ...
void supersets(PartVector &) const
This bucket is a subset of these parts.
bool modification_end()
Parallel synchronization of modifications and transition to the guaranteed parallel consistent state...
Bucket & bucket() const
The bucket which holds this mesh entity's field data.
ParallelMachine parallel() const
The parallel machine.
An application-defined subset of a problem domain.
Manager for an integrated collection of entities, entity relations, and buckets of field data...
const std::vector< Bucket * > & buckets(EntityRank rank) const
Query all buckets of a given entity rank.
bool member_all(const PartVector &) const
Bucket is a subset of all of the given parts.
Entity & declare_entity(EntityRank ent_rank, EntityId ent_id, const PartVector &parts)
Create or retrieve a locally owned entity of a given rank and id.
void generate_boxes(const BOX root_box, BOX local_box)
bool destroy_entity(Entity *&entity)
Request the destruction an entity on the local process.
bool modification_begin()
Begin a modification phase during which the mesh bulk data could become parallel inconsistent. This is a parallel synchronous call. The first time this method is called the mesh meta data is verified to be committed and parallel consistent. An exception is thrown if this verification fails.
unsigned parallel_size() const
Size of the parallel machine.