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