Cantera  3.2.0
Loading...
Searching...
No Matches
CVodesIntegrator.h
Go to the documentation of this file.
1/**
2 * @file CVodesIntegrator.h
3 */
4
5// This file is part of Cantera. See License.txt in the top-level directory or
6// at https://cantera.org/license.txt for license and copyright information.
7
8#ifndef CT_CVODESWRAPPER_H
9#define CT_CVODESWRAPPER_H
10
14
15#include "sundials/sundials_nvector.h"
16
17namespace Cantera
18{
19
20/**
21 * Wrapper class for 'cvodes' integrator from LLNL.
22 *
23 * See FuncEval.h. Classes that use CVodesIntegrator:
24 * ImplicitSurfChem, ReactorNet
25 */
27{
28public:
29 /**
30 * Constructor. Default settings: dense Jacobian, no user-supplied
31 * Jacobian function, Newton iteration.
32 */
34 ~CVodesIntegrator() override;
35 void setTolerances(double reltol, size_t n, double* abstol) override;
36 void setTolerances(double reltol, double abstol) override;
37 void setSensitivityTolerances(double reltol, double abstol) 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 double* derivative(double tout, int n) override;
45 int lastOrder() const override;
46 int nEquations() const override{
47 return static_cast<int>(m_neq);
48 }
49 int nEvals() const override;
50 void setMaxOrder(int n) override {
51 m_maxord = n;
52 }
53 void setMethod(MethodType t) override;
54 void setMaxStepSize(double hmax) override;
55 void setMinStepSize(double hmin) override;
56 void setMaxSteps(int nmax) override;
57 int maxSteps() override;
58 void setMaxErrTestFails(int n) override;
59 AnyMap solverStats() const override;
60 void setLinearSolverType(const string& linSolverType) override {
61 m_type = linSolverType;
62 }
63 string linearSolverType() const override {
64 return m_type;
65 }
66 void setBandwidth(int N_Upper, int N_Lower) override {
67 m_mupper = N_Upper;
68 m_mlower = N_Lower;
69 }
70 int nSensParams() override {
71 return static_cast<int>(m_np);
72 }
73 double sensitivity(size_t k, size_t p) override;
74
75 //! Returns a string listing the weighted error estimates associated
76 //! with each solution component.
77 //! This information can be used to identify which variables are
78 //! responsible for integrator failures or unexpected small timesteps.
79 string getErrorInfo(int N);
80
81 //! Error message information provide by CVodes
83
84 double currentTime() const override {
85 return m_time;
86 }
87
88 void setRootFunctionCount(size_t nroots) override;
89
90protected:
91 //! Applies user-specified options to the underlying CVODES solver. Called
92 //! during integrator initialization or reinitialization.
93 void applyOptions();
94
95private:
96 void sensInit(double t0, FuncEval& func);
97
98 //! Check whether a CVODES method indicated an error. If so, throw an exception
99 //! containing the method name and the error code stashed by the cvodes_err() function.
100 void checkError(long flag, const string& ctMethod, const string& cvodesMethod) const;
101
102 size_t m_neq = 0;
103 void* m_cvode_mem = nullptr; //!< CVODES internal memory object
104 SundialsContext m_sundials_ctx; //!< SUNContext object for Sundials>=6.0
105 void* m_linsol = nullptr; //!< Sundials linear solver object
106 void* m_linsol_matrix = nullptr; //!< matrix used by Sundials
107 FuncEval* m_func = nullptr;
108 double m_t0 = 0.0;
109
110 //! The current system time, corresponding to #m_y
111 double m_time;
112
113 //! The latest time reached by the integrator. May be greater than #m_time.
114 double m_tInteg;
115
116 //! The system state at #m_time
117 N_Vector m_y = nullptr;
118
119 N_Vector m_abstol = nullptr;
120 N_Vector m_dky = nullptr;
121 string m_type = "DENSE";
122 int m_itol;
123 int m_method;
124 int m_maxord = 0;
125 double m_reltol = 1e-9;
126 double m_abstols = 1e-15;
127 double m_reltolsens = 1e-5;
128 double m_abstolsens = 1e-4;
129 size_t m_nabs = 0;
130 double m_hmax = 0.0;
131 double m_hmin = 0.0;
132 int m_maxsteps = 20000;
133 int m_maxErrTestFails = 0;
134 N_Vector* m_yS = nullptr;
135 size_t m_np = 0;
136 int m_mupper = 0;
137 int m_mlower = 0;
138 //! Indicates whether the sensitivities stored in m_yS have been updated
139 //! for at the current integrator time.
140 bool m_sens_ok = false;
141 size_t m_nRootFunctions = 0;
142};
143
144} // namespace
145
146#endif
A map of string keys to values whose type can vary at runtime.
Definition AnyMap.h:431
Wrapper class for 'cvodes' integrator from LLNL.
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.
N_Vector m_y
The system state at m_time.
void * m_cvode_mem
CVODES internal memory object.
void setRootFunctionCount(size_t nroots) override
Configure how many event/root functions the integrator should monitor.
void * m_linsol
Sundials linear solver object.
int nEvals() const override
The number of function evaluations.
int nEquations() const override
The number of equations.
void setMaxOrder(int n) override
Set the maximum integration order that will be used.
double m_time
The current system time, corresponding to m_y.
bool m_sens_ok
Indicates whether the sensitivities stored in m_yS have been updated for at the current integrator ti...
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.
void setLinearSolverType(const string &linSolverType) override
Set the linear solver type.
void checkError(long flag, const string &ctMethod, const string &cvodesMethod) const
Check whether a CVODES method indicated an error.
void setSensitivityTolerances(double reltol, double abstol) override
Set the sensitivity error tolerances.
void applyOptions()
Applies user-specified options to the underlying CVODES solver.
int lastOrder() const override
Order used during the last solution step.
void setMinStepSize(double hmin) override
Set the minimum step size.
void integrate(double tout) override
Integrate the system of equations.
void setTolerances(double reltol, size_t n, double *abstol) override
Set error tolerances.
double currentTime() const override
Current value of the independent variable tracked by the integrator.
double * derivative(double tout, int n) override
n-th derivative of the output function at time tout.
string m_error_message
Error message information provide by CVodes.
void setMaxErrTestFails(int n) override
Set the maximum permissible number of error test failures.
string linearSolverType() const override
Return the integrator problem type.
void setMaxSteps(int nmax) override
Set the maximum number of time-steps the integrator can take before reaching the next output time.
AnyMap solverStats() const override
Get solver stats from integrator.
void * m_linsol_matrix
matrix used by Sundials
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.
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.
Virtual base class for ODE/DAE right-hand-side function evaluators.
Definition FuncEval.h:32
Abstract base class for ODE system integrators.
Definition Integrator.h:44
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:595
MethodType
Specifies the method used to integrate the system of equations.
Definition Integrator.h:23