Sierra Toolkit Version of the Day
BucketImpl.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 <sstream>
00011 #include <cstdlib>
00012 #include <cstring>
00013 #include <stdexcept>
00014 #include <stk_mesh/baseImpl/BucketImpl.hpp>
00015 #include <stk_mesh/base/Bucket.hpp>
00016 #include <stk_mesh/base/BulkData.hpp>
00017 //----------------------------------------------------------------------
00018 
00019 namespace stk {
00020 namespace mesh {
00021 namespace impl {
00022 
00023 //----------------------------------------------------------------------
00024 
00025 namespace {
00026 
00027 void memory_copy( unsigned char * dst , const unsigned char * src , unsigned n )
00028 { std::memcpy( dst , src , n ); }
00029 
00030 
00031 void memory_zero( unsigned char * dst , unsigned n )
00032 { std::memset( dst , 0 , n ); }
00033 
00034 } // namespace
00035 
00036 //----------------------------------------------------------------------
00037 BucketImpl::BucketImpl( BulkData        & arg_mesh ,
00038                 unsigned          arg_entity_rank ,
00039                 const unsigned  * arg_key ,
00040                 size_t            arg_alloc_size ,
00041                 size_t            arg_capacity ,
00042                 impl::BucketImpl::DataMap * arg_field_map ,
00043                 Entity         ** arg_entity_array )
00044 : m_mesh( arg_mesh ) ,
00045   m_entity_rank( arg_entity_rank ) ,
00046   m_key( arg_key ) ,
00047   m_alloc_size( arg_alloc_size ) ,
00048   m_capacity( arg_capacity ) ,
00049   m_size( 0 ) ,
00050   m_bucket() ,
00051   m_field_map( arg_field_map ) ,
00052   m_entities( arg_entity_array )
00053 {}
00054 
00055 //----------------------------------------------------------------------
00056 BucketImpl::~BucketImpl()
00057 {
00058   bool this_is_first_bucket_in_family = (bucket_counter() == 0);
00059   if (this_is_first_bucket_in_family) {
00060     try {
00061       std::free( m_field_map );
00062     } catch(...) {}
00063   }
00064 }
00065 
00066 //----------------------------------------------------------------------
00067 
00068 void BucketImpl::update_state()
00069 {
00070   bool this_is_first_bucket_in_family = ( bucket_counter() == 0 );
00071   if (this_is_first_bucket_in_family) {
00072 
00073     const MetaData & S = MetaData::get(m_mesh);
00074     const std::vector<FieldBase*> & field_set = S.get_fields();
00075 
00076     for ( unsigned i = 0 ; i < field_set.size() ; ) {
00077 
00078       DataMap * const tmp = m_field_map + i ;
00079       const FieldBase & field = * field_set[i] ;
00080       const unsigned num_state = field.number_of_states();
00081       i += num_state ;
00082 
00083       if ( 1 < num_state && tmp->m_size ) {
00084         unsigned offset[ MaximumFieldStates ] ;
00085 
00086         for ( unsigned j = 0 ; j < num_state ; ++j ) {
00087           offset[j] = tmp[j].m_base ;
00088         }
00089 
00090         for ( unsigned j = 0 ; j < num_state ; ++j ) {
00091           const unsigned j_new = ( j + num_state - 1 ) % num_state ;
00092           tmp[j_new].m_base = offset[j] ;
00093         }
00094       }
00095     }
00096   }
00097 }
00098 
00099 //----------------------------------------------------------------------
00100 // Every bucket in the family points to the first bucket,
00101 // except the first bucket which points to the last bucket.
00102 
00103 Bucket * BucketImpl::last_bucket_in_family()
00104 {
00105   Bucket * last = last_bucket_in_family_impl();
00106 
00107   ThrowRequireMsg( NULL != last, "Last is NULL");
00108   ThrowRequireMsg( last->size() != 0, "Last bucket is empty");
00109 
00110   return last ;
00111 }
00112 
00113 Bucket * BucketImpl::last_bucket_in_family_impl()
00114 {
00115   bool this_is_first_bucket_in_family = (bucket_counter() == 0);
00116 
00117   Bucket * last = NULL;
00118 
00119   if (this_is_first_bucket_in_family) {
00120     last = m_bucket;
00121   } else {
00122     last = m_bucket->m_bucketImpl.m_bucket;
00123   }
00124 
00125   return last;
00126 }
00127 
00128 //----------------------------------------------------------------------
00129 
00130 Bucket * BucketImpl::first_bucket_in_family()
00131 {
00132   return last_bucket_in_family_impl()->m_bucketImpl.m_bucket;
00133 }
00134 
00135 //----------------------------------------------------------------------
00136 
00137 void BucketImpl::set_last_bucket_in_family( Bucket * last_bucket )
00138 {
00139   Bucket * last = last_bucket_in_family_impl();
00140   Bucket * first = last->m_bucketImpl.m_bucket;
00141   first->m_bucketImpl.m_bucket = last_bucket;
00142 }
00143 
00144 //----------------------------------------------------------------------
00145 
00146 void BucketImpl::set_first_bucket_in_family( Bucket * first_bucket )
00147 {
00148   m_bucket = first_bucket;
00149 }
00150 
00151 //----------------------------------------------------------------------
00152 
00153 BucketImpl::DataMap * BucketImpl::get_field_map()
00154 {
00155   return m_field_map;
00156 }
00157 
00158 //----------------------------------------------------------------------
00159 
00160 void BucketImpl::zero_fields( unsigned i_dst )
00161 {
00162   const std::vector<FieldBase*> & field_set =
00163     MetaData::get(m_mesh).get_fields();
00164 
00165   unsigned char * const p = reinterpret_cast<unsigned char*>(m_entities);
00166   const DataMap *       i = m_field_map;
00167   const DataMap * const e = i + field_set.size();
00168 
00169   for ( ; i != e ; ++i ) {
00170     if ( i->m_size ) {
00171       memory_zero( p + i->m_base + i->m_size * i_dst , i->m_size );
00172     }
00173   }
00174 }
00175 
00176 void BucketImpl::replace_fields( unsigned i_dst , Bucket & k_src , unsigned i_src )
00177 {
00178   const std::vector<FieldBase*> & field_set =
00179     MetaData::get(m_mesh).get_fields();
00180 
00181   unsigned char * const s = reinterpret_cast<unsigned char*>(k_src.m_bucketImpl.m_entities);
00182   unsigned char * const d = reinterpret_cast<unsigned char*>(m_entities);
00183   const DataMap *       j = k_src.m_bucketImpl.m_field_map;
00184   const DataMap *       i = m_field_map;
00185   const DataMap * const e = i + field_set.size();
00186 
00187   for ( ; i != e ; ++i , ++j ) {
00188 
00189     if ( i->m_size ) {
00190       if ( j->m_size ) {
00191         ThrowErrorMsgIf( i->m_size != j->m_size,
00192             "Incompatible field sizes: " << i->m_size << " != " << j->m_size );
00193 
00194         memory_copy( d + i->m_base + i->m_size * i_dst ,
00195                      s + j->m_base + j->m_size * i_src , i->m_size );
00196       }
00197       else {
00198         memory_zero( d + i->m_base + i->m_size * i_dst , i->m_size );
00199       }
00200     }
00201   }
00202 }
00203 
00204 } // namespace impl
00205 } // namespace mesh
00206 } // namespace stk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends