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