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 
19 using namespace std;
20 
21 
22 static bool petsc_initialized = false;
23 
24 
25 /**
26  * Base virtual class of Flow123D application.
27  *
28  * Contains base methods of application for initialization, run and finalization which is used in derived class.
29  *
30  * Usage:
31  @code
32  class Application : public ApplicationBase {
33  public:
34  Application(int argc, char ** argv); // constructor
35  protected:
36  virtual void run(); // implementation of pure virtual method
37  virtual void after_run(); // overriding of parent method
38  }
39  @endcode
40  *
41  */
43 public:
44 
45  /**
46  * Contains basic structure of application (initialization, run and finalization).
47  * Method is call after constructor and allows to call virtual methods.
48  */
49  void init(int argc, char ** argv);
50 
51  /// Return codes of application
52  static const int exit_success = 0;
53  static const int exit_failure = 1;
54  static const int exit_output = 0; //return code if printout (text, JSON or LaTeX) is run
55 
56 protected:
57 
58  /**
59  * Constructor
60  *
61  * Construction is done in init method. We need to call virtual methods during construction.
62  */
63  ApplicationBase(int argc, char ** argv);
64 
65  /// Destructor
66  virtual ~ApplicationBase();
67 
68  /**
69  * Run application.
70  *
71  * Method must be implemented in derived class.
72  */
73  virtual void run() = 0;
74 
75  /**
76  * Read system parameters, open log.
77  */
78  void system_init( MPI_Comm comm, const string &log_filename );
79 
80  /**
81  * Parse command line parameters before Flow123D initialization.
82  *
83  * Method can be override in derived class.
84  */
85  virtual void parse_cmd_line(const int argc, char ** argv) {}
86 
87  /**
88  * Initialize PETSC.
89  */
90  void petsc_initialize(int argc, char ** argv);
91 
92  /**
93  * Finalize PETSC. If finalization failed return nonzero value.
94  */
95  int petcs_finalize();
96 
97  /**
98  * Execute part of program after run of simulation.
99  *
100  * Method can be override in derived class.
101  */
102  virtual void after_run() {}
103 
104  /**
105  * Log file name argument - passed to system_init; "" menas default, "\n" means no logging
106  * TODO: move whole system_init into Application, use singleton for some runtime global options
107  * for the Flow123d library.
108  */
110 };
111 
112 #endif /* APPLICATION_BASE_HH_ */