Cantera  3.2.0a4
Loading...
Searching...
No Matches
ReactorBase.cpp
Go to the documentation of this file.
1//! @file ReactorBase.cpp
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
12
13namespace Cantera
14{
15
16ReactorBase::ReactorBase(const string& name) : m_name(name)
17{
18}
19
20// TODO: After Cantera 3.2, this method can be replaced by delegating to
21// ReactorBase::ReactorBase(sol, true, name)
22ReactorBase::ReactorBase(shared_ptr<Solution> sol, const string& name)
23 : ReactorBase(name)
24{
25 warn_deprecated("ReactorBase::ReactorBase", "`clone` argument not specified; "
26 "Default behavior will change from `clone=False` to `clone=True` after "
27 "Cantera 3.2.");
28 if (!sol || !(sol->thermo())) {
29 throw CanteraError("ReactorBase::ReactorBase",
30 "Missing or incomplete Solution object.");
31 }
32 m_solution = sol->clone({}, true, false);
33 m_solution->thermo()->addSpeciesLock();
34 m_thermo = m_solution->thermo().get();
35 m_nsp = m_thermo->nSpecies();
36 m_thermo->saveState(m_state);
37 m_enthalpy = m_thermo->enthalpy_mass(); // Needed for flow and wall interactions
38 m_pressure = m_thermo->pressure(); // Needed for flow and wall interactions
39 try {
40 m_intEnergy = m_thermo->intEnergy_mass();
41 } catch (NotImplementedError&) {
42 // some ThermoPhase objects do not implement intEnergy_mass()
43 }
44}
45
46ReactorBase::ReactorBase(shared_ptr<Solution> sol, bool clone, const string& name)
47 : ReactorBase(name)
48{
49 if (!sol || !(sol->thermo())) {
50 throw CanteraError("ReactorBase::ReactorBase",
51 "Missing or incomplete Solution object.");
52 }
53 if (clone) {
54 m_solution = sol->clone({}, true, false);
55 } else {
56 m_solution = sol;
57 }
58 m_solution->thermo()->addSpeciesLock();
59 m_thermo = m_solution->thermo().get();
60 m_nsp = m_thermo->nSpecies();
61 m_thermo->saveState(m_state);
62 m_enthalpy = m_thermo->enthalpy_mass(); // Needed for flow and wall interactions
63 m_pressure = m_thermo->pressure(); // Needed for flow and wall interactions
64 try {
65 m_intEnergy = m_thermo->intEnergy_mass();
66 } catch (NotImplementedError&) {
67 // some ThermoPhase objects do not implement intEnergy_mass()
68 }
70
71
72ReactorBase::~ReactorBase()
73{
74 if (m_solution) {
75 m_solution->thermo()->removeSpeciesLock();
76 }
77}
78
79bool ReactorBase::setDefaultName(map<string, int>& counts)
80{
81 if (m_defaultNameSet) {
82 return false;
83 }
84 m_defaultNameSet = true;
85 if (m_name == "(none)" || m_name == "") {
86 m_name = fmt::format("{}_{}", type(), counts[type()]);
87 }
88 counts[type()]++;
89 return true;
90}
91
92void ReactorBase::setSolution(shared_ptr<Solution> sol)
93{
94 warn_deprecated("ReactorBase::setSolution",
95 "After Cantera 3.2, a change of reactor contents after instantiation "
96 "will be disabled.");
97 if (!sol || !(sol->thermo())) {
98 throw CanteraError("ReactorBase::setSolution",
99 "Missing or incomplete Solution object.");
100 }
101 if (m_solution) {
102 m_solution->thermo()->removeSpeciesLock();
103 }
104 m_solution = sol;
105 m_solution->thermo()->addSpeciesLock();
106 setThermo(*m_solution->thermo());
107 try {
108 setKinetics(*m_solution->kinetics());
109 } catch (NotImplementedError&) {
110 // kinetics not used (example: Reservoir)
111 }
112}
113
115{
116 warn_deprecated("ReactorBase::setThermo",
117 "After Cantera 3.2, a change of reactor contents after instantiation "
118 "will be disabled.");
119 m_thermo = &thermo;
120 m_nsp = m_thermo->nSpecies();
121 m_thermo->saveState(m_state);
122 m_enthalpy = m_thermo->enthalpy_mass(); // Needed for flow and wall interactions
123 m_pressure = m_thermo->pressure(); // Needed for flow and wall interactions
124 try {
125 m_intEnergy = m_thermo->intEnergy_mass();
126 } catch (NotImplementedError&) {
127 // some ThermoPhase objects do not implement intEnergy_mass()
128 }
129}
130
132{
133 m_inlet.push_back(&inlet);
134}
135
137{
138 m_outlet.push_back(&outlet);
139}
140
142{
143 m_wall.push_back(&w);
144 if (lr == 0) {
145 m_lr.push_back(0);
146 } else {
147 m_lr.push_back(1);
148 }
149}
150
152{
153 return *m_wall[n];
154}
155
157{
158 if (find(m_surfaces.begin(), m_surfaces.end(), surf) == m_surfaces.end()) {
159 m_surfaces.push_back(surf);
160 surf->setReactor(this);
161 }
162}
163
164void ReactorBase::addSurface(shared_ptr<ReactorBase> surf)
165{
166 warn_deprecated("ReactorBase::addSurface(shared_ptr<ReactorBase>)",
167 "To be removed after Cantear 3.2. Use alternate constructor.");
168 auto r = std::dynamic_pointer_cast<ReactorSurface>(surf);
169 if (!r) {
170 throw CanteraError("ReactorBase::addSurface",
171 "Invalid reactor type '{}'.", surf->type());
172 }
173 addSurface(r.get());
174}
175
177{
178 return m_surfaces[n];
179}
180
182 if (!m_thermo) {
183 throw CanteraError("ReactorBase::restoreState", "No phase defined.");
184 }
185 m_thermo->restoreState(m_state);
186}
187
189{
190 m_thermo->saveState(m_state);
191 m_enthalpy = m_thermo->enthalpy_mass();
192 try {
193 m_intEnergy = m_thermo->intEnergy_mass();
194 } catch (NotImplementedError&) {
195 m_intEnergy = NAN;
196 }
197 m_pressure = m_thermo->pressure();
198 m_mass = m_thermo->density() * m_vol;
199 if (m_net) {
201 }
202}
203
205{
206 if (m_net) {
207 return *m_net;
208 } else {
209 throw CanteraError("ReactorBase::network",
210 "Reactor is not part of a ReactorNet");
211 }
212}
213
215{
216 m_net = net;
217}
218
220{
221 double mout = 0.0;
222 for (size_t i = 0; i < m_outlet.size(); i++) {
223 mout += m_outlet[i]->massFlowRate();
224 }
225 return mass()/mout;
226}
227
229{
230 return *m_inlet[n];
231}
233{
234 return *m_outlet[n];
235}
236
237}
Header file for class ReactorSurface.
Header file for class ThermoPhase, the base class for phases with thermodynamic properties,...
Base class for exceptions thrown by Cantera classes.
Base class for 'flow devices' (valves, pressure regulators, etc.) connecting reactors.
Definition FlowDevice.h:25
An error indicating that an unimplemented function has been called.
void restoreState(const vector< double > &state)
Restore a state saved on a previous call to saveState.
Definition Phase.cpp:286
size_t nSpecies() const
Returns the number of species in the phase.
Definition Phase.h:232
void saveState(vector< double > &state) const
Save the current internal state of the phase.
Definition Phase.cpp:266
virtual double density() const
Density (kg/m^3).
Definition Phase.h:612
virtual double pressure() const
Return the thermodynamic pressure (Pa).
Definition Phase.h:605
Base class for reactor objects.
Definition ReactorBase.h:49
virtual void setThermo(ThermoPhase &thermo)
Specify the mixture contained in the reactor.
FlowDevice & outlet(size_t n=0)
Return a reference to the n-th outlet FlowDevice connected to this reactor.
shared_ptr< Solution > m_solution
Composite thermo/kinetics/transport handler.
bool m_defaultNameSet
true if default name has been previously set.
WallBase & wall(size_t n)
Return a reference to the n-th Wall connected to this reactor.
virtual void addOutlet(FlowDevice &outlet)
Connect an outlet FlowDevice to this reactor.
ReactorBase(const string &name="(none)")
double m_pressure
Current pressure in the reactor [Pa].
virtual string type() const
String indicating the reactor model implemented.
Definition ReactorBase.h:77
ReactorNet * m_net
The ReactorNet that this reactor is part of.
virtual void addWall(WallBase &w, int lr)
Insert a Wall between this reactor and another reactor.
void setNetwork(ReactorNet *net)
Set the ReactorNet that this reactor belongs to.
bool setDefaultName(map< string, int > &counts)
Set the default name of a reactor. Returns false if it was previously set.
virtual void setKinetics(Kinetics &kin)
Specify the kinetics manager for the reactor.
FlowDevice & inlet(size_t n=0)
Return a reference to the n-th inlet FlowDevice connected to this reactor.
vector< int > m_lr
Vector of length nWalls(), indicating whether this reactor is on the left (0) or right (1) of each wa...
double m_vol
Current volume of the reactor [m^3].
virtual void addInlet(FlowDevice &inlet)
Connect an inlet FlowDevice to this reactor.
void setSolution(shared_ptr< Solution > sol)
Set the Solution specifying the ReactorBase content.
virtual void syncState()
Set the state of the reactor to the associated ThermoPhase object.
double m_intEnergy
Current internal energy of the reactor [J/kg].
double m_mass
Current mass of the reactor [kg].
size_t m_nsp
Number of homogeneous species in the mixture.
string m_name
Reactor name.
double mass() const
Returns the mass (kg) of the reactor's contents.
virtual void restoreState()
Set the state of the Phase object associated with this reactor to the reactor's current state.
ReactorNet & network()
The ReactorNet that this reactor belongs to.
double residenceTime()
Return the residence time (s) of the contents of this reactor, based on the outlet mass flow rates an...
ReactorSurface * surface(size_t n)
Return a reference to the n-th ReactorSurface connected to this reactor.
double m_enthalpy
Current specific enthalpy of the reactor [J/kg].
virtual void addSurface(ReactorSurface *surf)
Add a ReactorSurface object to a Reactor object.
A class representing a network of connected reactors.
Definition ReactorNet.h:30
void setNeedsReinit()
Called to trigger integrator reinitialization before further integration.
Definition ReactorNet.h:336
A surface where reactions can occur that is in contact with the bulk fluid of a Reactor.
void setReactor(ReactorBase *reactor)
Set the reactor that this Surface interacts with.
Base class for a phase with thermodynamic properties.
double intEnergy_mass() const
Specific internal energy. Units: J/kg.
double enthalpy_mass() const
Specific enthalpy. Units: J/kg.
Base class for 'walls' (walls, pistons, etc.) connecting reactors.
Definition Wall.h:23
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595
void warn_deprecated(const string &source, const AnyBase &node, const string &message)
A deprecation warning for syntax in an input file.
Definition AnyMap.cpp:1997