Cantera  3.2.0
Loading...
Searching...
No Matches
FuncEval.h
Go to the documentation of this file.
1/**
2 * @file FuncEval.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_FUNCEVAL_H
9#define CT_FUNCEVAL_H
10
13#include "cantera/base/global.h"
14
15namespace Cantera
16{
17
18/**
19 * @defgroup odeGroup ODE and DAE Function Utilities
20*/
21
22/**
23 * Virtual base class for ODE/DAE right-hand-side function evaluators.
24 * Classes derived from FuncEval evaluate the right-hand-side function
25 * @f$ \vec{F}(t,\vec{y}) @f$ in
26 * @f[
27 * \dot{\vec{y}} = \vec{F}(t,\vec{y}).
28 * @f]
29 * @ingroup odeGroup
30 */
32{
33public:
34 FuncEval() = default;
35 virtual ~FuncEval() = default;
36
37 /**
38 * Evaluate the right-hand-side ODE function. Called by the integrator.
39 * @param[in] t time.
40 * @param[in] y solution vector, length neq()
41 * @param[out] ydot rate of change of solution vector, length neq()
42 * @param[in] p sensitivity parameter vector, length nparams()
43 */
44 virtual void eval(double t, double* y, double* ydot, double* p) {
45 throw NotImplementedError("FuncEval::eval");
46 }
47
48 /**
49 * Evaluate the right-hand-side DAE function. Called by the integrator.
50 * @param[in] t time.
51 * @param[in] y solution vector, length neq()
52 * @param[out] ydot rate of change of solution vector, length neq()
53 * @param[in] p sensitivity parameter vector, length nparams()
54 * @param[out] residual the DAE residuals, length nparams()
55 */
56 virtual void evalDae(double t, double* y, double* ydot, double* p,
57 double* residual) {
58 throw NotImplementedError("FuncEval::evalDae");
59 }
60
61 //! Given a vector of length neq(), mark which variables should be
62 //! considered algebraic constraints
63 virtual void getConstraints(double* constraints) {
64 throw NotImplementedError("FuncEval::getConstraints");
65 }
66
67 //! Evaluate the right-hand side using return code to indicate status.
68 /*!
69 * Errors are indicated using the return value, rather than by throwing
70 * exceptions. This method is used when calling from a C-based integrator
71 * such as CVODES. Exceptions may either be stored or printed, based on the
72 * setting of suppressErrors().
73 * @returns 0 for a successful evaluation; 1 after a potentially-
74 * recoverable error; -1 after an unrecoverable error.
75 */
76 int evalNoThrow(double t, double* y, double* ydot);
77
78 //! Evaluate the right-hand side using return code to indicate status.
79 /*!
80 * Errors are indicated using the return value, rather than by throwing
81 * exceptions. This method is used when calling from a C-based integrator
82 * such as CVODES. Exceptions may either be stored or printed, based on the
83 * setting of suppressErrors().
84 * @returns 0 for a successful evaluation; 1 after a potentially-
85 * recoverable error; -1 after an unrecoverable error.
86 */
87 int evalDaeNoThrow(double t, double* y, double* ydot, double* residual);
88
89 /**
90 * Evaluate the setup processes for the Jacobian preconditioner.
91 * @param[in] t time.
92 * @param[in] y solution vector, length neq()
93 * @param gamma the gamma in M=I-gamma*J
94 * @warning This function is an experimental part of the %Cantera API and may be
95 * changed or removed without notice.
96 */
97 virtual void preconditionerSetup(double t, double* y, double gamma) {
98 throw NotImplementedError("FuncEval::preconditionerSetup");
99 }
100
101 /**
102 * Evaluate the linear system Ax=b where A is the preconditioner.
103 * @param[in] rhs right hand side vector used in linear system
104 * @param[out] output output vector for solution
105 * @warning This function is an experimental part of the %Cantera API and may be
106 * changed or removed without notice.
107 */
108 virtual void preconditionerSolve(double* rhs, double* output) {
109 throw NotImplementedError("FuncEval::preconditionerSolve");
110 }
111
112 //! Update the preconditioner based on already computed jacobian values
113 virtual void updatePreconditioner(double gamma) {
114 throw NotImplementedError("FuncEval::updatePreconditioner");
115 }
116
117 /**
118 * Preconditioner setup that doesn't throw an error but returns a
119 * CVODES flag. It also helps as a first level of polymorphism
120 * which identifies the specific FuncEval, e.g., ReactorNet.
121 * @param[in] t time.
122 * @param[in] y solution vector, length neq()
123 * @param gamma the gamma in M=I-gamma*J
124 * @warning This function is an experimental part of the %Cantera API and may be
125 * changed or removed without notice.
126 */
127 int preconditioner_setup_nothrow(double t, double* y, double gamma);
128
129 /**
130 * Preconditioner solve that doesn't throw an error but returns a
131 * CVODES flag. It also helps as a first level of polymorphism
132 * which identifies the specific FuncEval, e.g., ReactorNet.
133 * @param[in] rhs right hand side vector used in linear system
134 * @param[out] output output vector for solution
135 * @warning This function is an experimental part of the %Cantera API and may be
136 * changed or removed without notice.
137 */
138 int preconditioner_solve_nothrow(double* rhs, double* output);
139
140 //! Number of event/root functions exposed to the integrator.
141 //! 0 indicates root finding is disabled.
142 virtual size_t nRootFunctions() const {
143 return 0;
144 }
145
146 /**
147 * Evaluate the event/root functions currently in play.
148 * Integrators invoke this whenever root finding is enabled; implementations
149 * should fill `gout` with the function values.
150 * @param[in] t Time at which to evaluate the root functions
151 * @param[in] y Current solution vector at time *t* of length neq()
152 * @param[out] gout Array of length nRootFunctions() to be filled with the
153 * values of the root functions
154 */
155 virtual void evalRootFunctions(double t, const double* y, double* gout) { }
156
157 //! Wrapper for evalRootFunctions that converts exceptions to return codes.
158 //! @returns 0 for a successful evaluation, 1 after a potentially-
159 //! recoverable error, or -1 after an unrecoverable error.
160 int evalRootFunctionsNoThrow(double t, const double* y, double* gout);
161
162 //! Fill in the vector *y* with the current state of the system.
163 //! Used for getting the initial state for ODE systems.
164 virtual void getState(double* y) {
165 throw NotImplementedError("FuncEval::getState");
166 }
167
168 //! Fill in the vectors *y* and *ydot* with the current state of the system.
169 //! Used for getting the initial state for DAE systems.
170 virtual void getStateDae(double* y, double* ydot) {
171 throw NotImplementedError("FuncEval::getStateDae");
172 }
173
174 //! Number of equations.
175 virtual size_t neq() const = 0;
176
177 //! Number of sensitivity parameters.
178 virtual size_t nparams() const {
179 return m_sens_params.size();
180 }
181
182 //! Enable or disable suppression of errors when calling eval()
183 void suppressErrors(bool suppress) {
184 m_suppress_errors = suppress;
185 }
186
187 //! Get current state of error suppression
188 bool suppressErrors() const {
189 return m_suppress_errors;
190 };
191
192 //! Return a string containing the text of any suppressed errors
193 string getErrors() const;
194
195 //! Clear any previously-stored suppressed errors
196 void clearErrors() {
197 m_errors.clear();
198 };
199
200 //! Values for the problem parameters for which sensitivities are computed
201 //! This is the array which is perturbed and passed back as the fourth
202 //! argument to eval().
203 vector<double> m_sens_params;
204
205 //! Scaling factors for each sensitivity parameter
206 vector<double> m_paramScales;
207
208protected:
209 // If true, errors are accumulated in m_errors. Otherwise, they are printed
210 bool m_suppress_errors = false;
211
212 //! Errors occurring during function evaluations
213 vector<string> m_errors;
214};
215
216}
217
218#endif
Virtual base class for ODE/DAE right-hand-side function evaluators.
Definition FuncEval.h:32
virtual void evalDae(double t, double *y, double *ydot, double *p, double *residual)
Evaluate the right-hand-side DAE function.
Definition FuncEval.h:56
void suppressErrors(bool suppress)
Enable or disable suppression of errors when calling eval()
Definition FuncEval.h:183
int preconditioner_solve_nothrow(double *rhs, double *output)
Preconditioner solve that doesn't throw an error but returns a CVODES flag.
Definition FuncEval.cpp:113
virtual void getStateDae(double *y, double *ydot)
Fill in the vectors y and ydot with the current state of the system.
Definition FuncEval.h:170
int evalRootFunctionsNoThrow(double t, const double *y, double *gout)
Wrapper for evalRootFunctions that converts exceptions to return codes.
Definition FuncEval.cpp:146
vector< double > m_paramScales
Scaling factors for each sensitivity parameter.
Definition FuncEval.h:206
int preconditioner_setup_nothrow(double t, double *y, double gamma)
Preconditioner setup that doesn't throw an error but returns a CVODES flag.
Definition FuncEval.cpp:80
int evalDaeNoThrow(double t, double *y, double *ydot, double *residual)
Evaluate the right-hand side using return code to indicate status.
Definition FuncEval.cpp:39
virtual void evalRootFunctions(double t, const double *y, double *gout)
Evaluate the event/root functions currently in play.
Definition FuncEval.h:155
void clearErrors()
Clear any previously-stored suppressed errors.
Definition FuncEval.h:196
int evalNoThrow(double t, double *y, double *ydot)
Evaluate the right-hand side using return code to indicate status.
Definition FuncEval.cpp:7
virtual void updatePreconditioner(double gamma)
Update the preconditioner based on already computed jacobian values.
Definition FuncEval.h:113
bool suppressErrors() const
Get current state of error suppression.
Definition FuncEval.h:188
virtual void eval(double t, double *y, double *ydot, double *p)
Evaluate the right-hand-side ODE function.
Definition FuncEval.h:44
virtual size_t neq() const =0
Number of equations.
virtual size_t nRootFunctions() const
Number of event/root functions exposed to the integrator.
Definition FuncEval.h:142
vector< string > m_errors
Errors occurring during function evaluations.
Definition FuncEval.h:213
virtual void getConstraints(double *constraints)
Given a vector of length neq(), mark which variables should be considered algebraic constraints.
Definition FuncEval.h:63
virtual size_t nparams() const
Number of sensitivity parameters.
Definition FuncEval.h:178
string getErrors() const
Return a string containing the text of any suppressed errors.
Definition FuncEval.cpp:71
vector< double > m_sens_params
Values for the problem parameters for which sensitivities are computed This is the array which is per...
Definition FuncEval.h:203
virtual void getState(double *y)
Fill in the vector y with the current state of the system.
Definition FuncEval.h:164
virtual void preconditionerSolve(double *rhs, double *output)
Evaluate the linear system Ax=b where A is the preconditioner.
Definition FuncEval.h:108
virtual void preconditionerSetup(double t, double *y, double gamma)
Evaluate the setup processes for the Jacobian preconditioner.
Definition FuncEval.h:97
An error indicating that an unimplemented function has been called.
This file contains definitions of constants, types and terms that are used in internal routines and a...
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
This file contains definitions for utility functions and text for modules, inputfiles and logging,...
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595