LifeV
LinearSolver.hpp
Go to the documentation of this file.
1 //@HEADER
2 /*
3 *******************************************************************************
4 
5  Copyright (C) 2004, 2005, 2007 EPFL, Politecnico di Milano, INRIA
6  Copyright (C) 2010 EPFL, Politecnico di Milano, Emory University
7 
8  This file is part of LifeV.
9 
10  LifeV is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  LifeV is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with LifeV. If not, see <http://www.gnu.org/licenses/>.
22 
23 *******************************************************************************
24 */
25 //@HEADER
26 
27 /*!
28  @file
29  @brief LinearSolver
30 
31  @author Gwenol Grandperrin <gwenol.grandperrin@epfl.ch>
32  @maintainer Gwenol Grandperrin <gwenol.grandperrin@epfl.ch>
33 
34  @date 03-08-2011
35  */
36 
37 #ifndef _LINEARSOLVER_HPP
38 #define _LINEARSOLVER_HPP 1
39 
40 #include <iomanip>
41 
42 
43 
44 #include <Epetra_ConfigDefs.h>
45 #ifdef EPETRA_MPI
46 #include <Epetra_MpiComm.h>
47 #else
48 #include <Epetra_SerialComm.h>
49 #endif
50 
51 #include <BelosConfigDefs.hpp>
52 #include <BelosLinearProblem.hpp>
53 #include <BelosEpetraAdapter.hpp>
54 #include <BelosEpetraOperator.h>
55 #include <BelosOutputManager.hpp>
56 #include <BelosMVOPTester.hpp>
57 #include <BelosBlockCGSolMgr.hpp>
58 #include <BelosBlockGmresSolMgr.hpp>
59 //#include <BelosGCRODRSolMgr.hpp>
60 #include <BelosGmresPolySolMgr.hpp>
61 #include <BelosPCPGSolMgr.hpp>
62 #include <BelosPseudoBlockCGSolMgr.hpp>
63 #include <BelosPseudoBlockGmresSolMgr.hpp>
64 #include <BelosRCGSolMgr.hpp>
65 #include <BelosTFQMRSolMgr.hpp>
66 
67 #include <Teuchos_ParameterList.hpp>
68 
69 
70 #include <lifev/core/LifeV.hpp>
71 
72 #include <lifev/core/util/LifeDebug.hpp>
73 #include <lifev/core/util/LifeChrono.hpp>
74 #include <lifev/core/util/Displayer.hpp>
75 #include <lifev/core/array/VectorEpetra.hpp>
76 #include <lifev/core/array/MatrixEpetra.hpp>
77 #include <lifev/core/algorithm/Preconditioner.hpp>
78 #include <lifev/core/filter/GetPot.hpp>
79 #include <lifev/core/operator/SolverOperator.hpp>
80 #include <lifev/core/operator/BelosOperator.hpp>
81 #include <lifev/core/operator/AztecooOperator.hpp>
82 
83 namespace LifeV
84 {
85 
86 //! LinearSolver - Class to wrap linear solver
87 /*!
88  By default the solver is block gmres.
89 
90  @author Gwenol Grandperrin <gwenol.grandperrin@epfl.ch>
91 */
92 class LinearSolver
93 {
94 public:
95 
96  //! @name Public Types
97  //@{
98 
99  typedef Real value_Type;
100 
101  typedef LinearSolver solver_Type;
102  typedef Epetra_MultiVector multiVector_Type;
103  typedef std::shared_ptr<multiVector_Type> multiVectorPtr_Type;
104  typedef Epetra_Operator operator_Type;
105  typedef std::shared_ptr<operator_Type> operatorPtr_Type;
106  typedef Operators::SolverOperator SolverOperator_Type;
107  typedef std::shared_ptr< SolverOperator_Type > SolverOperatorPtr_Type;
108  typedef MatrixEpetra<Real> matrix_Type;
109  typedef std::shared_ptr<matrix_Type> matrixPtr_Type;
110  typedef VectorEpetra vector_Type;
111  typedef std::shared_ptr<VectorEpetra> vectorPtr_Type;
112  typedef Preconditioner preconditioner_Type;
113  typedef std::shared_ptr<preconditioner_Type> preconditionerPtr_Type;
114  typedef Teuchos::ParameterList parameterList_Type;
115  typedef Teuchos::RCP< parameterList_Type > parameterListPtr_Type;
116 
117  enum SolverType { UndefinedSolver, Belos, AztecOO };
118 
119  //@}
120 
121  //! @name Constructors & Destructor
122  //@{
123 
124  //! Empty constructor
125  LinearSolver();
126 
127  //! Constructor
128  /*!
129  @param commPtr Communicator
130  */
131  LinearSolver ( const std::shared_ptr<Epetra_Comm> commPtr );
132 
133  //! Destructor
134  ~LinearSolver();
135 
136  //@}
137 
138  //! @name Methods
139  //@{
140 
141  //! Solves the system and returns the number of iterations.
142  /*!
143  The Matrix has already been passed by the method
144  setMatrix or setOperator
145 
146  The preconditioner is build starting from the matrix baseMatrixForPreconditioner
147  if it is set otherwise from the problem matrix.
148  @param solutionPtr Vector to store the solution
149  @return Number of iterations, M_maxIter+1 if solve failed.
150  */
151  Int solve ( vectorPtr_Type solutionPtr );
152 
153  //! Compute the residual
154  /*!
155  @param solutionPtr Shared pointer on the solution of the system
156  The method returns -1 if an error occurs
157  */
158  Real computeResidual ( vectorPtr_Type solutionPtr );
159 
160  //! return the solver status
161  std::string printStatus();
162 
163  //! Setup the preconditioner from a GetPot file
164  /*!
165  @param dataFile GetPot object which contains the data about the preconditioner
166  @param section Section the GetPot structure where to find the informations about the preconditioner
167  */
168  void setPreconditionerFromGetPot ( const GetPot& dataFile, const std::string& section );
169 
170  //! Builds the preconditioner starting from the matrix "baseMatrixForPreconditioner"
171  /*!
172  The preconditioner is build starting from the matrix baseMatrixForPreconditioner
173  by the preconditioner object passed in by the method setPreconditioner
174  @param baseMatrixForPreconditioner Base matrix for the preconditioner construction
175  */
176  void buildPreconditioner();
177 
178  //! Reset the stored preconditioner
179  /*!
180  Note: This method only affects the LifeV::Preconditioner (i.e. not the Epetra_Operators
181  used as preconditioner
182  */
183  void resetPreconditioner();
184 
185  //! Return true if preconditioner has been setted
186  bool isPreconditionerSet() const;
187 
188  //! Reset the status for the state of convergence and loss of accuracy
189  void resetStatus();
190 
191  //! Print informations about the solver
192  void showMe ( std::ostream& output = std::cout ) const;
193 
194  //! Setup the solver operator to be used
195  void setupSolverOperator();
196 
197  //@}
198 
199  //! @name Set Method
200  //@{
201 
202  //! Set the solver which should be used
203  /*!
204  @param solverOperatorType Type of solver manager
205  The solver type can be chosen from one of the following:
206  Aztecoo, Belos
207  */
208  void setSolverType ( const SolverType& solverType );
209 
210  //! Method to set communicator for Displayer (for empty constructor)
211  /*!
212  @param commPtr Communicator for the displayer
213  */
214  void setCommunicator ( const std::shared_ptr<Epetra_Comm> commPtr );
215 
216  //! Method to set matrix from MatrixEpetra
217  /*!
218  @param matrixPtr Matrix of the system
219  */
220  void setOperator ( matrixPtr_Type matrixPtr );
221 
222  //! Method to set a general linear operator (of class derived from Epetra_Operator) defining the linear system
223  /*!
224  @param operPtr Pointer to an operator for the system
225  */
226  void setOperator ( operatorPtr_Type operPtr );
227 
228  //! Method to set the right hand side (rhs) of the linear system
229  /*!
230  @param rhsPtr right hand side of the system
231  */
232  void setRightHandSide ( const vectorPtr_Type rhsPtr );
233 
234  //! Method to set an Preconditioner preconditioner
235  /*!
236  @param preconditionerPtr Preconditioner to be used to solve the system
237  */
238  void setPreconditioner ( preconditionerPtr_Type preconditionerPtr );
239 
240  //! Method to set a general Epetra_Operator as preconditioner
241  /*!
242  @param preconditionerPtr Preconditioner to be set of type Epetra_Operator
243  */
244  void setPreconditioner ( operatorPtr_Type preconditionerPtr );
245 
246  //! Method to set a matrix on which the preconditioner should be created
247  /*!
248  @param baseMatrixPtr matrix on which the preconditioner should be created
249  */
250  void setBaseMatrixForPreconditioner ( matrixPtr_Type baseMatrixPtr );
251 
252  //! Method to setup the solver using Teuchos::ParameterList
253  /*!
254  @param list Teuchos::ParameterList object
255  Note: The parameters are added to the existing one. Use resetParameters to clean the parameters list.
256  */
257  void setParameters ( const Teuchos::ParameterList& list );
258 
259  //! Method to set a particular parameter
260  /*!
261  @param name Name of the parameter
262  @param value Value of the parameter
263  Note: The parameters are added to the existing one. Use resetParameters to clean the parameters list.
264  */
265  template<typename T>
266  void setParameter ( const std::string& name, T value );
267 
268  //! Method to reset the parameters list of the solver
269  void resetParameters();
270 
271  //! Specify if the preconditioner should be reuse or not
272  /*!
273  @param reusePreconditioner If set to true, do not recompute the preconditioner
274  */
275  void setReusePreconditioner ( const bool reusePreconditioner );
276 
277  //! Specify if the application should stop when problems occur in the iterations
278  /*!
279  @param enable If set to true, application will stop if problems occur
280  Note: This option is useful for simulation on clusters. In particular if the
281  system does not converge or if a loss of precision occurs time is saved
282  by stoping the simulation
283  */
284  void setQuitOnFailure ( const bool enable );
285 
286  //! Set the tolerance of the solver
287  /*!
288  @param tolerance Tolerance used by the solver
289  */
290  void setTolerance ( const Real& tolerance );
291 
292  //@}
293 
294  //! @name Get Method
295  //@{
296 
297  //! Return the total number of iterations
298  Int numIterations() const;
299 
300  //! Return the recursive residual
301  /*!
302  The method returns -1 if an error occurs
303  */
304  Real recursiveResidual();
305 
306  //! Method to get a shared pointer to the preconditioner
307  preconditionerPtr_Type& preconditioner();
308 
309  //! Return a Teuchos parameters list
310  Teuchos::ParameterList& parametersList();
311 
312  //! Return a pointer on the Belos solver manager
313  SolverOperatorPtr_Type solver();
314 
315  //! Return a shared pointer on the displayer
316  std::shared_ptr<Displayer> displayer();
317 
318  //! Returns the maximum of iterations tolerate to avoid recomputing the preconditioner
319  Int maxItersForReuse() const;
320 
321  //! Returns if the preconditioner can be reused
322  bool reusePreconditioner() const;
323 
324  //! Returns if the application should stop if a problem occurs
325  bool quitOnFailure() const;
326 
327  //! Returns if the solver is in silent mode
328  bool silent() const;
329 
330  //! Returns if the maximum number of iterations has been reached
331  SolverOperator_Type::SolverOperatorStatusType hasReachedMaxNumIters() const;
332 
333  //! Returns if a loss of precision has been detected
334  SolverOperator_Type::SolverOperatorStatusType isLossOfAccuracyDetected() const;
335 
336  //! Returns if the convergence has been achieved
337  SolverOperator_Type::SolverOperatorStatusType hasConverged() const;
338 
339  //@}
340 
341 private:
342 
343  //! @name Private Methods
344  //@{
345 
346  //@}
347 
348  operatorPtr_Type M_operator;
349  matrixPtr_Type M_matrix;
350  matrixPtr_Type M_baseMatrixForPreconditioner;
351  vectorPtr_Type M_rhs;
352 
353  preconditionerPtr_Type M_preconditioner;
354  operatorPtr_Type M_preconditionerOperator;
355 
356  SolverType M_solverType;
357  SolverOperatorPtr_Type M_solverOperator;
358 
359  Teuchos::ParameterList M_parameterList;
360  std::shared_ptr<Displayer> M_displayer;
361 
362  // LifeV features
363  Int M_maxItersForReuse;
364  bool M_reusePreconditioner;
365  bool M_quitOnFailure;
366  bool M_silent;
367 
368  // Status informations
369  SolverOperator_Type::SolverOperatorStatusType M_lossOfPrecision;
370  SolverOperator_Type::SolverOperatorStatusType M_maxNumItersReached;
371  SolverOperator_Type::SolverOperatorStatusType M_converged;
372 
373  Real M_tolerance;
374 
375 };
376 
377 template<typename T>
378 void
379 LinearSolver::setParameter ( const std::string& name, T value )
380 {
381  M_parameterList.set ( name, value );
382 }
383 
384 namespace defaultParameterLists
385 {
386 //! Returns a default parameter list to initialize the LinearSolver class with Belos.
387 /*!
388  Belos uses the right preconditioned GMRES method with a tolerance of 1e-6.
389  The maximum number of iterations and Krylov vectors are set to 200.
390  The preconditioner is automatically recomputed if more than 80 iterations are
391  necessary to converge.
392  */
393 Teuchos::ParameterList
394 belosParameterList();
395 
396 //! Returns a default parameter list to initialize the LinearSolver class with AztecOO.
397 /*!
398  AztecOO uses the right preconditioned GMRES method with a tolerance of 1e-6.
399  The maximum number of iterations and Krylov vectors are set to 200.
400  The preconditioner is automatically recomputed if more than 80 iterations are
401  necessary to converge.
402  */
403 Teuchos::ParameterList
404 aztecOOParameterList();
405 }
406 
407 } // namespace LifeV
408 
409 #endif /* LINEARSOLVER_HPP */
int32_type Int
Generic integer data.
Definition: LifeV.hpp:188
double Real
Generic real data.
Definition: LifeV.hpp:175