Flow123d  jenkins-Flow123d-windows-release-multijob-285
main.cc
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2007 Technical University of Liberec. All rights reserved.
4  *
5  * Please make a following refer to Flow123d on your project site if you use the program for any purpose,
6  * especially for academic research:
7  * Flow123d, Research Centre: Advanced Remedial Technologies, Technical University of Liberec, Czech Republic
8  *
9  * This program is free software; you can redistribute it and/or modify it under the terms
10  * of the GNU General Public License version 3 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along with this program; if not,
17  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 021110-1307, USA.
18  *
19  *
20  * $Id$
21  * $Revision$
22  * $LastChangedBy$
23  * $LastChangedDate$
24  *
25  * @file main.cc
26  * @brief This file should contain only creation of Application object.
27  *
28  */
29 
30 
31 #include <petsc.h>
32 
33 #include "system/system.hh"
34 #include "system/sys_profiler.hh"
35 #include "system/python_loader.hh"
37 #include "input/input_type.hh"
38 #include "input/type_output.hh"
39 #include "input/accessors.hh"
40 #include "input/json_to_storage.hh"
41 
42 #include <iostream>
43 #include <fstream>
44 #include <boost/program_options/parsers.hpp>
45 #include <boost/program_options/variables_map.hpp>
46 #include <boost/program_options/options_description.hpp>
47 
48 #include "main.h"
49 
50 #include "rev_num.h"
51 
52 /// named version of the program
53 #define _PROGRAM_VERSION_ "0.0.0"
54 
55 #ifndef _PROGRAM_REVISION_
56  #define _PROGRAM_REVISION_ "(unknown revision)"
57 #endif
58 
59 #ifndef _PROGRAM_BRANCH_
60  #define _PROGRAM_BRANCH_ "(unknown branch)"
61 #endif
62 
63 #ifndef _COMPILER_FLAGS_
64  #define _COMPILER_FLAGS_ "(unknown compiler flags)"
65 #endif
66 
67 
68 namespace it = Input::Type;
69 
70 // this should be part of a system class containing all support information
72  = it::Record("Root", "Root record of JSON input for Flow123d.")
74  "Simulation problem to be solved.")
75  .declare_key("pause_after_run", it::Bool(), it::Default("false"),
76  "If true, the program will wait for key press before it terminates.");
77 
78 
79 
80 Application::Application( int argc, char ** argv)
81 : ApplicationBase(argc, argv),
82  main_input_dir_("."),
83  main_input_filename_(""),
84  passed_argc_(0),
85  passed_argv_(0),
86  use_profiler(true)
87 {
88  // initialize python stuff if we have
89  // nonstandard python home (release builds)
90 #ifdef HAVE_PYTHON
91  PythonLoader::initialize(argv[0]);
92 #endif
93 
94 }
95 
96 
97 void Application::split_path(const string& path, string& directory, string& file_name) {
98 
99  size_t delim_pos=path.find_last_of(DIR_DELIMITER);
100  if (delim_pos < string::npos) {
101 
102  // It seems, that there is some path in fname ... separate it
103  directory =path.substr(0,delim_pos);
104  file_name =path.substr(delim_pos+1); // till the end
105  } else {
106  directory = ".";
107  file_name = path;
108  }
109 }
110 
112  // Say Hello
113  // make strings from macros in order to check type
114  string version(_VERSION_NAME_);
115  string revision(_GIT_REVISION_);
116  string branch(_GIT_BRANCH_);
117  string url(_GIT_URL_);
118  string build = string(__DATE__) + ", " + string(__TIME__) + " flags: " + string(_COMPILER_FLAGS_);
119 
120 
121  xprintf(Msg, "This is Flow123d, version %s revision: %s\n", version.c_str(), revision.c_str());
122  xprintf(Msg,
123  "Branch: %s\n"
124  "Build: %s\n"
125  "Fetch URL: %s\n",
126  branch.c_str(), build.c_str() , url.c_str() );
127  Profiler::instance()->set_program_info("Flow123d", version, branch, revision, build);
128 }
129 
130 
131 
133  if (main_input_filename_ == "") {
134  cout << "Usage error: The main input file has to be specified through -s parameter.\n\n";
135  cout << program_arguments_desc_ << "\n";
136  exit( exit_failure );
137  }
138 
139  // read main input file
141  std::ifstream in_stream(fname.c_str());
142  if (! in_stream) {
143  xprintf(UsrErr, "Can not open main input file: '%s'.\n", fname.c_str());
144  }
145  try {
146  Input::JSONToStorage json_reader(in_stream, input_type );
148  } catch (Input::JSONToStorage::ExcInputError &e ) {
149  e << Input::JSONToStorage::EI_File(fname); throw;
150  } catch (Input::JSONToStorage::ExcNotJSONFormat &e) {
151  e << Input::JSONToStorage::EI_File(fname); throw;
152  }
153 
154  return root_record;
155 }
156 
157 
158 
159 
160 void Application::parse_cmd_line(const int argc, char ** argv) {
161  namespace po = boost::program_options;
162 
163 
164  // Declare the supported options.
165  po::options_description desc("Allowed options");
166  desc.add_options()
167  ("help", "produce help message")
168  ("solve,s", po::value< string >(), "Main input file to solve.")
169  ("input_dir,i", po::value< string >()->default_value("input"), "Directory for the ${INPUT} placeholder in the main input file.")
170  ("output_dir,o", po::value< string >()->default_value("output"), "Directory for all produced output files.")
171  ("log,l", po::value< string >()->default_value("flow123"), "Set base name for log files.")
172  ("version", "Display version and build information and exit.")
173  ("no_log", "Turn off logging.")
174  ("no_profiler", "Turn off profiler output.")
175  ("full_doc", "Prints full structure of the main input file.")
176  ("latex_doc", "Prints description of the main input file in Latex format using particular macros.")
177  ("JSON_machine", "Prints full structure of the main input file as a valid CON file.")
178  ("petsc_redirect", po::value<string>(), "Redirect all PETSc stdout and stderr to given file.");
179 
180  ;
181 
182  // parse the command line
183  po::variables_map vm;
184  po::parsed_options parsed = po::basic_command_line_parser<char>(argc, argv).options(desc).allow_unregistered().run();
185  po::store(parsed, vm);
186  po::notify(vm);
187 
188  // get unknown options
189  vector<string> to_pass_further = po::collect_unrecognized(parsed.options, po::include_positional);
190  passed_argc_ = to_pass_further.size();
191  passed_argv_ = new char * [passed_argc_+1];
192 
193  // first copy the program executable in argv[0]
194  int arg_i=0;
195  if (argc > 0) passed_argv_[arg_i++] = xstrcpy( argv[0] );
196 
197  for(int i=0; i < passed_argc_; i++) {
198  passed_argv_[arg_i++] = xstrcpy( to_pass_further[i].c_str() );
199  }
200  passed_argc_ = arg_i;
201 
202  // if there is "help" option
203  if (vm.count("help")) {
204  cout << desc << "\n";
205  exit( exit_output );
206  }
207 
208  if (vm.count("version")) {
209  display_version();
210  exit( exit_output );
211  }
212 
213  // if there is "full_doc" option
214  if (vm.count("full_doc")) {
216  Input::Type::OutputText type_output(&input_type);
217  type_output.set_filter(":Field:.*");
218  cout << type_output;
219  exit( exit_output );
220  }
221 
222  if (vm.count("latex_doc")) {
224  Input::Type::OutputLatex type_output(&input_type);
225  type_output.set_filter("");
226  cout << type_output;
227  exit( exit_output );
228  }
229 
230  if (vm.count("JSON_machine")) {
233  exit( exit_output );
234  }
235 
236  if (vm.count("petsc_redirect")) {
237  this->petsc_redirect_file_ = vm["petsc_redirect"].as<string>();
238  }
239 
240  // if there is "solve" option
241  if (vm.count("solve")) {
242  string input_filename = vm["solve"].as<string>();
244  }
245 
246  // possibly turn off profilling
247  if (vm.count("no_profiler")) use_profiler=false;
248 
249  string input_dir;
250  string output_dir;
251  if (vm.count("input_dir")) {
252  input_dir = vm["input_dir"].as<string>();
253  }
254  if (vm.count("output_dir")) {
255  output_dir = vm["output_dir"].as<string>();
256  }
257 
258  // assumes working directory "."
259  FilePath::set_io_dirs(".", main_input_dir_, input_dir, output_dir );
260 
261  if (vm.count("log")) {
262  this->log_filename_ = vm["log"].as<string>();
263  }
264 
265  if (vm.count("no_log")) {
266  this->log_filename_="//"; // override; do not open log files
267  }
268 
269  ostringstream tmp_stream(program_arguments_desc_);
270  tmp_stream << desc;
271  // TODO: catch specific exceptions and output usage messages
272 }
273 
274 
275 
276 
277 
279 
280  display_version();
281 
282  Input::Record i_rec = read_input();
283 
284 
285  {
286  using namespace Input;
287 
288  // get main input record handle
289 
290  // should flow123d wait for pressing "Enter", when simulation is completed
291  sys_info.pause_after_run = i_rec.val<bool>("pause_after_run");
292  // read record with problem configuration
293  Input::AbstractRecord i_problem = i_rec.val<AbstractRecord>("problem");
294 
295  if (i_problem.type() == HC_ExplicitSequential::input_type ) {
296 
297  HC_ExplicitSequential *problem = new HC_ExplicitSequential(i_problem);
298 
299  // run simulation
300  problem->run_simulation();
301 
302  delete problem;
303  } else {
304  xprintf(UsrErr,"Problem type not implemented.");
305  }
306 
307  }
308 }
309 
310 
311 
312 
315  printf("\nPress <ENTER> for closing the window\n");
316  getchar();
317  }
318 }
319 
320 
321 
322 
325  Profiler::instance()->output(PETSC_COMM_WORLD);
327  }
328 }
329 
330 
331 //=============================================================================
332 
333 /**
334  * FUNCTION "MAIN"
335  */
336 int main(int argc, char **argv) {
337  try {
338  Application app(argc, argv);
339  app.init(argc, argv);
340  } catch (std::exception & e) {
341  std::cerr << e.what();
343  } catch (...) {
344  std::cerr << "Unknown exception" << endl;
346  }
347 
348  // Say Goodbye
350 }
351 
virtual void run()
Definition: main.cc:278
Definition: system.hh:72
virtual void parse_cmd_line(const int argc, char **argv)
Definition: main.cc:160
Class Input::Type::Default specifies default value of keys of a Input::Type::Record.
Definition: type_record.hh:39
Class for declaration of the input of type Bool.
Definition: type_base.hh:332
string main_input_filename_
filename of main input file
Definition: main.h:100
static Profiler * instance()
virtual void after_run()
Definition: main.cc:313
Application(int argc, char **argv)
Application constructor.
Definition: main.cc:80
static Input::Type::AbstractRecord input_type
static Default obligatory()
Definition: type_record.hh:89
Class for create JSON machine readable documentation.
Definition: type_output.hh:442
void set_filter(string regex_filter)
Initializes and allocates regular expression filter regex_filter.
Definition: type_output.cc:49
char * xstrcpy(const char *src)
MAKE BRAND NEW COPY OF STRING.
Definition: system.cc:312
bool use_profiler
If true, we do output of profiling information.
Definition: main.h:109
???
static void uninitialize()
static const int exit_failure
static void lazy_finish()
Finishes all registered lazy types.
Definition: type_base.cc:88
static const int exit_success
Return codes of application.
Accessor to the data with type Type::Record.
Definition: accessors.hh:327
const Ret val(const string &key) const
#define xprintf(...)
Definition: system.hh:100
string main_input_dir_
directory of main input file (used to resolve relative paths of other input files) ...
Definition: main.h:98
void display_version()
Definition: main.cc:111
void split_path(const string &path, string &directory, string &file_name)
Definition: main.cc:97
char ** passed_argv_
Definition: main.h:103
void set_program_info(string program_name, string program_version, string branch, string revision, string build)
static Input::Type::Record input_type
int passed_argc_
Definition: main.h:102
int main(int argc, char **argv)
Definition: main.cc:336
void init(int argc, char **argv)
Accessor to the polymorphic input data of a type given by an AbstracRecord object.
Definition: accessors.hh:448
#define _COMPILER_FLAGS_
Definition: main.cc:64
Definition: system.hh:72
Class for create documentation in Latex format using particular macros.
Definition: type_output.hh:403
static void set_io_dirs(const string working_dir, const string root_input, const string input, const string output)
Definition: file_path.cc:48
static Input::Type::Record input_type
Root of the Input::Type tree. Description of whole input structure.
Definition: main.h:45
static const int exit_output
string program_arguments_desc_
Description of possible command line arguments.
Definition: main.h:106
#define DIR_DELIMITER
Definition: system.hh:56
SystemInfo sys_info
Definition: system.cc:53
Class for create text documentation.
Definition: type_output.hh:307
Record type proxy class.
Definition: type_record.hh:169
Class for solution of steady or unsteady flow with sequentially coupled explicit transport.
static bool is_initialized()
Reader for (slightly) modified JSON files.
virtual ~Application()
Destructor.
Definition: main.cc:323
Input::Record root_record
root input record
Definition: main.h:112
void output(MPI_Comm comm, ostream &os)
int pause_after_run
Definition: system.hh:81
Input::Record read_input()
Definition: main.cc:132
Record & declare_key(const string &key, const KeyType &type, const Default &default_value, const string &description)
Definition: type_record.cc:430