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 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
|
#include "sys-defines.h"
#include "ode.h"
#include "extern.h"
/*
* stuff
* that doesn't go anywhere
* else in particular
* Copyright Nicholas B. Tufillaro, 1982-1994. All rights reserved.
* GNU enhancements copyright (C) 1996-1999 Free Software Foundation, Inc.
*/
/*
* check checks internal consistency prior to calling the
* numerical routine. Tasks it performs include:
* ensures one and only one independent variable
* the symbol table is coherent
* the print queue is coherent
* the independent variable has a derivative == 1.
* each dependent variable has an ODE and an initial value
*/
bool
#ifdef _HAVE_PROTOS
check (void)
#else
check ()
#endif
{
struct sym *sp, *ivp, *prevp;
struct prt *pp;
/*
* discard any previous entry for "(indep)"
*/
prevp = NULL;
for (sp = symtab; sp != NULL; sp = sp->sy_link)
{
if (strncmp (sp->sy_name, "(indep)", NAMMAX) == 0)
{
if (prevp == NULL)
symtab = sp->sy_link;
else
prevp->sy_link = sp->sy_link;
sfree(sp);
break;
}
prevp = sp;
}
/*
* check for only one independent variable
*/
ivp = prevp = NULL;
for (sp = symtab; sp != NULL; sp = sp->sy_link)
{
if (!(sp->sy_flags & SF_DEPV))
{
if (ivp != NULL)
{
fprintf (stderr,
"%s: both `%.*s' and `%.*s' are independent\n",
progname,
NAMMAX, sp->sy_name,
NAMMAX, ivp->sy_name);
return false;
}
ivp = sp;
}
if (ivp == NULL)
prevp = sp;
}
/*
* invent one if it's missing
*/
if (ivp == NULL)
{
ivp = salloc();
strncpy (ivp->sy_name, "(indep)", NAMMAX);
}
else if (prevp != NULL)
{
/*
* link the independent var at the
* head of the symtab queue
*/
prevp->sy_link = ivp->sy_link;
ivp->sy_link = symtab;
symtab = ivp;
}
/*
* now ivp points to the ind. var. entry
* make sure the independent var gets
* printed when there's no print statement
*/
if (!sawprint)
{
for (pp = pqueue; pp != NULL; pp = pp->pr_link)
if (pp->pr_sym == ivp)
goto found;
pp = palloc();
pp->pr_link = pqueue;
pqueue = pp;
pp->pr_sym = ivp;
}
found:
/*
* indep var has a derivative of 1.0
*/
ivp->sy_expr = &exprone;
/*
* ensure an expr and value for each dep var
*/
for (sp = symtab; sp != NULL; sp = sp->sy_link)
{
switch (sp->sy_flags&SF_DEPV)
{
case SF_INIT:
sp->sy_expr = &exprzero;
sp->sy_flags |= SF_ISEQN;
break;
case SF_ISEQN:
sp->sy_value = 0;
sp->sy_flags |= SF_INIT;
break;
}
}
/*
* dependent variables start here
*/
dqueue = symtab->sy_link;
return true;
}
/*
* set default values
* determine step direction (forgive confused users)
* initialize values for printq() and numerical routines
*/
void
#ifdef _HAVE_PROTOS
defalt (void)
#else
defalt ()
#endif
{
if (!sawfrom)
tfrom = tstart;
if (!sawevery)
tevery = 1;
if (tstart>tstop && tstep>0)
tstep = -tstep;
else if (tstart<tstop && tstep<0)
tstep = -tstep;
printnum = false;
}
/*
* evaluate all the derivatives
*/
void
#ifdef _HAVE_PROTOS
field(void)
#else
field()
#endif
{
for (fsp = symtab->sy_link; fsp!=NULL; fsp = fsp->sy_link)
fsp->sy_prime = eval(fsp->sy_expr);
}
/*
* internal error (fatal)
*/
void
#ifdef _HAVE_PROTOS
panic (const char *s)
#else
panic (s)
const char *s;
#endif
{
fprintf (stderr, "%s panic: %s\n", progname, s);
exit (EXIT_FAILURE);
}
void
#ifdef _HAVE_PROTOS
panicn (const char *fmt, int n)
#else
panicn (fmt, n)
const char *fmt;
int n;
#endif
{
fprintf (stderr, "%s panic: ", progname);
fprintf (stderr, fmt, n);
fprintf (stderr, "\n");
exit (EXIT_FAILURE);
}
#define LASTVAL (tstep>0 ? t>=tstop-0.0625*tstep : t<=tstop-0.0625*tstep)
#define TFROM (tfrom - 0.0625*tstep)
#define PRFROM (tstep>0 ? t >= TFROM : t<= TFROM)
void
#ifdef _HAVE_PROTOS
printq (void)
#else
printq ()
#endif
{
double f = 0.0;
double t;
struct prt *pp;
t = symtab->sy_value;
if (!printnum && PRFROM)
printnum = true;
if (((it % tevery == 0) && printnum) || LASTVAL)
{
pp = pqueue;
if (pp != NULL)
for (;;)
{
switch (pp->pr_which)
{
case P_VALUE:
f = pp->pr_sym->sy_value;
break;
case P_PRIME:
f = pp->pr_sym->sy_prime;
break;
case P_ACERR:
f = pp->pr_sym->sy_acerr;
break;
case P_ABERR:
f = pp->pr_sym->sy_aberr;
break;
case P_SSERR:
f = pp->pr_sym->sy_sserr;
break;
default:
panicn ("bad cell spec (%d) in printq()", (int)(pp->pr_which));
break;
}
prval (f);
pp = pp->pr_link;
if (pp == NULL)
break;
putchar (' ');
}
putchar ('\n');
fflush (stdout);
}
if (it == LONGMAX)
it = 0;
}
/*
* print a value to current precision
* kludge for Pascal compatibility
*/
void
#ifdef _HAVE_PROTOS
prval (double x)
#else
prval (x)
double x;
#endif
{
if (prec < 0)
{
char outbuf[20];
if (x < 0)
{
putchar ('-');
x = -x;
}
sprintf (outbuf, "%.7g", x);
if (*outbuf == '.')
putchar ('0');
printf ("%s", outbuf);
}
else
printf ("%*.*e", fwd, prec, x);
}
/*
* handler for math library exceptions (`rterrors' may or may not return)
*/
#ifdef HAVE_MATHERR
int
#ifdef _HAVE_PROTOS
matherr (struct exception *x)
#else
matherr (x)
struct exception *x;
#endif
{
switch (x->type)
{
case DOMAIN:
rterrors ("domain error in %s", x->name);
break;
case SING:
rterrors ("singularity error in %s", x->name);
break;
case OVERFLOW:
rterrors ("range error (overflow) in %s", x->name);
break;
#ifdef TLOSS
case TLOSS:
rterrors ("range error (total loss of significance) in %s",
x->name);
break;
#endif
#ifdef PLOSS
case PLOSS:
rterrors ("range error (partial loss of significance) in %s",
x->name);
break;
#endif
case UNDERFLOW: /* treat as non-fatal */
rtsquawks ("range error (underflow) in %s", x->name);
break;
default:
rterrors ("unknown error in %s", x->name);
break;
}
return 1; /* suppress system error message */
}
#endif
/*
* print a diagnostic for run-time errors.
* uses fsp to decide which dependent variable was being worked
*/
void
#ifdef _HAVE_PROTOS
rterror (const char *s)
#else
rterror (s)
const char *s;
#endif
{
if (fsp == NULL) /* no computation, just print message */
fprintf (stderr, "%s: %s\n", progname, s);
else
{
fprintf (stderr, "%s: %s while calculating %.*s'\n", progname, s,
NAMMAX, fsp->sy_name);
longjmp (mark, 1); /* interrupt computation */
}
}
void
#ifdef _HAVE_PROTOS
rterrors (const char *fmt, const char *s)
#else
rterrors (fmt, s)
const char *fmt, *s;
#endif
{
if (fsp != NULL) /* interrupt computation */
{
fprintf (stderr, "%s: ", progname);
fprintf (stderr, fmt, s);
fprintf (stderr, " while calculating %.*s'\n", NAMMAX, fsp->sy_name);
longjmp (mark, 1);
}
else /* just print error message */
{
fprintf (stderr, "%s: ", progname);
fprintf (stderr, fmt, s);
fprintf (stderr, "\n");
}
}
/*
* same, but doesn't do a longjmp.
* computation continues
*/
void
#ifdef _HAVE_PROTOS
rtsquawks (const char *fmt, const char *s)
#else
rtsquawks (fmt, s)
const char *fmt, *s;
#endif
{
fprintf (stderr, "%s: ", progname);
fprintf (stderr, fmt, s);
if (fsp != NULL)
fprintf (stderr, " while calculating %.*s'", NAMMAX, fsp->sy_name);
fprintf (stderr, "\n");
return;
}
/*
* Run the numerical stuff.
* This gets called from the grammar
* because we want to solve on each
* 'step' statement.
*/
void
#ifdef _HAVE_PROTOS
solve (void)
#else
solve ()
#endif
{
struct sym *sp;
bool adapt;
if (check() == false)
return;
defalt ();
if (tflag)
title ();
fflush (stderr);
setflt ();
if (!setjmp (mark))
{
adapt = eflag|rflag|!conflag ? true : false;
if (tstart == tstop)
trivial();
else switch (algorithm)
{
case A_EULER:
eu();
break;
case A_ADAMS_MOULTON:
if (adapt || prerr)
ama();
else
am();
break;
case A_RUNGE_KUTTA_FEHLBERG:
default:
if (adapt || prerr)
rka();
else
rk();
break;
}
}
resetflt();
/* add final newline (to aid realtime postprocessing of dataset by graph) */
putchar ('\n');
fflush (stdout);
for (sp = symtab; sp != NULL; sp = sp->sy_link)
{
sp->sy_prime = sp->sy_pri[0];
sp->sy_value = sp->sy_val[0];
}
}
/*
* choose step size at tstart
*/
void
#ifdef _HAVE_PROTOS
startstep (void)
#else
startstep ()
#endif
{
if (!hflag)
hmax = fabs ((tstop-tstart)/2);
tstep = fabs ((tstop-tstart)/MESH);
if (tstep > hmax)
tstep = hmax;
if (tstep < hmin)
tstep = hmin;
while (tstep >= HMAX)
tstep *= HALF;
while (tstart + tstep == tstart)
tstep *= TWO;
}
/*
* print a header
* Try to center the headings over the columns
*/
void
#ifdef _HAVE_PROTOS
title (void)
#else
title ()
#endif
{
struct prt *pp;
char tag = '\0';
pp = pqueue;
if (pp != NULL)
for (;;)
{
switch (pp->pr_which)
{
case P_PRIME:
tag = '\'';
break;
case P_VALUE:
tag = ' ';
break;
case P_SSERR:
tag = '?';
break;
case P_ABERR:
tag = '!';
break;
case P_ACERR:
tag = '~';
break;
default:
panicn ("bad cell spec (%d) in title()", (int)(pp->pr_which));
break;
}
printf (" %*.*s%c", fwd - 2, NAMMAX, pp->pr_sym->sy_name, tag);
if ((pp=pp->pr_link) == NULL)
break;
putchar (' ');
}
putchar ('\n');
fflush (stdout);
}
|