Mesh Oriented datABase  (version 5.4.1)
Array-based unstructured mesh datastructure
io.hpp
Go to the documentation of this file.
00001 #ifndef GET_FILE_OPTIONS_H
00002 #define GET_FILE_OPTIONS_H
00003 
00004 #include "moab/Core.hpp"
00005 
00006 #include <iostream>
00007 
00008 namespace moab
00009 {
00010 namespace point_locator
00011 {
00012     namespace io
00013     {
00014 
00015         template < typename String        = std::string,
00016                    typename String_vector = std::vector< std::string >,
00017                    typename Char_vector   = std::vector< const char* > >
00018         class File_options
00019         {
00020           public:
00021             File_options()
00022                 : meshFiles(), interpTag(), gNormTag(), ssNormTag(), readOpts(), outFile(), writeOpts(), dbgFile(),
00023                   ssTagNames(), ssTagValues(), help( true )
00024             {
00025             }
00026             String_vector meshFiles;
00027             String interpTag;
00028             String gNormTag;
00029             String ssNormTag;
00030             String readOpts;
00031             String outFile;
00032             String writeOpts;
00033             String dbgFile;
00034             Char_vector ssTagNames;
00035             Char_vector ssTagValues;
00036             bool help;
00037         };
00038 
00039         // Check first character for a '-'.
00040         // Return true if one is found.  False otherwise.
00041         bool check_for_flag( const char* str )
00042         {
00043             if( str[0] == '-' )
00044                 return true;
00045             else
00046                 return false;
00047         }
00048 
00049         // New get_file_options() function with added possibilities for mbcoupler_test.
00050         ErrorCode get_file_options( int argc,
00051                                     char** argv,
00052                                     std::vector< std::string >& meshFiles,
00053                                     std::string& interpTag,
00054                                     std::string& gNormTag,
00055                                     std::string& ssNormTag,
00056                                     std::vector< const char* >& ssTagNames,
00057                                     std::vector< const char* >& ssTagValues,
00058                                     std::string& readOpts,
00059                                     std::string& outFile,
00060                                     std::string& writeOpts,
00061                                     std::string& dbgFile,
00062                                     bool& help )
00063         {
00064             // Initialize some of the outputs to null values indicating not present
00065             // in the argument list.
00066             gNormTag                   = "";
00067             ssNormTag                  = "";
00068             readOpts                   = "PARALLEL=READ_PART;PARTITION=PARALLEL_PARTITION;PARTITION_DISTRIBUTE;"
00069                                          "PARALLEL_RESOLVE_SHARED_ENTS;PARALLEL_GHOSTS=3.0.1;CPUTIME";
00070             outFile                    = "";
00071             writeOpts                  = "PARALLEL=WRITE_PART;CPUTIME";
00072             dbgFile                    = "";
00073             std::string defaultDbgFile = argv[0];  // The executable name will be the default debug output file.
00074 
00075             // These will indicate if we've gotten our required parameters at the end of parsing.
00076             bool haveMeshes    = false;
00077             bool haveInterpTag = false;
00078 
00079             // Loop over the values in argv pulling out an parsing each one
00080             int npos = 1;
00081 
00082             if( argc > 1 && argv[1] == std::string( "-h" ) )
00083             {
00084                 help = true;
00085                 return MB_SUCCESS;
00086             }
00087 
00088             while( npos < argc )
00089             {
00090                 if( argv[npos] == std::string( "-meshes" ) )
00091                 {
00092                     // Parse out the mesh filenames
00093                     npos++;
00094                     int numFiles = 2;
00095                     meshFiles.resize( numFiles );
00096                     for( int i = 0; i < numFiles; i++ )
00097                     {
00098                         if( ( npos < argc ) && ( !check_for_flag( argv[npos] ) ) )
00099                             meshFiles[i] = argv[npos++];
00100                         else
00101                         {
00102                             std::cerr << "    ERROR - missing correct number of mesh filenames" << std::endl;
00103                             return MB_FAILURE;
00104                         }
00105                     }
00106 
00107                     haveMeshes = true;
00108                 }
00109                 else if( argv[npos] == std::string( "-itag" ) )
00110                 {
00111                     // Parse out the interpolation tag
00112                     npos++;
00113                     if( ( npos < argc ) && ( !check_for_flag( argv[npos] ) ) )
00114                         interpTag = argv[npos++];
00115                     else
00116                     {
00117                         std::cerr << "    ERROR - missing <interp_tag>" << std::endl;
00118                         return MB_FAILURE;
00119                     }
00120 
00121                     haveInterpTag = true;
00122                 }
00123                 else if( argv[npos] == std::string( "-gnorm" ) )
00124                 {
00125                     // Parse out the global normalization tag
00126                     npos++;
00127                     if( ( npos < argc ) && ( !check_for_flag( argv[npos] ) ) )
00128                         gNormTag = argv[npos++];
00129                     else
00130                     {
00131                         std::cerr << "    ERROR - missing <gnorm_tag>" << std::endl;
00132                         return MB_FAILURE;
00133                     }
00134                 }
00135                 else if( argv[npos] == std::string( "-ssnorm" ) )
00136                 {
00137                     // Parse out the subset normalization tag and selection criteria
00138                     npos++;
00139                     if( ( npos < argc ) && ( !check_for_flag( argv[npos] ) ) )
00140                         ssNormTag = argv[npos++];
00141                     else
00142                     {
00143                         std::cerr << "    ERROR - missing <ssnorm_tag>" << std::endl;
00144                         return MB_FAILURE;
00145                     }
00146 
00147                     if( ( npos < argc ) && ( !check_for_flag( argv[npos] ) ) )
00148                     {
00149                         char* opts         = argv[npos++];
00150                         char sep1[1]       = { ';' };
00151                         char sep2[1]       = { '=' };
00152                         bool end_vals_seen = false;
00153                         std::vector< char* > tmpTagOpts;
00154 
00155                         // first get the options
00156                         for( char* i = strtok( opts, sep1 ); i; i = strtok( 0, sep1 ) )
00157                         {
00158                             tmpTagOpts.push_back( i );
00159                         }
00160 
00161                         // parse out the name and val or just name.
00162                         for( unsigned int j = 0; j < tmpTagOpts.size(); j++ )
00163                         {
00164                             char* e = strtok( tmpTagOpts[j], sep2 );
00165                             ssTagNames.push_back( e );
00166                             e = strtok( 0, sep2 );
00167                             if( e != NULL )
00168                             {
00169                                 // We have a value
00170                                 if( end_vals_seen )
00171                                 {
00172                                     // ERROR we should not have a value after none are seen
00173                                     std::cerr << "    ERROR - new value seen after end of values "
00174                                                  "in <ssnorm_selection>"
00175                                               << std::endl;
00176                                     return MB_FAILURE;
00177                                 }
00178                                 // Otherwise get the value string from e and convert it to an int
00179                                 int* valp = new int;
00180                                 *valp     = atoi( e );
00181                                 ssTagValues.push_back( (const char*)valp );
00182                             }
00183                             else
00184                             {
00185                                 // Otherwise there is no '=' so push a null on the list
00186                                 end_vals_seen = true;
00187                                 ssTagValues.push_back( (const char*)0 );
00188                             }
00189                         }
00190                     }
00191                     else
00192                     {
00193                         std::cerr << "    ERROR - missing <ssnorm_selection>" << std::endl;
00194                         return MB_FAILURE;
00195                     }
00196                 }
00197                 else if( argv[npos] == std::string( "-ropts" ) )
00198                 {
00199                     // Parse out the mesh file read options
00200                     npos++;
00201                     if( ( npos < argc ) && ( !check_for_flag( argv[npos] ) ) )
00202                         readOpts = argv[npos++];
00203                     else
00204                     {
00205                         std::cerr << "    ERROR - missing <roptions>" << std::endl;
00206                         return MB_FAILURE;
00207                     }
00208                 }
00209                 else if( argv[npos] == std::string( "-outfile" ) )
00210                 {
00211                     // Parse out the output file name
00212                     npos++;
00213                     if( ( npos < argc ) && ( !check_for_flag( argv[npos] ) ) )
00214                         outFile = argv[npos++];
00215                     else
00216                     {
00217                         std::cerr << "    ERROR - missing <out_file>" << std::endl;
00218                         return MB_FAILURE;
00219                     }
00220                 }
00221                 else if( argv[npos] == std::string( "-wopts" ) )
00222                 {
00223                     // Parse out the output file write options
00224                     npos++;
00225                     if( ( npos < argc ) && ( !check_for_flag( argv[npos] ) ) )
00226                         writeOpts = argv[npos++];
00227                     else
00228                     {
00229                         std::cerr << "    ERROR - missing <woptions>" << std::endl;
00230                         return MB_FAILURE;
00231                     }
00232                 }
00233                 else if( argv[npos] == std::string( "-dbgout" ) )
00234                 {
00235                     // Parse out the debug output file name.
00236                     // If no name then use the default.
00237                     npos++;
00238                     if( ( npos < argc ) && ( !check_for_flag( argv[npos] ) ) )
00239                         dbgFile = argv[npos++];
00240                     else
00241                         dbgFile = defaultDbgFile;
00242                 }
00243                 else
00244                 {
00245                     // Unrecognized parameter.  Skip it and move along.
00246                     std::cerr << "    ERROR - Unrecognized parameter:" << argv[npos] << std::endl;
00247                     std::cerr << "            Skipping..." << std::endl;
00248                     npos++;
00249                 }
00250             }
00251 
00252             if( !haveMeshes )
00253             {
00254                 meshFiles.resize( 2 );
00255                 meshFiles[0] = std::string( TestDir + "/64bricks_1khex.h5m" );
00256                 meshFiles[1] = std::string( TestDir + "/64bricks_12ktet.h5m" );
00257                 std::cout << "Mesh files not entered; using default files " << meshFiles[0] << " and " << meshFiles[1]
00258                           << std::endl;
00259             }
00260 
00261             if( !haveInterpTag )
00262             {
00263                 interpTag = "vertex_field";
00264                 std::cout << "Interpolation field name not given, using default of " << interpTag << std::endl;
00265             }
00266 
00267 #ifdef MOAB_HAVE_HDF5
00268             if( 1 == argc )
00269             {
00270                 std::cout << "No arguments given; using output file dum.h5m." << std::endl;
00271                 outFile = "dum.h5m";
00272             }
00273 #endif
00274 
00275             return MB_SUCCESS;
00276         }
00277 
00278         // "generic" get_file_options
00279         template < typename Options >
00280         ErrorCode get_file_options( int argc, char** argv, Options& o )
00281         {
00282             return get_file_options( argc, argv, o.meshFiles, o.interpTag, o.gNormTag, o.ssNormTag, o.ssTagNames,
00283                                      o.ssTagValues, o.readOpts, o.outFile, o.writeOpts, o.dbgFile, o.help );
00284         }
00285 
00286     }  // namespace io
00287 }  // namespace point_locator
00288 }  // namespace moab
00289 
00290 #endif  // GET_FILE_OPTIONS_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines