Sierra Toolkit  Version of the Day
UnitTestEntity.cpp
1 /*------------------------------------------------------------------------*/
2 /* Copyright 2010 Sandia Corporation. */
3 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */
4 /* license for use of this work by or on behalf of the U.S. Government. */
5 /* Export of this program may require a license from the */
6 /* United States Government. */
7 /*------------------------------------------------------------------------*/
8 
9 
10 #include <sstream>
11 #include <stdexcept>
12 #include <map>
13 
14 #include <stk_util/unit_test_support/stk_utest_macros.hpp>
15 
16 #include <stk_util/parallel/Parallel.hpp>
17 
18 #include <stk_mesh/base/Types.hpp>
19 #include <stk_mesh/base/MetaData.hpp>
20 #include <stk_mesh/base/Entity.hpp>
21 #include <stk_mesh/base/EntityKey.hpp>
22 #include <stk_mesh/base/Field.hpp>
23 #include <stk_mesh/base/Bucket.hpp>
24 #include <stk_mesh/base/BulkData.hpp>
25 #include <stk_mesh/base/Ghosting.hpp>
26 #include <stk_mesh/base/Field.hpp>
27 
28 #include <stk_mesh/baseImpl/EntityRepository.hpp>
29 
30 #include <stk_mesh/fem/FEMMetaData.hpp>
31 
35 using stk::mesh::Part;
39 using stk::mesh::Entity;
40 using stk::mesh::Bucket;
41 using stk::mesh::impl::PartRepository;
42 using stk::mesh::impl::EntityRepository;
43 
44 namespace {
45 
46 //----------------------------------------------------------------------
47 
48 STKUNIT_UNIT_TEST(UnitTestEntity,testEntityKey)
49 {
50  EntityKey key_bad_zero = EntityKey();
51  EntityKey key_good_0_1 = EntityKey( 0 , 1 );
52  EntityKey key_good_1_1 = EntityKey( 1 , 1 );
53  EntityKey key_good_2_10 = EntityKey( 2 , 10);
54  EntityKey key_order_1_12 = EntityKey( 1 , 12 );
55  EntityKey key_order_2_10 = EntityKey( 2 , 10 );
56 
57  STKUNIT_ASSERT( ! entity_key_valid( key_bad_zero ) );
58  STKUNIT_ASSERT( entity_key_valid( key_good_0_1 ) );
59  STKUNIT_ASSERT( entity_key_valid( key_good_1_1 ) );
60  STKUNIT_ASSERT( entity_key_valid( key_good_2_10 ) );
61 
62  STKUNIT_ASSERT( 0 == entity_rank( key_good_0_1));
63  STKUNIT_ASSERT( 1 == entity_rank( key_good_1_1) );
64  STKUNIT_ASSERT( 2 == entity_rank( key_good_2_10) );
65  STKUNIT_ASSERT( 1 == entity_id( key_good_0_1) );
66  STKUNIT_ASSERT( 1 == entity_id( key_good_1_1) );
67  STKUNIT_ASSERT( 10 == entity_id( key_good_2_10) );
68 
69  STKUNIT_ASSERT( key_order_1_12 < key_order_2_10);
70  STKUNIT_ASSERT( !( key_order_1_12 > key_order_2_10));
71 
72 #ifndef NDEBUG
73  STKUNIT_ASSERT_THROW( EntityKey( ~0u , 1 ) , std::logic_error );
74  STKUNIT_ASSERT_THROW( EntityKey( 0 , ~stk::mesh::EntityKey::raw_key_type(0) ) , std::logic_error );
75 #endif // NDEBUG
76 }
77 
78 
79 STKUNIT_UNIT_TEST(UnitTestEntity,testEntityRepository)
80 {
81  //Test Entity repository - covering EntityRepository.cpp/hpp
82  const int spatial_dimension = 3;
83  MetaData meta(stk::mesh::fem::entity_rank_names(spatial_dimension));
84  Part & part = meta.declare_part( "another part");
85  MPI_Barrier( MPI_COMM_WORLD );
86  ParallelMachine pm = MPI_COMM_WORLD;
87  BulkData bulk( meta , pm, 200 );
88  const int rank = stk::parallel_machine_rank( pm );
89  const int size = stk::parallel_machine_size( pm );
90  std::vector<stk::mesh::Part *> add_part;
91  meta.commit();
92 
93  // Bail if not parallel. This test involves inducing errorneous conditions that
94  // are only checked-for in parallel.
95  if (size == 1) {
96  return;
97  }
98 
99  add_part.push_back ( & part );
100 
101  bulk.modification_begin();
102 
103  int id_base = 0;
104  for ( id_base = 0 ; id_base < 97 ; ++id_base )
105  {
106  int new_id = size * id_base + rank;
107  bulk.declare_entity( 0 , new_id+1 , add_part );
108  }
109 
110  int new_id = size * (++id_base) + rank;
111  stk::mesh::Entity & elem = bulk.declare_entity( 3 , new_id+1 , add_part );
112 
113  bool use_memory_pool = false;
114  stk::mesh::impl::EntityRepository e(use_memory_pool);
115 
116  e.comm_clear( elem );
117 
118  e.comm_clear_ghosting( elem );
119 
120  const stk::mesh::Ghosting & ghost = bulk.shared_aura();
121 
122  STKUNIT_ASSERT_FALSE(e.erase_ghosting(elem, ghost));
123 
124  const stk::mesh::EntityCommInfo comm_info( ghost.ordinal() , 0 );
125 
126  STKUNIT_ASSERT_FALSE(e.erase_comm_info(elem, comm_info));
127 
128  STKUNIT_ASSERT(e.insert_comm_info(elem, comm_info));
129 
130  //Coverage of verfify_parallel_attributes in BulkDataParallelVerify.cpp
131  //for owned_closure = 1 AND recv_ghost = 1.
132  //Also uses pack and unpack in DataTraits.cpp, DataTraitsClass.hpp and DataTraitsEnum.hpp
133  STKUNIT_ASSERT_THROW(bulk.modification_end(), std::runtime_error);
134 
135  bulk.modification_begin();
136 
137 
138  //Checking internal_create_entity
139 
140  e.internal_create_entity( stk::mesh::EntityKey( 3, 2 ));
141  e.internal_create_entity( stk::mesh::EntityKey( 3, 5 ));
142  e.internal_create_entity( stk::mesh::EntityKey( 3, 7 ));
143 
144  //Checking get_entity with invalid key - no rank or id
145  {
146  STKUNIT_ASSERT_THROW(
147  e.get_entity(stk::mesh::EntityKey()),
148  std::runtime_error
149  );
150  }
151 
152  // stk::mesh::impl::EntityRepository::EntityMap eMap;
153  stk::mesh::Entity & elem2 = bulk.declare_entity( 3 , new_id+8 , add_part );
154  stk::mesh::Entity & elem3 = bulk.declare_entity( 3 , new_id+9 , add_part );
155  stk::mesh::Entity & elem4 = bulk.declare_entity( 3 , new_id+10 , add_part );
156 
157  e.internal_create_entity( stk::mesh::EntityKey( 3, new_id+8 ));
158  e.internal_create_entity( stk::mesh::EntityKey( 3, new_id+9 ));
159  e.internal_create_entity( stk::mesh::EntityKey( 3, new_id+10 ));
160 
161  typedef std::map<EntityKey,Entity*> EntityMap;
162  EntityMap entity_map_array;
163 
164  entity_map_array[stk::mesh::EntityKey( 3, new_id+8 )] = &elem2;
165  entity_map_array[stk::mesh::EntityKey( 3, new_id+9 )] = &elem3;
166  entity_map_array[stk::mesh::EntityKey( 3, new_id+10 )] = &elem4;
167 
168  //Coverage of destroy_later in EntityRepository.cpp
169  Bucket *nil_bucket = bulk.buckets(3)[0];
170  e.destroy_later(elem2, nil_bucket);
171  //Call a second time for more coverage
172  STKUNIT_ASSERT_THROW(e.destroy_later(elem2, nil_bucket), std::runtime_error);
173 
174  //Coverage of !comm_mesh_verify_parallel_consistency in BulkDataEndSync.cpp
175  //in internal_modification_end function
176  Bucket *nil_bucket2 = bulk.buckets(0)[0];
177 
178  STKUNIT_ASSERT ( nil_bucket2 != NULL);
179 
180  e.destroy_later(elem3, nil_bucket2);
181 
182  STKUNIT_ASSERT_THROW(bulk.modification_end(), std::runtime_error);
183 
184  bulk.modification_begin();
185 
186  STKUNIT_ASSERT_THROW(e.destroy_later(elem2, nil_bucket), std::runtime_error);
187 
188  STKUNIT_ASSERT_THROW(bulk.modification_end(), std::runtime_error);
189 
190  bulk.modification_begin();
191  STKUNIT_ASSERT_THROW(bulk.modification_end(), std::runtime_error);
192 }
193 
194 //----------------------------------------------------------------------
195 }//namespace <anonymous>
196 
std::vector< Part * > PartVector
Collections of parts are frequently maintained as a vector of Part pointers.
Definition: Types.hpp:31
The manager of an integrated collection of parts and fields.
Definition: MetaData.hpp:56
EntityRank entity_rank(const EntityKey &key)
Given an entity key, return an entity type (rank).
MPI_Comm ParallelMachine
Definition: Parallel.hpp:32
EntityId entity_id(const EntityKey &key)
Given an entity key, return the identifier for the entity.
unsigned parallel_machine_size(ParallelMachine parallel_machine)
Member function parallel_machine_size ...
Definition: Parallel.cpp:18
A fundamental unit within the discretization of a problem domain, including but not limited to nodes...
Definition: Entity.hpp:120
A container for the field data of a homogeneous collection of entities.
Definition: Bucket.hpp:94
unsigned parallel_machine_rank(ParallelMachine parallel_machine)
Member function parallel_machine_rank ...
Definition: Parallel.cpp:29
A defined entity-relationship between parts. An internal class that should never need to be directly...
Integer type for the entity keys, which is an encoding of the entity type and entity identifier...
An application-defined subset of a problem domain.
Definition: Part.hpp:49
Manager for an integrated collection of entities, entity relations, and buckets of field data...
Definition: BulkData.hpp:49
bool entity_key_valid(const EntityKey &key)
Query if an entity key is valid.
Data for ghosting mesh entities.
Definition: Ghosting.hpp:28
unsigned ordinal() const
Ordinal to identify the ghosting subset.
Definition: Ghosting.hpp:35