9 #include "sundials/sundials_types.h" 10 #include "sundials/sundials_math.h" 12 #if CT_SUNDIALS_VERSION >= 30 13 #if CT_SUNDIALS_USE_LAPACK 14 #include "sunlinsol/sunlinsol_lapackdense.h" 15 #include "sunlinsol/sunlinsol_lapackband.h" 17 #include "sunlinsol/sunlinsol_dense.h" 18 #include "sunlinsol/sunlinsol_band.h" 20 #include "sunlinsol/sunlinsol_spgmr.h" 21 #include "ida/ida_direct.h" 22 #include "ida/ida_spils.h" 24 #include "ida/ida_dense.h" 25 #include "ida/ida_spgmr.h" 26 #include "ida/ida_band.h" 28 #include "nvector/nvector_serial.h" 32 #if CT_SUNDIALS_VERSION < 25 33 typedef int sd_size_t;
35 typedef long int sd_size_t;
78 static int ida_resid(realtype t, N_Vector y, N_Vector ydot, N_Vector r,
void* f_data)
85 int flag = f->
evalResidNJ(t, delta_t, NV_DATA_S(y), NV_DATA_S(ydot),
110 #if CT_SUNDIALS_VERSION >= 30 111 static int ida_jacobian(realtype t, realtype c_j, N_Vector y, N_Vector yp,
112 N_Vector r, SUNMatrix Jac,
void *f_data,
113 N_Vector tmp1, N_Vector tmp2, N_Vector tmp3)
120 if (SUNMatGetID(Jac) == SUNMATRIX_DENSE) {
121 cols = SM_COLS_D(Jac);
122 }
else if (SUNMatGetID(Jac) == SUNMATRIX_BAND) {
123 cols = SM_COLS_B(Jac);
132 static int ida_jacobian(sd_size_t nrows, realtype t, realtype c_j, N_Vector y, N_Vector ydot, N_Vector r,
133 DlsMat Jac,
void* f_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3)
140 Jac->cols, NV_DATA_S(r));
176 m_maxErrTestFails(-1),
178 m_maxNonlinConvFails(-1),
185 IDA_Solver::~IDA_Solver()
191 N_VDestroy_Serial(
m_y);
194 N_VDestroy_Serial(
m_ydot);
197 N_VDestroy_Serial(m_abstol);
200 N_VDestroy_Serial(m_constraints);
206 return NV_Ith_S(
m_y,k);
209 const doublereal* IDA_Solver::solutionVector()
const 211 return NV_DATA_S(
m_y);
216 return NV_Ith_S(
m_ydot,k);
219 const doublereal* IDA_Solver::derivativeVector()
const 228 m_abstol = N_VNew_Serial(
m_neq);
230 for (
int i = 0; i <
m_neq; i++) {
231 NV_Ith_S(m_abstol, i) = abstol[i];
235 int flag = IDASVtolerances(
m_ida_mem, m_reltol, m_abstol);
236 if (flag != IDA_SUCCESS) {
238 "Memory allocation failed.");
249 int flag = IDASStolerances(
m_ida_mem, m_reltol, m_abstols);
250 if (flag != IDA_SUCCESS) {
252 "Memory allocation failed.");
257 void IDA_Solver::setLinearSolverType(
int solverType)
264 setLinearSolverType(0);
274 void IDA_Solver::setMaxOrder(
int n)
305 #if CT_SUNDIALS_VERSION >= 30 310 if (flag != IDA_SUCCESS) {
312 "IDADlsSetDenseJacFn failed.");
317 void IDA_Solver::setMaxErrTestFailures(
int maxErrTestFails)
332 void IDA_Solver::inclAlgebraicInErrorTest(
bool yesno)
348 N_VDestroy_Serial(
m_y);
351 N_VDestroy_Serial(
m_ydot);
354 N_VDestroy_Serial(m_id);
357 N_VDestroy_Serial(m_constraints);
362 m_constraints = N_VNew_Serial(
m_neq);
364 for (
int i=0; i<
m_neq; i++) {
365 NV_Ith_S(
m_y, i) = 0.0;
366 NV_Ith_S(
m_ydot, i) = 0.0;
367 NV_Ith_S(m_constraints, i) = 0.0;
381 if (m_itol == IDA_SV) {
383 if (flag != IDA_SUCCESS) {
384 if (flag == IDA_MEM_FAIL) {
386 "Memory allocation failed.");
387 }
else if (flag == IDA_ILL_INPUT) {
389 "Illegal value for IDAInit input argument.");
391 throw CanteraError(
"IDA_Solver::init",
"IDAInit failed.");
394 flag = IDASVtolerances(
m_ida_mem, m_reltol, m_abstol);
395 if (flag != IDA_SUCCESS) {
396 throw CanteraError(
"IDA_Solver::init",
"Memory allocation failed.");
400 if (flag != IDA_SUCCESS) {
401 if (flag == IDA_MEM_FAIL) {
403 "Memory allocation failed.");
404 }
else if (flag == IDA_ILL_INPUT) {
406 "Illegal value for IDAInit input argument.");
408 throw CanteraError(
"IDA_Solver::init",
"IDAInit failed.");
411 flag = IDASStolerances(
m_ida_mem, m_reltol, m_abstols);
412 if (flag != IDA_SUCCESS) {
413 throw CanteraError(
"IDA_Solver::init",
"Memory allocation failed.");
418 if (m_type == 1 || m_type == 0) {
421 #if CT_SUNDIALS_VERSION >= 30 422 SUNLinSolFree((SUNLinearSolver)
m_linsol);
425 #if CT_SUNDIALS_USE_LAPACK 436 throw CanteraError(
"IDA_Solver::init",
"IDADense failed");
438 }
else if (m_type == 2) {
440 long int nu = m_mupper;
441 long int nl = m_mlower;
442 #if CT_SUNDIALS_VERSION >= 30 443 SUNLinSolFree((SUNLinearSolver)
m_linsol);
446 #if CT_SUNDIALS_USE_LAPACK 458 "unsupported linear solver type");
462 #if CT_SUNDIALS_VERSION >= 30 467 if (flag != IDA_SUCCESS) {
469 "IDADlsSetDenseJacFn failed.");
475 flag = IDASetUserData(
m_ida_mem, m_fdata.get());
476 if (flag != IDA_SUCCESS) {
477 throw CanteraError(
"IDA_Solver::init",
"IDASetUserData failed.");
483 if (flag != IDA_SUCCESS) {
484 throw CanteraError(
"IDA_Solver::init",
"IDASetMaxOrd failed.");
489 if (flag != IDA_SUCCESS) {
490 throw CanteraError(
"IDA_Solver::init",
"IDASetMaxNumSteps failed.");
495 if (flag != IDA_SUCCESS) {
496 throw CanteraError(
"IDA_Solver::init",
"IDASetInitStep failed.");
501 if (flag != IDA_SUCCESS) {
502 throw CanteraError(
"IDA_Solver::init",
"IDASetStopTime failed.");
507 if (flag != IDA_SUCCESS) {
509 "IDASetMaxErrTestFails failed.");
514 if (flag != IDA_SUCCESS) {
516 "IDASetmaxNonlinIters failed.");
521 if (flag != IDA_SUCCESS) {
523 "IDASetMaxConvFails failed.");
528 if (flag != IDA_SUCCESS) {
529 throw CanteraError(
"IDA_Solver::init",
"IDASetSuppressAlg failed.");
536 doublereal tout1 = tout;
545 int flag = IDACalcIC(
m_ida_mem, IDA_Y_INIT, tout1);
546 if (flag != IDA_SUCCESS) {
547 throw CanteraError(
"IDA_Solver::correctInitial_Y_given_Yp",
548 "IDACalcIC failed: error = {}", flag);
552 if (flag != IDA_SUCCESS) {
553 throw CanteraError(
"IDA_Solver::correctInitial_Y_given_Yp",
554 "IDAGetSolution failed: error = {}", flag);
556 for (
int i = 0; i <
m_neq; i++) {
557 y[i] = NV_Ith_S(
m_y, i);
558 yp[i] = NV_Ith_S(
m_ydot, i);
564 int icopt = IDA_YA_YDP_INIT;
565 doublereal tout1 = tout;
574 int flag = IDACalcIC(
m_ida_mem, icopt, tout1);
575 if (flag != IDA_SUCCESS) {
576 throw CanteraError(
"IDA_Solver::correctInitial_YaYp_given_Yd",
577 "IDACalcIC failed: error = {}", flag);
581 if (flag != IDA_SUCCESS) {
582 throw CanteraError(
"IDA_Solver::correctInitial_YaYp_given_Yd",
583 "IDAGetSolution failed: error = {}", flag);
585 for (
int i = 0; i <
m_neq; i++) {
586 y[i] = NV_Ith_S(
m_y, i);
587 yp[i] = NV_Ith_S(
m_ydot, i);
593 double tretn = tout - 1000;
596 if (flag != IDA_SUCCESS) {
597 throw CanteraError(
"IDA_Solver::solve",
"IDA error encountered.");
599 while (tretn < tout) {
601 throw CanteraError(
"IDA_Solver::solve",
"tout <= tcurrent");
607 throw CanteraError(
"IDA_Solver::solve",
"IDA error encountered.");
608 }
else if (flag == IDA_TSTOP_RETURN) {
610 }
else if (flag == IDA_ROOT_RETURN) {
612 }
else if (flag == IDA_WARNING) {
613 throw CanteraError(
"IDA_Solver::solve",
"IDA Warning encountered.");
619 if (flag != IDA_SUCCESS && flag != IDA_TSTOP_RETURN) {
620 throw CanteraError(
"IDA_Solver::solve",
"IDA error encountered.");
630 throw CanteraError(
"IDA_Solver::step",
"tout <= tcurrent");
636 throw CanteraError(
"IDA_Solver::step",
"IDA error encountered.");
637 }
else if (flag == IDA_TSTOP_RETURN) {
639 }
else if (flag == IDA_ROOT_RETURN) {
641 }
else if (flag == IDA_WARNING) {
642 throw CanteraError(
"IDA_Solver::step",
"IDA Warning encountered.");
651 long int lenrw, leniw;
653 case REAL_WORKSPACE_SIZE:
654 flag = IDAGetWorkSpace(
m_ida_mem, &lenrw, &leniw);
655 return doublereal(lenrw);
void * m_linsol
Sundials linear solver object.
virtual void setStopTime(doublereal tstop)
Set the stop time.
virtual int evalJacobianDP(const doublereal t, const doublereal delta_t, doublereal cj, const doublereal *const y, const doublereal *const ydot, doublereal *const *jacobianColPts, doublereal *const resid)
Calculate an analytical Jacobian and the residual at the current time and values. ...
A simple class to hold an array of parameter values and a pointer to an instance of a subclass of Res...
integer m_neq
Number of total equations in the system.
Wrapper for Sundials IDA solver.
int m_maxNonlinConvFails
Maximum number of nonlinear convergence failures.
int nparams() const
Return the number of parameters in the calculation.
virtual void setBandedLinearSolver(int m_upper, int m_lower)
Set up the problem to use a band solver.
doublereal m_told
Value of the previous time.
int m_maxsteps
Maximum number of time steps allowed.
virtual double getCurrentStepFromIDA()
Get the current step size from IDA via a call.
virtual void setInitialStepSize(doublereal h0)
Set the initial step size.
virtual void setDenseLinearSolver()
Set up the problem to use a dense linear direct solver.
virtual int evalResidNJ(const doublereal t, const doublereal delta_t, const doublereal *const y, const doublereal *const ydot, doublereal *const resid, const ResidEval_Type_Enum evalType=Base_ResidEval, const int id_x=-1, const doublereal delta_x=0.0)
Evaluate the residual function.
int m_maxErrTestFails
maximum number of error test failures
virtual void correctInitial_YaYp_given_Yd(doublereal *y, doublereal *yp, doublereal tout)
Calculate consistent value of the algebraic constraints and derivatives at the start of the problem...
doublereal m_tstop
maximum time
doublereal m_tcurrent
Value of the current time.
virtual int getInitialConditions(const doublereal t0, doublereal *const y, doublereal *const ydot)
Fill in the initial conditions.
virtual void setMaxNonlinConvFailures(int n)
Set the maximum number of nonlinear solver convergence failures.
doublereal m_told_old
Value of the previous, previous time.
static int ida_resid(realtype t, N_Vector y, N_Vector ydot, N_Vector r, void *f_data)
Function called by IDA to evaluate the residual, given y and ydot.
Wrappers for the function evaluators for Nonlinear solvers and Time steppers.
void * m_ida_mem
Pointer to the IDA memory for the problem.
virtual void setMaxNumSteps(int n)
Set the maximum number of time steps.
void * m_linsol_matrix
matrix used by Sundials
N_Vector m_y
Current value of the solution vector.
doublereal m_h0
Value of the initial time step.
virtual int solve(doublereal tout)
Step the system to a final value of the time.
virtual void correctInitial_Y_given_Yp(doublereal *y, doublereal *yp, doublereal tout)
Calculate consistent value of the starting solution given the starting solution derivatives.
doublereal m_deltat
Value of deltaT for the current step.
virtual doublereal solution(int k) const
the current value of solution component k.
Base class for exceptions thrown by Cantera classes.
virtual doublereal derivative(int k) const
the current value of the derivative of solution component k.
virtual void setMaxNonlinIterations(int n)
Set the maximum number of nonlinear iterations on a timestep.
virtual void setJacobianType(int formJac)
Set the form of the Jacobian.
virtual doublereal getOutputParameter(int flag) const
Get the value of a solver-specific output parameter.
virtual void init(doublereal t0)
initialize.
Contains declarations for string manipulation functions within Cantera.
int m_maxNonlinIters
Maximum number of nonlinear solver iterations at one solution.
doublereal m_t0
Initial value of the time.
N_Vector m_ydot
Current value of the derivative of the solution vector.
Header file for class IDA_Solver.
virtual doublereal step(doublereal tout)
Take one internal step.
Namespace for the Cantera kernel.
int m_maxord
maximum time step order of the method
virtual void setTolerances(doublereal reltol, doublereal *abstol)
Set error tolerances.
static int ida_jacobian(sd_size_t nrows, realtype t, realtype c_j, N_Vector y, N_Vector ydot, N_Vector r, DlsMat Jac, void *f_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3)
Function called by by IDA to evaluate the Jacobian, given y and ydot.
int m_formJac
Form of the Jacobian.
int m_setSuppressAlg
If true, the algebraic variables don't contribute to error tolerances.