GCC Code Coverage Report


Directory: ./
File: PETScInterface/KSPInterface.hpp
Date: 2024-04-14 07:32:34
Exec Total Coverage
Lines: 12 12 100.0%
Branches: 1 2 50.0%

Line Branch Exec Source
1 // ______ ______ _ _ _____ ______
2 // | ____| ____| | (_)/ ____| | ____|
3 // | |__ | |__ | | _| (___ ___| |__
4 // | __| | __| | | | |\___ \ / __| __|
5 // | | | |____| |____| |____) | (__| |____
6 // |_| |______|______|_|_____/ \___|______|
7 // Finite Elements for Life Sciences and Engineering
8 //
9 // License: LGL2.1 License
10 // FELiScE default license: LICENSE in root folder
11 //
12 // Main authors: Vicente Mataix Ferrandiz
13 //
14
15 #ifndef KSP_INTERFACE_HPP
16 #define KSP_INTERFACE_HPP
17
18 // System includes
19
20 // External includes:
21 #include "Core/NoThirdPartyWarning/Petsc/ksp.hpp"
22
23 // Project includes
24 #include "Core/felisce.hpp"
25 #include "Core/shared_pointers.hpp"
26 #include "PETScInterface/petscMatrix.hpp"
27 #include "PETScInterface/petscVector.hpp"
28 #include "PETScInterface/SNESInterface.hpp"
29
30 namespace felisce
31 {
32 ///@name felisce Globals
33 ///@{
34
35 ///@}
36 ///@name Type Definitions
37 ///@{
38
39 ///@}
40 ///@name Enum's
41 ///@{
42
43 ///@}
44 ///@name Functions
45 ///@{
46
47 ///@}
48 ///@name felisce Classes
49 ///@{
50
51 /**
52 * @brief Interface for the abstract PETSc object that manages all Krylov methods. This is the object that manages the linear solves in PETSc (even those such as direct solvers that do no use Krylov accelerators).
53 */
54 class KSPInterface
55 {
56 public:
57 ///@name Type Definitions
58 ///@{
59
60 /// Pointer definition of KSPInterface
61 FELISCE_CLASS_POINTER_DEFINITION(KSPInterface);
62
63 ///@}
64 ///@name Life Cycle
65 ///@{
66
67 /**
68 * @brief Default constructor
69 */
70 KSPInterface();
71
72 /**
73 * @brief Destructor
74 */
75 ~KSPInterface();
76
77 ///@}
78 ///@name Operators
79 ///@{
80
81 ///@}
82 ///@name Operations
83 ///@{
84
85 /**
86 * @brief Initializes the KSP object
87 */
88 void init();
89
90 /**
91 * @brief Initializes the KSP object with custom MPI_Comm
92 */
93 void init(MPI_Comm comm);
94
95 /**
96 * @brief Initializes the KSP object from a SNED object
97 */
98 void initFromSNES(SNESInterface::Pointer snes);
99
100 /**
101 * @brief This method sets the type of solver, preconditioner with the given values
102 * @param[in] solver Name of the choosen solver
103 * @param[in] preconditioner Name of the choosen preconditioner
104 */
105 void setKSPandPCType(const std::string solver, const std::string preconditioner);
106
107 /**
108 * @brief This method sets the tolerances with the given values
109 * @param[in] relativeTolerance Relative convergence tolerance
110 * @param[in] absoluteTolerance Absolute convergence tolerance
111 * @param[in] maxIteration Maximum number of iterations
112 * @param[in] gmresRestart Sets the number of search directions for GMRES and FGMRES before restart
113 */
114 void setTolerances(
115 const double relativeTolerance,
116 const double absoluteTolerance,
117 const int maxIteration,
118 const int gmresRestart
119 );
120
121 /**
122 * @brief This method sets some options with the given values
123 * @param[in] solver Name of the choosen solver
124 * @param[in] usePrevSol Flag to use previous solution as initial guess
125 */
126 void setKSPOptions(const std::string solver, const bool usePrevSol);
127
128 /**
129 * @brief This method sets the matrix and some preconditioner options with the given values
130 * @param[in] mat Petsc matrix
131 * @param[in] preconditionerOption Preconditioner options
132 */
133 void setKSPOperator(PetscMatrix& mat, const std::string preconditionerOption);
134
135 /**
136 * @brief This method sets the initial guess to be zero
137 */
138 984 void setInitialGuessZero()
139 {
140 984 KSPSetInitialGuessNonzero(m_KSP, PETSC_FALSE);
141 984 }
142
143 /**
144 * @brief This method sets the initial guess to be non-zero
145 */
146 void setInitialGuessNonzero()
147 {
148 KSPSetInitialGuessNonzero(m_KSP, PETSC_TRUE);
149 }
150
151 /**
152 * @brief This method sets the diagonal scale
153 */
154 1084 void setDiagonalScale()
155 {
156 1084 KSPSetDiagonalScale(m_KSP, PETSC_TRUE);
157 1084 }
158
159 /**
160 * @brief This method sets the diagonal scale back after solve
161 */
162 984 void setDiagonalScaleFix()
163 {
164 984 KSPSetDiagonalScaleFix(m_KSP, PETSC_TRUE);
165 984 }
166
167 /**
168 * @brief This function solves using the KSP object
169 * @param[in] rRhs The rhs vector
170 * @param[out] rSol The solution vector
171 * @param[in] verbose Verbose level
172 */
173 void solve(PetscVector& rRhs, PetscVector& rSol, const int verbose);
174
175 /**
176 * @brief Destroy the KSP object
177 */
178 void destroy();
179
180 ///@}
181 ///@name Access
182 ///@{
183
184 /**
185 * @brief This function returns if the KSP object is initialized
186 * @return true if the KSP object is initialized, false otherwise
187 */
188 bool& initialized()
189 {
190 return m_Initialized;
191 }
192
193 /**
194 * @brief This function returns if the KSP object is initialized or not
195 * @return true if the KSP object is initialized, false otherwise
196 */
197 bool initialized() const
198 {
199 return m_Initialized;
200 }
201
202 /**
203 * @brief This function returns the KSP object
204 * @return The KSP object
205 */
206 KSP& getKSP()
207 {
208 return m_KSP;
209 }
210
211 /**
212 * @brief This function returns the KSP object (const version)
213 * @return The KSP object
214 */
215 const KSP& getKSP() const
216 {
217 return m_KSP;
218 }
219
220 /**
221 * @brief This function returns the PC object
222 * @return The PC object
223 */
224 PC& getPC()
225 {
226 return m_PC;
227 }
228
229 /**
230 * @brief This function returns the PC object (const version)
231 * @return The PC object
232 */
233 const PC& getPC() const
234 {
235 return m_PC;
236 }
237
238 /**
239 * @brief This function returns the number of interation
240 * @return The number of iteration
241 */
242 152 PetscInt getNumOfIteration()
243 {
244 PetscInt its;
245
1/2
✓ Branch 1 taken 152 times.
✗ Branch 2 not taken.
152 KSPGetIterationNumber(m_KSP, &its);
246 152 return its;
247 }
248
249 ///@}
250 ///@name Inquiry
251 ///@{
252
253 ///@}
254 ///@name Input and output
255 ///@{
256
257 ///@}
258 ///@name Friends
259 ///@{
260
261 ///@}
262 protected:
263 ///@name Protected static Member Variables
264 ///@{
265
266 ///@}
267 ///@name Protected member Variables
268 ///@{
269
270 ///@}
271 ///@name Protected Operators
272 ///@{
273
274 ///@}
275 ///@name Protected Operations
276 ///@{
277
278 ///@}
279 ///@name Protected Access
280 ///@{
281
282 ///@}
283 ///@name Protected Inquiry
284 ///@{
285
286 ///@}
287 ///@name Protected LifeCycle
288 ///@{
289
290 ///@}
291 private:
292 ///@name Private static Member Variables
293 ///@{
294
295 ///@}
296 ///@name Private member Variables
297 ///@{
298
299 bool m_Initialized = false; /// If the KSP class has been m_Initialized
300
301 KSP m_KSP; /// The abstract PETSc object that manages all Krylov methods. This is the object that manages the linear solves in PETSc (even those such as direct solvers that do no use Krylov accelerators).
302
303 PC m_PC; /// The preconditioner
304
305 MPI_Comm m_comm = MPI_COMM_NULL; /// The MPI communicator
306
307 ///@}
308 ///@name Private Operators
309 ///@{
310
311 ///@}
312 ///@name Private Operations
313 ///@{
314
315 ///@}
316 ///@name Private Access
317 ///@{
318
319 ///@}
320 ///@name Private Inquiry
321 ///@{
322
323 ///@}
324 ///@name Private LifeCycle
325 ///@{
326
327 ///@}
328 };
329
330 ///@}
331 ///@name Type Definitions
332 ///@{
333
334 ///@}
335
336 }
337
338 #endif // KSP_INTERFACE_HPP
339