Sierra Toolkit Version of the Day
UnitTestMetaData.cpp
00001 /*------------------------------------------------------------------------*/
00002 /*                 Copyright 2010 Sandia Corporation.                     */
00003 /*  Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive   */
00004 /*  license for use of this work by or on behalf of the U.S. Government.  */
00005 /*  Export of this program may require a license from the                 */
00006 /*  United States Government.                                             */
00007 /*------------------------------------------------------------------------*/
00008 
00009 #include <sstream>
00010 #include <stdexcept>
00011 
00012 #include <stk_util/unit_test_support/stk_utest_macros.hpp>
00013 
00014 #include <stk_util/parallel/Parallel.hpp>
00015 
00016 #include <stk_mesh/base/MetaData.hpp>
00017 
00018 #include <Shards_BasicTopologies.hpp>
00019 #include <stk_mesh/base/Part.hpp>
00020 #include <stk_mesh/baseImpl/PartRepository.hpp>
00021 #include <stk_mesh/baseImpl/EntityRepository.hpp>
00022 #include <stk_mesh/baseImpl/FieldBaseImpl.hpp>
00023 
00024 #include <stk_mesh/base/FieldRelation.hpp>
00025 #include <stk_mesh/base/PartRelation.hpp>
00026 
00027 #include <stk_mesh/fem/Stencils.hpp>
00028 #include <stk_mesh/fem/FEMMetaData.hpp>
00029 #include <stk_mesh/fem/FEMHelpers.hpp>
00030 
00031 using stk::mesh::MetaData;
00032 using stk::mesh::Part;
00033 using stk::mesh::PartVector;
00034 using stk::mesh::PartRelation;
00035 using stk::mesh::EntityRank;
00036 using std::cout;
00037 using std::endl;
00038 
00039 //----------------------------------------------------------------------
00040 
00041 namespace {
00042 int stencil_test_function( unsigned  from_type ,
00043                            unsigned  to_type ,
00044                            unsigned  identifier )
00045 {
00046   return 0;
00047 }
00048 
00049 
00050 
00051 STKUNIT_UNIT_TEST( UnitTestMetaData, testMetaData )
00052 {
00053   //Test functions in MetaData.cpp
00054   const int spatial_dimension = 3;
00055   const std::vector<std::string> & rank_names = stk::mesh::fem::entity_rank_names(spatial_dimension);
00056   MetaData metadata_committed(rank_names);
00057   MetaData metadata_not_committed(rank_names);
00058   MetaData metadata(rank_names);
00059 
00060   Part &pa = metadata.declare_part( std::string("a") , 0 );
00061   Part &pb = metadata.declare_part( std::string("b") , 0 );
00062   Part &pc = metadata.declare_part( std::string("c") , 0 );
00063   Part &pd = metadata.declare_part( std::string("d") , 0 );
00064   Part &pe = metadata.declare_part( std::string("e") , 0 );
00065   Part &pf = metadata.declare_part( std::string("f") , 0 );
00066   Part &pg = metadata.declare_part( std::string("g") , 0 );
00067   Part &ph = metadata.declare_part( std::string("h") , 0 );
00068   PartVector part_vector;
00069   metadata_committed.commit();
00070 
00071   //test get_part with part that does not exist
00072   std::string test_string = "this_part_does_not_exist";
00073   STKUNIT_ASSERT_THROW( metadata_committed.get_part(test_string,"test_throw"),std::runtime_error);
00074 
00075   //test get_part with valid part
00076   STKUNIT_ASSERT( metadata.get_part(std::string("a"),"do_not_throw"));
00077 
00078   //test declare part
00079   metadata.declare_part_relation( pe,stencil_test_function, pg);
00080 
00081   part_vector.push_back(& pa);
00082   part_vector.push_back(& pb);
00083   part_vector.push_back(& pc);
00084   part_vector.push_back(& pd);
00085 
00086   //Part * const intersection_part = &
00087   metadata.declare_part(part_vector);
00088 
00089   //Test declare_part_subset
00090   STKUNIT_ASSERT_THROW(  metadata.declare_part_subset( pe, pe), std::runtime_error);
00091 
00092   //Test declare_part_relation with parts that are not subsets of each other
00093   STKUNIT_ASSERT_THROW(  metadata.declare_part_relation( pg,stencil_test_function, ph), std::logic_error);
00094 
00095   //Test declare_part_relation with a NULL stencil function
00096   STKUNIT_ASSERT_THROW(  metadata.declare_part_relation( pe,NULL, pe), std::runtime_error);
00097 
00098   //Test declare_part_relation with parts that are subsets of each other
00099   metadata.declare_part_subset( pd, pf);
00100   STKUNIT_ASSERT_THROW(  metadata.declare_part_relation( pd,stencil_test_function, pf), std::runtime_error);
00101 
00102   metadata.commit();
00103 }
00104 
00105 STKUNIT_UNIT_TEST( UnitTestMetaData, rankHigherThanDefined )
00106 {
00107   //Test function entity_rank_name in MetaData.cpp
00108   const int spatial_dimension = 3;
00109   const std::vector<std::string> & rank_names = stk::mesh::fem::entity_rank_names(spatial_dimension);
00110   MetaData metadata(rank_names);
00111   int i = 2;
00112 
00113   const std::string& i_name2 =  metadata.entity_rank_name( i );
00114 
00115   STKUNIT_ASSERT( i_name2 == rank_names[i] );
00116 
00117   EntityRank one_rank_higher_than_defined = rank_names.size();
00118 
00119   STKUNIT_ASSERT_THROW(
00120     metadata.entity_rank_name( one_rank_higher_than_defined ),
00121     std::runtime_error
00122                         );
00123 }
00124 
00125 STKUNIT_UNIT_TEST( UnitTestMetaData, testEntityRepository )
00126 {
00127   static const size_t spatial_dimension = 3;
00128 
00129   //Test Entity repository - covering EntityRepository.cpp/hpp
00130   stk::mesh::MetaData meta ( stk::mesh::fem::entity_rank_names(spatial_dimension) );
00131   stk::mesh::Part & part = meta.declare_part("another part");
00132 
00133   meta.commit();
00134 
00135   stk::mesh::BulkData bulk ( meta , MPI_COMM_WORLD , 100 );
00136   std::vector<stk::mesh::Part *>  add_part;
00137   add_part.push_back ( &part );
00138 
00139   int  size , rank;
00140   rank = stk::parallel_machine_rank( MPI_COMM_WORLD );
00141   size = stk::parallel_machine_size( MPI_COMM_WORLD );
00142   PartVector tmp(1);
00143 
00144   bulk.modification_begin();
00145 
00146   int id_base = 0;
00147   for ( id_base = 0 ; id_base < 97 ; ++id_base )
00148   {
00149     int new_id = size * id_base + rank;
00150     bulk.declare_entity( 0 , new_id+1 , add_part );
00151   }
00152 
00153   int new_id = size * (++id_base) + rank;
00154   stk::mesh::Entity & elem  = bulk.declare_entity( 3 , new_id+1 , add_part );
00155 
00156   //new_id = size * (++id_base) + rank;
00157   // stk::mesh::Entity & elem2  = bulk.declare_entity( 3 , new_id+1 , add_part );
00158 
00159   bool use_memory_pool = false;
00160   stk::mesh::impl::EntityRepository e(use_memory_pool);
00161 
00162   e.comm_clear( elem );
00163 
00164   e.comm_clear_ghosting( elem );
00165 
00166   const stk::mesh::Ghosting & ghost = bulk.shared_aura();
00167 
00168   bulk.modification_end();
00169 
00170   STKUNIT_ASSERT_FALSE(e.erase_ghosting(elem, ghost));
00171 
00172   const stk::mesh::EntityCommInfo comm_info( ghost.ordinal() , 0 );
00173 
00174   STKUNIT_ASSERT_FALSE(e.erase_comm_info(elem, comm_info));
00175 
00176   STKUNIT_ASSERT(e.insert_comm_info(elem, comm_info));
00177 
00178   //Checking internal_create_entity
00179 
00180   e.internal_create_entity( stk::mesh::EntityKey( 3, 2 ));
00181   e.internal_create_entity( stk::mesh::EntityKey( 3, 5 ));
00182   e.internal_create_entity( stk::mesh::EntityKey( 3, 7 ));
00183 
00184   //Checking get_entity with invalid key - no rank or id
00185   {
00186     int ok = 0 ;
00187     try {
00188 
00189       stk::mesh::Entity * elem3 = e.get_entity(stk::mesh::EntityKey());
00190       if(elem3){
00191         // CAROL FIXME
00192       }
00193 
00194     }
00195     catch( const std::exception & x ) {
00196       ok = 1 ;
00197       std::cout << "UnitTestMetaData CORRECTLY caught error for : "
00198                 << x.what()
00199                 << std::endl ;
00200     }
00201     if ( ! ok ) {
00202       throw std::runtime_error("UnitTestMetaData FAILED to catch error for get_entity - invalid key");
00203     }
00204   }
00205 }
00206 
00207 STKUNIT_UNIT_TEST( UnitTestMetaData, noEntityTypes )
00208 {
00209   //MetaData constructor fails because there are no entity types:
00210   std::vector<std::string> empty_names;
00211   STKUNIT_ASSERT_THROW(
00212     MetaData metadata(empty_names),
00213     std::runtime_error
00214     );
00215 }
00216 STKUNIT_UNIT_TEST( UnitTestMetaData, declare_part_with_rank )
00217 {
00218   //MetaData constructor fails because there are no entity types:
00219   const int spatial_dimension = 3;
00220   MetaData metadata(stk::mesh::fem::entity_rank_names(spatial_dimension));
00221   metadata.declare_part("foo");
00222   STKUNIT_ASSERT_NO_THROW(metadata.declare_part("foo",1));
00223   STKUNIT_ASSERT_NO_THROW(metadata.declare_part("foo",1));
00224 
00225   // Should throw because we're trying to change rank
00226   STKUNIT_ASSERT_THROW(metadata.declare_part("foo",2),std::runtime_error);
00227 
00228   // Should not throw since we did not provide rank
00229   metadata.declare_part("foo");
00230 }
00231 
00232 STKUNIT_UNIT_TEST( UnitTestMetaData, declare_attribute_no_delete )
00233 {
00234   //Coverage of declare_attribute_no_delete in MetaData.hpp
00235   const CellTopologyData * singleton = NULL;
00236   const int spatial_dimension = 3;
00237   MetaData metadata(stk::mesh::fem::entity_rank_names(spatial_dimension));
00238   Part &pa = metadata.declare_part( std::string("a") , 0 );
00239   metadata.declare_attribute_no_delete( pa, singleton);
00240   metadata.commit();
00241 }
00242 
00243 }
00244 //----------------------------------------------------------------------
00245 
00246 
00247 
00248 
00249 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines