Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of 'ReactorEdge' class #1697

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions include/cantera/zeroD/ReactorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
class WallBase;
class ReactorNet;
class ReactorSurface;
class ReactorEdge;
class Kinetics;
class ThermoPhase;
class Solution;
Expand Down Expand Up @@ -163,6 +164,16 @@
return m_surfaces.size();
}

virtual void addEdge(ReactorEdge* edge);

//! Return a reference to the *n*-th ReactorEdge connected to this
//! reactor
ReactorEdge* edge(size_t n);
//! Return the number of edges in a reactor
virtual size_t nEdges() {
return m_edges.size();

Check warning on line 174 in include/cantera/zeroD/ReactorBase.h

View check run for this annotation

Codecov / codecov/patch

include/cantera/zeroD/ReactorBase.h#L173-L174

Added lines #L173 - L174 were not covered by tests
}

/**
* Initialize the reactor. Called automatically by ReactorNet::initialize.
*/
Expand Down Expand Up @@ -303,6 +314,7 @@

vector<WallBase*> m_wall;
vector<ReactorSurface*> m_surfaces;
vector<ReactorEdge*> m_edges;

//! Vector of length nWalls(), indicating whether this reactor is on the left (0)
//! or right (1) of each wall.
Expand Down
1 change: 1 addition & 0 deletions include/cantera/zeroD/ReactorDelegator.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "Reactor.h"
#include "cantera/base/Delegator.h"
#include "cantera/zeroD/ReactorSurface.h"
#include "cantera/zeroD/ReactorEdge.h"
#include "cantera/thermo/SurfPhase.h"

namespace Cantera
Expand Down
81 changes: 81 additions & 0 deletions include/cantera/zeroD/ReactorEdge.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//! @file ReactorEdge.h Header file for class ReactorEdge

// 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_REACTOR_EDGE_H
#define CT_REACTOR_EDGE_H

#include "cantera/zeroD/ReactorBase.h"

namespace Cantera
{

class Kinetics;
//class SurfPhase;
class EdgePhase;

//! An edge where reactions can occur that is in contact with the two surfaces of a
//! Reactor.
//! @ingroup wallGroup
class ReactorEdge
{
public:
ReactorEdge() = default;
virtual ~ReactorEdge() = default;

Check warning on line 25 in include/cantera/zeroD/ReactorEdge.h

View check run for this annotation

Codecov / codecov/patch

include/cantera/zeroD/ReactorEdge.h#L24-L25

Added lines #L24 - L25 were not covered by tests
ReactorEdge(const ReactorEdge&) = delete;
ReactorEdge& operator=(const ReactorEdge&) = delete;

//! Returns the edge length [m]
double length() const;

//! Set the edge length [m]
void setLength(double a);

//! Accessor for the EdgePhase object
EdgePhase* thermo() {
return m_thermo;
}

//! Accessor for the InterfaceKinetics object
Kinetics* kinetics() {
return m_kinetics;
}

//! Set the InterfaceKinetics object for the edge
void setKinetics(Kinetics* kin);

//! Set the reactor that this edge interacts with
void setReactor(ReactorBase* reactor);

//! Set the surface coverages. Array `cov` has length equal to the number of
//! surface species.
void setCoverages(const double* cov);

//! Set the surface coverages by name
void setCoverages(const Composition& cov);

//! Set the surface coverages by name
void setCoverages(const string& cov);

//! Get the surface coverages. Array `cov` should have length equal to the
//! number of surface species.
void getCoverages(double* cov) const;

//! Set the coverages and temperature in the surface phase object to the
//! values for this surface. The temperature is set to match the bulk phase
//! of the attached Reactor.
void syncState();

protected:
double m_length = 1.0;

EdgePhase* m_thermo = nullptr;
Kinetics* m_kinetics = nullptr;
ReactorBase* m_reactor = nullptr;
vector<double> m_cov;
};

}

#endif
3 changes: 3 additions & 0 deletions include/cantera/zerodim.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
// surface
#include "cantera/zeroD/ReactorSurface.h"

// edge
#include "cantera/zeroD/ReactorEdge.h"

// factories
#include "cantera/zeroD/ReactorFactory.h"
#include "cantera/zeroD/FlowDeviceFactory.h"
Expand Down
46 changes: 46 additions & 0 deletions interfaces/cython/cantera/drawnetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,52 @@ def draw_surface(surface, graph=None, graph_attr=None, node_attr=None,

return graph

"""
def draw_edge(edge, graph=None, graph_attr=None, node_attr=None,
surface_edge_attr=None, print_state=False, **kwargs):

Draw `~cantera.ReactorEdge` object with its connected reactor.

:param surface:
`~cantera.ReactorEdge` object.
:param graph:
``graphviz.graphs.BaseGraph`` object to which the connection is added.
If not provided, a new ``DiGraph`` is created. Defaults to ``None``.
:param graph_attr:
Attributes to be passed to the ``graphviz.Digraph`` function that control the
general appearance of the drawn network.
Has no effect if existing ``graph`` is provided.
See https://graphviz.org/docs/graph/ for a list of all usable attributes.
:param node_attr:
Attributes to be passed to the ``node`` method invoked to draw the reactor.
See https://graphviz.org/docs/nodes/ for a list of all usable attributes.
:param surface_edge_attr:
Attributes to be passed to the ``edge`` method invoked to draw the connection
between the surface and its reactor.
See https://graphviz.org/docs/edges/ for a list of all usable attributes.
Default is ``{"style": "dotted", "arrowhead": "none"}``.
:param print_state:
Whether state information of the reactor is printed into the node.
See ``draw_reactor`` for additional keywords to control this output.
Defaults to ``False``.
:param kwargs:
Additional keywords are passed on to `draw_reactor`.
:return:
A ``graphviz.graphs.BaseGraph`` object depicting the surface and its reactor.

.. versionadded:: 3.1????

r = edge.reactor
graph = draw_reactor(r, graph, graph_attr, node_attr, print_state, **kwargs)
name = f"{r.name} edge"
edge_attr = {"style": "dotted", "arrowhead": "none",
**(surface_edge_attr or {})}

graph.node(name, **dict(node_attr or {}, **edge.node_attr))
graph.edge(r.name, name, **edge_attr)

return graph
"""

@_needs_graphviz
def draw_flow_controllers(flow_controllers, graph=None, graph_attr=None, node_attr=None,
Expand Down
25 changes: 25 additions & 0 deletions interfaces/cython/cantera/reactor.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ cdef extern from "cantera/numerics/Integrator.h" namespace "Cantera":
cdef extern from "cantera/zerodim.h" namespace "Cantera":
cdef cppclass CxxWall "Cantera::Wall"
cdef cppclass CxxReactorSurface "Cantera::ReactorSurface"
cdef cppclass CxxReactorEdge "Cantera::ReactorEdge"
cdef cppclass CxxFlowDevice "Cantera::FlowDevice"

# factories
Expand Down Expand Up @@ -60,6 +61,7 @@ cdef extern from "cantera/zerodim.h" namespace "Cantera":
CxxSparseMatrix jacobian() except +translate_exception
CxxSparseMatrix finiteDifferenceJacobian() except +translate_exception
void addSurface(CxxReactorSurface*)
void addEdge(CxxReactorEdge*)
void setAdvanceLimit(string&, double) except +translate_exception
void addSensitivityReaction(size_t) except +translate_exception
void addSensitivitySpeciesEnthalpy(size_t) except +translate_exception
Expand Down Expand Up @@ -127,6 +129,15 @@ cdef extern from "cantera/zerodim.h" namespace "Cantera":
void addSensitivityReaction(size_t) except +translate_exception
size_t nSensParams()

# reactor edge

cdef cppclass CxxReactorEdge "Cantera::ReactorEdge":
CxxReactorEdge()
double length()
void setLength(double)
void setKinetics(CxxKinetics*)
void syncState()

# flow devices

cdef cppclass CxxFlowDevice "Cantera::FlowDevice":
Expand Down Expand Up @@ -223,6 +234,7 @@ cdef class ReactorBase:
cdef list _outlets
cdef list _walls
cdef list _surfaces
cdef list _edges
cdef public dict node_attr
"""
A dictionary containing draw attributes for the representation of the reactor as a
Expand Down Expand Up @@ -282,6 +294,19 @@ cdef class ReactorSurface:
.. versionadded:: 3.1
"""

cdef class ReactorEdge:
cdef CxxReactorEdge* edge
cdef Kinetics _kinetics
cdef ReactorBase _reactor
cdef public dict node_attr
"""
A dictionary containing draw attributes for the representation of the reactor
edge as a graphviz node. See https://graphviz.org/docs/nodes/ for a list of all
usable attributes.

.. versionadded:: 3.1???
"""

cdef class WallBase:
cdef shared_ptr[CxxWallBase] _wall
cdef CxxWallBase* wall
Expand Down
112 changes: 112 additions & 0 deletions interfaces/cython/cantera/reactor.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
self._outlets = []
self._walls = []
self._surfaces = []
self._edges = []
if isinstance(contents, _SolutionBase):
self.insert(contents) # leave insert for the time being

Expand Down Expand Up @@ -133,6 +134,11 @@
def __get__(self):
return self._surfaces

property edges:
"""List of reacting edges installed on this reactor"""
def __get__(self):
return self._edges

Check warning on line 140 in interfaces/cython/cantera/reactor.pyx

View check run for this annotation

Codecov / codecov/patch

interfaces/cython/cantera/reactor.pyx#L140

Added line #L140 was not covered by tests

def _add_inlet(self, inlet):
"""
Store a reference to ``inlet`` to prevent it from being prematurely
Expand Down Expand Up @@ -928,6 +934,112 @@
"""
self.surface.addSensitivityReaction(m)

cdef class ReactorEdge:
"""
Represents an edge in contact with the contents of a reactor.

:param kin:
The `Kinetics` or `Interface` object representing reactions on this
surface.
:param r:
The `Reactor` into which this surface should be installed.
:param L:
The length of the reacting edge [m]
:param node_attr:
Attributes to be passed to the ``node`` method invoked to draw this surface.
See https://graphviz.org/docs/nodes/ for a list of all usable attributes.

.. versionadded:: 3.1????
Added the ``node_attr`` parameter.
"""

def __cinit__(self):
self.edge = new CxxReactorEdge()

Check warning on line 957 in interfaces/cython/cantera/reactor.pyx

View check run for this annotation

Codecov / codecov/patch

interfaces/cython/cantera/reactor.pyx#L957

Added line #L957 was not covered by tests

def __dealloc__(self):
del self.edge

Check warning on line 960 in interfaces/cython/cantera/reactor.pyx

View check run for this annotation

Codecov / codecov/patch

interfaces/cython/cantera/reactor.pyx#L960

Added line #L960 was not covered by tests

def __init__(self, kin=None, Reactor r=None, *, L=None):
if kin is not None:
self.kinetics = kin
if r is not None:
self.install(r)
if L is not None:

Check warning on line 967 in interfaces/cython/cantera/reactor.pyx

View check run for this annotation

Codecov / codecov/patch

interfaces/cython/cantera/reactor.pyx#L963-L967

Added lines #L963 - L967 were not covered by tests
self.length = L
#self.node_attr = node_attr or {'shape': 'underline'}

def install(self, Reactor r):
"""
Add this `ReactorEdge` to the specified `Reactor`
"""
r._edges.append(self)
r.reactor.addEdge(self.edge)
self._reactor = r

Check warning on line 977 in interfaces/cython/cantera/reactor.pyx

View check run for this annotation

Codecov / codecov/patch

interfaces/cython/cantera/reactor.pyx#L975-L977

Added lines #L975 - L977 were not covered by tests

property length:
""" Length on which reactions can occur [m] """
def __get__(self):
return self.edge.length()

Check warning on line 982 in interfaces/cython/cantera/reactor.pyx

View check run for this annotation

Codecov / codecov/patch

interfaces/cython/cantera/reactor.pyx#L982

Added line #L982 was not covered by tests
def __set__(self, L):
self.edge.setLength(L)

Check warning on line 984 in interfaces/cython/cantera/reactor.pyx

View check run for this annotation

Codecov / codecov/patch

interfaces/cython/cantera/reactor.pyx#L984

Added line #L984 was not covered by tests

property kinetics:
"""
The `InterfaceKinetics` object used for calculating reaction rates on
this edge.
"""
def __get__(self):
self.edge.syncState()
return self._kinetics

Check warning on line 993 in interfaces/cython/cantera/reactor.pyx

View check run for this annotation

Codecov / codecov/patch

interfaces/cython/cantera/reactor.pyx#L993

Added line #L993 was not covered by tests
def __set__(self, Kinetics k):
self._kinetics = k
self.edge.setKinetics(self._kinetics.kinetics)

Check warning on line 996 in interfaces/cython/cantera/reactor.pyx

View check run for this annotation

Codecov / codecov/patch

interfaces/cython/cantera/reactor.pyx#L995-L996

Added lines #L995 - L996 were not covered by tests

@property
def reactor(self):
"""
Return the `Reactor` object the edge is connected to.

.. versionadded:: 3.1???
"""
return self._reactor

Check warning on line 1005 in interfaces/cython/cantera/reactor.pyx

View check run for this annotation

Codecov / codecov/patch

interfaces/cython/cantera/reactor.pyx#L1005

Added line #L1005 was not covered by tests

"""
def draw(self, graph=None, *, graph_attr=None, node_attr=None, surface_edge_attr=None,
print_state=False, **kwargs):

Draw the edge as a ``graphviz`` ``dot`` node connected to its reactor.
The node is added to an existing ``graph`` if provided.
Optionally include current reactor state in the reactor node.

:param graph:
``graphviz.graphs.BaseGraph`` object to which the reactor is added.
If not provided, a new ``DiGraph`` is created. Defaults to ``None``.
:param graph_attr:
Attributes to be passed to the ``graphviz.Digraph`` function that control
the general appearance of the drawn network.
See https://graphviz.org/docs/graph/ for a list of all usable attributes.
:param node_attr:
Attributes to be passed to the ``node`` method invoked to draw the reactor.
See https://graphviz.org/docs/nodes/ for a list of all usable attributes.
:param surface_edge_attr:
Attributes to be passed to the ``edge`` method invoked to draw the
connection between the surface and its reactor.
See https://graphviz.org/docs/edges/ for a list of all usable attributes.
:param print_state:
Whether state information of the reactor is printed into its node.
Defaults to ``False``
:param kwargs:
Additional keywords are passed on to ``~.drawnetwork.draw_reactor``.
:return:
``graphviz.graphs.BaseGraph`` object with edge and connected
reactor.

.. versionadded:: 3.1

return draw_edge(self, graph, graph_attr, node_attr, surface_edge_attr,
print_state, **kwargs)
"""

cdef class WallBase:
"""
Expand Down
1 change: 1 addition & 0 deletions interfaces/sourcegen/sourcegen/csharp/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class_crosswalk:
reactor: Reactor
reactornet: ReactorNet
reactorsurface: ReactorSurface
reactoredge: ReactorEdge
soln: Solution
surf: Surface
thermo: ThermoPhase
Expand Down
Loading
Loading