static void PrintOutput(void *cvode_mem, realtype t, N_Vector u) { long int nst; int qu, flag; realtype hu, *udata; udata = N_VGetArrayPointer_Serial(u); flag = CVodeGetNumSteps(cvode_mem, &nst); check_flag(&flag, "CVodeGetNumSteps", 1); flag = CVodeGetLastOrder(cvode_mem, &qu); check_flag(&flag, "CVodeGetLastOrder", 1); flag = CVodeGetLastStep(cvode_mem, &hu); check_flag(&flag, "CVodeGetLastStep", 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.3e %2d %8.3e %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.4e %12.4e %12.4e \n", udata[0], udata[1], udata[2]); #else printf("%12.4e %12.4e %12.4e \n", udata[0], udata[1], udata[2]); #endif }
int FCVBandJac(long int N, long int mupper, long int mlower, BandMat J, realtype t, N_Vector y, N_Vector fy, void *jac_data, N_Vector vtemp1, N_Vector vtemp2, N_Vector vtemp3) { int ier; realtype *ydata, *fydata, *jacdata, *v1data, *v2data, *v3data; realtype h; long int eband; FCVUserData CV_userdata; CVodeGetLastStep(CV_cvodemem, &h); ydata = N_VGetArrayPointer(y); fydata = N_VGetArrayPointer(fy); v1data = N_VGetArrayPointer(vtemp1); v2data = N_VGetArrayPointer(vtemp2); v3data = N_VGetArrayPointer(vtemp3); eband = (J->smu) + mlower + 1; jacdata = BAND_COL(J,0) - mupper; CV_userdata = (FCVUserData) jac_data; FCV_BJAC(&N, &mupper, &mlower, &eband, &t, ydata, fydata, jacdata, &h, CV_userdata->ipar, CV_userdata->rpar, v1data, v2data, v3data, &ier); return(ier); }
void FCVBandJac(long int N, long int mupper, long int mlower, BandMat J, realtype t, N_Vector y, N_Vector fy, void *jac_data, N_Vector vtemp1, N_Vector vtemp2, N_Vector vtemp3) { N_Vector ewt; realtype *ydata, *fydata, *jacdata, *ewtdata, *v1data, *v2data, *v3data; realtype h; long int eband; ewt = N_VClone(y); CVodeGetErrWeights(CV_cvodemem, ewt); CVodeGetLastStep(CV_cvodemem, &h); ydata = N_VGetArrayPointer(y); fydata = N_VGetArrayPointer(fy); v1data = N_VGetArrayPointer(vtemp1); v2data = N_VGetArrayPointer(vtemp2); v3data = N_VGetArrayPointer(vtemp3); ewtdata = N_VGetArrayPointer(ewt); eband = (J->smu) + mlower + 1; jacdata = BAND_COL(J,0) - mupper; FCV_BJAC(&N, &mupper, &mlower, &eband, &t, ydata, fydata, jacdata, ewtdata, &h, v1data, v2data, v3data); N_VDestroy(ewt); }
static void PrintOutput(void *cvode_mem, realtype t) { long int nst, nfe, nni; int qu, flag; realtype hu; flag = CVodeGetNumSteps(cvode_mem, &nst); check_flag(&flag, "CVodeGetNumSteps", 1); flag = CVodeGetNumRhsEvals(cvode_mem, &nfe); check_flag(&flag, "CVodeGetNumRhsEvals", 1); flag = CVodeGetNumNonlinSolvIters(cvode_mem, &nni); check_flag(&flag, "CVodeGetNumNonlinSolvIters", 1); flag = CVodeGetLastOrder(cvode_mem, &qu); check_flag(&flag, "CVodeGetLastOrder", 1); flag = CVodeGetLastStep(cvode_mem, &hu); check_flag(&flag, "CVodeGetLastStep", 1); #if defined(SUNDIALS_EXTENDED_PRECISION) printf("t = %10.2Le nst = %ld nfe = %ld nni = %ld", t, nst, nfe, nni); printf(" qu = %d hu = %11.2Le\n\n", qu, hu); #elif defined(SUNDIALS_DOUBLE_PRECISION) printf("t = %10.2le nst = %ld nfe = %ld nni = %ld", t, nst, nfe, nni); printf(" qu = %d hu = %11.2le\n\n", qu, hu); #else printf("t = %10.2e nst = %ld nfe = %ld nni = %ld", t, nst, nfe, nni); printf(" qu = %d hu = %11.2e\n\n", qu, hu); #endif }
static void PrintOutput(void *cvode_mem, realtype t, N_Vector u) { long int nst; int qu, flag; realtype hu; flag = CVodeGetNumSteps(cvode_mem, &nst); check_flag(&flag, "CVodeGetNumSteps", 1); flag = CVodeGetLastOrder(cvode_mem, &qu); check_flag(&flag, "CVodeGetLastOrder", 1); flag = CVodeGetLastStep(cvode_mem, &hu); check_flag(&flag, "CVodeGetLastStep", 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 \n", N_VMaxNorm(u)); #elif defined(SUNDIALS_DOUBLE_PRECISION) printf("%12.4le \n", N_VMaxNorm(u)); #else printf("%12.4e \n", N_VMaxNorm(u)); #endif }
int FCVDenseJac(int N, realtype t, N_Vector y, N_Vector fy, DlsMat J, void *user_data, N_Vector vtemp1, N_Vector vtemp2, N_Vector vtemp3) { int ier; realtype *ydata, *fydata, *jacdata, *v1data, *v2data, *v3data; realtype h; FCVUserData CV_userdata; CVodeGetLastStep(CV_cvodemem, &h); ydata = N_VGetArrayPointer(y); fydata = N_VGetArrayPointer(fy); v1data = N_VGetArrayPointer(vtemp1); v2data = N_VGetArrayPointer(vtemp2); v3data = N_VGetArrayPointer(vtemp3); jacdata = DENSE_COL(J,0); CV_userdata = (FCVUserData) user_data; FCV_DJAC(&N, &t, ydata, fydata, jacdata, &h, CV_userdata->ipar, CV_userdata->rpar, v1data, v2data, v3data, &ier); return(ier); }
int FCVPSet(realtype t, N_Vector y, N_Vector fy, booleantype jok, booleantype *jcurPtr, realtype gamma, void *P_data, N_Vector vtemp1, N_Vector vtemp2, N_Vector vtemp3) { int ier = 0; realtype *ydata, *fydata, *v1data, *v2data, *v3data; realtype h; FCVUserData CV_userdata; CVodeGetLastStep(CV_cvodemem, &h); ydata = N_VGetArrayPointer(y); fydata = N_VGetArrayPointer(fy); v1data = N_VGetArrayPointer(vtemp1); v2data = N_VGetArrayPointer(vtemp2); v3data = N_VGetArrayPointer(vtemp3); CV_userdata = (FCVUserData) P_data; FCV_PSET(&t, ydata, fydata, &jok, jcurPtr, &gamma, &h, CV_userdata->ipar, CV_userdata->rpar, v1data, v2data, v3data, &ier); return(ier); }
void FCVDenseJac(long int N, DenseMat J, realtype t, N_Vector y, N_Vector fy, void *jac_data, N_Vector vtemp1, N_Vector vtemp2, N_Vector vtemp3) { N_Vector ewt; realtype *ydata, *fydata, *jacdata, *ewtdata, *v1data, *v2data, *v3data; realtype h; ewt = N_VClone(y); CVodeGetErrWeights(CV_cvodemem, ewt); CVodeGetLastStep(CV_cvodemem, &h); ydata = N_VGetArrayPointer(y); fydata = N_VGetArrayPointer(fy); v1data = N_VGetArrayPointer(vtemp1); v2data = N_VGetArrayPointer(vtemp2); v3data = N_VGetArrayPointer(vtemp3); ewtdata = N_VGetArrayPointer(ewt); jacdata = DENSE_COL(J,0); FCV_DJAC(&N, &t, ydata, fydata, jacdata, ewtdata, &h, v1data, v2data, v3data); N_VDestroy(ewt); }
int FCVJtimes(N_Vector v, N_Vector Jv, realtype t, N_Vector y, N_Vector fy, void *user_data, N_Vector work) { realtype *vdata, *Jvdata, *ydata, *fydata, *wkdata; realtype h; FCVUserData CV_userdata; int ier = 0; CVodeGetLastStep(CV_cvodemem, &h); vdata = N_VGetArrayPointer(v); Jvdata = N_VGetArrayPointer(Jv); ydata = N_VGetArrayPointer(y); fydata = N_VGetArrayPointer(fy); wkdata = N_VGetArrayPointer(work); CV_userdata = (FCVUserData) user_data; FCV_JTIMES (vdata, Jvdata, &t, ydata, fydata, &h, CV_userdata->ipar, CV_userdata->rpar, wkdata, &ier); return(ier); }
static void PrintOutput(void *cvode_mem, int my_pe, MPI_Comm comm, N_Vector u, realtype t) { int qu, flag; realtype hu, *udata, tempu[2]; int npelast; long int i0, i1, nst; MPI_Status status; npelast = NPEX*NPEY - 1; udata = NV_DATA_P(u); /* Send c1,c2 at top right mesh point to PE 0 */ if (my_pe == npelast) { i0 = NVARS*MXSUB*MYSUB - 2; i1 = i0 + 1; if (npelast != 0) MPI_Send(&udata[i0], 2, PVEC_REAL_MPI_TYPE, 0, 0, comm); else { tempu[0] = udata[i0]; tempu[1] = udata[i1]; } } /* On PE 0, receive c1,c2 at top right, then print performance data and sampled solution values */ if (my_pe == 0) { if (npelast != 0) MPI_Recv(&tempu[0], 2, PVEC_REAL_MPI_TYPE, npelast, 0, comm, &status); flag = CVodeGetNumSteps(cvode_mem, &nst); check_flag(&flag, "CVodeGetNumSteps", 1, my_pe); flag = CVodeGetLastOrder(cvode_mem, &qu); check_flag(&flag, "CVodeGetLastOrder", 1, my_pe); flag = CVodeGetLastStep(cvode_mem, &hu); check_flag(&flag, "CVodeGetLastStep", 1, my_pe); #if defined(SUNDIALS_EXTENDED_PRECISION) printf("t = %.2Le no. steps = %ld order = %d stepsize = %.2Le\n", t, nst, qu, hu); printf("At bottom left: c1, c2 = %12.3Le %12.3Le \n", udata[0], udata[1]); printf("At top right: c1, c2 = %12.3Le %12.3Le \n\n", tempu[0], tempu[1]); #elif defined(SUNDIALS_DOUBLE_PRECISION) printf("t = %.2le no. steps = %ld order = %d stepsize = %.2le\n", t, nst, qu, hu); printf("At bottom left: c1, c2 = %12.3le %12.3le \n", udata[0], udata[1]); printf("At top right: c1, c2 = %12.3le %12.3le \n\n", tempu[0], tempu[1]); #else printf("t = %.2e no. steps = %ld order = %d stepsize = %.2e\n", t, nst, qu, hu); printf("At bottom left: c1, c2 = %12.3e %12.3e \n", udata[0], udata[1]); printf("At top right: c1, c2 = %12.3e %12.3e \n\n", tempu[0], tempu[1]); #endif } }
void OpenSMOKE_CVODE_Sundials<T>::Status() const { int flag; long int nst, nfe, nsetups, netf, nni, ncfn, nje, nfeLS, nge; int qcurrent, qlast; double hcurrent, hlast; flag = CVodeGetNumSteps(cvode_mem_, &nst); check_flag(&flag, std::string("CVodeGetNumSteps"), 1); flag = CVDlsGetNumJacEvals(cvode_mem_, &nje); check_flag(&flag, std::string("CVDlsGetNumJacEvals"), 1); flag = CVodeGetNumRhsEvals(cvode_mem_, &nfe); check_flag(&flag, std::string("CVodeGetNumRhsEvals"), 1); flag = CVodeGetNumLinSolvSetups(cvode_mem_, &nsetups); check_flag(&flag, std::string("CVodeGetNumLinSolvSetups"), 1); flag = CVodeGetNumErrTestFails(cvode_mem_, &netf); check_flag(&flag, std::string("CVodeGetNumErrTestFails"), 1); flag = CVodeGetNumNonlinSolvIters(cvode_mem_, &nni); check_flag(&flag, std::string("CVodeGetNumNonlinSolvIters"), 1); flag = CVodeGetNumNonlinSolvConvFails(cvode_mem_, &ncfn); check_flag(&flag, std::string("CVodeGetNumNonlinSolvConvFails"), 1); flag = CVodeGetNumGEvals(cvode_mem_, &nge); check_flag(&flag, std::string("CVodeGetNumGEvals"), 1); flag = CVDlsGetNumRhsEvals(cvode_mem_, &nfeLS); check_flag(&flag, std::string("CVDlsGetNumRhsEvals"), 1); flag = CVodeGetLastOrder(cvode_mem_, &qlast); check_flag(&flag, std::string("CVodeGetLastOrder"), 1); flag = CVodeGetCurrentOrder(cvode_mem_, &qcurrent); check_flag(&flag, std::string("CVodeGetCurrentOrder"), 1); flag = CVodeGetLastStep(cvode_mem_, &hlast); check_flag(&flag, std::string("CVodeGetLastStep"), 1); flag = CVodeGetCurrentStep(cvode_mem_, &hcurrent); check_flag(&flag, std::string("CVodeGetCurrentStep"), 1); std::cout << "CVODE Sundials Status" << std::endl; std::cout << " * Absolute tolerance: " << this->absTolerance_[0] << std::endl; // Absolute tolerance std::cout << " * Relative tolerance: " << this->relTolerance_[0] << std::endl; // Relative tolerance std::cout << " * Number of steps: " << nst << std::endl; // Number of steps taken for the problem so far std::cout << " * Number of function evaluations: " << nfe << std::endl; // Number of f evaluations for the problem so far. std::cout << " * Number of Jacobians: " << nje << std::endl; // Number of Jacobian evaluations (and of matrix LU decompositions) for the problem so far. std::cout << " * Last step: " << hlast << std::endl; std::cout << " * Next step: " << hcurrent << std::endl; std::cout << " * Last order: " << qlast << std::endl; std::cout << " * Next order: " << qcurrent << std::endl; }
static void PrintOutput(void *cvode_mem, realtype t, N_Vector y) { long int nst; int qu, flag; realtype hu; realtype *ydata; ydata = NV_DATA_S(y); flag = CVodeGetNumSteps(cvode_mem, &nst); check_flag(&flag, "CVodeGetNumSteps", 1); flag = CVodeGetLastOrder(cvode_mem, &qu); check_flag(&flag, "CVodeGetLastOrder", 1); flag = CVodeGetLastStep(cvode_mem, &hu); check_flag(&flag, "CVodeGetLastStep", 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 \n", IJKth(ydata,1,0,0), IJKth(ydata,1,MX-1,MZ-1)); #elif defined(SUNDIALS_DOUBLE_PRECISION) printf("%12.4le %12.4le \n", IJKth(ydata,1,0,0), IJKth(ydata,1,MX-1,MZ-1)); #else printf("%12.4e %12.4e \n", IJKth(ydata,1,0,0), IJKth(ydata,1,MX-1,MZ-1)); #endif printf(" "); #if defined(SUNDIALS_EXTENDED_PRECISION) printf("%12.4Le %12.4Le \n", IJKth(ydata,2,0,0), IJKth(ydata,2,MX-1,MZ-1)); #elif defined(SUNDIALS_DOUBLE_PRECISION) printf("%12.4le %12.4le \n", IJKth(ydata,2,0,0), IJKth(ydata,2,MX-1,MZ-1)); #else printf("%12.4e %12.4e \n", IJKth(ydata,2,0,0), IJKth(ydata,2,MX-1,MZ-1)); #endif }
static void PrintOutput(void *cvode_mem, N_Vector u,realtype t) { long int nst; int qu, flag; realtype hu, *udata; int mxh = MX/2 - 1, myh = MY/2 - 1, mx1 = MX - 1, my1 = MY - 1; udata = N_VGetArrayPointer_Serial(u); flag = CVodeGetNumSteps(cvode_mem, &nst); check_flag(&flag, "CVodeGetNumSteps", 1); flag = CVodeGetLastOrder(cvode_mem, &qu); check_flag(&flag, "CVodeGetLastOrder", 1); flag = CVodeGetLastStep(cvode_mem, &hu); check_flag(&flag, "CVodeGetLastStep", 1); #if defined(SUNDIALS_EXTENDED_PRECISION) printf("t = %.2Le no. steps = %ld order = %d stepsize = %.2Le\n", t, nst, qu, hu); printf("c1 (bot.left/middle/top rt.) = %12.3Le %12.3Le %12.3Le\n", IJKth(udata,1,0,0), IJKth(udata,1,mxh,myh), IJKth(udata,1,mx1,my1)); printf("c2 (bot.left/middle/top rt.) = %12.3Le %12.3Le %12.3Le\n\n", IJKth(udata,2,0,0), IJKth(udata,2,mxh,myh), IJKth(udata,2,mx1,my1)); #elif defined(SUNDIALS_DOUBLE_PRECISION) printf("t = %.2e no. steps = %ld order = %d stepsize = %.2e\n", t, nst, qu, hu); printf("c1 (bot.left/middle/top rt.) = %12.3e %12.3e %12.3e\n", IJKth(udata,1,0,0), IJKth(udata,1,mxh,myh), IJKth(udata,1,mx1,my1)); printf("c2 (bot.left/middle/top rt.) = %12.3e %12.3e %12.3e\n\n", IJKth(udata,2,0,0), IJKth(udata,2,mxh,myh), IJKth(udata,2,mx1,my1)); #else printf("t = %.2e no. steps = %ld order = %d stepsize = %.2e\n", t, nst, qu, hu); printf("c1 (bot.left/middle/top rt.) = %12.3e %12.3e %12.3e\n", IJKth(udata,1,0,0), IJKth(udata,1,mxh,myh), IJKth(udata,1,mx1,my1)); printf("c2 (bot.left/middle/top rt.) = %12.3e %12.3e %12.3e\n\n", IJKth(udata,2,0,0), IJKth(udata,2,mxh,myh), IJKth(udata,2,mx1,my1)); #endif }
static void PrintOutput(void *cvode_mem, int my_pe, realtype t, N_Vector u) { long int nst; int qu, retval; realtype hu, umax; retval = CVodeGetNumSteps(cvode_mem, &nst); check_retval(&retval, "CVodeGetNumSteps", 1, my_pe); retval = CVodeGetLastOrder(cvode_mem, &qu); check_retval(&retval, "CVodeGetLastOrder", 1, my_pe); retval = CVodeGetLastStep(cvode_mem, &hu); check_retval(&retval, "CVodeGetLastStep", 1, my_pe); umax = N_VMaxNorm(u); if (my_pe == 0) { #if defined(SUNDIALS_EXTENDED_PRECISION) printf("%8.3Le %2d %8.3Le %5ld\n", t,qu,hu,nst); #elif defined(SUNDIALS_DOUBLE_PRECISION) printf("%8.3e %2d %8.3e %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 \n", umax); #elif defined(SUNDIALS_DOUBLE_PRECISION) printf("%12.4e \n", umax); #else printf("%12.4e \n", umax); #endif } }
/* C interface to user-supplied Fortran routine FCVSPJAC; see fcvode.h for additional information */ int FCVSparseJac(realtype t, N_Vector y, N_Vector fy, SlsMat J, void *user_data, N_Vector vtemp1, N_Vector vtemp2, N_Vector vtemp3) { int ier; realtype *ydata, *fydata, *v1data, *v2data, *v3data; realtype h; FCVUserData CV_userdata; CVodeGetLastStep(CV_cvodemem, &h); ydata = N_VGetArrayPointer(y); fydata = N_VGetArrayPointer(fy); v1data = N_VGetArrayPointer(vtemp1); v2data = N_VGetArrayPointer(vtemp2); v3data = N_VGetArrayPointer(vtemp3); CV_userdata = (FCVUserData) user_data; FCV_SPJAC(&t, ydata, fydata, &(J->NP), &(J->NNZ), J->data, J->indexvals, J->indexptrs, &h, CV_userdata->ipar, CV_userdata->rpar, v1data, v2data, v3data, &ier); return(ier); }
static int Problem1(void) { realtype reltol=RTOL, abstol=ATOL, t, tout, ero, er; int miter, flag, temp_flag, iout, nerr=0; N_Vector y; void *cvode_mem; booleantype firstrun; int qu; realtype hu; y = NULL; cvode_mem = NULL; y = N_VNew_Serial(P1_NEQ); if(check_flag((void *)y, "N_VNew_Serial", 0)) return(1); PrintIntro1(); cvode_mem = CVodeCreate(CV_ADAMS, CV_FUNCTIONAL); if(check_flag((void *)cvode_mem, "CVodeCreate", 0)) return(1); for (miter=FUNC; miter <= DIAG; miter++) { ero = ZERO; NV_Ith_S(y,0) = TWO; NV_Ith_S(y,1) = ZERO; firstrun = (miter==FUNC); if (firstrun) { flag = CVodeMalloc(cvode_mem, f1, P1_T0, y, CV_SS, reltol, &abstol); if(check_flag(&flag, "CVodeMalloc", 1)) return(1); } else { flag = CVodeSetIterType(cvode_mem, CV_NEWTON); if(check_flag(&flag, "CVodeSetIterType", 1)) ++nerr; flag = CVodeReInit(cvode_mem, f1, P1_T0, y, CV_SS, reltol, &abstol); if(check_flag(&flag, "CVodeReInit", 1)) return(1); } flag = PrepareNextRun(cvode_mem, CV_ADAMS, miter, 0, 0); if(check_flag(&flag, "PrepareNextRun", 1)) return(1); PrintHeader1(); for(iout=1, tout=P1_T1; iout <= P1_NOUT; iout++, tout += P1_DTOUT) { flag = CVode(cvode_mem, tout, y, &t, CV_NORMAL); check_flag(&flag, "CVode", 1); temp_flag = CVodeGetLastOrder(cvode_mem, &qu); if(check_flag(&temp_flag, "CVodeGetLastOrder", 1)) ++nerr; temp_flag = CVodeGetLastStep(cvode_mem, &hu); if(check_flag(&temp_flag, "CVodeGetLastStep", 1)) ++nerr; PrintOutput1(t, NV_Ith_S(y,0), NV_Ith_S(y,1), qu, hu); if (flag != CV_SUCCESS) { nerr++; break; } if (iout%2 == 0) { er = ABS(NV_Ith_S(y,0)) / abstol; if (er > ero) ero = er; if (er > P1_TOL_FACTOR) { nerr++; PrintErrOutput(P1_TOL_FACTOR); } } } PrintFinalStats(cvode_mem, miter, ero); } CVodeFree(cvode_mem); cvode_mem = CVodeCreate(CV_BDF, CV_FUNCTIONAL); if(check_flag((void *)cvode_mem, "CVodeCreate", 0)) return(1); for (miter=FUNC; miter <= DIAG; miter++) { ero = ZERO; NV_Ith_S(y,0) = TWO; NV_Ith_S(y,1) = ZERO; firstrun = (miter==FUNC); if (firstrun) { flag = CVodeMalloc(cvode_mem, f1, P1_T0, y, CV_SS, reltol, &abstol); if(check_flag(&flag, "CVodeMalloc", 1)) return(1); } else { flag = CVodeSetIterType(cvode_mem, CV_NEWTON); if(check_flag(&flag, "CVodeSetIterType", 1)) ++nerr; flag = CVodeReInit(cvode_mem, f1, P1_T0, y, CV_SS, reltol, &abstol); if(check_flag(&flag, "CVodeReInit", 1)) return(1); } flag = PrepareNextRun(cvode_mem, CV_BDF, miter, 0, 0); if(check_flag(&flag, "PrepareNextRun", 1)) return(1); PrintHeader1(); for(iout=1, tout=P1_T1; iout <= P1_NOUT; iout++, tout += P1_DTOUT) { flag = CVode(cvode_mem, tout, y, &t, CV_NORMAL); check_flag(&flag, "CVode", 1); temp_flag = CVodeGetLastOrder(cvode_mem, &qu); if(check_flag(&temp_flag, "CVodeGetLastOrder", 1)) ++nerr; temp_flag = CVodeGetLastStep(cvode_mem, &hu); if(check_flag(&temp_flag, "CVodeGetLastStep", 1)) ++nerr; PrintOutput1(t, NV_Ith_S(y,0), NV_Ith_S(y,1), qu, hu); if (flag != CV_SUCCESS) { nerr++; break; } if (iout%2 == 0) { er = ABS(NV_Ith_S(y,0)) / abstol; if (er > ero) ero = er; if (er > P1_TOL_FACTOR) { nerr++; PrintErrOutput(P1_TOL_FACTOR); } } } PrintFinalStats(cvode_mem, miter, ero); } CVodeFree(cvode_mem); N_VDestroy_Serial(y); return(nerr); }
realtype SundialsCvode::getLastStep() { realtype h; CVodeGetLastStep(sundialsMem, &h); return h; }
void Cvode::CVodeCore() { _idid = CVodeReInit(_cvodeMem, _tCurrent, _CV_y); _idid = CVodeSetStopTime(_cvodeMem, _tEnd); _idid = CVodeSetInitStep(_cvodeMem, 1e-12); if (_idid < 0) throw ModelicaSimulationError(SOLVER,"CVode::ReInit"); bool writeEventOutput = (_settings->getGlobalSettings()->getOutputPointType() == OPT_ALL); bool writeOutput = !(_settings->getGlobalSettings()->getOutputPointType() == OPT_NONE); while ((_solverStatus & ISolver::CONTINUE) && !_interrupt ) { _cv_rt = CVode(_cvodeMem, _tEnd, _CV_y, &_tCurrent, CV_ONE_STEP); _idid = CVodeGetNumSteps(_cvodeMem, &_locStps); if (_idid != CV_SUCCESS) throw ModelicaSimulationError(SOLVER,"CVodeGetNumSteps failed. The cvode mem pointer is NULL"); _idid = CVodeGetLastStep(_cvodeMem, &_h); if (_idid != CV_SUCCESS) throw ModelicaSimulationError(SOLVER,"CVodeGetLastStep failed. The cvode mem pointer is NULL"); //set completed step to system and check if terminate was called if(_continuous_system->stepCompleted(_tCurrent)) _solverStatus = DONE; //Check if there was at least one output-point within the last solver interval // -> Write output if true if (writeOutput) { writeCVodeOutput(_tCurrent, _h, _locStps); } #ifdef RUNTIME_PROFILING MEASURETIME_REGION_DEFINE(cvodeStepCompletedHandler, "CVodeStepCompleted"); if(MeasureTime::getInstance() != NULL) { MEASURETIME_START(measuredFunctionStartValues, cvodeStepCompletedHandler, "CVodeStepCompleted"); } #endif #ifdef RUNTIME_PROFILING if(MeasureTime::getInstance() != NULL) { MEASURETIME_END(measuredFunctionStartValues, measuredFunctionEndValues, (*measureTimeFunctionsArray)[5], cvodeStepCompletedHandler); } #endif // Perform state selection bool state_selection = stateSelection(); if (state_selection) _continuous_system->getContinuousStates(_z); _zeroFound = false; // Check if step was successful if (check_flag(&_cv_rt, "CVode", 1)) { _solverStatus = ISolver::SOLVERERROR; break; } // A root was found if ((_cv_rt == CV_ROOT_RETURN) && !isInterrupted()) { // CVode is setting _tCurrent to the time where the first event occurred double _abs = fabs(_tLastEvent - _tCurrent); _zeroFound = true; if ((_abs < 1e-3) && _event_n == 0) { _tLastEvent = _tCurrent; _event_n++; } else if ((_abs < 1e-3) && (_event_n >= 1 && _event_n < 500)) { _event_n++; } else if ((_abs >= 1e-3)) { //restart event counter _tLastEvent = _tCurrent; _event_n = 0; } else throw ModelicaSimulationError(EVENT_HANDLING,"Number of events exceeded in time interval " + to_string(_abs) + " at time " + to_string(_tCurrent)); // CVode has interpolated the states at time 'tCurrent' _time_system->setTime(_tCurrent); // To get steep steps in the result file, two value points (P1 and P2) must be added // // Y | (P2) X........... // | : // | : // |........X (P1) // |----------------------------------> // | ^ t // _tCurrent // Write the values of (P1) if (writeEventOutput) { _continuous_system->evaluateAll(IContinuous::CONTINUOUS); writeToFile(0, _tCurrent, _h); } _idid = CVodeGetRootInfo(_cvodeMem, _zeroSign); for (int i = 0; i < _dimZeroFunc; i++) _events[i] = bool(_zeroSign[i]); if (_mixed_system->handleSystemEvents(_events)) { // State variables were reinitialized, thus we have to give these values to the cvode-solver // Take care about the memory regions, _z is the same like _CV_y _continuous_system->getContinuousStates(_z); } } if ((_zeroFound || state_selection)&& !isInterrupted()) { // Write the values of (P2) if (writeEventOutput) { // If we want to write the event-results, we should evaluate the whole system again _continuous_system->evaluateAll(IContinuous::CONTINUOUS); writeToFile(0, _tCurrent, _h); } _idid = CVodeReInit(_cvodeMem, _tCurrent, _CV_y); if (_idid < 0) throw ModelicaSimulationError(SOLVER,"CVode::ReInit()"); // Der Eventzeitpunkt kann auf der Endzeit liegen (Time-Events). In diesem Fall wird der Solver beendet, da CVode sonst eine interne Warnung schmeißt if (_tCurrent == _tEnd) _cv_rt = CV_TSTOP_RETURN; if(_continuous_system->stepCompleted(_tCurrent)) _solverStatus = DONE; } // Zähler für die Anzahl der ausgegebenen Schritte erhöhen ++_outStps; _tLastSuccess = _tCurrent; if (_cv_rt == CV_TSTOP_RETURN) { _time_system->setTime(_tEnd); //Solver has finished calculation - calculate the final values _continuous_system->setContinuousStates(NV_DATA_S(_CV_y)); _continuous_system->evaluateAll(IContinuous::CONTINUOUS); if(writeOutput) writeToFile(0, _tEnd, _h); _accStps += _locStps; _solverStatus = DONE; } } }
static int Problem2(void) { realtype reltol=RTOL, abstol=ATOL, t, tout, er, erm, ero; int miter, flag, temp_flag, nerr=0; N_Vector y; void *cvode_mem; booleantype firstrun; int qu, iout; realtype hu; y = NULL; cvode_mem = NULL; y = N_VNew_Serial(P2_NEQ); if(check_flag((void *)y, "N_VNew", 0)) return(1); PrintIntro2(); cvode_mem = CVodeCreate(CV_ADAMS, CV_FUNCTIONAL); if(check_flag((void *)cvode_mem, "CVodeCreate", 0)) return(1); for (miter=FUNC; miter <= BAND_DQ; miter++) { if ((miter==DENSE_USER) || (miter==DENSE_DQ)) continue; ero = ZERO; N_VConst(ZERO, y); NV_Ith_S(y,0) = ONE; firstrun = (miter==FUNC); if (firstrun) { flag = CVodeMalloc(cvode_mem, f2, P2_T0, y, CV_SS, reltol, &abstol); if(check_flag(&flag, "CVodeMalloc", 1)) return(1); } else { flag = CVodeSetIterType(cvode_mem, CV_NEWTON); if(check_flag(&flag, "CVodeSetIterType", 1)) ++nerr; flag = CVodeReInit(cvode_mem, f2, P2_T0, y, CV_SS, reltol, &abstol); if(check_flag(&flag, "CVodeReInit", 1)) return(1); } flag = PrepareNextRun(cvode_mem, CV_ADAMS, miter, P2_MU, P2_ML); if(check_flag(&flag, "PrepareNextRun", 1)) return(1); PrintHeader2(); for(iout=1, tout=P2_T1; iout <= P2_NOUT; iout++, tout*=P2_TOUT_MULT) { flag = CVode(cvode_mem, tout, y, &t, CV_NORMAL); check_flag(&flag, "CVode", 1); erm = MaxError(y, t); temp_flag = CVodeGetLastOrder(cvode_mem, &qu); if(check_flag(&temp_flag, "CVodeGetLastOrder", 1)) ++nerr; temp_flag = CVodeGetLastStep(cvode_mem, &hu); if(check_flag(&temp_flag, "CVodeGetLastStep", 1)) ++nerr; PrintOutput2(t, erm, qu, hu); if (flag != CV_SUCCESS) { nerr++; break; } er = erm / abstol; if (er > ero) ero = er; if (er > P2_TOL_FACTOR) { nerr++; PrintErrOutput(P2_TOL_FACTOR); } } PrintFinalStats(cvode_mem, miter, ero); } CVodeFree(cvode_mem); cvode_mem = CVodeCreate(CV_BDF, CV_FUNCTIONAL); if(check_flag((void *)cvode_mem, "CVodeCreate", 0)) return(1); for (miter=FUNC; miter <= BAND_DQ; miter++) { if ((miter==DENSE_USER) || (miter==DENSE_DQ)) continue; ero = ZERO; N_VConst(ZERO, y); NV_Ith_S(y,0) = ONE; firstrun = (miter==FUNC); if (firstrun) { flag = CVodeMalloc(cvode_mem, f2, P2_T0, y, CV_SS, reltol, &abstol); if(check_flag(&flag, "CVodeMalloc", 1)) return(1); } else { flag = CVodeSetIterType(cvode_mem, CV_NEWTON); if(check_flag(&flag, "CVodeSetIterType", 1)) ++nerr; flag = CVodeReInit(cvode_mem, f2, P2_T0, y, CV_SS, reltol, &abstol); if(check_flag(&flag, "CVodeReInit", 1)) return(1); } flag = PrepareNextRun(cvode_mem, CV_BDF, miter, P2_MU, P2_ML); if(check_flag(&flag, "PrepareNextRun", 1)) return(1); PrintHeader2(); for(iout=1, tout=P2_T1; iout <= P2_NOUT; iout++, tout*=P2_TOUT_MULT) { flag = CVode(cvode_mem, tout, y, &t, CV_NORMAL); check_flag(&flag, "CVode", 1); erm = MaxError(y, t); temp_flag = CVodeGetLastOrder(cvode_mem, &qu); if(check_flag(&temp_flag, "CVodeGetLastOrder", 1)) ++nerr; temp_flag = CVodeGetLastStep(cvode_mem, &hu); if(check_flag(&temp_flag, "CVodeGetLastStep", 1)) ++nerr; PrintOutput2(t, erm, qu, hu); if (flag != CV_SUCCESS) { nerr++; break; } er = erm / abstol; if (er > ero) ero = er; if (er > P2_TOL_FACTOR) { nerr++; PrintErrOutput(P2_TOL_FACTOR); } } PrintFinalStats(cvode_mem, miter, ero); } CVodeFree(cvode_mem); N_VDestroy_Serial(y); return(nerr); }
void handleError( void *cvode_mem, N_Vector y0, int flag, mxArray *plhs[], int nrhs, int sensitivity, N_Vector *yS0, realtype *sensitivities, struct mData *data ) { #ifdef DEBUG long int temp; realtype tempreal; #endif #ifdef DEBUG printf( "<<< DEBUG OUTPUT >>>\n" ); printf( "PARAMETERS: \n" ); for ( temp = 0; temp < N_PARAMS; temp++ ) { printf( "P(%d) = %f\n", temp, data->p[ temp ] ); } CVodeGetNumSteps(cvode_mem, &temp); printf( "Number of steps taken by the solver: %d\n", temp ); CVodeGetNumErrTestFails(cvode_mem, &temp); printf( "Number of local error test failures: %d\n", temp ); CVodeGetLastStep(cvode_mem, &tempreal); printf( "Last stepsize: %f\n", tempreal ); CVodeGetCurrentStep(cvode_mem, &tempreal); printf( "Last step: %f\n", tempreal ); #endif if ( sensitivity == 1 ) { if ( nrhs < 7 ) free( sensitivities ); free( data->p ); N_VDestroyVectorArray_Serial( yS0, N_STATES + N_PARAMS ); if ( plhs[2] != NULL ) mxDestroyArray(plhs[2]); } if ( plhs[1] != NULL ) mxDestroyArray(plhs[1]); if ( plhs[0] != NULL ) mxDestroyArray(plhs[0]); N_VDestroy_Serial( y0 ); /*printf( "Freeing..." );*/ /*CVodeFree( &cvode_mem );*/ /*printf( "Success!\n" );*/ switch( flag ) { case CV_MEM_NULL: printf( "ERROR: No memory was allocated for cvode_mem\n" ); break; case CV_NO_MALLOC: printf( "ERROR: Forgot or failed CVodeInit\n" ); break; case CV_ILL_INPUT: printf( "ERROR: Input for CVode was illegal\n" ); break; case CV_TOO_CLOSE: printf( "ERROR: Initial time too close to final time\n" ); break; case CV_TOO_MUCH_WORK: printf( "ERROR: Solver took maximum number of internal steps, but hasn't reached t_out\n" ); break; case CV_TOO_MUCH_ACC: printf( "ERROR: Could not attain desired accuracy\n" ); break; case CV_ERR_FAILURE: printf( "ERROR: Error tests failed too many times\n" ); break; case CV_CONV_FAILURE: printf( "ERROR: Convergence failure in solving the linear system\n" ); break; case CV_LINIT_FAIL: printf( "ERROR: Linear solver failed to initialize\n" ); break; case CV_LSETUP_FAIL: printf( "ERROR: Linear solver setup failed\n" ); break; case CV_RHSFUNC_FAIL: printf( "ERROR: Right hand side failed in an unrecoverable manner\n" ); break; case CV_REPTD_RHSFUNC_ERR: printf( "ERROR: Convergence test failures occured too many times in RHS\n" ); break; case CV_UNREC_RHSFUNC_ERR: printf( "ERROR: Unrecoverable error in the RHS\n" ); break; case CV_RTFUNC_FAIL: printf( "ERROR: Rootfinding function failed!\n" ); break; default: printf( "ERROR: I have no idea what's going on :(\n" ); break; } mexErrMsgTxt( "Aborting" ); }