Sierra Toolkit Version of the Day
MetaData.hpp
00001 /*------------------------------------------------------------------------*/
00002 /*                 Copyright 2010, 2011 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 #ifndef stk_mesh_MetaData_hpp
00010 #define stk_mesh_MetaData_hpp
00011 
00012 //----------------------------------------------------------------------
00013 
00014 #include <iosfwd>
00015 
00016 #include <stk_util/util/SameType.hpp>
00017 #include <stk_util/util/StaticAssert.hpp>
00018 #include <stk_util/parallel/Parallel.hpp>
00019 
00020 #include <stk_mesh/base/Types.hpp>
00021 #include <stk_mesh/base/Part.hpp>
00022 #include <stk_mesh/base/Field.hpp>
00023 #include <stk_mesh/base/PropertyBase.hpp>
00024 #include <stk_mesh/base/EntityKey.hpp>
00025 
00026 #include <stk_mesh/baseImpl/PartRepository.hpp>
00027 #include <stk_mesh/baseImpl/FieldBaseImpl.hpp>
00028 #include <stk_mesh/baseImpl/FieldRepository.hpp>
00029 
00030 namespace stk {
00031 namespace mesh {
00032 
00033 
00039 std::ostream &
00040 print_entity_key( std::ostream & os, const MetaData & meta_data, const EntityKey & key);
00041 
00042 std::string
00043 print_entity_key( const MetaData & meta_data, const EntityKey & key );
00044 
00045 //----------------------------------------------------------------------
00052 class MetaData {
00053 public:
00054 
00056   //------------------------------------
00061   inline static MetaData & get( const Part & part ) { return part.meta_data(); }
00062   inline static MetaData & get( const FieldBase & field ) { return field.meta_data(); }
00063   inline static MetaData & get( const PropertyBase & property ) { return property.meta_data(); }
00064 
00065   static MetaData & get( const BulkData & bulk_data );
00066   static MetaData & get( const Bucket & bucket );
00067   static MetaData & get( const Entity & entity );
00068   static MetaData & get( const Ghosting & ghost );
00069 
00071   explicit MetaData( const std::vector<std::string>& entity_rank_names );
00072 
00074   MetaData();
00075 
00076   //------------------------------------
00084   Part & universal_part() const { return *m_universal_part; }
00085 
00089   Part & locally_owned_part()  const { return *m_owns_part ; }
00090 
00094   Part & globally_shared_part() const { return *m_shares_part ; }
00095 
00097   //------------------------------------
00108 
00109   Part * get_part( const std::string & p_name,
00110                    const char * required_by = NULL ) const ;
00111 
00113   Part & get_part( unsigned ord ) const ;
00114 
00116   const PartVector & get_parts() const { return m_part_repo.get_all_parts(); }
00117 
00125   Part & declare_part( const std::string & p_name, EntityRank rank );
00126 
00132   Part & declare_part( const std::string & p_name);
00133 
00141   Part & declare_part( const PartVector & p_name);
00142 
00144   void declare_part_subset( Part & superset , Part & subset );
00145 
00155   void declare_part_relation( Part & root_part ,
00156                               relation_stencil_ptr stencil ,
00157                               Part & target_part );
00158 
00165   template<class T>
00166   const T * declare_attribute_with_delete( Part & part, const T * attribute);
00167   template<class T>
00168   const T * declare_attribute_no_delete( Part & part, const T * attribute);
00169 
00171   //------------------------------------
00178   void set_entity_rank_names(const std::vector<std::string> &entity_rank_names);
00179 
00180   EntityRank entity_rank( const std::string &name ) const;
00181 
00182   const std::vector<std::string> & entity_rank_names() const
00183     { return m_entity_rank_names ; }
00184 
00185   std::vector<std::string>::size_type entity_rank_count() const
00186     { return m_entity_rank_names.size(); }
00187 
00188   const std::string & entity_rank_name( EntityRank entity_rank ) const ;
00189 
00193   bool check_rank(EntityRank rank) const;
00194 
00196   //------------------------------------
00208   template< class field_type >
00209   field_type * get_field( const std::string & name ) const ;
00210 
00212   const FieldVector & get_fields() const {
00213     return m_field_repo.get_fields() ;
00214   }
00215 
00225   template< class field_type >
00226   field_type & declare_field( const std::string & name ,
00227                               unsigned number_of_states = 1 );
00228 
00234   template<class T>
00235   const T * declare_attribute_with_delete( FieldBase & field, const T * attribute);
00236   template<class T>
00237   const T * declare_attribute_no_delete( FieldBase & field, const T * attribute);
00238 
00258   template< class PointerFieldType , class ReferencedFieldType >
00259   void declare_field_relation( PointerFieldType & pointer_field ,
00260                                relation_stencil_ptr stencil ,
00261                                ReferencedFieldType & referenced_field );
00262 
00264   const std::vector<FieldRelation> & get_field_relations() const
00265     { return m_field_relations ; }
00266 
00268   //------------------------------------
00269 
00270   template<class T>
00271   const T * get_attribute() const ;
00272 
00277   template<class T>
00278   const T * declare_attribute_with_delete( const T * attribute);
00279 
00280   template<class T>
00281   const T * declare_attribute_no_delete( const T * attribute);
00282 
00283   template<class T>
00284   bool remove_attribute( const T * );
00285 
00286   //------------------------------------
00298   template< typename DataType >
00299   Property<DataType> * get_property( const std::string & name ) const ;
00300 
00302   const std::vector< PropertyBase * > & get_properties() const
00303     { return m_properties ; }
00304 
00311   template< typename DataType >
00312   Property<DataType> & declare_property( const std::string & name ,
00313                                          unsigned size = 1 );
00314 
00316   void put_property( PropertyBase & property, Part & part);
00317 
00326   void commit();
00327 
00329   bool is_commit() const { return m_commit ; }
00330 
00334   ~MetaData();
00335 
00337   //------------------------------------
00338 
00345   FieldBase * declare_field_base(
00346     const std::string & arg_name,
00347     const DataTraits  & arg_traits ,
00348     unsigned            arg_rank ,
00349     const shards::ArrayDimTag * const * arg_dim_tags ,
00350     unsigned arg_num_states );
00351 
00354   void declare_field_restriction( FieldBase      & arg_field ,
00355                                   EntityRank       arg_entity_rank ,
00356                                   const Part     & arg_part ,
00357                                   const unsigned * arg_stride );
00359 private:
00360   MetaData( const MetaData & );                
00361   MetaData & operator = ( const MetaData & );  
00362 
00363   bool   m_commit ;
00364   impl::PartRepository m_part_repo ;
00365   CSet   m_attributes ;
00366 
00367   Part * m_universal_part ;
00368   Part * m_owns_part ;
00369   Part * m_shares_part ;
00370 
00371 
00372   impl::FieldRepository        m_field_repo ;
00373 
00374   std::vector< FieldRelation > m_field_relations ;
00375   std::vector< PropertyBase* > m_properties ;
00376   std::vector< std::string >   m_entity_rank_names ;
00377 
00381   void require_committed() const ;
00382 
00383   void require_not_committed() const ;
00384 
00385   void require_same_mesh_meta_data( const MetaData & rhs ) const ;
00386 
00387   void require_valid_entity_rank( EntityRank rank) const ;
00388 
00389   void require_not_relation_target( const Part * const part ) const ;
00391   //------------------------------------
00392 
00393   Property<void> * get_property_base( const std::string & ,
00394                                       const std::type_info & ,
00395                                       unsigned = 0 ) const ;
00396 
00397   void internal_declare_field_relation( FieldBase & ,
00398                                         relation_stencil_ptr ,
00399                                         FieldBase & );
00400 
00401   void clean_field_restrictions();
00402 };
00403 
00405 void verify_parallel_consistency( const MetaData & , ParallelMachine );
00406 
00415 template< class field_type >
00416 field_type & put_field( field_type & field ,
00417                         EntityRank  entity_rank ,
00418                         const Part & part );
00419 
00425 template< class field_type >
00426 field_type & put_field( field_type & field ,
00427                         EntityRank  entity_rank ,
00428                         const Part & part ,
00429                         unsigned     n1 );
00430 
00431 template< class field_type >
00432 field_type & put_field( field_type & field ,
00433                         EntityRank  entity_rank ,
00434                         const Part & part ,
00435                         unsigned     n1 ,
00436                         unsigned     n2 );
00437 
00438 template< class field_type >
00439 field_type & put_field( field_type & field ,
00440                         EntityRank  entity_rank ,
00441                         const Part & part ,
00442                         unsigned     n1 ,
00443                         unsigned     n2 ,
00444                         unsigned     n3 );
00445 
00446 template< class field_type >
00447 field_type & put_field( field_type & field ,
00448                         EntityRank  entity_rank ,
00449                         const Part & part ,
00450                         unsigned     n1 ,
00451                         unsigned     n2 ,
00452                         unsigned     n3 ,
00453                         unsigned     n4 );
00454 
00455 template< class field_type >
00456 field_type & put_field( field_type & field ,
00457                         EntityRank  entity_rank ,
00458                         const Part & part ,
00459                         unsigned     n1 ,
00460                         unsigned     n2 ,
00461                         unsigned     n3 ,
00462                         unsigned     n4 ,
00463                         unsigned     n5 );
00464 
00465 template< class field_type >
00466 field_type & put_field( field_type & field ,
00467                         EntityRank  entity_rank ,
00468                         const Part & part ,
00469                         unsigned     n1 ,
00470                         unsigned     n2 ,
00471                         unsigned     n3 ,
00472                         unsigned     n4 ,
00473                         unsigned     n5 ,
00474                         unsigned     n6 );
00475 
00476 template< class field_type >
00477 field_type & put_field( field_type & field ,
00478                         EntityRank  entity_rank ,
00479                         const Part & part ,
00480                         unsigned     n1 ,
00481                         unsigned     n2 ,
00482                         unsigned     n3 ,
00483                         unsigned     n4 ,
00484                         unsigned     n5 ,
00485                         unsigned     n6 ,
00486                         unsigned     n7 );
00490 } // namespace mesh
00491 } // namespace stk
00492 
00493 //----------------------------------------------------------------------
00494 //----------------------------------------------------------------------
00495 
00496 #ifndef DOXYGEN_COMPILE
00497 
00498 namespace stk {
00499 namespace mesh {
00500 
00501 inline
00502 Part & MetaData::get_part( unsigned ord ) const
00503 { return * m_part_repo.get_all_parts()[ord] ; }
00504 
00505 template< class field_type >
00506 inline
00507 field_type * MetaData::get_field( const std::string & name ) const
00508 {
00509   typedef FieldTraits< field_type > Traits ;
00510 
00511   const DataTraits & dt = data_traits< typename Traits::data_type >();
00512 
00513   const shards::ArrayDimTag * tags[8] ;
00514 
00515   Traits::assign_tags( tags );
00516 
00517   FieldBase * const field =
00518     m_field_repo.get_field( "stk::mesh::MetaData::get_field" ,
00519                           name , dt , Traits::Rank , tags , 0 );
00520 
00521   return static_cast< field_type * >( field );
00522 }
00523 
00524 template< class field_type >
00525 inline
00526 field_type & MetaData::declare_field( const std::string & name ,
00527                                       unsigned number_of_states )
00528 {
00529   typedef FieldTraits< field_type > Traits ;
00530 
00531   const DataTraits & dt = data_traits< typename Traits::data_type >();
00532 
00533   const shards::ArrayDimTag * tags[8] ;
00534 
00535   Traits::assign_tags( tags );
00536 
00537   return * static_cast< field_type * >(
00538     declare_field_base( name , dt , Traits::Rank , tags , number_of_states ) );
00539 }
00540 
00541 template< class field_type >
00542 inline
00543 field_type & put_field(
00544   field_type & field ,
00545   EntityRank entity_rank ,
00546   const Part & part )
00547 {
00548   typedef FieldTraits< field_type > Traits ;
00549   typedef typename Traits::Helper   Helper ;
00550 
00551   unsigned stride[8] ;
00552 
00553   Helper::assign( stride );
00554 
00555   MetaData::get(field).declare_field_restriction( field, entity_rank, part, stride);
00556 
00557   return field ;
00558 }
00559 
00560 template< class field_type >
00561 inline
00562 field_type & put_field( field_type &field ,
00563                         EntityRank entity_rank ,
00564                         const Part &part ,
00565                         unsigned    n1 )
00566 {
00567   typedef FieldTraits< field_type > Traits ;
00568   typedef typename Traits::Helper   Helper ;
00569 
00570   unsigned stride[8] ;
00571 
00572   Helper::assign( stride , n1 );
00573 
00574   MetaData::get(field).declare_field_restriction( field, entity_rank, part, stride);
00575 
00576   return field ;
00577 }
00578 
00579 template< class field_type >
00580 inline
00581 field_type & put_field( field_type &field ,
00582                         EntityRank entity_rank ,
00583                         const Part &part ,
00584                         unsigned    n1 ,
00585                         unsigned    n2 )
00586 {
00587   typedef FieldTraits< field_type > Traits ;
00588   typedef typename Traits::Helper   Helper ;
00589 
00590   unsigned stride[8] ;
00591 
00592   Helper::assign( stride , n1 , n2 );
00593 
00594   MetaData::get(field).declare_field_restriction( field, entity_rank, part, stride);
00595 
00596   return field ;
00597 }
00598 
00599 template< class field_type >
00600 inline
00601 field_type & put_field( field_type &field ,
00602                         EntityRank entity_rank ,
00603                         const Part &part ,
00604                         unsigned    n1 ,
00605                         unsigned    n2 ,
00606                         unsigned    n3 )
00607 {
00608   typedef FieldTraits< field_type > Traits ;
00609   typedef typename Traits::Helper   Helper ;
00610 
00611   unsigned stride[8] ;
00612 
00613   Helper::assign( stride , n1 , n2 , n3 );
00614 
00615   MetaData::get(field).declare_field_restriction( field, entity_rank, part, stride);
00616 
00617   return field ;
00618 }
00619 
00620 template< class field_type >
00621 inline
00622 field_type & put_field( field_type &field ,
00623                         EntityRank entity_rank ,
00624                         const Part &part ,
00625                         unsigned    n1 ,
00626                         unsigned    n2 ,
00627                         unsigned    n3 ,
00628                         unsigned    n4 )
00629 {
00630   typedef FieldTraits< field_type > Traits ;
00631   typedef typename Traits::Helper   Helper ;
00632 
00633   unsigned stride[8] ;
00634 
00635   Helper::assign( stride , n1 , n2 , n3 , n4 );
00636 
00637   MetaData::get(field).declare_field_restriction( field, entity_rank, part, stride);
00638 
00639   return field ;
00640 }
00641 
00642 template< class field_type >
00643 inline
00644 field_type & put_field( field_type &field ,
00645                         EntityRank entity_rank ,
00646                         const Part &part ,
00647                         unsigned    n1 ,
00648                         unsigned    n2 ,
00649                         unsigned    n3 ,
00650                         unsigned    n4 ,
00651                         unsigned    n5 )
00652 {
00653   typedef FieldTraits< field_type > Traits ;
00654   typedef typename Traits::Helper   Helper ;
00655 
00656   unsigned stride[8] ;
00657 
00658   Helper::assign( stride , n1 , n2 , n3 , n4, n5 );
00659 
00660   MetaData::get(field).declare_field_restriction( field, entity_rank, part, stride);
00661 
00662   return field ;
00663 }
00664 
00665 template< class field_type >
00666 inline
00667 field_type & put_field( field_type &field ,
00668                         EntityRank entity_rank ,
00669                         const Part &part ,
00670                         unsigned    n1 ,
00671                         unsigned    n2 ,
00672                         unsigned    n3 ,
00673                         unsigned    n4 ,
00674                         unsigned    n5 ,
00675                         unsigned    n6 )
00676 {
00677   typedef FieldTraits< field_type > Traits ;
00678   typedef typename Traits::Helper   Helper ;
00679 
00680   unsigned stride[8] ;
00681 
00682   Helper::assign( stride , n1 , n2 , n3 , n4, n5, n6 );
00683 
00684   MetaData::get(field).declare_field_restriction( field, entity_rank, part, stride);
00685 
00686   return field ;
00687 }
00688 
00689 template< class field_type >
00690 inline
00691 field_type & put_field( field_type &field ,
00692                         EntityRank entity_rank ,
00693                         const Part &part ,
00694                         unsigned    n1 ,
00695                         unsigned    n2 ,
00696                         unsigned    n3 ,
00697                         unsigned    n4 ,
00698                         unsigned    n5 ,
00699                         unsigned    n6 ,
00700                         unsigned    n7 )
00701 {
00702   typedef FieldTraits< field_type > Traits ;
00703   typedef typename Traits::Helper   Helper ;
00704 
00705   unsigned stride[8] ;
00706 
00707   Helper::assign( stride , n1 , n2 , n3 , n4, n5, n6, n7 );
00708 
00709   MetaData::get(field).declare_field_restriction( field, entity_rank, part, stride);
00710 
00711   return field ;
00712 }
00713 
00714 template<class T>
00715 inline
00716 const T *
00717 MetaData::declare_attribute_with_delete( const T * a )
00718 {
00719   require_not_committed();
00720   return m_attributes.insert_with_delete( a );
00721 }
00722 
00723 template<class T>
00724 inline
00725 const T *
00726 MetaData::get_attribute() const
00727 { return m_attributes.get<T>(); }
00728 
00729 template<class T>
00730 inline
00731 const T *
00732 MetaData::declare_attribute_no_delete( const T * attribute )
00733 {
00734   require_not_committed();
00735   return m_attributes.insert_no_delete( attribute );
00736 }
00737 
00738 template<class T>
00739 inline
00740 bool
00741 MetaData::remove_attribute( const T * a )
00742 {
00743   return m_attributes.remove( a );
00744 }
00745 
00746 template<class T>
00747 inline
00748 const T *
00749 MetaData::declare_attribute_with_delete( Part & part , const T * attribute )
00750 {
00751   require_not_committed();
00752   return m_part_repo.declare_attribute_with_delete( part, attribute );
00753 }
00754 
00755 template<class T>
00756 inline
00757 const T *
00758 MetaData::declare_attribute_no_delete( Part & part , const T * attribute )
00759 {
00760   require_not_committed();
00761   return m_part_repo.declare_attribute_no_delete( part, attribute );
00762 }
00763 
00764 template<class T>
00765 inline
00766 const T *
00767 MetaData::declare_attribute_with_delete( FieldBase & field , const T * attribute )
00768 {
00769   require_not_committed();
00770   return m_field_repo.declare_attribute_with_delete(field, attribute);
00771 }
00772 
00773 template<class T>
00774 inline
00775 const T *
00776 MetaData::declare_attribute_no_delete( FieldBase & field , const T * attribute )
00777 {
00778   require_not_committed();
00779   return m_field_repo.declare_attribute_no_delete(field, attribute);
00780 }
00781 
00782 //----------------------------------------------------------------------
00783 
00784 template< class PointerFieldType , class ReferencedFieldType >
00785 inline
00786 void MetaData::declare_field_relation(
00787   PointerFieldType & pointer_field ,
00788   relation_stencil_ptr stencil ,
00789   ReferencedFieldType & referenced_field )
00790 {
00791   typedef typename FieldTraits< PointerFieldType >::data_type pointer_type ;
00792   typedef typename FieldTraits< ReferencedFieldType >::data_type data_type ;
00793 
00794   StaticAssert< SameType< pointer_type , data_type * >::value >::ok();
00795   StaticAssert< FieldTraits< PointerFieldType >::Rank == 1 >::ok();
00796 
00797   internal_declare_field_relation( pointer_field , stencil , referenced_field );
00798 }
00799 
00800 //----------------------------------------------------------------------
00801 
00802 template< typename DataType >
00803 inline
00804 Property<DataType> *
00805 MetaData::get_property( const std::string & name ) const
00806 {
00807   Property<void> * const pv = get_property_base( name, typeid(DataType) );
00808   return pv ? pv->property<DataType>() : (Property<DataType>*) NULL ;
00809 }
00810 
00811 template< typename DataType >
00812 inline
00813 Property<DataType> &
00814 MetaData::declare_property( const std::string & name , unsigned size )
00815 {
00816   Property<void> * pv = get_property_base(name,typeid(DataType),size);
00817   Property<DataType> * prop = NULL ;
00818 
00819   if ( pv != NULL ) {
00820     prop = pv->property<DataType>();
00821   }
00822   else {
00823     if ( 1 == size ) {
00824       pv = prop = new Property<DataType>( *this , m_properties.size() , name );
00825     }
00826     else {
00827       pv = prop = new Property< std::vector<DataType> >(
00828                     *this , m_properties.size() , name , size );
00829     }
00830     m_properties.push_back( pv );
00831   }
00832   return *prop ;
00833 }
00834 
00835 inline
00836 void MetaData::put_property( PropertyBase & property , Part & part )
00837 {
00838   property.add_property( part.mesh_meta_data_ordinal() );
00839 }
00840 
00841 inline
00842 bool MetaData::check_rank(EntityRank rank) const
00843 {
00844   return rank < m_entity_rank_names.size();
00845 }
00846 
00847 inline
00848 bool
00849 is_auto_declared_part(const Part &part)
00850 {
00851   const std::string &part_name = part.name();
00852 
00853   return !part_name.empty() && part_name[0] == '{';
00854 }
00855 
00856 } // namespace mesh
00857 } // namespace stk
00858 
00859 #endif /* DOXYGEN_COMPILE */
00860 
00861 //----------------------------------------------------------------------
00862 //----------------------------------------------------------------------
00863 
00864 #endif /* stk_mesh_MetaData_hpp */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends