Sierra Toolkit Version of the Day
Bucket.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 #include <stdlib.h>
00010 #include <memory.h>
00011 
00012 #include <stdexcept>
00013 #include <iostream>
00014 #include <sstream>
00015 #include <algorithm>
00016 
00017 #include <stk_mesh/base/Bucket.hpp>
00018 #include <stk_mesh/base/Entity.hpp>
00019 #include <stk_mesh/base/BulkData.hpp>
00020 #include <stk_mesh/base/MetaData.hpp>
00021 #include <stk_mesh/base/FieldData.hpp>
00022 
00023 namespace stk {
00024 namespace mesh {
00025 
00026 //----------------------------------------------------------------------
00027 
00028 bool bucket_part_equal( const unsigned * lhs , const unsigned * rhs )
00029 {
00030   bool result = true ;
00031   {
00032     const unsigned * const end_lhs = lhs + *lhs ;
00033     while ( result && end_lhs != lhs ) {
00034       result = *lhs == *rhs ;
00035       ++lhs ; ++rhs ;
00036     }
00037   }
00038   return result ;
00039 }
00040 
00041 inline
00042 bool bucket_key_less( const unsigned * lhs , const unsigned * rhs )
00043 {
00044   const unsigned * const last_lhs = lhs + ( *lhs < *rhs ? *lhs : *rhs );
00045   while ( last_lhs != lhs && *lhs == *rhs ) { ++lhs ; ++rhs ; }
00046   return *lhs < *rhs ;
00047 }
00048 
00049 // The part count and part ordinals are less
00050 bool BucketLess::operator()( const Bucket * lhs_bucket ,
00051                              const unsigned * rhs ) const
00052 { return bucket_key_less( lhs_bucket->key() , rhs ); }
00053 
00054 bool BucketLess::operator()( const unsigned * lhs ,
00055                              const Bucket * rhs_bucket ) const
00056 { return bucket_key_less( lhs , rhs_bucket->key() ); }
00057 
00058 //----------------------------------------------------------------------
00059 
00060 bool Bucket::member( const Part & part ) const
00061 {
00062   const unsigned * const i_beg = key() + 1 ;
00063   const unsigned * const i_end = key() + key()[0] ;
00064 
00065   const unsigned ord = part.mesh_meta_data_ordinal();
00066   const unsigned * const i = std::lower_bound( i_beg , i_end , ord );
00067 
00068   return i_end != i && ord == *i ;
00069 }
00070 
00071 bool Bucket::member_all( const std::vector<Part*> & parts ) const
00072 {
00073   const unsigned * const i_beg = key() + 1 ;
00074   const unsigned * const i_end = key() + key()[0] ;
00075 
00076   const std::vector<Part*>::const_iterator ip_end = parts.end();
00077         std::vector<Part*>::const_iterator ip     = parts.begin() ;
00078 
00079   bool result_all = true ;
00080 
00081   for ( ; result_all && ip_end != ip ; ++ip ) {
00082     const unsigned ord = (*ip)->mesh_meta_data_ordinal();
00083     const unsigned * const i = std::lower_bound( i_beg , i_end , ord );
00084     result_all = i_end != i && ord == *i ;
00085   }
00086   return result_all ;
00087 }
00088 
00089 bool Bucket::member_any( const std::vector<Part*> & parts ) const
00090 {
00091   const unsigned * const i_beg = key() + 1 ;
00092   const unsigned * const i_end = key() + key()[0] ;
00093 
00094   const std::vector<Part*>::const_iterator ip_end = parts.end();
00095         std::vector<Part*>::const_iterator ip     = parts.begin() ;
00096 
00097   bool result_none = true ;
00098 
00099   for ( ; result_none && ip_end != ip ; ++ip ) {
00100     const unsigned ord = (*ip)->mesh_meta_data_ordinal();
00101     const unsigned * const i = std::lower_bound( i_beg , i_end , ord );
00102     result_none = i_end == i || ord != *i ;
00103   }
00104   return ! result_none ;
00105 }
00106 
00107 //----------------------------------------------------------------------
00108 
00109 bool has_superset( const Bucket & bucket, const unsigned & ordinal )
00110 {
00111   std::pair<const unsigned *, const unsigned *>
00112     part_ord = bucket.superset_part_ordinals();
00113 
00114   part_ord.first =
00115     std::lower_bound( part_ord.first , part_ord.second , ordinal );
00116 
00117   return part_ord.first < part_ord.second && ordinal == *part_ord.first ;
00118 }
00119 
00120 bool has_superset( const Bucket & bucket , const Part & p )
00121 {
00122   const unsigned ordinal = p.mesh_meta_data_ordinal();
00123   return has_superset(bucket,ordinal);
00124 }
00125 
00126 bool has_superset( const Bucket & bucket , const PartVector & ps )
00127 {
00128   const std::pair<const unsigned *, const unsigned *>
00129     part_ord = bucket.superset_part_ordinals();
00130 
00131   bool result = ! ps.empty();
00132 
00133   for ( PartVector::const_iterator
00134         i = ps.begin() ; result && i != ps.end() ; ++i ) {
00135 
00136     const unsigned ordinal = (*i)->mesh_meta_data_ordinal();
00137 
00138     const unsigned * iter =
00139       std::lower_bound( part_ord.first , part_ord.second , ordinal );
00140 
00141     result = iter < part_ord.second && ordinal == *iter ;
00142   }
00143   return result ;
00144 }
00145 
00146 void Bucket::supersets( PartVector & ps ) const
00147 {
00148   const MetaData & mesh_meta_data = mesh().mesh_meta_data();
00149 
00150   std::pair<const unsigned *, const unsigned *>
00151     part_ord = superset_part_ordinals();
00152 
00153   ps.resize( part_ord.second - part_ord.first );
00154 
00155   for ( unsigned i = 0 ;
00156         part_ord.first < part_ord.second ; ++(part_ord.first) , ++i ) {
00157     ps[i] = & mesh_meta_data.get_part( * part_ord.first );
00158   }
00159 }
00160 
00161 //----------------------------------------------------------------------
00162 
00163 bool field_data_valid( const FieldBase & f ,
00164                        const Bucket & k ,
00165                        unsigned ord ,
00166                        const char * required_by )
00167 {
00168   const MetaData * const k_mesh_meta_data = & k.mesh().mesh_meta_data();
00169   const MetaData * const f_mesh_meta_data = & f.mesh_meta_data();
00170   const bool ok_mesh_meta_data  = k_mesh_meta_data == f_mesh_meta_data ;
00171   const bool ok_ord     = ord < k.size() ;
00172   const bool exists     = ok_mesh_meta_data && ok_ord &&
00173                           NULL != field_data( f , k.begin() );
00174 
00175   if ( required_by && ! exists ) {
00176     std::ostringstream msg ;
00177     msg << "stk::mesh::field_data_valid( " ;
00178     msg << f ;
00179     msg << " , " ;
00180     msg << k ;
00181     msg << " , " ;
00182     msg << ord ;
00183     msg << " , " ;
00184     msg << required_by ;
00185     msg << " ) FAILED with " ;
00186     if ( ! ok_mesh_meta_data ) {
00187       msg << " different MetaData" ;
00188     }
00189     else if ( ! ok_ord ) {
00190       msg << " Ordinal " ;
00191       msg << ord ;
00192       msg << " >= " ;
00193       msg << " size " ;
00194       msg << k.size();
00195     }
00196     else {
00197       msg << " no data" ;
00198     }
00199     throw std::runtime_error( msg.str() );
00200   }
00201 
00202   return exists ;
00203 }
00204 
00205 void throw_field_data_array( const FieldBase & f , unsigned R )
00206 {
00207   std::ostringstream msg ;
00208   msg << "stk::mesh::throw_field_data_array( Field["
00209       << f.name() << "].rank() = " << f.rank()
00210       << " , truncation_rank = " << R
00211       << " ) FAILED, bad array truncation" ;
00212   throw std::runtime_error( msg.str() );
00213 }
00214 
00215 //----------------------------------------------------------------------
00216 
00217 Bucket::Bucket( BulkData        & arg_mesh ,
00218                 unsigned          arg_entity_rank ,
00219                 const unsigned  * arg_key ,
00220                 size_t            arg_alloc_size ,
00221                 size_t            arg_capacity ,
00222                 impl::BucketImpl::DataMap * arg_field_map ,
00223                 Entity         ** arg_entity_array )
00224 : m_bucketImpl( arg_mesh, arg_entity_rank, arg_key, arg_alloc_size, arg_capacity, arg_field_map, arg_entity_array )
00225   //m_mesh( arg_mesh ),
00226   //m_entity_rank( arg_entity_rank ),
00227   //m_key( arg_key ),
00228   //m_alloc_size( arg_alloc_size ),
00229   //m_capacity( arg_capacity ),
00230   //m_size( 0 ),
00231   //m_bucket(),
00232   //m_field_map( arg_field_map ),
00233   //m_entities( arg_entity_array )
00234 {}
00235 
00236 
00237 //----------------------------------------------------------------------
00238 
00239 Bucket::~Bucket()
00240 {}
00241 
00242 
00243 
00244 //----------------------------------------------------------------------
00245 
00246 std::ostream & operator << ( std::ostream & s , const Bucket & k )
00247 {
00248   const MetaData & mesh_meta_data = k.mesh().mesh_meta_data();
00249   const std::string & entity_name =
00250     mesh_meta_data.entity_rank_names()[ k.entity_rank() ];
00251 
00252   PartVector parts ; k.supersets( parts );
00253 
00254   s << "Bucket( " << entity_name << " : " ;
00255   for ( PartVector::iterator i = parts.begin() ; i != parts.end() ; ++i ) {
00256     s << (*i)->name() << " " ;
00257   }
00258   s << ")" ;
00259 
00260   return s ;
00261 }
00262 
00263 
00264 std::ostream &
00265 print( std::ostream & os , const std::string & indent , const Bucket & bucket )
00266 {
00267   const MetaData & mesh_meta_data = bucket.mesh().mesh_meta_data();
00268   const std::string & entity_name =
00269     mesh_meta_data.entity_rank_names()[ bucket.entity_rank() ];
00270 
00271   const std::pair<const unsigned *, const unsigned *>
00272     part_ids = bucket.superset_part_ordinals();
00273 
00274   os << "Bucket(" << std::endl << indent << "Part intersection {" ;
00275 
00276   for ( const unsigned * i = part_ids.first ; i < part_ids.second ; ++i ) {
00277     const Part & part = mesh_meta_data.get_part( *i );
00278     os << " " << part.name();
00279   }
00280 
00281   os << " }" << std::endl << indent << entity_name << " members {" ;
00282 
00283   for ( unsigned j = 0 ; j < bucket.size() ; ++j ) {
00284     const EntityId id = bucket[j].identifier();
00285     os << " " << id ;
00286   }
00287   os << " } )" << std::endl ;
00288 
00289   return os ;
00290 }
00291 
00292 //----------------------------------------------------------------------
00293 
00294 void
00295 BucketIterator::throw_error(const char * err) const
00296 {
00297   static const char header[] = "stk::mesh::Bucket::iterator FAILED: " ;
00298   std::string msg( header );
00299   msg.append( err );
00300   throw std::runtime_error( msg );
00301 }
00302 
00303 
00304 } // namespace mesh
00305 } // namespace stk
00306 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends