programmer's 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-2018 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  * PETSc headers
32  *----------------------------------------------------------------------------*/
33 
34 #include <petscksp.h>
35 
36 /*----------------------------------------------------------------------------
37  * Local headers
38  *----------------------------------------------------------------------------*/
39 
40 #include "cs_base.h"
41 #include "cs_halo_perio.h"
42 #include "cs_matrix.h"
43 #include "cs_time_plot.h"
44 
45 /*----------------------------------------------------------------------------*/
46 
48 
49 /*============================================================================
50  * Macro definitions
51  *============================================================================*/
52 
53 /*============================================================================
54  * Type definitions
55  *============================================================================*/
56 
57 /*----------------------------------------------------------------------------
58  * Function pointer for user settings of a PETSc KSP solver setup.
59  *
60  * This function is called the end of the setup stage for a KSP solver.
61  *
62  * Note that using the advanced KSPSetPostSolve and KSPSetPreSolve functions,
63  * this also allows setting furthur function pointers for pre and post-solve
64  * operations (see the PETSc documentation).
65  *
66  * Note: if the context pointer is non-NULL, it must point to valid data
67  * when the selection function is called so that value or structure should
68  * not be temporary (i.e. local);
69  *
70  * parameters:
71  * context <-> pointer to optional (untyped) value or structure
72  * ksp <-> pointer to PETSc KSP context
73  *----------------------------------------------------------------------------*/
74 
75 typedef void
76 (cs_sles_petsc_setup_hook_t) (void *context,
77  KSP ksp);
78 
79 /* Iterative linear solver context (opaque) */
80 
81 typedef struct _cs_sles_petsc_t cs_sles_petsc_t;
82 
83 /*============================================================================
84  * Global variables
85  *============================================================================*/
86 
87 /*=============================================================================
88  * User function prototypes
89  *============================================================================*/
90 
91 /*----------------------------------------------------------------------------
92  * Function pointer for user settings of a PETSc KSP solver setup.
93  *
94  * This function is called the end of the setup stage for a KSP solver.
95  *
96  * Note that using the advanced KSPSetPostSolve and KSPSetPreSolve functions,
97  * this also allows setting furthur function pointers for pre and post-solve
98  * operations (see the PETSc documentation).
99  *
100  * Note: if the context pointer is non-NULL, it must point to valid data
101  * when the selection function is called so that value or structure should
102  * not be temporary (i.e. local);
103  *
104  * parameters:
105  * context <-> pointer to optional (untyped) value or structure
106  * ksp <-> pointer to PETSc KSP context
107  *----------------------------------------------------------------------------*/
108 
109 void
110 cs_user_sles_petsc_hook(void *context,
111  KSP ksp);
112 
113 /*=============================================================================
114  * Public function prototypes
115  *============================================================================*/
116 
117 /*----------------------------------------------------------------------------
118  * Define and associate a PETSc linear system solver
119  * for a given field or equation name.
120  *
121  * If this system did not previously exist, it is added to the list of
122  * "known" systems. Otherwise, its definition is replaced by the one
123  * defined here.
124  *
125  * This is a utility function: if finer control is needed, see
126  * cs_sles_define() and cs_sles_petsc_create().
127  *
128  * In case of rotational periodicity for a block (non-scalar) matrix,
129  * the matrix type will be forced to MATSHELL ("shell") regardless
130  * of the option used.
131  *
132  * Note that this function returns a pointer directly to the iterative solver
133  * management structure. This may be used to set further options.
134  * If needed, cs_sles_find() may be used to obtain a pointer to the matching
135  * cs_sles_t container.
136  *
137  * parameters:
138  * f_id <-- associated field id, or < 0
139  * name <-- associated name if f_id < 0, or NULL
140  * matrix_type <-- PETSc matrix type
141  * setup_hook <-- pointer to optional setup epilogue function
142  * context <-> pointer to optional (untyped) value or structure
143  * for setup_hook, or NULL
144  *
145  * returns:
146  * pointer to newly created iterative solver info object.
147  *----------------------------------------------------------------------------*/
148 
150 cs_sles_petsc_define(int f_id,
151  const char *name,
152  MatType matrix_type,
153  cs_sles_petsc_setup_hook_t *setup_hook,
154  void *context);
155 
156 /*----------------------------------------------------------------------------
157  * Create PETSc linear system solver info and context.
158  *
159  * In case of rotational periodicity for a block (non-scalar) matrix,
160  * the matrix type will be forced to MATSHELL ("shell") regardless
161  * of the option used.
162  *
163  * parameters:
164  * matrix_type <-- PETSc matrix type
165  * setup_hook <-- pointer to optional setup epilogue function
166  * context <-> pointer to optional (untyped) value or structure
167  * for setup_hook, or NULL
168  *
169  * returns:
170  * pointer to newly created solver info object.
171  *----------------------------------------------------------------------------*/
172 
174 cs_sles_petsc_create(MatType matrix_type,
175  cs_sles_petsc_setup_hook_t *setup_hook,
176  void *context);
177 
178 /*----------------------------------------------------------------------------
179  * Create PETSc linear system solver info and context
180  * based on existing info and context.
181  *
182  * parameters:
183  * context <-- pointer to reference info and context
184  * (actual type: cs_sles_petsc_t *)
185  *
186  * returns:
187  * pointer to newly created solver info object
188  * (actual type: cs_sles_petsc_t *)
189  *----------------------------------------------------------------------------*/
190 
191 void *
192 cs_sles_petsc_copy(const void *context);
193 
194 /*----------------------------------------------------------------------------
195  * Destroy PETSc linear system solver info and context.
196  *
197  * parameters:
198  * context <-> pointer to PETSc linear solver info
199  * (actual type: cs_sles_petsc_t **)
200  *----------------------------------------------------------------------------*/
201 
202 void
203 cs_sles_petsc_destroy(void **context);
204 
205 /*----------------------------------------------------------------------------
206  * Setup PETSc linear equation solver.
207  *
208  * parameters:
209  * context <-> pointer to PETSc linear solver info
210  * (actual type: cs_sles_petsc_t *)
211  * name <-- pointer to system name
212  * a <-- associated matrix
213  * verbosity <-- verbosity level
214  *----------------------------------------------------------------------------*/
215 
216 void
217 cs_sles_petsc_setup(void *context,
218  const char *name,
219  const cs_matrix_t *a,
220  int verbosity);
221 
222 /*----------------------------------------------------------------------------
223  * Call PETSc linear equation solver.
224  *
225  * parameters:
226  * context <-> pointer to PETSc linear solver info
227  * (actual type: cs_sles_petsc_t *)
228  * name <-- pointer to system name
229  * a <-- matrix
230  * verbosity <-- verbosity level
231  * rotation_mode <-- halo update option for rotational periodicity
232  * precision <-- solver precision
233  * r_norm <-- residue normalization
234  * n_iter --> number of iterations
235  * residue --> residue
236  * rhs <-- right hand side
237  * vx <-> system solution
238  * aux_size <-- number of elements in aux_vectors (in bytes)
239  * aux_vectors --- optional working area (internal allocation if NULL)
240  *
241  * returns:
242  * convergence state
243  *----------------------------------------------------------------------------*/
244 
246 cs_sles_petsc_solve(void *context,
247  const char *name,
248  const cs_matrix_t *a,
249  int verbosity,
250  cs_halo_rotation_t rotation_mode,
251  double precision,
252  double r_norm,
253  int *n_iter,
254  double *residue,
255  const cs_real_t *rhs,
256  cs_real_t *vx,
257  size_t aux_size,
258  void *aux_vectors);
259 
260 /*----------------------------------------------------------------------------
261  * Free PETSc linear equation solver setup context.
262  *
263  * This function frees resolution-related data, such as
264  * buffers and preconditioning but does not free the whole context,
265  * as info used for logging (especially performance data) is maintained.
266 
267  * parameters:
268  * context <-> pointer to PETSc linear solver info
269  * (actual type: cs_sles_petsc_t *)
270  *----------------------------------------------------------------------------*/
271 
272 void
273 cs_sles_petsc_free(void *context);
274 
275 /*----------------------------------------------------------------------------*/
292 /*----------------------------------------------------------------------------*/
293 
294 bool
297  const cs_matrix_t *a,
298  cs_halo_rotation_t rotation_mode,
299  const cs_real_t *rhs,
300  cs_real_t *vx);
301 
302 /*----------------------------------------------------------------------------
303  * Log sparse linear equation solver info.
304  *
305  * parameters:
306  * context <-> pointer to PETSc linear solver info
307  * (actual type: cs_sles_petsc_t *)
308  * log_type <-- log type
309  *----------------------------------------------------------------------------*/
310 
311 void
312 cs_sles_petsc_log(const void *context,
313  cs_log_t log_type);
314 
315 /*----------------------------------------------------------------------------*/
316 
318 
319 #endif /* __CS_SLES_PETSC_H__ */
bool cs_sles_petsc_error_post_and_abort(cs_sles_t *sles, cs_sles_convergence_state_t state, const cs_matrix_t *a, cs_halo_rotation_t rotation_mode, const cs_real_t *rhs, cs_real_t *vx)
Error handler for PETSc solver.
Definition: cs_sles_petsc.c:1417
struct _cs_sles_petsc_t cs_sles_petsc_t
Definition: cs_sles_petsc.h:81
cs_halo_rotation_t
Definition: cs_halo.h:60
void cs_user_sles_petsc_hook(void *context, KSP ksp)
Definition: cs_sles_petsc.c:395
#define BEGIN_C_DECLS
Definition: cs_defs.h:462
void() cs_sles_petsc_setup_hook_t(void *context, KSP ksp)
Function pointer for user settings of a PETSc KSP solver setup.
Definition: cs_sles_petsc.h:76
cs_sles_petsc_t * cs_sles_petsc_create(MatType matrix_type, cs_sles_petsc_setup_hook_t *setup_hook, void *context)
Create PETSc linear system solver info and context.
Definition: cs_sles_petsc.c:485
double cs_real_t
Floating-point value.
Definition: cs_defs.h:297
struct _cs_matrix_t cs_matrix_t
Definition: cs_matrix.h:90
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.c:632
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.c:562
cs_sles_convergence_state_t
Convergence status indicator.
Definition: cs_sles.h:56
struct _cs_sles_t cs_sles_t
Definition: cs_sles.h:68
double precision, save a
Definition: cs_fuel_incl.f90:146
void cs_sles_petsc_free(void *context)
Free PETSc linear equation solver setup context.
Definition: cs_sles_petsc.c:1370
cs_sles_petsc_t * cs_sles_petsc_define(int f_id, const char *name, MatType 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.c:439
cs_log_t
Definition: cs_log.h:48
cs_sles_convergence_state_t cs_sles_petsc_solve(void *context, const char *name, const cs_matrix_t *a, int verbosity, cs_halo_rotation_t rotation_mode, double precision, double r_norm, int *n_iter, double *residue, const cs_real_t *rhs, cs_real_t *vx, size_t aux_size, void *aux_vectors)
Call PETSc linear equation solver.
Definition: cs_sles_petsc.c:1204
#define END_C_DECLS
Definition: cs_defs.h:463
void cs_sles_petsc_destroy(void **context)
Destroy PETSc linear system solver info and context.
Definition: cs_sles_petsc.c:586
void cs_sles_petsc_log(const void *context, cs_log_t log_type)
Log sparse linear equation solver info.
Definition: cs_sles_petsc.c:1454