kinsol_direct.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * -----------------------------------------------------------------
  3. * $Revision: 4378 $
  4. * $Date: 2015-02-19 10:55:14 -0800 (Thu, 19 Feb 2015) $
  5. * -----------------------------------------------------------------
  6. * Programmer: Radu Serban @ LLNL
  7. * -----------------------------------------------------------------
  8. * LLNS Copyright Start
  9. * Copyright (c) 2014, Lawrence Livermore National Security
  10. * This work was performed under the auspices of the U.S. Department
  11. * of Energy by Lawrence Livermore National Laboratory in part under
  12. * Contract W-7405-Eng-48 and in part under Contract DE-AC52-07NA27344.
  13. * Produced at the Lawrence Livermore National Laboratory.
  14. * All rights reserved.
  15. * For details, see the LICENSE file.
  16. * LLNS Copyright End
  17. * -----------------------------------------------------------------
  18. * Common header file for the direct linear solvers in KINSOL.
  19. * -----------------------------------------------------------------
  20. */
  21. #ifndef _KINDLS_H
  22. #define _KINDLS_H
  23. #include <sundials/sundials_direct.h>
  24. #include <sundials/sundials_nvector.h>
  25. #ifdef __cplusplus /* wrapper to enable C++ usage */
  26. extern "C" {
  27. #endif
  28. /*
  29. * =================================================================
  30. * K I N D I R E C T C O N S T A N T S
  31. * =================================================================
  32. */
  33. /*
  34. * -----------------------------------------------------------------
  35. * KINDLS return values
  36. * -----------------------------------------------------------------
  37. */
  38. #define KINDLS_SUCCESS 0
  39. #define KINDLS_MEM_NULL -1
  40. #define KINDLS_LMEM_NULL -2
  41. #define KINDLS_ILL_INPUT -3
  42. #define KINDLS_MEM_FAIL -4
  43. /* Additional last_flag values */
  44. #define KINDLS_JACFUNC_UNRECVR -5
  45. #define KINDLS_JACFUNC_RECVR -6
  46. /*
  47. * =================================================================
  48. * F U N C T I O N T Y P E S
  49. * =================================================================
  50. */
  51. /*
  52. * -----------------------------------------------------------------
  53. * Type: KINDlsDenseJacFn
  54. * -----------------------------------------------------------------
  55. *
  56. * A dense Jacobian approximation function Jac must be of type
  57. * KINDlsDenseJacFn. Its parameters are:
  58. *
  59. * N - problem size.
  60. *
  61. * u - current iterate (unscaled) [input]
  62. *
  63. * fu - vector (type N_Vector) containing result of nonlinear
  64. * system function evaluated at current iterate:
  65. * fu = F(u) [input]
  66. *
  67. * J - dense matrix (of type DlsMat) that will be loaded
  68. * by a KINDlsDenseJacFn with an approximation to the
  69. * Jacobian matrix J = (dF_i/dy_j).
  70. *
  71. * user_data - pointer to user data - the same as the user_data
  72. * parameter passed to KINSetFdata.
  73. *
  74. * tmp1, tmp2 - available scratch vectors (volatile storage)
  75. *
  76. * A KINDlsDenseJacFn should return 0 if successful, a positive
  77. * value if a recoverable error occurred, and a negative value if
  78. * an unrecoverable error occurred.
  79. *
  80. * -----------------------------------------------------------------
  81. *
  82. * NOTE: The following are two efficient ways to load a dense Jac:
  83. * (1) (with macros - no explicit data structure references)
  84. * for (j=0; j < Neq; j++) {
  85. * col_j = DENSE_COL(Jac,j);
  86. * for (i=0; i < Neq; i++) {
  87. * generate J_ij = the (i,j)th Jacobian element
  88. * col_j[i] = J_ij;
  89. * }
  90. * }
  91. * (2) (without macros - explicit data structure references)
  92. * for (j=0; j < Neq; j++) {
  93. * col_j = (Jac->data)[j];
  94. * for (i=0; i < Neq; i++) {
  95. * generate J_ij = the (i,j)th Jacobian element
  96. * col_j[i] = J_ij;
  97. * }
  98. * }
  99. * A third way, using the DENSE_ELEM(A,i,j) macro, is much less
  100. * efficient in general. It is only appropriate for use in small
  101. * problems in which efficiency of access is NOT a major concern.
  102. *
  103. * -----------------------------------------------------------------
  104. */
  105. typedef int (*KINDlsDenseJacFn)(long int N,
  106. N_Vector u, N_Vector fu,
  107. DlsMat J, void *user_data,
  108. N_Vector tmp1, N_Vector tmp2);
  109. /*
  110. * -----------------------------------------------------------------
  111. * Type: KINDlsBandJacFn
  112. * -----------------------------------------------------------------
  113. *
  114. * A band Jacobian approximation function Jac must have the
  115. * prototype given below. Its parameters are:
  116. *
  117. * N is the problem size
  118. *
  119. * mupper is the upper half-bandwidth of the approximate banded
  120. * Jacobian. This parameter is the same as the mupper parameter
  121. * passed by the user to the linear solver initialization function.
  122. *
  123. * mlower is the lower half-bandwidth of the approximate banded
  124. * Jacobian. This parameter is the same as the mlower parameter
  125. * passed by the user to the linear solver initialization function.
  126. *
  127. * u - current iterate (unscaled) [input]
  128. *
  129. * fu - vector (type N_Vector) containing result of nonlinear
  130. * system function evaluated at current iterate:
  131. * fu = F(uu) [input]
  132. *
  133. * J - band matrix (of type DlsMat) that will be loaded by a
  134. * KINDlsBandJacFn with an approximation to the Jacobian
  135. * matrix Jac = (dF_i/dy_j).
  136. *
  137. * user_data - pointer to user data - the same as the user_data
  138. * parameter passed to KINSetFdata.
  139. *
  140. * tmp1, tmp2 - available scratch vectors (volatile storage)
  141. *
  142. * A KINDlsBandJacFn should return 0 if successful, a positive value
  143. * if a recoverable error occurred, and a negative value if an
  144. * unrecoverable error occurred.
  145. *
  146. * -----------------------------------------------------------------
  147. *
  148. * NOTE. Three efficient ways to load J are:
  149. *
  150. * (1) (with macros - no explicit data structure references)
  151. * for (j=0; j < n; j++) {
  152. * col_j = BAND_COL(Jac,j);
  153. * for (i=j-mupper; i <= j+mlower; i++) {
  154. * generate J_ij = the (i,j)th Jacobian element
  155. * BAND_COL_ELEM(col_j,i,j) = J_ij;
  156. * }
  157. * }
  158. *
  159. * (2) (with BAND_COL macro, but without BAND_COL_ELEM macro)
  160. * for (j=0; j < n; j++) {
  161. * col_j = BAND_COL(Jac,j);
  162. * for (k=-mupper; k <= mlower; k++) {
  163. * generate J_ij = the (i,j)th Jacobian element, i=j+k
  164. * col_j[k] = J_ij;
  165. * }
  166. * }
  167. *
  168. * (3) (without macros - explicit data structure references)
  169. * offset = Jac->smu;
  170. * for (j=0; j < n; j++) {
  171. * col_j = ((Jac->data)[j])+offset;
  172. * for (k=-mupper; k <= mlower; k++) {
  173. * generate J_ij = the (i,j)th Jacobian element, i=j+k
  174. * col_j[k] = J_ij;
  175. * }
  176. * }
  177. * Caution: Jac->smu is generally NOT the same as mupper.
  178. *
  179. * The BAND_ELEM(A,i,j) macro is appropriate for use in small
  180. * problems in which efficiency of access is NOT a major concern.
  181. *
  182. * -----------------------------------------------------------------
  183. */
  184. typedef int (*KINDlsBandJacFn)(long int N, long int mupper, long int mlower,
  185. N_Vector u, N_Vector fu,
  186. DlsMat J, void *user_data,
  187. N_Vector tmp1, N_Vector tmp2);
  188. /*
  189. * =================================================================
  190. * E X P O R T E D F U N C T I O N S
  191. * =================================================================
  192. */
  193. /*
  194. * -----------------------------------------------------------------
  195. * Optional inputs to the KINDLS linear solver
  196. * -----------------------------------------------------------------
  197. *
  198. * KINDlsSetDenseJacFn specifies the dense Jacobian approximation
  199. * routine to be used for a direct dense linear solver.
  200. *
  201. * KINDlsSetBandJacFn specifies the band Jacobian approximation
  202. * routine to be used for a direct band linear solver.
  203. *
  204. * By default, a difference quotient approximation, supplied with
  205. * the solver is used.
  206. *
  207. * The return value is one of:
  208. * KINDLS_SUCCESS if successful
  209. * KINDLS_MEM_NULL if the KINSOL memory was NULL
  210. * KINDLS_LMEM_NULL if the linear solver memory was NULL
  211. * -----------------------------------------------------------------
  212. */
  213. SUNDIALS_EXPORT int KINDlsSetDenseJacFn(void *kinmem, KINDlsDenseJacFn jac);
  214. SUNDIALS_EXPORT int KINDlsSetBandJacFn(void *kinmem, KINDlsBandJacFn jac);
  215. /*
  216. * -----------------------------------------------------------------
  217. * Optional outputs from a KINDLS linear solver
  218. * -----------------------------------------------------------------
  219. *
  220. * KINDlsGetWorkSpace returns the real and integer workspace used
  221. * by the KINDLS linear solver.
  222. * KINDlsGetNumJacEvals returns the number of calls made to the
  223. * Jacobian evaluation routine.
  224. * KINDlsGetNumFuncEvals returns the number of calls to the user's F
  225. * routine due to finite difference Jacobian
  226. * evaluation.
  227. * KINDlsGetLastFlag returns the last error flag set by any of
  228. * the KINDLS interface functions.
  229. * KINDlsGetReturnFlagName returns the name of the constant
  230. * associated with a KINDLS return flag
  231. *
  232. * The return value of KINDlsGet* is one of:
  233. * KINDLS_SUCCESS if successful
  234. * KINDLS_MEM_NULL if the KINSOL memory was NULL
  235. * KINDLS_LMEM_NULL if the linear solver memory was NULL
  236. * -----------------------------------------------------------------
  237. */
  238. SUNDIALS_EXPORT int KINDlsGetWorkSpace(void *kinmem, long int *lenrwB, long int *leniwB);
  239. SUNDIALS_EXPORT int KINDlsGetNumJacEvals(void *kinmem, long int *njevalsB);
  240. SUNDIALS_EXPORT int KINDlsGetNumFuncEvals(void *kinmem, long int *nfevalsB);
  241. SUNDIALS_EXPORT int KINDlsGetLastFlag(void *kinmem, long int *flag);
  242. SUNDIALS_EXPORT char *KINDlsGetReturnFlagName(long int flag);
  243. #ifdef __cplusplus
  244. }
  245. #endif
  246. #endif