SSAGES  0.1
A MetaDynamics Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Groups Pages
main.cpp
1 
22 #include <getopt.h>
23 #include <string>
24 #include <sstream>
25 
26 #include "config.h"
27 #include <mxx/comm.hpp>
28 #include <mxx/env.hpp>
29 #include "Driver.h"
30 #include "JSON/JSONLoader.h"
31 #include "Drivers/DriverException.h"
32 
33 using namespace SSAGES;
34 using namespace Json;
35 
36 int main(int argc, char* argv[])
37 {
38  // Let MPI collect command line arguments pertaining to MPI.
39  mxx::env env(argc, argv);
40  mxx::env::set_exception_on_error();
41  auto world = mxx::comm();
42 
43  std::stringstream helpStream;
44  helpStream
45  << "Welcome to SSAGES - Suite for Advanced Generalized Ensemble Simulations\n"
46  << "\n"
47  << argv[0] << " [-h | --help] [ --version ] InputFile.json\n"
48  << "\n"
49  << "Required parameters:\n"
50  << "InputFile.json - Input file specifying the generalized ensemble method and\n"
51  << " all required input parameters in the JSON format. For more\n"
52  << " information on the input file, confer the user manual.\n"
53  << "\n"
54  << "Optional parameters:\n"
55  << " -h | --help - Print this help message\n"
56  << " --version - Print SSAGES version and quit\n";
57 
58  std::stringstream versionStream;
59  versionStream << "SSAGES version " << SSAGES_VERSION_MAJOR << "."
60  << SSAGES_VERSION_MINOR << "."
61  << SSAGES_VERSION_TINY << "\n";
62 
63  // Define short options, e.g. -h
64  const char *OPT_STRING = "h!";
65 
66  // Define long options, e.g. --help
67  static struct option longOpts[] = {
68  { "help", no_argument, NULL, 'h' },
69  { "version", no_argument, NULL, '!' },
70  };
71 
72  // Parse command line arguments
73  int optionindex = 1;
74  int opt = getopt_long(argc, argv, OPT_STRING, longOpts, &optionindex);
75 
76  while (opt != -1) {
77  switch (opt) {
78  case 'h' :
79  std::cout << helpStream.str();
80  return 0;
81  break;
82  case '!' :
83  std::cout << versionStream.str();
84  return 0;
85  break;
86  default:
87  // getopt_long has already produced an error message
88  return 1;
89  }
90  opt = getopt_long(argc, argv, OPT_STRING, longOpts, &optionindex);
91  }
92 
93  // Check that there is still one command line option left: The JSON input file
94  if (optionindex >= argc) {
95  std::cerr << "Error! No JSON input file given. Use " << argv[0] << " --help "
96  << "for information on how to call SSAGES.\n";
97  return 2;
98  }
99 
100  try{
101  // Read in input file
102  std::string input = argv[optionindex];
103  auto json = JSONLoader().LoadFile(input, world);
104 
105  // Build driver.
106  auto* driver = Driver::Build(json, world);
107  driver->Run();
108  } catch(BuildException& e) {
109  if(world.rank() == 0)
111  MPI_Abort(world, -1);
112  } catch(std::exception& e) {
113  if(world.rank() == 0)
114  DumpErrorsToConsole({e.what()}, 30);
115  MPI_Abort(world, -1);
116  } catch(int& k) {
117  std::string err = strerror(k);
118  if(world.rank() == 0)
119  DumpErrorsToConsole({"File IO error: " + err}, 30);
120  MPI_Abort(world, -1);
121  }
122 
123  return 0;
124 }
Exception to be thrown when building the Driver fails.
std::vector< std::string > GetErrors()
Get specific error message.
int DumpErrorsToConsole(const std::vector< std::string > &msgs, int notw)
Print a list of errors.
Json::Value LoadFile(const std::string &filename, const mxx::comm &world)
Load file and return JSON tree.
Definition: JSONLoader.h:68
Class for loading JSON content from files.
Definition: JSONLoader.h:41