Cantera  3.1.0a1
IdasIntegrator.h
Go to the documentation of this file.
1 /**
2  * @file IdasIntegrator.h
3  * Header file for class IdasIntegrator
4  */
5 
6 // This file is part of Cantera. See License.txt in the top-level directory or
7 // at https://www.cantera.org/license.txt for license and copyright information.
8 
9 #ifndef CT_IDASINTEGRATOR_H
10 #define CT_IDASINTEGRATOR_H
11 
14 #include "sundials/sundials_nvector.h"
16 
17 namespace Cantera
18 {
19 
20 /**
21  * Wrapper for Sundials IDAS solver
22  * @see FuncEval.h. Classes that use IdasIntegrator:
23  * FlowReactor
24  */
25 class IdasIntegrator : public Integrator
26 {
27 public:
28  /**
29  * Constructor. Default settings: dense Jacobian, no user-supplied
30  * Jacobian function, Newton iteration.
31  */
33  ~IdasIntegrator() override;
34  void setTolerances(double reltol, size_t n, double* abstol) override;
35  void setTolerances(double reltol, double abstol) override;
36  void setSensitivityTolerances(double reltol, double abstol) override;
37  void setLinearSolverType(const string& linearSolverType) override;
38  void initialize(double t0, FuncEval& func) override;
39  void reinitialize(double t0, FuncEval& func) override;
40  void integrate(double tout) override;
41  double step(double tout) override;
42  double& solution(size_t k) override;
43  double* solution() override;
44  int nEquations() const override {
45  return static_cast<int>(m_neq);
46  }
47  int maxOrder() const override {
48  return m_maxord;
49  }
50  void setMaxOrder(int n) override;
51  void setMaxStepSize(double hmax) override;
52  void setMaxSteps(int nmax) override;
53  int maxSteps() override;
54  void setMaxErrTestFails(int n) override;
55  AnyMap solverStats() const override;
56  int nSensParams() override {
57  return static_cast<int>(m_np);
58  }
59  double sensitivity(size_t k, size_t p) override;
60 
61  //! Returns a string listing the weighted error estimates associated
62  //! with each solution component.
63  //! This information can be used to identify which variables are
64  //! responsible for integrator failures or unexpected small timesteps.
65  string getErrorInfo(int N);
66 
67  //! Error message information provide by IDAS
69 
70  int maxNonlinIterations() const override {
71  return m_maxNonlinIters;
72  }
73  void setMaxNonlinIterations(int n) override;
74 
75  int maxNonlinConvFailures() const override {
76  return m_maxNonlinConvFails;
77  }
78  void setMaxNonlinConvFailures(int n) override;
79 
80  bool algebraicInErrorTest() const override {
81  return !m_setSuppressAlg;
82  }
83  void includeAlgebraicInErrorTest(bool yesno) override;
84 
85  void setMethod(MethodType t) override;
86 
87 protected:
88  protected:
89  //! Applies user-specified options to the underlying IDAS solver. Called
90  //! during integrator initialization or reinitialization.
91  void applyOptions();
92 
93 private:
94  void sensInit(double t0, FuncEval& func);
95 
96  //! Check whether an IDAS method indicated an error. If so, throw an exception
97  //! containing the method name and the error code stashed by the ida_err() function.
98  void checkError(long flag, const string& ctMethod, const string& idaMethod) const;
99 
100  size_t m_neq; //!< Number of equations/variables in the system
101  void* m_ida_mem = nullptr; //!< Pointer to the IDA memory for the problem
102  void* m_linsol = nullptr; //!< Sundials linear solver object
103  void* m_linsol_matrix = nullptr; //!< matrix used by Sundials
104  SundialsContext m_sundials_ctx; //!< SUNContext object for Sundials>=6.0
105 
106  //! Object implementing the DAE residual function @f$ f(t, y, \dot{y}) = 0 @f$
107  FuncEval* m_func = nullptr;
108 
109  double m_t0 = 0.0; //!< The start time for the integrator
110  double m_time; //!< The current integrator time, corresponding to #m_y
111 
112  //! The latest time reached by the integrator. May be greater than #m_time
113  double m_tInteg;
114 
115  N_Vector m_y = nullptr; //!< The current system state
116  N_Vector m_ydot = nullptr; //!< The time derivatives of the system state
117  N_Vector m_abstol = nullptr; //!< Absolute tolerances for each state variable
118  string m_type = "DENSE"; //!< The linear solver type. @see setLinearSolverType()
119 
120  //! Flag indicating whether scalar (`IDA_SS`) or vector (`IDA_SV`) absolute
121  //! tolerances are being used.
122  int m_itol;
123 
124  int m_maxord = 0; //!< Maximum order allowed for the BDF method
125  double m_reltol = 1.0e-9; //!< Relative tolerance for all state variables
126  double m_abstols = 1.0e-15; //!< Scalar absolute tolerance
127  double m_reltolsens; //!< Scalar relative tolerance for sensitivities
128  double m_abstolsens; //!< Scalar absolute tolerance for sensitivities
129 
130  //!! Number of variables for which absolute tolerances were provided
131  size_t m_nabs = 0;
132 
133  double m_hmax = 0.0; //!< Maximum time step size. Zero means infinity.
134 
135  //! Maximum number of internal steps taken in a call to integrate()
136  int m_maxsteps = 20000;
137 
138  //! Maximum number of error test failures in attempting one step
140 
141  size_t m_np; //!< Number of sensitivity parameters
142  N_Vector* m_yS = nullptr; //!< Sensitivities of y, size #m_np by #m_neq.
143  N_Vector* m_ySdot = nullptr; //!< Sensitivities of ydot, size #m_np by #m_neq.
144  N_Vector m_constraints = nullptr; //!<
145 
146  //! Indicates whether the sensitivities stored in #m_yS and #m_ySdot have been
147  //! updated for the current integrator time.
148  bool m_sens_ok;
149 
150  //! Maximum number of nonlinear solver iterations at one solution
151  /*!
152  * If zero, this is the default of 4.
153  */
155 
156  //! Maximum number of nonlinear convergence failures
158 
159  //! If true, the algebraic variables don't contribute to error tolerances
160  bool m_setSuppressAlg = false;
161 
162  //! Initial IDA step size
163  double m_init_step = 1e-14;
164 };
165 
166 }
167 
168 #endif
Virtual base class for ODE/DAE right-hand-side function evaluators.
Definition: FuncEval.h:32
Wrapper for Sundials IDAS solver.
double step(double tout) override
Integrate the system of equations.
void setMaxStepSize(double hmax) override
Set the maximum step size.
double * solution() override
The current value of the solution of the system of equations.
double m_t0
The start time for the integrator.
N_Vector m_y
The current system state.
void * m_linsol
Sundials linear solver object.
int m_maxNonlinConvFails
Maximum number of nonlinear convergence failures.
double m_init_step
Initial IDA step size.
void checkError(long flag, const string &ctMethod, const string &idaMethod) const
Check whether an IDAS method indicated an error.
int nEquations() const override
The number of equations.
double m_abstolsens
Scalar absolute tolerance for sensitivities.
size_t m_nabs
! Number of variables for which absolute tolerances were provided
void setMaxOrder(int n) override
Set the maximum integration order that will be used.
double m_time
The current integrator time, corresponding to m_y.
int m_maxNonlinIters
Maximum number of nonlinear solver iterations at one solution.
bool m_sens_ok
Indicates whether the sensitivities stored in m_yS and m_ySdot have been updated for the current inte...
int maxSteps() override
Returns the maximum number of time-steps the integrator can take before reaching the next output time...
SundialsContext m_sundials_ctx
SUNContext object for Sundials>=6.0.
FuncEval * m_func
Object implementing the DAE residual function .
double m_abstols
Scalar absolute tolerance.
double m_hmax
Maximum time step size. Zero means infinity.
int m_itol
Flag indicating whether scalar (IDA_SS) or vector (IDA_SV) absolute tolerances are being used.
int m_maxErrTestFails
Maximum number of error test failures in attempting one step.
int m_maxord
Maximum order allowed for the BDF method.
void setSensitivityTolerances(double reltol, double abstol) override
Set the sensitivity error tolerances.
void applyOptions()
Applies user-specified options to the underlying IDAS solver.
void integrate(double tout) override
Integrate the system of equations.
void setTolerances(double reltol, size_t n, double *abstol) override
Set error tolerances.
void setLinearSolverType(const string &linearSolverType) override
Set the linear solver type.
double m_reltol
Relative tolerance for all state variables.
string m_error_message
Error message information provide by IDAS.
double m_reltolsens
Scalar relative tolerance for sensitivities.
N_Vector * m_yS
Sensitivities of y, size m_np by m_neq.
void setMaxErrTestFails(int n) override
Set the maximum permissible number of error test failures.
void setMaxSteps(int nmax) override
Set the maximum number of time-steps the integrator can take before reaching the next output time.
size_t m_neq
Number of equations/variables in the system.
AnyMap solverStats() const override
Get solver stats from integrator.
string m_type
The linear solver type.
size_t m_np
Number of sensitivity parameters.
void * m_linsol_matrix
matrix used by Sundials
N_Vector m_ydot
The time derivatives of the system state.
N_Vector * m_ySdot
Sensitivities of ydot, size m_np by m_neq.
double m_tInteg
The latest time reached by the integrator. May be greater than m_time.
void initialize(double t0, FuncEval &func) override
Initialize the integrator for a new problem.
int m_maxsteps
Maximum number of internal steps taken in a call to integrate()
string getErrorInfo(int N)
Returns a string listing the weighted error estimates associated with each solution component.
IdasIntegrator()
Constructor.
void setMethod(MethodType t) override
Set the solution method.
void * m_ida_mem
Pointer to the IDA memory for the problem.
bool m_setSuppressAlg
If true, the algebraic variables don't contribute to error tolerances.
N_Vector m_abstol
Absolute tolerances for each state variable.
Abstract base class for ODE system integrators.
Definition: Integrator.h:44
virtual string linearSolverType() const
Return the integrator problem type.
Definition: Integrator.h:131
A wrapper for managing a SUNContext object, need for Sundials >= 6.0.
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:564
MethodType
Specifies the method used to integrate the system of equations.
Definition: Integrator.h:23