Cantera  4.0.0a1
Loading...
Searching...
No Matches
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
17namespace Cantera
18{
19
20/**
21 * Wrapper for Sundials IDAS solver
22 * @see FuncEval.h. Classes that use IdasIntegrator:
23 * FlowReactor
24 */
26{
27public:
28 /**
29 * Constructor. Default settings: dense Jacobian, no user-supplied
30 * Jacobian function, Newton iteration.
31 */
33 ~IdasIntegrator() override;
34 void setTolerances(double reltol, span<const 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 span<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 {
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 double currentTime() const override {
86 return m_time;
87 }
88
89 void setMethod(MethodType t) override;
90
91protected:
92 protected:
93 //! Applies user-specified options to the underlying IDAS solver. Called
94 //! during integrator initialization or reinitialization.
95 void applyOptions();
96
97private:
98 void sensInit(double t0, FuncEval& func);
99
100 //! Check whether an IDAS method indicated an error. If so, throw an exception
101 //! containing the method name and the error code stashed by the ida_err() function.
102 void checkError(long flag, const string& ctMethod, const string& idaMethod) const;
103
104 size_t m_neq; //!< Number of equations/variables in the system
105 void* m_ida_mem = nullptr; //!< Pointer to the IDA memory for the problem
106 void* m_linsol = nullptr; //!< Sundials linear solver object
107 void* m_linsol_matrix = nullptr; //!< matrix used by Sundials
108 SundialsContext m_sundials_ctx; //!< SUNContext object for Sundials>=6.0
109
110 //! Object implementing the DAE residual function @f$ f(t, y, \dot{y}) = 0 @f$
111 FuncEval* m_func = nullptr;
112
113 double m_t0 = 0.0; //!< The start time for the integrator
114 double m_time; //!< The current integrator time, corresponding to #m_y
115
116 //! The latest time reached by the integrator. May be greater than #m_time
117 double m_tInteg;
118
119 N_Vector m_y = nullptr; //!< The current system state
120 N_Vector m_ydot = nullptr; //!< The time derivatives of the system state
121 N_Vector m_abstol = nullptr; //!< Absolute tolerances for each state variable
122 string m_type = "DENSE"; //!< The linear solver type. @see setLinearSolverType()
123
124 //! Flag indicating whether scalar (`IDA_SS`) or vector (`IDA_SV`) absolute
125 //! tolerances are being used.
127
128 int m_maxord = 0; //!< Maximum order allowed for the BDF method
129 double m_reltol = 1.0e-9; //!< Relative tolerance for all state variables
130 double m_abstols = 1.0e-15; //!< Scalar absolute tolerance
131 double m_reltolsens; //!< Scalar relative tolerance for sensitivities
132 double m_abstolsens; //!< Scalar absolute tolerance for sensitivities
133
134 //!! Number of variables for which absolute tolerances were provided
135 size_t m_nabs = 0;
136
137 double m_hmax = 0.0; //!< Maximum time step size. Zero means infinity.
138
139 //! Maximum number of internal steps taken in a call to integrate()
140 int m_maxsteps = 20000;
141
142 //! Maximum number of error test failures in attempting one step
144
145 size_t m_np; //!< Number of sensitivity parameters
146 N_Vector* m_yS = nullptr; //!< Sensitivities of y, size #m_np by #m_neq.
147 N_Vector* m_ySdot = nullptr; //!< Sensitivities of ydot, size #m_np by #m_neq.
148 N_Vector m_constraints = nullptr; //!<
149
150 //! Indicates whether the sensitivities stored in #m_yS and #m_ySdot have been
151 //! updated for the current integrator time.
153
154 //! Maximum number of nonlinear solver iterations at one solution
155 /*!
156 * If zero, this is the default of 4.
157 */
159
160 //! Maximum number of nonlinear convergence failures
162
163 //! If true, the algebraic variables don't contribute to error tolerances
164 bool m_setSuppressAlg = false;
165
166 //! Initial IDA step size
167 double m_init_step = 1e-14;
168};
169
170}
171
172#endif
Virtual base class for ODE/DAE right-hand-side function evaluators.
Definition FuncEval.h:32
Wrapper for Sundials IDAS solver.
span< double > solution() override
The current value of the solution of the system of equations.
double step(double tout) override
Integrate the system of equations.
void setMaxStepSize(double hmax) override
Set the maximum step size.
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.
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 setLinearSolverType(const string &linearSolverType) override
Set the linear solver type.
double currentTime() const override
Current value of the independent variable tracked by the integrator.
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 setTolerances(double reltol, span< const double > abstol) override
Set error tolerances.
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.
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:139
A wrapper for managing a SUNContext object.
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595
MethodType
Specifies the method used to integrate the system of equations.
Definition Integrator.h:23