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