XGCa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
command_line_inputs.hpp
Go to the documentation of this file.
1 #ifndef COMMAND_LINE_INPUTS_HPP
2 #define COMMAND_LINE_INPUTS_HPP
3 
4 #include <stdexcept>
5 #include <functional>
6 #include <vector>
7 #include <string>
8 
9 template<typename T>
10 static T read_arg_after(int argc, char* argv[], int i, int* ierr);
11 
12 template<>
13 int read_arg_after(int argc, char* argv[], int i, int* ierr){
14  *ierr = 0;
15  if (i + 1 < argc) { // Make sure we aren't at the end of argv!
16  std::string arg = argv[i+1];
17  try {
18  std::size_t pos;
19  int x = std::stoi(arg, &pos);
20  if (pos >= arg.size()) {
21  return x;
22  }
23  } catch(...){
24  //
25  }
26  }
27  *ierr = 1;
28  return -1;
29 }
30 
32  public:
37  bool do_dryrun;
39  bool debug;
40 
41  private:
42  struct Option{
43  std::string name;
44  std::string explanation;
45  Option(std::string name, std::string explanation) : name(name), explanation(explanation) {}
46  };
47  std::vector<Option> options;
48 
50  bool found;
51 
52  bool check(std::string input, std::string name, std::string explanation){
53  options.push_back(Option(name, explanation));
54  if (input==name){
55  found=true;
56  return true;
57  }else{
58  return false;
59  }
60  };
61 
62  void print_options(){
63  std::string msg = "Available options are:\n";
64  for(int j = 0; j < options.size(); j++)
65  msg += "\t" + options[j].name + "\t" + options[j].explanation + "\n";
66  printf("%s", msg.c_str());
67  }
68 
69  void error(std::string msg){
70  printf("ERROR: %s\n", msg.c_str());
71  print_options();
72  exit(1);
73  }
74 
75  public:
76 
77  CommandLineInputs(int argc, char* argv[])
78  : run_particle_test(false),
79  update_particle_test(false),
80  run_moments_test(false),
81  update_moments_test(false),
82  do_dryrun(false),
83  debug(false)
84  {
85  int ierr;
86  first_input = true;
87  // Loop through inputs
88  for (int i = 1; i < argc; ++i) {
89  std::string input = std::string(argv[i]);
90  found = false;
91 
92  if(check(input,"--test","Checks particles against reference data")){
93  run_particle_test=true;
94  }
95  if(check(input,"--update-test","Updates reference data used in particle test")){
97  }
98  if(check(input,"--moments-test","Checks moments against reference data")){
99  run_moments_test=true;
100  }
101  if(check(input,"--update-moments-test","Updates reference data used in moments test")){
102  update_moments_test=true;
103  }
104  if(check(input,"--debug","Activates extra barriered checkpoints and validity checks (not intended for production)")){
105  debug=true;
106  }
107  if(check(input,"--dryrun","Partially runs XGC setup to check for some common input errors")){
108  do_dryrun=true;
109  dryrun_n_mpi_ranks = -1; // default
110  }
111  if(check(input,"-n_ranks","Optional input if using --dryrun: Specify the number of MPI ranks you plan to use")){
112  if(!do_dryrun){
113  error("-n_ranks can only be used with --dryrun");
114  }
115  dryrun_n_mpi_ranks = read_arg_after<int>(argc, argv, i, &ierr);
116  if(ierr!=0) error("Expected integer after -n_ranks");
117  i++;
118  }
119  if(!found){
120  error("Unknown command line argument " + input);
121  }
122  first_input = false;
123  }
124  }
125 };
126 
127 #endif
std::string name
Definition: command_line_inputs.hpp:43
static T read_arg_after(int argc, char *argv[], int i, int *ierr)
Definition: command_line_inputs.hpp:13
bool run_particle_test
Definition: command_line_inputs.hpp:33
bool first_input
Definition: command_line_inputs.hpp:49
bool update_particle_test
Definition: command_line_inputs.hpp:34
Option(std::string name, std::string explanation)
Definition: command_line_inputs.hpp:45
Definition: command_line_inputs.hpp:31
void print_options()
Definition: command_line_inputs.hpp:62
bool debug
Definition: command_line_inputs.hpp:39
CommandLineInputs(int argc, char *argv[])
Definition: command_line_inputs.hpp:77
bool update_moments_test
Definition: command_line_inputs.hpp:36
std::string explanation
Definition: command_line_inputs.hpp:44
Option
Definition: streamed_parallel_for.hpp:13
bool check(std::string input, std::string name, std::string explanation)
Definition: command_line_inputs.hpp:52
void error(std::string msg)
Definition: command_line_inputs.hpp:69
bool do_dryrun
Definition: command_line_inputs.hpp:37
bool run_moments_test
Definition: command_line_inputs.hpp:35
std::vector< Option > options
Definition: command_line_inputs.hpp:47
int dryrun_n_mpi_ranks
Definition: command_line_inputs.hpp:38
bool found
Definition: command_line_inputs.hpp:50
Definition: command_line_inputs.hpp:42