Flow123d
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 static bool petsc_initialized = false;
26 
27 
28 /**
29  * Base virtual class of Flow123D application.
30  *
31  * Contains base methods of application for initialization, run and finalization which is used in derived class.
32  *
33  * Usage:
34  @code
35  class Application : public ApplicationBase {
36  public:
37  Application(int argc, char ** argv); // constructor
38  protected:
39  virtual void run(); // implementation of pure virtual method
40  virtual void after_run(); // overriding of parent method
41  }
42  @endcode
43  *
44  */
46 public:
47 
48  /**
49  * Contains basic structure of application (initialization, run and finalization).
50  * Method is call after constructor and allows to call virtual methods.
51  */
52  void init(int argc, char ** argv);
53 
54  /// Return codes of application
55  static const int exit_success = 0;
56  static const int exit_failure = 1;
57  static const int exit_output = 0; //return code if printout (text, JSON or LaTeX) is run
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_ */