cvode_dense.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * -----------------------------------------------------------------
  3. * $Revision: 4272 $
  4. * $Date: 2014-12-02 11:19:41 -0800 (Tue, 02 Dec 2014) $
  5. * -----------------------------------------------------------------
  6. * Programmer(s): Scott D. Cohen, Alan C. Hindmarsh and
  7. * Radu Serban @ LLNL
  8. * -----------------------------------------------------------------
  9. * LLNS Copyright Start
  10. * Copyright (c) 2014, Lawrence Livermore National Security
  11. * This work was performed under the auspices of the U.S. Department
  12. * of Energy by Lawrence Livermore National Laboratory in part under
  13. * Contract W-7405-Eng-48 and in part under Contract DE-AC52-07NA27344.
  14. * Produced at the Lawrence Livermore National Laboratory.
  15. * All rights reserved.
  16. * For details, see the LICENSE file.
  17. * LLNS Copyright End
  18. * -----------------------------------------------------------------
  19. * This is the impleentation file for the CVDENSE linear solver.
  20. * -----------------------------------------------------------------
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <cvode/cvode_dense.h>
  25. #include "cvode_direct_impl.h"
  26. #include "cvode_impl.h"
  27. #include <sundials/sundials_math.h>
  28. /* Constants */
  29. #define ZERO RCONST(0.0)
  30. #define ONE RCONST(1.0)
  31. #define TWO RCONST(2.0)
  32. /* CVDENSE linit, lsetup, lsolve, and lfree routines */
  33. static int cvDenseInit(CVodeMem cv_mem);
  34. static int cvDenseSetup(CVodeMem cv_mem, int convfail, N_Vector ypred,
  35. N_Vector fpred, booleantype *jcurPtr,
  36. N_Vector vtemp1, N_Vector vtemp2, N_Vector vtemp3);
  37. static int cvDenseSolve(CVodeMem cv_mem, N_Vector b, N_Vector weight,
  38. N_Vector ycur, N_Vector fcur);
  39. static void cvDenseFree(CVodeMem cv_mem);
  40. /* Readability Replacements */
  41. #define lmm (cv_mem->cv_lmm)
  42. #define f (cv_mem->cv_f)
  43. #define nst (cv_mem->cv_nst)
  44. #define tn (cv_mem->cv_tn)
  45. #define h (cv_mem->cv_h)
  46. #define gamma (cv_mem->cv_gamma)
  47. #define gammap (cv_mem->cv_gammap)
  48. #define gamrat (cv_mem->cv_gamrat)
  49. #define ewt (cv_mem->cv_ewt)
  50. #define linit (cv_mem->cv_linit)
  51. #define lsetup (cv_mem->cv_lsetup)
  52. #define lsolve (cv_mem->cv_lsolve)
  53. #define lfree (cv_mem->cv_lfree)
  54. #define lmem (cv_mem->cv_lmem)
  55. #define vec_tmpl (cv_mem->cv_tempv)
  56. #define setupNonNull (cv_mem->cv_setupNonNull)
  57. #define mtype (cvdls_mem->d_type)
  58. #define n (cvdls_mem->d_n)
  59. #define jacDQ (cvdls_mem->d_jacDQ)
  60. #define jac (cvdls_mem->d_djac)
  61. #define M (cvdls_mem->d_M)
  62. #define lpivots (cvdls_mem->d_lpivots)
  63. #define savedJ (cvdls_mem->d_savedJ)
  64. #define nstlj (cvdls_mem->d_nstlj)
  65. #define nje (cvdls_mem->d_nje)
  66. #define nfeDQ (cvdls_mem->d_nfeDQ)
  67. #define J_data (cvdls_mem->d_J_data)
  68. #define last_flag (cvdls_mem->d_last_flag)
  69. /*
  70. * -----------------------------------------------------------------
  71. * CVDense
  72. * -----------------------------------------------------------------
  73. * This routine initializes the memory record and sets various function
  74. * fields specific to the dense linear solver module. CVDense first
  75. * calls the existing lfree routine if this is not NULL. Then it sets
  76. * the cv_linit, cv_lsetup, cv_lsolve, cv_lfree fields in (*cvode_mem)
  77. * to be cvDenseInit, cvDenseSetup, cvDenseSolve, and cvDenseFree,
  78. * respectively. It allocates memory for a structure of type
  79. * CVDlsMemRec and sets the cv_lmem field in (*cvode_mem) to the
  80. * address of this structure. It sets setupNonNull in (*cvode_mem) to
  81. * TRUE, and the d_jac field to the default cvDlsDenseDQJac.
  82. * Finally, it allocates memory for M, savedJ, and lpivots.
  83. * The return value is SUCCESS = 0, or LMEM_FAIL = -1.
  84. *
  85. * NOTE: The dense linear solver assumes a serial implementation
  86. * of the NVECTOR package. Therefore, CVDense will first
  87. * test for compatible a compatible N_Vector internal
  88. * representation by checking that N_VGetArrayPointer and
  89. * N_VSetArrayPointer exist.
  90. * -----------------------------------------------------------------
  91. */
  92. SUNDIALS_EXPORT int CVDense(void *cvode_mem, long int N)
  93. {
  94. CVodeMem cv_mem;
  95. CVDlsMem cvdls_mem;
  96. /* Return immediately if cvode_mem is NULL */
  97. if (cvode_mem == NULL) {
  98. cvProcessError(NULL, CVDLS_MEM_NULL, "CVDENSE", "CVDense", MSGD_CVMEM_NULL);
  99. return(CVDLS_MEM_NULL);
  100. }
  101. cv_mem = (CVodeMem) cvode_mem;
  102. /* Test if the NVECTOR package is compatible with the DENSE solver */
  103. if (vec_tmpl->ops->nvgetarraypointer == NULL ||
  104. vec_tmpl->ops->nvsetarraypointer == NULL) {
  105. cvProcessError(cv_mem, CVDLS_ILL_INPUT, "CVDENSE", "CVDense", MSGD_BAD_NVECTOR);
  106. return(CVDLS_ILL_INPUT);
  107. }
  108. if (lfree !=NULL) lfree(cv_mem);
  109. /* Set four main function fields in cv_mem */
  110. linit = cvDenseInit;
  111. lsetup = cvDenseSetup;
  112. lsolve = cvDenseSolve;
  113. lfree = cvDenseFree;
  114. /* Get memory for CVDlsMemRec */
  115. cvdls_mem = NULL;
  116. cvdls_mem = (CVDlsMem) malloc(sizeof(struct CVDlsMemRec));
  117. if (cvdls_mem == NULL) {
  118. cvProcessError(cv_mem, CVDLS_MEM_FAIL, "CVDENSE", "CVDense", MSGD_MEM_FAIL);
  119. return(CVDLS_MEM_FAIL);
  120. }
  121. /* Set matrix type */
  122. mtype = SUNDIALS_DENSE;
  123. /* Initialize Jacobian-related data */
  124. jacDQ = TRUE;
  125. jac = NULL;
  126. J_data = NULL;
  127. last_flag = CVDLS_SUCCESS;
  128. setupNonNull = TRUE;
  129. /* Set problem dimension */
  130. n = N;
  131. /* Allocate memory for M, savedJ, and pivot array */
  132. M = NULL;
  133. M = NewDenseMat(N, N);
  134. if (M == NULL) {
  135. cvProcessError(cv_mem, CVDLS_MEM_FAIL, "CVDENSE", "CVDense", MSGD_MEM_FAIL);
  136. free(cvdls_mem); cvdls_mem = NULL;
  137. return(CVDLS_MEM_FAIL);
  138. }
  139. savedJ = NULL;
  140. savedJ = NewDenseMat(N, N);
  141. if (savedJ == NULL) {
  142. cvProcessError(cv_mem, CVDLS_MEM_FAIL, "CVDENSE", "CVDense", MSGD_MEM_FAIL);
  143. DestroyMat(M);
  144. free(cvdls_mem); cvdls_mem = NULL;
  145. return(CVDLS_MEM_FAIL);
  146. }
  147. lpivots = NULL;
  148. lpivots = NewLintArray(N);
  149. if (lpivots == NULL) {
  150. cvProcessError(cv_mem, CVDLS_MEM_FAIL, "CVDENSE", "CVDense", MSGD_MEM_FAIL);
  151. DestroyMat(M);
  152. DestroyMat(savedJ);
  153. free(cvdls_mem); cvdls_mem = NULL;
  154. return(CVDLS_MEM_FAIL);
  155. }
  156. /* Attach linear solver memory to integrator memory */
  157. lmem = cvdls_mem;
  158. return(CVDLS_SUCCESS);
  159. }
  160. /*
  161. * -----------------------------------------------------------------
  162. * cvDenseInit
  163. * -----------------------------------------------------------------
  164. * This routine does remaining initializations specific to the dense
  165. * linear solver.
  166. * -----------------------------------------------------------------
  167. */
  168. static int cvDenseInit(CVodeMem cv_mem)
  169. {
  170. CVDlsMem cvdls_mem;
  171. cvdls_mem = (CVDlsMem) lmem;
  172. nje = 0;
  173. nfeDQ = 0;
  174. nstlj = 0;
  175. /* Set Jacobian function and data, depending on jacDQ */
  176. if (jacDQ) {
  177. jac = cvDlsDenseDQJac;
  178. J_data = cv_mem;
  179. } else {
  180. J_data = cv_mem->cv_user_data;
  181. }
  182. last_flag = CVDLS_SUCCESS;
  183. return(0);
  184. }
  185. /*
  186. * -----------------------------------------------------------------
  187. * cvDenseSetup
  188. * -----------------------------------------------------------------
  189. * This routine does the setup operations for the dense linear solver.
  190. * It makes a decision whether or not to call the Jacobian evaluation
  191. * routine based on various state variables, and if not it uses the
  192. * saved copy. In any case, it constructs the Newton matrix
  193. * M = I - gamma*J, updates counters, and calls the dense LU
  194. * factorization routine.
  195. * -----------------------------------------------------------------
  196. */
  197. static int cvDenseSetup(CVodeMem cv_mem, int convfail, N_Vector ypred,
  198. N_Vector fpred, booleantype *jcurPtr,
  199. N_Vector vtemp1, N_Vector vtemp2, N_Vector vtemp3)
  200. {
  201. booleantype jbad, jok;
  202. realtype dgamma;
  203. long int ier;
  204. CVDlsMem cvdls_mem;
  205. int retval;
  206. cvdls_mem = (CVDlsMem) lmem;
  207. /* Use nst, gamma/gammap, and convfail to set J eval. flag jok */
  208. dgamma = SUNRabs((gamma/gammap) - ONE);
  209. jbad = (nst == 0) || (nst > nstlj + CVD_MSBJ) ||
  210. ((convfail == CV_FAIL_BAD_J) && (dgamma < CVD_DGMAX)) ||
  211. (convfail == CV_FAIL_OTHER);
  212. jok = !jbad;
  213. if (jok) {
  214. /* If jok = TRUE, use saved copy of J */
  215. *jcurPtr = FALSE;
  216. DenseCopy(savedJ, M);
  217. } else {
  218. /* If jok = FALSE, call jac routine for new J value */
  219. nje++;
  220. nstlj = nst;
  221. *jcurPtr = TRUE;
  222. SetToZero(M);
  223. retval = jac(n, tn, ypred, fpred, M, J_data, vtemp1, vtemp2, vtemp3);
  224. if (retval < 0) {
  225. cvProcessError(cv_mem, CVDLS_JACFUNC_UNRECVR, "CVDENSE", "cvDenseSetup", MSGD_JACFUNC_FAILED);
  226. last_flag = CVDLS_JACFUNC_UNRECVR;
  227. return(-1);
  228. }
  229. if (retval > 0) {
  230. last_flag = CVDLS_JACFUNC_RECVR;
  231. return(1);
  232. }
  233. DenseCopy(M, savedJ);
  234. }
  235. /* Scale and add I to get M = I - gamma*J */
  236. DenseScale(-gamma, M);
  237. AddIdentity(M);
  238. /* Do LU factorization of M */
  239. ier = DenseGETRF(M, lpivots);
  240. /* Return 0 if the LU was complete; otherwise return 1 */
  241. last_flag = ier;
  242. if (ier > 0) return(1);
  243. return(0);
  244. }
  245. /*
  246. * -----------------------------------------------------------------
  247. * cvDenseSolve
  248. * -----------------------------------------------------------------
  249. * This routine handles the solve operation for the dense linear solver
  250. * by calling the dense backsolve routine. The returned value is 0.
  251. * -----------------------------------------------------------------
  252. */
  253. static int cvDenseSolve(CVodeMem cv_mem, N_Vector b, N_Vector weight,
  254. N_Vector ycur, N_Vector fcur)
  255. {
  256. CVDlsMem cvdls_mem;
  257. realtype *bd;
  258. cvdls_mem = (CVDlsMem) lmem;
  259. bd = N_VGetArrayPointer(b);
  260. DenseGETRS(M, lpivots, bd);
  261. /* If CV_BDF, scale the correction to account for change in gamma */
  262. if ((lmm == CV_BDF) && (gamrat != ONE)) {
  263. N_VScale(TWO/(ONE + gamrat), b, b);
  264. }
  265. last_flag = CVDLS_SUCCESS;
  266. return(0);
  267. }
  268. /*
  269. * -----------------------------------------------------------------
  270. * cvDenseFree
  271. * -----------------------------------------------------------------
  272. * This routine frees memory specific to the dense linear solver.
  273. * -----------------------------------------------------------------
  274. */
  275. static void cvDenseFree(CVodeMem cv_mem)
  276. {
  277. CVDlsMem cvdls_mem;
  278. cvdls_mem = (CVDlsMem) lmem;
  279. DestroyMat(M);
  280. DestroyMat(savedJ);
  281. DestroyArray(lpivots);
  282. free(cvdls_mem);
  283. cv_mem->cv_lmem = NULL;
  284. }
  285. /* undefine to be able to compile all source code files in a single compilation unit, added by 3ds.com */
  286. #undef lmm
  287. #undef f
  288. #undef nst
  289. #undef tn
  290. #undef h
  291. #undef gamma
  292. #undef gammap
  293. #undef gamrat
  294. #undef ewt
  295. #undef linit
  296. #undef lsetup
  297. #undef lsolve
  298. #undef lfree
  299. #undef lmem
  300. #undef vec_tmpl
  301. #undef setupNonNull
  302. #undef mtype
  303. #undef n
  304. #undef jacDQ
  305. #undef jac
  306. #undef M
  307. #undef lpivots
  308. #undef savedJ
  309. #undef nstlj
  310. #undef nje
  311. #undef nfeDQ
  312. #undef J_data
  313. #undef last_flag