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() const
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() const
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() const
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::initialize_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 (std::vector<FieldBase*>::const_iterator field_iter=field_set.begin() ;
00170        i != e ; ++i, ++field_iter ) {
00171 
00172     if (i->m_size == 0) continue;
00173 
00174     const unsigned char* init_val = reinterpret_cast<const unsigned char*>((*field_iter)->get_initial_value());
00175     if (init_val != NULL) {
00176       memory_copy( p + i->m_base + i->m_size * i_dst , init_val, i->m_size );
00177     }
00178     else {
00179       memory_zero( p + i->m_base + i->m_size * i_dst , i->m_size );
00180     }
00181   }
00182 }
00183 
00184 void BucketImpl::replace_fields( unsigned i_dst , Bucket & k_src , unsigned i_src )
00185 {
00186   const std::vector<FieldBase*> & field_set =
00187     MetaData::get(m_mesh).get_fields();
00188 
00189   unsigned char * const s = reinterpret_cast<unsigned char*>(k_src.m_bucketImpl.m_entities);
00190   unsigned char * const d = reinterpret_cast<unsigned char*>(m_entities);
00191   const DataMap *       j = k_src.m_bucketImpl.m_field_map;
00192   const DataMap *       i = m_field_map;
00193   const DataMap * const e = i + field_set.size();
00194 
00195   for (std::vector<FieldBase*>::const_iterator field_iter=field_set.begin() ;
00196        i != e ; ++i , ++j, ++field_iter ) {
00197 
00198     if ( i->m_size ) {
00199       if ( j->m_size ) {
00200         ThrowErrorMsgIf( i->m_size != j->m_size,
00201             "Incompatible field sizes: " << i->m_size << " != " << j->m_size );
00202 
00203         memory_copy( d + i->m_base + i->m_size * i_dst ,
00204                      s + j->m_base + j->m_size * i_src , i->m_size );
00205       }
00206       else {
00207         const unsigned char* init_val = reinterpret_cast<const unsigned char*>((*field_iter)->get_initial_value());
00208         if (init_val != NULL) {
00209           memory_copy( d + i->m_base + i->m_size * i_dst ,
00210                        init_val, i->m_size );
00211         }
00212         else {
00213           memory_zero( d + i->m_base + i->m_size * i_dst , i->m_size );
00214         }
00215       }
00216     }
00217   }
00218 }
00219 
00220 } // namespace impl
00221 } // namespace mesh
00222 } // namespace stk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends