| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- /* Implementation of jac.h */
- #include "jac.h"
- #include "util.h"
- #include "integration.h"
- #include "fmiFunctions_fwd.h"
- /* Sundials */
- #include <cvode/cvode.h>
- #include <sundials/sundials_math.h>
- #include <nvector/nvector_serial.h>
- #include <sundials/sundials_direct.h>
- #include <assert.h>
- #include <time.h>
- /* ----------------- local function declarations ----------------- */
- /* common function for J and J * v computation */
- static int compute_Jdata(realtype t, N_Vector y, N_Vector fy, N_Vector v,
- DlsMat J, N_Vector Jv,
- realtype srur, realtype mindy, realtype* ewt_data,
- N_Vector tmp, FMIComponent c);
- /* computation of parameters for numeric computation of J */
- static int compute_Jconf(N_Vector y, N_Vector fy, realtype* srur, realtype* mindy,
- FMIComponent c);
- /* ----------------- local variables ----------------- */
- static size_t nGroups;
- /* ----------------- external variables ----------------- */
- extern int QJacobianCG_[];
- extern int QJacobianGC_[];
- /* ----------------- external function declarations ----------------- */
- /* ----------------- function definitions ----------------- */
- DYMOLA_STATIC void jac_setup(size_t ngroups){
- nGroups = ngroups;
- }
- /* ------------------------------------------------------ */
- /* computes function f(t,y) */
- DYMOLA_STATIC int jac_f(double t, N_Vector y, N_Vector ydot, void *user_data)
- {
- clock_t t0 = clock();
- FMIComponent* c = (FMIComponent*) user_data;
- Component* comp = (Component*) c;
- int retval = 0;
- N_VectorContent_Serial ydot_cont = NV_CONTENT_S(ydot);
- /* temporarily change time, states and inputs */
- FMIReal time = comp->time;
- FMIReal* states = comp->states;
- integration_extrapolate_inputs(comp, t);
- comp->states = N_VGetArrayPointer(y);
- comp->time = t;
- comp->icall = iDemandStart;
- if (fmiGetDerivatives_(c, ydot_cont->data, ydot_cont->length) != FMIOK) {
- retval = 1;
- }
- /* restore time, states and inputs */
- integration_extrapolate_inputs(comp, time);
- comp->states = states;
- comp->time = time;
- comp->iData->stat.fCalls++;
- comp->iData->stat.fTime += (clock() - t0);
- return retval;
- }
- /* ------------------------------------------------------ */
- /* Jacobian routine, computes J(t,y) = df/dy numerically */
- DYMOLA_STATIC int jac_Jacobian(long N, realtype t,
- N_Vector y, N_Vector fy, DlsMat J, void *user_data,
- N_Vector tmp1, N_Vector tmp2, N_Vector tmp3)
- {
- FMIComponent* c = (FMIComponent*) user_data;
- Component* comp = (Component*) c;
- N_Vector ftmp = tmp1;
- realtype* ewt_data;
- realtype srur;
- realtype mindy;
- int j;
- /* error here is recoverable */
- int retval = 1;
- /* for later restoring */
- realtype* tmp2_data = N_VGetArrayPointer(tmp2);
- /* need raw pointers for some */
- realtype* y_data = N_VGetArrayPointer(y);
- realtype* fy_data = N_VGetArrayPointer(fy);
- realtype* ftmp_data = N_VGetArrayPointer(ftmp);
- if (compute_Jconf(y, fy, &srur, &mindy, c)) {
- goto done;
- }
- ewt_data = N_VGetArrayPointer(comp->iData->ewt);
- comp->istruct->mInJacobian = 1;
- /* use grouping if possible */
- if (nGroups > 0) {
- /* use grouping */
- /* cannot use tmp2 here since it reuses the pointer provided in N_VMake_Serial,
- hence interfering with comp->states */
- if (!compute_Jdata(t, y, fy, NULL, J, NULL, srur, mindy, ewt_data, tmp3, c)) {
- retval = 0;
- goto done;
- }
- /* failed with grouping - fall back */
- }
- {
- /* no or failed grouping, perturb one variable at the time */
- N_Vector jthCol = tmp2;
-
- for (j = 0; j < N; j++) {
- realtype yjsaved;
- realtype dy1, dy2;
- realtype dy;
- realtype dy_inv;
- int k;
- /* generate the jth col of J(tn, y) */
- N_VSetArrayPointer(DENSE_COL(J, j), jthCol);
- yjsaved = y_data[j];
- /* perturb */
- dy1 = srur * SUNRabs(yjsaved);
- dy2 = mindy / ewt_data[j];
- dy = SUNMAX(dy1, dy2);
- /* keep sign to avoid y_data near 0 */
- if (yjsaved < 0) dy = -dy;
- /* retry a few times in case of failure */
- for (k = 0; k < 5; k++) {
- y_data[j] = yjsaved + dy;
- /* reduce rounding error at a low cost */
- dy = y_data[j] - yjsaved;
- /* compute new f */
- if (!jac_f(t, y, ftmp, user_data)) {
- break;
- }
- dy = -dy / 5; /* try smaller increment, and different sign */
- if (k>5) {
- /* failed */
- y_data[j] = yjsaved;
- goto done;
- }
- }
- y_data[j] = yjsaved;
- dy_inv = RCONST(1.0) / dy;
- /* concisesness for the cost of duplicated multiplication */
- N_VLinearSum(dy_inv, ftmp, -dy_inv, fy, jthCol);
- }
- }
- retval = 0;
- done:
- comp->istruct->mInJacobian = 0;
- /* restoring original array pointer appears to be necessary */
- N_VSetArrayPointer(tmp2_data, tmp2);
- comp->iData->stat.JacCalls++;
- return retval;
- }
- /* ----------------- local function definitions ----------------- */
- static int compute_Jdata(realtype t, N_Vector y, N_Vector fy, N_Vector v,
- DlsMat J, N_Vector Jv,
- realtype srur, realtype mindy, realtype* ewt_data,
- N_Vector tmp, FMIComponent c)
- {
- Component* comp = (Component*) c;
- N_Vector ftmp = comp->iData->tmp1;
- realtype* y_data = N_VGetArrayPointer(y);
- realtype* fy_data = N_VGetArrayPointer(fy);
- realtype* v_data;
- realtype* Jv_data;
- realtype* ftmp_data = N_VGetArrayPointer(ftmp);
- realtype* ysaved_data = N_VGetArrayPointer(comp->iData->tmp2);
- realtype* dy_data = N_VGetArrayPointer(tmp);
- size_t group_nr;
- int group_ix = 1;
- int j;
- int N = (int)comp->nStates;
- /* compute exactly one of J or J * v */
- assert(J != NULL && Jv == NULL || J == NULL && Jv != NULL);
- /* initialize result */
- if (J != NULL) {
- for(j = 0; j < N; j++) {
- /* temporarily borrow tmp */
- N_VSetArrayPointer(DENSE_COL(J, j), tmp);
- N_VConst(0, tmp);
- }
- } else {
- v_data = N_VGetArrayPointer(v);
- Jv_data = N_VGetArrayPointer(Jv);
- N_VConst(0, Jv);
- }
-
- N_VSetArrayPointer(dy_data, tmp);
- /* iterate the groups */
- for (group_nr = 0; group_nr < nGroups; group_nr++) {
- int nmembers = QJacobianCG_[group_ix];
- int member;
- int i;
- int gc_ix = (int)(N * group_nr);
- int jac_res;
- /* perturb each group member */
- for (member = 1; member <= nmembers; member++) {
- realtype dy1, dy2;
- /* fetch column */
- int j = QJacobianCG_[group_ix + member] - 1;
- ysaved_data[j] = y_data[j];
- /* perturb the jth variable */
- dy1 = srur * SUNRabs(ysaved_data[j]);
- dy2 = mindy / ewt_data[j];
- dy_data[j] = SUNMAX(dy1, dy2);
- /* keep sign to avoid y_data near 0 */
- if (ysaved_data[j] < 0) dy_data[j] = -dy_data[j];
- y_data[j] += dy_data[j];
- /* reduce rounding error at a low cost */
- dy_data[j] = y_data[j] - ysaved_data[j];
- }
- /* compute new f */
- jac_res = jac_f(t, y, ftmp, c);
-
- /* restore y */
- for (member = 1; member <= nmembers; member++) {
- /* fetch column */
- j = QJacobianCG_[group_ix + member] - 1;
- y_data[j] = ysaved_data[j];
- }
- if (jac_res) {
- return 1;
- }
- /* store result */
- for (i = 0; i < N; i++) {
- int j = QJacobianGC_[gc_ix + i];
- if (j-- >= 1) {
- realtype val = (ftmp_data[i] - fy_data[i]) / dy_data[j];
- if (J != NULL) {
- DENSE_ELEM(J, i, j) = val;
- } else {
- val = DENSE_ELEM(comp->iData->J, i, j);
- Jv_data[i] += val * v_data[j];
- }
- }
- }
- group_ix += 1 + nmembers;
- }
- return 0;
- }
- /* ------------------------------------------------------ */
- static int compute_Jconf(N_Vector y, N_Vector fy, realtype* srur, realtype* mindy,
- FMIComponent c)
- {
- /* the computation of minimal dy is borrowed from the default Jacobian
- cvDlsDenseDQJac in sundials */
- Component* comp = (Component*) c;
- realtype fnorm;
- realtype* y_data = N_VGetArrayPointer(y);
- realtype hcur;
- int flag = CVodeGetErrWeights(comp->iData->cvode_mem, comp->iData->ewt);
- if (util_check_flag(&flag, "CVodeGetErrWeights", 0, c)) return 1;
- /* base on uround and norm of f */
- *srur = SUNRsqrt(UNIT_ROUNDOFF);
- fnorm = N_VWrmsNorm(fy, comp->iData->ewt);
- flag = CVodeGetCurrentStep(comp->iData->cvode_mem, &hcur);
- if (util_check_flag(&flag, "CVodeGetCurrentStep", 1, c)) return 1;
- *mindy = (fnorm != RCONST(0.0)) ?
- (RCONST(1000.0) * SUNRabs(hcur) * UNIT_ROUNDOFF * comp->nStates * fnorm) : RCONST(1.0);
- return 0;
- }
|