Skip to content

Commit b83d3d5

Browse files
committed
[Reactor] YAML summary of ReactorNet
1 parent 265a186 commit b83d3d5

File tree

12 files changed

+278
-15
lines changed

12 files changed

+278
-15
lines changed

include/cantera/zeroD/FlowDevice.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class FlowDevice
5353
return m_type;
5454
}
5555

56+
//! Generate self-documenting YAML string.
57+
virtual std::string toYAML() const;
58+
5659
//! Mass flow rate (kg/s).
5760
double massFlowRate(double time = -999.0) {
5861
if (time != -999.0) {
@@ -89,7 +92,7 @@ class FlowDevice
8992
}
9093

9194
//! Return a const reference to the downstream reactor.
92-
const ReactorBase& out() const {
95+
ReactorBase& out() const {
9396
return *m_out;
9497
}
9598

include/cantera/zeroD/ReactorBase.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ class ReactorBase
101101
throw NotImplementedError("ReactorBase::setChemistry");
102102
}
103103

104+
//! Generate self-documenting YAML string.
105+
virtual std::string toYAML() const;
106+
104107
//! Set the energy equation on or off.
105108
virtual void setEnergy(int eflag = 1) {
106109
throw NotImplementedError("ReactorBase::setEnergy");

include/cantera/zeroD/ReactorNet.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class ReactorNet : public FuncEval
3131
//! @name Methods to set up a simulation.
3232
//@{
3333

34+
3435
//! Set initial time. Default = 0.0 s. Restarts integration from this time
3536
//! using the current mixture state as the initial condition.
3637
void setInitialTime(double time);
@@ -97,6 +98,9 @@ class ReactorNet : public FuncEval
9798

9899
//@}
99100

101+
//! Generate self-documenting YAML string.
102+
std::string toYAML() const;
103+
100104
//! Add the reactor *r* to this reactor network.
101105
void addReactor(Reactor& r);
102106

include/cantera/zeroD/ReactorSurface.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class ReactorSurface
2222
ReactorSurface(const ReactorSurface&) = delete;
2323
ReactorSurface& operator=(const ReactorSurface&) = delete;
2424

25+
//! Generate self-documenting YAML string.
26+
virtual std::string toYAML() const;
27+
2528
//! Returns the surface area [m^2]
2629
double area() const;
2730

include/cantera/zeroD/Wall.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ class WallBase
4040
return "WallBase";
4141
}
4242

43+
//! Generate self-documenting YAML string.
44+
virtual std::string toYAML() const;
45+
4346
//! Rate of volume change (m^3/s) for the adjacent reactors.
4447
/*!
4548
* This method is called by Reactor::evalWalls(). Base class method
@@ -72,9 +75,9 @@ class WallBase
7275
* @deprecated To be removed after Cantera 2.5.
7376
*/
7477
double getArea() const {
75-
warn_deprecated("WallBase::getArea()",
78+
warn_deprecated("WallBase::getArea()",
7679
"To be removed after Cantera 2.5. "
77-
"Replace with WallBase::area().");
80+
"Replace with WallBase::area().");
7881
return m_area;
7982
}
8083

@@ -95,7 +98,7 @@ class WallBase
9598
}
9699

97100
//! Return a reference to the Reactor or Reservoir to the right of the wall.
98-
const ReactorBase& right() {
101+
ReactorBase& right() const {
99102
return *m_right;
100103
}
101104

@@ -216,7 +219,7 @@ class Wall : public WallBase
216219
//! Heat flux function
217220
Func1* m_qf;
218221
};
219-
222+
220223
}
221224

222225
#endif

