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