14 SNESDestroy(&nl_solver_);
21void NonLinearSolverPETSc::ApplyToleranceOptions()
23 SNESSetTolerances(nl_solver_,
28 options_.nl_max_r_evaluations_);
29 SNESSetMaxLinearSolveFailures(nl_solver_, options_.l_max_failed_iterations_);
31 SNESGetKSP(nl_solver_, &ksp);
37 if (options_.l_method_ ==
"gmres")
39 KSPGMRESSetRestart(ksp, options_.l_gmres_restart_intvl_);
40 KSPGMRESSetBreakdownTolerance(ksp, options_.l_gmres_breakdown_tol_);
42 KSPSetInitialGuessNonzero(ksp, PETSC_FALSE);
45void NonLinearSolverPETSc::PreSetupCallback()
50void NonLinearSolverPETSc::SetOptions()
55void NonLinearSolverPETSc::SetSolverContext()
57 SNESSetApplicationContext(nl_solver_, &(*context_ptr_));
61void NonLinearSolverPETSc::SetConvergenceTest()
66void NonLinearSolverPETSc::SetMonitor()
71void NonLinearSolverPETSc::SetPreconditioner()
76void NonLinearSolverPETSc::PostSetupCallback()
81void NonLinearSolverPETSc::Setup()
83 if (IsSystemSet())
return;
84 this->PreSetupCallback();
86 SNESCreate(
Chi::mpi.comm, &nl_solver_);
88 SNESSetOptionsPrefix(nl_solver_, solver_name_.c_str());
90 SNESSetType(nl_solver_, options_.petsc_snes_type_.c_str());
91 if (options_.nl_method_ ==
"LINEAR") SNESSetType(nl_solver_, SNESKSPONLY);
92 SNESLineSearch linesearch;
93 SNESGetLineSearch(nl_solver_, &linesearch);
94 SNESLineSearchSetType(linesearch, SNESLINESEARCHBT);
97 SNESGetKSP(nl_solver_, &ksp);
98 KSPSetType(ksp, options_.l_method_.c_str());
100 KSPSetOptionsPrefix(ksp, solver_name_.c_str());
102 this->ApplyToleranceOptions();
106 this->SetSolverContext();
109 this->SetSolverContext();
110 this->SetConvergenceTest();
113 this->SetSystemSize();
119 this->SetPreconditioner();
121 this->PostSetupCallback();
126void NonLinearSolverPETSc::PreSolveCallback()
131void NonLinearSolverPETSc::PostSolveCallback()
136void NonLinearSolverPETSc::Solve()
139 converged_reason_string_ =
"Reason not obtained";
140 this->PreSolveCallback();
141 this->SetInitialGuess();
143 SNESSolve(nl_solver_,
nullptr, x_);
144 this->PostSolveCallback();
146 SNESConvergedReason conv_reason;
147 SNESGetConvergedReason(nl_solver_, &conv_reason);
149 if (conv_reason > 0) converged_ =
true;
151 const char* strreason;
152 SNESGetConvergedReasonString(nl_solver_, &strreason);
154 converged_reason_string_ = std::string(strreason);
158std::string NonLinearSolverPETSc::GetConvergedReasonString()
const
160 std::stringstream outstr;
163 <<
"Converged " << converged_reason_string_
167 <<
"Convergence failure " << converged_reason_string_
static chi::MPI_Info & mpi
virtual ~NonLinearSolver()
std::string StringStreamColor(StringSteamColorCode code)