From 3a6c2bf0996120e33e3a95f6f0dc39f9de0c07c7 Mon Sep 17 00:00:00 2001 From: Wenqing Wang <wenqing.wang@ufz.de> Date: Thu, 15 May 2014 11:22:06 +0200 Subject: [PATCH] added a member for quiting program --- MathLib/LinAlg/PETSc/PETScLinearSolver.cpp | 10 ++------- MathLib/LinAlg/PETSc/PETScLinearSolver.h | 24 ++++++++++++++-------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/MathLib/LinAlg/PETSc/PETScLinearSolver.cpp b/MathLib/LinAlg/PETSc/PETScLinearSolver.cpp index 5b92490d64f..9118c177db6 100644 --- a/MathLib/LinAlg/PETSc/PETScLinearSolver.cpp +++ b/MathLib/LinAlg/PETSc/PETScLinearSolver.cpp @@ -60,7 +60,7 @@ bool PETScLinearSolver::solve(const PETScVector &b, PETScVector &x) PetscPrintf(PETSC_COMM_WORLD, "\nLinear solver %s with %s preconditioner", ksp_type, pc_type); - int its; + PetscInt its; KSPGetIterationNumber(_solver, &its); PetscPrintf(PETSC_COMM_WORLD,"\nConvergence in %d iterations.\n", its); PetscPrintf(PETSC_COMM_WORLD,"\n================================================\n"); @@ -68,10 +68,10 @@ bool PETScLinearSolver::solve(const PETScVector &b, PETScVector &x) else if(reason == KSP_DIVERGED_ITS) { PetscPrintf(PETSC_COMM_WORLD, "\nWarning: maximum number of iterations reached.\n"); - converged = false; } else { + converged = false; if(reason == KSP_DIVERGED_INDEFINITE_PC) { PetscPrintf(PETSC_COMM_WORLD, "\nDivergence because of indefinite preconditioner,"); @@ -82,7 +82,6 @@ bool PETScLinearSolver::solve(const PETScVector &b, PETScVector &x) PetscPrintf(PETSC_COMM_WORLD, "\nKSPBICG method was detected so the method could not continue to enlarge the Krylov space."); PetscPrintf(PETSC_COMM_WORLD, "\nTry to run again with another solver.\n"); } - else if(reason == KSP_DIVERGED_NONSYMMETRIC) { PetscPrintf(PETSC_COMM_WORLD, "\nMatrix or preconditioner is unsymmetric but KSP requires symmetric.\n"); @@ -91,11 +90,6 @@ bool PETScLinearSolver::solve(const PETScVector &b, PETScVector &x) { PetscPrintf(PETSC_COMM_WORLD, "\nDivergence detected, use command option -ksp_monitor or -log_summary to check the details.\n"); } - - PetscPrintf(PETSC_COMM_WORLD, "\nLinear solver (PETSc KSP) failed, quit now.\n"); - KSPDestroy(&_solver); - PetscFinalize(); - std::exit(EXIT_FAILURE); } #ifdef TEST_MEM_PETSC diff --git a/MathLib/LinAlg/PETSc/PETScLinearSolver.h b/MathLib/LinAlg/PETSc/PETScLinearSolver.h index 7d653663bca..5be0fd0b45b 100644 --- a/MathLib/LinAlg/PETSc/PETScLinearSolver.h +++ b/MathLib/LinAlg/PETSc/PETScLinearSolver.h @@ -52,18 +52,13 @@ class PETScLinearSolver /*! Solve a system of equations. - \param b The right hand of the equations. + \param b The right hand side of the equations. \param x The solutions to be solved. - \return true: converged, false: diverged due to exceeding - the maximum iterations. + \return true: converged, false: diverged. */ bool solve(const PETScVector &b, PETScVector &x); - /*! - \brief Get number of iterations. - If function solve(...) returns false, the return value is - exactly the maximum iterations. - */ + /// Get number of iterations. PetscInt getNumberOfIterations() const { PetscInt its = 0; @@ -71,6 +66,19 @@ class PETScLinearSolver return its; } + /*! + \brief Function used to prepare quiting of program + Release memory and conclude the program before exit. + It must be called before exit() being called after solver fails + to converge. + */ + void Finalize() + { + PetscPrintf(PETSC_COMM_WORLD, "\nLinear solver (PETSc KSP) failed, quit now.\n"); + KSPDestroy(&_solver); + PetscFinalize(); + } + private: /// Matrix, kept as a member only for solving successive linear equation /// that preconditioner matrix may vary. -- GitLab