Sierra Toolkit Version of the Day
EntityImpl.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_EntityImpl_hpp
00010 #define stk_mesh_EntityImpl_hpp
00011 
00012 #include <stk_util/util/PairIter.hpp>
00013 
00014 #include <stk_util/environment/ReportHandler.hpp>
00015 
00016 #include <stk_mesh/base/Types.hpp>
00017 #include <stk_mesh/base/Relation.hpp>
00018 #include <stk_mesh/base/Trace.hpp>
00019 
00020 #include <algorithm>
00021 
00022 namespace stk {
00023 namespace mesh {
00024 namespace impl {
00025 
00030 class EntityImpl {
00031 public:
00032 
00033   EntityImpl( const EntityKey & arg_key );
00034   EntityImpl();
00035   ~EntityImpl(){}
00036 
00037   // Exposed in external interface:
00038   EntityRank entity_rank() const { return stk::mesh::entity_rank( m_key ); }
00039   EntityId identifier() const { return stk::mesh::entity_id( m_key ); }
00040   const EntityKey & key() const { return m_key ; }
00041   PairIterRelation relations() const { return PairIterRelation(m_relation); }
00042   PairIterRelation relations( unsigned rank ) const ;
00043   PairIterRelation node_relations( ) const
00044   {
00045     RelationVector::const_iterator i = m_relation.begin();
00046     RelationVector::const_iterator e = m_relation.end();
00047 
00048     const Relation::raw_relation_id_type hi = Relation::raw_relation_id(1, 0);
00049     e = std::lower_bound( i , e , hi , LessRelation() );
00050 
00051     return PairIterRelation( i , e );
00052   }
00053 
00054   RelationVector::const_iterator node_relation(unsigned ordinal) const
00055   { return m_relation.begin() + ordinal; }
00056 
00057   PairIterEntityComm comm() const;
00058   PairIterEntityComm sharing() const;
00059   PairIterEntityComm comm( const Ghosting & sub ) const;
00060 
00061   Bucket & bucket() const
00062   {
00063     ThrowAssert(m_bucket); //don't want to return a reference to a null bucket
00064     return *m_bucket ;
00065   }
00066 
00067   Bucket* bucket_ptr() const
00068   {
00069     return m_bucket; // allow for NULL return value
00070   }
00071 
00072   bool is_bucket_valid() const { return m_bucket != NULL; }
00073   unsigned bucket_ordinal() const { return m_bucket_ord ; }
00074   unsigned owner_rank() const { return m_owner_rank ; }
00075   size_t synchronized_count() const { return m_sync_count ; }
00076 
00077   // The two relation methods below need to be called symmetically, ideally
00078   // through EntityRepository which will enforce the symmetry.
00079 
00080   bool destroy_relation( Entity & e_to, const RelationIdentifier local_id);
00081   bool declare_relation( Entity & e_to,
00082                          const RelationIdentifier local_id,
00083                          unsigned sync_count,
00084                          bool is_back_relation = false);
00085 
00086   // Communication info access:
00087   bool insert( const EntityCommInfo & val );
00088   bool erase( const EntityCommInfo & val );
00089   bool erase( const Ghosting & ghost );
00090   void comm_clear_ghosting();
00091   void comm_clear();
00092 
00093   void set_bucket_and_ordinal( Bucket * in_bucket, unsigned ordinal )
00094   {
00095     TraceIfWatching("stk::mesh::impl::EntityRepository::set_bucket_and_ordinal", LOG_ENTITY, key());
00096 
00097     m_bucket = in_bucket;
00098     m_bucket_ord = ordinal;
00099   }
00100 
00101   // return true if entity was actually modified
00102   bool set_owner_rank( unsigned in_owner_rank )
00103   {
00104     TraceIfWatching("stk::mesh::impl::EntityRepository::set_owner_rank", LOG_ENTITY, key());
00105 
00106     if ( in_owner_rank != m_owner_rank ) {
00107       m_owner_rank = in_owner_rank;
00108       return true;
00109     }
00110     return false;
00111   }
00112 
00113   void set_sync_count( size_t sync_count )
00114   {
00115     TraceIfWatching("stk::mesh::impl::EntityRepository::set_sync_count", LOG_ENTITY, key());
00116 
00117     m_sync_count = sync_count;
00118   }
00119 
00120   // Change log access:
00121   EntityModificationLog log_query() const { return m_mod_log ; }
00122 
00123   void log_clear()
00124   {
00125     TraceIfWatching("stk::mesh::impl::EntityRepository::log_clear", LOG_ENTITY, key());
00126 
00127     m_mod_log = EntityLogNoChange;
00128   }
00129 
00130   void log_deleted()
00131   {
00132     TraceIfWatching("stk::mesh::impl::EntityRepository::log_deleted", LOG_ENTITY, key());
00133 
00134     m_mod_log = EntityLogDeleted;
00135   }
00136 
00141   void log_resurrect();
00142 
00150   void log_modified_and_propagate();
00151 
00153   void log_created_parallel_copy();
00154 
00155   bool marked_for_destruction() const
00156   {
00157     // The original implementation of this method checked bucket capacity. In
00158     // order to ensure that the addition of EntityLogDeleted does not change
00159     // behavior, we put error check here.
00160     //  ThrowErrorMsgIf((bucket().capacity() == 0) != (m_mod_log == EntityLogDeleted),
00161     //      "Inconsistent destruction state; " <<
00162     //      "destroyed entities should be in the nil bucket and vice versa.\n" <<
00163     //      "Problem is with entity: " <<
00164     //      print_entity_key( MetaData::get( bucket() ), key() ) <<
00165     //      "\nWas in nil bucket: " << (bucket().capacity() == 0) << ", " <<
00166     //      "was in destroyed state: " << (m_mod_log == EntityLogDeleted) );
00167 
00168     return m_mod_log == EntityLogDeleted;
00169   }
00170 
00171   //set_key is only to be used for setting a key on a newly-constructed entity.
00172   void set_key(EntityKey key);
00173 
00174   //update_key is used to change the key for an entity that has been in use with
00175   //a different key.
00176   void update_key(EntityKey key);
00177 
00178 //  RelationVector& rel_vec() {return m_relation;}
00179   void compress_relation_capacity();
00180 
00181  private:
00182 
00183   EntityKey               m_key ;        
00184   RelationVector          m_relation ;   
00185   Bucket                * m_bucket ;     
00186   unsigned                m_bucket_ord ; 
00187   unsigned                m_owner_rank ; 
00188   size_t                  m_sync_count ; 
00189   EntityModificationLog   m_mod_log ;
00190 
00191 //  EntityImpl( const EntityImpl & ); ///< Copy constructor not allowed
00192   EntityImpl & operator = ( const EntityImpl & ); 
00193 };
00194 
00195 inline
00196 EntityImpl::EntityImpl( const EntityKey & arg_key )
00197   : m_key(arg_key),
00198     m_relation(),
00199     m_bucket( NULL ),
00200     m_bucket_ord(0),
00201     m_owner_rank(0),
00202     m_sync_count(0),
00203     m_mod_log( EntityLogCreated )
00204 {
00205   TraceIfWatching("stk::mesh::impl::EntityImpl::EntityImpl", LOG_ENTITY, arg_key);
00206 }
00207 
00208 inline
00209 EntityImpl::EntityImpl()
00210   : m_key(),
00211     m_relation(),
00212     m_bucket( NULL ),
00213     m_bucket_ord(0),
00214     m_owner_rank(0),
00215     m_sync_count(0),
00216     m_mod_log( EntityLogCreated )
00217 {
00218 }
00219 
00220 //----------------------------------------------------------------------
00221 
00224 } // namespace impl
00225 } // namespace mesh
00226 } // namespace stk
00227 
00228 //----------------------------------------------------------------------
00229 //----------------------------------------------------------------------
00230 
00231 #endif /* stk_mesh_EntityImpl_hpp */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines