Stride Reference Manual  - generated for commit 9643b11
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
stride.cpp
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2017, 2018, Kuylen E, Willem L, Broeckhove J
14  */
15 
21 #include "GenPopController.h"
22 #include "SimController.h"
23 #include "StanController.h"
24 #include "util/FileSys.h"
25 #include "util/RunConfigManager.h"
26 #include "util/StringUtils.h"
27 #include "util/TimeStamp.h"
28 
29 #include <boost/property_tree/ptree.hpp>
30 #include <tclap/CmdLine.h>
31 #include <iostream>
32 #include <regex>
33 #include <string>
34 #include <vector>
35 
36 using namespace std;
37 using namespace stride;
38 using namespace stride::util;
39 using namespace TCLAP;
40 using namespace boost::property_tree;
41 
43 int main(int argc, char** argv)
44 {
45  int exitStatus = EXIT_SUCCESS;
46 
47  try {
48  // -----------------------------------------------------------------------------------------
49  // Parse command line (parameters displayed in --help in reverse order to order below).
50  // -----------------------------------------------------------------------------------------
51  CmdLine cmd("stride", ' ', "1.0");
52 
53  string sa = "Stochastic Analysis (stan) will run <COUNT> simulations, each with a "
54  "different seed for the random engine. Only applies in case of -e sim. ";
55  ValueArg<unsigned int> stanArg("", "stan", sa, false, 0, "COUNT", cmd);
56 
57  string si = "File are read from the appropriate (config, data) directories of the "
58  "stride install directory. If false, files are read and written to the "
59  "local directory. \nDefaults to true.";
60  SwitchArg installedArg("i", "installed", si, cmd, true);
61 
62  string so = "Override configuration file parameters with values provided here.";
63  MultiArg<string> overrideArg("o", "override", so, false, "<NAME>=<VALUE>", cmd);
64 
65  string sc = "Specifies the run configuration parameters. The format may be "
66  "either -c file=<file> or -c name=<name>. The first is mostly "
67  "used and may be shortened to -c <file>. The second refers to "
68  "built-in configurations specified by their name."
69  "\nDefaults to -c file=run_default.xml";
70  ValueArg<string> configArg("c", "config", sc, false, "run_default.xml", "CONFIGURATION", cmd);
71 
72  vector<string> execs{"clean", "dump", "sim", "genpop"};
73  ValuesConstraint<string> vc(execs);
74  string se = "Execute the corresponding function:"
75  " \n\t clean: cleans configuration and writes it to a new file."
76  " \n\t dump: takes built-in configuration writes it to a file."
77  " \n\t sim: runs the simulator and is the default."
78  " \n\t genpop: generates geo-based population to file (no sim)"
79  "\nDefaults to --exec sim.";
80  ValueArg<string> execArg("e", "exec", se, false, "sim", &vc, cmd);
81 
82  cmd.parse(argc, static_cast<const char* const*>(argv));
83 
84  // -----------------------------------------------------------------------------------------
85  // Get configuration and path with overrides (if any).
86  // -----------------------------------------------------------------------------------------
87  auto config = configArg.getValue();
88  ptree configPt;
89 
90  if (regex_search(config, regex("^name="))) {
91  config = regex_replace(config, regex(string("^name=")), string(""));
92  configPt = RunConfigManager::Create(config);
93  } else {
94  config = regex_replace(config, regex(string("^file=")), string(""));
95  const filesys::path configPath =
96  (installedArg.getValue()) ? FileSys::GetConfigDir() /= config : filesys::path(config);
97  configPt = FileSys::ReadPtreeFile(configPath);
98  }
99 
100  for (const auto& p_assignment : overrideArg.getValue()) {
101  const auto v = util::Tokenize(p_assignment, "=");
102  configPt.put("run." + v[0], v[1]);
103  }
104 
105  // -----------------------------------------------------------------------------------------
106  // If sim / stan / genpop ... fix config and then run simulation in cli ...
107  // -----------------------------------------------------------------------------------------
108  if (execArg.getValue() == "sim" || execArg.getValue() == "genpop") {
109  if (configPt.get<string>("run.output_prefix", "").empty()) {
110  configPt.put("run.output_prefix", TimeStamp().ToTag().append("/"));
111  }
112  if (stanArg.isSet()) {
113  configPt.put("run.stan_count", stanArg.getValue());
114  }
115  configPt.sort();
116 
117  if (execArg.getValue() == "sim") {
118  if (!stanArg.isSet()) {
119  SimController(configPt).Control();
120  } else {
121  StanController(configPt).Control();
122  }
123  } else {
124  GenPopController(configPt).Control();
125  }
126  }
127 
128  // -----------------------------------------------------------------------------------------
129  // If clean/dump ...
130  // -----------------------------------------------------------------------------------------
131  else if (execArg.getValue() == "clean" || execArg.getValue() == "dump") {
132  RunConfigManager::CleanConfigFile(configPt);
133  }
134  } catch (exception& e) {
135  exitStatus = EXIT_FAILURE;
136  cerr << "\nEXCEPION THROWN: " << e.what() << endl;
137  } catch (...) {
138  exitStatus = EXIT_FAILURE;
139  cerr << "\nEXCEPION THROWN: Unknown exception." << endl;
140  }
141  return exitStatus;
142 }
void Control()
Actual run of the simulator.
Controls a geo-population generation run initiated with the command line interface (cli)...
Header for the command line controller.
void Control()
Control the execution of the simulation.
Utilities for the project.
int main(int argc, char **argv)
Main program of the stride simulator.
Definition: stride.cpp:43
Runs a simulation multiple times, with different random engine seeds.
Provides wall-clock time stamp using the time call.
Definition: TimeStamp.h:34
STL namespace.
std::vector< std::string > Tokenize(const std::string &str, const std::string &delimiters)
Tokenize a string (in order of occurence) with the given delimiters.
Definition: StringUtils.h:67
Header for the command line controller.
Header for the St(ochastic)an(alysis)Controller.
Produce run config ptree.
TimeStamp class.
Miscellaneous string utilities.
Interface for install directory queries.
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28
Controls a simulation run initiated with the command line interface (cli).
Definition: SimController.h:47
void Control()
Actual run of the simulator.