1 | /* |
---|
2 | * ----------------------------------------------------------------- |
---|
3 | * Programmer(s): Daniel Reynolds @ SMU |
---|
4 | * David Gardner, Carol Woodward, Slaven Peles @ LLNL |
---|
5 | * ----------------------------------------------------------------- |
---|
6 | * LLNS/SMU Copyright Start |
---|
7 | * Copyright (c) 2017, Southern Methodist University and |
---|
8 | * Lawrence Livermore National Security |
---|
9 | * |
---|
10 | * This work was performed under the auspices of the U.S. Department |
---|
11 | * of Energy by Southern Methodist University and Lawrence Livermore |
---|
12 | * National Laboratory under Contract DE-AC52-07NA27344. |
---|
13 | * Produced at Southern Methodist University and the Lawrence |
---|
14 | * Livermore National Laboratory. |
---|
15 | * |
---|
16 | * All rights reserved. |
---|
17 | * For details, see the LICENSE file. |
---|
18 | * LLNS/SMU Copyright End |
---|
19 | * ----------------------------------------------------------------- |
---|
20 | * This is the header file for a generic linear solver package. |
---|
21 | * It defines the SUNLinearSolver structure (_generic_SUNLinearSolver) |
---|
22 | * which contains the following fields: |
---|
23 | * - an implementation-dependent 'content' field which contains |
---|
24 | * any internal data required by the solver |
---|
25 | * - an 'ops' filed which contains a structure listing operations |
---|
26 | * acting on/by such solvers |
---|
27 | * |
---|
28 | * We consider both direct linear solvers and iterative linear solvers |
---|
29 | * as available implementations of this package; as a result some of |
---|
30 | * the routines are applicable only to one type of linear solver (as |
---|
31 | * noted in the comments below). |
---|
32 | * |
---|
33 | * For most of the iterative linear solvers, instead of solving the |
---|
34 | * linear system A x = b directly, we apply the underlying iterative |
---|
35 | * algorithm to the transformed system |
---|
36 | * |
---|
37 | * Abar xbar = bbar |
---|
38 | * |
---|
39 | * where |
---|
40 | * |
---|
41 | * Abar = S1 (P1-inverse) A (P2-inverse) (S2-inverse), |
---|
42 | * bbar = S1 (P1-inverse) b, |
---|
43 | * xbar = S2 P2 x, |
---|
44 | * |
---|
45 | * and where |
---|
46 | * |
---|
47 | * P1 = left preconditioner |
---|
48 | * P2 = right preconditioner |
---|
49 | * S1 = diagonal matrix of scale factors for P1-inverse b |
---|
50 | * S2 = diagonal matrix of scale factors for P2 x. |
---|
51 | * |
---|
52 | * The stopping tolerance on iterative linear solvers is on the |
---|
53 | * 2-norm of the scaled preconditioned residual: |
---|
54 | * || bbar - Abar xbar ||_2 < tol. |
---|
55 | * |
---|
56 | * We note that the Preconditioned Conjugate Gradient (PCG) solver |
---|
57 | * considers S1=S2 and P1=P2 (where each is approximately A^{1/2}), and |
---|
58 | * both scaling and preconditioning are applied symmetrically and |
---|
59 | * simultaneously, so that the user-supplied S and P are in fact |
---|
60 | * S1^2 and P1^2, i.e. P is approximately A and S is the corresponding |
---|
61 | * diagonal matrix of scale factors for P. As such, in the PCG solver the |
---|
62 | * second scaling vector and the left/right "type" of P are ignored. |
---|
63 | * |
---|
64 | * ----------------------------------------------------------------- |
---|
65 | * |
---|
66 | * Part I of this file contains enumeration constants for all |
---|
67 | * SUNDIALS-defined linear solver types, as well as a generic type for |
---|
68 | * user-supplied linear solver types. |
---|
69 | * |
---|
70 | * Part II of this file contains type declarations for the |
---|
71 | * _generic_SUNLinearSolver and _generic_SUNLinearSolver_Ops structures, |
---|
72 | * as well as references to pointers to such structures |
---|
73 | * (SUNLinearSolver). |
---|
74 | * |
---|
75 | * Part III of this file contains the prototypes for the linear solver |
---|
76 | * functions which operate on/by SUNLinearSolver objects. |
---|
77 | * |
---|
78 | * At a minimum, a particular implementation of a SUNLinearSolver must |
---|
79 | * do the following: |
---|
80 | * - specify the 'content' field of SUNLinearSolver, |
---|
81 | * - implement the operations on/by those SUNLinearSolver, |
---|
82 | * - provide a constructor routine for new SUNLinearSolver objects |
---|
83 | * |
---|
84 | * Additionally, a SUNLinearSolver implementation may provide the |
---|
85 | * following: |
---|
86 | * - "Set" routines to control solver-specific parameters/options |
---|
87 | * - "Get" routines to access solver-specific performance metrics |
---|
88 | * |
---|
89 | * Part IV of this file contains error codes that may be returned by |
---|
90 | * SUNLinearSolver objects. |
---|
91 | * |
---|
92 | * ----------------------------------------------------------------- |
---|
93 | */ |
---|
94 | |
---|
95 | #ifndef _SUNLINEARSOLVER_H |
---|
96 | #define _SUNLINEARSOLVER_H |
---|
97 | |
---|
98 | #include <sundials/sundials_types.h> |
---|
99 | #include <sundials/sundials_iterative.h> |
---|
100 | #include <sundials/sundials_matrix.h> |
---|
101 | #include <sundials/sundials_nvector.h> |
---|
102 | |
---|
103 | #ifdef __cplusplus /* wrapper to enable C++ usage */ |
---|
104 | extern "C" { |
---|
105 | #endif |
---|
106 | |
---|
107 | |
---|
108 | /* |
---|
109 | * ----------------------------------------------------------------- |
---|
110 | * I. Implemented SUNLinearSolver types: |
---|
111 | * |
---|
112 | * These type names may be modified, but a at a minimum a client |
---|
113 | * nonlinear solver and/or time integrator will want to know whether |
---|
114 | * matrix/factorization information can be reused (hence the DIRECT |
---|
115 | * and ITERATIVE types). |
---|
116 | * ----------------------------------------------------------------- |
---|
117 | */ |
---|
118 | |
---|
119 | typedef enum { |
---|
120 | SUNLINEARSOLVER_DIRECT, |
---|
121 | SUNLINEARSOLVER_ITERATIVE, |
---|
122 | SUNLINEARSOLVER_CUSTOM |
---|
123 | } SUNLinearSolver_Type; |
---|
124 | |
---|
125 | |
---|
126 | /* |
---|
127 | * ----------------------------------------------------------------- |
---|
128 | * II. Generic definition of SUNLinearSolver |
---|
129 | * ----------------------------------------------------------------- |
---|
130 | */ |
---|
131 | |
---|
132 | /* Forward reference for pointer to SUNLinearSolver_Ops object */ |
---|
133 | typedef struct _generic_SUNLinearSolver_Ops *SUNLinearSolver_Ops; |
---|
134 | |
---|
135 | /* Forward reference for pointer to SUNLinearSolver object */ |
---|
136 | typedef struct _generic_SUNLinearSolver *SUNLinearSolver; |
---|
137 | |
---|
138 | /* Structure containing function pointers to linear solver operations */ |
---|
139 | struct _generic_SUNLinearSolver_Ops { |
---|
140 | SUNLinearSolver_Type (*gettype)(SUNLinearSolver); |
---|
141 | int (*setatimes)(SUNLinearSolver, void*, ATimesFn); |
---|
142 | int (*setpreconditioner)(SUNLinearSolver, void*, |
---|
143 | PSetupFn, PSolveFn); |
---|
144 | int (*setscalingvectors)(SUNLinearSolver, |
---|
145 | N_Vector, N_Vector); |
---|
146 | int (*initialize)(SUNLinearSolver); |
---|
147 | int (*setup)(SUNLinearSolver, SUNMatrix); |
---|
148 | int (*solve)(SUNLinearSolver, SUNMatrix, N_Vector, |
---|
149 | N_Vector, realtype); |
---|
150 | int (*numiters)(SUNLinearSolver); |
---|
151 | realtype (*resnorm)(SUNLinearSolver); |
---|
152 | long int (*lastflag)(SUNLinearSolver); |
---|
153 | int (*space)(SUNLinearSolver, long int*, long int*); |
---|
154 | N_Vector (*resid)(SUNLinearSolver); |
---|
155 | int (*free)(SUNLinearSolver); |
---|
156 | }; |
---|
157 | |
---|
158 | /* A linear solver is a structure with an implementation-dependent |
---|
159 | 'content' field, and a pointer to a structure of linear solver |
---|
160 | operations corresponding to that implementation. */ |
---|
161 | struct _generic_SUNLinearSolver { |
---|
162 | void *content; |
---|
163 | struct _generic_SUNLinearSolver_Ops *ops; |
---|
164 | }; |
---|
165 | |
---|
166 | |
---|
167 | /* |
---|
168 | * ----------------------------------------------------------------- |
---|
169 | * III. Functions exported by SUNLinearSolver module |
---|
170 | * |
---|
171 | * SUNLinSolGetType |
---|
172 | * Returns an identifier for the linear solver type from |
---|
173 | * enumeration SUNLinearSolver_Type. |
---|
174 | * |
---|
175 | * SUNLinSolSetATimes (iterative methods only) |
---|
176 | * Sets the function pointer for ATimes inside of an iterative |
---|
177 | * linear solver object. This function should only be called by a |
---|
178 | * main integrator, who will either provide this via |
---|
179 | * difference-quotients and vector operations, or by translating |
---|
180 | * between the generic ATimes call and the integrator-specific, |
---|
181 | * user-supplied routines. This should return zero for a |
---|
182 | * successful call, and a negative value for a failure. Ideally, |
---|
183 | * this should return one of the generic SUNLS_* error codes |
---|
184 | * listed at the bottom of this file. |
---|
185 | * |
---|
186 | * SUNLinSolSetPreconditioner (iterative methods only) |
---|
187 | * Sets function pointers for PSetup and PSolve routines inside |
---|
188 | * of iterative linear solver objects. This function should only |
---|
189 | * be called by a main integrator, who will provide translation |
---|
190 | * between the generic PSetup and PSolve calls and the integrator- |
---|
191 | * specific user-supplied routines. This should return |
---|
192 | * zero for a successful call, and a negative value for a failure. |
---|
193 | * Ideally, this should return one of the generic SUNLS_* error |
---|
194 | * codes listed at the bottom of this file. |
---|
195 | * |
---|
196 | * SUNLinSolSetScalingVectors (iterative methods only) |
---|
197 | * Sets pointers to left/right scaling vectors for the linear |
---|
198 | * system solve. Here, s1 is an N_Vector of positive scale factors |
---|
199 | * for P1-inv b, where P1 is the left preconditioner (the vector is |
---|
200 | * not tested for positivity). Pass NULL if no scaling on P1-inv b |
---|
201 | * is required. Similarly, s2 is an N_Vector of positive scale |
---|
202 | * factors for P2 x, where P2 is the right preconditioner (again |
---|
203 | * not tested for positivity). Pass NULL if no scaling on P2 x is |
---|
204 | * required. This should return zero for a successful call, and a |
---|
205 | * negative value for a failure. Ideally, this should return one of |
---|
206 | * the generic SUNLS_* error codes listed at the bottom of this file. |
---|
207 | * |
---|
208 | * SUNLinSolInitialize |
---|
209 | * Performs linear solver initialization (assumes that all |
---|
210 | * solver-specific options have been set). This should return |
---|
211 | * zero for a successful call, and a negative value for a failure. |
---|
212 | * Ideally, this should return one of the generic SUNLS_* error |
---|
213 | * codes listed at the bottom of this file. |
---|
214 | * |
---|
215 | * SUNLinSolSetup |
---|
216 | * Performs any linear solver setup needed, based on an updated |
---|
217 | * system matrix A. This may be called frequently (e.g. with a |
---|
218 | * full Newton method) or infrequently (for a modified Newton |
---|
219 | * method), based on the type of integrator and/or nonlinear |
---|
220 | * solver requesting the solves. This should return |
---|
221 | * zero for a successful call, a positive value for a recoverable |
---|
222 | * failure and a negative value for an unrecoverable failure. |
---|
223 | * Ideally, this should return one of the generic SUNLS_* error |
---|
224 | * codes listed at the bottom of this file. |
---|
225 | * |
---|
226 | * SUNLinSolSolve |
---|
227 | * Solves a linear system A*x = b. If the solver is scaled, it |
---|
228 | * uses the supplied scaling vectors. If the solver is iterative, |
---|
229 | * it attempts to solve to the specified tolerance (weighted |
---|
230 | * 2-norm). If the solver is direct it ignores the input tolerance |
---|
231 | * and scaling vectors, and if the solver does not support scaling |
---|
232 | * then it should just use a 2-norm. This should return zero for a |
---|
233 | * successful call, a positive value for a recoverable failure and |
---|
234 | * a negative value for an unrecoverable failure. Ideally, this |
---|
235 | * should return one of the generic SUNLS_* error codes listed at |
---|
236 | * the bottom of this file. |
---|
237 | * |
---|
238 | * SUNLinSolNumIters (iterative methods only) |
---|
239 | * Returns the number of linear iterations performed in the last |
---|
240 | * 'Solve' call. |
---|
241 | * |
---|
242 | * SUNLinSolResNorm (iterative methods only) |
---|
243 | * Returns the final residual norm from the last 'Solve' call. |
---|
244 | * |
---|
245 | * SUNLinSolResid (iterative methods only) |
---|
246 | * If an iterative method computes the preconditioned initial |
---|
247 | * residual and returns with a successful solve without performing |
---|
248 | * any iterations (i.e. either the initial guess or the |
---|
249 | * preconditioner is sufficiently accurate), then this |
---|
250 | * function may be called. It should return the N_Vector |
---|
251 | * containing the preconditioned initial residual. |
---|
252 | * |
---|
253 | * SUNLinSolLastFlag (optional) |
---|
254 | * Returns the last error flag encountered within the linear solver, |
---|
255 | * allowing the user to investigate linear solver issues after |
---|
256 | * failed solves. |
---|
257 | * |
---|
258 | * SUNLinSolSpace (optional) |
---|
259 | * Returns the integer and real workspace sizes for the linear |
---|
260 | * solver. |
---|
261 | * |
---|
262 | * SUNLinSolFree |
---|
263 | * Frees memory allocated by the linear solver. This should return |
---|
264 | * zero for a successful call, and a negative value for a failure. |
---|
265 | * |
---|
266 | * --------------------------------------------------------------- |
---|
267 | * |
---|
268 | * The following table lists the linear solver functions used by |
---|
269 | * different modules in SUNDIALS. The symbols in the table have |
---|
270 | * The following meaning: |
---|
271 | * M - called by the modified Newton nonlinear solver |
---|
272 | * I - called by the inexact Newton nonlinear solver |
---|
273 | * P - called by the Picard nonlinear solver |
---|
274 | * S - called by the integrator directly (and non-identity mass matrix) |
---|
275 | * |
---|
276 | * LinearSolver |
---|
277 | * Functions CVODE(S) ARKode IDA(S) KINSOL |
---|
278 | * ------------------------------------------------------------ |
---|
279 | * GetType M I P# M I P# M I P# M I P |
---|
280 | * SetATimes I I S+ I I |
---|
281 | * SetPreconditioner I* I* S* I* I* |
---|
282 | * SetScalingVectors I I S+ I I |
---|
283 | * Initialize M I P# M I P# S M I P# M I P |
---|
284 | * Setup M I P# M I P# S M I P# M I P |
---|
285 | * Solve M I P# M I P# S M I P# M I P |
---|
286 | * NumIters I I S+ I I |
---|
287 | * ResNorm I I S+ I I |
---|
288 | * Resid I |
---|
289 | * LastFlag^ |
---|
290 | * Space M I P# M I P# S M I P# M I P |
---|
291 | * Free M I P# M I P# S M I P# M I P |
---|
292 | * ------------------------------------------------------------ |
---|
293 | * Notes: * -- only if user calls integrator-specific |
---|
294 | * preconditioner "set" routine |
---|
295 | * + -- only called when using a non-identity mass matrix |
---|
296 | * with an iterative linear solver |
---|
297 | * # -- planned (currently only available for KINSOL) |
---|
298 | * ^ -- available for users to diagnose solver failures |
---|
299 | * --------------------------------------------------------------- |
---|
300 | */ |
---|
301 | |
---|
302 | SUNDIALS_EXPORT SUNLinearSolver_Type SUNLinSolGetType(SUNLinearSolver S); |
---|
303 | |
---|
304 | SUNDIALS_EXPORT int SUNLinSolSetATimes(SUNLinearSolver S, void* A_data, |
---|
305 | ATimesFn ATimes); |
---|
306 | |
---|
307 | SUNDIALS_EXPORT int SUNLinSolSetPreconditioner(SUNLinearSolver S, void* P_data, |
---|
308 | PSetupFn Pset, PSolveFn Psol); |
---|
309 | |
---|
310 | SUNDIALS_EXPORT int SUNLinSolSetScalingVectors(SUNLinearSolver S, N_Vector s1, |
---|
311 | N_Vector s2); |
---|
312 | |
---|
313 | SUNDIALS_EXPORT int SUNLinSolInitialize(SUNLinearSolver S); |
---|
314 | |
---|
315 | SUNDIALS_EXPORT int SUNLinSolSetup(SUNLinearSolver S, SUNMatrix A); |
---|
316 | |
---|
317 | SUNDIALS_EXPORT int SUNLinSolSolve(SUNLinearSolver S, SUNMatrix A, N_Vector x, |
---|
318 | N_Vector b, realtype tol); |
---|
319 | |
---|
320 | SUNDIALS_EXPORT int SUNLinSolNumIters(SUNLinearSolver S); |
---|
321 | |
---|
322 | SUNDIALS_EXPORT realtype SUNLinSolResNorm(SUNLinearSolver S); |
---|
323 | |
---|
324 | SUNDIALS_EXPORT N_Vector SUNLinSolResid(SUNLinearSolver S); |
---|
325 | |
---|
326 | SUNDIALS_EXPORT long int SUNLinSolLastFlag(SUNLinearSolver S); |
---|
327 | |
---|
328 | SUNDIALS_EXPORT int SUNLinSolSpace(SUNLinearSolver S, long int *lenrwLS, |
---|
329 | long int *leniwLS); |
---|
330 | |
---|
331 | SUNDIALS_EXPORT int SUNLinSolFree(SUNLinearSolver S); |
---|
332 | |
---|
333 | |
---|
334 | /* |
---|
335 | * ----------------------------------------------------------------- |
---|
336 | * IV. SUNLinearSolver error codes |
---|
337 | * --------------------------------------------------------------- |
---|
338 | */ |
---|
339 | |
---|
340 | #define SUNLS_SUCCESS 0 /* successful/converged */ |
---|
341 | |
---|
342 | #define SUNLS_MEM_NULL -1 /* mem argument is NULL */ |
---|
343 | #define SUNLS_ILL_INPUT -2 /* illegal function input */ |
---|
344 | #define SUNLS_MEM_FAIL -3 /* failed memory access */ |
---|
345 | #define SUNLS_ATIMES_FAIL_UNREC -4 /* atimes unrecoverable failure */ |
---|
346 | #define SUNLS_PSET_FAIL_UNREC -5 /* pset unrecoverable failure */ |
---|
347 | #define SUNLS_PSOLVE_FAIL_UNREC -6 /* psolve unrecoverable failure */ |
---|
348 | #define SUNLS_PACKAGE_FAIL_UNREC -7 /* external package unrec. fail */ |
---|
349 | #define SUNLS_GS_FAIL -8 /* Gram-Schmidt failure */ |
---|
350 | #define SUNLS_QRSOL_FAIL -9 /* QRsol found singular R */ |
---|
351 | |
---|
352 | #define SUNLS_RES_REDUCED 1 /* nonconv. solve, resid reduced */ |
---|
353 | #define SUNLS_CONV_FAIL 2 /* nonconvergent solve */ |
---|
354 | #define SUNLS_ATIMES_FAIL_REC 3 /* atimes failed recoverably */ |
---|
355 | #define SUNLS_PSET_FAIL_REC 4 /* pset failed recoverably */ |
---|
356 | #define SUNLS_PSOLVE_FAIL_REC 5 /* psolve failed recoverably */ |
---|
357 | #define SUNLS_PACKAGE_FAIL_REC 6 /* external package recov. fail */ |
---|
358 | #define SUNLS_QRFACT_FAIL 7 /* QRfact found singular matrix */ |
---|
359 | #define SUNLS_LUFACT_FAIL 8 /* LUfact found singular matrix */ |
---|
360 | |
---|
361 | #ifdef __cplusplus |
---|
362 | } |
---|
363 | #endif |
---|
364 | #endif |
---|