[zeroD] Implement Connector base class

This commit is contained in:
Ingmar Schoegl 2024-08-22 08:40:49 -05:00
parent e6f3e9d448
commit f9578bbedd
6 changed files with 109 additions and 79 deletions

View File

@ -0,0 +1,74 @@
//! @file ConnectorNode.h
// This file is part of Cantera. See License.txt in the top-level directory or
// at https://cantera.org/license.txt for license and copyright information.
#ifndef CT_CONNECTOR_H
#define CT_CONNECTOR_H
#include "cantera/base/ct_defs.h"
#include "cantera/base/global.h"
namespace Cantera
{
class ReactorBase;
/**
* Base class for walls and flow devices connecting reactors.
* In a reactor network, walls and flow devices (e.g., valves, pressure regulators)
* represent nodes in a directed bipartite graph - a graph whose vertices can be
* divided into two disjoint sets such that no two vertices within the same set are
* adjacent - with reactors forming the second set of nodes.
*
* @since New in %Cantera 3.2.
*
* @ingroup connectorGroup
*/
class ConnectorNode
{
public:
//! Transitional constructor.
//! @todo Implement deprecation warning.
ConnectorNode(const string& name="(none)") : m_name(name) {}
//! Instantiate a ConnectorNode object with associated ReactorBase objects.
//! @param r0 First reactor.
//! @param r1 Second reactor.
//! @param name Name of the connector.
ConnectorNode(shared_ptr<ReactorBase> r0, shared_ptr<ReactorBase> r1,
const string& name="(none)") : m_nodes({r0, r1}), m_name(name) {}
virtual ~ConnectorNode() = default;
ConnectorNode(const ConnectorNode&) = delete;
ConnectorNode& operator=(const ConnectorNode&) = delete;
//! String indicating the connector implemented. Usually
//! corresponds to the name of the derived class.
virtual string type() const {
return "ConnectorNode";
}
//! Retrieve connector name.
string name() const {
return m_name;
}
//! Set connector name.
void setName(const string& name) {
m_name = name;
}
//! Set the default name of a connector. Returns `false` if it was previously set.
void setDefaultName(map<string, int>& counts);
protected:
//! Pair of reactors forming end points of the connector.
pair<shared_ptr<ReactorBase>, shared_ptr<ReactorBase>> m_nodes;
string m_name; //!< ConnectorNode name.
bool m_defaultNameSet = false; //!< `true` if default name has been previously set.
};
}
#endif

View File

