Flow123d  JS_before_hm-1575-ga41e096
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 
88 protected:
89 
90  /**
91  * Constructor
92  *
93  * Construction is done in init method. We need to call virtual methods during construction.
94  */
96 
97  /// Destructor
98  virtual ~ApplicationBase();
99 
100  /**
101  * Read system parameters, open log.
102  */
103  void system_init( MPI_Comm comm, const string &log_filename);
104 
105  /**
106  * Parse command line parameters before Flow123D initialization.
107  *
108  * Method must be implemented in derived class.
109  */
110  virtual void parse_cmd_line(const int argc, char ** argv) = 0;
111 
112  /**
113  * Implement printf function for PETSc with support for redirecting.
114  */
115 #ifdef FLOW123D_HAVE_PETSC
116  static PetscErrorCode petscvfprintf(FILE *fd, const char format[], va_list Argp);
117 #endif
118 
119  /**
120  * Initialize PETSC.
121  */
122  void petsc_initialize(int argc, char ** argv);
123 
124  /**
125  * Finalize PETSC. If finalization failed return nonzero value.
126  */
127  int petcs_finalize();
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_ */
int MPI_Comm
Definition: mpi.h:141
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.
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! ]")