jac.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* Implementation of jac.h */
  2. #include "jac.h"
  3. #include "util.h"
  4. #include "integration.h"
  5. #include "fmiFunctions_fwd.h"
  6. /* Sundials */
  7. #include <cvode/cvode.h>
  8. #include <sundials/sundials_math.h>
  9. #include <nvector/nvector_serial.h>
  10. #include <sundials/sundials_direct.h>
  11. #include <assert.h>
  12. #include <time.h>
  13. /* ----------------- local function declarations ----------------- */
  14. /* common function for J and J * v computation */
  15. static int compute_Jdata(realtype t, N_Vector y, N_Vector fy, N_Vector v,
  16. DlsMat J, N_Vector Jv,
  17. realtype srur, realtype mindy, realtype* ewt_data,
  18. N_Vector tmp, FMIComponent c);
  19. /* computation of parameters for numeric computation of J */
  20. static int compute_Jconf(N_Vector y, N_Vector fy, realtype* srur, realtype* mindy,
  21. FMIComponent c);
  22. /* ----------------- local variables ----------------- */
  23. static size_t nGroups;
  24. /* ----------------- external variables ----------------- */
  25. extern int QJacobianCG_[];
  26. extern int QJacobianGC_[];
  27. /* ----------------- external function declarations ----------------- */
  28. /* ----------------- function definitions ----------------- */
  29. DYMOLA_STATIC void jac_setup(size_t ngroups){
  30. nGroups = ngroups;
  31. }
  32. /* ------------------------------------------------------ */
  33. /* computes function f(t,y) */
  34. DYMOLA_STATIC int jac_f(double t, N_Vector y, N_Vector ydot, void *user_data)
  35. {
  36. clock_t t0 = clock();
  37. FMIComponent* c = (FMIComponent*) user_data;
  38. Component* comp = (Component*) c;
  39. int retval = 0;
  40. N_VectorContent_Serial ydot_cont = NV_CONTENT_S(ydot);
  41. /* temporarily change time, states and inputs */
  42. FMIReal time = comp->time;
  43. FMIReal* states = comp->states;
  44. integration_extrapolate_inputs(comp, t);
  45. comp->states = N_VGetArrayPointer(y);
  46. comp->time = t;
  47. comp->icall = iDemandStart;
  48. if (fmiGetDerivatives_(c, ydot_cont->data, ydot_cont->length) != FMIOK) {
  49. retval = 1;
  50. }
  51. /* restore time, states and inputs */
  52. integration_extrapolate_inputs(comp, time);
  53. comp->states = states;
  54. comp->time = time;
  55. comp->iData->stat.fCalls++;
  56. comp->iData->stat.fTime += (clock() - t0);
  57. return retval;
  58. }
  59. /* ------------------------------------------------------ */
  60. /* Jacobian routine, computes J(t,y) = df/dy numerically */
  61. DYMOLA_STATIC int jac_Jacobian(long N, realtype t,
  62. N_Vector y, N_Vector fy, DlsMat J, void *user_data,
  63. N_Vector tmp1, N_Vector tmp2, N_Vector tmp3)
  64. {
  65. FMIComponent* c = (FMIComponent*) user_data;
  66. Component* comp = (Component*) c;
  67. N_Vector ftmp = tmp1;
  68. realtype* ewt_data;
  69. realtype srur;
  70. realtype mindy;
  71. int j;
  72. /* error here is recoverable */
  73. int retval = 1;
  74. /* for later restoring */
  75. realtype* tmp2_data = N_VGetArrayPointer(tmp2);
  76. /* need raw pointers for some */
  77. realtype* y_data = N_VGetArrayPointer(y);
  78. realtype* fy_data = N_VGetArrayPointer(fy);
  79. realtype* ftmp_data = N_VGetArrayPointer(ftmp);
  80. if (compute_Jconf(y, fy, &srur, &mindy, c)) {
  81. goto done;
  82. }
  83. ewt_data = N_VGetArrayPointer(comp->iData->ewt);
  84. comp->istruct->mInJacobian = 1;
  85. /* use grouping if possible */
  86. if (nGroups > 0) {
  87. /* use grouping */
  88. /* cannot use tmp2 here since it reuses the pointer provided in N_VMake_Serial,
  89. hence interfering with comp->states */
  90. if (!compute_Jdata(t, y, fy, NULL, J, NULL, srur, mindy, ewt_data, tmp3, c)) {
  91. retval = 0;
  92. goto done;
  93. }
  94. /* failed with grouping - fall back */
  95. }
  96. {
  97. /* no or failed grouping, perturb one variable at the time */
  98. N_Vector jthCol = tmp2;
  99. for (j = 0; j < N; j++) {
  100. realtype yjsaved;
  101. realtype dy1, dy2;
  102. realtype dy;
  103. realtype dy_inv;
  104. int k;
  105. /* generate the jth col of J(tn, y) */
  106. N_VSetArrayPointer(DENSE_COL(J, j), jthCol);
  107. yjsaved = y_data[j];
  108. /* perturb */
  109. dy1 = srur * SUNRabs(yjsaved);
  110. dy2 = mindy / ewt_data[j];
  111. dy = SUNMAX(dy1, dy2);
  112. /* keep sign to avoid y_data near 0 */
  113. if (yjsaved < 0) dy = -dy;
  114. /* retry a few times in case of failure */
  115. for (k = 0; k < 5; k++) {
  116. y_data[j] = yjsaved + dy;
  117. /* reduce rounding error at a low cost */
  118. dy = y_data[j] - yjsaved;
  119. /* compute new f */
  120. if (!jac_f(t, y, ftmp, user_data)) {
  121. break;
  122. }
  123. dy = -dy / 5; /* try smaller increment, and different sign */
  124. if (k>5) {
  125. /* failed */
  126. y_data[j] = yjsaved;
  127. goto done;
  128. }
  129. }
  130. y_data[j] = yjsaved;
  131. dy_inv = RCONST(1.0) / dy;
  132. /* concisesness for the cost of duplicated multiplication */
  133. N_VLinearSum(dy_inv, ftmp, -dy_inv, fy, jthCol);
  134. }
  135. }
  136. retval = 0;
  137. done:
  138. comp->istruct->mInJacobian = 0;
  139. /* restoring original array pointer appears to be necessary */
  140. N_VSetArrayPointer(tmp2_data, tmp2);
  141. comp->iData->stat.JacCalls++;
  142. return retval;
  143. }
  144. /* ----------------- local function definitions ----------------- */
  145. static int compute_Jdata(realtype t, N_Vector y, N_Vector fy, N_Vector v,
  146. DlsMat J, N_Vector Jv,
  147. realtype srur, realtype mindy, realtype* ewt_data,
  148. N_Vector tmp, FMIComponent c)
  149. {
  150. Component* comp = (Component*) c;
  151. N_Vector ftmp = comp->iData->tmp1;
  152. realtype* y_data = N_VGetArrayPointer(y);
  153. realtype* fy_data = N_VGetArrayPointer(fy);
  154. realtype* v_data;
  155. realtype* Jv_data;
  156. realtype* ftmp_data = N_VGetArrayPointer(ftmp);
  157. realtype* ysaved_data = N_VGetArrayPointer(comp->iData->tmp2);
  158. realtype* dy_data = N_VGetArrayPointer(tmp);
  159. size_t group_nr;
  160. int group_ix = 1;
  161. int j;
  162. int N = (int)comp->nStates;
  163. /* compute exactly one of J or J * v */
  164. assert(J != NULL && Jv == NULL || J == NULL && Jv != NULL);
  165. /* initialize result */
  166. if (J != NULL) {
  167. for(j = 0; j < N; j++) {
  168. /* temporarily borrow tmp */
  169. N_VSetArrayPointer(DENSE_COL(J, j), tmp);
  170. N_VConst(0, tmp);
  171. }
  172. } else {
  173. v_data = N_VGetArrayPointer(v);
  174. Jv_data = N_VGetArrayPointer(Jv);
  175. N_VConst(0, Jv);
  176. }
  177. N_VSetArrayPointer(dy_data, tmp);
  178. /* iterate the groups */
  179. for (group_nr = 0; group_nr < nGroups; group_nr++) {
  180. int nmembers = QJacobianCG_[group_ix];
  181. int member;
  182. int i;
  183. int gc_ix = (int)(N * group_nr);
  184. int jac_res;
  185. /* perturb each group member */
  186. for (member = 1; member <= nmembers; member++) {
  187. realtype dy1, dy2;
  188. /* fetch column */
  189. int j = QJacobianCG_[group_ix + member] - 1;
  190. ysaved_data[j] = y_data[j];
  191. /* perturb the jth variable */
  192. dy1 = srur * SUNRabs(ysaved_data[j]);
  193. dy2 = mindy / ewt_data[j];
  194. dy_data[j] = SUNMAX(dy1, dy2);
  195. /* keep sign to avoid y_data near 0 */
  196. if (ysaved_data[j] < 0) dy_data[j] = -dy_data[j];
  197. y_data[j] += dy_data[j];
  198. /* reduce rounding error at a low cost */
  199. dy_data[j] = y_data[j] - ysaved_data[j];
  200. }
  201. /* compute new f */
  202. jac_res = jac_f(t, y, ftmp, c);
  203. /* restore y */
  204. for (member = 1; member <= nmembers; member++) {
  205. /* fetch column */
  206. j = QJacobianCG_[group_ix + member] - 1;
  207. y_data[j] = ysaved_data[j];
  208. }
  209. if (jac_res) {
  210. return 1;
  211. }
  212. /* store result */
  213. for (i = 0; i < N; i++) {
  214. int j = QJacobianGC_[gc_ix + i];
  215. if (j-- >= 1) {
  216. realtype val = (ftmp_data[i] - fy_data[i]) / dy_data[j];
  217. if (J != NULL) {
  218. DENSE_ELEM(J, i, j) = val;
  219. } else {
  220. val = DENSE_ELEM(comp->iData->J, i, j);
  221. Jv_data[i] += val * v_data[j];
  222. }
  223. }
  224. }
  225. group_ix += 1 + nmembers;
  226. }
  227. return 0;
  228. }
  229. /* ------------------------------------------------------ */
  230. static int compute_Jconf(N_Vector y, N_Vector fy, realtype* srur, realtype* mindy,
  231. FMIComponent c)
  232. {
  233. /* the computation of minimal dy is borrowed from the default Jacobian
  234. cvDlsDenseDQJac in sundials */
  235. Component* comp = (Component*) c;
  236. realtype fnorm;
  237. realtype* y_data = N_VGetArrayPointer(y);
  238. realtype hcur;
  239. int flag = CVodeGetErrWeights(comp->iData->cvode_mem, comp->iData->ewt);
  240. if (util_check_flag(&flag, "CVodeGetErrWeights", 0, c)) return 1;
  241. /* base on uround and norm of f */
  242. *srur = SUNRsqrt(UNIT_ROUNDOFF);
  243. fnorm = N_VWrmsNorm(fy, comp->iData->ewt);
  244. flag = CVodeGetCurrentStep(comp->iData->cvode_mem, &hcur);
  245. if (util_check_flag(&flag, "CVodeGetCurrentStep", 1, c)) return 1;
  246. *mindy = (fnorm != RCONST(0.0)) ?
  247. (RCONST(1000.0) * SUNRabs(hcur) * UNIT_ROUNDOFF * comp->nStates * fnorm) : RCONST(1.0);
  248. return 0;
  249. }