Flow123d  release_1.8.3-6-gc7eaf42
application_base.hh
Go to the documentation of this file.
1 /*
2  * aplication_base.hh
3  *
4  */
5 
6 #ifndef APPLICATION_BASE_HH_
7 #define APPLICATION_BASE_HH_
8 
9 
10 #include <string>
11 #include <sstream>
12 #include <mpi.h>
13 
14 #include "global_defs.h"
15 #include "system/xio.h"
16 #include "system/file_path.hh"
17 
18 #ifdef HAVE_PETSC
19 #include "petsc.h"
20 #endif
21 
22 using namespace std;
23 
24 // Exception that prints the signal number and name.
25 TYPEDEF_ERR_INFO( EI_Signal, int);
26 TYPEDEF_ERR_INFO( EI_SignalName, string);
27 DECLARE_EXCEPTION( ExcSignal, << "[ Signal " << EI_Signal::val << " (" << EI_SignalName::val << ") received! ]" );
28 
29 
30 /**
31  * Base virtual class of Flow123D application.
32  *
33  * Contains base methods of application for initialization, run and finalization which is used in derived class.
34  *
35  * Usage:
36  @code
37  class Application : public ApplicationBase {
38  public:
39  Application(int argc, char ** argv); // constructor
40  protected:
41  virtual void run(); // implementation of pure virtual method
42  virtual void after_run(); // overriding of parent method
43  }
44  @endcode
45  *
46  */
48 public:
49 
50  /**
51  * Contains basic structure of application (initialization, run and finalization).
52  * Method is call after constructor and allows to call virtual methods.
53  */
54  void init(int argc, char ** argv);
55 
56  /// Return codes of application
57  static const int exit_success = 0;
58  static const int exit_failure = 1;
59  static const int exit_output = 0; //return code if printout (text, JSON or LaTeX) is run
60 
61  static bool petsc_initialized;
62 
63 protected:
64 
65  /**
66  * Constructor
67  *
68  * Construction is done in init method. We need to call virtual methods during construction.
69  */
70  ApplicationBase(int argc, char ** argv);
71 
72  /// Destructor
73  virtual ~ApplicationBase();
74 
75  /**
76  * Run application.
77  *
78  * Method must be implemented in derived class.
79  */
80  virtual void run() = 0;
81 
82  /**
83  * Read system parameters, open log.
84  */
85  void system_init( MPI_Comm comm, const string &log_filename);
86 
87  /**
88  * Parse command line parameters before Flow123D initialization.
89  *
90  * Method can be override in derived class.
91  */
92  virtual void parse_cmd_line(const int argc, char ** argv) {}
93 
94  /**
95  * Implement printf function for PETSc with support for redirecting.
96  */
97 #ifdef HAVE_PETSC
98  static PetscErrorCode petscvfprintf(FILE *fd, const char format[], va_list Argp);
99 #endif
100 
101  /**
102  * Initialize PETSC.
103  */
104  void petsc_initialize(int argc, char ** argv);
105 
106  /**
107  * Finalize PETSC. If finalization failed return nonzero value.
108  */
109  int petcs_finalize();
110 
111  /**
112  * Execute part of program after run of simulation.
113  *
114  * Method can be override in derived class.
115  */
116  virtual void after_run() {}
117 
118  /**
119  * Log file name argument - passed to system_init; "" means default, "\n" means no logging
120  * TODO: move whole system_init into Application, use singleton for some runtime global options
121  * for the Flow123d library.
122  */
124 
125  /// Optional file name for output of PETSc parameters.
126  /// Has to be set in @p parse_cmd_line()
127  string petsc_redirect_file_="";
128 
129  /// File handler for redirecting PETSc output
130  static FILE *petsc_output_;
131 };
132 
133 #endif /* APPLICATION_BASE_HH_ */
virtual void after_run()
int MPI_Comm
Definition: mpi.h:141
virtual void parse_cmd_line(const int argc, char **argv)
static bool petsc_initialized
static FILE * petsc_output_
File handler for redirecting PETSc output.
I/O functions with filename storing, able to track current line in opened file. All standard stdio fu...
Global macros to enhance readability and debugging, general constants.
TYPEDEF_ERR_INFO(EI_Signal, int)
DECLARE_EXCEPTION(ExcSignal,<< "[ Signal "<< EI_Signal::val<< " ("<< EI_SignalName::val<< ") received! ]")