interfaces/cython/cantera/_cantera.pxd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ cdef extern from "cantera/zerodim.h" namespace "Cantera":
505505
cdef cppclass CxxReactorBase "Cantera::ReactorBase":
506506
CxxReactorBase()
507507
string typeStr()
508+
string toYAML()
508509
void setThermoMgr(CxxThermoPhase&) except +translate_exception
509510
void restoreState() except +translate_exception
510511
void syncState() except +translate_exception
@@ -541,6 +542,7 @@ cdef extern from "cantera/zerodim.h" namespace "Cantera":
541542
cdef cppclass CxxWallBase "Cantera::WallBase":
542543
CxxWallBase()
543544
string type()
545+
string toYAML()
544546
cbool install(CxxReactorBase&, CxxReactorBase&)
545547
double area()
546548
void setArea(double)
@@ -569,6 +571,7 @@ cdef extern from "cantera/zerodim.h" namespace "Cantera":
569571

570572
cdef cppclass CxxReactorSurface "Cantera::ReactorSurface":
571573
CxxReactorSurface()
574+
string toYAML()
572575
double area()
573576
void setArea(double)
574577
void setKinetics(CxxKinetics*)
@@ -583,6 +586,7 @@ cdef extern from "cantera/zerodim.h" namespace "Cantera":
583586
cdef cppclass CxxFlowDevice "Cantera::FlowDevice":
584587
CxxFlowDevice()
585588
string typeStr()
589+
string toYAML()
586590
double massFlowRate(double) except +translate_exception
587591
cbool install(CxxReactorBase&, CxxReactorBase&) except +translate_exception
588592
void setPressureFunction(CxxFunc1*) except +translate_exception
@@ -609,6 +613,7 @@ cdef extern from "cantera/zerodim.h" namespace "Cantera":
609613
cdef cppclass CxxReactorNet "Cantera::ReactorNet":
610614
CxxReactorNet()
611615
void addReactor(CxxReactor&)
616+
string toYAML()
612617
double advance(double, cbool) except +translate_exception
613618
double step() except +translate_exception
614619
void initialize() except +translate_exception

interfaces/cython/cantera/reactor.pyx

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ cdef class ReactorBase:
6363
def __set__(self, name):
6464
self.rbase.setName(stringify(name))
6565

66+
def to_yaml(self):
67+
"""Return a YAML representation of the Reactor setup."""
68+
return pystr(self.rbase.toYAML())
69+
6670
def syncState(self):
6771
"""
6872
Set the state of the Reactor to match that of the associated
@@ -414,6 +418,10 @@ cdef class ReactorSurface:
414418
if A is not None:
415419
self.area = A
416420

421+
def to_yaml(self):
422+
"""Return a YAML representation of the ReactorSurface setup."""
423+
return pystr(self.surface.toYAML())
424+
417425
def install(self, Reactor r):
418426
r.reactor.addSurface(self.surface)
419427

@@ -542,6 +550,10 @@ cdef class WallBase:
542550
def __get__(self):
543551
return pystr(self.wall.type())
544552

553+
def to_yaml(self):
554+
"""Return a YAML representation of the WallBase setup."""
555+
return pystr(self.wall.toYAML())
556+
545557
property left:
546558
""" The left surface of this wall. """
547559
def __get__(self):
@@ -695,6 +707,10 @@ cdef class FlowDevice:
695707
def __get__(self):
696708
return pystr(self.dev.typeStr())
697709

710+
def to_yaml(self):
711+
"""Return a YAML representation of the FlowDevice setup."""
712+
return pystr(self.dev.toYAML())
713+
698714
def _install(self, ReactorBase upstream, ReactorBase downstream):
699715
"""
700716
Install the device between the *upstream* (source) and *downstream*
@@ -761,11 +777,11 @@ cdef class MassFlowController(FlowDevice):
761777
762778
.. math:: \dot m = \max(\dot m_0*g(t), 0.),
763779
764-
where :math:`\dot m_0` is a constant value and :math:`g(t)` is a function of
780+
where :math:`\dot m_0` is a constant value and :math:`g(t)` is a function of
765781
time. Both :math:`\dot m_0` and :math:`g(t)` can be set individually by
766-
the property `mass_flow_coeff` and the method `set_time_function`,
767-
respectively. The method `set_mass_flow_rate` combines the former
768-
into a single function. Note that if :math:`\dot m_0*g(t) < 0`, the mass flow
782+
the property `mass_flow_coeff` and the method `set_time_function`,
783+
respectively. The method `set_mass_flow_rate` combines the former
784+
into a single function. Note that if :math:`\dot m_0*g(t) < 0`, the mass flow
769785
rate will be set to zero, since reversal of the flow direction is not allowed.
770786
771787
Unlike a real mass flow controller, a MassFlowController object will
@@ -805,7 +821,7 @@ cdef class MassFlowController(FlowDevice):
805821
Set the mass flow rate [kg/s] through this controller to be either
806822
a constant or an arbitrary function of time. See `Func1`.
807823
808-
Note that depending on the argument type, this method either changes
824+
Note that depending on the argument type, this method either changes
809825
the property `mass_flow_coeff` or calls the `set_time_function` method.
810826
811827
>>> mfc.set_mass_flow_rate(0.3)
@@ -936,7 +952,7 @@ cdef class PressureController(FlowDevice):
936952
937953
.. math:: \dot m = \dot m_{\rm master} + K_v*f(P_1 - P_2)
938954
939-
where :math:`f` is the arbitrary function of a single argument.
955+
where :math:`f` is the arbitrary function of a single argument.
940956
"""
941957
flowdevice_type = "PressureController"
942958

@@ -1007,6 +1023,10 @@ cdef class ReactorNet:
10071023
self._reactors.append(r)
10081024
self.net.addReactor(deref(r.reactor))
10091025

1026+
def to_yaml(self):
1027+
"""Return a YAML representation of the ReactorNet setup."""
1028+
return pystr(self.net.toYAML())
1029+
10101030
def advance(self, double t, pybool apply_limit=True):
10111031
"""
10121032
Advance the state of the reactor network in time from the current time

