Cantera  4.0.0a1
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: ReactorNet
24 */
26{
27public:
28 /**
29 * Constructor. Default settings: dense Jacobian, no user-supplied
30 * Jacobian function, Newton iteration.
31 */
33 ~CVodesIntegrator() 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 initialize(double t0, FuncEval& func) override;
38 void reinitialize(double t0, FuncEval& func) override;
39 void integrate(double tout) override;
40 double step(double tout) override;
41 double& solution(size_t k) override;
42 double* solution() override;
43 double* derivative(double tout, int n) override;
44 int lastOrder() const override;
45 int nEquations() const override{
46 return static_cast<int>(m_neq);
47 }
48 int nEvals() const override;
49 void setMaxOrder(int n) override {
50 m_maxord = n;
51 }
52 void setMethod(MethodType t) override;
53 void setMaxStepSize(double hmax) override;
54 void setMinStepSize(double hmin) override;
55 void setMaxSteps(int nmax) override;
56 int maxSteps() override;
57 void setMaxErrTestFails(int n) override;
58 AnyMap solverStats() const override;
59 void setLinearSolverType(const string& linSolverType) override {
60 m_type = linSolverType;
61 }
62 string linearSolverType() const override {
63 return m_type;
64 }
65 void setBandwidth(int N_Upper, int N_Lower) override {
66 m_mupper = N_Upper;
67 m_mlower = N_Lower;
68 }
69 int nSensParams() override {
70 return static_cast<int>(m_np);
71 }
72 double sensitivity(size_t k, size_t p) override;
73
74 //! Returns a string listing the weighted error estimates associated
75 //! with each solution component.
76 //! This information can be used to identify which variables are
77 //! responsible for integrator failures or unexpected small timesteps.
78 string getErrorInfo(int N);
79
80 //! Error message information provide by CVodes
82
83 double currentTime() const override {
84 return m_time;
85 }
86
87 void setRootFunctionCount(size_t nroots) override;
88
89protected:
90 //! Applies user-specified options to the underlying CVODES solver. Called
91 //! during integrator initialization or reinitialization.
92 void applyOptions();
93
94private:
95 void sensInit(double t0, FuncEval& func);
96
97 //! Check whether a CVODES method indicated an error. If so, throw an exception
98 //! containing the method name and the error code stashed by the cvodes_err() function.
99 void checkError(long flag, const string& ctMethod, const string& cvodesMethod) const;
100
101 size_t m_neq = 0;
102 void* m_cvode_mem = nullptr; //!< CVODES internal memory object
103 SundialsContext m_sundials_ctx; //!< SUNContext object for Sundials>=6.0
104 void* m_linsol = nullptr; //!< Sundials linear solver object
105 void* m_linsol_matrix = nullptr; //!< matrix used by Sundials
106 FuncEval* m_func = nullptr;
107 double m_t0 = 0.0;
108
109 //! The current system time, corresponding to #m_y
110 double m_time;
111
112 //! The latest time reached by the integrator. May be greater than #m_time.
113 double m_tInteg;
114
115 //! The system state at #m_time
116 N_Vector m_y = nullptr;
117
118 N_Vector m_abstol = nullptr;
119 N_Vector m_dky = nullptr;
120 string m_type = "DENSE";
121 int m_itol;
122 int m_method;
123 int m_maxord = 0;
124 double m_reltol = 1e-9;
125 double m_abstols = 1e-15;
126 double m_reltolsens = 1e-5;
127 double m_abstolsens = 1e-4;
128 size_t m_nabs = 0;
129 double m_hmax = 0.0;
130 double m_hmin = 0.0;
131 int m_maxsteps = 20000;
132 int m_maxErrTestFails = 0;
133 N_Vector* m_yS = nullptr;
134 size_t m_np = 0;
135 int m_mupper = 0;
136 int m_mlower = 0;
137 //! Indicates whether the sensitivities stored in m_yS have been updated
138 //! for at the current integrator time.
139 bool m_sens_ok = false;
140 size_t m_nRootFunctions = 0;
141};
142
143} // namespace
144
145#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.
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