Cantera  3.1.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:
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
84protected:
85 //! Applies user-specified options to the underlying CVODES solver. Called
86 //! during integrator initialization or reinitialization.
87 void applyOptions();
88
89private:
90 void sensInit(double t0, FuncEval& func);
91
92 //! Check whether a CVODES method indicated an error. If so, throw an exception
93 //! containing the method name and the error code stashed by the cvodes_err() function.
94 void checkError(long flag, const string& ctMethod, const string& cvodesMethod) const;
95
96 size_t m_neq = 0;
97 void* m_cvode_mem = nullptr; //!< CVODES internal memory object
98 SundialsContext m_sundials_ctx; //!< SUNContext object for Sundials>=6.0
99 void* m_linsol = nullptr; //!< Sundials linear solver object
100 void* m_linsol_matrix = nullptr; //!< matrix used by Sundials
101 FuncEval* m_func = nullptr;
102 double m_t0 = 0.0;
103
104 //! The current system time, corresponding to #m_y
105 double m_time;
106
107 //! The latest time reached by the integrator. May be greater than #m_time.
108 double m_tInteg;
109
110 //! The system state at #m_time
111 N_Vector m_y = nullptr;
112
113 N_Vector m_abstol = nullptr;
114 N_Vector m_dky = nullptr;
115 string m_type = "DENSE";
116 int m_itol;
117 int m_method;
118 int m_maxord = 0;
119 double m_reltol = 1e-9;
120 double m_abstols = 1e-15;
121 double m_reltolsens = 1e-5;
122 double m_abstolsens = 1e-4;
123 size_t m_nabs = 0;
124 double m_hmax = 0.0;
125 double m_hmin = 0.0;
126 int m_maxsteps = 20000;
127 int m_maxErrTestFails = 0;
128 N_Vector* m_yS = nullptr;
129 size_t m_np = 0;
130 int m_mupper = 0;
131 int m_mlower = 0;
132 //! Indicates whether the sensitivities stored in m_yS have been updated
133 //! for at the current integrator time.
134 bool m_sens_ok = false;
135};
136
137} // namespace
138
139#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.
N_Vector m_y
The system state at m_time.
void * m_cvode_mem
CVODES internal memory object.
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: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:564
MethodType
Specifies the method used to integrate the system of equations.
Definition Integrator.h:23