sundials_spfgmr.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* -----------------------------------------------------------------
  2. Programmer(s): Daniel R. Reynolds and Hilari C. Tiedeman @ SMU
  3. -------------------------------------------------------------------
  4. Copyright (c) 2011, Southern Methodist University.
  5. All rights reserved.
  6. For details, see the LICENSE file.
  7. -------------------------------------------------------------------
  8. This is the header file for the implementation of SPFGMR Krylov
  9. iterative linear solver. The SPFGMR algorithm is based on the
  10. Scaled Preconditioned Flexible GMRES (Generalized Minimal Residual)
  11. method [Y. Saad, SIAM J. Sci. Comput., 1993].
  12. The SPFGMR algorithm solves a linear system A x = b.
  13. Preconditioning is only allowed on the right.
  14. Scaling is allowed on the right, and restarts are also allowed.
  15. We denote the preconditioner and scaling matrices as follows:
  16. P = right preconditioner
  17. S1 = diagonal matrix of scale factors for P-inverse b
  18. S2 = diagonal matrix of scale factors for x
  19. The matrices A and P are not required explicitly; only
  20. routines that provide A and P-inverse as operators are required.
  21. In this notation, SPFGMR applies the underlying FGMRES method to
  22. the equivalent transformed system
  23. Abar xbar = bbar , where
  24. Abar = S1 A (P-inverse) (S2-inverse),
  25. bbar = S1 b , and xbar = S2 P x .
  26. The scaling matrix must be chosen so that the vectors S1 b and
  27. S2 P x have dimensionless components. If preconditioning is not
  28. performed (P = I), then S2 must be a scaling for x, while S1 is a
  29. scaling for b. Similarly, if preconditioning is performed, then S1
  30. must be a scaling for b, while S2 is a scaling for P x, and may
  31. also be taken as a scaling for b.
  32. The stopping test for the SPFGMR iterations is on the L2 norm of
  33. the scaled preconditioned residual:
  34. || bbar - Abar xbar ||_2 < delta
  35. with an input test constant delta.
  36. The usage of this SPFGMR solver involves supplying two routines
  37. and making three calls. The user-supplied routines are
  38. atimes (A_data, x, y) to compute y = A x, given x,
  39. and
  40. psolve (P_data, y, x, lr)
  41. to solve P x = y for x, given y.
  42. The three user calls are:
  43. mem = SpfgmrMalloc(lmax, vec_tmpl);
  44. to initialize memory,
  45. flag = SpfgmrSolve(mem,A_data,x,b,...,
  46. P_data,s1,s2,atimes,psolve,...);
  47. to solve the system, and
  48. SpfgmrFree(mem);
  49. to free the memory created by SpfgmrMalloc.
  50. Complete details for specifying atimes and psolve and for the
  51. usage calls are given in the paragraphs below and in iterative.h.
  52. -----------------------------------------------------------------*/
  53. #ifndef _SPFGMR_H
  54. #define _SPFGMR_H
  55. #include <sundials/sundials_iterative.h>
  56. #ifdef __cplusplus /* wrapper to enable C++ usage */
  57. extern "C" {
  58. #endif
  59. /* -----------------------------------------------------------------
  60. Types: SpfgmrMemRec, SpfgmrMem
  61. -------------------------------------------------------------------
  62. SpfgmrMem is a pointer to an SpfgmrMemRec which contains
  63. the memory needed by SpfgmrSolve. The SpfgmrMalloc routine
  64. returns a pointer of type SpfgmrMem which should then be passed
  65. in subsequent calls to SpfgmrSolve. The SpfgmrFree routine frees
  66. the memory allocated by SpfgmrMalloc.
  67. l_max is the maximum Krylov dimension that SpfgmrSolve will be
  68. permitted to use.
  69. V is the array of Krylov basis vectors v_1, ..., v_(l_max+1),
  70. stored in V[0], ..., V[l_max], where l_max is the second
  71. parameter to SpfgmrMalloc. Each v_i is a vector of type
  72. N_Vector.
  73. Z is the array of preconditioned basis vectors z_1, ...,
  74. z_(l_max+1), stored in Z[0], ..., Z[l_max], where l_max is the
  75. second parameter to SpfgmrMalloc. Each z_i is a vector of type
  76. N_Vector.
  77. Hes is the (l_max+1) x l_max Hessenberg matrix. It is stored
  78. row-wise so that the (i,j)th element is given by Hes[i][j].
  79. givens is a length 2*l_max array which represents the
  80. Givens rotation matrices that arise in the algorithm. The
  81. Givens rotation matrices F_0, F_1, ..., F_j, where F_i is
  82. 1
  83. 1
  84. c_i -s_i <--- row i
  85. s_i c_i
  86. 1
  87. 1
  88. are represented in the givens vector as
  89. givens[0]=c_0, givens[1]=s_0, givens[2]=c_1, givens[3]=s_1,
  90. ..., givens[2j]=c_j, givens[2j+1]=s_j.
  91. xcor is a vector (type N_Vector) which holds the scaled,
  92. preconditioned correction to the initial guess.
  93. yg is a length (l_max+1) array of realtype used to hold "short"
  94. vectors (e.g. y and g).
  95. vtemp is a vector (type N_Vector) used as temporary vector
  96. storage during calculations.
  97. -----------------------------------------------------------------*/
  98. typedef struct _SpfgmrMemRec {
  99. int l_max;
  100. N_Vector *V;
  101. N_Vector *Z;
  102. realtype **Hes;
  103. realtype *givens;
  104. N_Vector xcor;
  105. realtype *yg;
  106. N_Vector vtemp;
  107. } SpfgmrMemRec, *SpfgmrMem;
  108. /*----------------------------------------------------------------
  109. Function : SpfgmrMalloc
  110. -----------------------------------------------------------------
  111. SpfgmrMalloc allocates the memory used by SpfgmrSolve. It
  112. returns a pointer of type SpfgmrMem which the user of the
  113. SPGMR package should pass to SpfgmrSolve. The parameter l_max
  114. is the maximum Krylov dimension that SpfgmrSolve will be
  115. permitted to use. The parameter vec_tmpl is a pointer to an
  116. N_Vector used as a template to create new vectors by duplication.
  117. This routine returns NULL if there is a memory request failure.
  118. ---------------------------------------------------------------*/
  119. SUNDIALS_EXPORT SpfgmrMem SpfgmrMalloc(int l_max, N_Vector vec_tmpl);
  120. /*----------------------------------------------------------------
  121. Function : SpfgmrSolve
  122. -----------------------------------------------------------------
  123. SpfgmrSolve solves the linear system Ax = b using the SPFGMR
  124. method. The return values are given by the symbolic constants
  125. below. The first SpfgmrSolve parameter is a pointer to memory
  126. allocated by a prior call to SpfgmrMalloc.
  127. mem is the pointer returned by SpfgmrMalloc to the structure
  128. containing the memory needed by SpfgmrSolve.
  129. A_data is a pointer to information about the coefficient
  130. matrix A. This pointer is passed to the user-supplied function
  131. atimes.
  132. x is the initial guess x_0 upon entry and the solution
  133. N_Vector upon exit with return value SPFGMR_SUCCESS or
  134. SPFGMR_RES_REDUCED. For all other return values, the output x
  135. is undefined.
  136. b is the right hand side N_Vector. It is undisturbed by this
  137. function.
  138. pretype is the type of preconditioning to be used. Its
  139. legal possible values are enumerated in iterative.h. These
  140. values are PREC_NONE, PREC_LEFT, PREC_RIGHT and PREC_BOTH;
  141. however since this solver can only precondition on the right,
  142. then right-preconditioning will be done if any of the values
  143. PREC_LEFT, PREC_RIGHT or PREC_BOTH are provided..
  144. gstype is the type of Gram-Schmidt orthogonalization to be
  145. used. Its legal values are enumerated in iterativ.h. These
  146. values are MODIFIED_GS=0 and CLASSICAL_GS=1.
  147. delta is the tolerance on the L2 norm of the scaled,
  148. preconditioned residual. On return with value SPFGMR_SUCCESS,
  149. this residual satisfies || s1 P1_inv (b - Ax) ||_2 <= delta.
  150. max_restarts is the maximum number of times the algorithm is
  151. allowed to restart.
  152. maxit is the maximum number of iterations allowed within the
  153. solve. This value must be less than or equal to the "l_max"
  154. value previously supplied to SpfgmrMalloc. If maxit is too
  155. large, l_max will be used instead.
  156. P_data is a pointer to preconditioner information. This
  157. pointer is passed to the user-supplied function psolve.
  158. s1 is an N_Vector of positive scale factors for b. (Not
  159. tested for positivity.) Pass NULL if no scaling on b is
  160. required.
  161. s2 is an N_Vector of positive scale factors for P x, where
  162. P is the right preconditioner. (Not tested for positivity.)
  163. Pass NULL if no scaling on P x is required.
  164. atimes is the user-supplied function which performs the
  165. operation of multiplying A by a given vector. Its description
  166. is given in iterative.h.
  167. psolve is the user-supplied function which solves a
  168. preconditioner system Pz = r, where P is P1 or P2. Its full
  169. description is given in iterative.h. The psolve function will
  170. not be called if pretype is NONE; in that case, the user
  171. should pass NULL for psolve.
  172. res_norm is a pointer to the L2 norm of the scaled,
  173. preconditioned residual. On return with value SPFGMR_SUCCESS or
  174. SPFGMR_RES_REDUCED, (*res_norm) contains the value
  175. || s1 (b - Ax) ||_2 for the computed solution x.
  176. For all other return values, (*res_norm) is undefined. The
  177. caller is responsible for allocating the memory (*res_norm)
  178. to be filled in by SpfgmrSolve.
  179. nli is a pointer to the number of linear iterations done in
  180. the execution of SpfgmrSolve. The caller is responsible for
  181. allocating the memory (*nli) to be filled in by SpfgmrSolve.
  182. nps is a pointer to the number of calls made to psolve during
  183. the execution of SpfgmrSolve. The caller is responsible for
  184. allocating the memory (*nps) to be filled in by SpfgmrSolve.
  185. Note: Repeated calls can be made to SpfgmrSolve with varying
  186. input arguments. If, however, the problem size N or the
  187. maximum Krylov dimension l_max changes, then a call to
  188. SpfgmrMalloc must be made to obtain new memory for SpfgmrSolve
  189. to use.
  190. ---------------------------------------------------------------*/
  191. SUNDIALS_EXPORT int SpfgmrSolve(SpfgmrMem mem, void *A_data, N_Vector x,
  192. N_Vector b, int pretype, int gstype,
  193. realtype delta, int max_restarts,
  194. int maxit, void *P_data, N_Vector s1,
  195. N_Vector s2, ATimesFn atimes, PSolveFn psolve,
  196. realtype *res_norm, int *nli, int *nps);
  197. /* Return values for SpfgmrSolve */
  198. #define SPFGMR_SUCCESS 0 /* Converged */
  199. #define SPFGMR_RES_REDUCED 1 /* Did not converge, but reduced
  200. norm of residual */
  201. #define SPFGMR_CONV_FAIL 2 /* Failed to converge */
  202. #define SPFGMR_QRFACT_FAIL 3 /* QRfact found singular matrix */
  203. #define SPFGMR_PSOLVE_FAIL_REC 4 /* psolve failed recoverably */
  204. #define SPFGMR_ATIMES_FAIL_REC 5 /* atimes failed recoverably */
  205. #define SPFGMR_PSET_FAIL_REC 6 /* pset faild recoverably */
  206. #define SPFGMR_MEM_NULL -1 /* mem argument is NULL */
  207. #define SPFGMR_ATIMES_FAIL_UNREC -2 /* atimes returned failure flag */
  208. #define SPFGMR_PSOLVE_FAIL_UNREC -3 /* psolve failed unrecoverably */
  209. #define SPFGMR_GS_FAIL -4 /* Gram-Schmidt routine faiuled */
  210. #define SPFGMR_QRSOL_FAIL -5 /* QRsol found singular R */
  211. #define SPFGMR_PSET_FAIL_UNREC -6 /* pset failed unrecoverably */
  212. /*----------------------------------------------------------------
  213. Function : SpfgmrFree
  214. -----------------------------------------------------------------
  215. SpfgmrMalloc frees the memory allocated by SpfgmrMalloc. It is
  216. illegal to use the pointer mem after a call to SpfgmrFree.
  217. ---------------------------------------------------------------*/
  218. SUNDIALS_EXPORT void SpfgmrFree(SpfgmrMem mem);
  219. /*----------------------------------------------------------------
  220. Macro: SPFGMR_VTEMP
  221. -----------------------------------------------------------------
  222. This macro provides access to the work vector vtemp in the
  223. memory block of the SPFGMR module. The argument mem is the
  224. memory pointer returned by SpfgmrMalloc, of type SpfgmrMem,
  225. and the macro value is of type N_Vector.
  226. On a return from SpfgmrSolve with *nli = 0, this vector
  227. contains the scaled preconditioned initial residual,
  228. s1 * P1_inverse * (b - A x_0).
  229. ---------------------------------------------------------------*/
  230. #define SPFGMR_VTEMP(mem) (mem->vtemp)
  231. #ifdef __cplusplus
  232. }
  233. #endif
  234. #endif