Cantera  3.0.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 void setProblemType(int probtype) override;
75
76 //! Returns a string listing the weighted error estimates associated
77 //! with each solution component.
78 //! This information can be used to identify which variables are
79 //! responsible for integrator failures or unexpected small timesteps.
80 string getErrorInfo(int N);
81
82 //! Error message information provide by CVodes
84
85protected:
86 //! Applies user-specified options to the underlying CVODES solver. Called
87 //! during integrator initialization or reinitialization.
88 void applyOptions();
89
90private:
91 void sensInit(double t0, FuncEval& func);
92
93 //! Check whether a CVODES method indicated an error. If so, throw an exception
94 //! containing the method name and the error code stashed by the cvodes_err() function.
95 void checkError(long flag, const string& ctMethod, const string& cvodesMethod) const;
96
97 size_t m_neq = 0;
98 void* m_cvode_mem = nullptr;
99 SundialsContext m_sundials_ctx; //!< SUNContext object for Sundials>=6.0
100 void* m_linsol = nullptr; //!< Sundials linear solver object
101 void* m_linsol_matrix = nullptr; //!< matrix used by Sundials
102 FuncEval* m_func = nullptr;
103 double m_t0 = 0.0;
104
105 //! The current system time, corresponding to #m_y
106 double m_time;
107
108 //! The latest time reached by the integrator. May be greater than #m_time.
109 double m_tInteg;
110
111 //! The system state at #m_time
112 N_Vector m_y = nullptr;
113
114 N_Vector m_abstol = nullptr;
115 N_Vector m_dky = nullptr;
116 string m_type = "DENSE";
117 int m_itol;
118 int m_method;
119 int m_maxord = 0;
120 double m_reltol = 1e-9;
121 double m_abstols = 1e-15;
122 double m_reltolsens = 1e-5;
123 double m_abstolsens = 1e-4;
124 size_t m_nabs = 0;
125 double m_hmax = 0.0;
126 double m_hmin = 0.0;
127 int m_maxsteps = 20000;
128 int m_maxErrTestFails = 0;
129 N_Vector* m_yS = nullptr;
130 size_t m_np = 0;
131 int m_mupper = 0;
132 int m_mlower = 0;
133 //! Indicates whether the sensitivities stored in m_yS have been updated
134 //! for at the current integrator time.
135 bool m_sens_ok = false;
136};
137
138} // namespace
139
140#endif
A map of string keys to values whose type can vary at runtime.
Definition AnyMap.h:427
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.
void setProblemType(int probtype) override
Set the problem type.
N_Vector m_y
The system state at m_time.
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 * 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:51
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:30