moab
size.cpp
Go to the documentation of this file.
00001 #include <iostream>
00002 #include <stdlib.h>
00003 #include <vector>
00004 #include <set>
00005 #include <string>
00006 #include <stdio.h>
00007 #include <iomanip>
00008 #ifndef WIN32
00009 #  include <sys/times.h>
00010 #  include <limits.h>
00011 #  include <unistd.h>
00012 #endif
00013 #include <time.h>
00014 #ifdef USE_MPI
00015 #  include "moab_mpi.h"
00016 #endif
00017 #if !defined(_MSC_VER) && !defined(__MINGW32__)
00018 #  include <termios.h>
00019 #  include <sys/ioctl.h>
00020 #endif
00021 #include <math.h>
00022 #include <assert.h>
00023 #include <float.h>
00024 
00025 #include "moab/Core.hpp"
00026 #include "moab/Range.hpp"
00027 #include "MBTagConventions.hpp"
00028 #include "moab/Interface.hpp"
00029 #include "moab/ReaderWriterSet.hpp"
00030 
00031 /* Exit values */
00032 #define USAGE_ERROR 1
00033 #define READ_ERROR 2
00034 #define WRITE_ERROR 3
00035 #define OTHER_ERROR 4
00036 #define ENT_NOT_FOUND 5
00037 
00038 using namespace moab;
00039 
00040 #include "measure.hpp"
00041 
00042 static void print_usage( const char* name, std::ostream& stream )
00043 {
00044   stream << "Usage: " << name 
00045          << " <options> <input_file> [<input_file2> ...]" << std::endl
00046          << "Options: " << std::endl
00047          << "\t-f             - List available file formats and exit." << std::endl
00048          << "\t-g             - print counts by geometric owner" << std::endl
00049          << "\t-h             - Print this help text and exit." << std::endl
00050          << "\t-l             - Print counts of mesh" << std::endl
00051          << "\t-ll            - Verbose listing of every entity" << std::endl
00052          << "\t-m             - Print counts per block/boundary" << std::endl
00053          << "\t-O option      - Specify read option." << std::endl
00054 #ifdef USE_MPI
00055          << "\t-p[0|1|2]      - Read in parallel[0], optionally also doing resolve_shared_ents (1) and exchange_ghosts (2)" << std::endl
00056 #endif
00057          << "\t-t             - Print counts by tag" << std::endl
00058          << "\t-T             - Time read of files." << std::endl
00059          << "\t--             - treat all subsequent options as file names" << std::endl
00060          << "\t                 (allows file names beginning with '-')" << std::endl
00061     ;
00062 }
00063 
00064 Core mb;
00065 
00066 
00067 struct stat_set 
00068 {
00069   double sum;
00070   double sqr;
00071   double min; 
00072   double max;
00073   long count;
00074   
00075   inline stat_set() : sum(0), sqr(0), min(HUGE_VAL), max(0), count (0) {}
00076   
00077   inline void add( double val )
00078   {
00079     if (val < min)
00080       min = val;
00081     if (val > max)
00082       max = val;
00083     sum += val;
00084     sqr += val*val;
00085     ++count;
00086   }
00087   
00088   inline void add( const stat_set& stats )
00089   {
00090     if (stats.min < min)
00091       min = stats.min;
00092     if (stats.max > max)
00093       max = stats.max;
00094     sum += stats.sum;
00095     sqr += stats.sqr;
00096     count += stats.count;
00097   }
00098   
00099   inline void clear()
00100   {
00101     sum = sqr = 0.0;
00102     max = count = 0;
00103     min = HUGE_VAL;
00104   }
00105 };
00106 
00107 struct set_stats {
00108   stat_set stats[MBMAXTYPE];
00109   stat_set edge_uses;
00110   size_t nodes;
00111   
00112   void add( const set_stats& other )
00113   {
00114     for (int i = 0; i < MBMAXTYPE; ++i)
00115       stats[i].add( other.stats[i] );
00116     edge_uses.add( other.edge_uses );
00117   }
00118   
00119   void clear()
00120   {
00121     for (int i = 0; i < MBMAXTYPE; ++i)
00122       stats[i].clear();
00123     edge_uses.clear();
00124   }
00125     
00126 };
00127 
00128 
00129 static ErrorCode gather_set_stats( EntityHandle set, set_stats& stats )
00130 {
00131   ErrorCode rval = MB_SUCCESS;
00132   
00133   int count;
00134   rval = mb.get_number_entities_by_type( set, MBVERTEX, count );
00135   if (MB_SUCCESS != rval) return rval;
00136   stats.nodes = count;
00137   
00138   int edge_vtx_idx[2];
00139   std::vector<EntityHandle> conn;
00140   std::vector<double> coords;
00141   for (EntityType type = MBEDGE; type < MBENTITYSET; ++type)
00142   {
00143     int num_edges = CN::NumSubEntities( type, 1 );
00144     
00145     Range range;
00146     rval = mb.get_entities_by_type( set, type, range, true );
00147     if (MB_SUCCESS != rval) return rval;
00148     for (Range::iterator i = range.begin(); i != range.end(); ++i)
00149     {
00150       rval = mb.get_connectivity( &*i, 1, conn, true );
00151       if (MB_SUCCESS != rval) return rval;
00152       if (type == MBPOLYHEDRON) {
00153         std::vector<EntityHandle> dum_conn(conn);
00154         conn.clear();
00155         rval = mb.get_adjacencies(&dum_conn[0], dum_conn.size(), 0, false, conn, Interface::UNION);
00156         if (MB_SUCCESS != rval) return rval;
00157       }
00158       coords.resize( 3*conn.size() );
00159       rval = mb.get_coords( &conn[0], conn.size(), &coords[0] );
00160       if (MB_SUCCESS != rval) return rval;
00161       stats.stats[type].add( measure( type, conn.size(), &coords[0] ) );
00162       
00163       if (type != MBEDGE)
00164       {
00165         if (type == MBPOLYGON)
00166           num_edges = conn.size();
00167 
00168         for (int e = 0; e < num_edges; ++e)
00169         {
00170           if (type == MBPOLYGON) {
00171             edge_vtx_idx[0] = e;
00172             edge_vtx_idx[1] = e+1;
00173           }
00174           else
00175             CN::SubEntityVertexIndices( type, 1, e, edge_vtx_idx );
00176           stats.edge_uses.add( edge_length( &coords[3*edge_vtx_idx[0]],
00177                                             &coords[3*edge_vtx_idx[1]] ) );
00178         }
00179       }
00180     }
00181   }
00182   return MB_SUCCESS;
00183 }
00184 
00185 struct TagCounts {
00186   TagCounts(std::string n) : name(n) 
00187     { std::fill(counts, counts+MBMAXTYPE, 0); }
00188   std::string name;
00189   int counts[MBMAXTYPE];
00190 };
00191 
00192 static ErrorCode gather_tag_counts( EntityHandle set, 
00193                                     std::vector<TagCounts>& counts )
00194 {
00195   std::vector<Tag> tags;
00196   mb.tag_get_tags( tags );
00197   for (size_t i = 0; i < tags.size(); ++i) {
00198     std::string name;
00199     ErrorCode rval = mb.tag_get_name( tags[i], name );
00200     if (MB_SUCCESS != rval || name.empty())
00201       continue;
00202     
00203     counts.push_back( name );
00204     for (EntityType t = MBVERTEX; t != MBMAXTYPE; ++t) 
00205       mb.get_number_entities_by_type_and_tag( set, t, &tags[i], 0, 1, counts.back().counts[t] );
00206   }
00207   
00208   return MB_SUCCESS;
00209 }
00210 
00211 void add_tag_counts( std::vector<TagCounts>& counts,
00212                      const std::vector<TagCounts>& add )
00213 {
00214   for (size_t i = 0; i < add.size(); ++i) {
00215     size_t j;
00216     for (j = 0; j < counts.size(); ++j)
00217       if (add[i].name == counts[j].name)
00218         break;
00219     if (j == counts.size()) {
00220       counts.push_back( add[i] );
00221       continue;
00222     }
00223     for (EntityType t = MBVERTEX; t != MBMAXTYPE; ++t) 
00224       counts[j].counts[t] += add[i].counts[t];
00225   }
00226 }
00227 
00228 static const char* dashes( unsigned count )
00229 {
00230   static std::vector<char> dashes;
00231   dashes.clear();
00232   dashes.resize( count + 1, '-' );
00233   dashes[count] = '\0';
00234   return &dashes[0];
00235 }
00236 
00237 static void print_tag_counts( const std::vector<TagCounts>& counts )
00238 {
00239   if (counts.empty()) {
00240     printf( "<No tags>\n");
00241     return;
00242   }
00243 
00244   int widths[MBMAXTYPE] = { 0 };
00245   int name_width = 0;
00246   for (size_t i = 0; i < counts.size(); ++i) {
00247     if (counts[i].name.length() > (unsigned)name_width)
00248       name_width = counts[i].name.length();
00249     for (EntityType t = MBVERTEX; t != MBMAXTYPE; ++t) 
00250       if (counts[i].counts[t] != 0)
00251         widths[t] = std::max(8,(int)strlen(CN::EntityTypeName(t)));
00252   }
00253   
00254   if (0 == std::min_element(widths, widths+MBMAXTYPE)) {
00255     printf( "<No Tagged Entities>\n");
00256     return;
00257   }
00258   
00259     // Print header line
00260   const char* name_title = "Tag Name";
00261   if ((unsigned)name_width < strlen(name_title))
00262     name_width = strlen(name_title);
00263   printf( "%*s", name_width, name_title );
00264   for (EntityType t = MBVERTEX; t != MBMAXTYPE; ++t) 
00265     if (widths[t])
00266       printf( " %*s", widths[t], CN::EntityTypeName(t) );
00267   printf("\n%s", dashes(name_width));
00268   for (EntityType t = MBVERTEX; t != MBMAXTYPE; ++t) 
00269     if (widths[t])
00270       printf( " %s", dashes(widths[t]) );
00271     printf("\n");
00272     
00273     // print data
00274   for (size_t i = 0; i < counts.size(); ++i) {
00275     printf( "%*s", name_width, counts[i].name.c_str() );
00276     for (EntityType t = MBVERTEX; t != MBMAXTYPE; ++t) 
00277       if (widths[t])
00278         printf( " %*d", widths[t], counts[i].counts[t] );
00279     printf("\n");
00280   }
00281 }
00282 
00283 static void print_stats( set_stats& stats )
00284 {
00285   const char* edge_use_name = "1D Side";
00286   const char* vertex_name = "Vertex";
00287   
00288   bool have_some = stats.edge_uses.count > 0 || stats.nodes > 0;
00289   for (int i = 0; i < MBMAXTYPE; ++i)
00290     if (stats.stats[i].count > 0)
00291       have_some = true;
00292   
00293   if (!have_some)
00294   {
00295     std::cout << "NO MESH" << std::endl;
00296     return;
00297   }
00298   
00299     // get field widths
00300   unsigned type_width = std::max( strlen(vertex_name), strlen( edge_use_name ) );
00301   unsigned count_width = 5;
00302   unsigned total_width = 5;
00303   unsigned total_prec = 2;
00304   unsigned precision = 5;
00305   int total_log = -10000;
00306   
00307   unsigned node_count_width = (unsigned)(ceil(log10((double)stats.nodes))) + 1;
00308   if (count_width < node_count_width)
00309     count_width = node_count_width;
00310   
00311   for (EntityType i = MBEDGE; i < MBMAXTYPE; ++i)
00312   {
00313     stat_set& s = (i == MBMAXTYPE) ? stats.edge_uses : stats.stats[i];
00314 
00315     if (s.count == 0)
00316       continue;
00317     
00318     unsigned len = strlen(CN::EntityTypeName(i));
00319     if (len > type_width)
00320       type_width = len;
00321     
00322     unsigned cw = (unsigned)(ceil(log10((double)s.count))) + 1;
00323     if (cw > count_width)
00324       count_width = cw;
00325     
00326     int tl = (unsigned)(ceil(log10(fabs(s.sum)))) + 1;
00327     if (tl > total_log)
00328       total_log = tl;
00329   }
00330   
00331   if (total_log > (int)total_width || total_log == -10000)
00332   {
00333     total_width = 8;
00334     total_prec = 2;
00335   }
00336   else if (total_log <= -(int)total_width)
00337   {
00338     total_width = -total_log + 5;
00339     total_prec = 2;
00340   }
00341   else if (total_log < 1)
00342   {
00343     total_width = -total_log + 4;
00344     total_prec = -total_log + 1;
00345   }
00346   else
00347   {
00348     total_width += 2;
00349   }
00350     
00351   
00352   // get terminal width
00353   unsigned term_width = 80;
00354 #if !defined(_MSC_VER) && !defined(__MINGW32__)
00355   struct winsize size;
00356   if ( ioctl( fileno(stdout), TIOCGWINSZ, (char*)&size ) == 0 )
00357     term_width = size.ws_col;
00358   if (!term_width) term_width = 80;
00359 #endif
00360   assert(term_width > 7 + type_width + count_width + total_width);
00361   
00362   term_width -= 7; // spaces
00363   term_width -= type_width;
00364   term_width -= count_width;
00365   term_width -= total_width;
00366   unsigned val_width = term_width / 5;
00367   if (val_width < 8)
00368     val_width = 8;
00369   
00370   printf( "%*s %*s %*s %*s %*s %*s %*s %*s\n",
00371           type_width, "type",
00372           count_width, "count",
00373           total_width, "total",
00374           val_width, "minimum",
00375           val_width, "average",
00376           val_width, "rms",
00377           val_width, "maximum",
00378           val_width, "std.dev." );
00379   
00380   printf( "%*s ", type_width, dashes(type_width) );
00381   printf( "%*s ", count_width, dashes(count_width) );
00382   printf( "%*s ", total_width, dashes(total_width) );
00383   printf( "%*s ", val_width, dashes(val_width) );
00384   printf( "%*s ", val_width, dashes(val_width) );
00385   printf( "%*s ", val_width, dashes(val_width) );
00386   printf( "%*s ", val_width, dashes(val_width) );
00387   printf( "%*s\n", val_width, dashes(val_width) );
00388   
00389   for (EntityType i = MBEDGE; i <= MBMAXTYPE; ++i)
00390   {
00391     stat_set& s = (i == MBMAXTYPE) ? stats.edge_uses : stats.stats[i];
00392     
00393     if (s.count == 0)
00394       continue;
00395 
00396     double tmp_dbl = s.sqr / s.count - s.sum*s.sum / (double)s.count / (double)s.count;
00397     if (tmp_dbl < 0.0) {
00398       if (tmp_dbl < -100.0*DBL_EPSILON)
00399         std::cout << "WARNING: stat values dubious, s^2 - sig_s = " << tmp_dbl << std::endl;
00400       tmp_dbl = 0.0;
00401     }
00402     
00403     printf( "%*s %*ld %*.*g %*.*g %*.*g %*.*g %*.*g %*.*g\n",
00404             type_width, i == MBMAXTYPE ? edge_use_name : CN::EntityTypeName(i),
00405             count_width, s.count,
00406             total_width, total_prec, s.sum,
00407             val_width, precision, s.min,
00408             val_width, precision, s.sum / s.count,
00409             val_width, precision, sqrt( s.sqr / s.count ),
00410             val_width, precision, s.max,
00411             val_width, precision,  
00412             sqrt(tmp_dbl)
00413           );
00414   }
00415   printf( "%*s %*lu\n", type_width, vertex_name, count_width, (unsigned long)stats.nodes );
00416   
00417   puts("");
00418 }
00419 
00420 bool parse_id_list( const char* string, std::set<int>& results )
00421 {
00422   bool okay = true;
00423   char* mystr = strdup( string );
00424   for (const char* ptr = strtok(mystr, ","); ptr; ptr = strtok(0,","))
00425   {
00426     char* endptr;
00427     long val = strtol( ptr, &endptr, 0 );
00428     if (endptr == ptr || val <= 0) {
00429       std::cerr << "Not a valid id: " << ptr << std::endl;
00430       okay = false;
00431       break;
00432     }
00433     
00434     long val2 = val;
00435     if (*endptr == '-') {
00436       const char* sptr = endptr+1;
00437       val2 = strtol( sptr, &endptr, 0 );
00438       if (endptr == sptr || val2 <= 0) {
00439         std::cerr << "Not a valid id: " << sptr << std::endl;
00440         okay = false;
00441         break;
00442       }
00443       if (val2 < val) {
00444         std::cerr << "Invalid id range: " << ptr << std::endl;
00445         okay = false;
00446         break;
00447       }
00448     }
00449     
00450     if (*endptr) {
00451       std::cerr << "Unexpected character: " << *endptr << std::endl;
00452       okay = false;
00453       break;
00454     }
00455     
00456     for (; val <= val2; ++val)
00457       if (!results.insert( (int)val ).second) 
00458         std::cerr << "Warning: duplicate Id: " << val << std::endl;
00459 
00460   }
00461   
00462   free( mystr );
00463   return okay;    
00464 }
00465 
00466 bool make_opts_string( std::vector<std::string> options, std::string& opts )
00467 {
00468   opts.clear();
00469   if (options.empty())
00470     return true;
00471 
00472     // choose a separator character
00473   std::vector<std::string>::const_iterator i;
00474   char separator = '\0';
00475   const char* alt_separators = ";+,:\t\n";
00476   for (const char* sep_ptr = alt_separators; *sep_ptr; ++sep_ptr) {
00477     bool seen = false;
00478     for (i = options.begin(); i != options.end(); ++i)
00479       if (i->find( *sep_ptr, 0 ) != std::string::npos) {
00480         seen = true;
00481         break;
00482       }
00483     if (!seen) {
00484       separator = *sep_ptr;
00485       break;
00486     }
00487   }
00488   if (!separator) {
00489     std::cerr << "Error: cannot find separator character for options string" << std::endl;
00490     return false;
00491   }
00492   if (separator != ';') {
00493     opts = ";";
00494     opts += separator;
00495   }
00496   
00497     // concatenate options
00498   i = options.begin();
00499   opts += *i;
00500   for (++i; i != options.end(); ++i) {
00501     opts += separator;
00502     opts += *i;
00503   }
00504 
00505   return true;
00506 }
00507 
00508 
00509 void list_formats( Interface* gMB )
00510 {
00511   const char iface_name[] = "ReaderWriterSet";
00512   ErrorCode err;
00513   ReaderWriterSet* set = 0;
00514   ReaderWriterSet::iterator i;
00515   std::ostream& str = std::cout;
00516     
00517     // get ReaderWriterSet
00518   err = gMB->query_interface( set );
00519   if (err != MB_SUCCESS || !set) {
00520     std::cerr << "Internal error:  Interface \"" << iface_name 
00521               << "\" not available.\n";
00522     exit(OTHER_ERROR);
00523   }
00524   
00525     // get field width for format description
00526   size_t w = 0;
00527   for (i = set->begin(); i != set->end(); ++i)
00528     if (i->description().length() > w)
00529       w = i->description().length();
00530   
00531     // write table header
00532   str << "Format  " << std::setw(w) << std::left << "Description"
00533       << "  Read  Write  File Name Suffixes\n"
00534       << "------  " << std::setw(w) << std::setfill('-') << "" << std::setfill(' ')
00535       << "  ----  -----  ------------------\n";
00536       
00537     // write table data
00538   for (i = set->begin(); i != set->end(); ++i)
00539   {
00540     std::vector<std::string> ext;
00541     i->get_extensions( ext );
00542     str << std::setw(6) << i->name() << "  " 
00543         << std::setw(w) << std::left << i->description() << "  "
00544         << (i->have_reader() ?  " yes" :  "  no") << "  "
00545         << (i->have_writer() ? "  yes" : "   no") << " ";
00546     for (std::vector<std::string>::iterator j = ext.begin(); j != ext.end(); ++j)
00547       str << " " << *j;
00548     str << std::endl;
00549   }
00550   str << std::endl;
00551   
00552   gMB->release_interface( set );
00553   exit(0);
00554 }
00555 
00556 static void usage_error( const char* name )
00557 {
00558   print_usage( name, std::cerr );
00559 #ifdef USE_MPI
00560   MPI_Finalize();
00561 #endif
00562   exit(USAGE_ERROR);
00563 } 
00564 
00565 static void print_time( int clk_per_sec, const char* prefix, clock_t ticks, std::ostream& stream )
00566 {
00567   ticks *= clk_per_sec/100;
00568   clock_t centi = ticks % 100;
00569   clock_t seconds = ticks / 100;
00570   stream << prefix;
00571   if (seconds < 120)
00572   {
00573     stream << (ticks / 100) << "." << centi << "s" << std::endl;
00574   }
00575   else
00576   {
00577     clock_t minutes = (seconds / 60) % 60;
00578     clock_t hours = (seconds / 3600);
00579     seconds %= 60;
00580     if (hours)
00581       stream << hours << "h";
00582     if (minutes)
00583       stream << minutes << "m";
00584     if (seconds || centi)
00585       stream << seconds << "." << centi << "s";
00586     stream << " (" << (ticks/100) << "." << centi << "s)" << std::endl;
00587   }
00588 }
00589 
00590 clock_t usr_time, sys_time, abs_time;
00591 
00592 #ifdef WIN32
00593 
00594 void reset_times() 
00595 {
00596   abs_time = clock();
00597 }
00598 
00599 
00600 void write_times( std::ostream& stream ) 
00601 {
00602   clock_t abs_tm = clock();
00603   print_time( CLOCKS_PER_SEC, "  ", abs_tm - abs_time, stream );
00604   abs_time = abs_tm;
00605 }
00606 
00607 #else
00608 
00609 void reset_times()
00610 {
00611   tms timebuf;
00612   abs_time = times( &timebuf );
00613   usr_time = timebuf.tms_utime;
00614   sys_time = timebuf.tms_stime;
00615 }
00616 
00617 void write_times( std::ostream& stream )
00618 {
00619   clock_t usr_tm, sys_tm, abs_tm;
00620   tms timebuf;
00621   abs_tm = times( &timebuf );
00622   usr_tm = timebuf.tms_utime;
00623   sys_tm = timebuf.tms_stime;
00624   print_time( sysconf(_SC_CLK_TCK), "  real:   ", abs_tm - abs_time, stream );
00625   print_time( sysconf(_SC_CLK_TCK), "  user:   ", usr_tm - usr_time, stream );
00626   print_time( sysconf(_SC_CLK_TCK), "  system: ", sys_tm - sys_time, stream );
00627   abs_time = abs_tm;
00628   usr_time = usr_tm;
00629   sys_time = sys_tm;
00630 }
00631 
00632 #endif
00633 
00634 const char* geom_type_names[] = { "Vertex", "Curve", "Surface", "Volume" } ;
00635 const char* mesh_type_names[] = { "Dirichlet Set", "Neumann Set", "Material Set" };
00636 const char* mesh_type_tags[] = { DIRICHLET_SET_TAG_NAME, NEUMANN_SET_TAG_NAME, MATERIAL_SET_TAG_NAME };
00637 
00638 int main( int argc, char* argv[] )
00639 {
00640   bool geom_owners = false;
00641   bool mesh_owners = false;
00642   bool just_list = false;
00643   bool just_list_basic = false;
00644   bool tag_count = false;
00645   std::vector<std::string> file_list;
00646   set_stats total_stats, file_stats;
00647   std::vector<TagCounts> total_counts, file_counts;
00648   ErrorCode rval;
00649   
00650   Range range;
00651 
00652   int i;
00653   std::vector<std::string> read_opts;
00654   
00655   int proc_id = 0;
00656 #ifdef USE_MPI
00657   int initd = 0;
00658   MPI_Initialized(&initd);
00659   if (!initd) MPI_Init(&argc,&argv);
00660   MPI_Comm_rank( MPI_COMM_WORLD, &proc_id );
00661 #endif
00662 
00663     // scan arguments
00664   bool do_flag = true;
00665   bool print_times = false;
00666   bool parallel = false, resolve_shared = false, exchange_ghosts = false;
00667   bool printed_usage = false;
00668   for (i = 1; i < argc; i++)
00669   {
00670     if (!argv[i][0])
00671       usage_error(argv[0]);
00672       
00673     if (do_flag && argv[i][0] == '-')
00674     {
00675       switch ( argv[i][1] )
00676       {
00677           // do flag arguments:
00678         case '-': do_flag = false;       break;
00679         case 'T': print_times = true;    break;
00680         case 'h': 
00681         case 'H': print_usage( argv[0], std::cerr ); printed_usage = true; break;
00682         case 'f': list_formats( &mb );   break;
00683         case 'l': 
00684             if (strlen(argv[i]) == 2)
00685               just_list_basic = true;
00686             else if (strlen(argv[i]) == 3 && argv[i][2] == 'l')
00687               just_list = true;
00688             break;
00689 #ifdef USE_MPI
00690         case 'p':
00691             parallel = true;
00692             if (argv[i][2] == '1' || argv[i][2] == '2') resolve_shared = true;
00693             if (argv[i][2] == '2') exchange_ghosts = true;
00694             break;
00695 #endif
00696         case 'g': geom_owners = true; break;
00697         case 'm': mesh_owners = true; break;
00698         case 't': tag_count = true; break;
00699         default: 
00700             ++i;
00701             switch ( argv[i-1][1] )
00702             {
00703               case 'O':  read_opts.push_back(argv[i]); break;
00704               default: std::cerr << "Invalid option: " << argv[i] << std::endl;
00705             }
00706           
00707       }
00708     }
00709       // do file names
00710     else {
00711       file_list.push_back( argv[i] );
00712     }
00713   }
00714     
00715     // construct options string from individual options
00716   std::string read_options;
00717   if (parallel) {
00718     read_opts.push_back("PARALLEL=READ_PART");
00719     read_opts.push_back("PARTITION=PARALLEL_PARTITION");
00720   }
00721   if (resolve_shared) read_opts.push_back("PARALLEL_RESOLVE_SHARED_ENTS");
00722   if (exchange_ghosts) read_opts.push_back("PARALLEL_GHOSTS=3.0.1");
00723   
00724   if (!make_opts_string(  read_opts,  read_options )) 
00725   {
00726 #ifdef USE_MPI
00727     MPI_Finalize();
00728 #endif
00729     return USAGE_ERROR;
00730   }
00731   
00732   if (file_list.empty() && !printed_usage)
00733     print_usage(argv[0], std::cerr);
00734   
00735   for (std::vector<std::string>::iterator f = file_list.begin(); f != file_list.end(); ++f)
00736   {
00737     reset_times();
00738     printf("File %s:\n", f->c_str() );
00739     if (MB_SUCCESS != mb.load_file( f->c_str(), NULL, read_options.c_str() ))
00740     {
00741       fprintf(stderr, "Error reading file: %s\n", f->c_str() );
00742       return 1;
00743     }
00744     
00745     if (tag_count)
00746       rval = gather_tag_counts( 0, file_counts );
00747     else if (!just_list)
00748       rval = gather_set_stats( 0, file_stats );
00749     else
00750       rval = MB_SUCCESS;
00751 
00752     if (MB_SUCCESS != rval)
00753     {
00754       fprintf(stderr, "Error processing mesh from file: %s\n", f->c_str());
00755       return 1;
00756     }
00757     
00758     if (tag_count) {
00759       add_tag_counts( total_counts, file_counts );
00760       print_tag_counts( file_counts );
00761       file_counts.clear();
00762     }
00763     else if (just_list) {
00764       mb.list_entities( 0, -1 );
00765     }
00766     else {
00767       total_stats.add( file_stats );
00768       print_stats( file_stats );
00769       file_stats.clear();
00770     }
00771     
00772     if (geom_owners)
00773     {
00774       Range entities;
00775       Tag dim_tag = 0, id_tag = 0;
00776       rval = mb.tag_get_handle( GEOM_DIMENSION_TAG_NAME, 1, MB_TYPE_INTEGER, dim_tag );
00777       if (MB_TAG_NOT_FOUND == rval) 
00778       {
00779         fprintf( stderr, "No geometry tag defined.\n" );
00780       }
00781       else if (MB_SUCCESS != rval)
00782       {
00783         fprintf( stderr, "Error retreiving geometry tag.\n");
00784         return 2;
00785       }
00786       
00787       rval = mb.tag_get_handle( GLOBAL_ID_TAG_NAME, 1, MB_TYPE_INTEGER, id_tag );
00788       if (MB_TAG_NOT_FOUND == rval) 
00789       {
00790         fprintf( stderr, "No ID tag defined.\n" );
00791       }
00792       else if (MB_SUCCESS != rval)
00793       {
00794         fprintf( stderr, "Error retreiving ID tag.\n");
00795         return 2;
00796       }
00797       
00798       if (dim_tag && id_tag)
00799       {
00800         if (MB_SUCCESS != mb.get_entities_by_type_and_tag( 0, 
00801                                                         MBENTITYSET, 
00802                                                         &dim_tag,
00803                                                         0,
00804                                                         1,
00805                                                         entities ))
00806         {
00807           fprintf( stderr, "Error retreiving geometry entitities.\n" );
00808         }
00809       }
00810       
00811       if (entities.empty())
00812       {
00813         fprintf( stderr, "No geometry entities defined in file.\n" );
00814       }
00815       
00816       for (Range::iterator rit = entities.begin(); rit != entities.end(); ++rit)
00817       {
00818         int id = 0, dim = 0;
00819         if (MB_SUCCESS != mb.tag_get_data( dim_tag, &*rit, 1, &dim ) ||
00820             MB_SUCCESS != mb.tag_get_data(  id_tag, &*rit, 1,  &id ))
00821         {
00822           fprintf( stderr, "Error retreiving tag data for geometry entity.\n");
00823           continue;
00824         }
00825         
00826         printf( "%s %d:\n", geom_type_names[dim], id );
00827         if (tag_count)
00828           rval = gather_tag_counts( *rit, file_counts );
00829         else if (!just_list && !just_list_basic)
00830           rval = gather_set_stats( *rit, file_stats );
00831         
00832         if (MB_SUCCESS != rval)
00833           fprintf(stderr, "Error processing mesh from file: %s\n", f->c_str());
00834         else if (tag_count)
00835           print_tag_counts( file_counts );
00836         else if (just_list)
00837           mb.list_entities( 0, 1 );
00838         else if (just_list_basic)
00839           mb.list_entities( 0, 0 );
00840         else
00841           print_stats( file_stats );
00842 
00843         file_stats.clear();
00844         file_counts.clear();
00845       }
00846     }
00847 
00848 
00849     if (mesh_owners)
00850     {
00851       for (int t = 0; t < 3; ++t)
00852       {
00853         Range entities;
00854         Tag tag = 0;
00855         rval = mb.tag_get_handle( mesh_type_tags[t], 1, MB_TYPE_INTEGER, tag );
00856         if (MB_TAG_NOT_FOUND == rval) 
00857         {
00858           continue;
00859         }
00860         else if (MB_SUCCESS != rval)
00861         {
00862           fprintf( stderr, "Error retreiving %s tag.\n", mesh_type_tags[t]);
00863           return 2;
00864         }
00865       
00866         if (MB_SUCCESS != mb.get_entities_by_type_and_tag( 0, 
00867                                                         MBENTITYSET, 
00868                                                         &tag,
00869                                                         0,
00870                                                         1,
00871                                                         entities ))
00872         {
00873           fprintf( stderr, "Error retreiving %s entitities.\n", mesh_type_names[t] );
00874           continue;
00875         }
00876       
00877         for (Range::iterator rit = entities.begin(); rit != entities.end(); ++rit)
00878         {
00879           int id = 0;
00880           if (MB_SUCCESS != mb.tag_get_data( tag, &*rit, 1, &id ))
00881           {
00882             fprintf( stderr, "Error retreiving tag data for %s entity.\n", mesh_type_names[t]);
00883             continue;
00884           }
00885 
00886           printf( "%s %d:\n", mesh_type_names[t], id );
00887           if (tag_count) {
00888             rval = gather_tag_counts( *rit, file_counts );
00889             if (MB_SUCCESS != rval) 
00890               fprintf(stderr, "Error processing tags from file: %s\n", f->c_str());
00891             else
00892               print_tag_counts( file_counts );
00893           }
00894           else if (just_list)
00895             mb.list_entities( 0, 1 );
00896           else if (just_list_basic)
00897             mb.list_entities( 0, 0 );
00898           else if (!just_list && !just_list_basic) {
00899             rval = gather_set_stats( *rit, file_stats );
00900 
00901             if (rval != MB_SUCCESS)
00902               fprintf(stderr, "Error processing mesh from file: %s\n", f->c_str());
00903             else
00904               print_stats( file_stats );
00905           }
00906           file_stats.clear();
00907           file_counts.clear();
00908         }
00909       }
00910     }
00911     
00912     if (print_times && !proc_id) write_times( std::cout );
00913     mb.delete_mesh();
00914   }
00915   
00916   if (file_list.size() > 1 && !just_list && !just_list_basic)
00917   {
00918     printf("Total for all files:\n");
00919     if (tag_count)
00920       print_tag_counts( total_counts );
00921     else
00922       print_stats( total_stats );
00923   }
00924   
00925   return 0;
00926 }
00927 
00928   
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines