forked from Ermentrout/xppaut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dae_fun.c
executable file
·328 lines (275 loc) · 5.88 KB
/
dae_fun.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
#include "dae_fun.h"
#include "gear.h"
#include "parserslow.h"
#include "ggets.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "xpplim.h"
#include "getvar.h"
#define MAXDAE 400
extern double variables[];
extern int NVAR;
extern int DelayErr;
extern double EVEC_ERR,NEWT_ERR,BOUND;
extern int EVEC_ITER;
double evaluate();
extern int NODE,FIX_VAR;
extern int *my_ode[];
double sdot();
/* will have more stuff someday */
typedef struct {
double *work;
int *iwork;
int status;
} DAEWORK;
DAEWORK dae_work;
typedef struct {
char name[12],*rhs;
int *form;
int index;
double value,last;
}SOL_VAR;
typedef struct {
char *rhs;
int *form;
} DAE_EQN;
SOL_VAR svar[MAXDAE];
DAE_EQN aeqn[MAXDAE];
int nsvar=0,naeqn=0;
/* this adds an algebraically defined variable and a formula
for the first guess */
int add_svar(name,rhs)
char *name,*rhs;
{
if(nsvar>=MAXDAE){
plintf(" Too many variables\n");
return 1;
}
strcpy(svar[nsvar].name,name);
svar[nsvar].rhs=(char *) malloc(80);
strcpy(svar[nsvar].rhs,rhs);
plintf(" Added sol-var[%d] %s = %s \n",
nsvar,svar[nsvar].name,svar[nsvar].rhs);
nsvar++;
return 0;
}
/* adds algebraically define name to name list */
int add_svar_names()
{
int i;
for(i=0;i<nsvar;i++){
svar[i].index=NVAR;
if(add_var(svar[i].name,0.0)==1)
return 1;
}
return 0;
}
/* adds a right-hand side to slove for zero */
int add_aeqn(rhs)
char *rhs;
{
if(naeqn>=MAXDAE){
plintf(" Too many equations\n");
return 1;
}
aeqn[naeqn].rhs=(char *) malloc(strlen(rhs)+5);
strcpy(aeqn[naeqn].rhs,rhs);
naeqn++;
return 0;
}
/* this compiles formulas to set to zero */
int compile_svars()
{
int i,f[256],n,k;
if(nsvar!=naeqn){
plintf(" #SOL_VAR(%d) must equal #ALG_EQN(%d) ! \n",nsvar,naeqn);
return 1;
}
for(i=0;i<naeqn;i++){
if(add_expr(aeqn[i].rhs,f,&n)==1){
plintf(" Bad right-hand side for alg-eqn \n");
return(1);
}
aeqn[i].form=(int *)malloc(sizeof(int)*(n+2));
for(k=0;k<n;k++)
aeqn[i].form[k]=f[k];
}
for(i=0;i<nsvar;i++){
if(add_expr(svar[i].rhs,f,&n)==1){
plintf(" Bad initial guess for sol-var \n");
return(1);
}
svar[i].form=(int *)malloc(100*sizeof(int));
for(k=0;k<n;k++)
svar[i].form[k]=f[k];
}
init_dae_work();
return 0;
}
void reset_dae()
{
dae_work.status=1;
}
void set_init_guess()
{
int i;
double z;
dae_work.status=1;
if(nsvar==0)return;
for(i=0;i<nsvar;i++){
z=evaluate(svar[i].form);
SETVAR(svar[i].index,z);
svar[i].value=z;
svar[i].last=z;
}
}
void err_dae()
{
switch(dae_work.status){
case 2:
err_msg(" Warning - no change in Iterates");
break;
case -1:
err_msg(" Singular jacobian for dae\n");
break;
case -2:
err_msg(" Maximum iterates exceeded for dae\n");
break;
case -3:
err_msg(" Newton update out of bounds\n");
break;
}
dae_work.status=1;
}
void init_dae_work()
{
dae_work.work=(double *)malloc(sizeof(double)*(nsvar*nsvar+10*nsvar));
dae_work.iwork=(int *)malloc(sizeof(int)*nsvar);
dae_work.status=1;
}
void get_dae_fun(y,f)
double *f,*y;
{
int i;
/* better do this in case fixed variables depend on sol_var */
for(i=0;i<nsvar;i++)
SETVAR(svar[i].index,y[i]);
for(i=NODE;i<NODE+FIX_VAR;i++)
SETVAR(i+1,evaluate(my_ode[i]));
for(i=0;i<naeqn;i++)
f[i]=evaluate(aeqn[i].form);
}
void do_daes()
{
int ans;
ans=solve_dae();
dae_work.status=ans;
if(ans==1||ans==2)return; /* accepts a no change error! */
DelayErr=1;
}
/* Newton solver for algebraic stuff */
int solve_dae()
{
int i,j,n;
int info;
double err,del,z,yold;
double tol=EVEC_ERR,eps=NEWT_ERR;
int maxit=EVEC_ITER,iter=0;
double *y,*ynew,*f,*fnew,*jac,*errvec;
n=nsvar;
if(nsvar==0)return 1;
if(dae_work.status<0)return dae_work.status; /* accepts no change error */
y=dae_work.work;
f=y+nsvar;
fnew=f+nsvar;
ynew=fnew+nsvar;
errvec=ynew+nsvar;
jac=errvec+nsvar;
for(i=0;i<n;i++){ /* copy current value as initial guess */
y[i]=svar[i].last;
ynew[i]=y[i]; /* keep old guess */
}
while(1){
get_dae_fun(y,f);
err=0.0;
for(i=0;i<n;i++){
err+=fabs(f[i]);
errvec[i]=f[i];
}
if(err<tol){ /* success */
for(i=0;i<n;i++){
SETVAR(svar[i].index,y[i]);
svar[i].last=y[i];
}
return 1;
}
/* compute jacobian */
for(i=0;i<n;i++){
z=fabs(y[i]);
if(z<eps)z=eps;
del=eps*z;
yold=y[i];
y[i]=y[i]+del;
get_dae_fun(y,fnew);
for(j=0;j<n;j++)
jac[j*n+i]=(fnew[j]-f[j])/del;
y[i]=yold;
}
sgefa(jac,n,n,dae_work.iwork,&info);
if(info!=-1){
for(i=0;i<n;i++)
SETVAR(svar[i].index,ynew[i]);
return -1; /* singular jacobian */
}
sgesl(jac,n,n,dae_work.iwork,errvec,0); /* get x=J^(-1) f */
err=0.0;
for(i=0;i<n;i++){
y[i]-=errvec[i];
err+=fabs(errvec[i]);
}
if(err>(n*BOUND)){
for(i=0;i<n;i++)
SETVAR(svar[i].index,svar[i].last);
return(-3); /* getting too big */
}
if(err<tol) /* not much change */
{
/* plintf(" no change .... \n"); */
for(i=0;i<n;i++){
SETVAR(svar[i].index,y[i]);
svar[i].last=y[i];
}
return 2;
}
iter++;
if(iter>maxit){
/* plintf(" Too many iterates ... \n"); */
for(i=0;i<n;i++)
SETVAR(svar[i].index,svar[i].last);
return(-2); /* too many iterates */
}
}
}
/* interface shit -- different for Win95 */
void get_new_guesses()
{
int i,n;
char name[30];
double z;
if(nsvar<1)return;
for(i=0;i<nsvar;i++){
z=svar[i].last;
sprintf(name,"Initial %s(%g):",svar[i].name,z);
new_string(name,svar[i].rhs);
if(add_expr(svar[i].rhs,svar[i].form,&n)){
err_msg("Illegal formula");
return;
}
z=evaluate(svar[i].form);
SETVAR(svar[i].index,z);
svar[i].value=z;
svar[i].last=z;
}
}