Sierra Toolkit Version of the Day
UnitTestField.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 
00010 #include <stk_util/unit_test_support/stk_utest_macros.hpp>
00011 
00012 #include <stdexcept>
00013 #include <iostream>
00014 #include <stk_util/parallel/Parallel.hpp>
00015 #include <stk_mesh/base/MetaData.hpp>
00016 #include <stk_mesh/base/BulkData.hpp>
00017 
00018 #include <stk_mesh/base/FieldData.hpp>
00019 #include <stk_mesh/fem/EntityRanks.hpp>
00020 #include <stk_mesh/fem/CoordinateSystems.hpp>
00021 #include <stk_mesh/fem/TopologicalMetaData.hpp>
00022 
00023 namespace {
00024 void testCartesian();
00025 void testCylindrical();
00026 void testFullTensor();
00027 void testSymmetricTensor();
00028 void testFieldDataArray( stk::ParallelMachine pm );
00029 
00030 STKUNIT_UNIT_TEST(UnitTestField, testUnit)
00031 {
00032 #if defined( STK_HAS_MPI )
00033   stk::ParallelMachine pworld = MPI_COMM_WORLD ;
00034   stk::ParallelMachine pself  = MPI_COMM_SELF ;
00035 #else
00036   stk::ParallelMachine pworld = stk::parallel_machine_null();
00037   stk::ParallelMachine pself  = stk::parallel_machine_null();
00038 #endif
00039   if ( 0 == stk::parallel_machine_rank( pworld ) ) {
00040     // Nothing parallel being tested, only run on one process
00041     testCartesian();
00042     testCylindrical();
00043     testFullTensor();
00044     testSymmetricTensor();
00045     testFieldDataArray( pself );
00046   }
00047 }
00048 
00049 //----------------------------------------------------------------------
00050 //----------------------------------------------------------------------
00051 
00052 SHARDS_ARRAY_DIM_TAG_SIMPLE_DECLARATION( ATAG )
00053 SHARDS_ARRAY_DIM_TAG_SIMPLE_DECLARATION( BTAG )
00054 SHARDS_ARRAY_DIM_TAG_SIMPLE_DECLARATION( CTAG )
00055 
00056 template< class FieldType >
00057 void print_bucket_array( const FieldType & f , const stk::mesh::Bucket & k )
00058 {
00059   typedef stk::mesh::BucketArray< FieldType > ArrayType ;
00060 
00061   try {
00062     ArrayType a( f , k.begin(), k.end() );
00063     ArrayType b( f , k );
00064 
00065     std::cout << "  BucketArray[" << f.name() << "](" ;
00066 
00067     if ( a.size() != b.size() ) {
00068       throw std::runtime_error("UnitTestField FAILED BucketArray dimensions not consistant with Bucket::iterator");
00069     }
00070 
00071     if ( a.size() ) {
00072       for ( unsigned i = 0 ; i < ArrayType::Rank ; ++i ) {
00073         if ( i ) { std::cout << "," ; }
00074         std::cout << a.dimension(i);
00075         if (a.dimension(i) != b.dimension(i)) {
00076           throw std::runtime_error("UnitTestField FAILED BucketArray dimensions not consistant with Bucket::iterator");
00077         }
00078       }
00079     }
00080     std::cout << ")" << std::endl ;
00081   }
00082   catch( const std::exception & ) {
00083   }
00084 }
00085 
00086 void testCartesian()
00087 {
00088   const stk::mesh::Cartesian&  cartesian_tag = stk::mesh::Cartesian::tag();
00089 
00090   std::string to_str = cartesian_tag.to_string(3, 1);
00091   std::string expected_str("y");
00092   STKUNIT_ASSERT_EQUAL( to_str == expected_str, true );
00093 
00094   //should throw if we supply a size < 3:
00095   STKUNIT_ASSERT_THROW( cartesian_tag.to_string(2, 1), std::runtime_error );
00096 
00097   shards::ArrayDimTag::size_type expected_idx = 1;
00098   shards::ArrayDimTag::size_type idx = cartesian_tag.to_index(3, "y");
00099 
00100   STKUNIT_ASSERT_EQUAL( idx, expected_idx );
00101 
00102   //should throw if we supply a "z" along with size==2:
00103   STKUNIT_ASSERT_THROW( cartesian_tag.to_index(2, "z"), std::runtime_error );
00104 }
00105 
00106 void testCylindrical()
00107 {
00108   const stk::mesh::Cylindrical&  cylindrical_tag = stk::mesh::Cylindrical::tag();
00109 
00110   std::string to_str = cylindrical_tag.to_string(3, 1);
00111   std::string expected_str("a");
00112   STKUNIT_ASSERT_EQUAL( to_str == expected_str, true );
00113 
00114   //should throw if we supply a size < 3:
00115   STKUNIT_ASSERT_THROW( cylindrical_tag.to_string(2, 1), std::runtime_error );
00116 
00117   shards::ArrayDimTag::size_type expected_idx = 1;
00118   shards::ArrayDimTag::size_type idx = cylindrical_tag.to_index(3, "a");
00119 
00120   STKUNIT_ASSERT_EQUAL( idx, expected_idx );
00121 
00122   //should throw if we supply a "z" along with size==2:
00123   STKUNIT_ASSERT_THROW( cylindrical_tag.to_index(2, "z"), std::runtime_error );
00124 }
00125 
00126 void testFullTensor()
00127 {
00128   const stk::mesh::FullTensor&  fulltensor_tag = stk::mesh::FullTensor::tag();
00129 
00130   std::string to_str = fulltensor_tag.to_string(9, 1);
00131   std::string expected_str("yx");
00132   STKUNIT_ASSERT_EQUAL( to_str == expected_str, true );
00133 
00134   //should throw if we supply a size < 9:
00135   STKUNIT_ASSERT_THROW( fulltensor_tag.to_string(2, 1), std::runtime_error );
00136 
00137   shards::ArrayDimTag::size_type expected_idx = 1;
00138   shards::ArrayDimTag::size_type idx = fulltensor_tag.to_index(9, "yx");
00139 
00140   STKUNIT_ASSERT_EQUAL( idx, expected_idx );
00141 
00142   //should throw if we supply a "zz" along with size==2:
00143   STKUNIT_ASSERT_THROW( fulltensor_tag.to_index(2, "zz"), std::runtime_error );
00144 }
00145 
00146 void testSymmetricTensor()
00147 {
00148   const stk::mesh::SymmetricTensor&  symmetrictensor_tag = stk::mesh::SymmetricTensor::tag();
00149 
00150   std::string to_str = symmetrictensor_tag.to_string(9, 1);
00151   std::string expected_str("yx");
00152   STKUNIT_ASSERT_EQUAL( to_str == expected_str, true );
00153 
00154   //should throw if we supply a size < 9:
00155   STKUNIT_ASSERT_THROW( symmetrictensor_tag.to_string(2, 1), std::runtime_error );
00156 
00157 
00158   shards::ArrayDimTag::size_type expected_idx = 1;
00159   shards::ArrayDimTag::size_type idx = symmetrictensor_tag.to_index(6, "yy");
00160 
00161   STKUNIT_ASSERT_EQUAL( idx, expected_idx );
00162 
00163   //should throw if we supply a "xz" along with size==5:
00164   STKUNIT_ASSERT_THROW( symmetrictensor_tag.to_index(5, "xz"), std::runtime_error );
00165 }
00166 
00167 void testFieldDataArray( stk::ParallelMachine pm )
00168 {
00169   typedef stk::mesh::Field<double>                rank_zero_field ;
00170   typedef stk::mesh::Field<double,ATAG>           rank_one_field ;
00171   typedef stk::mesh::Field<double,ATAG,BTAG>      rank_two_field ;
00172   typedef stk::mesh::Field<double,ATAG,BTAG,CTAG> rank_three_field ;
00173 
00174   std::cout << "UnitTestField BEGIN:" << std::endl ;
00175 
00176   const std::string name0("test_field_0");
00177   const std::string name1("test_field_1");
00178   const std::string name2("test_field_2");
00179   const std::string name3("test_field_3");
00180 
00181   const int spatial_dimension = 3;
00182   stk::mesh::MetaData meta_data( stk::mesh::TopologicalMetaData::entity_rank_names(spatial_dimension) );
00183   stk::mesh::BulkData bulk_data( meta_data , pm );
00184   stk::mesh::TopologicalMetaData top_data( meta_data, spatial_dimension );
00185 
00186   rank_zero_field  & f0 = meta_data.declare_field< rank_zero_field >( name0 );
00187   rank_one_field   & f1 = meta_data.declare_field< rank_one_field >(  name1 );
00188   rank_three_field & f3 = meta_data.declare_field< rank_three_field >( name3 );
00189   rank_two_field   & f2 = meta_data.declare_field< rank_two_field >(  name2 );
00190 
00191   {
00192     int ok = 0 ;
00193     try {
00194       typedef stk::mesh::Field<double,CTAG> error_type ;
00195       meta_data.declare_field< error_type >( name1 );
00196     }
00197     catch( const std::exception & x ) {
00198       ok = 1 ;
00199       std::cout << "UnitTestField CORRECTLY caught error: "
00200                 << x.what()
00201                 << std::endl ;
00202     }
00203     if ( ! ok ) {
00204       throw std::runtime_error("UnitTestField FAILED to catch error");
00205     }
00206   }
00207 
00208   stk::mesh::Part & p0 = meta_data.declare_part("P0", top_data.node_rank );
00209   stk::mesh::Part & p1 = meta_data.declare_part("P1", top_data.node_rank );
00210   stk::mesh::Part & p2 = meta_data.declare_part("P2", top_data.node_rank );
00211   stk::mesh::Part & p3 = meta_data.declare_part("P3", top_data.node_rank );
00212 
00213   stk::mesh::put_field( f0 , top_data.node_rank , p0 );
00214   stk::mesh::put_field( f1 , top_data.node_rank , p1 , 10 );
00215   stk::mesh::put_field( f2 , top_data.node_rank , p2 , 10 , 20 );
00216   stk::mesh::put_field( f3 , top_data.node_rank , p3 , 10 , 20 , 30 );
00217 
00218   stk::mesh::print( std::cout , "  " , f0 ); std::cout << std::endl ;
00219 
00220   meta_data.commit();
00221 
00222   bulk_data.modification_begin();
00223 
00224   for ( unsigned i = 1 ; i < 11 ; ++i ) {
00225     bulk_data.declare_entity( top_data.node_rank , i ,
00226                               std::vector< stk::mesh::Part * >( 1 , & p0 ) );
00227   }
00228 
00229   for ( unsigned i = 11 ; i < 21 ; ++i ) {
00230     bulk_data.declare_entity( top_data.node_rank , i ,
00231                               std::vector< stk::mesh::Part * >( 1 , & p1 ) );
00232   }
00233 
00234   for ( unsigned i = 21 ; i < 31 ; ++i ) {
00235     bulk_data.declare_entity( top_data.node_rank , i ,
00236                               std::vector< stk::mesh::Part * >( 1 , & p2 ) );
00237   }
00238 
00239   for ( unsigned i = 31 ; i < 41 ; ++i ) {
00240     bulk_data.declare_entity( top_data.node_rank , i ,
00241                               std::vector< stk::mesh::Part * >( 1 , & p3 ) );
00242   }
00243 
00244   const std::vector< stk::mesh::Bucket *> & node_buckets =
00245     bulk_data.buckets( top_data.node_rank );
00246 
00247   for ( std::vector< stk::mesh::Bucket *>::const_iterator
00248         ik = node_buckets.begin() ; ik != node_buckets.end() ; ++ik ) {
00249     stk::mesh::Bucket & k = **ik ;
00250 
00251     std::vector< stk::mesh::Part * > parts ;
00252     k.supersets( parts );
00253 
00254     std::cout << "Bucket:" ;
00255     for ( std::vector< stk::mesh::Part * >::iterator
00256           ip = parts.begin() ; ip != parts.end() ; ++ip ) {
00257       std::cout << " " << (*ip)->name();
00258     }
00259     std::cout << std::endl ;
00260 
00261     print_bucket_array( f0 , k );
00262     print_bucket_array( f1 , k );
00263     print_bucket_array( f2 , k );
00264     print_bucket_array( f3 , k );
00265   }
00266 
00267   std::cout << "UnitTestField END" << std::endl ;
00268 }
00269 
00270 SHARDS_ARRAY_DIM_TAG_SIMPLE_IMPLEMENTATION( ATAG )
00271 SHARDS_ARRAY_DIM_TAG_SIMPLE_IMPLEMENTATION( BTAG )
00272 SHARDS_ARRAY_DIM_TAG_SIMPLE_IMPLEMENTATION( CTAG )
00273 
00274 }//namespace <anonymous>
00275 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends