Skip to content

Commit 8eb72f4

Browse files
author
Ingmar Schoegl
committed
[Reactor] streamline/update naming of zeroD objects
* pass name attributes from Cython to C++ layer for FlowDevice and WallBase objects * create name and type attributes for ReactorSurface objects
1 parent 0404d5c commit 8eb72f4

File tree

10 files changed

+135
-53
lines changed

10 files changed

+135
-53
lines changed

include/cantera/zeroD/FlowDevice.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const int Valve_Type = 3;
2929
class FlowDevice
3030
{
3131
public:
32-
FlowDevice();
32+
FlowDevice(const std::string& name = "(none)");
3333

3434
virtual ~FlowDevice() {}
3535
FlowDevice(const FlowDevice&) = delete;
@@ -53,6 +53,16 @@ class FlowDevice
5353
return m_type;
5454
}
5555

56+
//! Return the name of this flow device
57+
std::string name() const {
58+
return m_name;
59+
}
60+
61+
//! Set the name of this flow device
62+
void setName(const std::string& name) {
63+
m_name = name;
64+
}
65+
5666
//! Generate self-documenting YAML string.
5767
virtual std::string toYAML() const;
5868

@@ -156,6 +166,8 @@ class FlowDevice
156166

157167
int m_type; //!< @deprecated To be removed after Cantera 2.5.
158168

169+
std::string m_name; //! flow device name
170+
159171
private:
160172
size_t m_nspin, m_nspout;
161173
ReactorBase* m_in;

include/cantera/zeroD/ReactorSurface.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! @file ReactorSurface.h Header file for class ReactorSurface
22

33
// This file is part of Cantera. See License.txt in the top-level directory or
4-
// at http://www.cantera.org/license.txt for license and copyright information.
4+
// at https://cantera.org/license.txt for license and copyright information.
55

66
#ifndef CT_REACTOR_SURFACE_H
77
#define CT_REACTOR_SURFACE_H
@@ -17,11 +17,27 @@ class SurfPhase;
1717
class ReactorSurface
1818
{
1919
public:
20-
ReactorSurface();
20+
ReactorSurface(const std::string& name = "(none)");
2121
virtual ~ReactorSurface() {}
2222
ReactorSurface(const ReactorSurface&) = delete;
2323
ReactorSurface& operator=(const ReactorSurface&) = delete;
2424

25+
//! String indicating the wall model implemented. Usually
26+
//! corresponds to the name of the derived class.
27+
virtual std::string type() const {
28+
return "ReactorSurface";
29+
}
30+
31+
//! Return the name of this surface
32+
std::string name() const {
33+
return m_name;
34+
}
35+
36+
//! Set the name of this surface
37+
void setName(const std::string& name) {
38+
m_name = name;
39+
}
40+
2541
//! Generate self-documenting YAML string.
2642
virtual std::string toYAML() const;
2743

@@ -93,6 +109,8 @@ class ReactorSurface
93109
ReactorBase* m_reactor;
94110
vector_fp m_cov;
95111
std::vector<SensitivityParameter> m_params;
112+
113+
std::string m_name; //! reactor surface name
96114
};
97115

98116
}

include/cantera/zeroD/Wall.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const int WallType = 1;
2828
class WallBase
2929
{
3030
public:
31-
WallBase();
31+
WallBase(const std::string& name = "(none)");
3232

3333
virtual ~WallBase() {}
3434
WallBase(const WallBase&) = delete;
@@ -40,6 +40,16 @@ class WallBase
4040
return "WallBase";
4141
}
4242

43+
//! Return the name of this wall
44+
std::string name() const {
45+
return m_name;
46+
}
47+
48+
//! Set the name of this wall
49+
void setName(const std::string& name) {
50+
m_name = name;
51+
}
52+
4353
//! Generate self-documenting YAML string.
4454
virtual std::string toYAML() const;
4555

@@ -109,6 +119,8 @@ class WallBase
109119
std::vector<ReactorSurface> m_surf;
110120

111121
double m_area;
122+
123+
std::string m_name; //! wall name
112124
};
113125

114126
//! Represents a wall between between two ReactorBase objects.

interfaces/cython/cantera/_cantera.pxd

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,8 @@ cdef extern from "cantera/zerodim.h" namespace "Cantera":
542542
cdef cppclass CxxWallBase "Cantera::WallBase":
543543
CxxWallBase()
544544
string type()
545+
string name()
546+
void setName(string)
545547
string toYAML()
546548
cbool install(CxxReactorBase&, CxxReactorBase&)
547549
double area()
@@ -571,6 +573,9 @@ cdef extern from "cantera/zerodim.h" namespace "Cantera":
571573

572574
cdef cppclass CxxReactorSurface "Cantera::ReactorSurface":
573575
CxxReactorSurface()
576+
string type()
577+
string name()
578+
void setName(string)
574579
string toYAML()
575580
double area()
576581
void setArea(double)
@@ -586,6 +591,8 @@ cdef extern from "cantera/zerodim.h" namespace "Cantera":
586591
cdef cppclass CxxFlowDevice "Cantera::FlowDevice":
587592
CxxFlowDevice()
588593
string typeStr()
594+
string name()
595+
void setName(string)
589596
string toYAML()
590597
double massFlowRate(double) except +translate_exception
591598
cbool install(CxxReactorBase&, CxxReactorBase&) except +translate_exception
@@ -1034,7 +1041,6 @@ cdef class WallBase:
10341041
cdef object _heat_flux_func
10351042
cdef ReactorBase _left_reactor
10361043
cdef ReactorBase _right_reactor
1037-
cdef str name
10381044

10391045
cdef class Wall(WallBase):
10401046
pass
@@ -1043,7 +1049,6 @@ cdef class FlowDevice:
10431049
cdef CxxFlowDevice* dev
10441050
cdef Func1 _rate_func
10451051
cdef Func1 _time_func
1046-
cdef str name
10471052
cdef ReactorBase _upstream
10481053
cdef ReactorBase _downstream
10491054

interfaces/cython/cantera/examples/reactors/ic_engine.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def piston_speed(t):
100100
inlet = ct.Reservoir(gas, name='Inlet')
101101

102102
# inlet valve
103-
inlet_valve = ct.Valve(inlet, cyl)
103+
inlet_valve = ct.Valve(inlet, cyl, name='InletValve')
104104
inlet_delta = np.mod(inlet_close - inlet_open, 4 * np.pi)
105105
inlet_valve.valve_coeff = inlet_valve_coeff
106106
inlet_valve.set_time_function(
@@ -111,7 +111,7 @@ def piston_speed(t):
111111
injector = ct.Reservoir(gas, name='Fuel')
112112

113113
# injector is modeled as a mass flow controller
114-
injector_mfc = ct.MassFlowController(injector, cyl)
114+
injector_mfc = ct.MassFlowController(injector, cyl, name='Injector')
115115
injector_delta = np.mod(injector_close - injector_open, 4 * np.pi)
116116
injector_t_open = (injector_close - injector_open) / 2. / np.pi / f
117117
injector_mfc.mass_flow_coeff = injector_mass / injector_t_open
@@ -123,7 +123,7 @@ def piston_speed(t):
123123
outlet = ct.Reservoir(gas, name='Outlet')
124124

125125
# outlet valve
126-
outlet_valve = ct.Valve(cyl, outlet)
126+
outlet_valve = ct.Valve(cyl, outlet, name='OutletValve')
127127
outlet_delta = np.mod(outlet_close - outlet_open, 4 * np.pi)
128128
outlet_valve.valve_coeff = outlet_valve_coeff
129129
outlet_valve.set_time_function(
@@ -134,7 +134,7 @@ def piston_speed(t):
134134
ambient_air = ct.Reservoir(ct.Solution('air.cti'), name='Ambient')
135135

136136
# piston is modeled as a moving wall
137-
piston = ct.Wall(ambient_air, cyl)
137+
piston = ct.Wall(ambient_air, cyl, name='Piston')
138138
piston.area = A_piston
139139
piston.set_velocity(piston_speed)
140140

interfaces/cython/cantera/reactor.pyx

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,14 +410,35 @@ cdef class ReactorSurface:
410410
def __dealloc__(self):
411411
del self.surface
412412

413-
def __init__(self, kin=None, Reactor r=None, *, A=None):
413+
def __init__(self, kin=None, Reactor r=None, *, name=None, A=None):
414+
414415
if kin is not None:
415416
self.kinetics = kin
416417
if r is not None:
417418
self.install(r)
418419
if A is not None:
419420
self.area = A
420421

422+
if name is not None:
423+
self.name = name
424+
else:
425+
_reactor_counts[self.__class__.__name__] += 1
426+
n = _reactor_counts[self.__class__.__name__]
427+
self.name = '{0}_{1}'.format(self.__class__.__name__, n)
428+
429+
property type:
430+
"""The type of the reactor."""
431+
def __get__(self):
432+
return pystr(self.surface.type())
433+
434+
property name:
435+
"""The name of the reactor."""
436+
def __get__(self):
437+
return pystr(self.surface.name())
438+
439+
def __set__(self, name):
440+
self.surface.setName(stringify(name))
441+
421442
def to_yaml(self):
422443
"""Return a YAML representation of the ReactorSurface setup."""
423444
return pystr(self.surface.toYAML())
@@ -515,12 +536,13 @@ cdef class WallBase:
515536
self._heat_flux_func = None
516537

517538
self._install(left, right)
539+
518540
if name is not None:
519541
self.name = name
520542
else:
521-
_reactor_counts['Wall'] += 1
522-
n = _reactor_counts['Wall']
523-
self.name = 'Wall_{0}'.format(n)
543+
_reactor_counts[self.__class__.__name__] += 1
544+
n = _reactor_counts[self.__class__.__name__]
545+
self.name = '{0}_{1}'.format(self.__class__.__name__, n)
524546

525547
if A is not None:
526548
self.area = A
@@ -550,6 +572,14 @@ cdef class WallBase:
550572
def __get__(self):
551573
return pystr(self.wall.type())
552574

575+
property name:
576+
"""The name of the reactor."""
577+
def __get__(self):
578+
return pystr(self.wall.name())
579+
580+
def __set__(self, name):
581+
self.wall.setName(stringify(name))
582+
553583
def to_yaml(self):
554584
"""Return a YAML representation of the WallBase setup."""
555585
return pystr(self.wall.toYAML())
@@ -707,6 +737,14 @@ cdef class FlowDevice:
707737
def __get__(self):
708738
return pystr(self.dev.typeStr())
709739

740+
property name:
741+
"""The name of the reactor."""
742+
def __get__(self):
743+
return pystr(self.dev.name())
744+
745+
def __set__(self, name):
746+
self.dev.setName(stringify(name))
747+
710748
def to_yaml(self):
711749
"""Return a YAML representation of the FlowDevice setup."""
712750
return pystr(self.dev.toYAML())

src/zeroD/FlowDevice.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! @file FlowDevice.cpp
22

33
// This file is part of Cantera. See License.txt in the top-level directory or
4-
// at http://www.cantera.org/license.txt for license and copyright information.
4+
// at https://cantera.org/license.txt for license and copyright information.
55

66
#include "cantera/zeroD/FlowDevice.h"
77
#include "cantera/zeroD/ReactorBase.h"
@@ -11,10 +11,15 @@
1111
namespace Cantera
1212
{
1313

14-
FlowDevice::FlowDevice() : m_mdot(0.0), m_pfunc(0), m_tfunc(0),
15-
m_coeff(1.0), m_type(0),
16-
m_nspin(0), m_nspout(0),
17-
m_in(0), m_out(0) {}
14+
FlowDevice::FlowDevice(const std::string& name)
15+
: m_mdot(0.0)
16+
, m_pfunc(0), m_tfunc(0)
17+
, m_coeff(1.0), m_type(0)
18+
, m_nspin(0), m_nspout(0)
19+
, m_in(0), m_out(0)
20+
{
21+
m_name = name;
22+
}
1823

1924
bool FlowDevice::install(ReactorBase& in, ReactorBase& out)
2025
{
@@ -52,14 +57,13 @@ std::string FlowDevice::toYAML() const
5257
YAML::Emitter yml;
5358
std::stringstream out;
5459

55-
// object is not aware of its unique identifier
5660
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();
61+
yml << YAML::Key << name();
62+
yml << YAML::BeginMap;
63+
yml << YAML::Key << "type" << YAML::Value << typeStr();
64+
yml << YAML::Key << "in" << YAML::Value << m_in->name();
65+
yml << YAML::Key << "out" << YAML::Value << m_out->name();
66+
yml << YAML::EndMap;
6367
yml << YAML::EndMap;
6468

6569
out << yml.c_str();

src/zeroD/ReactorNet.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ std::string ReactorNet::toYAML() const
132132
yml << YAML::Value << YAML::BeginSeq;
133133
for (const auto& p : phases) {
134134
yml << YAML::BeginMap;
135-
yml << YAML::Key << p.first;
135+
yml << YAML::Key << p.second->name();
136136
yml << YAML::BeginMap;
137137
yml << YAML::Key << "type";
138138
yml << YAML::Value << p.second->type();
@@ -154,10 +154,7 @@ std::string ReactorNet::toYAML() const
154154
yml << YAML::Key << "WallBase";
155155
yml << YAML::Value << YAML::BeginSeq;
156156
for (const auto& w : walls) {
157-
yml << YAML::BeginMap;
158-
yml << YAML::Key << w.first;
159157
yml << YAML::Load(w.second->toYAML());
160-
yml << YAML::EndMap;
161158
}
162159
yml << YAML::EndSeq;
163160
}
@@ -167,10 +164,7 @@ std::string ReactorNet::toYAML() const
167164
yml << YAML::Key << "FlowDevice";
168165
yml << YAML::Value << YAML::BeginSeq;
169166
for (const auto& d : devices) {
170-
yml << YAML::BeginMap;
171-
yml << YAML::Key << d.first;
172167
yml << YAML::Load(d.second->toYAML());
173-
yml << YAML::EndMap;
174168
}
175169
yml << YAML::EndSeq;
176170
}
@@ -180,10 +174,7 @@ std::string ReactorNet::toYAML() const
180174
yml << YAML::Key << "ReactorSurface";
181175
yml << YAML::Value << YAML::BeginSeq;
182176
for (const auto& s : surfaces) {
183-
yml << YAML::BeginMap;
184-
yml << YAML::Key << s.first;
185177
yml << YAML::Load(s.second->toYAML());
186-
yml << YAML::EndMap;
187178
}
188179
yml << YAML::EndSeq;
189180
}

0 commit comments

Comments
 (0)