@ -9,6 +9,7 @@
#include "cantera/base/ct_defs.h" #include "cantera/base/ct_defs.h"
#include "cantera/base/global.h" #include "cantera/base/global.h"
#include "cantera/base/ctexceptions.h" #include "cantera/base/ctexceptions.h"
#include "ConnectorNode.h"
namespace Cantera namespace Cantera
{ {
@ -20,34 +21,15 @@ class ReactorBase;
* connecting reactors. * connecting reactors.
* @ingroup flowDeviceGroup * @ingroup flowDeviceGroup
*/ */
class FlowDevice class FlowDevice : public ConnectorNode
{ {
public: public:
FlowDevice(const string& name="(none)") : m_name(name) {} using ConnectorNode::ConnectorNode; // inherit constructors
virtual ~FlowDevice() = default; string type() const override {
FlowDevice(const FlowDevice&) = delete;
FlowDevice& operator=(const FlowDevice&) = delete;
//! String indicating the flow device implemented. Usually
//! corresponds to the name of the derived class.
virtual string type() const {
return "FlowDevice"; return "FlowDevice";
} }
//! Retrieve flow device name.
string name() const {
return m_name;
}
//! Set flow device name.
void setName(const string& name) {
m_name = name;
}
//! Set the default name of a flow device. Returns `false` if it was previously set.
bool setDefaultName(map<string, int>& counts);
//! Mass flow rate (kg/s). //! Mass flow rate (kg/s).
double massFlowRate() { double massFlowRate() {
if (m_mdot == Undef) { if (m_mdot == Undef) {
@ -133,9 +115,6 @@ public:
} }
protected: protected:
string m_name; //!< Flow device name.
bool m_defaultNameSet = false; //!< `true` if default name has been previously set.
double m_mdot = Undef; double m_mdot = Undef;
//! Function set by setPressureFunction; used by updateMassFlowRate //! Function set by setPressureFunction; used by updateMassFlowRate

View File

@ -8,6 +8,7 @@
#include "cantera/base/ctexceptions.h" #include "cantera/base/ctexceptions.h"
#include "cantera/zeroD/ReactorBase.h" #include "cantera/zeroD/ReactorBase.h"
#include "ConnectorNode.h"
namespace Cantera namespace Cantera
{ {
@ -16,36 +17,17 @@ class Func1;
/** /**
* Base class for 'walls' (walls, pistons, etc.) connecting reactors. * Base class for 'walls' (walls, pistons, etc.) connecting reactors.
* @ingroup wallGroup * @ingroup connectorGroup
*/ */
class WallBase class WallBase : public ConnectorNode
{ {
public: public:
WallBase(const string& name="(none)") : m_name(name) {} using ConnectorNode::ConnectorNode; // inherit constructors
virtual ~WallBase() {} string type() const override {
WallBase(const WallBase&) = delete;
WallBase& operator=(const WallBase&) = delete;
//! String indicating the wall model implemented. Usually
//! corresponds to the name of the derived class.
virtual string type() const {
return "WallBase"; return "WallBase";
} }
//! Retrieve wall name.
string name() const {
return m_name;
}
//! Set wall name.
void setName(const string& name) {
m_name = name;
}
//! Set the default name of a wall. Returns `false` if it was previously set.
bool setDefaultName(map<string, int>& counts);
//! Rate of volume change (m^3/s) for the adjacent reactors at current reactor //! Rate of volume change (m^3/s) for the adjacent reactors at current reactor
//! network time. //! network time.
/*! /*!
@ -105,9 +87,6 @@ public:
} }
protected: protected:
string m_name; //!< Wall name.
bool m_defaultNameSet = false; //!< `true` if default name has been previously set.
ReactorBase* m_left = nullptr; ReactorBase* m_left = nullptr;
ReactorBase* m_right = nullptr; ReactorBase* m_right = nullptr;
@ -121,7 +100,7 @@ protected:
/*! /*!
* Walls can move (changing the volume of the adjacent reactors) and allow heat * Walls can move (changing the volume of the adjacent reactors) and allow heat
* transfer between reactors. * transfer between reactors.
* @ingroup wallGroup * @ingroup connectorGroup
*/ */
class Wall : public WallBase class Wall : public WallBase
{ {

View File

@ -0,0 +1,25 @@
//! @file ConnectorNode.cpp
// This file is part of Cantera. See License.txt in the top-level directory or
// at https://cantera.org/license.txt for license and copyright information.
#include "cantera/zeroD/ConnectorNode.h"
#include "cantera/zeroD/ReactorBase.h"
namespace Cantera
{
void ConnectorNode::setDefaultName(map<string, int>& counts)
{
if (m_defaultNameSet) {
return;
}
m_defaultNameSet = true;
string typ(type());
if (m_name == "(none)" || m_name == "") {
m_name = fmt::format("{}_{}", type(), counts[type()]);
}
counts[type()]++;
}
}

View File

@ -11,20 +11,6 @@
namespace Cantera namespace Cantera
{ {
bool FlowDevice::setDefaultName(map<string, int>& counts)
{
if (m_defaultNameSet) {
return false;
}
m_defaultNameSet = true;
string typ(type());
if (m_name == "(none)" || m_name == "") {
m_name = fmt::format("{}_{}", type(), counts[type()]);
}
counts[type()]++;
return true;
}
bool FlowDevice::install(ReactorBase& in, ReactorBase& out) bool FlowDevice::install(ReactorBase& in, ReactorBase& out)
{ {
if (m_in || m_out) { if (m_in || m_out) {

View File

@ -10,19 +10,6 @@
namespace Cantera namespace Cantera
{ {
bool WallBase::setDefaultName(map<string, int>& counts)
{
if (m_defaultNameSet) {
return false;
}
m_defaultNameSet = true;
if (m_name == "(none)" || m_name == "") {
m_name = fmt::format("{}_{}", type(), counts[type()]);
}
counts[type()]++;
return true;
}
bool WallBase::install(ReactorBase& rleft, ReactorBase& rright) bool WallBase::install(ReactorBase& rleft, ReactorBase& rright)
{ {
// check if wall is already installed // check if wall is already installed