diff --git a/MathLib/LinAlg/PETSc/PETScLinearSolver.cpp b/MathLib/LinAlg/PETSc/PETScLinearSolver.cpp index b14050661efafde477d2d274461b716d0da0bc36..cf3a8cee7151a00e729e27a87019c98961133516 100644 --- a/MathLib/LinAlg/PETSc/PETScLinearSolver.cpp +++ b/MathLib/LinAlg/PETSc/PETScLinearSolver.cpp @@ -18,19 +18,15 @@ namespace MathLib { -PETScLinearSolver::PETScLinearSolver(PETScMatrix &A, const std::string prefix) +PETScLinearSolver::PETScLinearSolver(PETScMatrix &A, const std::string &prefix) : _A(A) { KSPCreate(PETSC_COMM_WORLD, &_solver); - KSPSetOperators(_solver, A.getRawMatrix(), A.getRawMatrix(), DIFFERENT_NONZERO_PATTERN); + + KSPGetPC(_solver, &_pc); // KSPSetOptionsPrefix(_solver, prefix.c_str()); KSPSetFromOptions(_solver); // set running time option - - KSPGetPC(_solver, &_pc); - - PCSetOptionsPrefix(_pc, prefix.c_str()); - PCSetFromOptions(_pc); // set running time option } void PETScLinearSolver::solve(const PETScVector &b, PETScVector &x) @@ -41,6 +37,8 @@ void PETScLinearSolver::solve(const PETScVector &b, PETScVector &x) PetscMemoryGetCurrentUsage(&mem1); #endif + KSPSetOperators(_solver, _A.getRawMatrix(), _A.getRawMatrix(), DIFFERENT_NONZERO_PATTERN); + KSPSolve(_solver, b.getData(), x.getData()); KSPConvergedReason reason; diff --git a/MathLib/LinAlg/PETSc/PETScLinearSolver.h b/MathLib/LinAlg/PETSc/PETScLinearSolver.h index 10e7c93bb02cb000422d5b94f21dff0861318c9b..430374c5bc364034f18c8733b4a29e5f2670a5a6 100644 --- a/MathLib/LinAlg/PETSc/PETScLinearSolver.h +++ b/MathLib/LinAlg/PETSc/PETScLinearSolver.h @@ -43,7 +43,7 @@ class PETScLinearSolver line for this solver. It can be the name of the PDE that owns an instance of this class. */ - PETScLinearSolver(PETScMatrix &A, const std::string prefix); + PETScLinearSolver(PETScMatrix &A, const std::string &prefix=""); ~PETScLinearSolver() { @@ -58,8 +58,12 @@ class PETScLinearSolver void solve(const PETScVector &b, PETScVector &x); private: + /// Matrix, kept as a member only for solving successive linear equation + /// that preconditioner matrix may vary. + PETScMatrix &_A; + KSP _solver; ///< Slover type. - PC _pc; ///< Preconditioner type. + PC _pc; ///< Preconditioner type. }; } // end namespace diff --git a/Tests/MathLib/TestLinearSolver.cpp b/Tests/MathLib/TestLinearSolver.cpp index e119dcfce6f2cf3cf46e00e6e943431570a131f2..4efb05e27c8777bec4552cf1fb90bbb2742da19b 100644 --- a/Tests/MathLib/TestLinearSolver.cpp +++ b/Tests/MathLib/TestLinearSolver.cpp @@ -127,7 +127,7 @@ void checkLinearSolverInterface(T_MATRIX &A, boost::property_tree::ptree &ls_opt #ifdef USE_PETSC template <class T_MATRIX, class T_VECTOR, class T_LINEAR_SOVLER> -void checkLinearSolverInterface(T_MATRIX &A, T_VECTOR &b, const std::string prefix_name) +void checkLinearSolverInterface(T_MATRIX &A, T_VECTOR &b, const std::string &prefix_name) { int mrank; MPI_Comm_rank(PETSC_COMM_WORLD, &mrank); @@ -292,29 +292,6 @@ TEST(Math, CheckInterface_PETSc_Linear_Solver_gmres_amg) MathLib::PETScLinearSolver>(A, b, "ptest3_"); } -TEST(Math, CheckInterface_PETSc_Linear_Solver_cg_asm) -{ - MathLib::PETScMatrixOption opt; - opt.d_nz = 2; - opt.o_nz = 0; - opt.is_global_size = false; - opt.n_local_cols = 2; - MathLib::PETScMatrix A(2, opt); - - const bool is_gloabal_size = false; - MathLib::PETScVector b(2, is_gloabal_size); - - PetscOptionsSetValue("-ptest4_ksp_type", "cg"); - PetscOptionsSetValue("-ptest4_ksp_rtol", "1.e-8"); - - PetscOptionsSetValue("-ptest4_pc_type", "gasm"); - PetscOptionsSetValue("-ptest4_pc_gasm_type", "interpolate"); - - checkLinearSolverInterface<MathLib::PETScMatrix, MathLib::PETScVector, - MathLib::PETScLinearSolver>(A, b, "ptest4_"); - -} - #endif