Cantera  3.2.0a2
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
20ReactorBase::ReactorBase(shared_ptr<Solution> sol, const string& name)
21 : ReactorBase(name)
22{
23 if (!sol || !(sol->thermo())) {
24 throw CanteraError("ReactorBase::ReactorBase",
25 "Missing or incomplete Solution object.");
26 }
27 m_solution = sol;
28 m_solution->thermo()->addSpeciesLock();
29 setThermo(*sol->thermo());
30}
31
32ReactorBase::~ReactorBase()
33{
34 if (m_solution) {
35 m_solution->thermo()->removeSpeciesLock();
36 }
37}
38
39bool ReactorBase::setDefaultName(map<string, int>& counts)
40{
41 if (m_defaultNameSet) {
42 return false;
43 }
44 m_defaultNameSet = true;
45 if (m_name == "(none)" || m_name == "") {
46 m_name = fmt::format("{}_{}", type(), counts[type()]);
47 }
48 counts[type()]++;
49 return true;
50}
51
52void ReactorBase::setSolution(shared_ptr<Solution> sol)
53{
54 warn_deprecated("ReactorBase::setSolution",
55 "After Cantera 3.2, a change of reactor contents after instantiation "
56 "will be disabled.");
57 if (!sol || !(sol->thermo())) {
58 throw CanteraError("ReactorBase::setSolution",
59 "Missing or incomplete Solution object.");
60 }
61 if (m_solution) {
62 m_solution->thermo()->removeSpeciesLock();
63 }
64 m_solution = sol;
65 m_solution->thermo()->addSpeciesLock();
66 setThermo(*sol->thermo());
67 try {
68 setKinetics(*sol->kinetics());
69 } catch (NotImplementedError&) {
70 // kinetics not used (example: Reservoir)
71 }
72}
73
75{
76 m_thermo = &thermo;
77 m_nsp = m_thermo->nSpecies();
78 m_thermo->saveState(m_state);
79 m_enthalpy = m_thermo->enthalpy_mass(); // Needed for flow and wall interactions
80 m_pressure = m_thermo->pressure(); // Needed for flow and wall interactions
81 try {
82 m_intEnergy = m_thermo->intEnergy_mass();
83 } catch (NotImplementedError&) {
84 // some ThermoPhase objects do not implement intEnergy_mass()
85 }
86}
87
89{
90 m_inlet.push_back(&inlet);
91}
92
94{
95 m_outlet.push_back(&outlet);
96}
97
99{
100 m_wall.push_back(&w);
101 if (lr == 0) {
102 m_lr.push_back(0);
103 } else {
104 m_lr.push_back(1);
105 }
106}
107
109{
110 return *m_wall[n];
111}
112
114{
115 if (find(m_surfaces.begin(), m_surfaces.end(), surf) == m_surfaces.end()) {
116 m_surfaces.push_back(surf);
117 surf->setReactor(this);
118 }
119}
120
121void ReactorBase::addSurface(shared_ptr<ReactorBase> surf)
122{
123 auto r = std::dynamic_pointer_cast<ReactorSurface>(surf);
124 if (!r) {
125 throw CanteraError("ReactorBase::addSurface",
126 "Invalid reactor type '{}'.", surf->type());
127 }
128 addSurface(r.get());
129}
130
132{
133 return m_surfaces[n];
134}
135
137 if (!m_thermo) {
138 throw CanteraError("ReactorBase::restoreState", "No phase defined.");
139 }
140 m_thermo->restoreState(m_state);
141}
142
144{
145 m_thermo->saveState(m_state);
146 m_enthalpy = m_thermo->enthalpy_mass();
147 try {
148 m_intEnergy = m_thermo->intEnergy_mass();
149 } catch (NotImplementedError&) {
150 m_intEnergy = NAN;
151 }
152 m_pressure = m_thermo->pressure();
153 m_mass = m_thermo->density() * m_vol;
154 if (m_net) {
156 }
157}
158
160{
161 if (m_net) {
162 return *m_net;
163 } else {
164 throw CanteraError("ReactorBase::network",
165 "Reactor is not part of a ReactorNet");
166 }
167}
168
170{
171 m_net = net;
172}
173
175{
176 double mout = 0.0;
177 for (size_t i = 0; i < m_outlet.size(); i++) {
178 mout += m_outlet[i]->massFlowRate();
179 }
180 return mass()/mout;
181}
182
184{
185 return *m_inlet[n];
186}
188{
189 return *m_outlet[n];
190}
191
192}
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:260
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:236
virtual double density() const
Density (kg/m^3).
Definition Phase.h:588
virtual double pressure() const
Return the thermodynamic pressure (Pa).
Definition Phase.h:581
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.
double m_pressure
Current pressure in the reactor [Pa].
virtual string type() const
String indicating the reactor model implemented.
Definition ReactorBase.h:63
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.
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