Flow123d  jenkins-Flow123d-linux-release-multijob-282
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 
25 
26 /**
27  * Base virtual class of Flow123D application.
28  *
29  * Contains base methods of application for initialization, run and finalization which is used in derived class.
30  *
31  * Usage:
32  @code
33  class Application : public ApplicationBase {
34  public:
35  Application(int argc, char ** argv); // constructor
36  protected:
37  virtual void run(); // implementation of pure virtual method
38  virtual void after_run(); // overriding of parent method
39  }
40  @endcode
41  *
42  */
44 public:
45 
46  /**
47  * Contains basic structure of application (initialization, run and finalization).
48  * Method is call after constructor and allows to call virtual methods.
49  */
50  void init(int argc, char ** argv);
51 
52  /// Return codes of application
53  static const int exit_success = 0;
54  static const int exit_failure = 1;
55  static const int exit_output = 0; //return code if printout (text, JSON or LaTeX) is run
56 
57  static bool petsc_initialized;
58 
59 protected:
60 
61  /**
62  * Constructor
63  *
64  * Construction is done in init method. We need to call virtual methods during construction.
65  */
66  ApplicationBase(int argc, char ** argv);
67 
68  /// Destructor
69  virtual ~ApplicationBase();
70 
71  /**
72  * Run application.
73  *
74  * Method must be implemented in derived class.
75  */
76  virtual void run() = 0;
77 
78  /**
79  * Read system parameters, open log.
80  */
81  void system_init( MPI_Comm comm, const string &log_filename);
82 
83  /**
84  * Parse command line parameters before Flow123D initialization.
85  *
86  * Method can be override in derived class.
87  */
88  virtual void parse_cmd_line(const int argc, char ** argv) {}
89 
90  /**
91  * Implement printf function for PETSc with support for redirecting.
92  */
93 #ifdef HAVE_PETSC
94  static PetscErrorCode petscvfprintf(FILE *fd, const char format[], va_list Argp);
95 #endif
96 
97  /**
98  * Initialize PETSC.
99  */
100  void petsc_initialize(int argc, char ** argv);
101 
102  /**
103  * Finalize PETSC. If finalization failed return nonzero value.
104  */
105  int petcs_finalize();
106 
107  /**
108  * Execute part of program after run of simulation.
109  *
110  * Method can be override in derived class.
111  */
112  virtual void after_run() {}
113 
114  /**
115  * Log file name argument - passed to system_init; "" means default, "\n" means no logging
116  * TODO: move whole system_init into Application, use singleton for some runtime global options
117  * for the Flow123d library.
118  */
120 
121  /// Optional file name for output of PETSc parameters.
122  /// Has to be set in @p parse_cmd_line()
123  string petsc_redirect_file_="";
124 
125  /// File handler for redirecting PETSc output
126  static FILE *petsc_output_;
127 };
128 
129 #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.