Flow123d  release_2.2.0-33-g759111d
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 #include <boost/exception/info.hpp> // for error_info::~error_info<Tag, T>
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  /// Return codes of application
75  static const int exit_success = 0;
76  static const int exit_failure = 1;
77  static const int exit_output = 0; //return code if printout (text, JSON or LaTeX) is run
78 
79  static bool petsc_initialized;
80 
81 protected:
82 
83  /**
84  * Constructor
85  *
86  * Construction is done in init method. We need to call virtual methods during construction.
87  */
88  ApplicationBase(int argc, char ** argv);
89 
90  /// Destructor
91  virtual ~ApplicationBase();
92 
93  /**
94  * Run application.
95  *
96  * Method must be implemented in derived class.
97  */
98  virtual void run() = 0;
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 can be override in derived class.
109  */
110  virtual void parse_cmd_line(const int argc, char ** argv) {}
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  * Execute part of program after run of simulation.
131  *
132  * Method can be override in derived class.
133  */
134  virtual void after_run() {}
135 
136  /**
137  * Log file name argument - passed to system_init; "" means default, "\n" means no logging
138  * TODO: move whole system_init into Application, use singleton for some runtime global options
139  * for the Flow123d library.
140  */
142 
143  /// Optional file name for output of PETSc parameters.
144  /// Has to be set in @p parse_cmd_line()
145  string petsc_redirect_file_="";
146 
147  /// File handler for redirecting PETSc output
148  static FILE *petsc_output_;
149 
150  /// Turn off signal handling useful to debug with valgrind.
152 };
153 
154 #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.
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! ]")