Skip to content

Commit

Permalink
[Reactor] streamline/update naming of zeroD objects
Browse files Browse the repository at this point in the history
* pass name attributes from Cython to C++ layer for FlowDevice and
WallBase objects
* create name and type attributes for ReactorSurface objects
  • Loading branch information
Ingmar Schoegl authored and ischoegl committed Aug 15, 2019
1 parent 0404d5c commit 4fe2aa6
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 100 deletions.
14 changes: 13 additions & 1 deletion include/cantera/zeroD/FlowDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const int Valve_Type = 3;
class FlowDevice
{
public:
FlowDevice();
FlowDevice(const std::string& name = "(none)");

virtual ~FlowDevice() {}
FlowDevice(const FlowDevice&) = delete;
Expand All @@ -53,6 +53,16 @@ class FlowDevice
return m_type;
}

//! Return the name of this flow device
std::string name() const {
return m_name;
}

//! Set the name of this flow device
void setName(const std::string& name) {
m_name = name;
}

//! Generate self-documenting YAML string.
virtual std::string toYAML() const;

Expand Down Expand Up @@ -156,6 +166,8 @@ class FlowDevice

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

std::string m_name; //! flow device name

private:
size_t m_nspin, m_nspout;
ReactorBase* m_in;
Expand Down
3 changes: 0 additions & 3 deletions include/cantera/zeroD/Reactor.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,6 @@ class Reactor : public ReactorBase
//! Get initial conditions for SurfPhase objects attached to this reactor
virtual void getSurfaceInitialConditions(double* y);

//! Pointer to the homogeneous Kinetics object that handles the reactions
Kinetics* m_kin;

doublereal m_vdot; //!< net rate of volume change from moving walls [m^3/s]
doublereal m_Q; //!< net heat transfer through walls [W]
doublereal m_mass; //!< total mass
Expand Down
4 changes: 4 additions & 0 deletions include/cantera/zeroD/ReactorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ class ReactorBase
size_t m_nsp;

thermo_t* m_thermo;

//! Pointer to the homogeneous Kinetics object that handles the reactions
Kinetics* m_kin;

doublereal m_vol;
doublereal m_enthalpy;
doublereal m_intEnergy;
Expand Down
22 changes: 20 additions & 2 deletions include/cantera/zeroD/ReactorSurface.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! @file ReactorSurface.h Header file for class ReactorSurface

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

#ifndef CT_REACTOR_SURFACE_H
#define CT_REACTOR_SURFACE_H
Expand All @@ -17,11 +17,27 @@ class SurfPhase;
class ReactorSurface
{
public:
ReactorSurface();
ReactorSurface(const std::string& name = "(none)");
virtual ~ReactorSurface() {}
ReactorSurface(const ReactorSurface&) = delete;
ReactorSurface& operator=(const ReactorSurface&) = delete;

//! String indicating the wall model implemented. Usually
//! corresponds to the name of the derived class.
virtual std::string type() const {
return "ReactorSurface";
}

//! Return the name of this surface
std::string name() const {
return m_name;
}

//! Set the name of this surface
void setName(const std::string& name) {
m_name = name;
}

//! Generate self-documenting YAML string.
virtual std::string toYAML() const;

Expand Down Expand Up @@ -93,6 +109,8 @@ class ReactorSurface
ReactorBase* m_reactor;
vector_fp m_cov;
std::vector<SensitivityParameter> m_params;

std::string m_name; //! reactor surface name
};

}
Expand Down
14 changes: 13 additions & 1 deletion include/cantera/zeroD/Wall.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const int WallType = 1;
class WallBase
{
public:
WallBase();
WallBase(const std::string& name = "(none)");

virtual ~WallBase() {}
WallBase(const WallBase&) = delete;
Expand All @@ -40,6 +40,16 @@ class WallBase
return "WallBase";
}

//! Return the name of this wall
std::string name() const {
return m_name;
}

//! Set the name of this wall
void setName(const std::string& name) {
m_name = name;
}

//! Generate self-documenting YAML string.
virtual std::string toYAML() const;

Expand Down Expand Up @@ -109,6 +119,8 @@ class WallBase
std::vector<ReactorSurface> m_surf;

double m_area;

std::string m_name; //! wall name
};

//! Represents a wall between between two ReactorBase objects.
Expand Down
9 changes: 7 additions & 2 deletions interfaces/cython/cantera/_cantera.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,8 @@ cdef extern from "cantera/zerodim.h" namespace "Cantera":
cdef cppclass CxxWallBase "Cantera::WallBase":
CxxWallBase()
string type()
string name()
void setName(string)
string toYAML()
cbool install(CxxReactorBase&, CxxReactorBase&)
double area()
Expand Down Expand Up @@ -571,6 +573,9 @@ cdef extern from "cantera/zerodim.h" namespace "Cantera":

cdef cppclass CxxReactorSurface "Cantera::ReactorSurface":
CxxReactorSurface()
string type()
string name()
void setName(string)
string toYAML()
double area()
void setArea(double)
Expand All @@ -586,6 +591,8 @@ cdef extern from "cantera/zerodim.h" namespace "Cantera":
cdef cppclass CxxFlowDevice "Cantera::FlowDevice":
CxxFlowDevice()
string typeStr()
string name()
void setName(string)
string toYAML()
double massFlowRate(double) except +translate_exception
cbool install(CxxReactorBase&, CxxReactorBase&) except +translate_exception
Expand Down Expand Up @@ -1034,7 +1041,6 @@ cdef class WallBase:
cdef object _heat_flux_func
cdef ReactorBase _left_reactor
cdef ReactorBase _right_reactor
cdef str name

cdef class Wall(WallBase):
pass
Expand All @@ -1043,7 +1049,6 @@ cdef class FlowDevice:
cdef CxxFlowDevice* dev
cdef Func1 _rate_func
cdef Func1 _time_func
cdef str name
cdef ReactorBase _upstream
cdef ReactorBase _downstream

Expand Down
8 changes: 4 additions & 4 deletions interfaces/cython/cantera/examples/reactors/ic_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def piston_speed(t):
inlet = ct.Reservoir(gas, name='Inlet')

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

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

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

# piston is modeled as a moving wall
piston = ct.Wall(ambient_air, cyl)
piston = ct.Wall(ambient_air, cyl, name='Piston')
piston.area = A_piston
piston.set_velocity(piston_speed)

Expand Down
46 changes: 42 additions & 4 deletions interfaces/cython/cantera/reactor.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -410,14 +410,35 @@ cdef class ReactorSurface:
def __dealloc__(self):
del self.surface

def __init__(self, kin=None, Reactor r=None, *, A=None):
def __init__(self, kin=None, Reactor r=None, *, name=None, A=None):

if kin is not None:
self.kinetics = kin
if r is not None:
self.install(r)
if A is not None:
self.area = A

if name is not None:
self.name = name
else:
_reactor_counts[self.__class__.__name__] += 1
n = _reactor_counts[self.__class__.__name__]
self.name = '{0}_{1}'.format(self.__class__.__name__, n)

property type:
"""The type of the reactor."""
def __get__(self):
return pystr(self.surface.type())

property name:
"""The name of the reactor."""
def __get__(self):
return pystr(self.surface.name())

def __set__(self, name):
self.surface.setName(stringify(name))

def to_yaml(self):
"""Return a YAML representation of the ReactorSurface setup."""
return pystr(self.surface.toYAML())
Expand Down Expand Up @@ -515,12 +536,13 @@ cdef class WallBase:
self._heat_flux_func = None

self._install(left, right)

if name is not None:
self.name = name
else:
_reactor_counts['Wall'] += 1
n = _reactor_counts['Wall']
self.name = 'Wall_{0}'.format(n)
_reactor_counts[self.__class__.__name__] += 1
n = _reactor_counts[self.__class__.__name__]
self.name = '{0}_{1}'.format(self.__class__.__name__, n)

if A is not None:
self.area = A
Expand Down Expand Up @@ -550,6 +572,14 @@ cdef class WallBase:
def __get__(self):
return pystr(self.wall.type())

property name:
"""The name of the reactor."""
def __get__(self):
return pystr(self.wall.name())

def __set__(self, name):
self.wall.setName(stringify(name))

def to_yaml(self):
"""Return a YAML representation of the WallBase setup."""
return pystr(self.wall.toYAML())
Expand Down Expand Up @@ -707,6 +737,14 @@ cdef class FlowDevice:
def __get__(self):
return pystr(self.dev.typeStr())

property name:
"""The name of the reactor."""
def __get__(self):
return pystr(self.dev.name())

def __set__(self, name):
self.dev.setName(stringify(name))

def to_yaml(self):
"""Return a YAML representation of the FlowDevice setup."""
return pystr(self.dev.toYAML())
Expand Down
28 changes: 16 additions & 12 deletions src/zeroD/FlowDevice.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! @file FlowDevice.cpp

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

#include "cantera/zeroD/FlowDevice.h"
#include "cantera/zeroD/ReactorBase.h"
Expand All @@ -11,10 +11,15 @@
namespace Cantera
{

FlowDevice::FlowDevice() : m_mdot(0.0), m_pfunc(0), m_tfunc(0),
m_coeff(1.0), m_type(0),
m_nspin(0), m_nspout(0),
m_in(0), m_out(0) {}
FlowDevice::FlowDevice(const std::string& name) :
m_mdot(0.0),
m_pfunc(0), m_tfunc(0),
m_coeff(1.0), m_type(0),
m_nspin(0), m_nspout(0),
m_in(0), m_out(0)
{
m_name = name;
}

bool FlowDevice::install(ReactorBase& in, ReactorBase& out)
{
Expand Down Expand Up @@ -52,14 +57,13 @@ std::string FlowDevice::toYAML() const
YAML::Emitter yml;
std::stringstream out;

// object is not aware of its unique identifier
yml << YAML::BeginMap;
yml << YAML::Key << "type";
yml << YAML::Value << typeStr();
yml << YAML::Key << "in";
yml << YAML::Value << m_in->name();
yml << YAML::Key << "out";
yml << YAML::Value << m_out->name();
yml << YAML::Key << name();
yml << YAML::BeginMap;
yml << YAML::Key << "type" << YAML::Value << typeStr();
yml << YAML::Key << "in" << YAML::Value << m_in->name();
yml << YAML::Key << "out" << YAML::Value << m_out->name();
yml << YAML::EndMap;
yml << YAML::EndMap;

out << yml.c_str();
Expand Down
1 change: 0 additions & 1 deletion src/zeroD/Reactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ namespace bmt = boost::math::tools;
namespace Cantera
{
Reactor::Reactor() :
m_kin(0),
m_vdot(0.0),
m_Q(0.0),
m_mass(0.0),
Expand Down
16 changes: 13 additions & 3 deletions src/zeroD/ReactorBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ using namespace std;
namespace Cantera
{

ReactorBase::ReactorBase(const string& name) : m_nsp(0),
ReactorBase::ReactorBase(const string& name) :
m_nsp(0),
m_thermo(0),
m_kin(0),
m_vol(1.0),
m_enthalpy(0.0),
m_intEnergy(0.0),
Expand Down Expand Up @@ -55,8 +57,16 @@ std::string ReactorBase::toYAML() const
yml << YAML::BeginMap;
yml << YAML::Key << "type";
yml << YAML::Value << typeStr();
yml << YAML::Key << "thermo";
yml << YAML::Value << m_thermo->name();
if (m_thermo) {
yml << YAML::Key << "phase";
yml << YAML::Value << m_thermo->name();
yml << YAML::Key << "thermo.type";
yml << YAML::Value << m_thermo->type();
}
if (m_kin) {
yml << YAML::Key << "kinetics.type";
yml << YAML::Value << m_kin->kineticsType();
}
yml << YAML::EndMap;
yml << YAML::EndMap;

Expand Down
Loading

0 comments on commit 4fe2aa6

Please sign in to comment.