Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
io.hpp
Go to the documentation of this file.
1 #ifndef GET_FILE_OPTIONS_H
2 #define GET_FILE_OPTIONS_H
3 
4 #include "moab/Core.hpp"
5 
6 #include <iostream>
7 
8 namespace moab
9 {
10 namespace point_locator
11 {
12  namespace io
13  {
14 
15  template < typename String = std::string,
16  typename String_vector = std::vector< std::string >,
17  typename Char_vector = std::vector< const char* > >
19  {
20  public:
23  ssTagNames(), ssTagValues(), help( true )
24  {
25  }
26  String_vector meshFiles;
27  String interpTag;
28  String gNormTag;
29  String ssNormTag;
30  String readOpts;
31  String outFile;
32  String writeOpts;
33  String dbgFile;
34  Char_vector ssTagNames;
35  Char_vector ssTagValues;
36  bool help;
37  };
38 
39  // Check first character for a '-'.
40  // Return true if one is found. False otherwise.
41  bool check_for_flag( const char* str )
42  {
43  if( str[0] == '-' )
44  return true;
45  else
46  return false;
47  }
48 
49  // New get_file_options() function with added possibilities for mbcoupler_test.
51  char** argv,
52  std::vector< std::string >& meshFiles,
53  std::string& interpTag,
54  std::string& gNormTag,
55  std::string& ssNormTag,
56  std::vector< const char* >& ssTagNames,
57  std::vector< const char* >& ssTagValues,
58  std::string& readOpts,
59  std::string& outFile,
60  std::string& writeOpts,
61  std::string& dbgFile,
62  bool& help )
63  {
64  // Initialize some of the outputs to null values indicating not present
65  // in the argument list.
66  gNormTag = "";
67  ssNormTag = "";
68  readOpts = "PARALLEL=READ_PART;PARTITION=PARALLEL_PARTITION;PARTITION_DISTRIBUTE;"
69  "PARALLEL_RESOLVE_SHARED_ENTS;PARALLEL_GHOSTS=3.0.1;CPUTIME";
70  outFile = "";
71  writeOpts = "PARALLEL=WRITE_PART;CPUTIME";
72  dbgFile = "";
73  std::string defaultDbgFile = argv[0]; // The executable name will be the default debug output file.
74 
75  // These will indicate if we've gotten our required parameters at the end of parsing.
76  bool haveMeshes = false;
77  bool haveInterpTag = false;
78 
79  // Loop over the values in argv pulling out an parsing each one
80  int npos = 1;
81 
82  if( argc > 1 && argv[1] == std::string( "-h" ) )
83  {
84  help = true;
85  return MB_SUCCESS;
86  }
87 
88  while( npos < argc )
89  {
90  if( argv[npos] == std::string( "-meshes" ) )
91  {
92  // Parse out the mesh filenames
93  npos++;
94  int numFiles = 2;
95  meshFiles.resize( numFiles );
96  for( int i = 0; i < numFiles; i++ )
97  {
98  if( ( npos < argc ) && ( !check_for_flag( argv[npos] ) ) )
99  meshFiles[i] = argv[npos++];
100  else
101  {
102  std::cerr << " ERROR - missing correct number of mesh filenames" << std::endl;
103  return MB_FAILURE;
104  }
105  }
106 
107  haveMeshes = true;
108  }
109  else if( argv[npos] == std::string( "-itag" ) )
110  {
111  // Parse out the interpolation tag
112  npos++;
113  if( ( npos < argc ) && ( !check_for_flag( argv[npos] ) ) )
114  interpTag = argv[npos++];
115  else
116  {
117  std::cerr << " ERROR - missing <interp_tag>" << std::endl;
118  return MB_FAILURE;
119  }
120 
121  haveInterpTag = true;
122  }
123  else if( argv[npos] == std::string( "-gnorm" ) )
124  {
125  // Parse out the global normalization tag
126  npos++;
127  if( ( npos < argc ) && ( !check_for_flag( argv[npos] ) ) )
128  gNormTag = argv[npos++];
129  else
130  {
131  std::cerr << " ERROR - missing <gnorm_tag>" << std::endl;
132  return MB_FAILURE;
133  }
134  }
135  else if( argv[npos] == std::string( "-ssnorm" ) )
136  {
137  // Parse out the subset normalization tag and selection criteria
138  npos++;
139  if( ( npos < argc ) && ( !check_for_flag( argv[npos] ) ) )
140  ssNormTag = argv[npos++];
141  else
142  {
143  std::cerr << " ERROR - missing <ssnorm_tag>" << std::endl;
144  return MB_FAILURE;
145  }
146 
147  if( ( npos < argc ) && ( !check_for_flag( argv[npos] ) ) )
148  {
149  char* opts = argv[npos++];
150  char sep1[1] = { ';' };
151  char sep2[1] = { '=' };
152  bool end_vals_seen = false;
153  std::vector< char* > tmpTagOpts;
154 
155  // first get the options
156  for( char* i = strtok( opts, sep1 ); i; i = strtok( 0, sep1 ) )
157  {
158  tmpTagOpts.push_back( i );
159  }
160 
161  // parse out the name and val or just name.
162  for( unsigned int j = 0; j < tmpTagOpts.size(); j++ )
163  {
164  char* e = strtok( tmpTagOpts[j], sep2 );
165  ssTagNames.push_back( e );
166  e = strtok( 0, sep2 );
167  if( e != NULL )
168  {
169  // We have a value
170  if( end_vals_seen )
171  {
172  // ERROR we should not have a value after none are seen
173  std::cerr << " ERROR - new value seen after end of values "
174  "in <ssnorm_selection>"
175  << std::endl;
176  return MB_FAILURE;
177  }
178  // Otherwise get the value string from e and convert it to an int
179  int* valp = new int;
180  *valp = atoi( e );
181  ssTagValues.push_back( (const char*)valp );
182  }
183  else
184  {
185  // Otherwise there is no '=' so push a null on the list
186  end_vals_seen = true;
187  ssTagValues.push_back( (const char*)0 );
188  }
189  }
190  }
191  else
192  {
193  std::cerr << " ERROR - missing <ssnorm_selection>" << std::endl;
194  return MB_FAILURE;
195  }
196  }
197  else if( argv[npos] == std::string( "-ropts" ) )
198  {
199  // Parse out the mesh file read options
200  npos++;
201  if( ( npos < argc ) && ( !check_for_flag( argv[npos] ) ) )
202  readOpts = argv[npos++];
203  else
204  {
205  std::cerr << " ERROR - missing <roptions>" << std::endl;
206  return MB_FAILURE;
207  }
208  }
209  else if( argv[npos] == std::string( "-outfile" ) )
210  {
211  // Parse out the output file name
212  npos++;
213  if( ( npos < argc ) && ( !check_for_flag( argv[npos] ) ) )
214  outFile = argv[npos++];
215  else
216  {
217  std::cerr << " ERROR - missing <out_file>" << std::endl;
218  return MB_FAILURE;
219  }
220  }
221  else if( argv[npos] == std::string( "-wopts" ) )
222  {
223  // Parse out the output file write options
224  npos++;
225  if( ( npos < argc ) && ( !check_for_flag( argv[npos] ) ) )
226  writeOpts = argv[npos++];
227  else
228  {
229  std::cerr << " ERROR - missing <woptions>" << std::endl;
230  return MB_FAILURE;
231  }
232  }
233  else if( argv[npos] == std::string( "-dbgout" ) )
234  {
235  // Parse out the debug output file name.
236  // If no name then use the default.
237  npos++;
238  if( ( npos < argc ) && ( !check_for_flag( argv[npos] ) ) )
239  dbgFile = argv[npos++];
240  else
241  dbgFile = defaultDbgFile;
242  }
243  else
244  {
245  // Unrecognized parameter. Skip it and move along.
246  std::cerr << " ERROR - Unrecognized parameter:" << argv[npos] << std::endl;
247  std::cerr << " Skipping..." << std::endl;
248  npos++;
249  }
250  }
251 
252  if( !haveMeshes )
253  {
254  meshFiles.resize( 2 );
255  meshFiles[0] = std::string( TestDir + "/64bricks_1khex.h5m" );
256  meshFiles[1] = std::string( TestDir + "/64bricks_12ktet.h5m" );
257  std::cout << "Mesh files not entered; using default files " << meshFiles[0] << " and " << meshFiles[1]
258  << std::endl;
259  }
260 
261  if( !haveInterpTag )
262  {
263  interpTag = "vertex_field";
264  std::cout << "Interpolation field name not given, using default of " << interpTag << std::endl;
265  }
266 
267 #ifdef MOAB_HAVE_HDF5
268  if( 1 == argc )
269  {
270  std::cout << "No arguments given; using output file dum.h5m." << std::endl;
271  outFile = "dum.h5m";
272  }
273 #endif
274 
275  return MB_SUCCESS;
276  }
277 
278  // "generic" get_file_options
279  template < typename Options >
280  ErrorCode get_file_options( int argc, char** argv, Options& o )
281  {
282  return get_file_options( argc, argv, o.meshFiles, o.interpTag, o.gNormTag, o.ssNormTag, o.ssTagNames,
283  o.ssTagValues, o.readOpts, o.outFile, o.writeOpts, o.dbgFile, o.help );
284  }
285 
286  } // namespace io
287 } // namespace point_locator
288 } // namespace moab
289 
290 #endif // GET_FILE_OPTIONS_H