37 #include <lifev/core/LifeV.hpp> 38 #include <lifev/core/algorithm/LinearSolver.hpp> 39 #include <lifev/core/algorithm/PreconditionerIfpack.hpp> 40 #include <lifev/core/algorithm/PreconditionerML.hpp> 41 #include <lifev/core/util/WallClock.hpp> 117 M_displayer->leaderPrint (
"SLV- Reusing precond ...\n" );
121 if ( M_rhs.get() == 0 || M_operator == nullptr )
123 M_displayer->leaderPrint (
"SLV- ERROR: LinearSolver failed to set up correctly!\n" );
131 bool failure =
false;
133 M_solverOperator->resetStatus();
139 M_solverOperator->ApplyInverse ( M_rhs->epetraVector(), solutionPtr->epetraVector() );
140 M_converged = M_solverOperator->hasConverged();
141 M_lossOfPrecision = M_solverOperator->isLossOfAccuracyDetected();
145 M_displayer->leaderPrintMax (
"SLV- Solution time: " , chrono.diff(),
" s." );
149 Int numIters = M_solverOperator->numIterations();
158 M_displayer->leaderPrint (
"SLV- Iterative solver failed, numiter = " , numIters,
"\n" );
159 M_displayer->leaderPrint (
"SLV- retrying:\n" );
165 M_solverOperator->ApplyInverse ( M_rhs->epetraVector(), solutionPtr->epetraVector() );
166 M_converged = M_solverOperator->hasConverged();
167 M_lossOfPrecision = M_solverOperator->isLossOfAccuracyDetected();
171 M_displayer->leaderPrintMax (
"SLV- Solution time: " , chrono.diff(),
" s." );
177 M_displayer->leaderPrint (
"SLV- WARNING: Loss of accuracy detected!\n" );
185 M_displayer->leaderPrint (
"SLV- Convergence in " , numIters,
" iterations\n" );
191 M_displayer->leaderPrint (
"SLV- WARNING: Solver failed to converged to the desired precision!\n" );
204 M_solverOperator->resetSolver();
222 M_displayer->leaderPrint (
"SLV- WARNING: LinearSolver can not compute the residual if the operator and the RHS are not set!\n" );
229 M_operator->Apply ( solutionPtr->epetraVector(), Ax.epetraVector() );
231 residual.epetraVector().Update ( 1, Ax.epetraVector(), -1 );
243 std::ostringstream stat;
248 stat <<
"Accuracy loss ";
252 stat <<
"Maximum number of iterations reached ";
256 stat <<
"The solver has converged ";
260 stat <<
"The solver has not ";
269 std::string precName = dataFile ( ( section +
"/prectype" ).data(),
"Ifpack" );
271 M_preconditioner.reset ( PRECFactory::instance().createObject ( precName ) );
272 ASSERT ( M_preconditioner.get() != 0,
" Preconditioner not set" );
274 M_preconditioner->setDataFromGetPot ( dataFile, section );
285 if ( M_matrix.get() == 0 )
287 M_displayer->leaderPrint (
"SLV- ERROR: LinearSolver requires a matrix to build the preconditioner!\n" );
295 M_displayer->leaderPrint (
"SLV- Computing the preconditioner...\n" );
297 if ( M_baseMatrixForPreconditioner.get() == 0 )
301 M_displayer->leaderPrint (
"SLV- Build the preconditioner using the problem matrix\n" );
303 M_preconditioner->buildPreconditioner ( M_matrix );
309 M_displayer->leaderPrint (
"SLV- Build the preconditioner using the base matrix provided\n" );
311 M_preconditioner->buildPreconditioner ( M_baseMatrixForPreconditioner );
313 condest = M_preconditioner->condest();
317 M_displayer->leaderPrintMax (
"SLV- Preconditioner computed in " , chrono.diff(),
" s." );
321 M_displayer->leaderPrint (
"SLV- Estimated condition number " , condest,
"\n" );
332 M_preconditioner->resetPreconditioner();
344 return M_preconditioner.get() != 0 && M_preconditioner->preconditionerCreated();
358 if ( M_displayer->isLeader() )
360 output <<
"Solver parameters list:" << std::endl;
361 output <<
"-----------------------------" << std::endl;
362 output << M_parameterList << std::endl;
363 output <<
"-----------------------------" << std::endl;
376 M_solverOperator.reset ( Operators::SolverOperatorFactory::instance().createObject (
"Belos" ) );
379 M_solverOperator.reset ( Operators::SolverOperatorFactory::instance().createObject (
"AztecOO" ) );
382 M_displayer->leaderPrint (
"SLV- ERROR: The type of solver is not recognized!\n" );
391 M_solverOperator->setPreconditioner ( M_preconditioner->preconditionerPtr() );
395 M_solverOperator->setPreconditioner ( M_preconditionerOperator );
399 M_solverOperator->setOperator ( M_operator );
404 M_solverOperator->setTolerance ( M_tolerance );
408 M_solverOperator->setParameters ( M_parameterList.sublist (
"Solver: Operator List" ) );
423 M_displayer->setCommunicator ( commPtr );
428 M_operator = matrixPtr->matrixPtr();
449 M_preconditionerOperator.reset();
458 M_preconditioner.reset();
472 M_parameterList.setParameters ( list );
474 std::string solverName = M_parameterList.get<std::string> (
"Solver Type" );
475 if ( solverName ==
"Belos" )
479 else if ( solverName ==
"AztecOO" )
484 M_reusePreconditioner = M_parameterList.get (
"Reuse Preconditioner" ,
false );
485 Int maxIter = M_parameterList.get (
"Maximum Iterations" , 200 );
486 M_maxItersForReuse = M_parameterList.get (
"Max Iterations For Reuse" ,
static_cast<Int> ( maxIter * 8. / 10. ) );
487 M_quitOnFailure = M_parameterList.get (
"Quit On Failure" ,
false );
488 M_silent = M_parameterList.get (
"Silent" ,
false );
494 M_parameterList = Teuchos::ParameterList();
522 return M_solverOperator->numIterations();
529 M_displayer->leaderPrint (
"SLV- WARNING: LinearSoler::recursiveResidual is not yet implemented\n" );
555 return M_parameterList;
627 Teuchos::ParameterList defaultList;
628 defaultList.set (
"Reuse Preconditioner" ,
false );
629 defaultList.set (
"Max Iterations For Reuse", 80 );
630 defaultList.set (
"Quit On Failure" ,
false );
631 defaultList.set (
"Silent" ,
false );
632 defaultList.set (
"Solver Type" ,
"Belos" );
634 Teuchos::ParameterList& operatorList = defaultList.sublist (
"Solver: Operator List" );
635 operatorList.set (
"Solver Manager Type",
"BlockGmres" );
636 operatorList.set (
"Preconditioner Side",
"Right" );
638 Teuchos::ParameterList& defaultBelos = operatorList.sublist (
"Trilinos: Belos List" );
639 defaultBelos.set (
"Flexible Gmres" ,
false );
640 defaultBelos.set (
"Convergence Tolerance", 1e-6 );
641 defaultBelos.set (
"Maximum Iterations" , 200 );
642 defaultBelos.set (
"Output Frequency" , 1 );
643 defaultBelos.set (
"Block Size" , 1 );
644 defaultBelos.set (
"Num Blocks" , 200 );
645 defaultBelos.set (
"Maximum Restarts" , 0 );
646 defaultBelos.set (
"Output Style" , 1 );
647 defaultBelos.set (
"Verbosity" , 35 );
655 Teuchos::ParameterList defaultList;
656 defaultList.set (
"Reuse Preconditioner" ,
false );
657 defaultList.set (
"Max Iterations For Reuse", 80 );
658 defaultList.set (
"Quit On Failure" ,
false );
659 defaultList.set (
"Silent" ,
false );
660 defaultList.set (
"Solver Type" ,
"AztecOO" );
662 Teuchos::ParameterList& operatorList = defaultList.sublist (
"Solver: Operator List" );
664 Teuchos::ParameterList& defaultAztecOO = operatorList.sublist (
"Trilinos: AztecOO List" );
665 defaultAztecOO.set (
"solver" ,
"gmres" );
666 defaultAztecOO.set (
"conv" ,
"rhs" );
667 defaultAztecOO.set (
"scaling" ,
"none" );
668 defaultAztecOO.set (
"output" ,
"all" );
669 defaultAztecOO.set (
"tol" , 1e-6 );
670 defaultAztecOO.set (
"max_iter", 200 );
671 defaultAztecOO.set (
"kspace" , 200 );
672 defaultAztecOO.set (
"orthog" , 0 );
673 defaultAztecOO.set (
"aux_vec" , 0 );
SolverOperator_Type::SolverOperatorStatusType hasReachedMaxNumIters() const
Returns if the maximum number of iterations has been reached.
std::shared_ptr< SolverOperator_Type > SolverOperatorPtr_Type
Real computeResidual(vectorPtr_Type solutionPtr)
Compute the residual.
Operators::SolverOperator SolverOperator_Type
bool quitOnFailure() const
Returns if the application should stop if a problem occurs.
std::string printStatus()
return the solver status
bool isPreconditionerSet() const
Return true if preconditioner has been setted.
bool silent() const
Returns if the solver is in silent mode.
SolverOperator_Type::SolverOperatorStatusType hasConverged() const
Returns if the convergence has been achieved.
SolverOperator_Type::SolverOperatorStatusType M_converged
Int numIterations() const
Return the total number of iterations.
Real recursiveResidual()
Return the recursive residual.
void setReusePreconditioner(const bool reusePreconditioner)
Specify if the preconditioner should be reuse or not.
int32_type Int
Generic integer data.
void resetPreconditioner()
Reset the stored preconditioner.
void resetStatus()
Reset the status for the state of convergence and loss of accuracy.
void setQuitOnFailure(const bool enable)
Specify if the application should stop when problems occur in the iterations.
void updateInverseJacobian(const UInt &iQuadPt)
operatorPtr_Type M_operator
Teuchos::ParameterList aztecOOParameterList()
Returns a default parameter list to initialize the LinearSolver class with AztecOO.
void setParameters(const Teuchos::ParameterList &list)
Method to setup the solver using Teuchos::ParameterList.
SolverOperatorPtr_Type M_solverOperator
std::shared_ptr< Displayer > displayer()
Return a shared pointer on the displayer.
void norm2(Real *result) const
Compute and store the norm 2 in the given pointed variable.
void setSolverType(const SolverType &solverType)
Set the solver which should be used.
preconditionerPtr_Type & preconditioner()
Method to get a shared pointer to the preconditioner.
LinearSolver - Class to wrap linear solver.
void setOperator(operatorPtr_Type operPtr)
Method to set a general linear operator (of class derived from Epetra_Operator) defining the linear s...
SolverOperatorPtr_Type solver()
Return a pointer on the Belos solver manager.
void setRightHandSide(const vectorPtr_Type rhsPtr)
Method to set the right hand side (rhs) of the linear system.
bool M_reusePreconditioner
void resetParameters()
Method to reset the parameters list of the solver.
void setTolerance(const Real &tolerance)
Set the tolerance of the solver.
SolverOperator_Type::SolverOperatorStatusType M_lossOfPrecision
std::shared_ptr< operator_Type > operatorPtr_Type
Int maxItersForReuse() const
Returns the maximum of iterations tolerate to avoid recomputing the preconditioner.
SolverOperator_Type::SolverOperatorStatusType M_maxNumItersReached
double Real
Generic real data.
void setPreconditioner(operatorPtr_Type preconditionerPtr)
Method to set a general Epetra_Operator as preconditioner.
Teuchos::ParameterList belosParameterList()
Returns a default parameter list to initialize the LinearSolver class with Belos. ...
void setCommunicator(const std::shared_ptr< Epetra_Comm > commPtr)
Method to set communicator for Displayer (for empty constructor)
std::shared_ptr< VectorEpetra > vectorPtr_Type
void setupSolverOperator()
Setup the solver operator to be used.
void setBaseMatrixForPreconditioner(matrixPtr_Type baseMatrixPtr)
Method to set a matrix on which the preconditioner should be created.
Teuchos::ParameterList & parametersList()
Return a Teuchos parameters list.
Int solve(vectorPtr_Type solutionPtr)
Solves the system and returns the number of iterations.
std::shared_ptr< preconditioner_Type > preconditionerPtr_Type
std::shared_ptr< matrix_Type > matrixPtr_Type
SolverOperator_Type::SolverOperatorStatusType isLossOfAccuracyDetected() const
Returns if a loss of precision has been detected.
void showMe(std::ostream &output=std::cout) const
Print informations about the solver.
void buildPreconditioner()
Builds the preconditioner starting from the matrix "baseMatrixForPreconditioner". ...
void setPreconditionerFromGetPot(const GetPot &dataFile, const std::string §ion)
Setup the preconditioner from a GetPot file.
LinearSolver(const std::shared_ptr< Epetra_Comm > commPtr)
Constructor.
~LinearSolver()
Destructor.
matrixPtr_Type M_baseMatrixForPreconditioner
bool reusePreconditioner() const
Returns if the preconditioner can be reused.
operatorPtr_Type M_preconditionerOperator
preconditionerPtr_Type M_preconditioner
LinearSolver()
Empty constructor.