/usr/share/doc/libsundials-serial-dev/examples/idas/serial/idasRoberts_FSA_dns.c is in libsundials-serial-dev 2.5.0-3ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 | /*
* -----------------------------------------------------------------
* $Revision: 1.2 $
* $Date: 2008/04/15 16:37:37 $
* -----------------------------------------------------------------
* Programmer(s): Cosmin Petra and Radu Serban @ LLNL
* -----------------------------------------------------------------
* Example problem:
*
* This simple example problem for IDA, due to Robertson,
* is from chemical kinetics, and consists of the following three
* equations:
*
* dy1/dt = -p1*y1 + p2*y2*y3
* dy2/dt = p1*y1 - p2*y2*y3 - p3*y2**2
* 0 = y1 + y2 + y3 - 1
*
* on the interval from t = 0.0 to t = 4.e10, with initial
* conditions: y1 = 1, y2 = y3 = 0.The reaction rates are: p1=0.04,
* p2=1e4, and p3=3e7
*
* Optionally, IDAS can compute sensitivities with respect to the
* problem parameters p1, p2, and p3.
* The sensitivity right hand side is given analytically through the
* user routine fS (of type SensRhs1Fn).
* Any of two sensitivity methods (SIMULTANEOUS and STAGGERED can be
* used and sensitivities may be included in the error test or not
*(error control set on TRUE or FALSE, respectively).
*
* Execution:
*
* If no sensitivities are desired:
* % idasRoberts_FSA_dns -nosensi
* If sensitivities are to be computed:
* % idasRoberts_FSA_dns -sensi sensi_meth err_con
* where sensi_meth is one of {sim, stg} and err_con is one of
* {t, f}.
* -----------------------------------------------------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <idas/idas.h> /* prototypes for IDAS fcts. and consts. */
#include <idas/idas_dense.h>
#include <nvector/nvector_serial.h> /* defs. of serial NVECTOR fcts. and macros */
#include <sundials/sundials_types.h> /* def. of type realtype */
#include <sundials/sundials_math.h> /* definition of ABS */
/* Accessor macros */
#define Ith(v,i) NV_Ith_S(v,i-1) /* i-th vector component i=1..NEQ */
#define IJth(A,i,j) DENSE_ELEM(A,i-1,j-1) /* (i,j)-th matrix component i,j=1..NEQ */
/* Problem Constants */
#define NEQ 3 /* number of equations */
#define T0 RCONST(0.0) /* initial time */
#define T1 RCONST(0.4) /* first output time */
#define TMULT RCONST(10.0) /* output time factor */
#define NOUT 12 /* number of output times */
#define NP 3 /* number of problem parameters */
#define NS 3 /* number of sensitivities computed */
#define ZERO RCONST(0.0)
#define ONE RCONST(1.0)
/* Type : UserData */
typedef struct {
realtype p[3]; /* problem parameters */
realtype coef;
} *UserData;
/* Prototypes of functions by IDAS */
static int res(realtype t, N_Vector y, N_Vector yp, N_Vector resval, void *user_data);
static int resS(int Ns, realtype t,
N_Vector y, N_Vector yp, N_Vector resval,
N_Vector *yyS, N_Vector *ypS, N_Vector *resvalS,
void *user_data,
N_Vector tmp1, N_Vector tmp2, N_Vector tmp3);
static int rhsQ(realtype tres, N_Vector yy, N_Vector yp,
N_Vector rrQ, void *user_data);
/* Prototypes of private functions */
static void ProcessArgs(int argc, char *argv[],
booleantype *sensi, int *sensi_meth,
booleantype *err_con);
static void WrongArgs(char *name);
static void PrintIC(N_Vector y, N_Vector yp);
static void PrintSensIC(N_Vector y, N_Vector yp, N_Vector* yS, N_Vector* ypS);
static void PrintOutput(void *ida_mem, realtype t, N_Vector u);
static void PrintSensOutput(N_Vector *uS);
static void PrintFinalStats(void *ida_mem, booleantype sensi);
static int check_flag(void *flagvalue, char *funcname, int opt);
/*
*--------------------------------------------------------------------
* MAIN PROGRAM
*--------------------------------------------------------------------
*/
int main(int argc, char *argv[])
{
void *ida_mem;
UserData data;
realtype reltol, t, tout;
N_Vector y, yp, abstol, id;
int iout, flag;
realtype pbar[NS];
int is;
N_Vector *yS, *ypS;
booleantype sensi, err_con;
int sensi_meth;
ida_mem = NULL;
data = NULL;
y = NULL;
yS = NULL;
ypS = NULL;
/* Process arguments */
ProcessArgs(argc, argv, &sensi, &sensi_meth, &err_con);
/* User data structure */
data = (UserData) malloc(sizeof *data);
if (check_flag((void *)data, "malloc", 2)) return(1);
data->p[0] = RCONST(0.040);
data->p[1] = RCONST(1.0e4);
data->p[2] = RCONST(3.0e7);
data->coef = 0.5;
/* Initial conditions */
y = N_VNew_Serial(NEQ);
if (check_flag((void *)y, "N_VNew_Serial", 0)) return(1);
Ith(y,1) = ONE;
Ith(y,2) = ZERO;
Ith(y,3) = ZERO;
yp = N_VNew_Serial(NEQ);
if(check_flag((void *)yp, "N_VNew_Serial", 0)) return(1);
/* These initial conditions are NOT consistent. See IDACalcIC below. */
Ith(yp,1) = RCONST(0.1);
Ith(yp,2) = ZERO;
Ith(yp,3) = ZERO;
/* Create IDAS object */
ida_mem = IDACreate();
if (check_flag((void *)ida_mem, "IDACreate", 0)) return(1);
/* Allocate space for IDAS */
flag = IDAInit(ida_mem, res, T0, y, yp);
if (check_flag(&flag, "IDAInit", 1)) return(1);
/* Specify scalar relative tol. and vector absolute tol. */
reltol = RCONST(1.0e-6);
abstol = N_VNew_Serial(NEQ);
Ith(abstol,1) = RCONST(1.0e-8);
Ith(abstol,2) = RCONST(1.0e-14);
Ith(abstol,3) = RCONST(1.0e-6);
flag = IDASVtolerances(ida_mem, reltol, abstol);
if (check_flag(&flag, "IDASVtolerances", 1)) return(1);
/* Set ID vector */
id = N_VNew_Serial(NEQ);
Ith(id,1) = 1.0;
Ith(id,2) = 1.0;
Ith(id,3) = 0.0;
flag = IDASetId(ida_mem, id);
if (check_flag(&flag, "IDASetId", 1)) return(1);
/* Attach user data */
flag = IDASetUserData(ida_mem, data);
if (check_flag(&flag, "IDASetUserData", 1)) return(1);
/* Attach linear solver */
flag = IDADense(ida_mem, NEQ);
if (check_flag(&flag, "IDADense", 1)) return(1);
printf("\n3-species chemical kinetics problem\n");
/* Sensitivity-related settings */
if (sensi) {
pbar[0] = data->p[0];
pbar[1] = data->p[1];
pbar[2] = data->p[2];
yS = N_VCloneVectorArray_Serial(NS, y);
if (check_flag((void *)yS, "N_VCloneVectorArray_Serial", 0)) return(1);
for (is=0;is<NS;is++) N_VConst(ZERO, yS[is]);
ypS = N_VCloneVectorArray_Serial(NS, y);
if (check_flag((void *)ypS, "N_VCloneVectorArray_Serial", 0)) return(1);
for (is=0;is<NS;is++) N_VConst(ZERO, ypS[is]);
/*
* Only non-zero sensitivity I.C. are ypS[0]:
* - Ith(ypS[0],1) = -ONE;
* - Ith(ypS[0],2) = ONE;
*
* They are not set. IDACalcIC also computes consistent IC for sensitivities.
*/
flag = IDASensInit(ida_mem, NS, sensi_meth, resS, yS, ypS);
if(check_flag(&flag, "IDASensInit", 1)) return(1);
flag = IDASensEEtolerances(ida_mem);
if(check_flag(&flag, "IDASensEEtolerances", 1)) return(1);
flag = IDASetSensErrCon(ida_mem, err_con);
if (check_flag(&flag, "IDASetSensErrCon", 1)) return(1);
flag = IDASetSensParams(ida_mem, data->p, pbar, NULL);
if (check_flag(&flag, "IDASetSensParams", 1)) return(1);
printf("Sensitivity: YES ");
if(sensi_meth == IDA_SIMULTANEOUS)
printf("( SIMULTANEOUS +");
else
printf("( STAGGERED +");
if(err_con) printf(" FULL ERROR CONTROL )");
else printf(" PARTIAL ERROR CONTROL )");
} else {
printf("Sensitivity: NO ");
}
/*----------------------------------------------------------
* Q U A D R A T U R E S
* ---------------------------------------------------------*/
N_Vector yQ, *yQS;
yQ = N_VNew_Serial(2);
Ith(yQ,1) = 0;
Ith(yQ,2) = 0;
IDAQuadInit(ida_mem, rhsQ, yQ);
yQS = N_VCloneVectorArray_Serial(NS, yQ);
for (is=0;is<NS;is++) N_VConst(ZERO, yQS[is]);
IDAQuadSensInit(ida_mem, NULL, yQS);
/* Call IDACalcIC to compute consistent initial conditions. If sensitivity is
enabled, this function also try to find consistent IC for the sensitivities. */
flag = IDACalcIC(ida_mem, IDA_YA_YDP_INIT, T1);;
if (check_flag(&flag, "IDACalcIC", 1)) return(1);
flag = IDAGetConsistentIC(ida_mem, y, yp);
if (check_flag(&flag, "IDAGetConsistentIC", 1)) return(1);
PrintIC(y, yp);
if(sensi) {
IDAGetSensConsistentIC(ida_mem, yS, ypS);
PrintSensIC(y, yp, yS, ypS);
}
/* In loop over output points, call IDA, print results, test for error */
printf("\n\n");
printf("===========================================");
printf("============================\n");
printf(" T Q H NST y1");
printf(" y2 y3 \n");
printf("===========================================");
printf("============================\n");
for (iout=1, tout=T1; iout <= NOUT; iout++, tout *= TMULT) {
flag = IDASolve(ida_mem, tout, &t, y, yp, IDA_NORMAL);
if (check_flag(&flag, "IDASolve", 1)) break;
PrintOutput(ida_mem, t, y);
if (sensi) {
flag = IDAGetSens(ida_mem, &t, yS);
if (check_flag(&flag, "IDAGetSens", 1)) break;
PrintSensOutput(yS);
}
printf("-----------------------------------------");
printf("------------------------------\n");
}
printf("\nQuadrature:\n");
IDAGetQuad(ida_mem, &t, yQ);
printf("G: %10.4e\n", Ith(yQ,1));
if(sensi) {
IDAGetQuadSens(ida_mem, &t, yQS);
printf("\nSensitivities at t=%g:\n",t);
printf("dG/dp1: %11.4e\n", Ith(yQS[0], 1));
printf("dG/dp1: %11.4e\n", Ith(yQS[1], 1));
printf("dG/dp1: %11.4e\n", Ith(yQS[2], 1));
}
/* Print final statistics */
PrintFinalStats(ida_mem, sensi);
/* Free memory */
N_VDestroy_Serial(y);
if (sensi) {
N_VDestroyVectorArray_Serial(yS, NS);
}
free(data);
IDAFree(&ida_mem);
N_VDestroy_Serial(yQ);
return(0);
}
/*
*--------------------------------------------------------------------
* FUNCTIONS CALLED BY IDAS
*--------------------------------------------------------------------
*/
/*
* Residual routine. Compute F(t,y,y',p).
*/
static int res(realtype t, N_Vector yy, N_Vector yp, N_Vector resval, void *user_data)
{
UserData data;
realtype p1, p2, p3;
realtype y1, y2, y3;
realtype yp1, yp2, yp3;
data = (UserData) user_data;
p1 = data->p[0];
p2 = data->p[1];
p3 = data->p[2];
y1 = Ith(yy,1);
y2 = Ith(yy,2);
y3 = Ith(yy,3);
yp1 = Ith(yp,1);
yp2 = Ith(yp,2);
yp3 = Ith(yp,3);
Ith(resval,1) = yp1 + p1*y1 - p2*y2*y3;
Ith(resval,2) = yp2 - p1*y1 + p2*y2*y3 + p3*y2*y2;
Ith(resval,3) = y1 + y2 + y3 - ONE;
return(0);
}
/*
* resS routine. Compute sensitivity r.h.s.
*/
static int resS(int Ns, realtype t,
N_Vector yy, N_Vector yp, N_Vector resval,
N_Vector *yyS, N_Vector *ypS, N_Vector *resvalS,
void *user_data,
N_Vector tmp1, N_Vector tmp2, N_Vector tmp3)
{
UserData data;
realtype p1, p2, p3;
realtype y1, y2, y3;
realtype yp1, yp2, yp3;
realtype s1, s2, s3;
realtype sd1, sd2, sd3;
realtype rs1, rs2, rs3;
int is;
data = (UserData) user_data;
p1 = data->p[0];
p2 = data->p[1];
p3 = data->p[2];
y1 = Ith(yy,1);
y2 = Ith(yy,2);
y3 = Ith(yy,3);
yp1 = Ith(yp,1);
yp2 = Ith(yp,2);
yp3 = Ith(yp,3);
for (is=0; is<NS; is++) {
s1 = Ith(yyS[is],1);
s2 = Ith(yyS[is],2);
s3 = Ith(yyS[is],3);
sd1 = Ith(ypS[is],1);
sd2 = Ith(ypS[is],2);
sd3 = Ith(ypS[is],3);
rs1 = sd1 + p1*s1 - p2*y3*s2 - p2*y2*s3;
rs2 = sd2 - p1*s1 + p2*y3*s2 + p2*y2*s3 + 2*p3*y2*s2;
rs3 = s1 + s2 + s3;
switch (is) {
case 0:
rs1 += y1;
rs2 -= y1;
break;
case 1:
rs1 -= y2*y3;
rs2 += y2*y3;
break;
case 2:
rs2 += y2*y2;
break;
}
Ith(resvalS[is],1) = rs1;
Ith(resvalS[is],2) = rs2;
Ith(resvalS[is],3) = rs3;
}
return(0);
}
static int rhsQ(realtype t, N_Vector y, N_Vector yp,
N_Vector ypQ, void* user_data)
{
UserData data;
data = (UserData) user_data;
Ith(ypQ,1) = Ith(y,3);
Ith(ypQ,2) = data->coef*( Ith(y,1)*Ith(y,1)+
Ith(y,2)*Ith(y,2)+
Ith(y,3)*Ith(y,3) );
return(0);
}
/*
*--------------------------------------------------------------------
* PRIVATE FUNCTIONS
*--------------------------------------------------------------------
*/
/*
* Process and verify arguments to idasfwddenx.
*/
static void ProcessArgs(int argc, char *argv[],
booleantype *sensi, int *sensi_meth, booleantype *err_con)
{
*sensi = FALSE;
*sensi_meth = -1;
*err_con = FALSE;
if (argc < 2) WrongArgs(argv[0]);
if (strcmp(argv[1],"-nosensi") == 0)
*sensi = FALSE;
else if (strcmp(argv[1],"-sensi") == 0)
*sensi = TRUE;
else
WrongArgs(argv[0]);
if (*sensi) {
if (argc != 4)
WrongArgs(argv[0]);
if (strcmp(argv[2],"sim") == 0)
*sensi_meth = IDA_SIMULTANEOUS;
else if (strcmp(argv[2],"stg") == 0)
*sensi_meth = IDA_STAGGERED;
else
WrongArgs(argv[0]);
if (strcmp(argv[3],"t") == 0)
*err_con = TRUE;
else if (strcmp(argv[3],"f") == 0)
*err_con = FALSE;
else
WrongArgs(argv[0]);
}
}
static void WrongArgs(char *name)
{
printf("\nUsage: %s [-nosensi] [-sensi sensi_meth err_con]\n",name);
printf(" sensi_meth = sim or stg\n");
printf(" err_con = t or f\n");
exit(0);
}
static void PrintIC(N_Vector y, N_Vector yp)
{
realtype* data;
data = NV_DATA_S(y);
printf("\n\nConsistent IC:\n");
printf("\ty = ");
#if defined(SUNDIALS_EXTENDED_PRECISION)
printf("%12.4Le %12.4Le %12.4Le \n", data[0], data[1], data[2]);
#elif defined(SUNDIALS_DOUBLE_PRECISION)
printf("%12.4le %12.4le %12.4le \n", data[0], data[1], data[2]);
#else
printf("%12.4e %12.4e %12.4e \n", data[0], data[1], data[2]);
#endif
data = NV_DATA_S(yp);
printf("\typ= ");
#if defined(SUNDIALS_EXTENDED_PRECISION)
printf("%12.4Le %12.4Le %12.4Le \n", data[0], data[1], data[2]);
#elif defined(SUNDIALS_DOUBLE_PRECISION)
printf("%12.4le %12.4le %12.4le \n", data[0], data[1], data[2]);
#else
printf("%12.4e %12.4e %12.4e \n", data[0], data[1], data[2]);
#endif
}
static void PrintSensIC(N_Vector y, N_Vector yp, N_Vector* yS, N_Vector* ypS)
{
realtype *sdata;
sdata = NV_DATA_S(yS[0]);
printf(" Sensitivity 1 ");
printf("\n\ts1 = ");
#if defined(SUNDIALS_EXTENDED_PRECISION)
printf("%12.4Le %12.4Le %12.4Le \n", sdata[0], sdata[1], sdata[2]);
#elif defined(SUNDIALS_DOUBLE_PRECISION)
printf("%12.4le %12.4le %12.4le \n", sdata[0], sdata[1], sdata[2]);
#else
printf("%12.4e %12.4e %12.4e \n", sdata[0], sdata[1], sdata[2]);
#endif
sdata = NV_DATA_S(ypS[0]);
printf("\ts1'= ");
#if defined(SUNDIALS_EXTENDED_PRECISION)
printf("%12.4Le %12.4Le %12.4Le \n", sdata[0], sdata[1], sdata[2]);
#elif defined(SUNDIALS_DOUBLE_PRECISION)
printf("%12.4le %12.4le %12.4le \n", sdata[0], sdata[1], sdata[2]);
#else
printf("%12.4e %12.4e %12.4e \n", sdata[0], sdata[1], sdata[2]);
#endif
printf(" Sensitivity 2 ");
sdata = NV_DATA_S(yS[1]);
printf("\n\ts2 = ");
#if defined(SUNDIALS_EXTENDED_PRECISION)
printf("%12.4Le %12.4Le %12.4Le \n", sdata[0], sdata[1], sdata[2]);
#elif defined(SUNDIALS_DOUBLE_PRECISION)
printf("%12.4le %12.4le %12.4le \n", sdata[0], sdata[1], sdata[2]);
#else
printf("%12.4e %12.4e %12.4e \n", sdata[0], sdata[1], sdata[2]);
#endif
sdata = NV_DATA_S(ypS[1]);
printf("\ts2'= ");
#if defined(SUNDIALS_EXTENDED_PRECISION)
printf("%12.4Le %12.4Le %12.4Le \n", sdata[0], sdata[1], sdata[2]);
#elif defined(SUNDIALS_DOUBLE_PRECISION)
printf("%12.4le %12.4le %12.4le \n", sdata[0], sdata[1], sdata[2]);
#else
printf("%12.4e %12.4e %12.4e \n", sdata[0], sdata[1], sdata[2]);
#endif
printf(" Sensitivity 3 ");
sdata = NV_DATA_S(yS[2]);
printf("\n\ts3 = ");
#if defined(SUNDIALS_EXTENDED_PRECISION)
printf("%12.4Le %12.4Le %12.4Le \n", sdata[0], sdata[1], sdata[2]);
#elif defined(SUNDIALS_DOUBLE_PRECISION)
printf("%12.4le %12.4le %12.4le \n", sdata[0], sdata[1], sdata[2]);
#else
printf("%12.4e %12.4e %12.4e \n", sdata[0], sdata[1], sdata[2]);
#endif
sdata = NV_DATA_S(ypS[2]);
printf("\ts3'= ");
#if defined(SUNDIALS_EXTENDED_PRECISION)
printf("%12.4Le %12.4Le %12.4Le \n", sdata[0], sdata[1], sdata[2]);
#elif defined(SUNDIALS_DOUBLE_PRECISION)
printf("%12.4le %12.4le %12.4le \n", sdata[0], sdata[1], sdata[2]);
#else
printf("%12.4e %12.4e %12.4e \n", sdata[0], sdata[1], sdata[2]);
#endif
}
/*
* Print current t, step count, order, stepsize, and solution.
*/
static void PrintOutput(void *ida_mem, realtype t, N_Vector u)
{
long int nst;
int qu, flag;
realtype hu, *udata;
udata = NV_DATA_S(u);
flag = IDAGetNumSteps(ida_mem, &nst);
check_flag(&flag, "IDAGetNumSteps", 1);
flag = IDAGetLastOrder(ida_mem, &qu);
check_flag(&flag, "IDAGetLastOrder", 1);
flag = IDAGetLastStep(ida_mem, &hu);
check_flag(&flag, "IDAGetLastStep", 1);
#if defined(SUNDIALS_EXTENDED_PRECISION)
printf("%8.3Le %2d %8.3Le %5ld\n", t, qu, hu, nst);
#elif defined(SUNDIALS_DOUBLE_PRECISION)
printf("%8.3le %2d %8.3le %5ld\n", t, qu, hu, nst);
#else
printf("%8.3e %2d %8.3e %5ld\n", t, qu, hu, nst);
#endif
printf(" Solution ");
#if defined(SUNDIALS_EXTENDED_PRECISION)
printf("%12.4Le %12.4Le %12.4Le \n", udata[0], udata[1], udata[2]);
#elif defined(SUNDIALS_DOUBLE_PRECISION)
printf("%12.4le %12.4le %12.4le \n", udata[0], udata[1], udata[2]);
#else
printf("%12.4e %12.4e %12.4e \n", udata[0], udata[1], udata[2]);
#endif
}
/*
* Print sensitivities.
*/
static void PrintSensOutput(N_Vector *uS)
{
realtype *sdata;
sdata = NV_DATA_S(uS[0]);
printf(" Sensitivity 1 ");
#if defined(SUNDIALS_EXTENDED_PRECISION)
printf("%12.4Le %12.4Le %12.4Le \n", sdata[0], sdata[1], sdata[2]);
#elif defined(SUNDIALS_DOUBLE_PRECISION)
printf("%12.4le %12.4le %12.4le \n", sdata[0], sdata[1], sdata[2]);
#else
printf("%12.4e %12.4e %12.4e \n", sdata[0], sdata[1], sdata[2]);
#endif
sdata = NV_DATA_S(uS[1]);
printf(" Sensitivity 2 ");
#if defined(SUNDIALS_EXTENDED_PRECISION)
printf("%12.4Le %12.4Le %12.4Le \n", sdata[0], sdata[1], sdata[2]);
#elif defined(SUNDIALS_DOUBLE_PRECISION)
printf("%12.4le %12.4le %12.4le \n", sdata[0], sdata[1], sdata[2]);
#else
printf("%12.4e %12.4e %12.4e \n", sdata[0], sdata[1], sdata[2]);
#endif
sdata = NV_DATA_S(uS[2]);
printf(" Sensitivity 3 ");
#if defined(SUNDIALS_EXTENDED_PRECISION)
printf("%12.4Le %12.4Le %12.4Le \n", sdata[0], sdata[1], sdata[2]);
#elif defined(SUNDIALS_DOUBLE_PRECISION)
printf("%12.4le %12.4le %12.4le \n", sdata[0], sdata[1], sdata[2]);
#else
printf("%12.4e %12.4e %12.4e \n", sdata[0], sdata[1], sdata[2]);
#endif
}
/*
* Print some final statistics from the IDAS memory.
*/
static void PrintFinalStats(void *ida_mem, booleantype sensi)
{
long int nst;
long int nfe, nsetups, nni, ncfn, netf;
long int nfSe, nfeS, nsetupsS, nniS, ncfnS, netfS;
long int nje, nfeLS;
int flag;
flag = IDAGetNumSteps(ida_mem, &nst);
check_flag(&flag, "IDAGetNumSteps", 1);
flag = IDAGetNumResEvals(ida_mem, &nfe);
check_flag(&flag, "IDAGetNumRhsEvals", 1);
flag = IDAGetNumLinSolvSetups(ida_mem, &nsetups);
check_flag(&flag, "IDAGetNumLinSolvSetups", 1);
flag = IDAGetNumErrTestFails(ida_mem, &netf);
check_flag(&flag, "IDAGetNumErrTestFails", 1);
flag = IDAGetNumNonlinSolvIters(ida_mem, &nni);
check_flag(&flag, "IDAGetNumNonlinSolvIters", 1);
flag = IDAGetNumNonlinSolvConvFails(ida_mem, &ncfn);
check_flag(&flag, "IDAGetNumNonlinSolvConvFails", 1);
if (sensi) {
flag = IDAGetSensNumResEvals(ida_mem, &nfSe);
check_flag(&flag, "IDAGetSensNumRhsEvals", 1);
flag = IDAGetNumResEvalsSens(ida_mem, &nfeS);
check_flag(&flag, "IDAGetNumResEvalsSens", 1);
flag = IDAGetSensNumLinSolvSetups(ida_mem, &nsetupsS);
check_flag(&flag, "IDAGetSensNumLinSolvSetups", 1);
flag = IDAGetSensNumErrTestFails(ida_mem, &netfS);
check_flag(&flag, "IDAGetSensNumErrTestFails", 1);
flag = IDAGetSensNumNonlinSolvIters(ida_mem, &nniS);
check_flag(&flag, "IDAGetSensNumNonlinSolvIters", 1);
flag = IDAGetSensNumNonlinSolvConvFails(ida_mem, &ncfnS);
check_flag(&flag, "IDAGetSensNumNonlinSolvConvFails", 1);
}
flag = IDADlsGetNumJacEvals(ida_mem, &nje);
check_flag(&flag, "IDADlsGetNumJacEvals", 1);
flag = IDADlsGetNumResEvals(ida_mem, &nfeLS);
check_flag(&flag, "IDADlsGetNumResEvals", 1);
printf("\nFinal Statistics\n\n");
printf("nst = %5ld\n\n", nst);
printf("nfe = %5ld\n", nfe);
printf("netf = %5ld nsetups = %5ld\n", netf, nsetups);
printf("nni = %5ld ncfn = %5ld\n", nni, ncfn);
if(sensi) {
printf("\n");
printf("nfSe = %5ld nfeS = %5ld\n", nfSe, nfeS);
printf("netfs = %5ld nsetupsS = %5ld\n", netfS, nsetupsS);
printf("nniS = %5ld ncfnS = %5ld\n", nniS, ncfnS);
}
printf("\n");
printf("nje = %5ld nfeLS = %5ld\n", nje, nfeLS);
}
/*
* Check function return value.
* opt == 0 means SUNDIALS function allocates memory so check if
* returned NULL pointer
* opt == 1 means SUNDIALS function returns a flag so check if
* flag >= 0
* opt == 2 means function allocates memory so check if returned
* NULL pointer
*/
static int check_flag(void *flagvalue, char *funcname, int opt)
{
int *errflag;
/* Check if SUNDIALS function returned NULL pointer - no memory allocated */
if (opt == 0 && flagvalue == NULL) {
fprintf(stderr,
"\nSUNDIALS_ERROR: %s() failed - returned NULL pointer\n\n",
funcname);
return(1); }
/* Check if flag < 0 */
else if (opt == 1) {
errflag = (int *) flagvalue;
if (*errflag < 0) {
fprintf(stderr,
"\nSUNDIALS_ERROR: %s() failed with flag = %d\n\n",
funcname, *errflag);
return(1); }}
/* Check if function returned NULL pointer - no memory allocated */
else if (opt == 2 && flagvalue == NULL) {
fprintf(stderr,
"\nMEMORY_ERROR: %s() failed - returned NULL pointer\n\n",
funcname);
return(1); }
return(0);
}
|