kspaceFirstOrder3D-OMP  1.2
The C++ implementation of the k-wave toolbox for the time-domain simulation of acoustic wave fields in 3D
CommandLineParameters.cpp
Go to the documentation of this file.
1 /**
2  * @file CommandLineParameters.cpp
3  *
4  * @author Jiri Jaros \n
5  * Faculty of Information Technology \n
6  * Brno University of Technology \n
7  * jarosjir@fit.vutbr.cz
8  *
9  * @brief The implementation file containing the command line parameters.
10  *
11  * @version kspaceFirstOrder3D 2.16
12  *
13  * @date 29 August 2012, 11:25 (created) \n
14  * 04 September 2017, 11:10 (revised)
15  *
16  * @copyright Copyright (C) 2017 Jiri Jaros and Bradley Treeby.
17  *
18  * This file is part of the C++ extension of the [k-Wave Toolbox](http://www.k-wave.org).
19  *
20  * This file is part of the k-Wave. k-Wave is free software: you can redistribute it and/or modify it under the terms
21  * of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the
22  * License, or (at your option) any later version.
23  *
24  * k-Wave is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
25  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
26  * more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public License along with k-Wave.
29  * If not, see [http://www.gnu.org/licenses/](http://www.gnu.org/licenses/).
30  */
31 
32 
33 //Linux build
34 #ifdef __linux__
35  #include <getopt.h>
36 #endif
37 
38 //Windows build
39 #ifdef _WIN64
40  #include <GetoptWin64/Getopt.h>
41 #endif
42 
43 #include <cstring>
44 #ifdef _OPENMP
45  #include <omp.h>
46 #endif
47 
48 #include <stdexcept>
49 
51 #include <Logger/Logger.h>
52 
53 using std::string;
54 //--------------------------------------------------------------------------------------------------------------------//
55 //---------------------------------------------------- Constants -----------------------------------------------------//
56 //--------------------------------------------------------------------------------------------------------------------//
57 
58 
59 
60 //--------------------------------------------------------------------------------------------------------------------//
61 //------------------------------------------------- Public methods ---------------------------------------------------//
62 //--------------------------------------------------------------------------------------------------------------------//
63 
64 /**
65  * Print usage and exit.
66  */
68 {
70 
71  #ifdef _OPENMP
73  #endif
74 
76 }// end of printUsage
77 //----------------------------------------------------------------------------------------------------------------------
78 
79 /**
80  * Print out commandline parameters.
81  */
83 {
85 
88 
91 
92  if (isCheckpointEnabled())
93  {
96 
98 
100  }
101  else
102  {
104  }
105 
106 
108 
110 
111  if (mBenchmarkFlag)
112  {
114  }
115 
117 
118 
119  string sampledQuantitiesList = "";
120  // Sampled p quantities
121 
123  {
124  sampledQuantitiesList += "p_raw, ";
125  }
127  {
128  sampledQuantitiesList += "p_rms, ";
129  }
131  {
132  sampledQuantitiesList += "p_max, ";
133  }
135  {
136  sampledQuantitiesList += "p_min, ";
137  }
139  {
140  sampledQuantitiesList += "p_max_all, ";
141  }
143  {
144  sampledQuantitiesList += "p_min_all, ";
145  }
147  {
148  sampledQuantitiesList += "p_final, ";
149  }
150 
151  // Sampled u quantities
153  {
154  sampledQuantitiesList += "u_raw, ";
155  }
157  {
158  sampledQuantitiesList += "u_rms, ";
159  }
161  {
162  sampledQuantitiesList += "u_max, ";
163  }
165  {
166  sampledQuantitiesList += "u_min, ";
167  }
169  {
170  sampledQuantitiesList += "u_max_all, ";
171  }
173  {
174  sampledQuantitiesList += "u_min_all, ";
175  }
177  {
178  sampledQuantitiesList += "u_final, ";
179  }
180 
182  {
183  sampledQuantitiesList += "u_non_staggered_raw, ";
184  }
185 
186  // remove comma and space symbols
187  if (sampledQuantitiesList.length() > 0)
188  {
189  sampledQuantitiesList.pop_back();
190  sampledQuantitiesList.pop_back();
191  }
192 
194  Logger::wordWrapString(sampledQuantitiesList," ", 2).c_str());
195 
197 
199 
201  {
203  }
204 }// end of printComandlineParamers
205 //----------------------------------------------------------------------------------------------------------------------
206 
207 /**
208  * Parse command line.
209  */
210 void CommandLineParameters::parseCommandLine(int argc, char** argv)
211 {
212  char c;
213  int longIndex = -1;
214  bool checkpointFlag = false;
215 
216  constexpr int errorLineIndent = 9;
217 
218  // all optional arguments are in fact requested. This was chosen to prevent
219  // getopt error messages and provide custom error handling.
220  #ifdef _OPENMP
221  const char* shortOpts = "i:o:r:c:t:puhs:";
222  #else
223  const char* shortOpts = "i:o:r:c:puhs:";
224  #endif
225 
226  const struct option longOpts[] =
227  {
228  { "benchmark", required_argument, nullptr, 1 },
229  { "copy_sensor_mask", no_argument, nullptr, 2 },
230  { "checkpoint_file" , required_argument, nullptr, 3 },
231  { "checkpoint_interval", required_argument, nullptr, 4 },
232  { "help", no_argument, nullptr,'h'},
233  { "verbose", required_argument, nullptr, 5 },
234  { "version", no_argument, nullptr, 6 },
235 
236  { "p_raw", no_argument, nullptr,'p' },
237  { "p_rms", no_argument, nullptr, 10 },
238  { "p_max", no_argument, nullptr, 11 },
239  { "p_min", no_argument, nullptr, 12 },
240  { "p_max_all", no_argument, nullptr, 13 },
241  { "p_min_all", no_argument, nullptr, 14 },
242  { "p_final", no_argument, nullptr, 15 },
243 
244  { "u_raw", no_argument, nullptr,'u' },
245  { "u_rms", no_argument, nullptr, 20},
246  { "u_max", no_argument, nullptr, 21},
247  { "u_min", no_argument, nullptr, 22},
248  { "u_max_all", no_argument, nullptr, 23},
249  { "u_min_all", no_argument, nullptr, 24},
250  { "u_final", no_argument, nullptr, 25},
251  { "u_non_staggered_raw", no_argument, nullptr, 26},
252 
253  { nullptr, no_argument, nullptr, 0}
254  };
255 
256  // all optional arguments are in fact requested. This was chosen to prevent
257  // getopt error messages and provide custom error handling.
258  opterr = 0;
259 
260  // Short parameters //
261  while ((c = getopt_long (argc, argv, shortOpts, longOpts, &longIndex )) != -1)
262  {
263  switch (c)
264  {
265  case 'i':
266  {
267  // test if the wile was correctly entered (if not, getopt could eat
268  // the following parameter)
269  if ((optarg != nullptr) &&
270  ((strlen(optarg) > 0) && (optarg[0] != '-')))
271  {
272  mInputFileName = optarg;
273  }
274  else
275  {
276  printUsage();
278  }
279  break;
280  }
281 
282  case 'o':
283  {
284  // test if the wile was correctly entered (if not, getopt could eat
285  // the following parameter)
286  if ((optarg != nullptr) &&
287  ((strlen(optarg) > 0) && (optarg[0] != '-')))
288  {
289  mOutputFileName = optarg;
290  }
291  else
292  {
293  printUsage();
295  }
296  break;
297  }
298 
299  case 'r':
300  {
301  try
302  {
303  int convertedValue = std::stoi(optarg);
304  if ((convertedValue < 1) || (convertedValue > 100))
305  {
306  throw std::invalid_argument("-r");
307  }
308  mProgressPrintInterval = std::stoll(optarg);
309  }
310  catch (...)
311  {
312  printUsage();
314  }
315  break;
316  }
317 
318  #ifdef _OPENMP
319  case 't':
320  {
321  try
322  {
323  if (std::stoi(optarg) < 1)
324  {
325  throw std::invalid_argument("-t");
326  }
327  mNumberOfThreads = std::stoll(optarg);
328  }
329  catch (...)
330  {
331  printUsage();
333  }
334  break;
335  }
336  #endif
337 
338  case 'c':
339  {
340  try
341  {
342  int covertedValue = std::stoi(optarg);
343  if ((covertedValue < 0) || (covertedValue > 9))
344  {
345  throw std::invalid_argument("-c");
346  }
347  mCompressionLevel = std::stoll(optarg);
348  }
349  catch (...)
350  {
351  printUsage();
353  }
354  break;
355  }
356 
357  case 'h':
358  {
359  printUsage();
360  exit(EXIT_SUCCESS);
361  }
362 
363  case 's':
364  {
365  try
366  {
367  if (std::stoll(optarg) < 1)
368  {
369  throw std::invalid_argument("-s");
370  }
371  mSamplingStartTimeStep = std::stoll(optarg) - 1;
372  }
373  catch (...)
374  {
375  printUsage();
377  }
378  break;
379  }
380 
381  case 1: // benchmark
382  {
383  try
384  {
385  mBenchmarkFlag = true;
386  if (std::stoll(optarg) <= 0)
387  {
388  throw std::invalid_argument("benchmark");
389  }
390  mBenchmarkTimeStepCount = std::stoll(optarg);
391  }
392  catch (...)
393  {
394  printUsage();
396  }
397  break;
398  }
399 
400  case 2: // copy_sensor_mask
401  {
402  mCopySensorMaskFlag = true;
403  break;
404  }
405 
406  case 3: // checkpoint_file
407  {
408  checkpointFlag = true;
409  // test if the wile was correctly entered (if not, getopt could eat
410  // the following parameter)
411  if ((optarg != NULL) &&
412  ((strlen(optarg) > 0) && (optarg[0] != '-')))
413  {
414  mCheckpointFileName = optarg;
415  }
416  else
417  {
418  printUsage();
420  }
421  break;
422  }
423 
424  case 4: // checkpoint_interval
425  {
426  try
427  {
428  checkpointFlag = true;
429  if (std::stoll(optarg) <= 0)
430  {
431  throw std::invalid_argument("checkpoint_interval");
432  }
433  mCheckpointInterval = std::stoll(optarg);
434  }
435  catch (...)
436  {
437  printUsage();
439  }
440  break;
441  }
442 
443  case 5: // verbose
444  {
445  try
446  {
447  int verboseLevel = std::stoi(optarg);
448  if ((verboseLevel < 0) || (verboseLevel > 2))
449  {
450  throw std::invalid_argument("verbose");
451  }
452  Logger::setLevel(static_cast<Logger::LogLevel> (verboseLevel));
453  }
454  catch (...)
455  {
456  printUsage();
458  }
459  break;
460  }
461 
462  case 6: // version
463  {
464  mPrintVersionFlag = true;
465  break;
466  }
467 
468  case 'p':
469  {
470  mStorePressureRawFlag = true;
471  break;
472  }
473 
474  case 10: // p_rms
475  {
476  mStorePressureRmsFlag = true;
477  break;
478  }
479 
480  case 11: // p_max
481  {
482  mStorePressureMaxFlag = true;
483  break;
484  }
485 
486  case 12: // p_min
487  {
488  mStorePressureMinFlag = true;
489  break;
490  }
491 
492  case 13: // p_max_all
493  {
495  break;
496  }
497 
498  case 14: // p_min_all
499  {
501  break;
502  }
503 
504  case 15: // p_final
505  {
507  break;
508  }
509 
510  case 'u':
511  {
512  mStoreVelocityRawFlag = true;
513  break;
514  }
515 
516  case 20: // u_rms
517  {
518  mStoreVelocityRmsFlag = true;
519  break;
520  }
521 
522  case 21: // u_max
523  {
524  mStoreVelocityMaxFlag = true;
525  break;
526  }
527 
528  case 22: // u_min
529  {
530  mStoreVelocityMinFlag = true;
531  break;
532  }
533 
534  case 23: // u_max_all
535  {
537  break;
538  }
539 
540  case 24: // u_min_all
541  {
543  break;
544  }
545 
546  case 25: // u_final
547  {
549  break;
550  }
551 
552  case 26: // u_non_staggered_raw
553  {
555  break;
556  }
557 
558  // unknown parameter or a missing mandatory argument
559  case ':':
560  case '?':
561  {
562  switch (optopt)
563  {
564  case 'i':
565  {
566  printUsage();
568  break;
569  }
570  case 'o':
571  {
572  printUsage();
574  break;
575  }
576 
577  case 'r':
578  {
579  printUsage();
581  break;
582  }
583 
584  case 'c':
585  {
586  printUsage();
588  break;
589  }
590 
591  #ifdef _OPENMP
592  case 't':
593  {
594  printUsage();
596  break;
597  }
598  #endif
599 
600  case 's':
601  {
602  printUsage();
604  break;
605  }
606 
607  case 1: // benchmark
608  {
609  printUsage();
611  break;
612  }
613 
614  case 3: // checkpoint_file
615  {
616  printUsage();
618  break;
619  }
620 
621  case 4: // checkpoint_interval
622  {
623  printUsage();
625  break;
626  }
627 
628  case 5: // verbose
629  {
630  printUsage();
632  break;
633  }
634 
635  default :
636  {
637  printUsage();
639  break;
640  }
641  }
642  }
643 
644  default:
645  {
646  printUsage();
648  break;
649  }
650  }
651  }
652 
653  if (mPrintVersionFlag) return;
654 
655  //-- Post checks --//
656  if (mInputFileName == "")
657  {
658  printUsage();
660  }
661 
662  if (mOutputFileName == "")
663  {
664  printUsage();
666  }
667 
668  if (checkpointFlag)
669  {
670  if (mCheckpointFileName == "")
671  {
672  printUsage();
674  }
675 
676  if (mCheckpointInterval <= 0)
677  {
678  printUsage();
680  }
681  }
682 
683  // set a default flag if necessary
689  {
690  mStorePressureRawFlag = true;
691  }
692 }// end of parseCommandLine
693 //----------------------------------------------------------------------------------------------------------------------
694 
695 //--------------------------------------------------------------------------------------------------------------------//
696 //------------------------------------------------ Protected methods -------------------------------------------------//
697 //--------------------------------------------------------------------------------------------------------------------//
698 
699 /**
700  * Constructor.
701  */
704  #ifdef _OPENMP
705  mNumberOfThreads(omp_get_num_procs()),
706  #else
707  mNumberOfThreads(1),
708  #endif
709 
714  mPrintVersionFlag (false),
715  // output flags
722  mCopySensorMaskFlag(false),
724 {
725 
726 }// end of constructor
727 //----------------------------------------------------------------------------------------------------------------------
bool mStoreVelocityRmsFlag
Store RMS of velocity over the the sensor mask?
ErrorMessage kErrFmtInvalidNumberOfThreads
Command line parameters error message.
static void setLevel(const LogLevel actualLogLevel)
Set the log level.
Definition: Logger.cpp:51
OutputMessage kOutFmtUsagePart2
Usage massage.
static constexpr size_t kDefaultCompressionLevel
Default compression level.
OutputMessage kOutFmtUsagePart1
Usage massage.
bool mCopySensorMaskFlag
Copy sensor mask to the output file.
bool mStorePressureMinAllFlag
Store minimum of pressure over the whole domain?
ErrorMessage kErrFmtPathDelimiters
delimiters for linux paths
Definition: ErrorMessages.h:53
static void log(const LogLevel queryLevel, const std::string &format, Args ... args)
Log desired activity for a given log level, version with string format.
Definition: Logger.h:97
Advanced level of verbosity.
bool mStorePressureMinFlag
Store minimum of pressure over the sensor mask?
size_t mProgressPrintInterval
Progress interval value.
static std::string wordWrapString(const std::string &inputString, const std::string &delimiters, const int indentation=0, const int lineSize=65)
Definition: Logger.cpp:111
std::string mCheckpointFileName
Checkpoint file name.
OutputMessage kOutFmtCheckpointInterval
Output message.
void printUsage()
Print usage of the code.
ErrorMessage kErrFmtNoCompressionLevel
Command line parameters error message.
size_t mCheckpointInterval
Checkpoint interval in seconds.
ErrorMessage kErrFmtNoInputFile
Error message - input file was not specified.
ErrorMessage kErrFmtNoSamplingStartTimeStep
Command line parameters error message.
std::string mInputFileName
Input file name.
bool mStoreVelocityMinAllFlag
Store minimum of velocity over the whole domain?
bool mStorePressureRawFlag
Store raw time-series of pressure over the sensor mask?
bool mStorePressureRmsFlag
Store RMS of pressure over the the sensor mask?
OutputMessage kOutFmtBenchmarkTimeStep
Output message.
bool mStorePressureFinalAllFlag
Store pressure in the final time step over the whole domain?
bool mPrintVersionFlag
Print version of the code and exit.
static void errorAndTerminate(const std::string &errorMessage)
Log an error and terminate the execution.
Definition: Logger.cpp:84
bool mStoreVelocityMaxFlag
Store maximum of velocity over the sensor mask?
CommandLineParameters()
Default constructor - only friend class can create an instance.
ErrorMessage kErrFmtUnknownParameter
Command line parameter error message.
OutputMessage kOutFmtCheckpointFile
Output message.
The header file containing a class responsible for printing out info and error messages (stdout...
OutputMessage kOutFmtOutputFile
Output message.
bool isCheckpointEnabled() const
Is checkpoint enabled?
Full level of verbosity.
std::string mOutputFileName
Output file name.
bool mStoreVelocityNonStaggeredRawFlag
Store un staggered raw time-series of velocity over the sensor mask?
ErrorMessage kErrFmtNoCheckpointFile
Command line parameters error message.
OutputMessage kOutFmtSamplingStartsAt
Output message.
bool mStoreVelocityMinFlag
Store minimum of velocity over the sensor mask?
OutputMessage kOutFmtCompressionLevel
Output message.
void printComandlineParamers()
Print setup commandline parameters.
bool mStorePressureMaxFlag
Store maximum of pressure over the sensor mask?
Basic (default) level of verbosity.
size_t mCompressionLevel
Compression level value for output and checkpoint files.
OutputMessage kOutFmtSamplingFlags
Output message.
OutputMessage kOutFmtInputFile
Output message.
OutputMessage kOutFmtCopySensorMask
Output message.
bool mStoreVelocityMaxAllFlag
Store maximum of velocity over the whole domain?
bool mStorePressureMaxAllFlag
Store maximum of pressure over the whole domain?
size_t mNumberOfThreads
Number of CPU threads value.
bool mStoreVelocityFinalAllFlag
Store velocity in the final time step over the whole domain?
OutputMessage kOutFmtPrintProgressIntrerval
Output message.
ErrorMessage kErrFmtNoVerboseLevel
Command line parameters error message.
OutputMessage kOutFmtSeparator
Output message - separator.
ErrorMessage kErrFmtNoOutputFile
Command line parameters error message.
size_t mBenchmarkTimeStepCount
Number of time steps used to benchmark the code.
The header file containing the command line parameters.
size_t mSamplingStartTimeStep
StartTimeStep value.
OutputMessage kOutFmtUsageThreads
Usage massage.
void parseCommandLine(int argc, char **argv)
Parse commandline parameters.
bool mStoreVelocityRawFlag
Store raw time-series of velocity over the sensor mask?
ErrorMessage kErrFmtNoProgressPrintInterval
Command line parameters error message.
ErrorMessage kErrFmtNoBenchmarkTimeStep
Command line parameters error message.
ErrorMessage kErrFmtNoCheckpointInterval
Command line parameters error message.
ErrorMessage kErrFmtUnknownParameterOrArgument
Command line parameter error message.
bool mBenchmarkFlag
BenchmarkFlag value.
static constexpr size_t kDefaultProgressPrintInterval
Default progress print interval.