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
|
/*
* -----------------------------------------------------------------
* Programmers: Radu Serban, Alan Hindmarsh, and Cody Balos @ LLNL
* -----------------------------------------------------------------
* Simple 1D example to illustrate integrating over discontinuities:
*
* A) Discontinuity in solution
* y' = -y ; y(0) = 1 ; t = [0,1]
* y' = -y ; y(1) = 1 ; t = [1,2]
*
* B) Discontinuity in RHS (y')
* y' = -y ; y(0) = 1 ; t = [0,1]
* z' = -5*z ; z(1) = y(1) ; t = [1,2]
* This case is solved twice, first by explicitly treating the
* discontinuity point and secondly by letting the integrator
* deal with the discontinuity.
* -----------------------------------------------------------------
*/
#include <stdio.h>
#include <cvode/cvode.h> /* prototypes for CVODE functions and const */
#include <nvector/nvector_serial.h> /* access to serial NVector */
#include <sunmatrix/sunmatrix_dense.h> /* access to dense SUNMatrix */
#include <sunlinsol/sunlinsol_dense.h> /* access to dense SUNLinearSolver */
#include <sundials/sundials_types.h> /* defs. of realtype, sunindextype */
#if defined(SUNDIALS_EXTENDED_PRECISION)
#define GSYM "Lg"
#define ESYM "Le"
#define FSYM "Lf"
#else
#define GSYM "g"
#define ESYM "e"
#define FSYM "f"
#endif
/* Problem Constants */
#define NEQ 1 /* number of equations */
#define RHS1 1
#define RHS2 2
/* User provided routine called by the solver to compute
* the function f(t,y). */
static int f(realtype t, N_Vector y, N_Vector ydot, void *f_data);
/* Function for checking return values */
static int check_retval(void *flagvalue, const char *funcname, int opt);
int main()
{
void *cvode_mem;
SUNMatrix A;
SUNLinearSolver LS;
SUNContext sunctx;
N_Vector y;
int flag, retval;
realtype reltol, abstol, t0, t1, t2, t;
long int nst1, nst2, nst;
reltol = RCONST(1.0e-3);
abstol = RCONST(1.0e-4);
t0 = RCONST(0.0);
t1 = RCONST(1.0);
t2 = RCONST(2.0);
/* Create the SUNDIALS context */
retval = SUNContext_Create(NULL, &sunctx);
if(check_retval(&retval, "SUNContext_Create", 1)) return(1);
/* Allocate the vector of initial conditions */
y = N_VNew_Serial(NEQ, sunctx);
/* Set initial condition */
NV_Ith_S(y,0) = RCONST(1.0);
/*
* ------------------------------------------------------------
* Shared initialization and setup
* ------------------------------------------------------------
*/
/* Call CVodeCreate to create CVODE memory block and specify the
* Backward Differentiaion Formula */
cvode_mem = CVodeCreate(CV_BDF, sunctx);
if (check_retval((void *)cvode_mem, "CVodeCreate", 0)) return(1);
/* Call CVodeInit to initialize integrator memory and specify the
* user's right hand side function y'=f(t,y), the initial time T0
* and the initial condiition vector y. */
retval = CVodeInit(cvode_mem, f, t0, y);
if (check_retval((void *)&retval, "CVodeInit", 1)) return(1);
/* Call CVodeSStolerances to specify integration tolereances,
* specifically the scalar relative and absolute tolerance. */
retval = CVodeSStolerances(cvode_mem, reltol, abstol);
if (check_retval((void *)&retval, "CVodeSStolerances", 1)) return(1);
/* Provide RHS flag as user data which can be access in user provided routines */
retval = CVodeSetUserData(cvode_mem, &flag);
if (check_retval((void *)&retval, "CVodeSetUserData", 1)) return(1);
/* Create dense SUNMatrix for use in linear solver */
A = SUNDenseMatrix(NEQ, NEQ, sunctx);
if (check_retval((void *)A, "SUNDenseMatrix", 0)) return(1);
/* Create dense linear solver for use by CVode */
LS = SUNLinSol_Dense(y, A, sunctx);
if (check_retval((void *)LS, "SUNLinSol_Dense", 0)) return(1);
/* Attach the linear solver and matrix to CVode by calling CVodeSetLinearSolver */
retval = CVodeSetLinearSolver(cvode_mem, LS, A);
if (check_retval((void *)&retval, "CVodeSetLinearSolver", 1)) return(1);
/*
* ---------------------------------------------------------------
* Discontinuity in the solution
*
* 1) Integrate to the discontinuity
* 2) Integrate from the discontinuity
* ---------------------------------------------------------------
*/
/* ---- Integrate to the discontinuity */
printf("\nDiscontinuity in solution\n\n");
/* set TSTOP (max time solution proceeds to) - this is not required */
retval = CVodeSetStopTime(cvode_mem, t1);
if (check_retval((void *)&retval, "CVodeSetStopTime", 1)) return(1);
flag = RHS1; /* use -y for RHS */
t = t0; /* set the integrator start time */
printf("%12.8"ESYM" %12.8"ESYM"\n",t,NV_Ith_S(y,0));
while (t<t1) {
/* advance solver just one internal step */
retval = CVode(cvode_mem, t1, y, &t, CV_ONE_STEP);
if (check_retval((void *)&retval, "CVode", 1)) return(1);
printf("%12.8"ESYM" %12.8"ESYM"\n",t,NV_Ith_S(y,0));
}
/* Get the number of steps the solver took to get to the discont. */
retval = CVodeGetNumSteps(cvode_mem, &nst1);
if (check_retval((void *)&retval, "CvodeGetNumSteps", 1)) return(1);
/* ---- Integrate from the discontinuity */
/* Include discontinuity */
NV_Ith_S(y,0) = RCONST(1.0);
/* Reinitialize the solver */
retval = CVodeReInit(cvode_mem, t1, y);
if (check_retval((void *)&retval, "CVodeReInit", 1)) return(1);
/* set TSTOP (max time solution proceeds to) - this is not required */
retval = CVodeSetStopTime(cvode_mem, t2);
if (check_retval((void *)&retval, "CVodeSetStopTime", 1)) return(1);
flag = RHS1; /* use -y for RHS */
t = t1; /* set the integrator start time */
printf("%12.8"ESYM" %12.8"ESYM"\n",t,NV_Ith_S(y,0));
while (t<t2) {
/* advance solver just one internal step */
retval = CVode(cvode_mem, t2, y, &t, CV_ONE_STEP);
if (check_retval((void *)&retval, "CVode", 1)) return(1);
printf("%12.8"ESYM" %12.8"ESYM"\n",t,NV_Ith_S(y,0));
}
/* Get the number of steps the solver took after the discont. */
retval = CVodeGetNumSteps(cvode_mem, &nst2);
if (check_retval((void *)&retval, "CvodeGetNumSteps", 1)) return(1);
/* Print statistics */
nst = nst1 + nst2;
printf("\nNumber of steps: %ld + %ld = %ld\n",nst1, nst2, nst);
/*
* ---------------------------------------------------------------
* Discontinuity in RHS: Case 1 - explicit treatment
* Note that it is not required to set TSTOP, but without it
* we would have to find y(t1) to reinitialize the solver.
* ---------------------------------------------------------------
*/
printf("\nDiscontinuity in RHS: Case 1 - explicit treatment\n\n");
/* Set initial condition */
NV_Ith_S(y,0) = RCONST(1.0);
/* Reinitialize the solver. CVodeReInit does not reallocate memory
* so it can only be used when the new problem size is the same as
* the problem size when CVodeCreate was called. */
retval = CVodeReInit(cvode_mem, t0, y);
if (check_retval((void *)&retval, "CVodeReInit", 1)) return(1);
/* ---- Integrate to the discontinuity */
/* Set TSTOP (max time solution proceeds to) to location of discont. */
retval = CVodeSetStopTime(cvode_mem, t1);
if (check_retval((void *)&retval, "CVodeSetStopTime", 1)) return(1);
flag = RHS1; /* use -y for RHS */
t = t0; /* set the integrator start time */
printf("%12.8"ESYM" %12.8"ESYM"\n",t,NV_Ith_S(y,0));
while (t<t1) {
/* advance solver just one internal step */
retval = CVode(cvode_mem, t1, y, &t, CV_ONE_STEP);
if (check_retval((void *)&retval, "CVode", 1)) return(1);
printf("%12.8"ESYM" %12.8"ESYM"\n",t,NV_Ith_S(y,0));
}
/* Get the number of steps the solver took to get to the discont. */
retval = CVodeGetNumSteps(cvode_mem, &nst1);
if (check_retval((void *)&retval, "CvodeGetNumSteps", 1)) return(1);
/* If TSTOP was not set, we'd need to find y(t1): */
/* CVodeGetDky(cvode_mem, t1, 0, y); */
/* ---- Integrate from the discontinuity */
/* Reinitialize solver */
retval = CVodeReInit(cvode_mem, t1, y);
/* set TSTOP (max time solution proceeds to) - this is not required */
retval = CVodeSetStopTime(cvode_mem, t2);
if (check_retval((void *)&retval, "CVodeSetStopTime", 1)) return(1);
flag = RHS2; /* use -5y for RHS */
t = t1; /* set the integrator start time */
printf("%12.8"ESYM" %12.8"ESYM"\n",t,NV_Ith_S(y,0));
while (t<t2) {
/* advance solver just one internal step */
retval = CVode(cvode_mem, t2, y, &t, CV_ONE_STEP);
if (check_retval((void *)&retval, "CVode", 1)) return(1);
printf("%12.8"ESYM" %12.8"ESYM"\n",t,NV_Ith_S(y,0));
}
/* Get the number of steps the solver took after the discont. */
retval = CVodeGetNumSteps(cvode_mem, &nst2);
if (check_retval((void *)&retval, "CvodeGetNumSteps", 1)) return(1);
/* Print statistics */
nst = nst1 + nst2;
printf("\nNumber of steps: %ld + %ld = %ld\n",nst1, nst2, nst);
/*
* ---------------------------------------------------------------
* Discontinuity in RHS: Case 2 - let CVODE deal with it
* Note that here we MUST set TSTOP to ensure that the
* change in the RHS happens at the appropriate time
* ---------------------------------------------------------------
*/
printf("\nDiscontinuity in RHS: Case 2 - let CVODE deal with it\n\n");
/* Set initial condition */
NV_Ith_S(y,0) = RCONST(1.0);
/* Reinitialize the solver. CVodeReInit does not reallocate memory
* so it can only be used when the new problem size is the same as
* the problem size when CVodeCreate was called. */
retval = CVodeReInit(cvode_mem, t0, y);
if (check_retval((void *)&retval, "CVodeReInit", 1)) return(1);
/* ---- Integrate to the discontinuity */
/* Set TSTOP (max time solution proceeds to) to location of discont. */
retval = CVodeSetStopTime(cvode_mem, t1);
if (check_retval((void *)&retval, "CVodeSetStopTime", 1)) return(1);
flag = RHS1; /* use -y for RHS */
t = t0; /* set the integrator start time */
printf("%12.8"ESYM" %12.8"ESYM"\n",t,NV_Ith_S(y,0));
while (t<t1) {
/* advance solver just one internal step */
retval = CVode(cvode_mem, t1, y, &t, CV_ONE_STEP);
if (check_retval((void *)&retval, "CVode", 1)) return(1);
printf("%12.8"ESYM" %12.8"ESYM"\n",t,NV_Ith_S(y,0));
}
/* Get the number of steps the solver took to get to the discont. */
retval = CVodeGetNumSteps(cvode_mem, &nst1);
if (check_retval((void *)&retval, "CvodeGetNumSteps", 1)) return(1);
/* ---- Integrate from the discontinuity */
/* set TSTOP (max time solution proceeds to) - this is not required */
retval = CVodeSetStopTime(cvode_mem, t2);
if (check_retval((void *)&retval, "CVodeSetStopTime", 1)) return(1);
flag = RHS2; /* use -5y for RHS */
t = t1; /* set the integrator start time */
printf("%12.8"ESYM" %12.8"ESYM"\n",t,NV_Ith_S(y,0));
while (t<t2) {
/* advance solver just one internal step */
retval = CVode(cvode_mem, t2, y, &t, CV_ONE_STEP);
if (check_retval((void *)&retval, "CVode", 1)) return(1);
printf("%12.8"ESYM" %12.8"ESYM"\n",t,NV_Ith_S(y,0));
}
/* Get the number of steps the solver took after the discont. */
retval = CVodeGetNumSteps(cvode_mem, &nst);
if (check_retval((void *)&retval, "CvodeGetNumSteps", 1)) return(1);
/* Print statistics */
nst2 = nst - nst1;
printf("\nNumber of steps: %ld + %ld = %ld\n",nst1, nst2, nst);
/* Free memory */
N_VDestroy(y);
SUNMatDestroy(A);
SUNLinSolFree(LS);
CVodeFree(&cvode_mem);
SUNContext_Free(&sunctx);
return(0);
}
/*
* RHS function
* The form of the RHS function is controlled by the flag passed as f_data:
* flag = RHS1 -> y' = -y
* flag = RHS2 -> y' = -5*y
*/
static int f(realtype t, N_Vector y, N_Vector ydot, void *f_data)
{
int *flag;
flag = (int *) f_data;
switch(*flag) {
case RHS1:
NV_Ith_S(ydot,0) = -NV_Ith_S(y,0);
break;
case RHS2:
NV_Ith_S(ydot,0) = -5.0*NV_Ith_S(y,0);
break;
}
return(0);
}
/*
* 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_retval(void *flagvalue, const 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);
}
|