| 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
11#include "cantera/numerics/Integrator.h"
12#include "cantera/numerics/SundialsContext.h"
13#include "cantera/base/ctexceptions.h"
14
15#include "sundials/sundials_nvector.h"
16
17 namespace 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 */
26 class CVodesIntegrator : public Integrator
27{
28 public:
29 /**
30 * Constructor. Default settings: dense Jacobian, no user-supplied
31 * Jacobian function, Newton iteration.
32 */
33 CVodesIntegrator();
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
83 string m_error_message;
84
85 protected:
86 //! Applies user-specified options to the underlying CVODES solver. Called
87 //! during integrator initialization or reinitialization.
88 void applyOptions();
89
90 private:
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
Integrator.h
SundialsContext.h
Cantera::AnyMap
A map of string keys to values whose type can vary at runtime.
Definition AnyMap.h:427
Cantera::CVodesIntegrator
Wrapper class for 'cvodes' integrator from LLNL.
Definition CVodesIntegrator.h:27
Cantera::CVodesIntegrator::step
double step(double tout) override
Integrate the system of equations.
Definition CVodesIntegrator.cpp:533
Cantera::CVodesIntegrator::setMaxStepSize
void setMaxStepSize(double hmax) override
Set the maximum step size.
Definition CVodesIntegrator.cpp:184
Cantera::CVodesIntegrator::solution
double * solution() override
The current value of the solution of the system of equations.
Definition CVodesIntegrator.cpp:121
Cantera::CVodesIntegrator::setProblemType
void setProblemType(int probtype) override
Set the problem type.
Definition CVodesIntegrator.cpp:155
Cantera::CVodesIntegrator::m_y
N_Vector m_y
The system state at m_time.
Definition CVodesIntegrator.h:112
Cantera::CVodesIntegrator::m_linsol
void * m_linsol
Sundials linear solver object.
Definition CVodesIntegrator.h:100
Cantera::CVodesIntegrator::nEvals
int nEvals() const override
The number of function evaluations.
Definition CVodesIntegrator.cpp:567
Cantera::CVodesIntegrator::nEquations
int nEquations() const override
The number of equations.
Definition CVodesIntegrator.h:46
Cantera::CVodesIntegrator::CVodesIntegrator
CVodesIntegrator()
Constructor.
Definition CVodesIntegrator.cpp:79
Cantera::CVodesIntegrator::setMaxOrder
void setMaxOrder(int n) override
Set the maximum integration order that will be used.
Definition CVodesIntegrator.h:50
Cantera::CVodesIntegrator::m_time
double m_time
The current system time, corresponding to m_y.
Definition CVodesIntegrator.h:106
Cantera::CVodesIntegrator::m_sens_ok
bool m_sens_ok
Indicates whether the sensitivities stored in m_yS have been updated for at the current integrator ti...
Definition CVodesIntegrator.h:135
Cantera::CVodesIntegrator::maxSteps
int maxSteps() override
Returns the maximum number of time-steps the integrator can take before reaching the next output time...
Definition CVodesIntegrator.cpp:208
Cantera::CVodesIntegrator::m_sundials_ctx
SundialsContext m_sundials_ctx
SUNContext object for Sundials>=6.0.
Definition CVodesIntegrator.h:99
Cantera::CVodesIntegrator::setLinearSolverType
void setLinearSolverType(const string &linSolverType) override
Set the linear solver type.
Definition CVodesIntegrator.h:60
Cantera::CVodesIntegrator::checkError
void checkError(long flag, const string &ctMethod, const string &cvodesMethod) const
Check whether a CVODES method indicated an error.
Definition CVodesIntegrator.cpp:681
Cantera::CVodesIntegrator::setSensitivityTolerances
void setSensitivityTolerances(double reltol, double abstol) override
Set the sensitivity error tolerances.
Definition CVodesIntegrator.cpp:149
Cantera::CVodesIntegrator::applyOptions
void applyOptions()
Applies user-specified options to the underlying CVODES solver.
Definition CVodesIntegrator.cpp:352
Cantera::CVodesIntegrator::lastOrder
int lastOrder() const override
Order used during the last solution step.
Definition CVodesIntegrator.cpp:560
Cantera::CVodesIntegrator::setMinStepSize
void setMinStepSize(double hmin) override
Set the minimum step size.
Definition CVodesIntegrator.cpp:192
Cantera::CVodesIntegrator::integrate
void integrate(double tout) override
Integrate the system of equations.
Definition CVodesIntegrator.cpp:495
Cantera::CVodesIntegrator::setTolerances
void setTolerances(double reltol, size_t n, double *abstol) override
Set error tolerances.
Definition CVodesIntegrator.cpp:126
Cantera::CVodesIntegrator::derivative
double * derivative(double tout, int n) override
n-th derivative of the output function at time tout.
Definition CVodesIntegrator.cpp:553
Cantera::CVodesIntegrator::m_error_message
string m_error_message
Error message information provide by CVodes.
Definition CVodesIntegrator.h:83
Cantera::CVodesIntegrator::setMaxErrTestFails
void setMaxErrTestFails(int n) override
Set the maximum permissible number of error test failures.
Definition CVodesIntegrator.cpp:213
Cantera::CVodesIntegrator::linearSolverType
string linearSolverType() const override
Return the integrator problem type.
Definition CVodesIntegrator.h:63
Cantera::CVodesIntegrator::setMaxSteps
void setMaxSteps(int nmax) override
Set the maximum number of time-steps the integrator can take before reaching the next output time.
Definition CVodesIntegrator.cpp:200
Cantera::CVodesIntegrator::solverStats
AnyMap solverStats() const override
Get solver stats from integrator.
Definition CVodesIntegrator.cpp:574
Cantera::CVodesIntegrator::m_linsol_matrix
void * m_linsol_matrix
matrix used by Sundials
Definition CVodesIntegrator.h:101
Cantera::CVodesIntegrator::m_tInteg
double m_tInteg
The latest time reached by the integrator. May be greater than m_time.
Definition CVodesIntegrator.h:109
Cantera::CVodesIntegrator::initialize
void initialize(double t0, FuncEval &func) override
Initialize the integrator for a new problem.
Definition CVodesIntegrator.cpp:251
Cantera::CVodesIntegrator::getErrorInfo
string getErrorInfo(int N)
Returns a string listing the weighted error estimates associated with each solution component.
Definition CVodesIntegrator.cpp:656
Cantera::CVodesIntegrator::setMethod
void setMethod(MethodType t) override
Set the solution method.
Definition CVodesIntegrator.cpp:173
Cantera::FuncEval
Virtual base class for ODE/DAE right-hand-side function evaluators.
Definition FuncEval.h:32
Cantera::Integrator
Abstract base class for ODE system integrators.
Definition Integrator.h:51
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