/
jac.c
306 lines (249 loc) · 8.21 KB
/
jac.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* 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(int 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 * ABS(yjsaved);
dy2 = mindy / ewt_data[j];
dy = MAX(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 * ABS(ysaved_data[j]);
dy2 = mindy / ewt_data[j];
dy_data[j] = MAX(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 = RSqrt(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) * ABS(hcur) * UNIT_ROUNDOFF * comp->nStates * fnorm) : RCONST(1.0);
return 0;
}