-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFspSensCVodeMex.c
378 lines (338 loc) · 13.9 KB
/
FspSensCVodeMex.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include <math.h>
#include <mex.h>
#include "FspCvodeForwardSens.h"
#define MXSetScalarLayout(p){\
mxSetM(p, 1); mxSetN(p, 1);\
}
#define MXSetVecLength(p, len){\
mxSetM(p, (mwSize) len);\
mxSetN(p, 1);\
}
#define MXSetEmptyLayout(p){\
mxSetM(p,0); mxSetN(p,0);\
mxSetData(p, NULL);\
}
typedef struct f_out_ml_dat {
mxArray *output_cell;
mxArray *function_handle;
mxArray *finput[3];
mxArray **sens_vec_ptr;
int n_par;
int n_state;
};
typedef struct f_stop_ml_dat {
mxArray *function_handle;
mxArray *t;
mxArray *p;
mxArray *stop_cond;
mxArray *stop_status;
int n_state;
mxArray* plhs[2];
mxArray* prhs[4];
double cput;
};
typedef struct mv_ml_dat {
mxArray *function_handle;
mxArray *input_args[2];
int n_state;
};
typedef struct dmv_ml_dat {
mxArray *function_handle_cell;
int n_par;
mxArray *input_args[2];
int n_state;
};
void CreateFOutData(struct f_out_ml_dat *fdat, const mxArray *out_cell, const mxArray *fhandle, int n_par, int n_state){
fdat->n_par = n_par;
fdat->n_state = n_state;
fdat->output_cell = out_cell;
fdat->function_handle = fhandle;
fdat->finput[0] = mxCreateNumericMatrix(0, 0, mxDOUBLE_CLASS, mxREAL);
fdat->finput[1] = mxCreateNumericMatrix(0, 0, mxDOUBLE_CLASS, mxREAL);
fdat->finput[2] = mxCreateCellMatrix(n_par, 1);
// Initialize the pointers to sensitivity vectors
fdat->sens_vec_ptr = mxMalloc(n_par*sizeof(mxArray*));
for (int i = 0; i < n_par; ++i){
fdat->sens_vec_ptr[i] = mxCreateNumericMatrix(0, 0, mxDOUBLE_CLASS, mxREAL);
mxSetCell(fdat->finput[2], i, fdat->sens_vec_ptr[i]);
}
}
void DestroyFOutData(struct f_out_ml_dat *fdat){
mxDestroyArray(fdat->finput[0]);
mxDestroyArray(fdat->finput[1]);
mxDestroyArray(fdat->finput[2]);
for (int i = 0; i < fdat->n_par; ++i){
mxDestroyArray(fdat->sens_vec_ptr[i]);
}
}
void CreateFStopData(struct f_stop_ml_dat *f_stop_dat, const mxArray *stop_cond, const mxArray *fhandle, int n_state){
f_stop_dat->stop_cond = stop_cond;
f_stop_dat->function_handle = fhandle;
f_stop_dat->t = mxCreateNumericMatrix(0, 0, mxDOUBLE_CLASS, mxREAL);
f_stop_dat->p = mxCreateNumericMatrix(0, 0, mxDOUBLE_CLASS, mxREAL);
f_stop_dat->n_state = n_state;
f_stop_dat->stop_status = NULL;
f_stop_dat->prhs[0] = f_stop_dat->function_handle;
f_stop_dat->prhs[1] = f_stop_dat->t;
f_stop_dat->prhs[2] = f_stop_dat->p;
f_stop_dat->prhs[3] = f_stop_dat->stop_cond;
f_stop_dat->cput = 0.0;
}
void DestroyFStopData(struct f_stop_ml_dat *f_stop_dat){
mxDestroyArray(f_stop_dat->t);
mxDestroyArray(f_stop_dat->p);
}
void CreateMVData(struct mv_ml_dat *mv_dat, const mxArray *fhandle, int n_state){
mv_dat->function_handle = fhandle;
mv_dat->n_state = n_state;
mv_dat->input_args[0] = mxCreateNumericMatrix(1, 1, mxDOUBLE_CLASS, mxREAL);
mv_dat->input_args[1] = mxCreateNumericMatrix(0, 0, mxDOUBLE_CLASS, mxREAL);
}
void DestroyMVData(struct mv_ml_dat *mv_dat){
mxDestroyArray(mv_dat->input_args[0]);
mxDestroyArray(mv_dat->input_args[1]);
}
void CreateDMVData(struct dmv_ml_dat *dmv_dat, const mxArray *fhandle_cell, int n_par, int n_state){
dmv_dat->function_handle_cell = fhandle_cell;
dmv_dat->n_par = n_par;
dmv_dat->n_state = n_state;
dmv_dat->input_args[0] = mxCreateNumericMatrix(1, 1, mxDOUBLE_CLASS, mxREAL);
dmv_dat->input_args[1] = mxCreateNumericMatrix(0, 0, mxDOUBLE_CLASS, mxREAL);
}
void DestroyDMVData(struct dmv_ml_dat *dmv_dat){
mxDestroyArray(dmv_dat->input_args[0]);
mxDestroyArray(dmv_dat->input_args[1]);
}
void mex_out_fun(int i_tspan, double t, double *p, double **dp, void* output_mem){
struct f_out_ml_dat *f_dat = (struct f_out_ml_dat *) output_mem;
// Put in input arrays into the arguments of the output function
mxSetDoubles(f_dat->finput[0], &t); MXSetScalarLayout(f_dat->finput[0]);
mxSetDoubles(f_dat->finput[1], p); MXSetVecLength(f_dat->finput[1], f_dat->n_state);
// Set pointers to dp
for (int i=0; i < f_dat->n_par; ++i){
mxSetDoubles(f_dat->sens_vec_ptr[i], dp[i]);
MXSetVecLength(f_dat->sens_vec_ptr[i], f_dat->n_state);
}
// Call the function handle
int ierr;
mxArray *lhs[1];
mxArray *rhs[4];
rhs[0] = f_dat->function_handle;
rhs[1] = f_dat->finput[0];
rhs[2] = f_dat->finput[1];
rhs[3] = f_dat->finput[2];
ierr = mexCallMATLAB(1, lhs, 4, rhs, "feval");
if (ierr!=0){
mexErrMsgIdAndTxt("MATLAB:FSPSensCVodeSolve",
"Error when calling user-suplied output function.");
exit(-1);
}
// Enter the result to the output cell
mxSetCell(f_dat->output_cell, i_tspan, lhs[0]);
// Return dp to the calling process (i.e., remove references from sens_vec_ptr)
MXSetEmptyLayout(f_dat->finput[0]);
MXSetEmptyLayout(f_dat->finput[1]);
for (int i=0; i < f_dat->n_par; ++i){
MXSetEmptyLayout(f_dat->sens_vec_ptr[i]);
}
}
void mex_stop_fun( double t, double *p, void *stopping_data, int *stop){
struct f_stop_ml_dat *fstop_dat = (struct f_stop_ml_dat *) stopping_data;
// Attach matlab function to the input arrays
mxSetDoubles(fstop_dat->t, &t); MXSetScalarLayout(fstop_dat->t);
mxSetDoubles(fstop_dat->p, p); MXSetVecLength(fstop_dat->p, fstop_dat->n_state);
// Evaluate the stopping criteria
int ierr;
ierr = mexCallMATLAB(2, fstop_dat->plhs, 4, fstop_dat->prhs, "feval");
if (ierr!=0){
mexErrMsgIdAndTxt("MATLAB:FspCvodeMex",
"Error when calling user-suplied stop function.");
exit(-1);
}
// Update the stop status and decision
if (fstop_dat->stop_status != NULL){
mxDestroyArray(fstop_dat->stop_status);
fstop_dat->stop_status = NULL;
}
fstop_dat->stop_status = mxDuplicateArray(fstop_dat->plhs[1]);
double *istop_db;
if (!mxIsDouble(fstop_dat->plhs[0])){
mexErrMsgIdAndTxt("MATLAB:FspCvodeMex",
"First ouput of the stopping evaluation function must be a real value of class double.");
exit(-1);
}
istop_db = mxGetDoubles(fstop_dat->plhs[0]);
*stop = (int) *istop_db;
mxDestroyArray(fstop_dat->plhs[0]);
mxDestroyArray(fstop_dat->plhs[1]);
// Detach matlab function from the input arrays
MXSetEmptyLayout(fstop_dat->t);
MXSetEmptyLayout(fstop_dat->p);
}
void mex_matvec(int n_state, double t, double *x, double *y, void *user_data){
struct mv_ml_dat* mv_dat = (struct mv_ml_dat*) user_data;
mxArray *lhs[1];
mxArray *rhs[3];
// Point mv_dat internal data to the input arrays
mxSetDoubles(mv_dat->input_args[0], &t);
mxSetDoubles(mv_dat->input_args[1], x);
mxSetM(mv_dat->input_args[1], (mwSize) mv_dat->n_state); mxSetN(mv_dat->input_args[1], 1);
// Form the rhs for feval
rhs[0] = mv_dat->function_handle;
rhs[1] = mv_dat->input_args[0];
rhs[2] = mv_dat->input_args[1];
// Call the matrix action handle from MATLAB
int ierr;
ierr = mexCallMATLAB(1, lhs, 3, rhs, "feval");
if (ierr!=0){
mexErrMsgIdAndTxt("MATLAB:FSPSensCVodeSolve",
"Error when calling user-suplied matvec function.");
exit(-1);
}
// Copy the result of matvec to the output array
mxDouble* out_ptr = mxGetDoubles(lhs[0]);
for ( int i = 0; i < n_state; ++i ) {
y[i] = out_ptr[i];
}
mxDestroyArray(lhs[0]);
// Un-reference the mv_dat internal data from the input arrays
mxSetDoubles(mv_dat->input_args[0], NULL);
mxSetDoubles(mv_dat->input_args[1], NULL);
mxSetM(mv_dat->input_args[1], 0); mxSetN(mv_dat->input_args[1], 0);
}
void mex_dmatvec(int n_state, int i_par, double t, double *x, double *y, void *user_data){
struct dmv_ml_dat* dmv_dat = (struct dmv_ml_dat*) user_data;
mxArray *lhs[1];
mxArray *rhs[3];
// Point mv_dat internal data to the input arrays
mxSetDoubles(dmv_dat->input_args[0], &t);
mxSetDoubles(dmv_dat->input_args[1], x);
mxSetM(dmv_dat->input_args[1], (mwSize) dmv_dat->n_state); mxSetN(dmv_dat->input_args[1], 1);
// Form the rhs for feval
rhs[0] = mxGetCell(dmv_dat->function_handle_cell, i_par);
rhs[1] = dmv_dat->input_args[0];
rhs[2] = dmv_dat->input_args[1];
// Call the matrix action handle from MATLAB
int ierr;
ierr = mexCallMATLAB(1, lhs, 3, rhs, "feval");
if (ierr!=0){
mexErrMsgIdAndTxt("MATLAB:FSPSensCVodeSolve",
"Error when calling user-suplied matvec function.");
exit(-1);
}
// Copy the result of matvec to the output array
mxDouble* out_ptr = mxGetDoubles(lhs[0]);
for ( int i = 0; i < n_state; ++i ) {
y[i] = out_ptr[i];
}
mxDestroyArray(lhs[0]);
// Un-reference the mv_dat internal data from the input arrays
mxSetDoubles(dmv_dat->input_args[0], NULL);
mxSetDoubles(dmv_dat->input_args[1], NULL);
mxSetM(dmv_dat->input_args[1], 0); mxSetN(dmv_dat->input_args[1], 0);
}
/* Mex Function to evaluate a generic function of the CME solution and sensitivities
* This function should be called in Matlab with the following syntax
* [y_out, stop_status] = FSPSensCVodeSolve(t_start, t_out, Av, dAv, p0, dp0, f_out, f_stop, stop_cond)
* where
*
* t_start : initial time.
*
* t_out : vector of output times.
*
* Av : function handle to evaluate action of the (possibly time-dependent) CME matrix, callable in the form y = Av(t, x).
*
* dAv : cell of function handles to evaluate action of the partial derivative of the CME matrix, callable in the form y = dAv{i}(t, x)
*where i is the index of the parameter with respect to which the partial derivative is taken.
*
* p0 : column vector of the initial probability vector.
*
* dp0 : column vector made by stacking together the initial sensitivity vectors.
*
* f_out : function handle to evaluate the outputs, callable in the form y = f_out(p, dp) where p is the probability vector and dp
* contains all sensitivity vectors at a time point.
*
* * f_stop : function handle to evaluate whether to stop the ODE integration before reaching final time (for example, when the FSP error exceeds tolerance).
* This function should be callable in Matlab in the form [istop, stop_stat] = f_stop(t, p, stop_cond) where t is the current time for the ODE solver and p is the current solution. Here, istop = 0 if p(t) passes the stopping condition, and 1 otherwise. The second output contains further information (for example, sum of sink states in the FSP).
*
* stop_cond : an array/cell/struct compatible with f_stop, containing the information needed by f_stop to make decision.
*
* y_out : cell array with the same number of entries as t_out, used to store the results of f_out evaluations.
*
* stop_status : the status of the solver
*/
void
mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
/* Check that input arguments are correct */
if (nlhs != 2){
mexErrMsgIdAndTxt( "MATLAB:FSPSensCVodeSolve",
"Not enough outputs.");
}
if (nrhs != 9){
mexErrMsgIdAndTxt("MATLAB:FSPSensCVodeSolve",
"Not enough inputs.");
}
if (!mxIsClass(prhs[0], "double")){
mexErrMsgIdAndTxt("MATLAB:FSPSensCVodeSolve",
"Input t_start must be of class double.");
}
if (!mxIsClass(prhs[1], "double")){
mexErrMsgIdAndTxt("MATLAB:FSPSensCVodeSolve",
"Input tspan must be of class double.");
}
if (!mxIsClass(prhs[2], "function_handle")){
mexErrMsgIdAndTxt("MATLAB:FSPSensCVodeSolve",
"Third argument must be a function handle.");
}
if (!mxIsClass(prhs[3], "cell")){
mexErrMsgIdAndTxt("MATLAB:FSPSensCVodeSolve",
"Fourth argument must be a cell of function handles.");
}
if (!mxIsClass(prhs[4], "double")){
mexErrMsgIdAndTxt("MATLAB:FSPSensCVodeSolve",
"Fifth argument must be a column vector of double type.");
}
if (!mxIsClass(prhs[5], "double")){
mexErrMsgIdAndTxt("MATLAB:FSPSensCVodeSolve",
"Sixth argument must be a column vector of double type.");
}
if (!mxIsClass(prhs[6], "function_handle")){
mexErrMsgIdAndTxt("MATLAB:FSPSensCVodeSolve",
"Seventh argument must be a function handle.");
}
size_t n_tspan = mxGetNumberOfElements(prhs[1]);
size_t n_state = mxGetNumberOfElements(prhs[4]);
size_t n_par = mxGetNumberOfElements(prhs[5])/n_state;
plhs[0] = mxCreateCellMatrix(n_tspan, 1);
printf("Evaluating for %d time points with %d sensitivity parameters and %d states.\n",
n_tspan, n_par, n_state);
struct f_out_ml_dat fout_dat;
struct f_stop_ml_dat fstop_dat;
struct mv_ml_dat mv_dat;
struct dmv_ml_dat dmv_dat;
CreateFOutData(&fout_dat, plhs[0], prhs[6], n_par, n_state);
CreateMVData(&mv_dat, prhs[2], n_state);
CreateDMVData(&dmv_dat, prhs[3], n_par, n_state);
CreateFStopData(&fstop_dat, prhs[8], prhs[7], n_state);
double* p0_ptr;
double* dp0_ptr;
double* t_start;
double* tspan;
p0_ptr = (double*)mxGetPr(prhs[4]);
dp0_ptr = (double*)mxGetPr(prhs[5]);
t_start = mxGetDoubles(prhs[0]);
tspan = (double*)mxGetPr(prhs[1]);
int ierr = FspCVodeForwardSens( n_tspan, *t_start, tspan, n_state, n_par, p0_ptr, dp0_ptr, &mex_matvec, ( void * ) &mv_dat,
&mex_dmatvec, ( void * ) &dmv_dat, &mex_out_fun, ( void * ) &fout_dat,
&mex_stop_fun, ( void * ) &fstop_dat );
if (ierr != 0){
mexErrMsgIdAndTxt("MATLAB:FSPSensCVodeSolve",
"CVode gives error status %d \n.", ierr);
}
plhs[1] = mxDuplicateArray(fstop_dat.stop_status);
DestroyDMVData(&dmv_dat);
DestroyMVData(&mv_dat);
DestroyFStopData(&fstop_dat);
DestroyFOutData(&fout_dat);
}