Chi-Tech
linear_solver_petsc.cc
Go to the documentation of this file.
1#include "linear_solver.h"
2
3#include <petscksp.h>
4
5namespace chi_math
6{
7
8template<>
10{
11 VecDestroy(&x_);
12 VecDestroy(&b_);
13 KSPDestroy(&solver_);
14}
15
16template<>
18{
19 KSPSetTolerances(solver_,
20 tolerance_options_.residual_relative,
21 tolerance_options_.residual_absolute,
22 tolerance_options_.residual_divergence,
23 tolerance_options_.maximum_iterations);
24}
25
26template<>
28{}
29
30template<>
32{}
33
34template<>
36{
37 KSPSetApplicationContext(solver_, &(*context_ptr_));
38}
39
40template<>
42{
43 KSPSetConvergenceTest(solver_, &KSPConvergedDefault, nullptr, nullptr);
44}
45
46template<>
48{}
49
50template<>
52{}
53
54template<>
56{}
57
58
59template<>
61{
62 if (IsSystemSet()) return;
63 this->PreSetupCallback();
64
65 KSPCreate(PETSC_COMM_WORLD, &solver_);
66 KSPSetType(solver_, iterative_method_.c_str());
67
68 this->ApplyToleranceOptions();
69
70 if (iterative_method_ == "gmres")
71 {
72 KSPGMRESSetRestart(solver_, tolerance_options_.gmres_restart_interval);
73 KSPGMRESSetBreakdownTolerance(solver_,
74 tolerance_options_.gmres_breakdown_tolerance);
75 }
76
77 KSPSetInitialGuessNonzero(solver_, PETSC_FALSE);
78
79 this->SetOptions();
80
81 this->SetSolverContext();
82 this->SetConvergenceTest();
83 this->SetMonitor();
84
85 this->SetSystemSize();
86 this->SetSystem();
87
88 this->SetPreconditioner();
89
90 this->PostSetupCallback();
91 system_set_ = true;
92}
93
94
95template<>
97{}
98
99template<>
101{}
102
103template<>
105{
106 this->PreSolveCallback();
107 this->SetInitialGuess();
108 this->SetRHS();
109
110 if (not suppress_kspsolve_)
111 KSPSolve(solver_, b_, x_);
112 this->PostSolveCallback();
113}
114
115}//namespace chi_math
virtual void PreSetupCallback()
virtual void PreSolveCallback()
virtual void Solve()
virtual void SetOptions()
virtual void SetPreconditioner()
virtual void SetConvergenceTest()
virtual void Setup()
virtual void SetSolverContext()
virtual void PostSetupCallback()
virtual void PostSolveCallback()
virtual void SetMonitor()