From f9578bbedd3e3d2531a7943dcbcc6101b43a45ca Mon Sep 17 00:00:00 2001 From: Ingmar Schoegl Date: Thu, 22 Aug 2024 08:40:49 -0500 Subject: [PATCH] [zeroD] Implement Connector base class --- include/cantera/zeroD/ConnectorNode.h | 74 +++++++++++++++++++++++++++ include/cantera/zeroD/FlowDevice.h | 29 ++--------- include/cantera/zeroD/Wall.h | 33 +++--------- src/zeroD/ConnectorNode.cpp | 25 +++++++++ src/zeroD/FlowDevice.cpp | 14 ----- src/zeroD/Wall.cpp | 13 ----- 6 files changed, 109 insertions(+), 79 deletions(-) create mode 100644 include/cantera/zeroD/ConnectorNode.h create mode 100644 src/zeroD/ConnectorNode.cpp diff --git a/include/cantera/zeroD/ConnectorNode.h b/include/cantera/zeroD/ConnectorNode.h new file mode 100644 index 000000000..077c6219a --- /dev/null +++ b/include/cantera/zeroD/ConnectorNode.h @@ -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 r0, shared_ptr 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& counts); + +protected: + //! Pair of reactors forming end points of the connector. + pair, shared_ptr> m_nodes; + + string m_name; //!< ConnectorNode name. + bool m_defaultNameSet = false; //!< `true` if default name has been previously set. +}; + +} + +#endif diff --git a/include/cantera/zeroD/FlowDevice.h b/include/cantera/zeroD/FlowDevice.h index 00e622b2b..2f53b26af 100644 --- a/include/cantera/zeroD/FlowDevice.h +++ b/include/cantera/zeroD/FlowDevice.h @@ -9,6 +9,7 @@ #include "cantera/base/ct_defs.h" #include "cantera/base/global.h" #include "cantera/base/ctexceptions.h" +#include "ConnectorNode.h" namespace Cantera { @@ -20,34 +21,15 @@ class ReactorBase; * connecting reactors. * @ingroup flowDeviceGroup */ -class FlowDevice +class FlowDevice : public ConnectorNode { public: - FlowDevice(const string& name="(none)") : m_name(name) {} + using ConnectorNode::ConnectorNode; // inherit constructors - virtual ~FlowDevice() = default; - 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 { + string type() const override { 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& counts); - //! Mass flow rate (kg/s). double massFlowRate() { if (m_mdot == Undef) { @@ -133,9 +115,6 @@ public: } protected: - string m_name; //!< Flow device name. - bool m_defaultNameSet = false; //!< `true` if default name has been previously set. - double m_mdot = Undef; //! Function set by setPressureFunction; used by updateMassFlowRate diff --git a/include/cantera/zeroD/Wall.h b/include/cantera/zeroD/Wall.h index 14b9dbd03..f5a941ef2 100644 --- a/include/cantera/zeroD/Wall.h +++ b/include/cantera/zeroD/Wall.h @@ -8,6 +8,7 @@ #include "cantera/base/ctexceptions.h" #include "cantera/zeroD/ReactorBase.h" +#include "ConnectorNode.h" namespace Cantera { @@ -16,36 +17,17 @@ class Func1; /** * Base class for 'walls' (walls, pistons, etc.) connecting reactors. - * @ingroup wallGroup + * @ingroup connectorGroup */ -class WallBase +class WallBase : public ConnectorNode { public: - WallBase(const string& name="(none)") : m_name(name) {} + using ConnectorNode::ConnectorNode; // inherit constructors - virtual ~WallBase() {} - 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 { + string type() const override { 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& counts); - //! Rate of volume change (m^3/s) for the adjacent reactors at current reactor //! network time. /*! @@ -105,9 +87,6 @@ public: } protected: - string m_name; //!< Wall name. - bool m_defaultNameSet = false; //!< `true` if default name has been previously set. - ReactorBase* m_left = nullptr; ReactorBase* m_right = nullptr; @@ -121,7 +100,7 @@ protected: /*! * Walls can move (changing the volume of the adjacent reactors) and allow heat * transfer between reactors. - * @ingroup wallGroup + * @ingroup connectorGroup */ class Wall : public WallBase { diff --git a/src/zeroD/ConnectorNode.cpp b/src/zeroD/ConnectorNode.cpp new file mode 100644 index 000000000..8dc33d292 --- /dev/null +++ b/src/zeroD/ConnectorNode.cpp @@ -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& 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()]++; +} + +} diff --git a/src/zeroD/FlowDevice.cpp b/src/zeroD/FlowDevice.cpp index 225d19a44..d298a933c 100644 --- a/src/zeroD/FlowDevice.cpp +++ b/src/zeroD/FlowDevice.cpp @@ -11,20 +11,6 @@ namespace Cantera { -bool FlowDevice::setDefaultName(map& 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) { if (m_in || m_out) { diff --git a/src/zeroD/Wall.cpp b/src/zeroD/Wall.cpp index 2d1d61a81..519e80f85 100644 --- a/src/zeroD/Wall.cpp +++ b/src/zeroD/Wall.cpp @@ -10,19 +10,6 @@ namespace Cantera { -bool WallBase::setDefaultName(map& 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) { // check if wall is already installed