/usr/share/doc/libsundials-serial-dev/examples/ida/serial/idaRoberts_dns.c is in libsundials-serial-dev 2.5.0-3+b3.
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 | /*
* -----------------------------------------------------------------
* $Revision: 1.4 $
* $Date: 2010/12/01 23:02:23 $
* -----------------------------------------------------------------
* Programmer(s): Allan Taylor, Alan Hindmarsh and
* Radu Serban @ LLNL
* -----------------------------------------------------------------
* This simple example problem for IDA, due to Robertson,
* is from chemical kinetics, and consists of the following three
* equations:
*
* dy1/dt = -.04*y1 + 1.e4*y2*y3
* dy2/dt = .04*y1 - 1.e4*y2*y3 - 3.e7*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.
*
* While integrating the system, we also use the rootfinding
* feature to find the points at which y1 = 1e-4 or at which
* y3 = 0.01.
*
* The problem is solved with IDA using IDADENSE for the linear
* solver, with a user-supplied Jacobian. Output is printed at
* t = .4, 4, 40, ..., 4e10.
* -----------------------------------------------------------------
*/
#include <stdio.h>
#include <math.h>
#include <ida/ida.h>
#include <ida/ida_dense.h>
#include <nvector/nvector_serial.h>
#include <sundials/sundials_math.h>
#include <sundials/sundials_types.h>
/* Problem Constants */
#define NEQ 3
#define NOUT 12
#define ZERO RCONST(0.0);
#define ONE RCONST(1.0);
/* Macro to define dense matrix elements, indexed from 1. */
#define IJth(A,i,j) DENSE_ELEM(A,i-1,j-1)
/* Prototypes of functions called by IDA */
int resrob(realtype tres, N_Vector yy, N_Vector yp,
N_Vector resval, void *user_data);
static int grob(realtype t, N_Vector yy, N_Vector yp,
realtype *gout, void *user_data);
int jacrob(long int Neq, realtype tt, realtype cj,
N_Vector yy, N_Vector yp, N_Vector resvec,
DlsMat JJ, void *user_data,
N_Vector tempv1, N_Vector tempv2, N_Vector tempv3);
/* Prototypes of private functions */
static void PrintHeader(realtype rtol, N_Vector avtol, N_Vector y);
static void PrintOutput(void *mem, realtype t, N_Vector y);
static void PrintRootInfo(int root_f1, int root_f2);
static void PrintFinalStats(void *mem);
static int check_flag(void *flagvalue, char *funcname, int opt);
/*
*--------------------------------------------------------------------
* Main Program
*--------------------------------------------------------------------
*/
int main(void)
{
void *mem;
N_Vector yy, yp, avtol;
realtype rtol, *yval, *ypval, *atval;
realtype t0, tout1, tout, tret;
int iout, retval, retvalr;
int rootsfound[2];
mem = NULL;
yy = yp = avtol = NULL;
yval = ypval = atval = NULL;
/* Allocate N-vectors. */
yy = N_VNew_Serial(NEQ);
if(check_flag((void *)yy, "N_VNew_Serial", 0)) return(1);
yp = N_VNew_Serial(NEQ);
if(check_flag((void *)yp, "N_VNew_Serial", 0)) return(1);
avtol = N_VNew_Serial(NEQ);
if(check_flag((void *)avtol, "N_VNew_Serial", 0)) return(1);
/* Create and initialize y, y', and absolute tolerance vectors. */
yval = NV_DATA_S(yy);
yval[0] = ONE;
yval[1] = ZERO;
yval[2] = ZERO;
ypval = NV_DATA_S(yp);
ypval[0] = RCONST(-0.04);
ypval[1] = RCONST(0.04);
ypval[2] = ZERO;
rtol = RCONST(1.0e-4);
atval = NV_DATA_S(avtol);
atval[0] = RCONST(1.0e-8);
atval[1] = RCONST(1.0e-14);
atval[2] = RCONST(1.0e-6);
/* Integration limits */
t0 = ZERO;
tout1 = RCONST(0.4);
PrintHeader(rtol, avtol, yy);
/* Call IDACreate and IDAInit to initialize IDA memory */
mem = IDACreate();
if(check_flag((void *)mem, "IDACreate", 0)) return(1);
retval = IDAInit(mem, resrob, t0, yy, yp);
if(check_flag(&retval, "IDAInit", 1)) return(1);
/* Call IDASVtolerances to set tolerances */
retval = IDASVtolerances(mem, rtol, avtol);
if(check_flag(&retval, "IDASVtolerances", 1)) return(1);
/* Free avtol */
N_VDestroy_Serial(avtol);
/* Call IDARootInit to specify the root function grob with 2 components */
retval = IDARootInit(mem, 2, grob);
if (check_flag(&retval, "IDARootInit", 1)) return(1);
/* Call IDADense and set up the linear solver. */
retval = IDADense(mem, NEQ);
if(check_flag(&retval, "IDADense", 1)) return(1);
retval = IDADlsSetDenseJacFn(mem, jacrob);
if(check_flag(&retval, "IDADlsSetDenseJacFn", 1)) return(1);
/* In loop, call IDASolve, print results, and test for error.
Break out of loop when NOUT preset output times have been reached. */
iout = 0; tout = tout1;
while(1) {
retval = IDASolve(mem, tout, &tret, yy, yp, IDA_NORMAL);
PrintOutput(mem,tret,yy);
if(check_flag(&retval, "IDASolve", 1)) return(1);
if (retval == IDA_ROOT_RETURN) {
retvalr = IDAGetRootInfo(mem, rootsfound);
check_flag(&retvalr, "IDAGetRootInfo", 1);
PrintRootInfo(rootsfound[0],rootsfound[1]);
}
if (retval == IDA_SUCCESS) {
iout++;
tout *= RCONST(10.0);
}
if (iout == NOUT) break;
}
PrintFinalStats(mem);
/* Free memory */
IDAFree(&mem);
N_VDestroy_Serial(yy);
N_VDestroy_Serial(yp);
return(0);
}
/*
*--------------------------------------------------------------------
* Functions called by IDA
*--------------------------------------------------------------------
*/
/*
* Define the system residual function.
*/
int resrob(realtype tres, N_Vector yy, N_Vector yp, N_Vector rr, void *user_data)
{
realtype *yval, *ypval, *rval;
yval = NV_DATA_S(yy);
ypval = NV_DATA_S(yp);
rval = NV_DATA_S(rr);
rval[0] = RCONST(-0.04)*yval[0] + RCONST(1.0e4)*yval[1]*yval[2];
rval[1] = -rval[0] - RCONST(3.0e7)*yval[1]*yval[1] - ypval[1];
rval[0] -= ypval[0];
rval[2] = yval[0] + yval[1] + yval[2] - ONE;
return(0);
}
/*
* Root function routine. Compute functions g_i(t,y) for i = 0,1.
*/
static int grob(realtype t, N_Vector yy, N_Vector yp, realtype *gout,
void *user_data)
{
realtype *yval, y1, y3;
yval = NV_DATA_S(yy);
y1 = yval[0]; y3 = yval[2];
gout[0] = y1 - RCONST(0.0001);
gout[1] = y3 - RCONST(0.01);
return(0);
}
/*
* Define the Jacobian function.
*/
int jacrob(long int Neq, realtype tt, realtype cj,
N_Vector yy, N_Vector yp, N_Vector resvec,
DlsMat JJ, void *user_data,
N_Vector tempv1, N_Vector tempv2, N_Vector tempv3)
{
realtype *yval;
yval = NV_DATA_S(yy);
IJth(JJ,1,1) = RCONST(-0.04) - cj;
IJth(JJ,2,1) = RCONST(0.04);
IJth(JJ,3,1) = ONE;
IJth(JJ,1,2) = RCONST(1.0e4)*yval[2];
IJth(JJ,2,2) = RCONST(-1.0e4)*yval[2] - RCONST(6.0e7)*yval[1] - cj;
IJth(JJ,3,2) = ONE;
IJth(JJ,1,3) = RCONST(1.0e4)*yval[1];
IJth(JJ,2,3) = RCONST(-1.0e4)*yval[1];
IJth(JJ,3,3) = ONE;
return(0);
}
/*
*--------------------------------------------------------------------
* Private functions
*--------------------------------------------------------------------
*/
/*
* Print first lines of output (problem description)
*/
static void PrintHeader(realtype rtol, N_Vector avtol, N_Vector y)
{
realtype *atval, *yval;
atval = NV_DATA_S(avtol);
yval = NV_DATA_S(y);
printf("\nidaRoberts_dns: Robertson kinetics DAE serial example problem for IDA\n");
printf(" Three equation chemical kinetics problem.\n\n");
printf("Linear solver: IDADENSE, with user-supplied Jacobian.\n");
#if defined(SUNDIALS_EXTENDED_PRECISION)
printf("Tolerance parameters: rtol = %Lg atol = %Lg %Lg %Lg \n",
rtol, atval[0],atval[1],atval[2]);
printf("Initial conditions y0 = (%Lg %Lg %Lg)\n",
yval[0], yval[1], yval[2]);
#elif defined(SUNDIALS_DOUBLE_PRECISION)
printf("Tolerance parameters: rtol = %lg atol = %lg %lg %lg \n",
rtol, atval[0],atval[1],atval[2]);
printf("Initial conditions y0 = (%lg %lg %lg)\n",
yval[0], yval[1], yval[2]);
#else
printf("Tolerance parameters: rtol = %g atol = %g %g %g \n",
rtol, atval[0],atval[1],atval[2]);
printf("Initial conditions y0 = (%g %g %g)\n",
yval[0], yval[1], yval[2]);
#endif
printf("Constraints and id not used.\n\n");
printf("-----------------------------------------------------------------------\n");
printf(" t y1 y2 y3");
printf(" | nst k h\n");
printf("-----------------------------------------------------------------------\n");
}
/*
* Print Output
*/
static void PrintOutput(void *mem, realtype t, N_Vector y)
{
realtype *yval;
int retval, kused;
long int nst;
realtype hused;
yval = NV_DATA_S(y);
retval = IDAGetLastOrder(mem, &kused);
check_flag(&retval, "IDAGetLastOrder", 1);
retval = IDAGetNumSteps(mem, &nst);
check_flag(&retval, "IDAGetNumSteps", 1);
retval = IDAGetLastStep(mem, &hused);
check_flag(&retval, "IDAGetLastStep", 1);
#if defined(SUNDIALS_EXTENDED_PRECISION)
printf("%10.4Le %12.4Le %12.4Le %12.4Le | %3ld %1d %12.4Le\n",
t, yval[0], yval[1], yval[2], nst, kused, hused);
#elif defined(SUNDIALS_DOUBLE_PRECISION)
printf("%10.4le %12.4le %12.4le %12.4le | %3ld %1d %12.4le\n",
t, yval[0], yval[1], yval[2], nst, kused, hused);
#else
printf("%10.4e %12.4e %12.4e %12.4e | %3ld %1d %12.4e\n",
t, yval[0], yval[1], yval[2], nst, kused, hused);
#endif
}
static void PrintRootInfo(int root_f1, int root_f2)
{
printf(" rootsfound[] = %3d %3d\n", root_f1, root_f2);
return;
}
/*
* Print final integrator statistics
*/
static void PrintFinalStats(void *mem)
{
int retval;
long int nst, nni, nje, nre, nreLS, netf, ncfn, nge;
retval = IDAGetNumSteps(mem, &nst);
check_flag(&retval, "IDAGetNumSteps", 1);
retval = IDAGetNumResEvals(mem, &nre);
check_flag(&retval, "IDAGetNumResEvals", 1);
retval = IDADlsGetNumJacEvals(mem, &nje);
check_flag(&retval, "IDADlsGetNumJacEvals", 1);
retval = IDAGetNumNonlinSolvIters(mem, &nni);
check_flag(&retval, "IDAGetNumNonlinSolvIters", 1);
retval = IDAGetNumErrTestFails(mem, &netf);
check_flag(&retval, "IDAGetNumErrTestFails", 1);
retval = IDAGetNumNonlinSolvConvFails(mem, &ncfn);
check_flag(&retval, "IDAGetNumNonlinSolvConvFails", 1);
retval = IDADlsGetNumResEvals(mem, &nreLS);
check_flag(&retval, "IDADlsGetNumResEvals", 1);
retval = IDAGetNumGEvals(mem, &nge);
check_flag(&retval, "IDAGetNumGEvals", 1);
printf("\nFinal Run Statistics: \n\n");
printf("Number of steps = %ld\n", nst);
printf("Number of residual evaluations = %ld\n", nre+nreLS);
printf("Number of Jacobian evaluations = %ld\n", nje);
printf("Number of nonlinear iterations = %ld\n", nni);
printf("Number of error test failures = %ld\n", netf);
printf("Number of nonlinear conv. failures = %ld\n", ncfn);
printf("Number of root fn. evaluations = %ld\n", nge);
}
/*
* 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);
} else if (opt == 1) {
/* Check if flag < 0 */
errflag = (int *) flagvalue;
if (*errflag < 0) {
fprintf(stderr,
"\nSUNDIALS_ERROR: %s() failed with flag = %d\n\n",
funcname, *errflag);
return(1);
}
} else if (opt == 2 && flagvalue == NULL) {
/* Check if function returned NULL pointer - no memory allocated */
fprintf(stderr,
"\nMEMORY_ERROR: %s() failed - returned NULL pointer\n\n",
funcname);
return(1);
}
return(0);
}
|