37 const std::string& in_solver_name,
38 const std::string& in_solver_type,
39 const std::string& in_preconditioner_type,
40 double in_relative_residual_tolerance,
41 int64_t in_maximum_iterations)
45 KSPCreate(PETSC_COMM_WORLD,&setup.
ksp);
46 KSPSetOperators(setup.
ksp,ref_matrix,ref_matrix);
47 KSPSetType(setup.
ksp,in_solver_type.c_str());
49 KSPSetOptionsPrefix(setup.
ksp,in_solver_name.c_str());
51 KSPGetPC(setup.
ksp,&setup.
pc);
52 PCSetType(setup.
pc,in_preconditioner_type.c_str());
54 KSPSetTolerances(setup.
ksp,1.e-50,
55 in_relative_residual_tolerance,1.0e50,
56 in_maximum_iterations);
57 KSPSetInitialGuessNonzero(setup.
ksp,PETSC_TRUE);
61 KSPSetFromOptions(setup.
ksp);
84 KSPConvergedReason* convergedReason,
91 VecNorm(Rhs,NORM_2,&rhs_norm);
92 if (rhs_norm < 1.0e-12)
98 KSPGetTolerances(ksp,
nullptr, &tol,
nullptr, &maxIts);
100 double relative_residual = rnorm/rhs_norm;
102 if (relative_residual < tol)
103 *convergedReason = KSP_CONVERGED_RTOL;
105 return KSP_CONVERGED_ITERATING;
112 KSP ksp, PetscInt n, PetscReal rnorm,
void*)
117 VecNorm(Rhs,NORM_2,&rhs_norm);
118 if (rhs_norm < 1.0e-12)
122 const char* ksp_name;
123 KSPGetOptionsPrefix(ksp,&ksp_name);
126 const char NONAME_SOLVER[] =
"NoName-Solver\0";
128 if (ksp_name ==
nullptr)
129 ksp_name = NONAME_SOLVER;
132 std::stringstream buff;
138 << std::scientific << std::setprecision(7) << rnorm / rhs_norm
150 KSP ksp, PetscInt n, PetscReal rnorm,
void*)
153 const char* ksp_name;
154 KSPGetOptionsPrefix(ksp,&ksp_name);
157 const char NONAME_SOLVER[] =
"NoName-Solver\0";
159 if (ksp_name ==
nullptr)
160 ksp_name = NONAME_SOLVER;
163 std::stringstream buff;
169 << std::scientific << std::setprecision(7) << rnorm
LogStream Log(LOG_LVL level=LOG_0)
PetscErrorCode KSPMonitorStraight(KSP ksp, PetscInt n, PetscReal rnorm, void *)
PetscErrorCode RelativeResidualConvergenceTest(KSP ksp, PetscInt n, PetscReal rnorm, KSPConvergedReason *convergedReason, void *monitordestroy)
PETScSolverSetup CreateCommonKrylovSolverSetup(Mat ref_matrix, const std::string &in_solver_name="KSPSolver", const std::string &in_solver_type=KSPGMRES, const std::string &in_preconditioner_type=PCNONE, double in_relative_residual_tolerance=1.0e-6, int64_t in_maximum_iterations=100)
PetscErrorCode KSPMonitorRelativeToRHS(KSP ksp, PetscInt n, PetscReal rnorm, void *)