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   stk::mesh::impl::EntityRepository e;
00160 
00161   e.comm_clear( elem );
00162 
00163   e.comm_clear_ghosting( elem );
00164 
00165   const stk::mesh::Ghosting & ghost = bulk.shared_aura();
00166 
00167   bulk.modification_end();
00168 
00169   STKUNIT_ASSERT_FALSE(e.erase_ghosting(elem, ghost));
00170 
00171   const stk::mesh::EntityCommInfo comm_info( ghost.ordinal() , 0 );
00172 
00173   STKUNIT_ASSERT_FALSE(e.erase_comm_info(elem, comm_info));
00174 
00175   STKUNIT_ASSERT(e.insert_comm_info(elem, comm_info));
00176 
00177   //Checking internal_create_entity
00178 
00179   e.internal_create_entity( stk::mesh::EntityKey( 3, 2 ));
00180   e.internal_create_entity( stk::mesh::EntityKey( 3, 5 ));
00181   e.internal_create_entity( stk::mesh::EntityKey( 3, 7 ));
00182 
00183   //Checking get_entity with invalid key - no rank or id
00184   {
00185     int ok = 0 ;
00186     try {
00187 
00188       stk::mesh::Entity * elem3 = e.get_entity(stk::mesh::EntityKey());
00189       if(elem3){
00190         // CAROL FIXME
00191       }
00192 
00193     }
00194     catch( const std::exception & x ) {
00195       ok = 1 ;
00196       std::cout << "UnitTestMetaData CORRECTLY caught error for : "
00197                 << x.what()
00198                 << std::endl ;
00199     }
00200     if ( ! ok ) {
00201       throw std::runtime_error("UnitTestMetaData FAILED to catch error for get_entity - invalid key");
00202     }
00203   }
00204 }
00205 
00206 STKUNIT_UNIT_TEST( UnitTestMetaData, noEntityTypes )
00207 {
00208   //MetaData constructor fails because there are no entity types:
00209   std::vector<std::string> empty_names;
00210   STKUNIT_ASSERT_THROW(
00211     MetaData metadata(empty_names),
00212     std::runtime_error
00213     );
00214 }
00215 STKUNIT_UNIT_TEST( UnitTestMetaData, declare_part_with_rank )
00216 {
00217   //MetaData constructor fails because there are no entity types:
00218   const int spatial_dimension = 3;
00219   MetaData metadata(stk::mesh::fem::entity_rank_names(spatial_dimension));
00220   metadata.declare_part("foo");
00221   STKUNIT_ASSERT_NO_THROW(metadata.declare_part("foo",1));
00222   STKUNIT_ASSERT_NO_THROW(metadata.declare_part("foo",1));
00223 
00224   // Should throw because we're trying to change rank
00225   STKUNIT_ASSERT_THROW(metadata.declare_part("foo",2),std::runtime_error);
00226 
00227   // Should not throw since we did not provide rank
00228   metadata.declare_part("foo");
00229 }
00230 
00231 STKUNIT_UNIT_TEST( UnitTestMetaData, declare_attribute_no_delete )
00232 {
00233   //Coverage of declare_attribute_no_delete in MetaData.hpp
00234   const CellTopologyData * singleton = NULL;
00235   const int spatial_dimension = 3;
00236   MetaData metadata(stk::mesh::fem::entity_rank_names(spatial_dimension));
00237   Part &pa = metadata.declare_part( std::string("a") , 0 );
00238   metadata.declare_attribute_no_delete( pa, singleton);
00239   metadata.commit();
00240 }
00241 
00242 }
00243 //----------------------------------------------------------------------
00244 
00245 
00246 
00247 
00248 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends