29 std::string petsc_options =
30 "-ksp_type cg -pc_type bjacobi -ksp_rtol 1e-16 -ksp_max_it 10000";
41 if (
auto const parameters =
43 subtree->getConfigParameterOptional<std::string>(
"parameters"))
45 petsc_options = *parameters;
50 subtree->getConfigParameterOptional<std::string>(
"prefix"))
57 #if PETSC_VERSION_LT(3, 7, 0)
58 PetscOptionsInsertString(petsc_options.c_str());
60 PetscOptionsInsertString(
nullptr, petsc_options.c_str());
63 KSPCreate(PETSC_COMM_WORLD, &
solver_);
69 KSPSetOptionsPrefix(
solver_, prefix.c_str());
72 KSPSetInitialGuessNonzero(
solver_, PETSC_TRUE);
83 PetscLogDouble mem1, mem2;
84 PetscMemoryGetCurrentUsage(&mem1);
91 KSPConvergedReason reason;
92 KSPGetConvergedReason(
solver_, &reason);
94 bool converged =
true;
100 PCGetType(
pc_, &pc_type);
102 PetscPrintf(PETSC_COMM_WORLD,
103 "\n================================================");
104 PetscPrintf(PETSC_COMM_WORLD,
105 "\nLinear solver %s with %s preconditioner", ksp_type,
109 KSPGetIterationNumber(
solver_, &its);
110 PetscPrintf(PETSC_COMM_WORLD,
"\nconverged in %d iterations", its);
113 case KSP_CONVERGED_RTOL:
114 PetscPrintf(PETSC_COMM_WORLD,
115 " (relative convergence criterion fulfilled).");
117 case KSP_CONVERGED_ATOL:
118 PetscPrintf(PETSC_COMM_WORLD,
119 " (absolute convergence criterion fulfilled).");
122 PetscPrintf(PETSC_COMM_WORLD,
".");
125 PetscPrintf(PETSC_COMM_WORLD,
126 "\n================================================\n");
128 else if (reason == KSP_DIVERGED_ITS)
130 const char* ksp_type;
132 KSPGetType(
solver_, &ksp_type);
133 PCGetType(
pc_, &pc_type);
134 PetscPrintf(PETSC_COMM_WORLD,
135 "\nLinear solver %s with %s preconditioner", ksp_type,
137 PetscPrintf(PETSC_COMM_WORLD,
138 "\nWarning: maximum number of iterations reached.\n");
143 if (reason == KSP_DIVERGED_INDEFINITE_PC)
145 PetscPrintf(PETSC_COMM_WORLD,
146 "\nDivergence because of indefinite preconditioner,");
147 PetscPrintf(PETSC_COMM_WORLD,
148 "\nTry to run again with "
149 "-pc_factor_shift_positive_definite option.\n");
151 else if (reason == KSP_DIVERGED_BREAKDOWN_BICG)
153 PetscPrintf(PETSC_COMM_WORLD,
154 "\nKSPBICG method was detected so the method could not "
155 "continue to enlarge the Krylov space.");
156 PetscPrintf(PETSC_COMM_WORLD,
157 "\nTry to run again with another solver.\n");
159 else if (reason == KSP_DIVERGED_NONSYMMETRIC)
161 PetscPrintf(PETSC_COMM_WORLD,
162 "\nMatrix or preconditioner is unsymmetric but KSP "
163 "requires symmetric.\n");
167 PetscPrintf(PETSC_COMM_WORLD,
168 "\nDivergence detected, use command option "
169 "-ksp_monitor or -log_summary to check the details.\n");
173 #ifdef TEST_MEM_PETSC
174 PetscMemoryGetCurrentUsage(&mem2);
177 "###Memory usage by solver. Before: %f After: %f Increase: %d\n", mem1,
178 mem2, (
int)(mem2 - mem1));
Declaration of class PETScLinearSolver, which defines a solver object based on PETSc routines.
Definition of the RunTime class.
std::optional< ConfigTree > getConfigSubtreeOptional(std::string const &root) const
double elapsed() const
Get the elapsed time in seconds.
void start()
Start the timer.
double elapsed_ctime_
Clock time.
PC pc_
Preconditioner type.
bool solve(PETScMatrix &A, PETScVector &b, PETScVector &x)
PETScLinearSolver(const std::string prefix, BaseLib::ConfigTree const *const option)
Wrapper class for PETSc matrix routines for matrix.
Mat & getRawMatrix()
Get matrix reference.
Wrapper class for PETSc vector.
PETSc_Vec & getRawVector()
Exposes the underlying PETSc vector.
void ignoreOtherLinearSolvers(const BaseLib::ConfigTree &config, const std::string &solver_name)