src/zeroD/FlowDevice.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "cantera/zeroD/FlowDevice.h"
77
#include "cantera/zeroD/ReactorBase.h"
88
#include "cantera/numerics/Func1.h"
9+
#include "cantera/base/yaml.h"
910

1011
namespace Cantera
1112
{
@@ -46,6 +47,25 @@ bool FlowDevice::install(ReactorBase& in, ReactorBase& out)
4647
return true;
4748
}
4849

50+
std::string FlowDevice::toYAML() const
51+
{
52+
YAML::Emitter yml;
53+
std::stringstream out;
54+
55+
// object is not aware of its unique identifier
56+
yml << YAML::BeginMap;
57+
yml << YAML::Key << "type";
58+
yml << YAML::Value << typeStr();
59+
yml << YAML::Key << "in";
60+
yml << YAML::Value << m_in->name();
61+
yml << YAML::Key << "out";
62+
yml << YAML::Value << m_out->name();
63+
yml << YAML::EndMap;
64+
65+
out << yml.c_str();
66+
return out.str();
67+
}
68+
4969
void FlowDevice::setPressureFunction(Func1* f)
5070
{
5171
m_pfunc = f;

src/zeroD/ReactorBase.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "cantera/zeroD/FlowDevice.h"
88
#include "cantera/zeroD/ReactorNet.h"
99
#include "cantera/zeroD/ReactorSurface.h"
10+
#include "cantera/base/yaml.h"
1011

1112
using namespace std;
1213
namespace Cantera
@@ -44,6 +45,25 @@ void ReactorBase::syncState()
4445
}
4546
}
4647

48+
std::string ReactorBase::toYAML() const
49+
{
50+
YAML::Emitter yml;
51+
std::stringstream out;
52+
53+
yml << YAML::BeginMap;
54+
yml << YAML::Key << name();
55+
yml << YAML::BeginMap;
56+
yml << YAML::Key << "type";
57+
yml << YAML::Value << typeStr();
58+
yml << YAML::Key << "thermo";
59+
yml << YAML::Value << m_thermo->name();
60+
yml << YAML::EndMap;
61+
yml << YAML::EndMap;
62+
63+
out << yml.c_str();
64+
return out.str();
65+
}
66+
4767
void ReactorBase::addInlet(FlowDevice& inlet)
4868
{
4969
m_inlet.push_back(&inlet);

0 commit comments

Comments
 (0)