Cantera  4.0.0a1
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
52 string type() const override {
53 return "Reactor";
54 }
55
56 //! Indicate whether the governing equations for this reactor type are a system of
57 //! ODEs or DAEs. In the first case, this class implements the eval() method. In the
58 //! second case, this class implements the evalDae() method.
59 virtual bool isOde() const {
60 return true;
61 }
62
63 void setInitialVolume(double vol) override {
64 m_vol = vol;
65 }
66
67 void setChemistryEnabled(bool cflag=true) override {
68 m_chem = cflag;
69 }
70
71 bool chemistryEnabled() const override {
72 return m_chem;
73 }
74
75 void setEnergyEnabled(bool eflag=true) override {
76 m_energy = eflag;
77 }
78
79 bool energyEnabled() const override {
80 return m_energy;
81 }
82
83 void getState(span<double> y) override;
84 void initialize(double t0=0.0) override;
85 void eval(double t, span<double> LHS, span<double> RHS) override;
86 void evalSteady(double t, span<double> LHS, span<double> RHS) override;
87 vector<size_t> initializeSteady() override;
88 void updateState(span<const double> y) override;
89 void addSensitivityReaction(size_t rxn) override;
90 void addSensitivitySpeciesEnthalpy(size_t k) override;
91
92 //! Return the index in the solution vector for this reactor of the
93 //! component named *nm*. Possible values for *nm* are "mass", "volume",
94 //! "int_energy", the name of a homogeneous phase species, or the name of a
95 //! surface species.
96 size_t componentIndex(const string& nm) const override;
97 string componentName(size_t k) override;
98 double upperBound(size_t k) const override;
99 double lowerBound(size_t k) const override;
100 void resetBadValues(span<double> y) override;
101
102 //! Set absolute step size limits during advance
103 //! @param limits array of step size limits with length neq
104 void setAdvanceLimits(span<const double> limits);
105
106 //! Check whether Reactor object uses advance limits
107 //! @returns True if at least one limit is set, False otherwise
108 bool hasAdvanceLimits() const {
109 return !m_advancelimits.empty();
110 }
111
112 //! Retrieve absolute step size limits during advance
113 //! @param[out] limits array of step size limits with length neq
114 //! @returns True if at least one limit is set, False otherwise
115 bool getAdvanceLimits(span<double> limits) const;
116
117 //! Set individual step size limit for component name *nm*
118 //! @param nm component name
119 //! @param limit value for step size limit
120 void setAdvanceLimit(const string& nm, const double limit);
121
122 //! Calculate the reactor-specific Jacobian using a finite difference method.
123 //!
124 //! This method is used only for informational purposes. Jacobian calculations
125 //! for the full reactor system are handled internally by CVODES.
126 //!
127 //! @warning This method is an experimental part of the %Cantera
128 //! API and may be changed or removed without notice.
129 Eigen::SparseMatrix<double> finiteDifferenceJacobian();
130
131 //! Control terms included when calculating sparse Jacobian approximations
132 //!
133 //! The `settings` map may include the following boolean options:
134 //!
135 //! - `skip-flow-devices` omits flow-device coupling terms.
136 //! - `skip-walls` omits wall heat-transfer and expansion terms.
137 //! - `skip-connector-composition-dependence` omits derivatives of flow-carried
138 //! composition with respect to upstream species moles.
139 //! - `skip-connector-pressure-composition-dependence` omits the species-mole part
140 //! of pressure derivatives. These terms can be dense and are omitted by default
141 //! when an AdaptivePreconditioner is installed, while pressure derivatives with
142 //! respect to temperature and volume are retained.
143 //!
144 //! Settings for keys not included in the map are left unchanged. Passing an empty
145 //! map will reset all settings to their defaults.
146 //!
147 //! See @ref kinDerivs for additional settings that affect how derivatives for
148 //! certain reaction-related terms are calculated.
149 //!
150 void setDerivativeSettings(AnyMap& settings) override;
151
152 void applySensitivity(span<const double> params) override;
153 void resetSensitivity(span<const double> params) override;
154
155protected:
156 //! Update #m_sdot to reflect current production rates of bulk phase species due to
157 //! reactions on adjacent surfaces.
159
160 //! Evaluate terms related to Walls. Calculates #m_vdot and #m_Qdot based on
161 //! wall movement and heat transfer.
162 //! @param t the current time
163 void evalWalls(double t) override;
164
165 //! Pointer to the homogeneous Kinetics object that handles the reactions
166 Kinetics* m_kin = nullptr;
167
168 double m_vdot = 0.0; //!< net rate of volume change from moving walls [m^3/s]
169 double m_Qdot = 0.0; //!< net heat transfer into the reactor, through walls [W]
170 vector<double> m_wdot; //!< Species net molar production rates
171 vector<double> m_uk; //!< Species molar internal energies
172
173 //! Total production rate of bulk phase species on surfaces [kmol/s]
174 vector<double> m_sdot;
175
176 bool m_chem = false;
177 bool m_energy = true;
178
179 //! Omit flow-device terms from the sparse Jacobian
181
182 //! Omit wall terms from the sparse Jacobian
183 bool m_jac_skip_walls = false;
184
185 //! Omit flow composition derivatives, which can add dense connector blocks
187
188 //! Omit species terms in pressure derivatives, which can add fill-in
190
191 vector<double> m_advancelimits; //!< Advance step limit
192
193 //! Initial volume [m³]; used for steady-state calculations
195};
196}
197
198#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
Base class for reactor objects.
Definition ReactorBase.h:51
double m_vol
Current volume of the reactor [m^3].
string name() const
Return the name of this reactor.
Definition ReactorBase.h:81
Class Reactor is a general-purpose class for stirred reactors.
Definition Reactor.h:47
void evalWalls(double t) override
Evaluate terms related to Walls.
Definition Reactor.cpp:209
double upperBound(size_t k) const override
Get the upper bound on the k-th component of the local state vector.
Definition Reactor.cpp:350
bool m_jac_skip_connector_pressure_composition_dependence
Omit species terms in pressure derivatives, which can add fill-in.
Definition Reactor.h:189
void setChemistryEnabled(bool cflag=true) override
Enable or disable changes in reactor composition due to chemical reactions.
Definition Reactor.h:67
void resetBadValues(span< double > y) override
Reset physically or mathematically problematic values, such as negative species concentrations.
Definition Reactor.cpp:378
bool getAdvanceLimits(span< double > limits) const
Retrieve absolute step size limits during advance.
Definition Reactor.cpp:432
void eval(double t, span< double > LHS, span< double > RHS) override
Evaluate the reactor governing equations.
Definition Reactor.cpp:139
bool m_jac_skip_connector_composition_dependence
Omit flow composition derivatives, which can add dense connector blocks.
Definition Reactor.h:186
Kinetics * m_kin
Pointer to the homogeneous Kinetics object that handles the reactions.
Definition Reactor.h:166
void evalSteady(double t, span< double > LHS, span< double > RHS) override
Evaluate the governing equations with modifications for the steady-state solver.
Definition Reactor.cpp:203
vector< double > m_wdot
Species net molar production rates.
Definition Reactor.h:170
Eigen::SparseMatrix< double > finiteDifferenceJacobian()
Calculate the reactor-specific Jacobian using a finite difference method.
Definition Reactor.cpp:233
bool energyEnabled() const override
Returns true if solution of the energy equation is enabled.
Definition Reactor.h:79
string type() const override
String indicating the reactor model implemented.
Definition Reactor.h:52
bool m_jac_skip_flow_devices
Omit flow-device terms from the sparse Jacobian.
Definition Reactor.h:180
double m_Qdot
net heat transfer into the reactor, through walls [W]
Definition Reactor.h:169
size_t componentIndex(const string &nm) const override
Return the index in the solution vector for this reactor of the component named nm.
Definition Reactor.cpp:318
vector< double > m_advancelimits
Advance step limit.
Definition Reactor.h:191
bool m_jac_skip_walls
Omit wall terms from the sparse Jacobian.
Definition Reactor.h:183
void setEnergyEnabled(bool eflag=true) override
Set the energy equation on or off.
Definition Reactor.h:75
void resetSensitivity(span< const double > params) override
Reset the reaction rate multipliers.
Definition Reactor.cpp:403
void setInitialVolume(double vol) override
Set the initial reactor volume.
Definition Reactor.h:63
vector< double > m_uk
Species molar internal energies.
Definition Reactor.h:171
void applySensitivity(span< const double > params) override
Set reaction rate multipliers based on the sensitivity variables in params.
Definition Reactor.cpp:384
void updateSurfaceProductionRates()
Update m_sdot to reflect current production rates of bulk phase species due to reactions on adjacent ...
Definition Reactor.cpp:306
void addSensitivitySpeciesEnthalpy(size_t k) override
Add a sensitivity parameter associated with the enthalpy formation of species k.
Definition Reactor.cpp:291
void setAdvanceLimit(const string &nm, const double limit)
Set individual step size limit for component name nm
Definition Reactor.cpp:443
vector< size_t > initializeSteady() override
Initialize the reactor before solving a steady-state problem.
Definition Reactor.cpp:221
void addSensitivityReaction(size_t rxn) override
Add a sensitivity parameter associated with the reaction number rxn
Definition Reactor.cpp:278
double lowerBound(size_t k) const override
Get the lower bound on the k-th component of the local state vector.
Definition Reactor.cpp:364
vector< double > m_sdot
Total production rate of bulk phase species on surfaces [kmol/s].
Definition Reactor.h:174
string componentName(size_t k) override
Return the name of the solution component with index i.
Definition Reactor.cpp:337
bool hasAdvanceLimits() const
Check whether Reactor object uses advance limits.
Definition Reactor.h:108
double m_vdot
net rate of volume change from moving walls [m^3/s]
Definition Reactor.h:168
void initialize(double t0=0.0) override
Initialize the reactor.
Definition Reactor.cpp:78
double m_initialVolume
Initial volume [m³]; used for steady-state calculations.
Definition Reactor.h:194
void updateState(span< const double > y) override
Set the state of the reactor to correspond to the state vector y.
Definition Reactor.cpp:92
void setAdvanceLimits(span< const double > limits)
Set absolute step size limits during advance.
Definition Reactor.cpp:421
void setDerivativeSettings(AnyMap &settings) override
Control terms included when calculating sparse Jacobian approximations.
Definition Reactor.cpp:42
void getState(span< double > y) override
Get the current state of the reactor.
Definition Reactor.cpp:62
virtual bool isOde() const
Indicate whether the governing equations for this reactor type are a system of ODEs or DAEs.
Definition Reactor.h:59
bool chemistryEnabled() const override
Returns true if changes in the reactor composition due to chemical reactions are enabled.
Definition Reactor.h:71
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595