-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNLP_u.c
377 lines (335 loc) · 12.7 KB
/
NLP_u.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
/* This file contains a template for the model-specific information and code.
* The ??? symbols should be replaced with appropriate code and then the file
* s_parm_reduction.h should be editted to add this model to the list in the
* variable all_models. Then the program should be recompiled. */
/* First define a prefix for the function names which is unique to this model.
* Put this in quotes in the definition of MODEL_NAME, and put it without quotes
* in the definition of NAMEGEN. */
#define MODEL_NAME "NLP_u"
#define NAMEGEN(A) NLP_u ## A
/* Next define values for the model: */
#define DEDIM 2 /* Enter the dimension of the Differential Equation. */
#define OFDIM 1 /* Enter the dimension of other functions. */
#define AEDIM 0 /* Enter the dimension of the Algebraic State Equations */
#define NPARMS 3 /* Enter the number of parameters in the DE. */
#define NISPECS 0 /* Enter number of integer specs for the model. */
#define NFSPECS 1 /* Enter number of floating point specs for the model. */
/* The user must code two external variables and two functions for the MODEL.
* The two variables simply define whether each equation in the model involves
* each parameter or variable.
* The two functions are:
* 1) A dependence function which determines whether the vector field is monotonic
* with respect to the parameters.
* 2) A function which computes f + ay where f is the vector field.
*/
/* Do not change the next 4 lines. */
#include <stdlib.h>
#include <stdio.h>
#include <float.h>
#include "parm_red_structs.h"
#include "readtoken.h"
/* Add any other include files or defines needed for the coded functions. */
#include <math.h>
#include "interval_math.h"
#define GRAV (model->fspecs[0])
/* NLP: Forced, damped, nonlinear pendulum.
* The vector field is
* f = [ y[1] ]
* [ -GRAV*p[0]*sin(y[0]) - p[1]*y[1] + p[2]*u[0] ]
* where
* GRAV is the the gravitational acceleration,
* p[0] is 1/L
* p[1] is a/m
* p[2] is 1/mL
* u[0] is the driving force
* L is the length of the pendulum
* a is the damping coefficient
* m is the mass.
* It is assumed grav is known precisely.
* */
/* Specify whether each equation involves the parameters and variables and other
* functions. 1 indicates this equation depends on the parameter/variable,
* 0 indicates no. */
static int PARMDEP[DEDIM][NPARMS] = {
{0, 0, 0},
{1, 1, 1}
};
static int VARDEP[DEDIM][DEDIM] = {
{0, 1},
{1, 1}
};
static int VARPARTIALDEP[DEDIM+AEDIM][DEDIM+1] = {
{0, 0, 0},
{1, 1, 0},
};
static int OFDEP[DEDIM][OFDIM] = {
{0},
{1}
};
static int OFPARTIALDEP[DEDIM+AEDIM][OFDIM] = {
{0},
{1}
};
/* Specify any a priori extreme bounds on the parameters. Use +-DBL_MAX to
* represent +- infinity, if there are no bounds, and +-DBL_MIN for the numbers
* closest to zero. */
static double P_EXTREME[NPARMS][2] = {
{DBL_MIN, DBL_MAX},
{DBL_MIN, DBL_MAX},
{DBL_MIN, DBL_MAX}
};
static double Y_EXTREME[DEDIM][2] = {
{-DBL_MAX, DBL_MAX},
{-DBL_MAX, DBL_MAX}
};
void NAMEGEN(_determine_dependence)(int eqn, Range *p, Range t, Range *y, Range *u, Range partial,
struct s_model *model, int *dep, struct s_discr *discr, int parm) {
Range z;
double temp;
/* dep[i]=1 if equation eqn of the vector field is monotonically nondecreasing
* with p[i] for each range of t, y, and u values. -1 if it is monotonically
* nonincreasing, 0 if it does not depend on p[i], and -2 if it is not monotonic.
* Return a value of 0 if any of dep entries are -2, else return 1. */
switch (eqn) {
case 0:
if (model->var == 0) { /* If y[0] (x) is unmeasured */
partial[0] = 0.0;
partial[1] = 0.0;
}
else if (model->var == 1) { /* If y[1] (y) is unmeasured */
partial[0] = 1.0;
partial[1] = 1.0;
}
break;
default: /* eqn == 1 */
switch (parm) {
case 0:
int_sine(y[0],z);
int_put(z,partial);
multiply(-GRAV,partial);
if (ISZERO_INTERIOR(z)) {
dep[parm] = -3;
}
else {
temp = z[0] + z[1];
if (temp > 0.0)
dep[parm] = -1;
else if (temp < 0.0)
dep[parm] = 1;
else
dep[parm] = 0;
}
break;
case 1:
int_put(y[1],partial);
multiply(-1.0,partial);
if (ISZERO_INTERIOR(y[1])) {
dep[parm] = -3;
}
else {
temp = y[1][0] + y[1][1];
if (temp > 0.0)
dep[parm] = -1;
else if (temp < 0.0)
dep[parm] = 1;
else
dep[parm] = 0;
}
break;
case 2:
int_put(u[0],partial);
if (ISZERO_INTERIOR(u[0])) {
dep[parm] = -3;
}
else {
temp = u[0][0] + u[0][1];
if (temp > 0.0)
dep[parm] = 1;
else if (temp < 0.0)
dep[parm] = -1;
else
dep[parm] = 0;
break;
}
break;
default: /* unmeasured variable */
if (model->var == 0) { /* If y[0] (x) is unmeasured */
int_cosine(y[0],partial);
int_multiply(p[0],partial,partial);
multiply(GRAV,partial);
multiply(-1.0,partial);
}
else { /* If y[1] (y) is unmeasured */
int_put(p[1],partial);
multiply(-1.0,partial);
}
}
}
return;
}
double NAMEGEN(_vec_field)(int cor, int eqn, double *p, double a, double t,
Range *y, Range *u, struct s_model *model) {
/* This function returns either the lower or upper value (depending on whether
* cor is 0 or 1) of the eqn'th equation of f + ay, where f is the vector
* field.
*
* The parameters p are assumed nonnegative. */
int j,opp_cor;
Range z;
double g,temp;
if (cor == 0) opp_cor = 1;
else opp_cor = 0;
switch (eqn) {
case 0:
if (a >= 0.0) j = cor;
else j = opp_cor;
g = y[1][cor] + a*y[0][j];
break;
default: /* eqn == 1 */
int_sine(y[0],z);
g = -GRAV*p[0]*z[opp_cor];
temp = -p[1] + a;
if (temp >= 0.0) j = cor;
else j = opp_cor;
g += temp*y[1][j];
temp = p[2];
if (temp >= 0) j = cor;
else j = opp_cor;
g += temp*u[0][j];
}
return g;
}
double NAMEGEN(_interval_vec_field)(int eqn, int cor, Range *p, double *t, Range **y, Range **u,
struct s_model *model, struct s_discr *discr) {
/* This function returns either the lower or upper value (depending on whether
* cor is 0 or 1) of the eqn'th equation of f + ay, where f is the vector
* field.
*
* The parameters p are assumed nonnegative. */
int i;
Range *z=model->window.z;
Range g,w;
switch (eqn) {
case 0:
printf("OOPS! I thought NLP equation 1 would always be monotonic!\n");
/* no need to code, never non-monotonic */
exit(1);
break;
default: /* eqn == 1 */
for (i=discr->sub_window[0]; i<discr->sub_window[1]; i++) int_sine(y[0][i],z[i]);
int_vfield_sum(z,w,discr);
int_multiply(p[0],w,w);
multiply(-GRAV,w);
int_put(w,g);
int_vfield_sum(y[1],w,discr);
int_multiply(p[1],w,w);
multiply(-1.0,w);
int_add(g,w,g);
int_vfield_sum(u[0],w,discr);
int_multiply(p[2],w,w);
int_add(g,w,g);
break;
}
return g[cor];
}
int NAMEGEN(_invert_vec_field)(int eqn, int i, Range F, struct s_model *model, Range *p, double t, Range *y, Range *u) {
/* in this function, we are solving for parameter i in equation eqn.
* If we start with f^s(p_i,**) = F (f^s is the vector field at the s-th time step, and F is the input to the function
* then we must invert f^s, and find the expression p_i = (f^s)^(-1)(F,**) */
int ok = 0;
Range hull,K;
switch (eqn) {
case 1:
switch (i) {
case 0:
int_multiply(p[1],y[1],K);
int_add(F,K,hull);
int_multiply(p[2],u[0],K);
int_subtract(hull,K,hull);
int_sine(y[0],K);
multiply(-GRAV,K);
int_divide(hull,K,hull);
ok = 1;
break;
case 1:
int_sine(y[0],K);
int_multiply(p[0],K,K);
multiply(GRAV,K);
int_add(F,K,hull);
int_multiply(p[2],u[0],K);
int_subtract(hull,K,hull);
int_divide(hull,y[1],hull);
multiply(-1.0,hull);
ok = 1;
break;
case 2:
int_sine(y[0],K);
int_multiply(p[0],K,K);
multiply(GRAV,K);
int_add(F,K,hull);
int_multiply(p[1],y[1],K);
int_add(hull,K,hull);
int_divide(hull,u[0],hull);
ok = 1;
break;
}
break;
}
/* now compare parameter to the interval hull */
if (ok) ok = intersect(p[i],hull);
return ok;
}
/* Do not edit below this line. */
/* This function initializes the model structure. */
void NAMEGEN(_model_init)(struct s_model *model, FILE *fptr) {
char sep;
int i,j;
model->name = MODEL_NAME;
model->DEdim = DEDIM;
model->OFdim = OFDIM;
model->AEdim = AEDIM;
model->TEdim = AEDIM+DEDIM;
model->nparms = NPARMS;
model->p_extreme = (Range *) malloc(NPARMS*sizeof(Range));
for (i=0; i<NPARMS; i++) {
for (j=0; j<2; j++) model->p_extreme[i][j] = P_EXTREME[i][j];
}
model->y_extreme = (Range *) malloc(DEDIM*sizeof(Range));
for (i=0; i<DEDIM; i++) {
for (j=0; j<2; j++) model->y_extreme[i][j] = Y_EXTREME[i][j];
}
model->n_ispecs = NISPECS;
model->ispecs = (int *) malloc(NISPECS*sizeof(int));
if (readtoken(fptr,NISPECS,"model_ispecs","%d",model->ispecs,&sep)
!= NISPECS) {
fprintf(stderr,"parm_reduction: error reading model_ispecs.\n");
exit(1);
}
model->n_fspecs = NFSPECS;
model->fspecs = (double *) malloc(NFSPECS*sizeof(double));
if (readtoken(fptr,NFSPECS,"model_fspecs","%lf",model->fspecs,&sep)
!= NFSPECS) {
fprintf(stderr,"parm_reduction: error reading model_fspecs.\n");
exit(1);
}
model->determine_dependence = &NAMEGEN(_determine_dependence);
model->vec_field = &NAMEGEN(_vec_field);
model->interval_vec_field = &NAMEGEN(_interval_vec_field);
model->invert_vec_field = &NAMEGEN(_invert_vec_field);
model->parmdep = (int **) malloc((DEDIM+AEDIM)*sizeof(int *));
model->parmdep_flag = (int *) calloc(DEDIM+AEDIM,sizeof(int));
model->vardep = (int **) malloc((DEDIM+AEDIM)*sizeof(int *));
model->ofdep = (int **) malloc((DEDIM+AEDIM)*sizeof(int *));
model->varpartialdep = (int **) malloc((DEDIM+AEDIM)*sizeof(int *));
model->ofpartialdep = (int **) malloc((DEDIM+AEDIM)*sizeof(int *));
for (i=0; i<DEDIM+AEDIM; i++) {
model->parmdep[i] = PARMDEP[i];
model->vardep[i] = VARDEP[i];
model->ofdep[i] = OFDEP[i];
model->varpartialdep[i] = VARPARTIALDEP[i];
model->ofpartialdep[i] = OFPARTIALDEP[i];
for (j=0; j<NPARMS; j++) {
if (model->parmdep[i][j]) { model->parmdep_flag[i] = 1; break; }
}
}
}