24 std::string
const& petsc_options)
26#if PETSC_VERSION_LT(3, 7, 0)
27 PetscOptionsInsertString(petsc_options.c_str());
29 PetscOptionsInsertString(
nullptr, petsc_options.c_str());
32 KSPCreate(PETSC_COMM_WORLD, &
solver_);
38 KSPSetOptionsPrefix(
solver_, prefix.c_str());
41 KSPSetInitialGuessNonzero(
solver_, PETSC_TRUE);
52 PetscLogDouble mem1, mem2;
53 PetscMemoryGetCurrentUsage(&mem1);
56 KSPNormType norm_type;
57 KSPGetNormType(
solver_, &norm_type);
61 PCGetType(
pc_, &pc_type);
63 PetscPrintf(PETSC_COMM_WORLD,
64 "\n================================================");
65 PetscPrintf(PETSC_COMM_WORLD,
66 "\nLinear solver %s with %s preconditioner using %s", ksp_type,
67 pc_type, KSPNormTypes[norm_type]);
73 KSPConvergedReason reason;
74 KSPGetConvergedReason(
solver_, &reason);
76 bool converged =
true;
80 KSPGetIterationNumber(
solver_, &its);
81 PetscPrintf(PETSC_COMM_WORLD,
"\nconverged in %d iterations", its);
84 case KSP_CONVERGED_RTOL:
85 PetscPrintf(PETSC_COMM_WORLD,
86 " (relative convergence criterion fulfilled).");
88 case KSP_CONVERGED_ATOL:
89 PetscPrintf(PETSC_COMM_WORLD,
90 " (absolute convergence criterion fulfilled).");
93 PetscPrintf(PETSC_COMM_WORLD,
".");
96 PetscPrintf(PETSC_COMM_WORLD,
97 "\n================================================\n");
99 else if (reason == KSP_DIVERGED_ITS)
101 PetscPrintf(PETSC_COMM_WORLD,
102 "\nWarning: maximum number of iterations reached.\n");
107 if (reason == KSP_DIVERGED_INDEFINITE_PC)
109 PetscPrintf(PETSC_COMM_WORLD,
110 "\nDivergence because of indefinite preconditioner,");
111 PetscPrintf(PETSC_COMM_WORLD,
112 "\nTry to run again with "
113 "-pc_factor_shift_positive_definite option.\n");
115 else if (reason == KSP_DIVERGED_BREAKDOWN_BICG)
117 PetscPrintf(PETSC_COMM_WORLD,
118 "\nKSPBICG method was detected so the method could not "
119 "continue to enlarge the Krylov space.");
120 PetscPrintf(PETSC_COMM_WORLD,
121 "\nTry to run again with another solver.\n");
123 else if (reason == KSP_DIVERGED_NONSYMMETRIC)
125 PetscPrintf(PETSC_COMM_WORLD,
126 "\nMatrix or preconditioner is unsymmetric but KSP "
127 "requires symmetric.\n");
131 PetscPrintf(PETSC_COMM_WORLD,
132 "\nDivergence detected, use command option "
133 "-ksp_monitor or -log_summary to check the details.\n");
138 PetscMemoryGetCurrentUsage(&mem2);
141 "###Memory usage by solver. Before: %f After: %f Increase: %d\n", mem1,
142 mem2, (
int)(mem2 - mem1));