cvode_direct.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 CVODE.
  19. * -----------------------------------------------------------------
  20. */
  21. #ifndef _CVDLS_H
  22. #define _CVDLS_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. * C V D I R E C T C O N S T A N T S
  31. * =================================================================
  32. */
  33. /*
  34. * -----------------------------------------------------------------
  35. * CVDLS return values
  36. * -----------------------------------------------------------------
  37. */
  38. #define CVDLS_SUCCESS 0
  39. #define CVDLS_MEM_NULL -1
  40. #define CVDLS_LMEM_NULL -2
  41. #define CVDLS_ILL_INPUT -3
  42. #define CVDLS_MEM_FAIL -4
  43. /* Additional last_flag values */
  44. #define CVDLS_JACFUNC_UNRECVR -5
  45. #define CVDLS_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: CVDlsDenseJacFn
  54. * -----------------------------------------------------------------
  55. *
  56. * A dense Jacobian approximation function Jac must be of type
  57. * CVDlsDenseJacFn. Its parameters are:
  58. *
  59. * N is the problem size.
  60. *
  61. * Jac is the dense matrix (of type DlsMat) that will be loaded
  62. * by a CVDlsDenseJacFn with an approximation to the Jacobian
  63. * matrix J = (df_i/dy_j) at the point (t,y).
  64. *
  65. * t is the current value of the independent variable.
  66. *
  67. * y is the current value of the dependent variable vector,
  68. * namely the predicted value of y(t).
  69. *
  70. * fy is the vector f(t,y).
  71. *
  72. * user_data is a pointer to user data - the same as the user_data
  73. * parameter passed to CVodeSetFdata.
  74. *
  75. * tmp1, tmp2, and tmp3 are pointers to memory allocated for
  76. * vectors of length N which can be used by a CVDlsDenseJacFn
  77. * as temporary storage or work space.
  78. *
  79. * A CVDlsDenseJacFn should return 0 if successful, a positive
  80. * value if a recoverable error occurred, and a negative value if
  81. * an unrecoverable error occurred.
  82. *
  83. * -----------------------------------------------------------------
  84. *
  85. * NOTE: The following are two efficient ways to load a dense Jac:
  86. * (1) (with macros - no explicit data structure references)
  87. * for (j=0; j < Neq; j++) {
  88. * col_j = DENSE_COL(Jac,j);
  89. * for (i=0; i < Neq; i++) {
  90. * generate J_ij = the (i,j)th Jacobian element
  91. * col_j[i] = J_ij;
  92. * }
  93. * }
  94. * (2) (without macros - explicit data structure references)
  95. * for (j=0; j < Neq; j++) {
  96. * col_j = (Jac->data)[j];
  97. * for (i=0; i < Neq; i++) {
  98. * generate J_ij = the (i,j)th Jacobian element
  99. * col_j[i] = J_ij;
  100. * }
  101. * }
  102. * A third way, using the DENSE_ELEM(A,i,j) macro, is much less
  103. * efficient in general. It is only appropriate for use in small
  104. * problems in which efficiency of access is NOT a major concern.
  105. *
  106. * NOTE: If the user's Jacobian routine needs other quantities,
  107. * they are accessible as follows: hcur (the current stepsize)
  108. * and ewt (the error weight vector) are accessible through
  109. * CVodeGetCurrentStep and CVodeGetErrWeights, respectively
  110. * (see cvode.h). The unit roundoff is available as
  111. * UNIT_ROUNDOFF defined in sundials_types.h.
  112. *
  113. * -----------------------------------------------------------------
  114. */
  115. typedef int (*CVDlsDenseJacFn)(long int N, realtype t,
  116. N_Vector y, N_Vector fy,
  117. DlsMat Jac, void *user_data,
  118. N_Vector tmp1, N_Vector tmp2, N_Vector tmp3);
  119. /*
  120. * -----------------------------------------------------------------
  121. * Type: CVDlsBandJacFn
  122. * -----------------------------------------------------------------
  123. *
  124. * A band Jacobian approximation function Jac must have the
  125. * prototype given below. Its parameters are:
  126. *
  127. * N is the length of all vector arguments.
  128. *
  129. * mupper is the upper half-bandwidth of the approximate banded
  130. * Jacobian. This parameter is the same as the mupper parameter
  131. * passed by the user to the linear solver initialization function.
  132. *
  133. * mlower is the lower half-bandwidth of the approximate banded
  134. * Jacobian. This parameter is the same as the mlower parameter
  135. * passed by the user to the linear solver initialization function.
  136. *
  137. * t is the current value of the independent variable.
  138. *
  139. * y is the current value of the dependent variable vector,
  140. * namely the predicted value of y(t).
  141. *
  142. * fy is the vector f(t,y).
  143. *
  144. * Jac is the band matrix (of type DlsMat) that will be loaded
  145. * by a CVDlsBandJacFn with an approximation to the Jacobian matrix
  146. * Jac = (df_i/dy_j) at the point (t,y).
  147. * Three efficient ways to load J are:
  148. *
  149. * (1) (with macros - no explicit data structure references)
  150. * for (j=0; j < n; j++) {
  151. * col_j = BAND_COL(Jac,j);
  152. * for (i=j-mupper; i <= j+mlower; i++) {
  153. * generate J_ij = the (i,j)th Jacobian element
  154. * BAND_COL_ELEM(col_j,i,j) = J_ij;
  155. * }
  156. * }
  157. *
  158. * (2) (with BAND_COL macro, but without BAND_COL_ELEM macro)
  159. * for (j=0; j < n; j++) {
  160. * col_j = BAND_COL(Jac,j);
  161. * for (k=-mupper; k <= mlower; k++) {
  162. * generate J_ij = the (i,j)th Jacobian element, i=j+k
  163. * col_j[k] = J_ij;
  164. * }
  165. * }
  166. *
  167. * (3) (without macros - explicit data structure references)
  168. * offset = Jac->smu;
  169. * for (j=0; j < n; j++) {
  170. * col_j = ((Jac->data)[j])+offset;
  171. * for (k=-mupper; k <= mlower; k++) {
  172. * generate J_ij = the (i,j)th Jacobian element, i=j+k
  173. * col_j[k] = J_ij;
  174. * }
  175. * }
  176. * Caution: Jac->smu is generally NOT the same as mupper.
  177. *
  178. * The BAND_ELEM(A,i,j) macro is appropriate for use in small
  179. * problems in which efficiency of access is NOT a major concern.
  180. *
  181. * user_data is a pointer to user data - the same as the user_data
  182. * parameter passed to CVodeSetFdata.
  183. *
  184. * NOTE: If the user's Jacobian routine needs other quantities,
  185. * they are accessible as follows: hcur (the current stepsize)
  186. * and ewt (the error weight vector) are accessible through
  187. * CVodeGetCurrentStep and CVodeGetErrWeights, respectively
  188. * (see cvode.h). The unit roundoff is available as
  189. * UNIT_ROUNDOFF defined in sundials_types.h
  190. *
  191. * tmp1, tmp2, and tmp3 are pointers to memory allocated for
  192. * vectors of length N which can be used by a CVDlsBandJacFn
  193. * as temporary storage or work space.
  194. *
  195. * A CVDlsBandJacFn should return 0 if successful, a positive value
  196. * if a recoverable error occurred, and a negative value if an
  197. * unrecoverable error occurred.
  198. * -----------------------------------------------------------------
  199. */
  200. typedef int (*CVDlsBandJacFn)(long int N, long int mupper, long int mlower,
  201. realtype t, N_Vector y, N_Vector fy,
  202. DlsMat Jac, void *user_data,
  203. N_Vector tmp1, N_Vector tmp2, N_Vector tmp3);
  204. /*
  205. * =================================================================
  206. * E X P O R T E D F U N C T I O N S
  207. * =================================================================
  208. */
  209. /*
  210. * -----------------------------------------------------------------
  211. * Optional inputs to the CVDLS linear solver
  212. * -----------------------------------------------------------------
  213. *
  214. * CVDlsSetDenseJacFn specifies the dense Jacobian approximation
  215. * routine to be used for a direct dense linear solver.
  216. *
  217. * CVDlsSetBandJacFn specifies the band Jacobian approximation
  218. * routine to be used for a direct band linear solver.
  219. *
  220. * By default, a difference quotient approximation, supplied with
  221. * the solver is used.
  222. *
  223. * The return value is one of:
  224. * CVDLS_SUCCESS if successful
  225. * CVDLS_MEM_NULL if the CVODE memory was NULL
  226. * CVDLS_LMEM_NULL if the linear solver memory was NULL
  227. * -----------------------------------------------------------------
  228. */
  229. SUNDIALS_EXPORT int CVDlsSetDenseJacFn(void *cvode_mem, CVDlsDenseJacFn jac);
  230. SUNDIALS_EXPORT int CVDlsSetBandJacFn(void *cvode_mem, CVDlsBandJacFn jac);
  231. /*
  232. * -----------------------------------------------------------------
  233. * Optional outputs from the CVDLS linear solver
  234. * -----------------------------------------------------------------
  235. *
  236. * CVDlsGetWorkSpace returns the real and integer workspace used
  237. * by the direct linear solver.
  238. * CVDlsGetNumJacEvals returns the number of calls made to the
  239. * Jacobian evaluation routine jac.
  240. * CVDlsGetNumRhsEvals returns the number of calls to the user
  241. * f routine due to finite difference Jacobian
  242. * evaluation.
  243. * CVDlsGetLastFlag returns the last error flag set by any of
  244. * the CVDLS interface functions.
  245. *
  246. * The return value of CVDlsGet* is one of:
  247. * CVDLS_SUCCESS if successful
  248. * CVDLS_MEM_NULL if the CVODE memory was NULL
  249. * CVDLS_LMEM_NULL if the linear solver memory was NULL
  250. * -----------------------------------------------------------------
  251. */
  252. SUNDIALS_EXPORT int CVDlsGetWorkSpace(void *cvode_mem, long int *lenrwLS, long int *leniwLS);
  253. SUNDIALS_EXPORT int CVDlsGetNumJacEvals(void *cvode_mem, long int *njevals);
  254. SUNDIALS_EXPORT int CVDlsGetNumRhsEvals(void *cvode_mem, long int *nfevalsLS);
  255. SUNDIALS_EXPORT int CVDlsGetLastFlag(void *cvode_mem, long int *flag);
  256. /*
  257. * -----------------------------------------------------------------
  258. * The following function returns the name of the constant
  259. * associated with a CVDLS return flag
  260. * -----------------------------------------------------------------
  261. */
  262. SUNDIALS_EXPORT char *CVDlsGetReturnFlagName(long int flag);
  263. #ifdef __cplusplus
  264. }
  265. #endif
  266. #endif