8.3
general documentation
cs_sles_petsc.h
Go to the documentation of this file.
1#ifndef __CS_SLES_PETSC_H__
2#define __CS_SLES_PETSC_H__
3
4/*============================================================================
5 * Sparse Linear Equation Solvers using PETSc
6 *============================================================================*/
7
8/*
9 This file is part of code_saturne, a general-purpose CFD tool.
10
11 Copyright (C) 1998-2024 EDF S.A.
12
13 This program is free software; you can redistribute it and/or modify it under
14 the terms of the GNU General Public License as published by the Free Software
15 Foundation; either version 2 of the License, or (at your option) any later
16 version.
17
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 details.
22
23 You should have received a copy of the GNU General Public License along with
24 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
25 Street, Fifth Floor, Boston, MA 02110-1301, USA.
26*/
27
28/*----------------------------------------------------------------------------*/
29
30/*----------------------------------------------------------------------------
31 * Local headers
32 *----------------------------------------------------------------------------*/
33
34#include "cs_base.h"
35#include "cs_matrix.h"
36#include "cs_sles.h"
37
38/*----------------------------------------------------------------------------*/
39
41
42/*============================================================================
43 * Macro definitions
44 *============================================================================*/
45
46/*============================================================================
47 * Type definitions
48 *============================================================================*/
49
50/*----------------------------------------------------------------------------
51 * Function pointer for user settings of a PETSc KSP solver setup.
52 *
53 * This function is called at the end of the setup stage for a KSP solver.
54 *
55 * Note that using the advanced KSPSetPostSolve and KSPSetPreSolve functions,
56 * this also allows setting further function pointers for pre and post-solve
57 * operations (see the PETSc documentation).
58 *
59 * Note: if the context pointer is non-null, it must point to valid data
60 * when the selection function is called so that value or structure should
61 * not be temporary (i.e. local);
62 *
63 * parameters:
64 * context <-> pointer to optional (untyped) value or structure
65 * ksp <-> pointer to PETSc KSP context
66 *----------------------------------------------------------------------------*/
67
68typedef void
69(cs_sles_petsc_setup_hook_t) (void *context,
70 void *ksp);
71
72/* Iterative linear solver context (opaque) */
73
74typedef struct _cs_sles_petsc_t cs_sles_petsc_t;
75
76/*============================================================================
77 * Global variables
78 *============================================================================*/
79
80/*=============================================================================
81 * User function prototypes
82 *============================================================================*/
83
84/*----------------------------------------------------------------------------
85 * Function pointer for user settings of a PETSc KSP solver setup.
86 *
87 * This function is called at the end of the setup stage for a KSP solver.
88 *
89 * Note that using the advanced KSPSetPostSolve and KSPSetPreSolve functions,
90 * this also allows setting further function pointers for pre and post-solve
91 * operations (see the PETSc documentation).
92 *
93 * Note: if the context pointer is non-null, it must point to valid data
94 * when the selection function is called so that value or structure should
95 * not be temporary (i.e. local);
96 *
97 * parameters:
98 * context <-> pointer to optional (untyped) value or structure
99 * ksp <-> pointer to PETSc KSP context
100 *----------------------------------------------------------------------------*/
101
102void
103cs_user_sles_petsc_hook(void *context,
104 void *ksp);
105
106/*=============================================================================
107 * Public function prototypes
108 *============================================================================*/
109
110/*----------------------------------------------------------------------------
111 * Initialize PETSc if needed (calls cs_matrix_petsc_ensure_init).
112 *----------------------------------------------------------------------------*/
113
114void
116
117/*----------------------------------------------------------------------------*/
118/*
119 * \brief Define and associate a PETSc linear system solver
120 * for a given field or equation name.
121 *
122 * If this system did not previously exist, it is added to the list of
123 * "known" systems. Otherwise, its definition is replaced by the one
124 * defined here.
125 *
126 * This is a utility function: if finer control is needed, see
127 * \ref cs_sles_define and \ref cs_sles_petsc_create.
128 *
129 * In case of rotational periodicity for a block (non-scalar) matrix,
130 * the matrix type will be forced to MATSHELL ("shell") regardless
131 * of the option used.
132 *
133 * Note that this function returns a pointer directly to the iterative solver
134 * management structure. This may be used to set further options.
135 * If needed, \ref cs_sles_find may be used to obtain a pointer to the matching
136 * \ref cs_sles_t container.
137 *
138 * \param[in] f_id associated field id, or < 0
139 * \param[in] name associated name if f_id < 0, or nullptr
140 * \param[in] matrix_type PETSc matrix type
141 * \param[in] setup_hook pointer to optional setup epilogue function
142 * \param[in,out] context pointer to optional (untyped) value or
143 * structure for setup_hook, or nullptr
144 *
145 * \return pointer to newly created iterative solver info object.
146 */
147/*----------------------------------------------------------------------------*/
148
150cs_sles_petsc_define(int f_id,
151 const char *name,
152 const char *matrix_type,
153 cs_sles_petsc_setup_hook_t *setup_hook,
154 void *context);
155
156/*----------------------------------------------------------------------------*/
157/*
158 * \brief Create PETSc linear system solver info and context.
159 *
160 * In case of rotational periodicity for a block (non-scalar) matrix,
161 * the matrix type will be forced to MATSHELL ("shell") regardless
162 * of the option used.
163 *
164 * \param[in] matrix_type PETSc matrix type
165 * \param[in] setup_hook pointer to optional setup epilogue function
166 * \param[in,out] context pointer to optional (untyped) value or
167 * structure for setup_hook, or nullptr
168 *
169 * \return pointer to associated linear system object.
170 */
171/*----------------------------------------------------------------------------*/
172
174cs_sles_petsc_create(const char *matrix_type,
175 cs_sles_petsc_setup_hook_t *setup_hook,
176 void *context);
177
178/*----------------------------------------------------------------------------*/
179/*
180 * \brief Create PETSc linear system solver info and context
181 * based on existing info and context.
182 *
183 * \param[in] context pointer to reference info and context
184 * (actual type: cs_sles_petsc_t *)
185 *
186 * \return pointer to newly created solver info object.
187 * (actual type: cs_sles_petsc_t *)
188 */
189/*----------------------------------------------------------------------------*/
190
191void *
192cs_sles_petsc_copy(const void *context);
193
194/*----------------------------------------------------------------------------*/
195/*
196 * \brief Destroy PETSc linear system solver info and context.
197 *
198 * \param[in, out] context pointer to iterative solver info and context
199 * (actual type: cs_sles_petsc_t **)
200 */
201/*----------------------------------------------------------------------------*/
202
203void
204cs_sles_petsc_destroy(void **context);
205
206/*----------------------------------------------------------------------------*/
207/*
208 * \brief Setup PETSc linear equation solver.
209 *
210 * \param[in, out] context pointer to iterative solver info and context
211 * (actual type: cs_sles_petsc_t *)
212 * \param[in] name pointer to system name
213 * \param[in] a associated matrix
214 * \param[in] verbosity associated verbosity
215 */
216/*----------------------------------------------------------------------------*/
217
218void
219cs_sles_petsc_setup(void *context,
220 const char *name,
221 const cs_matrix_t *a,
222 int verbosity);
223
224/*----------------------------------------------------------------------------*/
225/*
226 * \brief Call PETSc linear equation solver.
227 *
228 * \param[in, out] context pointer to iterative solver info and context
229 * (actual type: cs_sles_petsc_t *)
230 * \param[in] name pointer to system name
231 * \param[in] a matrix
232 * \param[in] verbosity associated verbosity
233 * \param[in] precision solver precision
234 * \param[in] r_norm residual normalization
235 * \param[out] n_iter number of "equivalent" iterations
236 * \param[out] residual residual
237 * \param[in] rhs right hand side
238 * \param[in] vx_ini initial system solution
239 * (vx if nonzero, nullptr if zero)
240 * \param[in, out] vx system solution
241 * \param[in] aux_size number of elements in aux_vectors (in bytes)
242 * \param aux_vectors optional working area
243 * (internal allocation if nullptr)
244 *
245 * \return convergence state
246 */
247/*----------------------------------------------------------------------------*/
248
250cs_sles_petsc_solve(void *context,
251 const char *name,
252 const cs_matrix_t *a,
253 int verbosity,
254 double precision,
255 double r_norm,
256 int *n_iter,
257 double *residual,
258 const cs_real_t *rhs,
259 cs_real_t *vx_ini,
260 cs_real_t *vx,
261 size_t aux_size,
262 void *aux_vectors);
263
264/*----------------------------------------------------------------------------*/
265/*
266 * \brief Free PETSc linear equation solver setup context.
267 *
268 * This function frees resolution-related data, such as
269 * buffers and preconditioning but does not free the whole context,
270 * as info used for logging (especially performance data) is maintained.
271 *
272 * \param[in, out] context pointer to iterative solver info and context
273 * (actual type: cs_sles_petsc_t *)
274 */
275/*----------------------------------------------------------------------------*/
276
277void
278cs_sles_petsc_free(void *context);
279
280/*----------------------------------------------------------------------------*/
281/*
282 * \brief Error handler for PETSc solver.
283 *
284 * In case of divergence or breakdown, this error handler outputs an error
285 * message
286 * It does nothing in case the maximum iteration count is reached.
287
288 * \param[in, out] sles pointer to solver object
289 * \param[in] state convergence state
290 * \param[in] a matrix
291 * \param[in] rhs right hand side
292 * \param[in, out] vx system solution
293 *
294 * \return false (do not attempt new solve)
295 */
296/*----------------------------------------------------------------------------*/
297
298bool
301 const cs_matrix_t *a,
302 const cs_real_t *rhs,
303 cs_real_t *vx);
304
305/*----------------------------------------------------------------------------*/
306/*
307 * \brief Log sparse linear equation solver info.
308 *
309 * \param[in] context pointer to iterative solver info and context
310 * (actual type: cs_sles_petsc_t *)
311 * \param[in] log_type log type
312 */
313/*----------------------------------------------------------------------------*/
314
315void
316cs_sles_petsc_log(const void *context,
317 cs_log_t log_type);
318
319/*----------------------------------------------------------------------------
320 *
321 * \brief Output the settings of a KSP structure
322 *
323 * \param[in] ksp Krylov SubSpace structure
324 *----------------------------------------------------------------------------*/
325
326void
327cs_sles_petsc_log_setup(void *ksp);
328
329/*----------------------------------------------------------------------------*/
330/*
331 * \brief Set the parameters driving the termination of an iterative process
332 * associated to a KSP structure
333 *
334 * \param[in, out] context pointer to iterative solver info and context
335 * (actual type: cs_sles_petsc_t *)
336 * \param[in] rtol relative tolerance
337 * \param[in] atol absolute tolerance
338 * \param[in] dtol divergence tolerance
339 * \param[in] max_it max. number of iterations
340 */
341/*----------------------------------------------------------------------------*/
342
343void
344cs_sles_petsc_set_cvg_criteria(const void *context,
345 double rtol,
346 double atol,
347 double dtol,
348 int max_it);
349
350/*----------------------------------------------------------------------------*/
351/*
352 * \brief Return matrix type associated with PETSc linear system solver
353 * info and context.
354 *
355 * \param[in, out] context pointer to iterative solver info and context
356 * (actual type: cs_sles_petsc_t **)
357 *
358 * \return pointer to matrix type name
359 */
360/*----------------------------------------------------------------------------*/
361
362const char *
363cs_sles_petsc_get_mat_type(void *context);
364
365/*----------------------------------------------------------------------------*/
366/*
367 * \brief Print information on PETSc library.
368 *
369 * \param[in] log_type log type
370 */
371/*----------------------------------------------------------------------------*/
372
373void
375
376/*----------------------------------------------------------------------------*/
377
379
380#endif /* __CS_SLES_PETSC_H__ */
#define BEGIN_C_DECLS
Definition: cs_defs.h:542
double cs_real_t
Floating-point value.
Definition: cs_defs.h:342
#define END_C_DECLS
Definition: cs_defs.h:543
cs_log_t
Definition: cs_log.h:48
struct _cs_matrix_t cs_matrix_t
Definition: cs_matrix.h:110
cs_sles_convergence_state_t
Definition: cs_sles.h:56
struct _cs_sles_t cs_sles_t
Definition: cs_sles.h:68
cs_sles_convergence_state_t cs_sles_petsc_solve(void *context, const char *name, const cs_matrix_t *a, int verbosity, double precision, double r_norm, int *n_iter, double *residual, const cs_real_t *rhs, cs_real_t *vx_ini, cs_real_t *vx, size_t aux_size, void *aux_vectors)
Call PETSc linear equation solver.
Definition: cs_sles_petsc.cpp:1464
void cs_sles_petsc_set_cvg_criteria(const void *context, double rtol, double atol, double dtol, int max_it)
Set the parameters driving the termination of an iterative process associated to a KSP structure.
Definition: cs_sles_petsc.cpp:1899
void cs_sles_petsc_log_setup(void *ksp)
Definition: cs_sles_petsc.cpp:1871
void cs_sles_petsc_setup(void *context, const char *name, const cs_matrix_t *a, int verbosity)
Setup PETSc linear equation solver.
Definition: cs_sles_petsc.cpp:1048
cs_sles_petsc_t * cs_sles_petsc_create(const char *matrix_type, cs_sles_petsc_setup_hook_t *setup_hook, void *context)
Create PETSc linear system solver info and context.
Definition: cs_sles_petsc.cpp:894
cs_sles_petsc_t * cs_sles_petsc_define(int f_id, const char *name, const char *matrix_type, cs_sles_petsc_setup_hook_t *setup_hook, void *context)
Define and associate a PETSc linear system solver for a given field or equation name.
Definition: cs_sles_petsc.cpp:852
void cs_user_sles_petsc_hook(void *context, void *ksp)
Definition: cs_sles_petsc.cpp:800
const char * cs_sles_petsc_get_mat_type(void *context)
Return matrix type associated with PETSc linear system solver info and context.
Definition: cs_sles_petsc.cpp:1930
void * cs_sles_petsc_copy(const void *context)
Create PETSc linear system solver info and context based on existing info and context.
Definition: cs_sles_petsc.cpp:977
bool cs_sles_petsc_error_post_and_abort(cs_sles_t *sles, cs_sles_convergence_state_t state, const cs_matrix_t *a, const cs_real_t *rhs, cs_real_t *vx)
Error handler for PETSc solver.
Definition: cs_sles_petsc.cpp:1750
void cs_sles_petsc_destroy(void **context)
Destroy PETSc linear system solver info and context.
Definition: cs_sles_petsc.cpp:1002
struct _cs_sles_petsc_t cs_sles_petsc_t
Definition: cs_sles_petsc.h:74
void cs_sles_petsc_log(const void *context, cs_log_t log_type)
Log sparse linear equation solver info.
Definition: cs_sles_petsc.cpp:1791
void() cs_sles_petsc_setup_hook_t(void *context, void *ksp)
Function pointer for user settings of a PETSc KSP solver setup.
Definition: cs_sles_petsc.h:69
void cs_sles_petsc_init(void)
Definition: cs_sles_petsc.cpp:814
void cs_sles_petsc_free(void *context)
Free PETSc linear equation solver setup context.
Definition: cs_sles_petsc.cpp:1698
void cs_sles_petsc_library_info(cs_log_t log_type)
Print information on PETSc library.
Definition: cs_sles_petsc.cpp:1949