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   template<class T>
00170   bool remove_attribute( Part & part, const T * attribute);
00171 
00173   //------------------------------------
00180   void set_entity_rank_names(const std::vector<std::string> &entity_rank_names);
00181 
00182   EntityRank entity_rank( const std::string &name ) const;
00183 
00184   const std::vector<std::string> & entity_rank_names() const
00185     { return m_entity_rank_names ; }
00186 
00187   std::vector<std::string>::size_type entity_rank_count() const
00188     { return m_entity_rank_names.size(); }
00189 
00190   const std::string & entity_rank_name( EntityRank entity_rank ) const ;
00191 
00195   bool check_rank(EntityRank rank) const;
00196 
00198   //------------------------------------
00210   template< class field_type >
00211   field_type * get_field( const std::string & name ) const ;
00212 
00214   const FieldVector & get_fields() const {
00215     return m_field_repo.get_fields() ;
00216   }
00217 
00227   template< class field_type >
00228   field_type & declare_field( const std::string & name ,
00229                               unsigned number_of_states = 1 );
00230 
00236   template<class T>
00237   const T * declare_attribute_with_delete( FieldBase & field, const T * attribute);
00238   template<class T>
00239   const T * declare_attribute_no_delete( FieldBase & field, const T * attribute);
00240 
00260   template< class PointerFieldType , class ReferencedFieldType >
00261   void declare_field_relation( PointerFieldType & pointer_field ,
00262                                relation_stencil_ptr stencil ,
00263                                ReferencedFieldType & referenced_field );
00264 
00266   const std::vector<FieldRelation> & get_field_relations() const
00267     { return m_field_relations ; }
00268 
00270   //------------------------------------
00271 
00272   template<class T>
00273   const T * get_attribute() const ;
00274 
00279   template<class T>
00280   const T * declare_attribute_with_delete( const T * attribute);
00281 
00282   template<class T>
00283   const T * declare_attribute_no_delete( const T * attribute);
00284 
00285   template<class T>
00286   bool remove_attribute( const T * );
00287 
00288   //------------------------------------
00300   template< typename DataType >
00301   Property<DataType> * get_property( const std::string & name ) const ;
00302 
00304   const std::vector< PropertyBase * > & get_properties() const
00305     { return m_properties ; }
00306 
00313   template< typename DataType >
00314   Property<DataType> & declare_property( const std::string & name ,
00315                                          unsigned size = 1 );
00316 
00318   void put_property( PropertyBase & property, Part & part);
00319 
00328   void commit();
00329 
00331   bool is_commit() const { return m_commit ; }
00332 
00336   ~MetaData();
00337 
00339   //------------------------------------
00340 
00347   FieldBase * declare_field_base(
00348     const std::string & arg_name,
00349     const DataTraits  & arg_traits ,
00350     unsigned            arg_rank ,
00351     const shards::ArrayDimTag * const * arg_dim_tags ,
00352     unsigned arg_num_states );
00353 
00356   void declare_field_restriction( FieldBase      & arg_field ,
00357                                   EntityRank       arg_entity_rank ,
00358                                   const Part     & arg_part ,
00359                                   const unsigned * arg_stride ,
00360                                   const void*      arg_init_value = NULL );
00362 private:
00363   MetaData( const MetaData & );                
00364   MetaData & operator = ( const MetaData & );  
00365 
00366   bool   m_commit ;
00367   impl::PartRepository m_part_repo ;
00368   CSet   m_attributes ;
00369 
00370   Part * m_universal_part ;
00371   Part * m_owns_part ;
00372   Part * m_shares_part ;
00373 
00374 
00375   impl::FieldRepository        m_field_repo ;
00376 
00377   std::vector< FieldRelation > m_field_relations ;
00378   std::vector< PropertyBase* > m_properties ;
00379   std::vector< std::string >   m_entity_rank_names ;
00380 
00384   void require_committed() const ;
00385 
00386   void require_not_committed() const ;
00387 
00388   void require_same_mesh_meta_data( const MetaData & rhs ) const ;
00389 
00390   void require_valid_entity_rank( EntityRank rank) const ;
00391 
00392   void require_not_relation_target( const Part * const part ) const ;
00394   //------------------------------------
00395 
00396   Property<void> * get_property_base( const std::string & ,
00397                                       const std::type_info & ,
00398                                       unsigned = 0 ) const ;
00399 
00400   void internal_declare_field_relation( FieldBase & ,
00401                                         relation_stencil_ptr ,
00402                                         FieldBase & );
00403 
00404   void clean_field_restrictions();
00405 };
00406 
00408 void verify_parallel_consistency( const MetaData & , ParallelMachine );
00409 
00418 template< class field_type >
00419 field_type & put_field( field_type & field ,
00420                         EntityRank  entity_rank ,
00421                         const Part & part ,
00422                         const void* init_value = NULL);
00423 
00429 template< class field_type >
00430 field_type & put_field( field_type & field ,
00431                         EntityRank  entity_rank ,
00432                         const Part & part ,
00433                         unsigned     n1 ,
00434                         const void* init_value = NULL);
00435 
00436 template< class field_type >
00437 field_type & put_field( field_type & field ,
00438                         EntityRank  entity_rank ,
00439                         const Part & part ,
00440                         unsigned     n1 ,
00441                         unsigned     n2 ,
00442                         const void* init_value = NULL);
00443 
00444 template< class field_type >
00445 field_type & put_field( field_type & field ,
00446                         EntityRank  entity_rank ,
00447                         const Part & part ,
00448                         unsigned     n1 ,
00449                         unsigned     n2 ,
00450                         unsigned     n3 ,
00451                         const void* init_value = NULL);
00452 
00453 template< class field_type >
00454 field_type & put_field( field_type & field ,
00455                         EntityRank  entity_rank ,
00456                         const Part & part ,
00457                         unsigned     n1 ,
00458                         unsigned     n2 ,
00459                         unsigned     n3 ,
00460                         unsigned     n4 ,
00461                         const void* init_value = NULL);
00462 
00463 template< class field_type >
00464 field_type & put_field( field_type & field ,
00465                         EntityRank  entity_rank ,
00466                         const Part & part ,
00467                         unsigned     n1 ,
00468                         unsigned     n2 ,
00469                         unsigned     n3 ,
00470                         unsigned     n4 ,
00471                         unsigned     n5 ,
00472                         const void* init_value = NULL);
00473 
00474 template< class field_type >
00475 field_type & put_field( field_type & field ,
00476                         EntityRank  entity_rank ,
00477                         const Part & part ,
00478                         unsigned     n1 ,
00479                         unsigned     n2 ,
00480                         unsigned     n3 ,
00481                         unsigned     n4 ,
00482                         unsigned     n5 ,
00483                         unsigned     n6 ,
00484                         const void* init_value = NULL);
00485 
00486 template< class field_type >
00487 field_type & put_field( field_type & field ,
00488                         EntityRank  entity_rank ,
00489                         const Part & part ,
00490                         unsigned     n1 ,
00491                         unsigned     n2 ,
00492                         unsigned     n3 ,
00493                         unsigned     n4 ,
00494                         unsigned     n5 ,
00495                         unsigned     n6 ,
00496                         unsigned     n7 ,
00497                         const void* init_value = NULL);
00501 } // namespace mesh
00502 } // namespace stk
00503 
00504 //----------------------------------------------------------------------
00505 //----------------------------------------------------------------------
00506 
00507 #ifndef DOXYGEN_COMPILE
00508 
00509 namespace stk {
00510 namespace mesh {
00511 
00512 inline
00513 Part & MetaData::get_part( unsigned ord ) const
00514 { return * m_part_repo.get_all_parts()[ord] ; }
00515 
00516 template< class field_type >
00517 inline
00518 field_type * MetaData::get_field( const std::string & name ) const
00519 {
00520   typedef FieldTraits< field_type > Traits ;
00521 
00522   const DataTraits & dt = data_traits< typename Traits::data_type >();
00523 
00524   const shards::ArrayDimTag * tags[8] ;
00525 
00526   Traits::assign_tags( tags );
00527 
00528   FieldBase * const field =
00529     m_field_repo.get_field( "stk::mesh::MetaData::get_field" ,
00530                           name , dt , Traits::Rank , tags , 0 );
00531 
00532   return static_cast< field_type * >( field );
00533 }
00534 
00535 template< class field_type >
00536 inline
00537 field_type & MetaData::declare_field( const std::string & name ,
00538                                       unsigned number_of_states )
00539 {
00540   typedef FieldTraits< field_type > Traits ;
00541 
00542   const DataTraits & dt = data_traits< typename Traits::data_type >();
00543 
00544   const shards::ArrayDimTag * tags[8] ;
00545 
00546   Traits::assign_tags( tags );
00547 
00548   return * static_cast< field_type * >(
00549     declare_field_base( name , dt , Traits::Rank , tags , number_of_states ) );
00550 }
00551 
00552 template< class field_type >
00553 inline
00554 field_type & put_field(
00555   field_type & field ,
00556   EntityRank entity_rank ,
00557   const Part & part ,
00558   const void* init_value)
00559 {
00560   typedef FieldTraits< field_type > Traits ;
00561   typedef typename Traits::Helper   Helper ;
00562 
00563   unsigned stride[8] ;
00564 
00565   Helper::assign( stride );
00566 
00567   MetaData::get(field).declare_field_restriction( field, entity_rank, part, stride, init_value);
00568 
00569   return field ;
00570 }
00571 
00572 template< class field_type >
00573 inline
00574 field_type & put_field( field_type &field ,
00575                         EntityRank entity_rank ,
00576                         const Part &part ,
00577                         unsigned    n1 ,
00578                         const void* init_value )
00579 {
00580   typedef FieldTraits< field_type > Traits ;
00581   typedef typename Traits::Helper   Helper ;
00582 
00583   unsigned stride[8] ;
00584 
00585   Helper::assign( stride , n1 );
00586 
00587   MetaData::get(field).declare_field_restriction( field, entity_rank, part, stride, init_value);
00588 
00589   return field ;
00590 }
00591 
00592 template< class field_type >
00593 inline
00594 field_type & put_field( field_type &field ,
00595                         EntityRank entity_rank ,
00596                         const Part &part ,
00597                         unsigned    n1 ,
00598                         unsigned    n2 ,
00599                         const void* init_value )
00600 {
00601   typedef FieldTraits< field_type > Traits ;
00602   typedef typename Traits::Helper   Helper ;
00603 
00604   unsigned stride[8] ;
00605 
00606   Helper::assign( stride , n1 , n2 );
00607 
00608   MetaData::get(field).declare_field_restriction( field, entity_rank, part, stride, init_value);
00609 
00610   return field ;
00611 }
00612 
00613 template< class field_type >
00614 inline
00615 field_type & put_field( field_type &field ,
00616                         EntityRank entity_rank ,
00617                         const Part &part ,
00618                         unsigned    n1 ,
00619                         unsigned    n2 ,
00620                         unsigned    n3 ,
00621                         const void* init_value )
00622 {
00623   typedef FieldTraits< field_type > Traits ;
00624   typedef typename Traits::Helper   Helper ;
00625 
00626   unsigned stride[8] ;
00627 
00628   Helper::assign( stride , n1 , n2 , n3 );
00629 
00630   MetaData::get(field).declare_field_restriction( field, entity_rank, part, stride, init_value);
00631 
00632   return field ;
00633 }
00634 
00635 template< class field_type >
00636 inline
00637 field_type & put_field( field_type &field ,
00638                         EntityRank entity_rank ,
00639                         const Part &part ,
00640                         unsigned    n1 ,
00641                         unsigned    n2 ,
00642                         unsigned    n3 ,
00643                         unsigned    n4 ,
00644                         const void* init_value )
00645 {
00646   typedef FieldTraits< field_type > Traits ;
00647   typedef typename Traits::Helper   Helper ;
00648 
00649   unsigned stride[8] ;
00650 
00651   Helper::assign( stride , n1 , n2 , n3 , n4 );
00652 
00653   MetaData::get(field).declare_field_restriction( field, entity_rank, part, stride, init_value);
00654 
00655   return field ;
00656 }
00657 
00658 template< class field_type >
00659 inline
00660 field_type & put_field( field_type &field ,
00661                         EntityRank entity_rank ,
00662                         const Part &part ,
00663                         unsigned    n1 ,
00664                         unsigned    n2 ,
00665                         unsigned    n3 ,
00666                         unsigned    n4 ,
00667                         unsigned    n5 ,
00668                         const void* init_value )
00669 {
00670   typedef FieldTraits< field_type > Traits ;
00671   typedef typename Traits::Helper   Helper ;
00672 
00673   unsigned stride[8] ;
00674 
00675   Helper::assign( stride , n1 , n2 , n3 , n4, n5 );
00676 
00677   MetaData::get(field).declare_field_restriction( field, entity_rank, part, stride, init_value);
00678 
00679   return field ;
00680 }
00681 
00682 template< class field_type >
00683 inline
00684 field_type & put_field( field_type &field ,
00685                         EntityRank entity_rank ,
00686                         const Part &part ,
00687                         unsigned    n1 ,
00688                         unsigned    n2 ,
00689                         unsigned    n3 ,
00690                         unsigned    n4 ,
00691                         unsigned    n5 ,
00692                         unsigned    n6 ,
00693                         const void* init_value )
00694 {
00695   typedef FieldTraits< field_type > Traits ;
00696   typedef typename Traits::Helper   Helper ;
00697 
00698   unsigned stride[8] ;
00699 
00700   Helper::assign( stride , n1 , n2 , n3 , n4, n5, n6 );
00701 
00702   MetaData::get(field).declare_field_restriction( field, entity_rank, part, stride, init_value);
00703 
00704   return field ;
00705 }
00706 
00707 template< class field_type >
00708 inline
00709 field_type & put_field( field_type &field ,
00710                         EntityRank entity_rank ,
00711                         const Part &part ,
00712                         unsigned    n1 ,
00713                         unsigned    n2 ,
00714                         unsigned    n3 ,
00715                         unsigned    n4 ,
00716                         unsigned    n5 ,
00717                         unsigned    n6 ,
00718                         unsigned    n7 ,
00719                         const void* init_value )
00720 {
00721   typedef FieldTraits< field_type > Traits ;
00722   typedef typename Traits::Helper   Helper ;
00723 
00724   unsigned stride[8] ;
00725 
00726   Helper::assign( stride , n1 , n2 , n3 , n4, n5, n6, n7 );
00727 
00728   MetaData::get(field).declare_field_restriction( field, entity_rank, part, stride, init_value);
00729 
00730   return field ;
00731 }
00732 
00733 template<class T>
00734 inline
00735 const T *
00736 MetaData::declare_attribute_with_delete( const T * a )
00737 {
00738   require_not_committed();
00739   return m_attributes.insert_with_delete( a );
00740 }
00741 
00742 template<class T>
00743 inline
00744 const T *
00745 MetaData::get_attribute() const
00746 { return m_attributes.get<T>(); }
00747 
00748 template<class T>
00749 inline
00750 const T *
00751 MetaData::declare_attribute_no_delete( const T * attribute )
00752 {
00753   require_not_committed();
00754   return m_attributes.insert_no_delete( attribute );
00755 }
00756 
00757 template<class T>
00758 inline
00759 bool
00760 MetaData::remove_attribute( const T * a )
00761 {
00762   return m_attributes.remove( a );
00763 }
00764 
00765 template<class T>
00766 inline
00767 const T *
00768 MetaData::declare_attribute_with_delete( Part & part , const T * attribute )
00769 {
00770   require_not_committed();
00771   return m_part_repo.declare_attribute_with_delete( part, attribute );
00772 }
00773 
00774 template<class T>
00775 inline
00776 const T *
00777 MetaData::declare_attribute_no_delete( Part & part , const T * attribute )
00778 {
00779   require_not_committed();
00780   return m_part_repo.declare_attribute_no_delete( part, attribute );
00781 }
00782 
00783 template<class T>
00784 inline
00785 bool
00786 MetaData::remove_attribute( Part & part , const T * attribute )
00787 {
00788   return m_part_repo.remove_attribute(part, attribute);
00789 }
00790 
00791 template<class T>
00792 inline
00793 const T *
00794 MetaData::declare_attribute_with_delete( FieldBase & field , const T * attribute )
00795 {
00796   require_not_committed();
00797   return m_field_repo.declare_attribute_with_delete(field, attribute);
00798 }
00799 
00800 template<class T>
00801 inline
00802 const T *
00803 MetaData::declare_attribute_no_delete( FieldBase & field , const T * attribute )
00804 {
00805   require_not_committed();
00806   return m_field_repo.declare_attribute_no_delete(field, attribute);
00807 }
00808 
00809 //----------------------------------------------------------------------
00810 
00811 template< class PointerFieldType , class ReferencedFieldType >
00812 inline
00813 void MetaData::declare_field_relation(
00814   PointerFieldType & pointer_field ,
00815   relation_stencil_ptr stencil ,
00816   ReferencedFieldType & referenced_field )
00817 {
00818   typedef typename FieldTraits< PointerFieldType >::data_type pointer_type ;
00819   typedef typename FieldTraits< ReferencedFieldType >::data_type data_type ;
00820 
00821   StaticAssert< SameType< pointer_type , data_type * >::value >::ok();
00822   StaticAssert< FieldTraits< PointerFieldType >::Rank == 1 >::ok();
00823 
00824   internal_declare_field_relation( pointer_field , stencil , referenced_field );
00825 }
00826 
00827 //----------------------------------------------------------------------
00828 
00829 template< typename DataType >
00830 inline
00831 Property<DataType> *
00832 MetaData::get_property( const std::string & name ) const
00833 {
00834   Property<void> * const pv = get_property_base( name, typeid(DataType) );
00835   return pv ? pv->property<DataType>() : (Property<DataType>*) NULL ;
00836 }
00837 
00838 template< typename DataType >
00839 inline
00840 Property<DataType> &
00841 MetaData::declare_property( const std::string & name , unsigned size )
00842 {
00843   Property<void> * pv = get_property_base(name,typeid(DataType),size);
00844   Property<DataType> * prop = NULL ;
00845 
00846   if ( pv != NULL ) {
00847     prop = pv->property<DataType>();
00848   }
00849   else {
00850     if ( 1 == size ) {
00851       pv = prop = new Property<DataType>( *this , m_properties.size() , name );
00852     }
00853     else {
00854       pv = prop = new Property< std::vector<DataType> >(
00855                     *this , m_properties.size() , name , size );
00856     }
00857     m_properties.push_back( pv );
00858   }
00859   return *prop ;
00860 }
00861 
00862 inline
00863 void MetaData::put_property( PropertyBase & property , Part & part )
00864 {
00865   property.add_property( part.mesh_meta_data_ordinal() );
00866 }
00867 
00868 inline
00869 bool MetaData::check_rank(EntityRank rank) const
00870 {
00871   return rank < m_entity_rank_names.size();
00872 }
00873 
00874 inline
00875 bool
00876 is_auto_declared_part(const Part &part)
00877 {
00878   const std::string &part_name = part.name();
00879 
00880   return !part_name.empty() && part_name[0] == '{';
00881 }
00882 
00883 } // namespace mesh
00884 } // namespace stk
00885 
00886 #endif /* DOXYGEN_COMPILE */
00887 
00888 //----------------------------------------------------------------------
00889 //----------------------------------------------------------------------
00890 
00891 #endif /* stk_mesh_MetaData_hpp */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends