/* * ----------------------------------------------------------------- * Programmers: Radu Serban and Alan Hindmarsh @ 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. * * NOTE: For readibility, no checks are performed on the various * function return flags. * ----------------------------------------------------------------- */ #include #include #include #include #define Ith(v,i) NV_Ith_S(v,i-1) #define RHS1 1 #define RHS2 2 static int f(realtype t, N_Vector y, N_Vector ydot, void *f_data); int main() { void *cvode_mem; N_Vector y; int neq, flag, ret; realtype reltol, abstol, t0, t1, t2, t; long int nst1, nst2, nst; neq = 1; reltol = RCONST(1.0e-3); abstol = RCONST(1.0e-4); t0 = RCONST(0.0); t1 = RCONST(1.0); t2 = RCONST(2.0); y = N_VNew_Serial(neq); /* * --------------------------------------------------------------- * Discontinuity in solution * Note that it is not required to set TSTOP. * --------------------------------------------------------------- */ printf("\nDiscontinuity in solution\n\n"); /* Initialize solver */ cvode_mem = CVodeCreate(CV_BDF, CV_NEWTON); Ith(y,1) = 1.0; ret = CVodeInit(cvode_mem, f, t0, y); ret = CVodeSStolerances(cvode_mem, reltol, abstol); ret = CVodeSetUserData(cvode_mem, &flag); ret = CVDense(cvode_mem, neq); /* Integrate to the point of discontinuity */ ret = CVodeSetStopTime(cvode_mem, t1); flag = RHS1; t = t0; printf("%12.8e %12.8e\n",t,Ith(y,1)); while (t 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: Ith(ydot,1) = -Ith(y,1); break; case RHS2: Ith(ydot,1) = -5.0*Ith(y,1); break; } return(0); }