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