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 = MetaData::get( *this );
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 = & MetaData::get(k);
00169   const MetaData * const f_mesh_meta_data = & MetaData::get(f);
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_begin ;
00177     msg_begin << "For args: " ;
00178     msg_begin << f << " , " ;
00179     msg_begin << k << " , " ;
00180     msg_begin << ord << " , " ;
00181     msg_begin << required_by ;
00182     msg_begin << "; operation FAILED with " ;
00183     ThrowErrorMsgIf( ! ok_mesh_meta_data,
00184                      msg_begin.str() << " different MetaData");
00185     ThrowErrorMsgIf( ! ok_ord, msg_begin.str() <<
00186                      " Ordinal " <<  ord << " >= " << " size " << k.size());
00187     ThrowErrorMsg( msg_begin.str() << " no data");
00188   }
00189 
00190   return exists ;
00191 }
00192 
00193 //----------------------------------------------------------------------
00194 
00195 Bucket::Bucket( BulkData        & arg_mesh ,
00196                 EntityRank        arg_entity_rank ,
00197                 const unsigned  * arg_key ,
00198                 size_t            arg_alloc_size ,
00199                 size_t            arg_capacity ,
00200                 impl::BucketImpl::DataMap * arg_field_map ,
00201                 Entity         ** arg_entity_array )
00202 : m_bucketImpl( arg_mesh, arg_entity_rank, arg_key, arg_alloc_size, arg_capacity, arg_field_map, arg_entity_array )
00203 {}
00204 
00205 //----------------------------------------------------------------------
00206 
00207 Bucket::~Bucket()
00208 {
00209 }
00210 
00211 //----------------------------------------------------------------------
00212 
00213 std::ostream & operator << ( std::ostream & s , const Bucket & k )
00214 {
00215   const MetaData & mesh_meta_data = MetaData::get(k);
00216   const std::string & entity_rank_name =
00217     k.entity_rank() == InvalidEntityRank ? "Nil" :
00218     mesh_meta_data.entity_rank_names()[ k.entity_rank() ];
00219 
00220   PartVector parts ; k.supersets( parts );
00221 
00222   s << "Bucket( " << entity_rank_name << " : " ;
00223   for ( PartVector::iterator i = parts.begin() ; i != parts.end() ; ++i ) {
00224     s << (*i)->name() << " " ;
00225   }
00226   s << ")" ;
00227 
00228   return s ;
00229 }
00230 
00231 
00232 std::ostream &
00233 print( std::ostream & os , const std::string & indent , const Bucket & bucket )
00234 {
00235   const MetaData & mesh_meta_data = MetaData::get(bucket);
00236   const std::string & entity_rank_name =
00237     bucket.entity_rank() == InvalidEntityRank ? "Nil" :
00238     mesh_meta_data.entity_rank_names()[ bucket.entity_rank() ];
00239 
00240   const std::pair<const unsigned *, const unsigned *>
00241     part_ids = bucket.superset_part_ordinals();
00242 
00243   os << "Bucket(" << std::endl << indent << "Part intersection {" ;
00244 
00245   for ( const unsigned * i = part_ids.first ; i < part_ids.second ; ++i ) {
00246     const Part & part = mesh_meta_data.get_part( *i );
00247     os << " " << part.name();
00248   }
00249 
00250   os << " }" << std::endl << indent << entity_rank_name << " members {" ;
00251 
00252   for ( unsigned j = 0 ; j < bucket.size() ; ++j ) {
00253     const EntityId id = bucket[j].identifier();
00254     os << " " << id ;
00255   }
00256   os << " } )" << std::endl ;
00257 
00258   return os ;
00259 }
00260 
00261 } // namespace mesh
00262 } // namespace stk
00263 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends