LifeV
SolverAztecOO.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 SolverAztecOO
30 
31  @author Simone Deparis <simone.deparis@epfl.ch>
32  @author Gilles Fourestey <gilles.fourestey@epfl.ch>
33  @contributor Gwenol Grandperrin <gwenol.grandperrin@epfl.ch>
34  @maintainer Gwenol Grandperrin <gwenol.grandperrin@epfl.ch>
35 
36  @date 08-11-2006
37  */
38 
39 #ifndef __SOLVERAZTECOO_H
40 #define __SOLVERAZTECOO_H 1
41 
42 #include <iomanip>
43 
44 
45 #include <Epetra_ConfigDefs.h>
46 #ifdef EPETRA_MPI
47 #include <Epetra_MpiComm.h>
48 #else
49 #include <Epetra_SerialComm.h>
50 #endif
51 
52 #include <AztecOO_ConfigDefs.h>
53 #include <AztecOO.h>
54 #include <Teuchos_ParameterList.hpp>
55 
56 
57 #include <lifev/core/LifeV.hpp>
58 
59 #include <lifev/core/array/VectorEpetra.hpp>
60 #include <lifev/core/array/MatrixEpetra.hpp>
61 #include <lifev/core/algorithm/Preconditioner.hpp>
62 #include <lifev/core/algorithm/PreconditionerIfpack.hpp>
63 #include <lifev/core/util/LifeDebug.hpp>
64 #include <lifev/core/filter/GetPot.hpp>
65 #include <lifev/core/util/LifeChrono.hpp>
66 #include <lifev/core/util/Displayer.hpp>
67 
68 namespace LifeV
69 {
70 
71 //! SolverAztecOO - Class to wrap linear solver
72 /*!
73  By default the solver is gmres and the preconditioner is ilu.
74 
75  @author Simone Deparis <simone.deparis@epfl.ch>
76  @author Gilles Fourestey <gilles.fourestey@epfl.ch>
77 */
79 {
80 public:
81 
82  //! @name Public Types
83  //@{
84 
85  typedef Real value_type;
86 
88 
91 
97 
98  //@}
99 
100  //! @name Constructors & Destructor
101  //@{
102 
103  //! Empty constructor
104  LIFEV_DEPRECATED ( SolverAztecOO() );
105 
106  //! Constructor
107  /*!
108  @param comm Communicator
109  */
110  LIFEV_DEPRECATED ( SolverAztecOO ( const std::shared_ptr<Epetra_Comm>& comm ) );
111 
112  //@}
113 
114  //! @name Methods
115  //@{
116 
117  //! Solve the problem \f$ A x = b \f$.
118  /*!
119  A has been entered via \c setMatrix.
120  @param solution Vector to store the solution
121  @rhs rhs Right hand side of the problem
122  @return Number of iterations, M_maxIter+1 if solve failed.
123  */
124  Int solve ( vector_type& solution, const vector_type& rhs );
125 
126  //! Compute the residual
127  /*!
128  @param solution Solution of the system
129  @param rhs Right hand side of the problem
130  */
131  Real computeResidual ( vector_type& solution, vector_type& rhs );
132 
133  //! return the Aztec status
134  std::string printStatus();
135 
136  //! Solves the system and returns the number of iterations.
137  /*!
138  The Matrix has already been passed by the method
139  setMatrix or setOperator
140 
141  The preconditioner is build starting from the matrix baseMatrixForPreconditioner
142  by the preconditioner object passed in by the method setPreconditioner
143  @param rhsFull Right hand side
144  @param solution vector to store the solution
145  @param baseMatrixForPreconditioner Base matrix for the preconditioner construction
146  @return number of iterations. If negative, the solver did not converge,
147  the preconditioner has been recomputed, and a second solution is tried
148  */
149  Int solveSystem ( const vector_type& rhsFull,
150  vector_type& solution,
151  matrix_ptrtype& baseMatrixForPreconditioner );
152 
153  //! Solves the system and returns the number of iterations.
154  /*!
155  The Matrix has already been passed by the method
156  setMatrix or setOperator
157  @param rhsFull Right hand side
158  @param solution Vector to store the solution
159  @param preconditionerPtr Pointer on a preconditioner to use (templated parameter, can derive from
160  Preconditioner class or from Epetra_Operator)
161  */
162  template <typename PrecPtrOperator>
163  Int solveSystem ( const vector_type& rhsFull,
164  vector_type& solution,
165  PrecPtrOperator preconditionerPtr );
166 
167  //! Setup the preconditioner
168  /*!
169  @param dataFile GetPot object which contains the data about the preconditioner
170  @param section Section the GetPot structure where to find the informations about the preconditioner
171  */
172  void setupPreconditioner ( const GetPot& dataFile, const std::string& section );
173 
174  //! Builds the preconditioner starting from the matrix "baseMatrixForPreconditioner"
175  /*!
176  The preconditioner is build starting from the matrix baseMatrixForPreconditioner
177  by the preconditioner object passed in by the method setPreconditioner
178  @param baseMatrixForPreconditioner Base matrix for the preconditioner construction
179  */
180  void buildPreconditioner ( matrix_ptrtype& baseMatrixForPreconditioner );
181 
182  //! Delete the stored preconditioner
183  void resetPreconditioner();
184 
185  //! Return true if preconditioner has been setted
186  bool isPreconditionerSet() const;
187 
188  //! Print informations about the solver
189  void showMe ( std::ostream& output = std::cout ) const;
190 
191  //@}
192 
193  //! @name Set Method
194  //@{
195 
196  //! Method to set communicator for Displayer (for empty constructor)
197  /*!
198  @param comm Communicator for the displayer
199  */
200  void setCommunicator ( const std::shared_ptr<Epetra_Comm>& comm );
201 
202  //! Method to set matrix from MatrixEpetra
203  /*!
204  @param matrix Matrix of the system
205  */
206  void setMatrix ( matrix_type& matrix );
207 
208  //! Method to set a general linear operator (of class derived from Epetra_Operator) defining the linear system
209  /*!
210  @param oper Operator for the system
211  */
212  void setOperator ( Epetra_Operator& oper );
213 
214  //! Method to set an Preconditioner preconditioner
215  /*!
216  @param preconditioner Preconditioner to be used to solve the system
217  */
218  void setPreconditioner ( prec_type& preconditioner );
219 
220  //! Method to set a general Epetra_Operator as preconditioner
221  /*!
222  @param preconditioner Preconditioner to be set of type Epetra_Operator
223  */
224  void setPreconditioner ( comp_prec_type& preconditioner );
225 
226  //! Method to setup the solver using GetPot
227  /*!
228  @param dataFile GetPot object which contains the data about the solver
229  */
230  void setDataFromGetPot ( const GetPot& dataFile, const std::string& section );
231 
232  //! Set the current parameters with the internal parameters list
233  /*!
234  Note: The parameter list is set using "setDataFromGetPot".
235  @param cerrWarningIfUnused If true the solver return warning if some parameters are unused
236  */
237  void setParameters ( bool cerrWarningIfUnused = false );
238 
239  //! Set the tolerance of the solver
240  /*!
241  @param tolerance Tolerance for the solver
242  */
243  void setTolerance ( const Real tolerance );
244 
245  //! Set the tolerance and the maximum number of iterations
246  /*!
247  @param maxIter Maximum number of iteration
248  */
249  void setMaxNumIterations ( const Int maxIter = -1 );
250 
251  //! Specify if the preconditioner should be reuse or not
252  /*!
253  @param reusePreconditioner If set to true, do not recompute the preconditioner
254  */
255  void setReusePreconditioner ( const bool reusePreconditioner );
256 
257  //! Return the displayer
259 
260  //@}
261 
262  //! @name Get Method
263  //@{
264 
265  //! Return the total number of iterations
266  Int numIterations() const;
267 
268  //! Return the maximum total number of iterations
269  Int maxNumIterations() const;
270 
271  //! Return the true residual
272  Real trueResidual();
273 
274  //! Method to get a shared pointer to the preconditioner (of type derived from Preconditioner)*/
276 
277  //! Return the AztecStatus
278  void aztecStatus ( Real status[AZ_STATUS_SIZE] );
279 
280  //! Return a Teuchos parameters list
282 
283  //! Return a reference on the AztecOO solver
284  AztecOO& solver();
285 
286  //@}
287 
288 private:
289 
292 
294 
297 
302 };
303 
304 template <typename PrecPtrOperator>
306  vector_type& solution,
307  PrecPtrOperator preconditioner )
308 
309 {
310  M_displayer->leaderPrint ("SLV- AztecOO solving system ... ");
311  setPreconditioner (preconditioner);
312 
313  LifeChrono chrono;
314  chrono.start();
315  Int numIter = solve ( solution, rhsFull );
316  chrono.stop();
317  M_displayer->leaderPrintMax ( "done in " , chrono.diff() );
318 
319  // If we use the "none" as output setting, we display just a summary
320  if ( M_TrilinosParameterList.get ( "output", "all" ) == "none" )
321  {
322  M_displayer->leaderPrint ( "SLV- Iterations number: ", M_solver.NumIters(), "\n" );
323  M_displayer->leaderPrint ( "SLV- Scaled residual: ", M_solver.ScaledResidual(), "\n" );
324  }
325 
326  if ( numIter >= M_maxIter )
327  {
328  numIter = -numIter;
329  }
330 
331  return numIter;
332 }
333 
334 
335 } // namespace LifeV
336 
337 #endif /* __SOLVERAZTECOO_H */
VectorEpetra - The Epetra Vector format Wrapper.
Real computeResidual(vector_type &solution, vector_type &rhs)
Compute the residual.
std::string printStatus()
return the Aztec status
void start()
Start the timer.
Definition: LifeChrono.hpp:93
std::shared_ptr< prec_raw_type > prec_type
Real trueResidual()
Return the true residual.
SolverAztecOO - Class to wrap linear solver.
Int numIterations() const
Return the total number of iterations.
void setMaxNumIterations(const Int maxIter=-1)
Set the tolerance and the maximum number of iterations.
void importFromHDF5(std::string const &fileName, std::string const &matrixName="matrix")
Read a matrix from a HDF5 (.h5) file.
Teuchos::ParameterList M_TrilinosParameterList
Int solve(vector_type &solution, const vector_type &rhs)
Solve the problem .
VectorEpetra vector_type
void showMe(std::ostream &output=std::cout) const
Print informations about the solver.
int32_type Int
Generic integer data.
Definition: LifeV.hpp:188
void aztecStatus(Real status[AZ_STATUS_SIZE])
Return the AztecStatus.
void updateInverseJacobian(const UInt &iQuadPt)
void setOperator(Epetra_Operator &oper)
Method to set a general linear operator (of class derived from Epetra_Operator) defining the linear s...
void setReusePreconditioner(const bool reusePreconditioner)
Specify if the preconditioner should be reuse or not.
std::shared_ptr< Displayer > M_displayer
Teuchos::ParameterList & getParametersList()
Return a Teuchos parameters list.
void setCommunicator(const std::shared_ptr< Epetra_Comm > &comm)
Method to set communicator for Displayer (for empty constructor)
std::shared_ptr< Epetra_Operator > comp_prec_type
AztecOO & solver()
Return a reference on the AztecOO solver.
SolverAztecOO solver_type
bool isPreconditionerSet() const
Return true if preconditioner has been setted.
Int solveSystem(const vector_type &rhsFull, vector_type &solution, PrecPtrOperator preconditionerPtr)
Solves the system and returns the number of iterations.
prec_type & preconditioner()
Method to get a shared pointer to the preconditioner (of type derived from Preconditioner)*/.
Preconditioner prec_raw_type
void buildPreconditioner(matrix_ptrtype &baseMatrixForPreconditioner)
Builds the preconditioner starting from the matrix "baseMatrixForPreconditioner". ...
double Real
Generic real data.
Definition: LifeV.hpp:175
std::shared_ptr< VectorEpetra > vector_ptrtype
Preconditioner - Abstract preconditioner class.
Int maxNumIterations() const
Return the maximum total number of iterations.
matrix_type::matrix_ptrtype M_matrix
void stop()
Stop the timer.
Definition: LifeChrono.hpp:100
std::shared_ptr< matrix_type > matrix_ptrtype
void setParameters(bool cerrWarningIfUnused=false)
Set the current parameters with the internal parameters list.
#define LIFEV_DEPRECATED(func)
Definition: LifeV.hpp:117
void resetPreconditioner()
Delete the stored preconditioner.
std::shared_ptr< Displayer > displayer()
Return the displayer.
void setTolerance(const Real tolerance)
Set the tolerance of the solver.
MatrixEpetra< Real > matrix_type
void setMatrix(matrix_type &matrix)
Method to set matrix from MatrixEpetra.
Int solveSystem(const vector_type &rhsFull, vector_type &solution, matrix_ptrtype &baseMatrixForPreconditioner)
Solves the system and returns the number of iterations.
void setPreconditioner(comp_prec_type &preconditioner)
Method to set a general Epetra_Operator as preconditioner.