Skip to content

Commit

Permalink
[Reactor] YAML summary of ReactorNet
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl committed Aug 14, 2019
1 parent 265a186 commit b83d3d5
Show file tree
Hide file tree
Showing 12 changed files with 278 additions and 15 deletions.
5 changes: 4 additions & 1 deletion include/cantera/zeroD/FlowDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class FlowDevice
return m_type;
}

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

//! Mass flow rate (kg/s).
double massFlowRate(double time = -999.0) {
if (time != -999.0) {
Expand Down Expand Up @@ -89,7 +92,7 @@ class FlowDevice
}

//! Return a const reference to the downstream reactor.
const ReactorBase& out() const {
ReactorBase& out() const {
return *m_out;
}

Expand Down
3 changes: 3 additions & 0 deletions include/cantera/zeroD/ReactorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ class ReactorBase
throw NotImplementedError("ReactorBase::setChemistry");
}

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

//! Set the energy equation on or off.
virtual void setEnergy(int eflag = 1) {
throw NotImplementedError("ReactorBase::setEnergy");
Expand Down
4 changes: 4 additions & 0 deletions include/cantera/zeroD/ReactorNet.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ReactorNet : public FuncEval
//! @name Methods to set up a simulation.
//@{


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

//@}

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

//! Add the reactor *r* to this reactor network.
void addReactor(Reactor& r);

Expand Down
3 changes: 3 additions & 0 deletions include/cantera/zeroD/ReactorSurface.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class ReactorSurface
ReactorSurface(const ReactorSurface&) = delete;
ReactorSurface& operator=(const ReactorSurface&) = delete;

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

//! Returns the surface area [m^2]
double area() const;

Expand Down
11 changes: 7 additions & 4 deletions include/cantera/zeroD/Wall.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class WallBase
return "WallBase";
}

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

//! Rate of volume change (m^3/s) for the adjacent reactors.
/*!
* This method is called by Reactor::evalWalls(). Base class method
Expand Down Expand Up @@ -72,9 +75,9 @@ class WallBase
* @deprecated To be removed after Cantera 2.5.
*/
double getArea() const {
warn_deprecated("WallBase::getArea()",
warn_deprecated("WallBase::getArea()",
"To be removed after Cantera 2.5. "
"Replace with WallBase::area().");
"Replace with WallBase::area().");
return m_area;
}

Expand All @@ -95,7 +98,7 @@ class WallBase
}

//! Return a reference to the Reactor or Reservoir to the right of the wall.
const ReactorBase& right() {
ReactorBase& right() const {
return *m_right;
}

Expand Down Expand Up @@ -216,7 +219,7 @@ class Wall : public WallBase
//! Heat flux function
Func1* m_qf;
};

}

#endif
5 changes: 5 additions & 0 deletions interfaces/cython/cantera/_cantera.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ cdef extern from "cantera/zerodim.h" namespace "Cantera":
cdef cppclass CxxReactorBase "Cantera::ReactorBase":
CxxReactorBase()
string typeStr()
string toYAML()
void setThermoMgr(CxxThermoPhase&) except +translate_exception
void restoreState() except +translate_exception
void syncState() except +translate_exception
Expand Down Expand Up @@ -541,6 +542,7 @@ cdef extern from "cantera/zerodim.h" namespace "Cantera":
cdef cppclass CxxWallBase "Cantera::WallBase":
CxxWallBase()
string type()
string toYAML()
cbool install(CxxReactorBase&, CxxReactorBase&)
double area()
void setArea(double)
Expand Down Expand Up @@ -569,6 +571,7 @@ cdef extern from "cantera/zerodim.h" namespace "Cantera":

cdef cppclass CxxReactorSurface "Cantera::ReactorSurface":
CxxReactorSurface()
string toYAML()
double area()
void setArea(double)
void setKinetics(CxxKinetics*)
Expand All @@ -583,6 +586,7 @@ cdef extern from "cantera/zerodim.h" namespace "Cantera":
cdef cppclass CxxFlowDevice "Cantera::FlowDevice":
CxxFlowDevice()
string typeStr()
string toYAML()
double massFlowRate(double) except +translate_exception
cbool install(CxxReactorBase&, CxxReactorBase&) except +translate_exception
void setPressureFunction(CxxFunc1*) except +translate_exception
Expand All @@ -609,6 +613,7 @@ cdef extern from "cantera/zerodim.h" namespace "Cantera":
cdef cppclass CxxReactorNet "Cantera::ReactorNet":
CxxReactorNet()
void addReactor(CxxReactor&)
string toYAML()
double advance(double, cbool) except +translate_exception
double step() except +translate_exception
void initialize() except +translate_exception
Expand Down
32 changes: 26 additions & 6 deletions interfaces/cython/cantera/reactor.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ cdef class ReactorBase:
def __set__(self, name):
self.rbase.setName(stringify(name))

def to_yaml(self):
"""Return a YAML representation of the Reactor setup."""
return pystr(self.rbase.toYAML())

def syncState(self):
"""
Set the state of the Reactor to match that of the associated
Expand Down Expand Up @@ -414,6 +418,10 @@ cdef class ReactorSurface:
if A is not None:
self.area = A

def to_yaml(self):
"""Return a YAML representation of the ReactorSurface setup."""
return pystr(self.surface.toYAML())

def install(self, Reactor r):
r.reactor.addSurface(self.surface)

Expand Down Expand Up @@ -542,6 +550,10 @@ cdef class WallBase:
def __get__(self):
return pystr(self.wall.type())

def to_yaml(self):
"""Return a YAML representation of the WallBase setup."""
return pystr(self.wall.toYAML())

property left:
""" The left surface of this wall. """
def __get__(self):
Expand Down Expand Up @@ -695,6 +707,10 @@ cdef class FlowDevice:
def __get__(self):
return pystr(self.dev.typeStr())

def to_yaml(self):
"""Return a YAML representation of the FlowDevice setup."""
return pystr(self.dev.toYAML())

def _install(self, ReactorBase upstream, ReactorBase downstream):
"""
Install the device between the *upstream* (source) and *downstream*
Expand Down Expand Up @@ -761,11 +777,11 @@ cdef class MassFlowController(FlowDevice):
.. math:: \dot m = \max(\dot m_0*g(t), 0.),
where :math:`\dot m_0` is a constant value and :math:`g(t)` is a function of
where :math:`\dot m_0` is a constant value and :math:`g(t)` is a function of
time. Both :math:`\dot m_0` and :math:`g(t)` can be set individually by
the property `mass_flow_coeff` and the method `set_time_function`,
respectively. The method `set_mass_flow_rate` combines the former
into a single function. Note that if :math:`\dot m_0*g(t) < 0`, the mass flow
the property `mass_flow_coeff` and the method `set_time_function`,
respectively. The method `set_mass_flow_rate` combines the former
into a single function. Note that if :math:`\dot m_0*g(t) < 0`, the mass flow
rate will be set to zero, since reversal of the flow direction is not allowed.
Unlike a real mass flow controller, a MassFlowController object will
Expand Down Expand Up @@ -805,7 +821,7 @@ cdef class MassFlowController(FlowDevice):
Set the mass flow rate [kg/s] through this controller to be either
a constant or an arbitrary function of time. See `Func1`.
Note that depending on the argument type, this method either changes
Note that depending on the argument type, this method either changes
the property `mass_flow_coeff` or calls the `set_time_function` method.
>>> mfc.set_mass_flow_rate(0.3)
Expand Down Expand Up @@ -936,7 +952,7 @@ cdef class PressureController(FlowDevice):
.. math:: \dot m = \dot m_{\rm master} + K_v*f(P_1 - P_2)
where :math:`f` is the arbitrary function of a single argument.
where :math:`f` is the arbitrary function of a single argument.
"""
flowdevice_type = "PressureController"

Expand Down Expand Up @@ -1007,6 +1023,10 @@ cdef class ReactorNet:
self._reactors.append(r)
self.net.addReactor(deref(r.reactor))

def to_yaml(self):
"""Return a YAML representation of the ReactorNet setup."""
return pystr(self.net.toYAML())

def advance(self, double t, pybool apply_limit=True):
"""
Advance the state of the reactor network in time from the current time
Expand Down
20 changes: 20 additions & 0 deletions src/zeroD/FlowDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "cantera/zeroD/FlowDevice.h"
#include "cantera/zeroD/ReactorBase.h"
#include "cantera/numerics/Func1.h"
#include "cantera/base/yaml.h"

namespace Cantera
{
Expand Down Expand Up @@ -46,6 +47,25 @@ bool FlowDevice::install(ReactorBase& in, ReactorBase& out)
return true;
}

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::EndMap;

out << yml.c_str();
return out.str();
}

void FlowDevice::setPressureFunction(Func1* f)
{
m_pfunc = f;
Expand Down
20 changes: 20 additions & 0 deletions src/zeroD/ReactorBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "cantera/zeroD/FlowDevice.h"
#include "cantera/zeroD/ReactorNet.h"
#include "cantera/zeroD/ReactorSurface.h"
#include "cantera/base/yaml.h"

using namespace std;
namespace Cantera
Expand Down Expand Up @@ -44,6 +45,25 @@ void ReactorBase::syncState()
}
}

std::string ReactorBase::toYAML() const
{
YAML::Emitter yml;
std::stringstream out;

yml << YAML::BeginMap;
yml << YAML::Key << name();
yml << YAML::BeginMap;
yml << YAML::Key << "type";
yml << YAML::Value << typeStr();
yml << YAML::Key << "thermo";
yml << YAML::Value << m_thermo->name();
yml << YAML::EndMap;
yml << YAML::EndMap;

out << yml.c_str();
return out.str();
}

void ReactorBase::addInlet(FlowDevice& inlet)
{
m_inlet.push_back(&inlet);
Expand Down
Loading

0 comments on commit b83d3d5

Please sign in to comment.