kspaceFirstOrder3D-OMP  1.1
The C++ implementation of the k-wave toolbox for the time-domain simulation of acoustic wave fields in 3D
 All Classes Files Functions Variables Typedefs Enumerations Friends Pages
CommandLineParameters.cpp
Go to the documentation of this file.
1 /**
2  * @file CommandLineParameters.cpp
3  * @author Jiri Jaros \n
4  * Faculty of Information Technology\n
5  * Brno University of Technology \n
6  * jarosjir@fit.vutbr.cz
7  *
8  * @brief The implementation file containing the command line parameters.
9  *
10  * @version kspaceFirstOrder3D 2.15
11  *
12  * @date 29 August 2012, 11:25 (created) \n
13  * 29 September 2014, 12:23 (revised)
14  *
15  *
16  * @section License
17  * This file is part of the C++ extension of the k-Wave Toolbox (http://www.k-wave.org).\n
18  * Copyright (C) 2014 Jiri Jaros and Bradley Treeby
19  *
20  * This file is part of k-Wave. k-Wave is free software: you can redistribute it
21  * and/or modify it under the terms of the GNU Lesser General Public License as
22  * published by the Free Software Foundation, either version 3 of the License,
23  * or (at your option) any later version.
24  *
25  * k-Wave is distributed in the hope that it will be useful, but
26  * WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28  * See the GNU Lesser General Public License for more details.
29  *
30  * You should have received a copy of the GNU Lesser General Public License
31  * along with k-Wave. If not, see <http://www.gnu.org/licenses/>.
32  */
33 
34 
35 //Linux build
36 #ifdef __linux__
37  #include <getopt.h>
38 #endif
39 
40 //Windows build
41 #ifdef _WIN64
42  #include <GetoptWin64/Getopt.h>
43 #endif
44 
45 #include <stdio.h>
46 #include <string.h>
47 #ifdef _OPENMP
48  #include <omp.h>
49 #endif
50 
52 
53 #include <Utils/ErrorMessages.h>
54 //----------------------------------------------------------------------------//
55 //---------------------------- Constants -------------------------------------//
56 //----------------------------------------------------------------------------//
57 
58 //----------------------------------------------------------------------------//
59 //----------------------------- Public -------------------------------------//
60 //----------------------------------------------------------------------------//
61 
62 
63 
64 /**
65  * Constructor.
66  */
68  InputFileName(""), OutputFileName (""), CheckpointFileName(""),
69  #ifdef _OPENMP
70  NumberOfThreads(omp_get_num_procs()),
71  #else
72  NumberOfThreads(1),
73  #endif
74  VerboseInterval(DefaultVerboseInterval), CompressionLevel (DefaultCompressionLevel),
75  BenchmarkFlag (false), BenchmarkTimeStepsCount(0), CheckpointInterval(0),
76  PrintVersion (false),
77  Store_p_raw(false), Store_p_rms(false), Store_p_max(false), Store_p_min(false),
78  Store_p_max_all(false), Store_p_min_all(false), Store_p_final(false),
79  Store_u_raw(false), Store_u_non_staggered_raw(false),
80  Store_u_rms(false), Store_u_max(false), Store_u_min(false),
81  Store_u_max_all(false), Store_u_min_all(false), Store_u_final(false),
82  CopySensorMask(false),
83  StartTimeStep(0)
84 {
85 
86 }// end of TCommandLineParameters
87 //------------------------------------------------------------------------------
88 
89 /**
90  * Print usage and exit.
91  */
93 {
94  printf("---------------------------------- Usage ---------------------------------\n");
95  printf("Mandatory parameters:\n");
96  printf(" -i <input_file_name> : HDF5 input file\n");
97  printf(" -o <output_file_name> : HDF5 output file\n");
98  printf("\n");
99  printf("Optional parameters: \n");
100 
101 #ifdef _OPENMP
102  printf(" -t <num_threads> : Number of CPU threads\n");
103  printf(" (default = %d)\n", omp_get_num_procs());
104 #endif
105 
106  printf(" -r <interval_in_%%> : Progress print interval\n");
107  printf(" (default = %ld%%)\n", DefaultVerboseInterval);
108  printf(" -c <comp_level> : Output file compression level <0,9>\n");
109  printf(" (default = %ld)\n", DefaultCompressionLevel);
110  printf(" --benchmark <steps> : Run a specified number of time steps\n");
111  printf("\n");
112  printf(" --checkpoint_file <file_name> : HDF5 Checkpoint file\n");
113  printf(" --checkpoint_interval <seconds> : Stop after a given number of seconds and\n");
114  printf(" store the actual state\n");
115  printf("\n");
116  printf(" -h : Print help\n");
117  printf(" --help : Print help\n");
118  printf(" --version : Print version\n");
119  printf("\n");
120  printf("Output flags:\n");
121  printf(" -p : Store acoustic pressure \n");
122  printf(" (default if nothing else is on)\n");
123  printf(" (the same as --p_raw)\n");
124  printf(" --p_raw : Store raw time series of p (default)\n");
125  printf(" --p_rms : Store rms of p\n");
126  printf(" --p_max : Store max of p\n");
127  printf(" --p_min : Store min of p\n");
128  printf(" --p_max_all : Store max of p (whole domain)\n");
129  printf(" --p_min_all : Store min of p (whole domain)\n");
130  printf(" --p_final : Store final pressure field \n");
131  printf("\n");
132  printf(" -u : Store ux, uy, uz\n");
133  printf(" (the same as --u_raw)\n");
134  printf(" --u_raw : Store raw time series of ux, uy, uz\n");
135  printf(" --u_non_staggered_raw : Store non-staggered raw time series of\n");
136  printf(" ux, uy, uz \n");
137  printf(" --u_rms : Store rms of ux, uy, uz\n");
138  printf(" --u_max : Store max of ux, uy, uz\n");
139  printf(" --u_min : Store min of ux, uy, uz\n");
140  printf(" --u_max_all : Store max of ux, uy, uz (whole domain)\n");
141  printf(" --u_min_all : Store min of ux, uy, uz (whole domain)\n");
142  printf(" --u_final : Store final acoustic velocity\n");
143  printf("\n");
144  printf(" --copy_sensor_mask : Copy sensor mask to the output file\n");
145  printf("\n");
146  printf(" -s <timestep> : Time step when data collection begins\n");
147  printf(" (default = 1)\n");
148  printf("--------------------------------------------------------------------------\n");
149  printf("\n");
150 
151  exit(EXIT_FAILURE);
152 }// end of PrintUsageAndExit
153 //------------------------------------------------------------------------------
154 
155 /**
156  * Print setup.
157  */
159 {
160  printf("List of enabled parameters:\n");
161 
162  printf(" Input file %s\n", InputFileName.c_str());
163  printf(" Output file %s\n", OutputFileName.c_str());
164  printf("\n");
165  printf(" Number of threads %ld\n", NumberOfThreads);
166  printf(" Verbose interval[%%] %ld\n", VerboseInterval);
167  printf(" Compression level %ld\n", CompressionLevel);
168  printf("\n");
169  printf(" Benchmark flag %d\n", BenchmarkFlag);
170  printf(" Benchmark time steps %ld\n", BenchmarkTimeStepsCount);
171  printf("\n");
172  printf(" Checkpoint_file %s\n", CheckpointFileName.c_str());
173  printf(" Checkpoint_interval %ld\n", CheckpointInterval);
174  printf("\n");
175  printf(" Store p_raw %d\n", Store_p_raw);
176  printf(" Store p_rms %d\n", Store_p_rms);
177  printf(" Store p_max %d\n", Store_p_max);
178  printf(" Store p_min %d\n", Store_p_min);
179  printf(" Store p_max_all %d\n", Store_p_max_all);
180  printf(" Store p_min_all %d\n", Store_p_min_all);
181  printf(" Store p_final %d\n", Store_p_final);
182  printf("\n");
183  printf(" Store u_raw %d\n", Store_u_raw);
184  printf(" Store u_non_staggered_raw %d\n", Store_u_non_staggered_raw);
185  printf(" Store u_rms %d\n", Store_u_rms);
186  printf(" Store u_max %d\n", Store_u_max);
187  printf(" Store u_min %d\n", Store_u_min);
188  printf(" Store u_max_all %d\n", Store_u_max_all);
189  printf(" Store u_min_all %d\n", Store_u_min_all);
190  printf(" Store u_final %d\n", Store_u_final);
191  printf("\n");
192  printf(" Copy sensor mask %d\n", CopySensorMask);
193  printf("\n");
194  printf(" Collection begins at %ld\n", StartTimeStep + 1);
195 }// end of PrintSetup
196 //------------------------------------------------------------------------------
197 
198 /**
199  * Parse command line.
200  * @param [in, out] argc
201  * @param [in, out] argv
202  */
203 void TCommandLineParameters::ParseCommandLine(int argc, char** argv)
204 {
205 
206  char c;
207  int longIndex;
208  bool CheckpointFlag = false;
209 
210 #ifdef _OPENMP
211  const char * shortOpts = "i:o:r:c:t:puhs:";
212 #else
213  const char * shortOpts = "i:o:r:c:puIhs:";
214 #endif
215 
216  const struct option longOpts[] =
217  {
218  { "benchmark", required_argument, NULL, 0},
219  { "help", no_argument, NULL, 'h'},
220  { "version", no_argument, NULL, 0},
221  { "checkpoint_file" , required_argument, NULL, 0 },
222  { "checkpoint_interval", required_argument, NULL, 0 },
223 
224  { "p_raw", no_argument, NULL, 'p'},
225  { "p_rms", no_argument, NULL, 0},
226  { "p_max", no_argument, NULL, 0},
227  { "p_min", no_argument, NULL, 0},
228  { "p_max_all", no_argument, NULL, 0},
229  { "p_min_all", no_argument, NULL, 0},
230  { "p_final", no_argument, NULL, 0},
231 
232  { "u_raw", no_argument, NULL, 'u'},
233  { "u_non_staggered_raw", no_argument, NULL, 0},
234  { "u_rms", no_argument, NULL, 0},
235  { "u_max", no_argument, NULL, 0},
236  { "u_min", no_argument, NULL, 0},
237  { "u_max_all", no_argument, NULL, 0},
238  { "u_min_all", no_argument, NULL, 0},
239  { "u_final", no_argument, NULL, 0},
240 
241  { "copy_sensor_mask", no_argument, NULL, 0},
242  { NULL, no_argument, NULL, 0}
243  };
244 
245 
246  // Short parameters //
247  while ((c = getopt_long (argc, argv, shortOpts, longOpts, &longIndex )) != -1)
248  {
249  switch (c)
250  {
251  case 'i':
252  {
253  InputFileName = optarg;
254  break;
255  }
256 
257  case 'o':
258  {
259  OutputFileName = optarg;
260  break;
261  }
262 
263  case 'r':
264  {
265  if ((optarg == NULL) || (atol(optarg) <= 0))
266  {
269  }
270  else
271  {
272  VerboseInterval = atol(optarg);
273  }
274  break;
275  }
276 
277  case 't':
278  {
279  if ((optarg == NULL) || (atol(optarg) <= 0))
280  {
281  fprintf(stderr,"%s", CommandlineParameters_ERR_FMT_NoThreadNumbers);
283  }
284  else
285  {
286  NumberOfThreads = atol(optarg);
287  }
288  break;
289  }
290 
291  case 'c':
292  {
293  if ((optarg == NULL) || (atol(optarg) < 0) || atol(optarg) > 9)
294  {
297  }
298  else
299  {
300  CompressionLevel = atol(optarg);
301  }
302  break;
303  }
304 
305  case 'p':
306  {
307  Store_p_raw = true;
308  break;
309  }
310 
311  case 'u':
312  {
313  Store_u_raw = true;
314  break;
315  }
316 
317  case 'h':
318  {
320  break;
321  }
322 
323  case 's':
324  {
325  if ((optarg == NULL) || (atol(optarg) < 1))
326  {
327  fprintf(stderr,"%s", CommandlineParameters_ERR_FMT_NoStartTimestep);
329  }
330  StartTimeStep = (size_t) (atol(optarg) - 1);
331  break;
332  }
333 
334  // long option without a short arg
335  case 0:
336  {
337  if( strcmp( "benchmark", longOpts[longIndex].name ) == 0 )
338  {
339  BenchmarkFlag = true;
340  if ((optarg == NULL) || (atol(optarg) <= 0))
341  {
344  }
345  else
346  {
347  BenchmarkTimeStepsCount = atol(optarg);
348  }
349  }
350  else if( strcmp( "checkpoint_file", longOpts[longIndex].name ) == 0 )
351  {
352  CheckpointFlag = true;
353  if ((optarg == NULL))
354  {
357  }
358  else
359  {
360  CheckpointFileName = optarg;
361  }
362  }
363  else if( strcmp( "checkpoint_interval", longOpts[longIndex].name ) == 0 )
364  {
365  CheckpointFlag = true ;
366  if ((optarg == NULL) || (atol(optarg) <= 0))
367  {
370  }
371  else
372  {
373  CheckpointInterval = atol(optarg);
374  }
375  }
376  else if (strcmp("version", longOpts[longIndex].name) == 0)
377  {
378  PrintVersion = true;
379  return;
380  }
381 
382  //-- pressure related flags
383  else if (strcmp("p_rms", longOpts[longIndex].name) == 0)
384  {
385  Store_p_rms = true;
386  }
387  else if (strcmp("p_max", longOpts[longIndex].name) == 0)
388  {
389  Store_p_max = true;
390  }
391  else if (strcmp("p_min", longOpts[longIndex].name) == 0)
392  {
393  Store_p_min = true;
394  }
395  else if (strcmp("p_max_all", longOpts[longIndex].name) == 0)
396  {
397  Store_p_max_all = true;
398  }
399  else if (strcmp("p_min_all", longOpts[longIndex].name) == 0)
400  {
401  Store_p_min_all = true;
402  }
403  else if (strcmp("p_final", longOpts[longIndex].name) == 0)
404  {
405  Store_p_final = true;
406  }
407 
408  //-- velocity related flags
409  else if (strcmp("u_non_staggered_raw", longOpts[longIndex].name) == 0)
410  {
412  }
413  else if (strcmp("u_rms", longOpts[longIndex].name) == 0)
414  {
415  Store_u_rms = true;
416  }
417  else if (strcmp("u_max", longOpts[longIndex].name) == 0)
418  {
419  Store_u_max = true;
420  }
421  else if (strcmp("u_min", longOpts[longIndex].name) == 0)
422  {
423  Store_u_min = true;
424  }
425  else if (strcmp("u_max_all", longOpts[longIndex].name) == 0)
426  {
427  Store_u_max_all = true;
428  }
429  else if (strcmp("u_min_all", longOpts[longIndex].name) == 0)
430  {
431  Store_u_min_all = true;
432  }
433  else if (strcmp("u_final", longOpts[longIndex].name) == 0)
434  {
435  Store_u_final = true;
436  }
437 
438  else if (strcmp("copy_sensor_mask", longOpts[longIndex].name) == 0)
439  {
440  CopySensorMask = true;
441  }
442  else
443  {
445  }
446 
447  break;
448  }
449  default:
450  {
452  }
453  }
454  }
455 
456 
457  //-- Post checks --//
458  if (InputFileName == "")
459  {
460  fprintf(stderr, "%s", CommandlineParameters_ERR_FMT_NoInputFile);
462  }
463 
464 
465  if (OutputFileName == "")
466  {
467  fprintf(stderr, "%s", CommandlineParameters_ERR_FMT_NoOutputFile);
469  }
470 
471  if (CheckpointFlag)
472  {
473  if (CheckpointFileName == "")
474  {
475  fprintf(stderr, "%s", CommandlineParameters_ERR_FMT_NoCheckpointFile);
477  }
478  if (CheckpointInterval == 0)
479  {
482  }
483  }
484 
490  {
491  Store_p_raw = true;
492  }
493 
494  //PrintSetup();
495 
496 }// end of ParseCommandLine
497 //------------------------------------------------------------------------------
std::string OutputFileName
Output file name.
bool Store_u_raw
Store_u_raw value.
void PrintUsageAndExit()
Print usage and exit.
size_t CompressionLevel
CompressionLevel value.
const char *const CommandlineParameters_ERR_FMT_NoCheckpointFile
Command line parameters error message.
const char *const CommandlineParameters_ERR_FMT_NoInputFile
Command line parameters error message.
bool Store_u_min
Store_u_min value.
bool Store_u_max
Store_u_max value.
size_t VerboseInterval
VerboseInterval value.
bool Store_u_non_staggered_raw
Store_u_non_staggered_raw value.
bool Store_p_max
Store_p_max value.
bool Store_p_min_all
Store_p_min_all value.
const char *const CommandlineParameters_ERR_FMT_NoThreadNumbers
Command line parameters error message.
void PrintSetup()
Print setup.
bool Store_p_raw
Store_p_raw value.
bool Store_u_final
Store_u_final value.
const char *const CommandlineParameters_ERR_FMT_NoVerboseIntreval
Command line parameters error message.
bool Store_p_min
Store_p_min value.
The header file containing all error messages of the project.
std::string InputFileName
Input file name.
bool Store_p_max_all
Store_p_max_all value.
bool Store_u_rms
Store_u_rms value.
bool BenchmarkFlag
BenchmarkFlag value.
bool Store_p_final
Store_p_final value.
size_t StartTimeStep
StartTimeStep value.
const char *const CommandlineParameters_ERR_FMT_NoStartTimestep
Command line parameters error message.
const char *const CommandlineParameters_ERR_FMT_NoCheckpointInterval
Command line parameters error message.
const char *const CommandlineParameters_ERR_FMT_NoOutputFile
Command line parameters error message.
bool Store_u_min_all
Store_u_min_all value.
The header file containing the command line parameters.
std::string CheckpointFileName
Checkpoint file name.
static const size_t DefaultCompressionLevel
Default compression level.
bool CopySensorMask
Copy sensor mask to the output file.
const char *const CommandlineParameters_ERR_FMT_NoBenchmarkTimeStepCount
Command line parameters error message.
bool Store_u_max_all
Store_u_max_all value.
size_t BenchmarkTimeStepsCount
BenchmarkTimeStepsCount value.
bool PrintVersion
PrintVersion value.
size_t CheckpointInterval
Checkpoint interval in seconds.
const char *const CommandlineParameters_ERR_FMT_NoCompressionLevel
Command line parameters error message.
static const size_t DefaultVerboseInterval
Default verbose interval.
bool Store_p_rms
Store_p_rms value.
void ParseCommandLine(int argc, char **argv)
Parse command line.
size_t NumberOfThreads
NumberOfThreads value.