39 #include <lifev/core/LifeV.hpp> 40 #include <lifev/core/algorithm/SolverAztecOO.hpp> 51 M_TrilinosParameterList(),
52 M_displayer (
new Displayer() ),
58 if ( M_displayer->isLeader() )
60 std::cerr <<
"Warning: SolverAztecOO is deprecated!" << std::endl
61 <<
" You should use LinearSolver instead!" << std::endl;
65 SolverAztecOO::SolverAztecOO (
const std::shared_ptr<Epetra_Comm>& comm ) :
68 M_TrilinosParameterList(),
69 M_displayer (
new Displayer (comm) ),
75 if ( M_displayer->isLeader() )
77 std::cerr <<
"Warning: SolverAztecOO is deprecated!" << std::endl
78 <<
" You should use LinearSolver instead!" << std::endl;
88 M_solver.SetLHS ( &solution.epetraVector() );
91 Epetra_FEVector* rhsVectorPtr (
const_cast<Epetra_FEVector*> (&rhs.epetraVector() ) );
92 M_solver.SetRHS ( rhsVectorPtr );
98 if ( isPreconditionerSet() && M_preconditioner->preconditionerType().compare (
"AztecOO") )
100 M_solver.SetPrecOperator (M_preconditioner->preconditioner() );
103 status = M_solver.Iterate (maxiter, mytol);
105 #ifdef HAVE_LIFEV_DEBUG 106 M_displayer->comm()->Barrier();
107 M_displayer->leaderPrint (
" o- Number of iterations = ", M_solver.NumIters() );
108 M_displayer->leaderPrint (
" o- Norm of the true residual = ", M_solver.TrueResidual() );
109 M_displayer->leaderPrint (
" o- Norm of the true ratio = ", M_solver.ScaledResidual() );
121 Int oldIter = M_solver.NumIters();
122 status = M_solver.Iterate (maxiter, mytol);
124 #ifdef HAVE_LIFEV_DEBUG 125 M_displayer->comm()->Barrier();
126 M_displayer->leaderPrint (
" o- Second run: number of iterations = ", M_solver.NumIters() );
127 M_displayer->leaderPrint (
" o- Norm of the true residual = ", M_solver.TrueResidual() );
128 M_displayer->leaderPrint (
" o- Norm of the true ratio = ", M_solver.ScaledResidual() );
130 return ( M_solver.NumIters() + oldIter );
133 return ( M_solver.NumIters() );
142 M_solver.GetUserMatrix()->Apply ( solution.epetraVector(), Ax.epetraVector() );
144 res.epetraVector().Update ( 1, Ax.epetraVector(), -1 );
157 std::ostringstream stat;
160 Real status[AZ_STATUS_SIZE];
161 aztecStatus ( status );
163 if ( status[AZ_why] == AZ_normal )
165 stat <<
"Normal Convergence ";
167 else if ( status[AZ_why] == AZ_maxits )
169 stat <<
"Maximum iters reached ";
171 else if ( status[AZ_why] == AZ_loss )
173 stat <<
"Accuracy loss ";
175 else if ( status[AZ_why] == AZ_ill_cond )
177 stat <<
"Ill-conditioned ";
179 else if ( status[AZ_why] == AZ_breakdown )
181 stat <<
"Breakdown ";
184 stat << std::setw (12) <<
"res = " << status[AZ_scaled_r];
185 stat << std::setw (4) <<
" " << (Int) status[AZ_its] <<
" iters. ";
202 M_displayer->leaderPrint (
"SLV- Setting up the solver ... \n" );
204 if ( baseMatrixForPreconditioner.get() == 0 )
206 M_displayer->leaderPrint (
"SLV- Warning: baseMatrixForPreconditioner is empty \n" );
217 M_displayer->leaderPrint (
"SLV- Reusing precond ... \n" );
224 if ( numIter < 0 && retry )
228 M_displayer->leaderPrint (
"SLV- Iterative solver failed, numiter = " , - numIter );
229 M_displayer->leaderPrint (
"SLV- maxIterSolver = " , M_maxIter );
230 M_displayer->leaderPrint (
"SLV- retrying: " );
235 M_displayer->leaderPrintMax (
"done in " , chrono.diff() );
241 M_displayer->leaderPrint (
" ERROR: Iterative solver failed again.\n" );
255 std::string precType = dataFile ( (section +
"/prectype").data(),
"Ifpack" );
256 M_preconditioner.reset ( PRECFactory::instance().createObject ( precType ) );
258 ASSERT ( M_preconditioner.get() != 0,
" Preconditioner not set" );
259 M_preconditioner->setSolver ( *
this );
260 M_preconditioner->setDataFromGetPot ( dataFile, section );
270 M_displayer->leaderPrint (
"SLV- Computing the precond ... " );
272 M_preconditioner->buildPreconditioner ( preconditioner );
274 condest = M_preconditioner->condest();
277 M_displayer->leaderPrintMax (
"done in " , chrono.diff() );
278 M_displayer->leaderPrint (
"SLV- Estimated condition number " , condest,
"\n" );
283 M_preconditioner->resetPreconditioner();
289 return ( M_preconditioner.get() != 0 && M_preconditioner->preconditionerCreated() );
295 output <<
"showMe must be implemented for the SolverAztecOO class" << std::endl;
304 M_displayer->setCommunicator ( comm );
309 M_matrix = matrix.matrixPtr();
310 M_solver.SetUserMatrix ( M_matrix.get() );
316 M_solver.SetUserOperator ( &oper );
328 M_solver.SetPrecOperator ( preconditioner.get() );
337 M_TrilinosParameterList.set (
"solver", dataFile ( ( section +
"/solver" ).data(),
"gmres" ) );
340 M_TrilinosParameterList.set (
"conv", dataFile ( ( section +
"/conv" ).data(),
"rhs" ) );
343 M_TrilinosParameterList.set (
"scaling", dataFile ( ( section +
"/scaling" ).data(),
"none" ) );
346 M_TrilinosParameterList.set (
"output", dataFile ( ( section +
"/output" ).data(),
"all" ) );
349 M_tolerance = dataFile ( ( section +
"/tol" ).data(), 1.e-6 );
350 M_TrilinosParameterList.set (
"tol", M_tolerance );
353 M_maxIter = dataFile ( ( section +
"/max_iter" ).data(), 200 );
354 M_maxIterForReuse = dataFile ( ( section +
"/max_iter_reuse").data(),
static_cast<Int> ( M_maxIter * 8. / 10.) );
355 M_reusePreconditioner = dataFile ( (section +
"/reuse").data(), M_reusePreconditioner );
357 M_TrilinosParameterList.set (
"max_iter", M_maxIter );
362 M_TrilinosParameterList.set (
"kspace", dataFile ( ( section +
"/kspace" ).data(), M_maxIter ) );
365 M_TrilinosParameterList.set (
"orthog", dataFile ( ( section +
"/orthog" ).data(), AZ_classic ) );
368 M_TrilinosParameterList.set (
"aux_vec", dataFile ( ( section +
"/aux_vec" ).data(), AZ_resid ) );
378 M_solver.SetParameters ( M_TrilinosParameterList, cerrWarningIfUnused );
387 M_TrilinosParameterList.set (
"tol", M_tolerance );
397 M_TrilinosParameterList.set (
"max_iter", M_maxIter );
419 return M_solver.NumIters();
432 return M_solver.TrueResidual();
444 M_solver.GetAllAztecStatus ( status );
450 return M_TrilinosParameterList;
prec_type M_preconditioner
Real computeResidual(vector_type &solution, vector_type &rhs)
Compute the residual.
std::string printStatus()
return the Aztec status
void start()
Start the timer.
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.
Int solve(vector_type &solution, const vector_type &rhs)
Solve the problem .
void showMe(std::ostream &output=std::cout) const
Print informations about the solver.
int32_type Int
Generic integer data.
void aztecStatus(Real status[AZ_STATUS_SIZE])
Return the AztecStatus.
VectorEpetra(const MapEpetra &map, const MapEpetraType &mapType=Unique, const combineMode_Type combineMode=Add)
Constructor - Using Maps.
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.
void norm2(Real *result) const
Compute and store the norm 2 in the given pointed variable.
bool M_reusePreconditioner
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
const MapEpetra & map() const
Return the MapEpetra of the vector.
AztecOO & solver()
Return a reference on the AztecOO solver.
bool isPreconditionerSet() const
Return true if preconditioner has been setted.
prec_type & preconditioner()
Method to get a shared pointer to the preconditioner (of type derived from Preconditioner)*/.
void buildPreconditioner(matrix_ptrtype &baseMatrixForPreconditioner)
Builds the preconditioner starting from the matrix "baseMatrixForPreconditioner". ...
double Real
Generic real data.
Int maxNumIterations() const
Return the maximum total number of iterations.
void stop()
Stop the timer.
std::shared_ptr< matrix_type > matrix_ptrtype
void setParameters(bool cerrWarningIfUnused=false)
Set the current parameters with the internal parameters list.
void resetPreconditioner()
Delete the stored preconditioner.
VectorEpetra(const VectorEpetra &vector)
Copy constructor.
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.