Flow123d  release_3.0.0-1094-g626f1a1
main.cc
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file main.cc
15  * @brief This file should contain only creation of Application object.
16  */
17 
18 
19 #include "system/system.hh"
20 #include "system/sys_profiler.hh"
21 #include "system/python_loader.hh"
23 #include "coupling/balance.hh"
24 #include "input/accessors.hh"
27 
28 #include <iostream>
29 #include <fstream>
30 #include <regex>
31 #include <boost/program_options/parsers.hpp>
32 #include <boost/program_options/variables_map.hpp>
33 #include <boost/program_options/options_description.hpp>
34 #include <boost/filesystem.hpp>
35 #include <thread> // std::this_thread::sleep_for
36 #include <chrono> // std::chrono::seconds
37 
38 
39 #include "main.h"
40 
41 #include "rev_num.h"
42 
43 /// named version of the program
44 //#define _PROGRAM_VERSION_ "0.0.0"
45 
46 //#ifndef _PROGRAM_REVISION_
47 // #define _PROGRAM_REVISION_ "(unknown revision)"
48 //#endif
49 
50 //#ifndef _PROGRAM_BRANCH_
51 // #define _PROGRAM_BRANCH_ "(unknown branch)"
52 //#endif
53 
54 #ifndef FLOW123D_COMPILER_FLAGS_
55  #define FLOW123D_COMPILER_FLAGS_ "(unknown compiler flags)"
56 #endif
57 
58 
59 namespace it = Input::Type;
60 
61 // this should be part of a system class containing all support information
63  static it::Record type = it::Record("Root", "Root record of JSON input for Flow123d.")
64  .declare_key("flow123d_version", it::String(), it::Default::obligatory(),
65  "Version of Flow123d for which the input file was created."
66  "Flow123d only warn about version incompatibility. "
67  "However, external tools may use this information to provide conversion "
68  "of the input file to the structure required by another version of Flow123d.")
70  "Simulation problem to be solved.")
71  .declare_key("pause_after_run", it::Bool(), it::Default("false"),
72  "If true, the program will wait for key press before it terminates.")
73  .close();
74 
75  return type;
76 }
77 
78 
79 
80 Application::Application(const std::string &python_path)
81 : ApplicationBase(),
82  problem_(nullptr),
84  //passed_argc_(0),
85  //passed_argv_(0),
86  use_profiler(true),
88 {
89  // initialize python stuff if we have
90  // nonstandard python home (release builds)
91 #ifdef FLOW123D_HAVE_PYTHON
92  PythonLoader::initialize(python_path);
93 #endif
94 
95 }
96 
97 
99  Input::Type::RevNumData rev_num_data;
100 
101  rev_num_data.version = string(FLOW123D_VERSION_NAME_);
102  rev_num_data.revision = string(FLOW123D_GIT_REVISION_);
103  rev_num_data.branch = string(FLOW123D_GIT_BRANCH_);
104  rev_num_data.url = string(FLOW123D_GIT_URL_);
105 
106  return rev_num_data;
107 }
108 
109 
111  // Say Hello
112  // make strings from macros in order to check type
113  Input::Type::RevNumData rev_num_data = this->get_rev_num_data();
114  string build = string(__DATE__) + ", " + string(__TIME__)
115  + " flags: " + string(FLOW123D_COMPILER_FLAGS_);
116 
117 
118  MessageOut().fmt("This is Flow123d, version {} commit: {}\n",
119  rev_num_data.version, rev_num_data.revision);
120  MessageOut().fmt("Branch: {}\nBuild: {}\nFetch URL: {}\n",
121  rev_num_data.branch, build, rev_num_data.url );
122  Profiler::instance()->set_program_info("Flow123d",
123  rev_num_data.version, rev_num_data.branch, rev_num_data.revision, build);
124 }
125 
126 
127 
129  if (main_input_filename_ == "") {
130  cout << "Usage error: The main input file has to be specified through -s parameter.\n\n";
131  cout << program_arguments_desc_ << "\n";
132  exit( exit_failure );
133  }
134 
135  // read main input file
136  FilePath fpath(main_input_filename_, FilePath::FileType::input_file);
137  try {
138  Input::ReaderToStorage json_reader(fpath, get_input_type() );
140  } catch (Input::ReaderInternalBase::ExcInputError &e ) {
141  e << Input::ReaderInternalBase::EI_File(fpath); throw;
142  } catch (Input::ReaderInternalBase::ExcNotJSONFormat &e) {
143  e << Input::ReaderInternalBase::EI_File(fpath); throw;
144  }
145  return root_record;
146 }
147 
148 
149 
150 void Application::parse_cmd_line(const int argc, char ** argv) {
151  namespace po = boost::program_options;
152 
153 
154  // Declare the supported options.
155  po::options_description desc("Allowed options");
156  desc.add_options()
157  ("help", "produce help message")
158  ("solve,s", po::value< string >(), "Main input file to solve.")
159  ("input_dir,i", po::value< string >()->default_value("input"), "Directory for the $INPUT_DIR$ placeholder in the main input file.")
160  ("output_dir,o", po::value< string >()->default_value("output"), "Directory for all produced output files.")
161  ("log,l", po::value< string >()->default_value("flow123"), "Set base name for log files.")
162  ("version", "Display version and build information and exit.")
163  ("no_log", "Turn off logging.")
164  ("no_signal_handler", "Turn off signal handling. Useful for debugging with valgrind.")
165  ("no_profiler", "Turn off profiler output.")
166  ("input_format", po::value< string >(), "Writes full structure of the main input file into given file.")
167  ("petsc_redirect", po::value<string>(), "Redirect all PETSc stdout and stderr to given file.")
168  ("yaml_balance", "Redirect balance output to YAML format too (simultaneously with the selected balance output format).");
169 
170  ;
171 
172  // Can not use positional arguments together with PETSC options.
173  // Use our own solution trying to use the first unrecognized option as the main input file.
174 
175  // parse the command line
176  po::variables_map vm;
177  auto parser = po::basic_command_line_parser<char>(argc, argv)
178  .options(desc)
179  .allow_unregistered();
180  po::parsed_options parsed = parser.run();
181  po::store(parsed, vm);
182  po::notify(vm);
183 
184  // get unknown options
185  vector<string> to_pass_further = po::collect_unrecognized(parsed.options, po::include_positional);
186 
187 
188  /*
189  passed_argc_ = to_pass_further.size();
190  passed_argv_ = new char * [passed_argc_+1];
191 
192  // first copy the program executable in argv[0]
193  int arg_i=0;
194  if (argc > 0) passed_argv_[arg_i++] = xstrcpy( argv[0] );
195 
196  for(int i=0; i < passed_argc_; i++) {
197  passed_argv_[arg_i++] = xstrcpy( to_pass_further[i].c_str() );
198  }
199  passed_argc_ = arg_i;
200  */
201 
202  // possibly turn off profilling
203  if (vm.count("no_profiler")) use_profiler=false;
204 
205  // if there is "help" option
206  if (vm.count("help")) {
207  display_version();
208  cout << endl;
209  cout << "Usage:" << endl;
210  cout << " flow123d -s <main_input>.yaml <other options> <PETSC options>" << endl;
211  cout << " flow123d <main_input>.yaml <other options> <PETSC options>" << endl;
212  cout << desc << "\n";
213  exit( exit_output );
214  }
215 
216  if (vm.count("version")) {
217  display_version();
218  exit( exit_output );
219  }
220 
221  // if there is "input_format" option
222  if (vm.count("input_format")) {
223  // write ist to json file
224  ofstream json_stream;
225  FilePath(vm["input_format"].as<string>(), FilePath::output_file).open_stream(json_stream);
226  // create the root Record
227  it::Record root_type = get_input_type();
228  root_type.finish();
230  json_stream << Input::Type::OutputJSONMachine( root_type, this->get_rev_num_data() );
231  json_stream.close();
232  exit( exit_output );
233  }
234 
235  if (vm.count("petsc_redirect")) {
236  this->petsc_redirect_file_ = vm["petsc_redirect"].as<string>();
237  }
238 
239  if (vm.count("no_signal_handler")) {
240  this->signal_handler_off_ = true;
241  }
242 
243  // if there is "solve" option
244  string input_filename = "";
245 
246  // check for positional main input file
247  if (to_pass_further.size()) {
248  string file_candidate = to_pass_further[0];
249  if (file_candidate[0] != '-') {
250  // pop the first option
251  input_filename = file_candidate;
252  to_pass_further.erase(to_pass_further.begin());
253  }
254  }
255 
256  if (vm.count("solve")) {
257  input_filename = vm["solve"].as<string>();
258  }
259 
260  if (input_filename == "")
261  THROW(ExcMessage() << EI_Message("Main input file not specified (option -s)."));
262 
263  // preserves output of balance in YAML format
264  if (vm.count("yaml_balance")) Balance::set_yaml_output();
265 
266  string input_dir;
267  string output_dir;
268  if (vm.count("input_dir")) {
269  input_dir = vm["input_dir"].as<string>();
270  }
271  if (vm.count("output_dir")) {
272  output_dir = vm["output_dir"].as<string>();
273  }
274 
275  // assumes working directory "."
276  try {
277  main_input_filename_ = FilePath::set_dirs_from_input(input_filename, input_dir, output_dir );
278  } catch (FilePath::ExcMkdirFail &e) {
279  use_profiler = false; // avoid profiler output
280  throw e;
281  }
282 
283  if (vm.count("log")) {
284  this->log_filename_ = vm["log"].as<string>();
285  }
286 
287  if (vm.count("no_log")) {
288  this->log_filename_="//"; // override; do not open log files
289  }
290 
291  ostringstream tmp_stream(program_arguments_desc_);
292  tmp_stream << desc;
293  // TODO: catch specific exceptions and output usage messages
294 }
295 
296 
297 
298 
299 
301  START_TIMER("Application::run");
302  display_version();
303 
304  START_TIMER("Read Input");
305  // get main input record handle
306  Input::Record i_rec = read_input();
307  END_TIMER("Read Input");
308 
309  {
310  using namespace Input;
311  // check input file version against the version of executable
312  std::regex version_re("([^.]*)[.]([^.]*)[.]([^.]*)");
313  std::smatch match;
314  std::string version(FLOW123D_VERSION_NAME_);
315  vector<string> ver_fields(3);
316  if ( std::regex_match(version, match, version_re) ) {
317  ver_fields[0]=match[1];
318  ver_fields[1]=match[2];
319  ver_fields[2]=match[3];
320  } else {
321  OLD_ASSERT(1, "Bad Flow123d version format: %s\n", version.c_str() );
322  }
323 
324  std::string input_version = i_rec.val<string>("flow123d_version");
325  vector<string> iver_fields(3);
326  if ( std::regex_match(input_version, match, version_re) ) {
327  iver_fields[0]=match[1];
328  iver_fields[1]=match[2];
329  iver_fields[2]=match[3];
330  } else {
331  THROW( ExcVersionFormat() << EI_InputVersionStr(input_version) );
332  }
333 
334  if ( iver_fields[0] != ver_fields[0] || iver_fields[1] > ver_fields[1] ) {
335  WarningOut().fmt("Input file with version: '{}' is no compatible with the program version: '{}' \n",
336  input_version, version);
337  }
338 
339  // should flow123d wait for pressing "Enter", when simulation is completed
340  sys_info.pause_after_run = i_rec.val<bool>("pause_after_run");
341  // read record with problem configuration
342  Input::AbstractRecord i_problem = i_rec.val<AbstractRecord>("problem");
343 
344  if (i_problem.type() == HC_ExplicitSequential::get_input_type() ) {
345 
346  problem_ = new HC_ExplicitSequential(i_problem);
347 
348  // run simulation
350  } else {
351  xprintf(UsrErr,"Problem type not implemented.");
352  }
353 
354  }
355 
356  this->after_run();
357 }
358 
359 
360 
361 
364  printf("\nPress <ENTER> for closing the window\n");
365  getchar();
366  }
367 }
368 
369 
370 
371 
373  // Test if all processes are in the exception.
374  MPI_Request request;
375  MPI_Ibarrier(MPI_COMM_WORLD, &request);
376  std::this_thread::sleep_for(std::chrono::microseconds(10));
377  int done;
378  MPI_Status status;
379  MPI_Test(&request, &done, &status);
380  if (! done) {
381  // Kill all if we can not synchronize.
383  }
384  // Peacefull end.
385 }
386 
387 
388 
389 
391  if (problem_) delete problem_;
392 
393  if (use_profiler) {
394  if (petsc_initialized) {
395  // log profiler data to this stream
396  Profiler::instance()->output (PETSC_COMM_WORLD);
397  } else {
399  }
400 
401  // call python script which transforms json file at given location
402  // Profiler::instance()->transform_profiler_data (".csv", "CSVFormatter");
403  Profiler::instance()->transform_profiler_data (".txt", "SimpleTableFormatter");
404 
405  // finally uninitialize
407  }
408 }
409 
410 
411 //=============================================================================
412 
413 /**
414  * FUNCTION "MAIN"
415  */
416 int main(int argc, char **argv) {
417  Application app(argv[0]);
418  try {
419  app.init(argc, argv);
420  app.run();
421  } catch (std::exception & e) {
422  _LOG( Logger::MsgType::error ).every_proc() << e.what();
423  app.terminate();
425  } catch (...) {
426  _LOG( Logger::MsgType::error ).every_proc() << "Unknown exception" << endl;
427  app.terminate();
429  }
430 
431  // Say Goodbye
433 }
#define MPI_Test(request, flag, status)
Definition: mpi.h:366
static Input::Type::Abstract & get_input_type()
Input::Type::RevNumData get_rev_num_data()
Get version of program and other base data from rev_num.h and store them to map.
Definition: main.cc:98
virtual void parse_cmd_line(const int argc, char **argv)
Definition: main.cc:150
Reader for (slightly) modified input files.
Class Input::Type::Default specifies default value of keys of a Input::Type::Record.
Definition: type_record.hh:61
Class for declaration of the input of type Bool.
Definition: type_base.hh:459
#define MessageOut()
Macro defining &#39;message&#39; record of log.
Definition: logger.hh:243
Abstract linear system class.
Definition: balance.hh:35
string main_input_filename_
filename of main input file
Definition: main.h:102
void after_run()
Definition: main.cc:362
static bool petsc_initialized
static Default obligatory()
The factory function to make an empty default value which is obligatory.
Definition: type_record.hh:110
Class for create JSON machine readable documentation.
Definition: type_output.hh:252
Application(const std::string &python_path)
Application constructor.
Definition: main.cc:80
void * MPI_Request
Definition: mpi.h:142
std::string branch
Actual branch of application.
Definition: type_output.hh:47
bool use_profiler
If true, we do output of profiling information.
Definition: main.h:111
static string set_dirs_from_input(const string main_yaml, const string input, const string output)
Method for set input and output directories.
Definition: file_path.cc:137
Record & close() const
Close the Record for further declarations of keys.
Definition: type_record.cc:303
static void set_yaml_output()
Set global variable to output balance files into YAML format (in addition to the table format)...
Definition: balance.cc:61
HC_ExplicitSequential * problem_
Main Flow123d problem.
Definition: main.h:99
#define OLD_ASSERT(...)
Definition: global_defs.h:131
void open_stream(Stream &stream) const
Definition: file_path.cc:211
Stores version of program and other base data of application.
Definition: type_output.hh:44
static void uninitialize()
static const int exit_failure
void terminate()
Definition: main.cc:372
static const int exit_success
Return codes of application.
T get_root_interface() const
Returns the root accessor.
#define _LOG(type)
Internal macro defining universal record of log.
Definition: logger.hh:240
Accessor to the data with type Type::Record.
Definition: accessors.hh:292
const Ret val(const string &key) const
#define xprintf(...)
Definition: system.hh:92
bool yaml_balance_output_
If true, preserves output of balance in YAML format.
Definition: main.h:114
#define START_TIMER(tag)
Starts a timer with specified tag.
Record & declare_key(const string &key, std::shared_ptr< TypeBase > type, const Default &default_value, const string &description, TypeBase::attribute_map key_attributes=TypeBase::attribute_map())
Declares a new key of the Record.
Definition: type_record.cc:501
void display_version()
Definition: main.cc:110
void set_program_info(string program_name, string program_version, string branch, string revision, string build)
bool signal_handler_off_
Turn off signal handling useful to debug with valgrind.
static Input::Type::Record & get_input_type()
Root of the Input::Type tree. Description of whole input structure.
Definition: main.cc:62
int main(int argc, char **argv)
Definition: main.cc:416
void init(int argc, char **argv)
Accessor to the polymorphic input data of a type given by an AbstracRecord object.
Definition: accessors.hh:459
void transform_profiler_data(const string &output_file_suffix, const string &formatter)
Dedicated class for storing path to input and output files.
Definition: file_path.hh:54
Definition: system.hh:64
std::string revision
Actual revision of application.
Definition: type_output.hh:46
FinishStatus finish(FinishStatus finish_type=FinishStatus::regular_) override
Finish declaration of the Record type.
Definition: type_record.cc:242
static const int exit_output
#define MPI_Abort
Definition: mpi.h:222
static Profiler * instance()
string program_arguments_desc_
Description of possible command line arguments.
Definition: main.h:108
SystemInfo sys_info
Definition: system.cc:41
#define WarningOut()
Macro defining &#39;warning&#39; record of log.
Definition: logger.hh:246
#define MPI_COMM_WORLD
Definition: mpi.h:123
#define END_TIMER(tag)
Ends a timer with specified tag.
static const Input::Type::Record & get_input_type()
void run() override
Definition: main.cc:300
static void delete_unfinished_types()
Finishes and marks all types registered in type repositories and unused in IST.
Definition: type_base.cc:108
Record type proxy class.
Definition: type_record.hh:182
std::string version
Actual version of application.
Definition: type_output.hh:45
Class for solution of steady or unsteady flow with sequentially coupled explicit transport.
virtual ~Application()
Destructor.
Definition: main.cc:390
#define FLOW123D_COMPILER_FLAGS_
named version of the program
Definition: main.cc:55
Input::Record root_record
root input record
Definition: main.h:117
Class for declaration of the input data that are in string format.
Definition: type_base.hh:589
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
void output(MPI_Comm comm, ostream &os)
int pause_after_run
Definition: system.hh:73
Input::Record read_input()
Definition: main.cc:128
void printf(BasicWriter< Char > &w, BasicCStringRef< Char > format, ArgList args)
Definition: printf.h:444
std::string url
Url of application.
Definition: type_output.hh:48