Skip to content

Commit

Permalink
[Reactor] Allow drawing contents for full ReactorNet
Browse files Browse the repository at this point in the history
  • Loading branch information
speth committed Jun 19, 2024
1 parent 667a2ae commit 991f4df
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
28 changes: 18 additions & 10 deletions interfaces/cython/cantera/drawnetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ def draw_reactor(r, graph=None, graph_attr=None, node_attr=None, print_state=Fal
def draw_reactor_net(n, graph_attr=None, node_attr=None, edge_attr=None,
heat_flow_attr=None, mass_flow_attr=None,
moving_wall_edge_attr=None, surface_edge_attr=None,
print_state=False, show_wall_velocity=True, **kwargs):
show_wall_velocity=True, print_state=False, species=None,
species_units="percent"):
"""
See `.ReactorNet.draw`.
Expand All @@ -119,22 +120,26 @@ def draw_reactor_net(n, graph_attr=None, node_attr=None, edge_attr=None,
for name, group in reactor_groups.items():
sub = _graphviz.Digraph(name=f"cluster_{name}", graph_attr=graph_attr)
for r in group:
draw_reactor(r, sub, print_state=print_state, **kwargs)
draw_reactor(r, sub, print_state=print_state, species=species,
species_units=species_units)
drawn_reactors.add(r)
flow_controllers.update(r.inlets + r.outlets)
walls.update(r.walls)
for surface in r.surfaces:
draw_surface(surface, sub, print_state=print_state, **kwargs)
draw_surface(surface, sub, print_state=print_state, species=species,
species_units=species_units)
sub.attr(label=name)
graph.subgraph(sub)
reactors -= drawn_reactors

for r in reactors:
draw_reactor(r, graph, print_state=print_state, **kwargs)
draw_reactor(r, graph, print_state=print_state, species=species,
species_units=species_units)
flow_controllers.update(r.inlets + r.outlets)
walls.update(r.walls)
for surface in r.surfaces:
draw_surface(surface, graph, print_state=print_state, **kwargs)
draw_surface(surface, graph, print_state=print_state, species=species,
species_units=species_units)

# some Reactors or Reservoirs only exist as connecting nodes
connected_reactors = set()
Expand All @@ -151,29 +156,32 @@ def draw_reactor_net(n, graph_attr=None, node_attr=None, edge_attr=None,
# remove already drawn reactors and draw new reactors
connected_reactors -= drawn_reactors
for r in connected_reactors:
draw_reactor(r, graph, print_state=print_state, **kwargs)
draw_reactor(r, graph, print_state=print_state, species=species,
species_units=species_units)

fc_edge_attr = {**(edge_attr or {}), **(mass_flow_attr or {})}
draw_flow_controllers(flow_controllers, graph, edge_attr=fc_edge_attr, **kwargs)
draw_flow_controllers(flow_controllers, graph, edge_attr=fc_edge_attr)
w_edge_attr = {**(edge_attr or {}), "color": "red", "style": "dashed",
**(heat_flow_attr or {})}
draw_walls(walls, graph, edge_attr=w_edge_attr,
moving_wall_edge_attr=moving_wall_edge_attr,
show_wall_velocity=show_wall_velocity, **kwargs)
show_wall_velocity=show_wall_velocity)

return graph


def draw_surface(surface, graph=None, graph_attr=None, node_attr=None,
surface_edge_attr=None, print_state=False, **kwargs):
surface_edge_attr=None, print_state=False, species=None,
species_units="percent"):
"""
See `.ReactorSurface.draw`.
.. versionadded:: 3.1
"""

r = surface.reactor
graph = draw_reactor(r, graph, graph_attr, node_attr, print_state, **kwargs)
graph = draw_reactor(r, graph, graph_attr, node_attr, print_state, species=species,
species_units=species_units)
name = f"{r.name} surface"
edge_attr = {"style": "dotted", "arrowhead": "none",
**(surface_edge_attr or {})}
Expand Down
40 changes: 27 additions & 13 deletions interfaces/cython/cantera/reactor.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -884,8 +884,9 @@ cdef class ReactorSurface:
"""
return self._reactor

def draw(self, graph=None, *, graph_attr=None, node_attr=None, surface_edge_attr=None,
print_state=False, **kwargs):
def draw(self, graph=None, *, graph_attr=None, node_attr=None,
surface_edge_attr=None, print_state=False, species=None,
species_units="percent"):
"""
Draw the surface as a ``graphviz`` ``dot`` node connected to its reactor.
The node is added to an existing ``graph`` if provided.
Expand All @@ -908,16 +909,22 @@ cdef class ReactorSurface:
: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``.
:param species:
If ``print_state`` is ``True``, define how species are to be printed.
Options are ``'X'`` and ``'Y'`` for mole and mass fractions of all species,
respectively, or an iterable that contains the desired species names as
strings. Defaults to ``None``.
:param species_units:
Defines the units the species are displayed in as either ``"percent"`` or
``"ppm"``. Defaults to ``"percent"``.
:return:
``graphviz.graphs.BaseGraph`` object with surface and connected
reactor.
.. versionadded:: 3.1
"""
return draw_surface(self, graph, graph_attr, node_attr, surface_edge_attr,
print_state, **kwargs)
print_state, species, species_units)

def add_sensitivity_reaction(self, int m):
"""
Expand Down Expand Up @@ -2017,8 +2024,8 @@ cdef class ReactorNet:

def draw(self, *, graph_attr=None, node_attr=None, edge_attr=None,
heat_flow_attr=None, mass_flow_attr=None, moving_wall_edge_attr=None,
surface_edge_attr=None, print_state=False, show_wall_velocity=True,
**kwargs):
surface_edge_attr=None, show_wall_velocity=True, print_state=False,
species=None, species_units="percent"):
"""
Draw as ``graphviz.graphs.DiGraph``. Connecting flow controllers and
walls are depicted as arrows.
Expand Down Expand Up @@ -2050,18 +2057,25 @@ cdef class ReactorNet:
Same as ``edge_attr`` but only applied to edges representing connections
between `ReactorSurface` objects and reactors.
Default is ``{"style": "dotted", "arrowhead": "none"}``.
:param show_wall_velocity:
If ``True``, wall movement will be indicated by additional arrows with the
corresponding wall velocity as a label.
:param print_state:
Whether state information of the reactors is printed into each node.
Defaults to ``False``.
:param kwargs:
Additional keywords are passed on to each call of
`~.drawnetwork.draw_reactor`, `~.drawnetwork.draw_surface`,
`~.drawnetwork.draw_flow_controllers`, and `~.drawnetwork.draw_walls`.
:param species:
If ``print_state`` is ``True``, define how species are to be printed.
Options are ``'X'`` and ``'Y'`` for mole and mass fractions of all species,
respectively, or an iterable that contains the desired species names as
strings. Defaults to ``None``.
:param species_units:
Defines the units the species are displayed in as either ``"percent"`` or
``"ppm"``. Defaults to ``"percent"``.
:return:
``graphviz.graphs.BaseGraph`` object with reactor net.
.. versionadded:: 3.1
"""
return draw_reactor_net(self, graph_attr, node_attr, edge_attr, heat_flow_attr,
mass_flow_attr, moving_wall_edge_attr, surface_edge_attr, print_state,
**kwargs)
mass_flow_attr, moving_wall_edge_attr, surface_edge_attr,
show_wall_velocity, print_state, species, species_units)

0 comments on commit 991f4df

Please sign in to comment.