Sierra Toolkit  Version of the Day
UnitTestMetaData.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 #include <sstream>
10 #include <stdexcept>
11 
12 #include <stk_util/unit_test_support/stk_utest_macros.hpp>
13 
14 #include <stk_util/parallel/Parallel.hpp>
15 
16 #include <stk_mesh/base/MetaData.hpp>
17 
18 #include <Shards_BasicTopologies.hpp>
19 #include <stk_mesh/base/Part.hpp>
20 #include <stk_mesh/baseImpl/PartRepository.hpp>
21 #include <stk_mesh/baseImpl/EntityRepository.hpp>
22 #include <stk_mesh/baseImpl/FieldBaseImpl.hpp>
23 
24 #include <stk_mesh/base/FieldRelation.hpp>
25 #include <stk_mesh/base/PartRelation.hpp>
26 
27 #include <stk_mesh/fem/Stencils.hpp>
28 #include <stk_mesh/fem/FEMMetaData.hpp>
29 #include <stk_mesh/fem/FEMHelpers.hpp>
30 
32 using stk::mesh::Part;
35 using stk::mesh::EntityRank;
36 using std::cout;
37 using std::endl;
38 
39 //----------------------------------------------------------------------
40 
41 namespace {
42 int stencil_test_function( unsigned from_type ,
43  unsigned to_type ,
44  unsigned identifier )
45 {
46  return 0;
47 }
48 
49 
50 
51 STKUNIT_UNIT_TEST( UnitTestMetaData, testMetaData )
52 {
53  //Test functions in MetaData.cpp
54  const int spatial_dimension = 3;
55  const std::vector<std::string> & rank_names = stk::mesh::fem::entity_rank_names(spatial_dimension);
56  MetaData metadata_committed(rank_names);
57  MetaData metadata_not_committed(rank_names);
58  MetaData metadata(rank_names);
59 
60  Part &pa = metadata.declare_part( std::string("a") , 0 );
61  Part &pb = metadata.declare_part( std::string("b") , 0 );
62  Part &pc = metadata.declare_part( std::string("c") , 0 );
63  Part &pd = metadata.declare_part( std::string("d") , 0 );
64  Part &pe = metadata.declare_part( std::string("e") , 0 );
65  Part &pf = metadata.declare_part( std::string("f") , 0 );
66  Part &pg = metadata.declare_part( std::string("g") , 0 );
67  Part &ph = metadata.declare_part( std::string("h") , 0 );
68  PartVector part_vector;
69  metadata_committed.commit();
70 
71  //test get_part with part that does not exist
72  std::string test_string = "this_part_does_not_exist";
73  STKUNIT_ASSERT_THROW( metadata_committed.get_part(test_string,"test_throw"),std::runtime_error);
74 
75  //test get_part with valid part
76  STKUNIT_ASSERT( metadata.get_part(std::string("a"),"do_not_throw"));
77 
78  //test declare part
79  metadata.declare_part_relation( pe,stencil_test_function, pg);
80 
81  part_vector.push_back(& pa);
82  part_vector.push_back(& pb);
83  part_vector.push_back(& pc);
84  part_vector.push_back(& pd);
85 
86  //Part * const intersection_part = &
87  metadata.declare_part(part_vector);
88 
89  //Test declare_part_subset
90  STKUNIT_ASSERT_THROW( metadata.declare_part_subset( pe, pe), std::runtime_error);
91 
92  //Test declare_part_relation with parts that are not subsets of each other
93  STKUNIT_ASSERT_THROW( metadata.declare_part_relation( pg,stencil_test_function, ph), std::logic_error);
94 
95  //Test declare_part_relation with a NULL stencil function
96  STKUNIT_ASSERT_THROW( metadata.declare_part_relation( pe,NULL, pe), std::runtime_error);
97 
98  //Test declare_part_relation with parts that are subsets of each other
99  metadata.declare_part_subset( pd, pf);
100  STKUNIT_ASSERT_THROW( metadata.declare_part_relation( pd,stencil_test_function, pf), std::runtime_error);
101 
102  metadata.commit();
103 }
104 
105 STKUNIT_UNIT_TEST( UnitTestMetaData, rankHigherThanDefined )
106 {
107  //Test function entity_rank_name in MetaData.cpp
108  const int spatial_dimension = 3;
109  const std::vector<std::string> & rank_names = stk::mesh::fem::entity_rank_names(spatial_dimension);
110  MetaData metadata(rank_names);
111  int i = 2;
112 
113  const std::string& i_name2 = metadata.entity_rank_name( i );
114 
115  STKUNIT_ASSERT( i_name2 == rank_names[i] );
116 
117  EntityRank one_rank_higher_than_defined = rank_names.size();
118 
119  STKUNIT_ASSERT_THROW(
120  metadata.entity_rank_name( one_rank_higher_than_defined ),
121  std::runtime_error
122  );
123 }
124 
125 STKUNIT_UNIT_TEST( UnitTestMetaData, testEntityRepository )
126 {
127  static const size_t spatial_dimension = 3;
128 
129  //Test Entity repository - covering EntityRepository.cpp/hpp
130  stk::mesh::MetaData meta ( stk::mesh::fem::entity_rank_names(spatial_dimension) );
131  stk::mesh::Part & part = meta.declare_part("another part");
132 
133  meta.commit();
134 
135  stk::mesh::BulkData bulk ( meta , MPI_COMM_WORLD , 100 );
136  std::vector<stk::mesh::Part *> add_part;
137  add_part.push_back ( &part );
138 
139  int size , rank;
140  rank = stk::parallel_machine_rank( MPI_COMM_WORLD );
141  size = stk::parallel_machine_size( MPI_COMM_WORLD );
142  PartVector tmp(1);
143 
144  bulk.modification_begin();
145 
146  int id_base = 0;
147  for ( id_base = 0 ; id_base < 97 ; ++id_base )
148  {
149  int new_id = size * id_base + rank;
150  bulk.declare_entity( 0 , new_id+1 , add_part );
151  }
152 
153  int new_id = size * (++id_base) + rank;
154  stk::mesh::Entity & elem = bulk.declare_entity( 3 , new_id+1 , add_part );
155 
156  //new_id = size * (++id_base) + rank;
157  // stk::mesh::Entity & elem2 = bulk.declare_entity( 3 , new_id+1 , add_part );
158 
159  bool use_memory_pool = false;
160  stk::mesh::impl::EntityRepository e(use_memory_pool);
161 
162  e.comm_clear( elem );
163 
164  e.comm_clear_ghosting( elem );
165 
166  const stk::mesh::Ghosting & ghost = bulk.shared_aura();
167 
168  bulk.modification_end();
169 
170  STKUNIT_ASSERT_FALSE(e.erase_ghosting(elem, ghost));
171 
172  const stk::mesh::EntityCommInfo comm_info( ghost.ordinal() , 0 );
173 
174  STKUNIT_ASSERT_FALSE(e.erase_comm_info(elem, comm_info));
175 
176  STKUNIT_ASSERT(e.insert_comm_info(elem, comm_info));
177 
178  //Checking internal_create_entity
179 
180  e.internal_create_entity( stk::mesh::EntityKey( 3, 2 ));
181  e.internal_create_entity( stk::mesh::EntityKey( 3, 5 ));
182  e.internal_create_entity( stk::mesh::EntityKey( 3, 7 ));
183 
184  //Checking get_entity with invalid key - no rank or id
185  {
186  int ok = 0 ;
187  try {
188 
189  stk::mesh::Entity * elem3 = e.get_entity(stk::mesh::EntityKey());
190  if(elem3){
191  // CAROL FIXME
192  }
193 
194  }
195  catch( const std::exception & x ) {
196  ok = 1 ;
197  std::cout << "UnitTestMetaData CORRECTLY caught error for : "
198  << x.what()
199  << std::endl ;
200  }
201  if ( ! ok ) {
202  throw std::runtime_error("UnitTestMetaData FAILED to catch error for get_entity - invalid key");
203  }
204  }
205 }
206 
207 STKUNIT_UNIT_TEST( UnitTestMetaData, noEntityTypes )
208 {
209  //MetaData constructor fails because there are no entity types:
210  std::vector<std::string> empty_names;
211  STKUNIT_ASSERT_THROW(
212  MetaData metadata(empty_names),
213  std::runtime_error
214  );
215 }
216 STKUNIT_UNIT_TEST( UnitTestMetaData, declare_part_with_rank )
217 {
218  //MetaData constructor fails because there are no entity types:
219  const int spatial_dimension = 3;
220  MetaData metadata(stk::mesh::fem::entity_rank_names(spatial_dimension));
221  metadata.declare_part("foo");
222  STKUNIT_ASSERT_NO_THROW(metadata.declare_part("foo",1));
223  STKUNIT_ASSERT_NO_THROW(metadata.declare_part("foo",1));
224 
225  // Should throw because we're trying to change rank
226  STKUNIT_ASSERT_THROW(metadata.declare_part("foo",2),std::runtime_error);
227 
228  // Should not throw since we did not provide rank
229  metadata.declare_part("foo");
230 }
231 
232 STKUNIT_UNIT_TEST( UnitTestMetaData, declare_attribute_no_delete )
233 {
234  //Coverage of declare_attribute_no_delete in MetaData.hpp
235  const CellTopologyData * singleton = NULL;
236  const int spatial_dimension = 3;
237  MetaData metadata(stk::mesh::fem::entity_rank_names(spatial_dimension));
238  Part &pa = metadata.declare_part( std::string("a") , 0 );
239  metadata.declare_attribute_no_delete( pa, singleton);
240  metadata.commit();
241 }
242 
243 }
244 //----------------------------------------------------------------------
245 
246 
247 
248 
249 
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
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
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
Data for ghosting mesh entities.
Definition: Ghosting.hpp:28
unsigned ordinal() const
Ordinal to identify the ghosting subset.
Definition: Ghosting.hpp:35