OpenSMOKE_CVODE_Sundials<T>::~OpenSMOKE_CVODE_Sundials(void) { /* Free vectors */ N_VDestroy_Serial(y0Sundials_); N_VDestroy_Serial(ySundials_); /* Free integrator memory */ CVodeFree(&cvode_mem_); SUNLinSolFree(LS); SUNMatDestroy(A); delete[] this->y0_; delete[] this->y_; }
void KinsolSolver::reset() { if (!mSolver) // The solver hasn't been initialised, so... return; N_VDestroy_Serial(mParametersVector); N_VDestroy_Serial(mOnesVector); KINFree(&mSolver); delete mUserData; }
CVodesIntegrator::~CVodesIntegrator() { if (m_cvode_mem) { if (m_np > 0) { CVodeSensFree(m_cvode_mem); } CVodeFree(&m_cvode_mem); } if (m_y) { N_VDestroy_Serial(m_y); } if (m_abstol) { N_VDestroy_Serial(m_abstol); } }
void CVodesIntegrator::sensInit(double t0, FuncEval& func) { m_np = func.nparams(); m_sens_ok = false; N_Vector y = N_VNew_Serial(static_cast<sd_size_t>(func.neq())); m_yS = N_VCloneVectorArray_Serial(static_cast<sd_size_t>(m_np), y); for (size_t n = 0; n < m_np; n++) { N_VConst(0.0, m_yS[n]); } N_VDestroy_Serial(y); int flag = CVodeSensInit(m_cvode_mem, static_cast<sd_size_t>(m_np), CV_STAGGERED, CVSensRhsFn(0), m_yS); if (flag != CV_SUCCESS) { throw CanteraError("CVodesIntegrator::sensInit", "Error in CVodeSensInit"); } vector_fp atol(m_np); for (size_t n = 0; n < m_np; n++) { // This scaling factor is tuned so that reaction and species enthalpy // sensitivities can be computed simultaneously with the same abstol. atol[n] = m_abstolsens / func.m_paramScales[n]; } flag = CVodeSensSStolerances(m_cvode_mem, m_reltolsens, atol.data()); }
/* frees only sensitivity structure, not used at the moment */ static void IntegratorInstance_freeIDASpecSolverStructures(integratorInstance_t *engine) { /* Free sensitivity vector yS */ N_VDestroy_Serial(engine->solver->dy); engine->solver->dy = NULL; }
int cvode_free(solver_props *props){ unsigned int modelid; /* // Debug code long int count; for(modelid=0; modelid<mem[0].props->num_models; modelid++){ PRINTF( "Model number: %d\n", modelid); if (CVodeGetNumSteps(mem->cvmem, &count) == CV_SUCCESS) PRINTF( " Total Number of steps: %ld\n", count); if (CVodeGetNumRhsEvals(mem->cvmem, &count) == CV_SUCCESS) PRINTF( " RHS Evals: %ld\n", count); if (CVodeGetNumErrTestFails(mem->cvmem, &count) == CV_SUCCESS) PRINTF( " Num of step errors: %ld\n", count); } */ // Cleanup cvode_mem *mem = props->mem; for(modelid = 0; modelid<props->num_models; modelid++){ N_VDestroy_Serial(((N_Vector)(mem[modelid].y0))); CVodeFree(&(mem[modelid].cvmem)); } free(mem); return 0; }
IdaSolver::~IdaSolver() { if (!mSolver) // The solver hasn't been initialised, so... return; // Delete some internal objects N_VDestroy_Serial(mStatesVector); N_VDestroy_Serial(mRatesVector); IDAFree(&mSolver); delete mUserData; }
N_Vector N_VClone_Serial(N_Vector w) { N_Vector v; realtype *data; long int length; v = NULL; v = N_VCloneEmpty_Serial(w); if (v == NULL) return(NULL); length = NV_LENGTH_S(w); /* Create data */ if (length > 0) { /* Allocate memory */ data = NULL; data = (realtype *) malloc(length * sizeof(realtype)); if(data == NULL) { N_VDestroy_Serial(v); return(NULL); } /* Attach data */ NV_OWN_DATA_S(v) = TRUE; NV_DATA_S(v) = data; } return(v); }
static void Problem2(void) { void *fct; void *cpode_mem; N_Vector y, yp; realtype reltol=RTOL, abstol=ATOL, t, tout, erm, hu; int flag, iout, qu; y = NULL; yp = NULL; cpode_mem = NULL; y = N_VNew_Serial(P2_NEQ); N_VConst(ZERO, y); NV_Ith_S(y,0) = ONE; yp = N_VNew_Serial(P2_NEQ); if (ODE == CP_EXPL) { fct = (void *)f2; } else { fct = (void *)res2; f2(P2_T0, y, yp, NULL); } cpode_mem = CPodeCreate(ODE, CP_ADAMS, CP_FUNCTIONAL); /* flag = CPodeSetInitStep(cpode_mem, 2.0e-9);*/ flag = CPodeInit(cpode_mem, fct, NULL, P2_T0, y, yp, CP_SS, reltol, &abstol); printf("\n t max.err qu hu \n"); for(iout=1, tout=P2_T1; iout <= P2_NOUT; iout++, tout*=P2_TOUT_MULT) { flag = CPode(cpode_mem, tout, &t, y, yp, CP_NORMAL); if (flag != CP_SUCCESS) break; erm = MaxError(y, t); flag = CPodeGetLastOrder(cpode_mem, &qu); flag = CPodeGetLastStep(cpode_mem, &hu); printf("%10.3f %12.4le %2d %12.4le\n", t, erm, qu, hu); } PrintFinalStats(cpode_mem); CPodeFree(&cpode_mem); N_VDestroy_Serial(y); N_VDestroy_Serial(yp); return; }
int main() { void *fct; void *cpode_mem; N_Vector y, yp; realtype reltol=RTOL, abstol=ATOL, t, tout, hu; int flag, iout, qu; y = NULL; yp = NULL; cpode_mem = NULL; y = N_VNew_Serial(P1_NEQ); NV_Ith_S(y,0) = TWO; NV_Ith_S(y,1) = ZERO; yp = N_VNew_Serial(P1_NEQ); if (ODE == CP_EXPL) { fct = (void *)f; } else { fct = (void *)res; f(P1_T0, y, yp, NULL); } cpode_mem = CPodeCreate(ODE, CP_ADAMS, CP_FUNCTIONAL); /* flag = CPodeSetInitStep(cpode_mem, 4.0e-9);*/ flag = CPodeInit(cpode_mem, fct, NULL, P1_T0, y, yp, CP_SS, reltol, &abstol); printf("\n t x xdot qu hu \n"); for(iout=1, tout=P1_T1; iout <= P1_NOUT; iout++, tout += P1_DTOUT) { flag = CPode(cpode_mem, tout, &t, y, yp, CP_NORMAL); if (flag != CP_SUCCESS) break; flag = CPodeGetLastOrder(cpode_mem, &qu); flag = CPodeGetLastStep(cpode_mem, &hu); printf("%10.5f %12.5le %12.5le %2d %6.4le\n", t, NV_Ith_S(y,0), NV_Ith_S(y,1), qu, hu); } PrintFinalStats(cpode_mem); CPodeFree(&cpode_mem); N_VDestroy_Serial(y); N_VDestroy_Serial(yp); return 0; }
void ViCaRS::cleanup(void) { unsigned int i; // Free y and abstol vectors N_VDestroy_Serial(_vars); for (i=0;i<_solvers.size();++i) delete _solvers[i]; }
void N_VDestroyVectorArray_Serial(N_Vector *vs, int count) { int j; for (j = 0; j < count; j++) N_VDestroy_Serial(vs[j]); free(vs); }
CVodesIntegrator::~CVodesIntegrator() { if (m_cvode_mem) { if (m_np > 0) { CVodeSensFree(m_cvode_mem); } CVodeFree(&m_cvode_mem); } if (m_y) { N_VDestroy_Serial(m_y); } if (m_abstol) { N_VDestroy_Serial(m_abstol); } if (m_yS) { N_VDestroyVectorArray_Serial(m_yS, static_cast<sd_size_t>(m_np)); } }
void ode_solver_free(ode_solver* solver){ free(solver->params); CVodeFree(&(solver->cvode_mem)); if (solver->yS != 0) { N_VDestroyVectorArray_Serial(solver->yS, solver->odeModel->P); } N_VDestroy_Serial(solver->y); free(solver); }
Ida::~Ida() { if (_z) delete[] _z; if (_zInit) delete[] _zInit; if (_zeroSign) delete[] _zeroSign; if (_absTol) delete[] _absTol; if (_zWrite) delete[] _zWrite; if (_ida_initialized) { N_VDestroy_Serial(_CV_y0); N_VDestroy_Serial(_CV_y); N_VDestroy_Serial(_CV_yp); N_VDestroy_Serial(_CV_yWrite); N_VDestroy_Serial(_CV_absTol); IDAFree(&_idaMem); } if (_colorOfColumn) delete [] _colorOfColumn; if(_delta) delete [] _delta; if(_deltaInv) delete [] _deltaInv; if(_ysave) delete [] _ysave; #ifdef RUNTIME_PROFILING if(measuredFunctionStartValues) delete measuredFunctionStartValues; if(measuredFunctionEndValues) delete measuredFunctionEndValues; if(solveFunctionStartValues) delete solveFunctionStartValues; if(solveFunctionEndValues) delete solveFunctionEndValues; if(solverValues) delete solverValues; #endif }
static void FreeUserData(WebData wdata) { int i, ngrp; ngrp = wdata->ngrp; for(i=0; i < ngrp; i++) { destroyMat((wdata->P)[i]); destroyArray((wdata->pivot)[i]); } N_VDestroy_Serial(wdata->rewt); free(wdata); }
void CVodesIntegrator::setTolerances(double reltol, int n, double* abstol) { m_itol = CV_SV; m_nabs = n; if (n != m_neq) { if (m_abstol) N_VDestroy_Serial(nv(m_abstol)); m_abstol = reinterpret_cast<void*>(N_VNew_Serial(n)); } for (int i=0; i<n; i++) { NV_Ith_S(nv(m_abstol), i) = abstol[i]; } m_reltol = reltol; }
int main(void) { realtype dx, dy, reltol, abstol, t, tout, umax; N_Vector u, up; UserData data; void *cvode_mem; int iout, flag; long int nst; u = NULL; data = NULL; cvode_mem = NULL; u = N_VNew_Serial(NEQ); up = N_VNew_Serial(NEQ); reltol = ZERO; abstol = ATOL; data = (UserData) malloc(sizeof *data); dx = data->dx = XMAX/(MX+1); dy = data->dy = YMAX/(MY+1); data->hdcoef = ONE/(dx*dx); data->hacoef = HALF/(TWO*dx); data->vdcoef = ONE/(dy*dy); SetIC(u, data); cvode_mem = CPodeCreate(CP_EXPL, CP_BDF, CP_NEWTON); flag = CPodeInit(cvode_mem, (void *)f, data, T0, u, NULL, CP_SS, reltol, &abstol); flag = CPLapackBand(cvode_mem, NEQ, MY, MY); flag = CPDlsSetJacFn(cvode_mem, (void *)Jac, data); /* In loop over output points: call CPode, print results, test for errors */ umax = N_VMaxNorm(u); PrintHeader(reltol, abstol, umax); for(iout=1, tout=T1; iout <= NOUT; iout++, tout += DTOUT) { flag = CPode(cvode_mem, tout, &t, u, up, CP_NORMAL); umax = N_VMaxNorm(u); flag = CPodeGetNumSteps(cvode_mem, &nst); PrintOutput(t, umax, nst); } PrintFinalStats(cvode_mem); N_VDestroy_Serial(u); CPodeFree(&cvode_mem); free(data); return(0); }
int DestroyIntegrator(struct Integrator** integrator) { struct Integrator* intg = *integrator; if (intg) { if (intg->y) N_VDestroy_Serial(intg->y); if (intg->cvode_mem) CVodeFree(&(intg->cvode_mem)); if (intg->simulation) DestroySimulation(&(intg->simulation)); free(intg); } *integrator = NULL; return(OK); }
//main int main(int argc, char **argv){ int i, j, n, pv=0; char* file_name; cs *jac; N_Vector u; Gen *gens; double *u0; //load ps file, populate ps, and grab some data if(argc > 1) file_name = argv[1]; else{ printf("Please specify PS data file."); return 1; } PS ps=GetPS(file_name); if(ps==NULL){ printf("Unable to build PS data.\n"); return 1; } n=(ps->num_buses-1)<<1; gens=ps->gens; u=N_VNew_Serial(n); u0=NV_DATA_S(u); //create an initial condition if(gens[pv].index==ps->swing_bus)pv++; for(i=0;i<ps->ybus->n;i++){ if(i==ps->swing_bus) continue; j=(i>ps->swing_bus?(i-1)<<1:i<<1); if(gens[pv].index==i){ u0[j]=0.1; u0[j+1]=0.1; pv++; if(pv<ps->num_gens&&gens[pv].index==ps->swing_bus)pv++; continue; } u0[j]=1.0; u0[j+1]=0.1; } //numerically evaluate the jacobian at initial condition jac=CheckDerivativeN(n, n, ComputeF, u, (void*)ps); cs_di_print(jac, 0); //free memory and return N_VDestroy_Serial(u); return 0; }
void CVodesIntegrator::setTolerances(double reltol, size_t n, double* abstol) { m_itol = CV_SV; m_nabs = n; if (n != m_neq) { if (m_abstol) { N_VDestroy_Serial(m_abstol); } m_abstol = N_VNew_Serial(static_cast<sd_size_t>(n)); } for (size_t i=0; i<n; i++) { NV_Ith_S(m_abstol, i) = abstol[i]; } m_reltol = reltol; }
CvodeSolver::~CvodeSolver() { // Make sure that the solver has been initialised if (!mSolver) return; // Delete some internal objects N_VDestroy_Serial(mStatesVector); CVodeFree(&mSolver); delete mUserData; }
void ode_solver_setErrTol(ode_solver* solver, const double rel_tol, double* abs_tol, const int abs_tol_len){ if( (abs_tol_len != 1) && (abs_tol_len != solver->odeModel->N)){ fprintf(stderr,"ode_solver_setErrTol: length of abs_tol must be 1 or equal to the number of variables in the ode model.\n"); return ; } /* set tollerances to the cvode_mem internal structure */ if ( abs_tol_len == 1 ) CVodeSStolerances(solver->cvode_mem, rel_tol, abs_tol[0]); else{ N_Vector abs_tol_vec = N_VNewEmpty_Serial(abs_tol_len); /* alloc */ NV_DATA_S(abs_tol_vec) = abs_tol; CVodeSVtolerances(solver->cvode_mem, rel_tol, abs_tol_vec); N_VDestroy_Serial(abs_tol_vec); /* free */ } }
static void FreeUserData(UserData data) { int jx, jy; for (jx=0; jx < MX; jx++) { for (jy=0; jy < MY; jy++) { destroyMat((data->P)[jx][jy]); destroyArray((data->pivot)[jx][jy]); } } destroyMat(acoef); free(bcoef); free(cox); free(coy); N_VDestroy_Serial(data->rates); free(data); }
int main() { void *cpode_mem; N_Vector yref, yy0; realtype tol, tout; int i, flag; tout = 30.0; /* Get reference solution */ yref = N_VNew_Serial(4); RefSol(tout, yref); /* Initialize solver */ tol = TOL; cpode_mem = CPodeCreate(CP_EXPL, CP_BDF, CP_NEWTON); yy0 = N_VNew_Serial(4); Ith(yy0,1) = 1.0; /* x */ Ith(yy0,2) = 0.0; /* y */ Ith(yy0,3) = 0.0; /* xd */ Ith(yy0,4) = 0.0; /* yd */ flag = CPodeInit(cpode_mem, (void *)f, NULL, 0.0, yy0, NULL, CP_SS, tol, &tol); flag = CPodeSetMaxNumSteps(cpode_mem, 50000); flag = CPodeSetStopTime(cpode_mem, tout); flag = CPodeProjDefine(cpode_mem, proj, NULL); flag = CPDense(cpode_mem, 4); for (i=0;i<5;i++) { printf("\n\n%.2e\n", tol); GetSol(cpode_mem, yy0, tol, tout, TRUE, yref); GetSol(cpode_mem, yy0, tol, tout, FALSE, yref); tol /= 10.0; } N_VDestroy_Serial(yref); CPodeFree(&cpode_mem); return(0); }
void SOLVER(cvode, free, TARGET, SIMENGINE_STORAGE, cvode_mem *mem){ unsigned int modelid; /* // Debug code long int count; for(modelid=0; modelid<mem[0].props->num_models; modelid++){ fprintf(stderr, "Model number: %d\n", modelid); if (CVodeGetNumSteps(mem->cvmem, &count) == CV_SUCCESS) fprintf(stderr, " Total Number of steps: %ld\n", count); if (CVodeGetNumRhsEvals(mem->cvmem, &count) == CV_SUCCESS) fprintf(stderr, " RHS Evals: %ld\n", count); if (CVodeGetNumErrTestFails(mem->cvmem, &count) == CV_SUCCESS) fprintf(stderr, " Num of step errors: %ld\n", count); } */ // Cleanup for(modelid = 0; modelid<mem[0].props->num_models; modelid++){ N_VDestroy_Serial(((N_Vector)(mem[modelid].y0))); CVodeFree(&(mem[modelid].cvmem)); } free(mem[0].k1); free(mem); }
int main() { void *mem; UserData data; N_Vector uu, up, constraints, res; int ier, iout; realtype rtol, atol, t0, t1, tout, tret; long int netf, ncfn, ncfl; mem = NULL; data = NULL; uu = up = constraints = res = NULL; /* Allocate N-vectors and the user data structure. */ uu = N_VNew_Serial(NEQ); if(check_flag((void *)uu, "N_VNew_Serial", 0)) return(1); up = N_VNew_Serial(NEQ); if(check_flag((void *)up, "N_VNew_Serial", 0)) return(1); res = N_VNew_Serial(NEQ); if(check_flag((void *)res, "N_VNew_Serial", 0)) return(1); constraints = N_VNew_Serial(NEQ); if(check_flag((void *)constraints, "N_VNew_Serial", 0)) return(1); data = (UserData) malloc(sizeof *data); data->pp = NULL; if(check_flag((void *)data, "malloc", 2)) return(1); /* Assign parameters in the user data structure. */ data->mm = MGRID; data->dx = ONE/(MGRID-ONE); data->coeff = ONE/(data->dx * data->dx); data->pp = N_VNew_Serial(NEQ); if(check_flag((void *)data->pp, "N_VNew_Serial", 0)) return(1); /* Initialize uu, up. */ SetInitialProfile(data, uu, up, res); /* Set constraints to all 1's for nonnegative solution values. */ N_VConst(ONE, constraints); /* Assign various parameters. */ t0 = ZERO; t1 = RCONST(0.01); rtol = ZERO; atol = RCONST(1.0e-3); /* Call IDACreate and IDAMalloc to initialize solution */ mem = IDACreate(); if(check_flag((void *)mem, "IDACreate", 0)) return(1); ier = IDASetUserData(mem, data); if(check_flag(&ier, "IDASetUserData", 1)) return(1); ier = IDASetConstraints(mem, constraints); if(check_flag(&ier, "IDASetConstraints", 1)) return(1); N_VDestroy_Serial(constraints); ier = IDAInit(mem, resHeat, t0, uu, up); if(check_flag(&ier, "IDAInit", 1)) return(1); ier = IDASStolerances(mem, rtol, atol); if(check_flag(&ier, "IDASStolerances", 1)) return(1); /* Call IDASpgmr to specify the linear solver. */ ier = IDASpgmr(mem, 0); if(check_flag(&ier, "IDASpgmr", 1)) return(1); ier = IDASpilsSetPreconditioner(mem, PsetupHeat, PsolveHeat); if(check_flag(&ier, "IDASpilsSetPreconditioner", 1)) return(1); /* Print output heading. */ PrintHeader(rtol, atol); /* * ------------------------------------------------------------------------- * CASE I * ------------------------------------------------------------------------- */ /* Print case number, output table heading, and initial line of table. */ printf("\n\nCase 1: gsytpe = MODIFIED_GS\n"); printf("\n Output Summary (umax = max-norm of solution) \n\n"); printf(" time umax k nst nni nje nre nreLS h npe nps\n" ); printf("----------------------------------------------------------------------\n"); /* Loop over output times, call IDASolve, and print results. */ for (tout = t1,iout = 1; iout <= NOUT ; iout++, tout *= TWO) { ier = IDASolve(mem, tout, &tret, uu, up, IDA_NORMAL); if(check_flag(&ier, "IDASolve", 1)) return(1); PrintOutput(mem, tret, uu); } /* Print remaining counters. */ ier = IDAGetNumErrTestFails(mem, &netf); check_flag(&ier, "IDAGetNumErrTestFails", 1); ier = IDAGetNumNonlinSolvConvFails(mem, &ncfn); check_flag(&ier, "IDAGetNumNonlinSolvConvFails", 1); ier = IDASpilsGetNumConvFails(mem, &ncfl); check_flag(&ier, "IDASpilsGetNumConvFails", 1); printf("\nError test failures = %ld\n", netf); printf("Nonlinear convergence failures = %ld\n", ncfn); printf("Linear convergence failures = %ld\n", ncfl); /* * ------------------------------------------------------------------------- * CASE II * ------------------------------------------------------------------------- */ /* Re-initialize uu, up. */ SetInitialProfile(data, uu, up, res); /* Re-initialize IDA and IDASPGMR */ ier = IDAReInit(mem, t0, uu, up); if(check_flag(&ier, "IDAReInit", 1)) return(1); ier = IDASpilsSetGSType(mem, CLASSICAL_GS); if(check_flag(&ier, "IDASpilsSetGSType",1)) return(1); /* Print case number, output table heading, and initial line of table. */ printf("\n\nCase 2: gstype = CLASSICAL_GS\n"); printf("\n Output Summary (umax = max-norm of solution) \n\n"); printf(" time umax k nst nni nje nre nreLS h npe nps\n" ); printf("----------------------------------------------------------------------\n"); /* Loop over output times, call IDASolve, and print results. */ for (tout = t1,iout = 1; iout <= NOUT ; iout++, tout *= TWO) { ier = IDASolve(mem, tout, &tret, uu, up, IDA_NORMAL); if(check_flag(&ier, "IDASolve", 1)) return(1); PrintOutput(mem, tret, uu); } /* Print remaining counters. */ ier = IDAGetNumErrTestFails(mem, &netf); check_flag(&ier, "IDAGetNumErrTestFails", 1); ier = IDAGetNumNonlinSolvConvFails(mem, &ncfn); check_flag(&ier, "IDAGetNumNonlinSolvConvFails", 1); ier = IDASpilsGetNumConvFails(mem, &ncfl); check_flag(&ier, "IDASpilsGetNumConvFails", 1); printf("\nError test failures = %ld\n", netf); printf("Nonlinear convergence failures = %ld\n", ncfn); printf("Linear convergence failures = %ld\n", ncfl); /* Free Memory */ IDAFree(&mem); N_VDestroy_Serial(uu); N_VDestroy_Serial(up); N_VDestroy_Serial(res); N_VDestroy_Serial(data->pp); free(data); return(0); }
int main() { realtype abstol=ATOL, reltol=RTOL, t, tout; N_Vector c; WebData wdata; void *cvode_mem; booleantype firstrun; int jpre, gstype, flag; int ns, mxns, iout; c = NULL; wdata = NULL; cvode_mem = NULL; /* Initializations */ c = N_VNew_Serial(NEQ); if(check_flag((void *)c, "N_VNew_Serial", 0)) return(1); wdata = AllocUserData(); if(check_flag((void *)wdata, "AllocUserData", 2)) return(1); InitUserData(wdata); ns = wdata->ns; mxns = wdata->mxns; /* Print problem description */ PrintIntro(); /* Loop over jpre and gstype (four cases) */ for (jpre = PREC_LEFT; jpre <= PREC_RIGHT; jpre++) { for (gstype = MODIFIED_GS; gstype <= CLASSICAL_GS; gstype++) { /* Initialize c and print heading */ CInit(c, wdata); PrintHeader(jpre, gstype); /* Call CVodeInit or CVodeReInit, then CVSpgmr to set up problem */ firstrun = (jpre == PREC_LEFT) && (gstype == MODIFIED_GS); if (firstrun) { cvode_mem = CVodeCreate(CV_BDF, CV_NEWTON); if(check_flag((void *)cvode_mem, "CVodeCreate", 0)) return(1); wdata->cvode_mem = cvode_mem; flag = CVodeSetUserData(cvode_mem, wdata); if(check_flag(&flag, "CVodeSetUserData", 1)) return(1); flag = CVodeInit(cvode_mem, f, T0, c); if(check_flag(&flag, "CVodeInit", 1)) return(1); flag = CVodeSStolerances(cvode_mem, reltol, abstol); if (check_flag(&flag, "CVodeSStolerances", 1)) return(1); flag = CVSpgmr(cvode_mem, jpre, MAXL); if(check_flag(&flag, "CVSpgmr", 1)) return(1); flag = CVSpilsSetGSType(cvode_mem, gstype); if(check_flag(&flag, "CVSpilsSetGSType", 1)) return(1); flag = CVSpilsSetEpsLin(cvode_mem, DELT); if(check_flag(&flag, "CVSpilsSetEpsLin", 1)) return(1); flag = CVSpilsSetPreconditioner(cvode_mem, Precond, PSolve); if(check_flag(&flag, "CVSpilsSetPreconditioner", 1)) return(1); } else { flag = CVodeReInit(cvode_mem, T0, c); if(check_flag(&flag, "CVodeReInit", 1)) return(1); flag = CVSpilsSetPrecType(cvode_mem, jpre); check_flag(&flag, "CVSpilsSetPrecType", 1); flag = CVSpilsSetGSType(cvode_mem, gstype); if(check_flag(&flag, "CVSpilsSetGSType", 1)) return(1); } /* Print initial values */ if (firstrun) PrintAllSpecies(c, ns, mxns, T0); /* Loop over output points, call CVode, print sample solution values. */ tout = T1; for (iout = 1; iout <= NOUT; iout++) { flag = CVode(cvode_mem, tout, c, &t, CV_NORMAL); PrintOutput(cvode_mem, t); if (firstrun && (iout % 3 == 0)) PrintAllSpecies(c, ns, mxns, t); if(check_flag(&flag, "CVode", 1)) break; if (tout > RCONST(0.9)) tout += DTOUT; else tout *= TOUT_MULT; } /* Print final statistics, and loop for next case */ PrintFinalStats(cvode_mem); } } /* Free all memory */ CVodeFree(&cvode_mem); N_VDestroy_Serial(c); FreeUserData(wdata); return(0); }
int main(void) { int globalstrategy; realtype fnormtol, scsteptol; N_Vector cc, sc, constraints; UserData data; int flag, maxl, maxlrst; void *kmem; cc = sc = constraints = NULL; kmem = NULL; data = NULL; /* Allocate memory, and set problem data, initial values, tolerances */ globalstrategy = KIN_NONE; data = AllocUserData(); if (check_flag((void *)data, "AllocUserData", 2)) return(1); InitUserData(data); /* Create serial vectors of length NEQ */ cc = N_VNew_Serial(NEQ); if (check_flag((void *)cc, "N_VNew_Serial", 0)) return(1); sc = N_VNew_Serial(NEQ); if (check_flag((void *)sc, "N_VNew_Serial", 0)) return(1); data->rates = N_VNew_Serial(NEQ); if (check_flag((void *)data->rates, "N_VNew_Serial", 0)) return(1); constraints = N_VNew_Serial(NEQ); if (check_flag((void *)constraints, "N_VNew_Serial", 0)) return(1); N_VConst(TWO, constraints); SetInitialProfiles(cc, sc); fnormtol=FTOL; scsteptol=STOL; /* Call KINCreate/KINInit to initialize KINSOL. A pointer to KINSOL problem memory is returned and stored in kmem. */ kmem = KINCreate(); if (check_flag((void *)kmem, "KINCreate", 0)) return(1); /* Vector cc passed as template vector. */ flag = KINInit(kmem, func, cc); if (check_flag(&flag, "KINInit", 1)) return(1); flag = KINSetUserData(kmem, data); if (check_flag(&flag, "KINSetUserData", 1)) return(1); flag = KINSetConstraints(kmem, constraints); if (check_flag(&flag, "KINSetConstraints", 1)) return(1); flag = KINSetFuncNormTol(kmem, fnormtol); if (check_flag(&flag, "KINSetFuncNormTol", 1)) return(1); flag = KINSetScaledStepTol(kmem, scsteptol); if (check_flag(&flag, "KINSetScaledStepTol", 1)) return(1); /* We no longer need the constraints vector since KINSetConstraints creates a private copy for KINSOL to use. */ N_VDestroy_Serial(constraints); /* Call KINSpgmr to specify the linear solver KINSPGMR with preconditioner routines PrecSetupBD and PrecSolveBD. */ maxl = 15; maxlrst = 2; flag = KINSpgmr(kmem, maxl); if (check_flag(&flag, "KINSpgmr", 1)) return(1); flag = KINSpilsSetMaxRestarts(kmem, maxlrst); if (check_flag(&flag, "KINSpilsSetMaxRestarts", 1)) return(1); flag = KINSpilsSetPreconditioner(kmem, PrecSetupBD, PrecSolveBD); if (check_flag(&flag, "KINSpilsSetPreconditioner", 1)) return(1); /* Print out the problem size, solution parameters, initial guess. */ PrintHeader(globalstrategy, maxl, maxlrst, fnormtol, scsteptol); /* Call KINSol and print output concentration profile */ flag = KINSol(kmem, /* KINSol memory block */ cc, /* initial guess on input; solution vector */ globalstrategy, /* global stragegy choice */ sc, /* scaling vector, for the variable cc */ sc); /* scaling vector for function values fval */ if (check_flag(&flag, "KINSol", 1)) return(1); printf("\n\nComputed equilibrium species concentrations:\n"); PrintOutput(cc); /* Print final statistics and free memory */ PrintFinalStats(kmem); N_VDestroy_Serial(cc); N_VDestroy_Serial(sc); KINFree(&kmem); FreeUserData(data); return(0); }
int main() { realtype abstol, reltol, t, tout; N_Vector u; UserData data; void *cvode_mem; int flag, iout, jpre; long int ml, mu; u = NULL; data = NULL; cvode_mem = NULL; /* Allocate and initialize u, and set problem data and tolerances */ u = N_VNew_Serial(NEQ); if(check_flag((void *)u, "N_VNew_Serial", 0)) return(1); data = (UserData) malloc(sizeof *data); if(check_flag((void *)data, "malloc", 2)) return(1); InitUserData(data); SetInitialProfiles(u, data->dx, data->dy); abstol = ATOL; reltol = RTOL; /* Call CVodeCreate to create the solver memory and specify the * Backward Differentiation Formula and the use of a Newton iteration */ cvode_mem = CVodeCreate(CV_BDF, CV_NEWTON); if(check_flag((void *)cvode_mem, "CVodeCreate", 0)) return(1); /* Set the pointer to user-defined data */ flag = CVodeSetUserData(cvode_mem, data); if(check_flag(&flag, "CVodeSetUserData", 1)) return(1); /* Call CVodeInit to initialize the integrator memory and specify the * user's right hand side function in u'=f(t,u), the inital time T0, and * the initial dependent variable vector u. */ flag = CVodeInit(cvode_mem, f, T0, u); if(check_flag(&flag, "CVodeInit", 1)) return(1); /* Call CVodeSStolerances to specify the scalar relative tolerance * and scalar absolute tolerances */ flag = CVodeSStolerances(cvode_mem, reltol, abstol); if (check_flag(&flag, "CVodeSStolerances", 1)) return(1); /* Call CVSpgmr to specify the linear solver CVSPGMR with left preconditioning and the maximum Krylov dimension maxl */ flag = CVSpgmr(cvode_mem, PREC_LEFT, 0); if(check_flag(&flag, "CVSpgmr", 1)) return(1); /* Call CVBandPreInit to initialize band preconditioner */ ml = mu = 2; flag = CVBandPrecInit(cvode_mem, NEQ, mu, ml); if(check_flag(&flag, "CVBandPrecInit", 0)) return(1); PrintIntro(mu, ml); /* Loop over jpre (= PREC_LEFT, PREC_RIGHT), and solve the problem */ for (jpre = PREC_LEFT; jpre <= PREC_RIGHT; jpre++) { /* On second run, re-initialize u, the solver, and CVSPGMR */ if (jpre == PREC_RIGHT) { SetInitialProfiles(u, data->dx, data->dy); flag = CVodeReInit(cvode_mem, T0, u); if(check_flag(&flag, "CVodeReInit", 1)) return(1); flag = CVSpilsSetPrecType(cvode_mem, PREC_RIGHT); check_flag(&flag, "CVSpilsSetPrecType", 1); printf("\n\n-------------------------------------------------------"); printf("------------\n"); } printf("\n\nPreconditioner type is: jpre = %s\n\n", (jpre == PREC_LEFT) ? "PREC_LEFT" : "PREC_RIGHT"); /* In loop over output points, call CVode, print results, test for error */ for (iout = 1, tout = TWOHR; iout <= NOUT; iout++, tout += TWOHR) { flag = CVode(cvode_mem, tout, u, &t, CV_NORMAL); check_flag(&flag, "CVode", 1); PrintOutput(cvode_mem, u, t); if (flag != CV_SUCCESS) { break; } } /* Print final statistics */ PrintFinalStats(cvode_mem); } /* End of jpre loop */ /* Free memory */ N_VDestroy_Serial(u); free(data); CVodeFree(&cvode_mem); return(0); }