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 PartVector & parts ) const
00072 {
00073   const unsigned * const i_beg = key() + 1 ;
00074   const unsigned * const i_end = key() + key()[0] ;
00075 
00076   const PartVector::const_iterator ip_end = parts.end();
00077         PartVector::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 PartVector & parts ) const
00090 {
00091   const unsigned * const i_beg = key() + 1 ;
00092   const unsigned * const i_end = key() + key()[0] ;
00093 
00094   const PartVector::const_iterator ip_end = parts.end();
00095         PartVector::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 bool Bucket::member_any( const OrdinalVector & parts ) const
00108 {
00109   const unsigned * const i_beg = key() + 1 ;
00110   const unsigned * const i_end = key() + key()[0] ;
00111 
00112   const OrdinalVector::const_iterator ip_end = parts.end();
00113         OrdinalVector::const_iterator ip     = parts.begin() ;
00114 
00115   bool result_none = true ;
00116 
00117   for ( ; result_none && ip_end != ip ; ++ip ) {
00118     const unsigned ord = *ip;
00119     const unsigned * const i = std::lower_bound( i_beg , i_end , ord );
00120     result_none = i_end == i || ord != *i ;
00121   }
00122   return ! result_none ;
00123 }
00124 
00125 //----------------------------------------------------------------------
00126 
00127 bool has_superset( const Bucket & bucket, const unsigned & ordinal )
00128 {
00129   std::pair<const unsigned *, const unsigned *>
00130     part_ord = bucket.superset_part_ordinals();
00131 
00132   part_ord.first =
00133     std::lower_bound( part_ord.first , part_ord.second , ordinal );
00134 
00135   return part_ord.first < part_ord.second && ordinal == *part_ord.first ;
00136 }
00137 
00138 bool has_superset( const Bucket & bucket , const Part & p )
00139 {
00140   const unsigned ordinal = p.mesh_meta_data_ordinal();
00141   return has_superset(bucket,ordinal);
00142 }
00143 
00144 bool has_superset( const Bucket & bucket , const PartVector & ps )
00145 {
00146   const std::pair<const unsigned *, const unsigned *>
00147     part_ord = bucket.superset_part_ordinals();
00148 
00149   bool result = ! ps.empty();
00150 
00151   for ( PartVector::const_iterator
00152         i = ps.begin() ; result && i != ps.end() ; ++i ) {
00153 
00154     const unsigned ordinal = (*i)->mesh_meta_data_ordinal();
00155 
00156     const unsigned * iter =
00157       std::lower_bound( part_ord.first , part_ord.second , ordinal );
00158 
00159     result = iter < part_ord.second && ordinal == *iter ;
00160   }
00161   return result ;
00162 }
00163 
00164 void Bucket::supersets( PartVector & ps ) const
00165 {
00166   const MetaData & mesh_meta_data = MetaData::get( *this );
00167 
00168   std::pair<const unsigned *, const unsigned *>
00169     part_ord = superset_part_ordinals();
00170 
00171   ps.resize( part_ord.second - part_ord.first );
00172 
00173   for ( unsigned i = 0 ;
00174         part_ord.first < part_ord.second ; ++(part_ord.first) , ++i ) {
00175     ps[i] = & mesh_meta_data.get_part( * part_ord.first );
00176   }
00177 }
00178 
00179 void Bucket::supersets( OrdinalVector & ps ) const
00180 {
00181   std::pair<const unsigned *, const unsigned *>
00182     part_ord = superset_part_ordinals();
00183 
00184   ps.resize( part_ord.second - part_ord.first );
00185 
00186   for ( unsigned i = 0 ;
00187         part_ord.first < part_ord.second ; ++(part_ord.first) , ++i ) {
00188     ps[i] = *part_ord.first;
00189   }
00190 }
00191 
00192 //----------------------------------------------------------------------
00193 
00194 bool field_data_valid( const FieldBase & f ,
00195                        const Bucket & k ,
00196                        unsigned ord ,
00197                        const char * required_by )
00198 {
00199   const MetaData * const k_mesh_meta_data = & MetaData::get(k);
00200   const MetaData * const f_mesh_meta_data = & MetaData::get(f);
00201   const bool ok_mesh_meta_data  = k_mesh_meta_data == f_mesh_meta_data ;
00202   const bool ok_ord     = ord < k.size() ;
00203   const bool exists     = ok_mesh_meta_data && ok_ord &&
00204                           NULL != field_data( f , k.begin() );
00205 
00206   if ( required_by && ! exists ) {
00207     std::ostringstream msg_begin ;
00208     msg_begin << "For args: " ;
00209     msg_begin << f << " , " ;
00210     msg_begin << k << " , " ;
00211     msg_begin << ord << " , " ;
00212     msg_begin << required_by ;
00213     msg_begin << "; operation FAILED with " ;
00214     ThrowErrorMsgIf( ! ok_mesh_meta_data,
00215                      msg_begin.str() << " different MetaData");
00216     ThrowErrorMsgIf( ! ok_ord, msg_begin.str() <<
00217                      " Ordinal " <<  ord << " >= " << " size " << k.size());
00218     ThrowErrorMsg( msg_begin.str() << " no data");
00219   }
00220 
00221   return exists ;
00222 }
00223 
00224 //----------------------------------------------------------------------
00225 bool Bucket::assert_correct() const {
00226   // test equivalent() method
00227   const Bucket* bucket = this;
00228   const Bucket * first = m_bucketImpl.first_bucket_in_family();
00229   if (!first || ! bucket->equivalent(*first) || ! first->equivalent(*bucket) )
00230     return false;
00231 
00232   // other tests...
00233 
00234   return true;
00235 }
00236 
00237 //----------------------------------------------------------------------
00238 
00239 std::ostream & operator << ( std::ostream & s , const Bucket & k )
00240 {
00241   const MetaData & mesh_meta_data = MetaData::get(k);
00242   const std::string & entity_rank_name =
00243     k.entity_rank() == InvalidEntityRank ? "Nil" :
00244     mesh_meta_data.entity_rank_names()[ k.entity_rank() ];
00245 
00246   PartVector parts ; k.supersets( parts );
00247 
00248   s << "Bucket( " << entity_rank_name << " : " ;
00249   for ( PartVector::iterator i = parts.begin() ; i != parts.end() ; ++i ) {
00250     s << (*i)->name() << " " ;
00251   }
00252   s << ")" ;
00253 
00254   return s ;
00255 }
00256 
00257 
00258 std::ostream &
00259 print( std::ostream & os , const std::string & indent , const Bucket & bucket )
00260 {
00261   const MetaData & mesh_meta_data = MetaData::get(bucket);
00262   const std::string & entity_rank_name =
00263     bucket.entity_rank() == InvalidEntityRank ? "Nil" :
00264     mesh_meta_data.entity_rank_names()[ bucket.entity_rank() ];
00265 
00266   const std::pair<const unsigned *, const unsigned *>
00267     part_ids = bucket.superset_part_ordinals();
00268 
00269   os << "Bucket(" << std::endl << indent << "Part intersection {" ;
00270 
00271   for ( const unsigned * i = part_ids.first ; i < part_ids.second ; ++i ) {
00272     const Part & part = mesh_meta_data.get_part( *i );
00273     os << " " << part.name();
00274   }
00275 
00276   os << " }" << std::endl << indent << entity_rank_name << " members {" ;
00277 
00278   for ( unsigned j = 0 ; j < bucket.size() ; ++j ) {
00279     const EntityId id = bucket[j].identifier();
00280     os << " " << id ;
00281   }
00282   os << " } )" << std::endl ;
00283 
00284   return os ;
00285 }
00286 
00287 } // namespace mesh
00288 } // namespace stk
00289 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines