| Cantera 3.0.0

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
12#include "cantera/numerics/Integrator.h"
13#include "cantera/base/ctexceptions.h"
14#include "sundials/sundials_nvector.h"
15#include "cantera/numerics/SundialsContext.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 */
32 IdasIntegrator();
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
68 string m_error_message;
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
139 int m_maxErrTestFails = -1;
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 */
154 int m_maxNonlinIters = 0;
155
156 //! Maximum number of nonlinear convergence failures
157 int m_maxNonlinConvFails = -1;
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
Integrator.h
SundialsContext.h
Cantera::FuncEval
Virtual base class for ODE/DAE right-hand-side function evaluators.
Definition FuncEval.h:32
Cantera::IdasIntegrator
Wrapper for Sundials IDAS solver.
Definition IdasIntegrator.h:26
Cantera::IdasIntegrator::step
double step(double tout) override
Integrate the system of equations.
Definition IdasIntegrator.cpp:489
Cantera::IdasIntegrator::setMaxStepSize
void setMaxStepSize(double hmax) override
Set the maximum step size.
Definition IdasIntegrator.cpp:144
Cantera::IdasIntegrator::solution
double * solution() override
The current value of the solution of the system of equations.
Definition IdasIntegrator.cpp:95
Cantera::IdasIntegrator::m_t0
double m_t0
The start time for the integrator.
Definition IdasIntegrator.h:109
Cantera::IdasIntegrator::m_y
N_Vector m_y
The current system state.
Definition IdasIntegrator.h:115
Cantera::IdasIntegrator::m_linsol
void * m_linsol
Sundials linear solver object.
Definition IdasIntegrator.h:102
Cantera::IdasIntegrator::m_maxNonlinConvFails
int m_maxNonlinConvFails
Maximum number of nonlinear convergence failures.
Definition IdasIntegrator.h:157
Cantera::IdasIntegrator::m_init_step
double m_init_step
Initial IDA step size.
Definition IdasIntegrator.h:163
Cantera::IdasIntegrator::checkError
void checkError(long flag, const string &ctMethod, const string &idaMethod) const
Check whether an IDAS method indicated an error.
Definition IdasIntegrator.cpp:555
Cantera::IdasIntegrator::nEquations
int nEquations() const override
The number of equations.
Definition IdasIntegrator.h:44
Cantera::IdasIntegrator::m_abstolsens
double m_abstolsens
Scalar absolute tolerance for sensitivities.
Definition IdasIntegrator.h:128
Cantera::IdasIntegrator::m_nabs
size_t m_nabs
! Number of variables for which absolute tolerances were provided
Definition IdasIntegrator.h:131
Cantera::IdasIntegrator::setMaxOrder
void setMaxOrder(int n) override
Set the maximum integration order that will be used.
Definition IdasIntegrator.cpp:135
Cantera::IdasIntegrator::m_time
double m_time
The current integrator time, corresponding to m_y.
Definition IdasIntegrator.h:110
Cantera::IdasIntegrator::m_maxNonlinIters
int m_maxNonlinIters
Maximum number of nonlinear solver iterations at one solution.
Definition IdasIntegrator.h:154
Cantera::IdasIntegrator::m_sens_ok
bool m_sens_ok
Indicates whether the sensitivities stored in m_yS and m_ySdot have been updated for the current inte...
Definition IdasIntegrator.h:148
Cantera::IdasIntegrator::maxSteps
int maxSteps() override
Returns the maximum number of time-steps the integrator can take before reaching the next output time...
Definition IdasIntegrator.cpp:161
Cantera::IdasIntegrator::m_sundials_ctx
SundialsContext m_sundials_ctx
SUNContext object for Sundials>=6.0.
Definition IdasIntegrator.h:104
Cantera::IdasIntegrator::m_func
FuncEval * m_func
Object implementing the DAE residual function .
Definition IdasIntegrator.h:107
Cantera::IdasIntegrator::m_abstols
double m_abstols
Scalar absolute tolerance.
Definition IdasIntegrator.h:126
Cantera::IdasIntegrator::m_hmax
double m_hmax
Maximum time step size. Zero means infinity.
Definition IdasIntegrator.h:133
Cantera::IdasIntegrator::m_itol
int m_itol
Flag indicating whether scalar (IDA_SS) or vector (IDA_SV) absolute tolerances are being used.
Definition IdasIntegrator.h:122
Cantera::IdasIntegrator::m_maxErrTestFails
int m_maxErrTestFails
Maximum number of error test failures in attempting one step.
Definition IdasIntegrator.h:139
Cantera::IdasIntegrator::m_maxord
int m_maxord
Maximum order allowed for the BDF method.
Definition IdasIntegrator.h:124
Cantera::IdasIntegrator::setSensitivityTolerances
void setSensitivityTolerances(double reltol, double abstol) override
Set the sensitivity error tolerances.
Definition IdasIntegrator.cpp:123
Cantera::IdasIntegrator::applyOptions
void applyOptions()
Applies user-specified options to the underlying IDAS solver.
Definition IdasIntegrator.cpp:331
Cantera::IdasIntegrator::integrate
void integrate(double tout) override
Integrate the system of equations.
Definition IdasIntegrator.cpp:452
Cantera::IdasIntegrator::setTolerances
void setTolerances(double reltol, size_t n, double *abstol) override
Set error tolerances.
Definition IdasIntegrator.cpp:100
Cantera::IdasIntegrator::setLinearSolverType
void setLinearSolverType(const string &linearSolverType) override
Set the linear solver type.
Definition IdasIntegrator.cpp:130
Cantera::IdasIntegrator::m_reltol
double m_reltol
Relative tolerance for all state variables.
Definition IdasIntegrator.h:125
Cantera::IdasIntegrator::m_error_message
string m_error_message
Error message information provide by IDAS.
Definition IdasIntegrator.h:68
Cantera::IdasIntegrator::m_reltolsens
double m_reltolsens
Scalar relative tolerance for sensitivities.
Definition IdasIntegrator.h:127
Cantera::IdasIntegrator::m_yS
N_Vector * m_yS
Sensitivities of y, size m_np by m_neq.
Definition IdasIntegrator.h:142
Cantera::IdasIntegrator::setMaxErrTestFails
void setMaxErrTestFails(int n) override
Set the maximum permissible number of error test failures.
Definition IdasIntegrator.cpp:166
Cantera::IdasIntegrator::setMaxSteps
void setMaxSteps(int nmax) override
Set the maximum number of time-steps the integrator can take before reaching the next output time.
Definition IdasIntegrator.cpp:153
Cantera::IdasIntegrator::m_neq
size_t m_neq
Number of equations/variables in the system.
Definition IdasIntegrator.h:100
Cantera::IdasIntegrator::solverStats
AnyMap solverStats() const override
Get solver stats from integrator.
Definition IdasIntegrator.cpp:174
Cantera::IdasIntegrator::m_type
string m_type
The linear solver type.
Definition IdasIntegrator.h:118
Cantera::IdasIntegrator::m_np
size_t m_np
Number of sensitivity parameters.
Definition IdasIntegrator.h:141
Cantera::IdasIntegrator::m_linsol_matrix
void * m_linsol_matrix
matrix used by Sundials
Definition IdasIntegrator.h:103
Cantera::IdasIntegrator::m_ydot
N_Vector m_ydot
The time derivatives of the system state.
Definition IdasIntegrator.h:116
Cantera::IdasIntegrator::m_ySdot
N_Vector * m_ySdot
Sensitivities of ydot, size m_np by m_neq.
Definition IdasIntegrator.h:143
Cantera::IdasIntegrator::m_tInteg
double m_tInteg
The latest time reached by the integrator. May be greater than m_time.
Definition IdasIntegrator.h:113
Cantera::IdasIntegrator::initialize
void initialize(double t0, FuncEval &func) override
Initialize the integrator for a new problem.
Definition IdasIntegrator.cpp:225
Cantera::IdasIntegrator::m_maxsteps
int m_maxsteps
Maximum number of internal steps taken in a call to integrate()
Definition IdasIntegrator.h:136
Cantera::IdasIntegrator::getErrorInfo
string getErrorInfo(int N)
Returns a string listing the weighted error estimates associated with each solution component.
Definition IdasIntegrator.cpp:531
Cantera::IdasIntegrator::IdasIntegrator
IdasIntegrator()
Constructor.
Definition IdasIntegrator.cpp:66
Cantera::IdasIntegrator::setMethod
void setMethod(MethodType t) override
Set the solution method.
Definition IdasIntegrator.cpp:571
Cantera::IdasIntegrator::m_ida_mem
void * m_ida_mem
Pointer to the IDA memory for the problem.
Definition IdasIntegrator.h:101
Cantera::IdasIntegrator::m_setSuppressAlg
bool m_setSuppressAlg
If true, the algebraic variables don't contribute to error tolerances.
Definition IdasIntegrator.h:160
Cantera::IdasIntegrator::m_abstol
N_Vector m_abstol
Absolute tolerances for each state variable.
Definition IdasIntegrator.h:117
Cantera::Integrator
Abstract base class for ODE system integrators.
Definition Integrator.h:51
Cantera::Integrator::linearSolverType
virtual string linearSolverType() const
Return the integrator problem type.
Definition Integrator.h:152
Cantera::SundialsContext
A wrapper for managing a SUNContext object, need for Sundials >= 6.0.
Definition SundialsContext.h:20
ctexceptions.h
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
Cantera
Namespace for the Cantera kernel.
Definition AnyMap.cpp:564
Cantera::MethodType
MethodType
Specifies the method used to integrate the system of equations.
Definition Integrator.h:30