Flow123d  build_with_4.0.3-407d361
application.hh
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 application_base.hh
15  * @brief
16  */
17 
18 #ifndef APPLICATION_BASE_HH_
19 #define APPLICATION_BASE_HH_
20 
21 #include "system/application.hh"
22 //#include <mpi.hh>
23 //#include "system/system.hh"
24 //#include "system/sys_profiler.hh"
25 #include "system/python_loader.hh"
27 #include "coupling/balance.hh"
28 //#include "input/accessors.hh"
29 //#include "input/reader_to_storage.hh"
30 //#include "input/reader_internal_base.hh"
32 //
33 #include <iostream>
34 #include <fstream>
35 #include <regex>
36 #include <boost/program_options/parsers.hpp>
37 #include <boost/program_options/variables_map.hpp>
38 #include <boost/program_options/options_description.hpp>
39 //#include <boost/filesystem.hpp>
40 
41 
42 #include <string>
43 //#include <sstream>
44 #include <mpi.h>
45 //#include "global_defs.h"
46 //#include "system/file_path.hh"
47 
48 #include <stdarg.h> // for va_list
49 #include <stdio.h> // for FILE
50 
51 #include "config.h" // for FLOW123D_HAVE_PETSC
52 #include "petscsys.h" // for PetscErrorCode
53 #include "system/exceptions.hh" // for ExcStream, operator<<, EI, TYPED...
54 
55 #include "input/type_output.hh"
56 #include "input/accessors.hh"
59 
60 
61 //#ifdef FLOW123D_HAVE_PETSC
62 //#include "petsc.h"
63 //#endif
64 
65 using namespace std;
66 
67 // Exception that prints the signal number and name.
68 TYPEDEF_ERR_INFO( EI_Signal, int);
69 TYPEDEF_ERR_INFO( EI_SignalName, string);
70 DECLARE_EXCEPTION( ExcSignal, << "[ Signal " << EI_Signal::val << " (" << EI_SignalName::val << ") received! ]" );
71 
72 
73 
74 
75 /**
76  * Base virtual class of Flow123D application.
77  *
78  * Contains base methods of application for initialization, run and finalization which is used in derived class.
79  *
80  * Usage:
81  @code
82  class Application : public ApplicationBase {
83  public:
84  Application(int argc, char ** argv); // constructor
85  protected:
86  virtual void run(); // implementation of pure virtual method
87  virtual void after_run(); // overriding of parent method
88  }
89  @endcode
90  *
91  */
92 class Application {
93 public:
94  TYPEDEF_ERR_INFO( EI_InputVersionStr, string);
95  DECLARE_EXCEPTION( ExcVersionFormat,
96  << "Wrong format of the version specification: "
97  << EI_InputVersionStr::qval);
98  DECLARE_INPUT_EXCEPTION( ExcUnknownProblem, << "Problem type not implemented.\n" );
99  DECLARE_INPUT_EXCEPTION( ExcNoRunOption, << "No run option should be catched. Seeng this message is an error.\n" );
100 
101  /// Return codes of application
102  static const int exit_success = 0;
103  static const int exit_failure = 1;
104  static const int exit_output = 0; //return code if printout (text, JSON or LaTeX) is run
105 
106  static bool petsc_initialized;
107  static bool permon_initialized;
108 
109 
110 
111  /// Root of the Input::Type tree. Description of whole input structure.
112  static Input::Type::Record & get_input_type();
113 
114 
115  /**
116  * Constructor
117  *
118  * Construction is done in init method. We need to call virtual methods during construction.
119  */
120  Application();
121 
122 
123  /**
124  * Read main input file
125  *
126  * Returns accessor to the root Record.
127  */
128  Input::Record read_input();
129 
130 
131 
132  void init(int argc, char ** argv);
133  /**
134  * Run application.
135  *
136  * Read input and solve problem.
137  */
138  void run();
139 
140 
141  /**
142  * Displays program version and build info.
143  * Pass version information to Profiler.
144  *
145  * TODO: Split these two functionalities.
146  */
147  void display_version();
148 
149 
150  /// Destructor
151  ~Application();
152 
153 
154 protected:
155 
156  /**
157  * Check pause_after_run flag defined in input file.
158  */
159  void after_run();
160 
161 
162 
163  /**
164  * Parse command line parameters.
165  * @param[in] argc command line argument count
166  * @param[in] argv command line arguments
167  */
168  void parse_cmd_line(const int argc, char ** argv);
169 
170 protected:
171 
172 
173 
174  /**
175  * Read system parameters, open log.
176  */
177  void system_init( MPI_Comm comm, const string &log_filename);
178 
179 
180  /**
181  * Implement printf function for PETSc with support for redirecting.
182  */
183 #ifdef FLOW123D_HAVE_PETSC
184  static PetscErrorCode petscvfprintf(FILE *fd, const char format[], va_list Argp);
185 #endif
186 
187  /**
188  * Initialize PETSC.
189  */
190  void petsc_initialize(int argc, char ** argv);
191 
192  /**
193  * Finalize PETSC. If finalization failed return nonzero value.
194  */
195  int petcs_finalize();
196 
197  /**
198  * Initialize PERMON.
199  */
200  void permon_initialize(int argc, char ** argv);
201 
202  /**
203  * Finalize PERMON. If finalization failed return nonzero value.
204  */
205  int permon_finalize();
206 
207 
208  /**
209  * Log file name argument - passed to system_init; "" means default, "\n" means no logging
210  * TODO: move whole system_init into Application, use singleton for some runtime global options
211  * for the Flow123d library.
212  */
214 
215  /// Optional file name for output of PETSc parameters.
216  /// Has to be set in @p parse_cmd_line()
217  string petsc_redirect_file_="";
218 
219  /// File handler for redirecting PETSc output
220  static FILE *petsc_output_;
221 
222  /// Turn off signal handling useful to debug with valgrind.
224 
225 
226  /// Get version of program and other base data from rev_num.h and store them to map
227  //Input::Type::RevNumData get_rev_num_data();
228 
229  /// Main Flow123d problem
231 
232  /// filename of main input file
234 
235  //int passed_argc_;
236  //char ** passed_argv_;
237 
238  /// Description of possible command line arguments.
240 
241  /// If true, we do output of profiling information.
243 
244  /// location of the profiler report file
246 
247  /// If true, preserves output of balance in YAML format.
249 
250  /// root input record
252 
253 };
254 
255 
256 
257 
258 
259 
260 #endif /* APPLICATION_BASE_HH_ */
DECLARE_EXCEPTION(ExcSignal,<< "[ Signal "<< EI_Signal::val<< " ("<< EI_SignalName::val<< ") received! ]")
TYPEDEF_ERR_INFO(EI_Signal, int)
static bool permon_initialized
Definition: application.hh:107
string main_input_filename_
filename of main input file
Definition: application.hh:233
static FILE * petsc_output_
File handler for redirecting PETSc output.
Definition: application.hh:220
DECLARE_EXCEPTION(ExcVersionFormat,<< "Wrong format of the version specification: "<< EI_InputVersionStr::qval)
string program_arguments_desc_
Description of possible command line arguments.
Definition: application.hh:239
string log_filename_
Definition: application.hh:213
DECLARE_INPUT_EXCEPTION(ExcNoRunOption,<< "No run option should be catched. Seeng this message is an error.\n")
bool use_profiler
If true, we do output of profiling information.
Definition: application.hh:242
string profiler_path
location of the profiler report file
Definition: application.hh:245
DECLARE_INPUT_EXCEPTION(ExcUnknownProblem,<< "Problem type not implemented.\n")
Input::Record root_record
root input record
Definition: application.hh:251
HC_ExplicitSequential * problem_
Get version of program and other base data from rev_num.h and store them to map.
Definition: application.hh:230
bool yaml_balance_output_
If true, preserves output of balance in YAML format.
Definition: application.hh:248
static bool petsc_initialized
Definition: application.hh:106
TYPEDEF_ERR_INFO(EI_InputVersionStr, string)
bool signal_handler_off_
Turn off signal handling useful to debug with valgrind.
Definition: application.hh:223
Class for solution of steady or unsteady flow with sequentially coupled explicit transport.
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
Record type proxy class.
Definition: type_record.hh:182
manipulators::Array< T, Delim > format(T const &deduce, Delim delim=", ")
Definition: logger.hh:325
int MPI_Comm
Definition: mpi.h:141