Cantera  4.0.0a1
Loading...
Searching...
No Matches
FlowDevice.h
Go to the documentation of this file.
1//! @file FlowDevice.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_FLOWDEVICE_H
7#define CT_FLOWDEVICE_H
8
10#include "cantera/base/global.h"
12#include "ConnectorNode.h"
13#include "cantera/numerics/eigen_sparse.h"
14
15namespace Cantera
16{
17class Func1;
18class ReactorBase;
19
20/**
21 * Base class for 'flow devices' (valves, pressure regulators, etc.)
22 * connecting reactors.
23 * @ingroup connectorGroup
24 */
26{
27public:
28 FlowDevice(shared_ptr<ReactorBase> r0, shared_ptr<ReactorBase> r1,
29 const string& name="(none)");
30 using ConnectorNode::ConnectorNode; // inherit constructors
31
32 string type() const override {
33 return "FlowDevice";
34 }
35
36 //! Mass flow rate (kg/s).
37 double massFlowRate() {
38 if (m_mdot == Undef) {
39 throw CanteraError("FlowDevice::massFlowRate",
40 "Flow device is not ready. Try initializing the reactor network.");
41 } else {
42 return m_mdot;
43 }
44 }
45
46 //! Update the mass flow rate at time 'time'.
47 //! This must be overloaded in derived classes to update m_mdot.
48 virtual void updateMassFlowRate(double time) {}
49
50 //! Set the fixed mass flow rate (kg/s) through a flow device.
51 virtual void setMassFlowRate(double mdot) {
52 throw NotImplementedError("FlowDevice::setMassFlowRate");
53 }
54
55 //! Derivative of mass flow rate with respect to pressure difference.
56 //!
57 //! Returns `d(mdot)/d(P_in - P_out)` for the current state. A return value of zero
58 //! indicates that pressure coupling does not contribute Jacobian entries.
59 //!
60 //! @since New in %Cantera 4.0.
61 virtual double massFlowRate_ddP() const {
62 return 0.0;
63 }
64
65 //! Get the device coefficient (defined by derived class).
66 //! @since New in %Cantera 3.2.
67 double deviceCoefficient() const {
68 return m_coeff;
69 }
70
71 //! Set the device coefficient (defined by derived class).
72 //! @since New in %Cantera 3.2.
73 void setDeviceCoefficient(double c) {
74 m_coeff = c;
75 }
76
77 //! Set the primary mass flow controller.
78 //! @since New in %Cantera 3.2.
79 virtual void setPrimary(shared_ptr<ConnectorNode> primary) {
80 throw NotImplementedError("FlowDevice::setPrimary");
81 }
82
83 //! Mass flow rate (kg/s) of outlet species k. Returns zero if this species
84 //! is not present in the upstream mixture.
85 double outletSpeciesMassFlowRate(size_t k);
86
87 //! Add Jacobian terms proportional to derivatives of the mass flow rate.
88 //!
89 //! Adds entries for `coeff * d(mdot)/dy_j` to the specified global row of the
90 //! reactor network Jacobian. The flow device supplies the scalar derivative of
91 //! mass flow rate with respect to connector variables such as pressure drop while
92 //! the adjacent reactors supply derivatives of those variables with respect to
93 //! their state variables.
94 //!
95 //! @param[in,out] trips Sparse Jacobian entries. Implementations append entries
96 //! using global row and column indices in the reactor network.
97 //! @param row Global row index receiving these chain-rule terms.
98 //! @param coeff Multiplicative factor applied to `d(mdot)/dy_j`.
99 //! @param includePressureSpecies Include pressure derivatives with respect to
100 //! species state variables when pressure is a derived quantity. These terms
101 //! may be dense and are controlled by preconditioner sparsity settings.
102 //! @since New in %Cantera 4.0.
103 virtual void addMassFlowRateJacobian(SparseTriplets& trips, size_t row,
104 double coeff, bool includePressureSpecies=true);
105
106 //! Add Jacobian terms proportional to derivatives of
107 //! `outletSpeciesMassFlowRate(k)`.
108 //!
109 //! Adds entries for `coeff * d(mdot * Y_k)/dy_j`, where `Y_k` is the upstream mass
110 //! fraction mapped to downstream species `k`.
111 //!
112 //! @param[in,out] trips Sparse Jacobian entries. Implementations append entries
113 //! using global row and column indices in the reactor network.
114 //! @param row Global row index receiving these chain-rule terms.
115 //! @param k Species index in the downstream reactor's phase.
116 //! @param coeff Multiplicative factor applied to `d(mdot * Y_k)/dy_j`.
117 //! @param includeComposition Include derivatives of upstream mass fraction `Y_k`
118 //! with respect to upstream reactor state variables.
119 //! @param includePressureSpecies Include pressure derivatives with respect to
120 //! species state variables when pressure is a derived quantity.
121 //! @since New in %Cantera 4.0.
123 SparseTriplets& trips, size_t row, size_t k, double coeff,
124 bool includeComposition=true, bool includePressureSpecies=true);
125
126 //! Add Jacobian terms for the inlet enthalpy dependence on upstream state.
127 //!
128 //! Adds entries for `coeff * mdot * d(h_in)/dy_j`, where `h_in` is the specific
129 //! enthalpy of the upstream reactor. This captures how the energy carried into a
130 //! downstream reactor changes when the upstream temperature or composition changes.
131 //!
132 //! @param[in,out] trips Sparse Jacobian entries.
133 //! @param row Global row index receiving these chain-rule terms.
134 //! @param coeff Multiplicative factor applied to `mdot * d(h_in)/dy_j`.
135 //! @param includeComposition Include derivatives of upstream enthalpy with respect
136 //! to upstream species moles. These terms add composition-mediated fill-in and
137 //! are controlled by preconditioner sparsity settings.
138 //! @since New in %Cantera 4.0.
139 void addInletEnthalpyJacobian(SparseTriplets& trips, size_t row, double coeff,
140 bool includeComposition=true);
141
142 //! specific enthalpy
143 double enthalpy_mass();
144
145 virtual bool ready() {
146 return (m_in != 0 && m_out != 0);
147 }
148
149 //! Return a reference to the upstream reactor.
150 ReactorBase& in() const {
151 return *m_in;
152 }
153
154 //! Return a const reference to the downstream reactor.
155 const ReactorBase& out() const {
156 return *m_out;
157 }
158
159 //! Return a mutable reference to the downstream reactor.
161 return *m_out;
162 }
163
164 //! Return current value of the pressure function.
165 /*!
166 * The mass flow rate [kg/s] is calculated given the pressure drop [Pa] and a
167 * coefficient set by a flow device specific function; unless a user-defined
168 * pressure function is set, this is the pressure difference across the device.
169 * The calculation of mass flow rate depends to the flow device.
170 * @since New in %Cantera 3.0.
171 */
172 double evalPressureFunction();
173
174 //! Set a function of pressure to modify the pressure response.
175 //! Set a function of pressure that is used in determining the
176 //! mass flow rate through the device. The evaluation of mass flow
177 //! depends on the derived flow device class.
178 //! @since Changed in %Cantera 3.2. Previous version used a raw pointer.
179 virtual void setPressureFunction(shared_ptr<Func1> f) {
180 m_pfunc = f;
181 }
182
183 //! Return current value of the time function.
184 /*!
185 * The mass flow rate [kg/s] is calculated for a Flow device, and multiplied by a
186 * function of time, which returns 1.0 unless a user-defined function is provided.
187 * The calculation of mass flow rate depends on the flow device.
188 * @since New in %Cantera 3.0.
189 */
190 double evalTimeFunction();
191
192 //! Set a function of time to modulate the mass flow rate.
193 //! Set a function of time that is used in determining
194 //! the mass flow rate through the device. The evaluation of mass flow
195 //! depends on the derived flow device class.
196 //! @since Changed in %Cantera 3.2. Previous version used a raw pointer.
197 virtual void setTimeFunction(shared_ptr<Func1> g) {
198 m_tfunc = g;
199 }
200
201 //! Set current reactor network time
202 /*!
203 * @since New in %Cantera 3.0.
204 */
205 void setSimTime(double time) {
206 m_time = time;
207 }
208
209protected:
210 //! Return the derivative of the pressure function at `deltaP`.
211 //! @param deltaP Pressure difference `P_in - P_out` [Pa].
212 //! @since New in %Cantera 4.0.
213 double pressureFunction_ddP(double deltaP) const;
214
215 double m_mdot = Undef;
216
217 //! Function set by setPressureFunction; used by updateMassFlowRate
218 shared_ptr<Func1> m_pfunc;
219
220 //! Function set by setTimeFunction; used by updateMassFlowRate
221 shared_ptr<Func1> m_tfunc;
222
223 //! Coefficient set by derived classes; used by updateMassFlowRate
224 double m_coeff = 1.0;
225
226 //! Current reactor network time
227 double m_time = 0.;
228
229private:
230 size_t m_nspin = 0;
231 size_t m_nspout = 0;
232 ReactorBase* m_in = nullptr;
233 ReactorBase* m_out = nullptr;
234 vector<size_t> m_in2out, m_out2in;
235};
236
237}
238
239#endif
Base class for exceptions thrown by Cantera classes.
Base class for walls and flow devices connecting reactors.
string name() const
Retrieve connector name.
ConnectorNode(const string &name="(none)")
Transitional constructor.
Base class for 'flow devices' (valves, pressure regulators, etc.) connecting reactors.
Definition FlowDevice.h:26
double outletSpeciesMassFlowRate(size_t k)
Mass flow rate (kg/s) of outlet species k.
void addInletEnthalpyJacobian(SparseTriplets &trips, size_t row, double coeff, bool includeComposition=true)
Add Jacobian terms for the inlet enthalpy dependence on upstream state.
virtual void addMassFlowRateJacobian(SparseTriplets &trips, size_t row, double coeff, bool includePressureSpecies=true)
Add Jacobian terms proportional to derivatives of the mass flow rate.
double m_time
Current reactor network time.
Definition FlowDevice.h:227
virtual void setMassFlowRate(double mdot)
Set the fixed mass flow rate (kg/s) through a flow device.
Definition FlowDevice.h:51
double m_coeff
Coefficient set by derived classes; used by updateMassFlowRate.
Definition FlowDevice.h:224
void setSimTime(double time)
Set current reactor network time.
Definition FlowDevice.h:205
string type() const override
String indicating the connector implemented.
Definition FlowDevice.h:32
double enthalpy_mass()
specific enthalpy
double deviceCoefficient() const
Get the device coefficient (defined by derived class).
Definition FlowDevice.h:67
ReactorBase & in() const
Return a reference to the upstream reactor.
Definition FlowDevice.h:150
virtual void setTimeFunction(shared_ptr< Func1 > g)
Set a function of time to modulate the mass flow rate.
Definition FlowDevice.h:197
void addOutletSpeciesMassFlowRateJacobian(SparseTriplets &trips, size_t row, size_t k, double coeff, bool includeComposition=true, bool includePressureSpecies=true)
Add Jacobian terms proportional to derivatives of outletSpeciesMassFlowRate(k).
double massFlowRate()
Mass flow rate (kg/s).
Definition FlowDevice.h:37
void setDeviceCoefficient(double c)
Set the device coefficient (defined by derived class).
Definition FlowDevice.h:73
const ReactorBase & out() const
Return a const reference to the downstream reactor.
Definition FlowDevice.h:155
double evalTimeFunction()
Return current value of the time function.
ReactorBase & out()
Return a mutable reference to the downstream reactor.
Definition FlowDevice.h:160
virtual void setPressureFunction(shared_ptr< Func1 > f)
Set a function of pressure to modify the pressure response.
Definition FlowDevice.h:179
virtual double massFlowRate_ddP() const
Derivative of mass flow rate with respect to pressure difference.
Definition FlowDevice.h:61
shared_ptr< Func1 > m_tfunc
Function set by setTimeFunction; used by updateMassFlowRate.
Definition FlowDevice.h:221
virtual void updateMassFlowRate(double time)
Update the mass flow rate at time 'time'.
Definition FlowDevice.h:48
double evalPressureFunction()
Return current value of the pressure function.
shared_ptr< Func1 > m_pfunc
Function set by setPressureFunction; used by updateMassFlowRate.
Definition FlowDevice.h:218
double pressureFunction_ddP(double deltaP) const
Return the derivative of the pressure function at deltaP.
virtual void setPrimary(shared_ptr< ConnectorNode > primary)
Set the primary mass flow controller.
Definition FlowDevice.h:79
An error indicating that an unimplemented function has been called.
Base class for reactor objects.
Definition ReactorBase.h:51
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
const double Undef
Fairly random number to be used to initialize variables against to see if they are subsequently defin...
Definition ct_defs.h:167