Cantera  3.2.0a4
Loading...
Searching...
No Matches
Reactor.h
Go to the documentation of this file.
1//! @file Reactor.h
2
3// This file is part of Cantera. See License.txt in the top-level directory or
4// at https://cantera.org/license.txt for license and copyright information.
5
6#ifndef CT_REACTOR_H
7#define CT_REACTOR_H
8
9#include "ReactorBase.h"
10#include "cantera/numerics/eigen_sparse.h"
11
12
13namespace Cantera
14{
15
16class Solution;
17class AnyMap;
18
19/**
20 * Class Reactor is a general-purpose class for stirred reactors. The reactor
21 * may have an arbitrary number of inlets and outlets, each of which may be
22 * connected to a "flow device" such as a mass flow controller, a pressure
23 * regulator, etc. Additional reactors may be connected to the other end of
24 * the flow device, allowing construction of arbitrary reactor networks.
25 *
26 * The reactor class integrates the same governing equations no matter what
27 * type of reactor is simulated. The differences among reactor types are
28 * completely specified by the attached flow devices and the time-dependent
29 * user-specified boundary conditions.
30 *
31 * If an instance of class Reactor is used directly, it will simulate an
32 * adiabatic, constant volume reactor with gas-phase chemistry but no surface
33 * chemistry. Other reactor types may be simulated by deriving a class from
34 * Reactor. This method allows specifying the following in terms of the
35 * instantaneous reactor state:
36 *
37 * - rate of change of the total volume (m^3/s)
38 * - surface heat loss rate (W)
39 * - species surface production rates (kmol/s)
40 *
41 * See the [Science Reference](../reference/reactors/controlreactor.html) for
42 * the governing equations of class Reactor.
43 *
44 * @ingroup reactorGroup
45 */
46class Reactor : public ReactorBase
47{
48public:
49 Reactor(shared_ptr<Solution> sol, const string& name="(none)");
50 Reactor(shared_ptr<Solution> sol, bool clone, const string& name="(none)");
51 //! TODO: Remove after %Cantera 3.2 -- all derived classes should use Solution-based
52 //! constructors.
53 using ReactorBase::ReactorBase; // inherit constructors
54
55 string type() const override {
56 return "Reactor";
57 }
58
59 //! Indicate whether the governing equations for this reactor type are a system of
60 //! ODEs or DAEs. In the first case, this class implements the eval() method. In the
61 //! second case, this class implements the evalDae() method.
62 virtual bool isOde() const {
63 return true;
64 }
65
66 //! Indicates whether the governing equations for this reactor are functions of time
67 //! or a spatial variable. All reactors in a network must have the same value.
68 virtual bool timeIsIndependent() const {
69 return true;
70 }
71
72 void setInitialVolume(double vol) override {
73 m_vol = vol;
74 }
75
76 void setChemistry(bool cflag=true) override {
77 m_chem = cflag;
78 }
79
80 //! Returns `true` if changes in the reactor composition due to chemical reactions
81 //! are enabled.
82 bool chemistryEnabled() const {
83 return m_chem;
84 }
85
86 void setEnergy(int eflag=1) override {
87 if (eflag > 0) {
88 m_energy = true;
89 } else {
90 m_energy = false;
91 }
92 }
93
94 //! Returns `true` if solution of the energy equation is enabled.
95 bool energyEnabled() const {
96 return m_energy;
97 }
98
99 //! Number of equations (state variables) for this reactor
100 size_t neq() {
101 if (!m_nv) {
102 initialize();
103 }
104 return m_nv;
105 }
106
107 //! Get the the current state of the reactor.
108 /*!
109 * @param[out] y state vector representing the initial state of the reactor
110 */
111 virtual void getState(double* y);
112
113 //! Get the current state and derivative vector of the reactor for a DAE solver
114 /*!
115 * @param[out] y state vector representing the initial state of the reactor
116 * @param[out] ydot state vector representing the initial derivatives of the
117 * reactor
118 */
119 virtual void getStateDae(double* y, double* ydot) {
120 throw NotImplementedError("Reactor::getStateDae(y, ydot)");
121 }
122
123 void initialize(double t0=0.0) override;
124
125 //! Evaluate the reactor governing equations. Called by ReactorNet::eval.
126 //! @param[in] t time.
127 //! @param[out] LHS pointer to start of vector of left-hand side
128 //! coefficients for governing equations, length m_nv, default values 1
129 //! @param[out] RHS pointer to start of vector of right-hand side
130 //! coefficients for governing equations, length m_nv, default values 0
131 virtual void eval(double t, double* LHS, double* RHS);
132
133 /**
134 * Evaluate the reactor governing equations. Called by ReactorNet::eval.
135 * @param[in] t time.
136 * @param[in] y solution vector, length neq()
137 * @param[in] ydot rate of change of solution vector, length neq()
138 * @param[out] residual residuals vector, length neq()
139 */
140 virtual void evalDae(double t, double* y, double* ydot, double* residual) {
141 throw NotImplementedError("Reactor::evalDae");
142 }
143
144 //! Given a vector of length neq(), mark which variables should be
145 //! considered algebraic constraints
146 virtual void getConstraints(double* constraints) {
147 throw NotImplementedError("Reactor::getConstraints");
148 }
149
150 //! Get the indices of equations that are algebraic constraints when solving the
151 //! steady-state problem.
152 //!
153 //! @warning This method is an experimental part of the %Cantera API and may be
154 //! changed or removed without notice.
155 //! @since New in %Cantera 3.2.
156 virtual vector<size_t> steadyConstraints() const;
157
158 //! Set the state of the reactor to correspond to the state vector *y*.
159 virtual void updateState(double* y);
160
161 //! Number of sensitivity parameters associated with this reactor.
162 //! (including walls)
163 size_t nSensParams() const override;
164
165 void addSensitivityReaction(size_t rxn) override;
166
167 //! Add a sensitivity parameter associated with the enthalpy formation of
168 //! species *k* (in the homogeneous phase)
169 virtual void addSensitivitySpeciesEnthalpy(size_t k);
170
171 //! Return the index in the solution vector for this reactor of the
172 //! component named *nm*. Possible values for *nm* are "mass", "volume",
173 //! "int_energy", the name of a homogeneous phase species, or the name of a
174 //! surface species.
175 virtual size_t componentIndex(const string& nm) const;
176
177 //! Return the name of the solution component with index *i*.
178 //! @see componentIndex()
179 virtual string componentName(size_t k);
180
181 //! Get the upper bound on the k-th component of the local state vector.
182 virtual double upperBound(size_t k) const;
183
184 //! Get the lower bound on the k-th component of the local state vector.
185 virtual double lowerBound(size_t k) const;
186
187 //! Reset physically or mathematically problematic values, such as negative species
188 //! concentrations.
189 //! @param[inout] y current state vector, to be updated; length neq()
190 virtual void resetBadValues(double* y);
191
192 //! Set absolute step size limits during advance
193 //! @param limits array of step size limits with length neq
194 void setAdvanceLimits(const double* limits);
195
196 //! Check whether Reactor object uses advance limits
197 //! @returns True if at least one limit is set, False otherwise
198 bool hasAdvanceLimits() const {
199 return !m_advancelimits.empty();
200 }
201
202 //! Retrieve absolute step size limits during advance
203 //! @param[out] limits array of step size limits with length neq
204 //! @returns True if at least one limit is set, False otherwise
205 bool getAdvanceLimits(double* limits) const;
206
207 //! Set individual step size limit for component name *nm*
208 //! @param nm component name
209 //! @param limit value for step size limit
210 void setAdvanceLimit(const string& nm, const double limit);
211
212 //! Calculate the Jacobian of a specific Reactor specialization.
213 //! @warning Depending on the particular implementation, this may return an
214 //! approximate Jacobian intended only for use in forming a preconditioner for
215 //! iterative solvers.
216 //! @ingroup derivGroup
217 //!
218 //! @warning This method is an experimental part of the %Cantera
219 //! API and may be changed or removed without notice.
220 virtual Eigen::SparseMatrix<double> jacobian() {
221 throw NotImplementedError("Reactor::jacobian");
222 }
223
224 //! Calculate the reactor-specific Jacobian using a finite difference method.
225 //!
226 //! This method is used only for informational purposes. Jacobian calculations
227 //! for the full reactor system are handled internally by CVODES.
228 //!
229 //! @warning This method is an experimental part of the %Cantera
230 //! API and may be changed or removed without notice.
231 Eigen::SparseMatrix<double> finiteDifferenceJacobian();
232
233 //! Use this to set the kinetics objects derivative settings
234 virtual void setDerivativeSettings(AnyMap& settings);
235
236 //! Set reaction rate multipliers based on the sensitivity variables in
237 //! *params*.
238 virtual void applySensitivity(double* params);
239
240 //! Reset the reaction rate multipliers
241 virtual void resetSensitivity(double* params);
242
243 //! Return a false if preconditioning is not supported or true otherwise.
244 //!
245 //! @warning This method is an experimental part of the %Cantera
246 //! API and may be changed or removed without notice.
247 //!
248 //! @since New in %Cantera 3.0
249 //!
250 virtual bool preconditionerSupported() const {return false;};
251
252protected:
253 //! @deprecated To be removed after %Cantera 3.2. Use constructor with
254 //! Solution object instead.
255 void setKinetics(Kinetics& kin) override;
256
257 //! Return the index in the solution vector for this reactor of the species
258 //! named *nm*, in either the homogeneous phase or a surface phase, relative
259 //! to the start of the species terms. Used to implement componentIndex for
260 //! specific reactor implementations.
261 virtual size_t speciesIndex(const string& nm) const;
262
263 //! Evaluate terms related to Walls. Calculates #m_vdot and #m_Qdot based on
264 //! wall movement and heat transfer.
265 //! @param t the current time
266 virtual void evalWalls(double t);
267
268 //! Evaluate terms related to surface reactions.
269 //! @param[out] LHS Multiplicative factor on the left hand side of ODE for surface
270 //! species coverages
271 //! @param[out] RHS Right hand side of ODE for surface species coverages
272 //! @param[out] sdot array of production rates of bulk phase species on surfaces
273 //! [kmol/s]
274 virtual void evalSurfaces(double* LHS, double* RHS, double* sdot);
275
276 virtual void evalSurfaces(double* RHS, double* sdot);
277
278 //! Update the state of SurfPhase objects attached to this reactor
279 virtual void updateSurfaceState(double* y);
280
281 //! Update the state information needed by connected reactors, flow devices,
282 //! and reactor walls. Called from updateState().
283 //! @param updatePressure Indicates whether to update #m_pressure. Should
284 //! `true` for reactors where the pressure is a dependent property,
285 //! calculated from the state, and `false` when the pressure is constant
286 //! or an independent variable.
287 virtual void updateConnected(bool updatePressure);
288
289 //! Get initial conditions for SurfPhase objects attached to this reactor
290 virtual void getSurfaceInitialConditions(double* y);
291
292 //! Pointer to the homogeneous Kinetics object that handles the reactions
293 Kinetics* m_kin = nullptr;
294
295 double m_vdot = 0.0; //!< net rate of volume change from moving walls [m^3/s]
296
297 double m_Qdot = 0.0; //!< net heat transfer into the reactor, through walls [W]
298
299 vector<double> m_work;
300
301 //! Production rates of gas phase species on surfaces [kmol/s]
302 vector<double> m_sdot;
303
304 vector<double> m_wdot; //!< Species net molar production rates
305 vector<double> m_uk; //!< Species molar internal energies
306 bool m_chem = false;
307 bool m_energy = true;
308 size_t m_nv = 0;
309 size_t m_nv_surf; //!!< Number of variables associated with reactor surfaces
310
311 vector<double> m_advancelimits; //!< Advance step limit
312
313 //! Vector of triplets representing the jacobian
314 vector<Eigen::Triplet<double>> m_jac_trips;
315};
316}
317
318#endif
A map of string keys to values whose type can vary at runtime.
Definition AnyMap.h:431
Public interface for kinetics managers.
Definition Kinetics.h:126
An error indicating that an unimplemented function has been called.
Base class for reactor objects.
Definition ReactorBase.h:49
ReactorBase(const string &name="(none)")
double m_vol
Current volume of the reactor [m^3].
string name() const
Return the name of this reactor.
Definition ReactorBase.h:82
Class Reactor is a general-purpose class for stirred reactors.
Definition Reactor.h:47
virtual double lowerBound(size_t k) const
Get the lower bound on the k-th component of the local state vector.
Definition Reactor.cpp:542
virtual string componentName(size_t k)
Return the name of the solution component with index i.
Definition Reactor.cpp:502
bool chemistryEnabled() const
Returns true if changes in the reactor composition due to chemical reactions are enabled.
Definition Reactor.h:82
virtual void evalSurfaces(double *LHS, double *RHS, double *sdot)
Evaluate terms related to surface reactions.
Definition Reactor.cpp:309
virtual size_t componentIndex(const string &nm) const
Return the index in the solution vector for this reactor of the component named nm.
Definition Reactor.cpp:486
virtual void getStateDae(double *y, double *ydot)
Get the current state and derivative vector of the reactor for a DAE solver.
Definition Reactor.h:119
virtual void updateSurfaceState(double *y)
Update the state of SurfPhase objects attached to this reactor.
Definition Reactor.cpp:193
virtual void applySensitivity(double *params)
Set reaction rate multipliers based on the sensitivity variables in params.
Definition Reactor.cpp:562
Kinetics * m_kin
Pointer to the homogeneous Kinetics object that handles the reactions.
Definition Reactor.h:293
vector< double > m_wdot
Species net molar production rates.
Definition Reactor.h:304
virtual void eval(double t, double *LHS, double *RHS)
Evaluate the reactor governing equations.
Definition Reactor.cpp:233
virtual void evalWalls(double t)
Evaluate terms related to Walls.
Definition Reactor.cpp:297
bool energyEnabled() const
Returns true if solution of the energy equation is enabled.
Definition Reactor.h:95
Eigen::SparseMatrix< double > finiteDifferenceJacobian()
Calculate the reactor-specific Jacobian using a finite difference method.
Definition Reactor.cpp:354
size_t neq()
Number of equations (state variables) for this reactor.
Definition Reactor.h:100
virtual void addSensitivitySpeciesEnthalpy(size_t k)
Add a sensitivity parameter associated with the enthalpy formation of species k (in the homogeneous p...
Definition Reactor.cpp:449
size_t nSensParams() const override
Number of sensitivity parameters associated with this reactor.
Definition Reactor.cpp:136
string type() const override
String indicating the reactor model implemented.
Definition Reactor.h:55
double m_Qdot
net heat transfer into the reactor, through walls [W]
Definition Reactor.h:297
void setKinetics(Kinetics &kin) override
Definition Reactor.cpp:60
vector< double > m_advancelimits
!< Number of variables associated with reactor surfaces
Definition Reactor.h:311
void setEnergy(int eflag=1) override
Set the energy equation on or off.
Definition Reactor.h:86
virtual vector< size_t > steadyConstraints() const
Get the indices of equations that are algebraic constraints when solving the steady-state problem.
Definition Reactor.cpp:338
virtual double upperBound(size_t k) const
Get the upper bound on the k-th component of the local state vector.
Definition Reactor.cpp:528
void setInitialVolume(double vol) override
Set the initial reactor volume.
Definition Reactor.h:72
virtual void setDerivativeSettings(AnyMap &settings)
Use this to set the kinetics objects derivative settings.
Definition Reactor.cpp:51
vector< double > m_uk
Species molar internal energies.
Definition Reactor.h:305
virtual void updateState(double *y)
Set the state of the reactor to correspond to the state vector y.
Definition Reactor.cpp:145
virtual void evalDae(double t, double *y, double *ydot, double *residual)
Evaluate the reactor governing equations.
Definition Reactor.h:140
void setChemistry(bool cflag=true) override
Enable or disable changes in reactor composition due to chemical reactions.
Definition Reactor.h:76
void setAdvanceLimits(const double *limits)
Set absolute step size limits during advance.
Definition Reactor.cpp:605
vector< Eigen::Triplet< double > > m_jac_trips
Vector of triplets representing the jacobian.
Definition Reactor.h:314
void setAdvanceLimit(const string &nm, const double limit)
Set individual step size limit for component name nm
Definition Reactor.cpp:631
virtual void resetSensitivity(double *params)
Reset the reaction rate multipliers.
Definition Reactor.cpp:584
void addSensitivityReaction(size_t rxn) override
Add a sensitivity parameter associated with the reaction number rxn
Definition Reactor.cpp:436
vector< double > m_sdot
Production rates of gas phase species on surfaces [kmol/s].
Definition Reactor.h:302
virtual void getState(double *y)
Get the the current state of the reactor.
Definition Reactor.cpp:73
bool hasAdvanceLimits() const
Check whether Reactor object uses advance limits.
Definition Reactor.h:198
double m_vdot
net rate of volume change from moving walls [m^3/s]
Definition Reactor.h:295
virtual void getSurfaceInitialConditions(double *y)
Get initial conditions for SurfPhase objects attached to this reactor.
Definition Reactor.cpp:99
void initialize(double t0=0.0) override
Initialize the reactor.
Definition Reactor.cpp:108
virtual void getConstraints(double *constraints)
Given a vector of length neq(), mark which variables should be considered algebraic constraints.
Definition Reactor.h:146
virtual bool preconditionerSupported() const
Return a false if preconditioning is not supported or true otherwise.
Definition Reactor.h:250
bool getAdvanceLimits(double *limits) const
Retrieve absolute step size limits during advance.
Definition Reactor.cpp:620
virtual size_t speciesIndex(const string &nm) const
Return the index in the solution vector for this reactor of the species named nm, in either the homog...
Definition Reactor.cpp:464
virtual bool isOde() const
Indicate whether the governing equations for this reactor type are a system of ODEs or DAEs.
Definition Reactor.h:62
virtual bool timeIsIndependent() const
Indicates whether the governing equations for this reactor are functions of time or a spatial variabl...
Definition Reactor.h:68
virtual void resetBadValues(double *y)
Reset physically or mathematically problematic values, such as negative species concentrations.
Definition Reactor.cpp:556
virtual void updateConnected(bool updatePressure)
Update the state information needed by connected reactors, flow devices, and reactor walls.
Definition Reactor.cpp:202
virtual Eigen::SparseMatrix< double > jacobian()
Calculate the Jacobian of a specific Reactor specialization.
Definition Reactor.h:220
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595