cvode_direct.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * -----------------------------------------------------------------
  3. * $Revision: 4272 $
  4. * $Date: 2014-12-02 11:19:41 -0800 (Tue, 02 Dec 2014) $
  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. * This is the implementation file for the CVDLS linear solvers
  19. * -----------------------------------------------------------------
  20. */
  21. /*
  22. * =================================================================
  23. * IMPORTED HEADER FILES
  24. * =================================================================
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include "cvode_impl.h"
  29. #include "cvode_direct_impl.h"
  30. #include <sundials/sundials_math.h>
  31. /*
  32. * =================================================================
  33. * FUNCTION SPECIFIC CONSTANTS
  34. * =================================================================
  35. */
  36. /* Constant for DQ Jacobian approximation */
  37. #define MIN_INC_MULT RCONST(1000.0)
  38. #define ZERO RCONST(0.0)
  39. #define ONE RCONST(1.0)
  40. #define TWO RCONST(2.0)
  41. /*
  42. * =================================================================
  43. * READIBILITY REPLACEMENTS
  44. * =================================================================
  45. */
  46. #define f (cv_mem->cv_f)
  47. #define user_data (cv_mem->cv_user_data)
  48. #define uround (cv_mem->cv_uround)
  49. #define nst (cv_mem->cv_nst)
  50. #define tn (cv_mem->cv_tn)
  51. #define h (cv_mem->cv_h)
  52. #define gamma (cv_mem->cv_gamma)
  53. #define gammap (cv_mem->cv_gammap)
  54. #define gamrat (cv_mem->cv_gamrat)
  55. #define ewt (cv_mem->cv_ewt)
  56. #define lmem (cv_mem->cv_lmem)
  57. #define mtype (cvdls_mem->d_type)
  58. #define n (cvdls_mem->d_n)
  59. #define ml (cvdls_mem->d_ml)
  60. #define mu (cvdls_mem->d_mu)
  61. #define smu (cvdls_mem->d_smu)
  62. #define jacDQ (cvdls_mem->d_jacDQ)
  63. #define djac (cvdls_mem->d_djac)
  64. #define bjac (cvdls_mem->d_bjac)
  65. #define M (cvdls_mem->d_M)
  66. #define nje (cvdls_mem->d_nje)
  67. #define nfeDQ (cvdls_mem->d_nfeDQ)
  68. #define last_flag (cvdls_mem->d_last_flag)
  69. /*
  70. * =================================================================
  71. * EXPORTED FUNCTIONS
  72. * =================================================================
  73. */
  74. /*
  75. * CVDlsSetDenseJacFn specifies the dense Jacobian function.
  76. */
  77. SUNDIALS_EXPORT int CVDlsSetDenseJacFn(void *cvode_mem, CVDlsDenseJacFn jac)
  78. {
  79. CVodeMem cv_mem;
  80. CVDlsMem cvdls_mem;
  81. /* Return immediately if cvode_mem is NULL */
  82. if (cvode_mem == NULL) {
  83. cvProcessError(NULL, CVDLS_MEM_NULL, "CVDLS", "CVDlsSetDenseJacFn", MSGD_CVMEM_NULL);
  84. return(CVDLS_MEM_NULL);
  85. }
  86. cv_mem = (CVodeMem) cvode_mem;
  87. if (lmem == NULL) {
  88. cvProcessError(cv_mem, CVDLS_LMEM_NULL, "CVDLS", "CVDlsSetDenseJacFn", MSGD_LMEM_NULL);
  89. return(CVDLS_LMEM_NULL);
  90. }
  91. cvdls_mem = (CVDlsMem) lmem;
  92. if (jac != NULL) {
  93. jacDQ = FALSE;
  94. djac = jac;
  95. } else {
  96. jacDQ = TRUE;
  97. }
  98. return(CVDLS_SUCCESS);
  99. }
  100. /*
  101. * CVDlsSetBandJacFn specifies the band Jacobian function.
  102. */
  103. SUNDIALS_EXPORT int CVDlsSetBandJacFn(void *cvode_mem, CVDlsBandJacFn jac)
  104. {
  105. CVodeMem cv_mem;
  106. CVDlsMem cvdls_mem;
  107. /* Return immediately if cvode_mem is NULL */
  108. if (cvode_mem == NULL) {
  109. cvProcessError(NULL, CVDLS_MEM_NULL, "CVDLS", "CVDlsSetBandJacFn", MSGD_CVMEM_NULL);
  110. return(CVDLS_MEM_NULL);
  111. }
  112. cv_mem = (CVodeMem) cvode_mem;
  113. if (lmem == NULL) {
  114. cvProcessError(cv_mem, CVDLS_LMEM_NULL, "CVDLS", "CVDlsSetBandJacFn", MSGD_LMEM_NULL);
  115. return(CVDLS_LMEM_NULL);
  116. }
  117. cvdls_mem = (CVDlsMem) lmem;
  118. if (jac != NULL) {
  119. jacDQ = FALSE;
  120. bjac = jac;
  121. } else {
  122. jacDQ = TRUE;
  123. }
  124. return(CVDLS_SUCCESS);
  125. }
  126. /*
  127. * CVDlsGetWorkSpace returns the length of workspace allocated for the
  128. * CVDLS linear solver.
  129. */
  130. SUNDIALS_EXPORT int CVDlsGetWorkSpace(void *cvode_mem, long int *lenrwLS, long int *leniwLS)
  131. {
  132. CVodeMem cv_mem;
  133. CVDlsMem cvdls_mem;
  134. /* Return immediately if cvode_mem is NULL */
  135. if (cvode_mem == NULL) {
  136. cvProcessError(NULL, CVDLS_MEM_NULL, "CVDLS", "CVDlsGetWorkSpace", MSGD_CVMEM_NULL);
  137. return(CVDLS_MEM_NULL);
  138. }
  139. cv_mem = (CVodeMem) cvode_mem;
  140. if (lmem == NULL) {
  141. cvProcessError(cv_mem, CVDLS_LMEM_NULL, "CVDLS", "CVDlsGetWorkSpace", MSGD_LMEM_NULL);
  142. return(CVDLS_LMEM_NULL);
  143. }
  144. cvdls_mem = (CVDlsMem) lmem;
  145. if (mtype == SUNDIALS_DENSE) {
  146. *lenrwLS = 2*n*n;
  147. *leniwLS = n;
  148. } else if (mtype == SUNDIALS_BAND) {
  149. *lenrwLS = n*(smu + mu + 2*ml + 2);
  150. *leniwLS = n;
  151. }
  152. return(CVDLS_SUCCESS);
  153. }
  154. /*
  155. * CVDlsGetNumJacEvals returns the number of Jacobian evaluations.
  156. */
  157. SUNDIALS_EXPORT int CVDlsGetNumJacEvals(void *cvode_mem, long int *njevals)
  158. {
  159. CVodeMem cv_mem;
  160. CVDlsMem cvdls_mem;
  161. /* Return immediately if cvode_mem is NULL */
  162. if (cvode_mem == NULL) {
  163. cvProcessError(NULL, CVDLS_MEM_NULL, "CVDLS", "CVDlsGetNumJacEvals", MSGD_CVMEM_NULL);
  164. return(CVDLS_MEM_NULL);
  165. }
  166. cv_mem = (CVodeMem) cvode_mem;
  167. if (lmem == NULL) {
  168. cvProcessError(cv_mem, CVDLS_LMEM_NULL, "CVDLS", "CVDlsGetNumJacEvals", MSGD_LMEM_NULL);
  169. return(CVDLS_LMEM_NULL);
  170. }
  171. cvdls_mem = (CVDlsMem) lmem;
  172. *njevals = nje;
  173. return(CVDLS_SUCCESS);
  174. }
  175. /*
  176. * CVDlsGetNumRhsEvals returns the number of calls to the ODE function
  177. * needed for the DQ Jacobian approximation.
  178. */
  179. SUNDIALS_EXPORT int CVDlsGetNumRhsEvals(void *cvode_mem, long int *nfevalsLS)
  180. {
  181. CVodeMem cv_mem;
  182. CVDlsMem cvdls_mem;
  183. /* Return immediately if cvode_mem is NULL */
  184. if (cvode_mem == NULL) {
  185. cvProcessError(NULL, CVDLS_MEM_NULL, "CVDLS", "CVDlsGetNumRhsEvals", MSGD_CVMEM_NULL);
  186. return(CVDLS_MEM_NULL);
  187. }
  188. cv_mem = (CVodeMem) cvode_mem;
  189. if (lmem == NULL) {
  190. cvProcessError(cv_mem, CVDLS_LMEM_NULL, "CVDLS", "CVDlsGetNumRhsEvals", MSGD_LMEM_NULL);
  191. return(CVDLS_LMEM_NULL);
  192. }
  193. cvdls_mem = (CVDlsMem) lmem;
  194. *nfevalsLS = nfeDQ;
  195. return(CVDLS_SUCCESS);
  196. }
  197. /*
  198. * CVDlsGetReturnFlagName returns the name associated with a CVDLS
  199. * return value.
  200. */
  201. SUNDIALS_EXPORT char *CVDlsGetReturnFlagName(long int flag)
  202. {
  203. char *name;
  204. name = (char *)malloc(30*sizeof(char));
  205. switch(flag) {
  206. case CVDLS_SUCCESS:
  207. sprintf(name,"CVDLS_SUCCESS");
  208. break;
  209. case CVDLS_MEM_NULL:
  210. sprintf(name,"CVDLS_MEM_NULL");
  211. break;
  212. case CVDLS_LMEM_NULL:
  213. sprintf(name,"CVDLS_LMEM_NULL");
  214. break;
  215. case CVDLS_ILL_INPUT:
  216. sprintf(name,"CVDLS_ILL_INPUT");
  217. break;
  218. case CVDLS_MEM_FAIL:
  219. sprintf(name,"CVDLS_MEM_FAIL");
  220. break;
  221. case CVDLS_JACFUNC_UNRECVR:
  222. sprintf(name,"CVDLS_JACFUNC_UNRECVR");
  223. break;
  224. case CVDLS_JACFUNC_RECVR:
  225. sprintf(name,"CVDLS_JACFUNC_RECVR");
  226. break;
  227. default:
  228. sprintf(name,"NONE");
  229. }
  230. return(name);
  231. }
  232. /*
  233. * CVDlsGetLastFlag returns the last flag set in a CVDLS function.
  234. */
  235. SUNDIALS_EXPORT int CVDlsGetLastFlag(void *cvode_mem, long int *flag)
  236. {
  237. CVodeMem cv_mem;
  238. CVDlsMem cvdls_mem;
  239. /* Return immediately if cvode_mem is NULL */
  240. if (cvode_mem == NULL) {
  241. cvProcessError(NULL, CVDLS_MEM_NULL, "CVDLS", "CVDlsGetLastFlag", MSGD_CVMEM_NULL);
  242. return(CVDLS_MEM_NULL);
  243. }
  244. cv_mem = (CVodeMem) cvode_mem;
  245. if (lmem == NULL) {
  246. cvProcessError(cv_mem, CVDLS_LMEM_NULL, "CVDLS", "CVDlsGetLastFlag", MSGD_LMEM_NULL);
  247. return(CVDLS_LMEM_NULL);
  248. }
  249. cvdls_mem = (CVDlsMem) lmem;
  250. *flag = last_flag;
  251. return(CVDLS_SUCCESS);
  252. }
  253. /*
  254. * =================================================================
  255. * DQ JACOBIAN APPROXIMATIONS
  256. * =================================================================
  257. */
  258. /*
  259. * -----------------------------------------------------------------
  260. * cvDlsDenseDQJac
  261. * -----------------------------------------------------------------
  262. * This routine generates a dense difference quotient approximation to
  263. * the Jacobian of f(t,y). It assumes that a dense matrix of type
  264. * DlsMat is stored column-wise, and that elements within each column
  265. * are contiguous. The address of the jth column of J is obtained via
  266. * the macro DENSE_COL and this pointer is associated with an N_Vector
  267. * using the N_VGetArrayPointer/N_VSetArrayPointer functions.
  268. * Finally, the actual computation of the jth column of the Jacobian is
  269. * done with a call to N_VLinearSum.
  270. * -----------------------------------------------------------------
  271. */
  272. SUNDIALS_EXPORT int cvDlsDenseDQJac(long int N, realtype t,
  273. N_Vector y, N_Vector fy,
  274. DlsMat Jac, void *data,
  275. N_Vector tmp1, N_Vector tmp2, N_Vector tmp3)
  276. {
  277. realtype fnorm, minInc, inc, inc_inv, yjsaved, srur;
  278. realtype *tmp2_data, *y_data, *ewt_data;
  279. N_Vector ftemp, jthCol;
  280. long int j;
  281. int retval = 0;
  282. CVodeMem cv_mem;
  283. CVDlsMem cvdls_mem;
  284. /* data points to cvode_mem */
  285. cv_mem = (CVodeMem) data;
  286. cvdls_mem = (CVDlsMem) lmem;
  287. /* Save pointer to the array in tmp2 */
  288. tmp2_data = N_VGetArrayPointer(tmp2);
  289. /* Rename work vectors for readibility */
  290. ftemp = tmp1;
  291. jthCol = tmp2;
  292. /* Obtain pointers to the data for ewt, y */
  293. ewt_data = N_VGetArrayPointer(ewt);
  294. y_data = N_VGetArrayPointer(y);
  295. /* Set minimum increment based on uround and norm of f */
  296. srur = SUNRsqrt(uround);
  297. fnorm = N_VWrmsNorm(fy, ewt);
  298. minInc = (fnorm != ZERO) ?
  299. (MIN_INC_MULT * SUNRabs(h) * uround * N * fnorm) : ONE;
  300. for (j = 0; j < N; j++) {
  301. /* Generate the jth col of J(tn,y) */
  302. N_VSetArrayPointer(DENSE_COL(Jac,j), jthCol);
  303. yjsaved = y_data[j];
  304. inc = SUNMAX(srur*SUNRabs(yjsaved), minInc/ewt_data[j]);
  305. y_data[j] += inc;
  306. retval = f(t, y, ftemp, user_data);
  307. nfeDQ++;
  308. if (retval != 0) break;
  309. y_data[j] = yjsaved;
  310. inc_inv = ONE/inc;
  311. N_VLinearSum(inc_inv, ftemp, -inc_inv, fy, jthCol);
  312. DENSE_COL(Jac,j) = N_VGetArrayPointer(jthCol);
  313. }
  314. /* Restore original array pointer in tmp2 */
  315. N_VSetArrayPointer(tmp2_data, tmp2);
  316. return(retval);
  317. }
  318. /*
  319. * -----------------------------------------------------------------
  320. * cvDlsBandDQJac
  321. * -----------------------------------------------------------------
  322. * This routine generates a banded difference quotient approximation to
  323. * the Jacobian of f(t,y). It assumes that a band matrix of type
  324. * DlsMat is stored column-wise, and that elements within each column
  325. * are contiguous. This makes it possible to get the address of a column
  326. * of J via the macro BAND_COL and to write a simple for loop to set
  327. * each of the elements of a column in succession.
  328. * -----------------------------------------------------------------
  329. */
  330. SUNDIALS_EXPORT int cvDlsBandDQJac(long int N, long int mupper, long int mlower,
  331. realtype t, N_Vector y, N_Vector fy,
  332. DlsMat Jac, void *data,
  333. N_Vector tmp1, N_Vector tmp2, N_Vector tmp3)
  334. {
  335. N_Vector ftemp, ytemp;
  336. realtype fnorm, minInc, inc, inc_inv, srur;
  337. realtype *col_j, *ewt_data, *fy_data, *ftemp_data, *y_data, *ytemp_data;
  338. long int group, i, j, width, ngroups, i1, i2;
  339. int retval = 0;
  340. CVodeMem cv_mem;
  341. CVDlsMem cvdls_mem;
  342. /* data points to cvode_mem */
  343. cv_mem = (CVodeMem) data;
  344. cvdls_mem = (CVDlsMem) lmem;
  345. /* Rename work vectors for use as temporary values of y and f */
  346. ftemp = tmp1;
  347. ytemp = tmp2;
  348. /* Obtain pointers to the data for ewt, fy, ftemp, y, ytemp */
  349. ewt_data = N_VGetArrayPointer(ewt);
  350. fy_data = N_VGetArrayPointer(fy);
  351. ftemp_data = N_VGetArrayPointer(ftemp);
  352. y_data = N_VGetArrayPointer(y);
  353. ytemp_data = N_VGetArrayPointer(ytemp);
  354. /* Load ytemp with y = predicted y vector */
  355. N_VScale(ONE, y, ytemp);
  356. /* Set minimum increment based on uround and norm of f */
  357. srur = SUNRsqrt(uround);
  358. fnorm = N_VWrmsNorm(fy, ewt);
  359. minInc = (fnorm != ZERO) ?
  360. (MIN_INC_MULT * SUNRabs(h) * uround * N * fnorm) : ONE;
  361. /* Set bandwidth and number of column groups for band differencing */
  362. width = mlower + mupper + 1;
  363. ngroups = SUNMIN(width, N);
  364. /* Loop over column groups. */
  365. for (group=1; group <= ngroups; group++) {
  366. /* Increment all y_j in group */
  367. for(j=group-1; j < N; j+=width) {
  368. inc = SUNMAX(srur*SUNRabs(y_data[j]), minInc/ewt_data[j]);
  369. ytemp_data[j] += inc;
  370. }
  371. /* Evaluate f with incremented y */
  372. retval = f(tn, ytemp, ftemp, user_data);
  373. nfeDQ++;
  374. if (retval != 0) break;
  375. /* Restore ytemp, then form and load difference quotients */
  376. for (j=group-1; j < N; j+=width) {
  377. ytemp_data[j] = y_data[j];
  378. col_j = BAND_COL(Jac,j);
  379. inc = SUNMAX(srur*SUNRabs(y_data[j]), minInc/ewt_data[j]);
  380. inc_inv = ONE/inc;
  381. i1 = SUNMAX(0, j-mupper);
  382. i2 = SUNMIN(j+mlower, N-1);
  383. for (i=i1; i <= i2; i++)
  384. BAND_COL_ELEM(col_j,i,j) = inc_inv * (ftemp_data[i] - fy_data[i]);
  385. }
  386. }
  387. return(retval);
  388. }
  389. /* undefine to be able to compile all source code files in a single compilation unit, added by 3ds.com */
  390. #undef f
  391. #undef user_data
  392. #undef uround
  393. #undef nst
  394. #undef tn
  395. #undef h
  396. #undef gamma
  397. #undef gammap
  398. #undef gamrat
  399. #undef ewt
  400. #undef lmem
  401. #undef mtype
  402. #undef n
  403. #undef ml
  404. #undef mu
  405. #undef smu
  406. #undef jacDQ
  407. #undef djac
  408. #undef bjac
  409. #undef M
  410. #undef nje
  411. #undef nfeDQ
  412. #undef last_flag