mirror of
https://github.com/Cantera/cantera.git
synced 2025-02-25 18:55:29 -06:00
[Examples] Update reaction_path.py for Sphinx Gallery
This commit is contained in:
@@ -57,7 +57,7 @@ sphinx_gallery_conf = {
|
||||
'ignore_pattern': r'(__.*__\.py|test_examples\.m)',
|
||||
'image_srcset': ["2x"],
|
||||
'remove_config_comments': True,
|
||||
'ignore_repr_types': r'matplotlib\.(text|axes|legend)',
|
||||
'ignore_repr_types': r'(matplotlib\.(text|axes|legend)|graphviz\.(sources\.Source|graphs\.Graph|graphs\.Digraph))',
|
||||
'image_scrapers': ('matplotlib', ctutils.GraphvizScraper()),
|
||||
'examples_dirs': [
|
||||
'../samples/python/',
|
||||
|
||||
@@ -41,9 +41,9 @@ class GraphvizScraper():
|
||||
# a path to a file name that adheres to Sphinx-Gallery naming convention.
|
||||
image_path_iterator = block_vars['image_path_iterator']
|
||||
|
||||
# Define a list of our already-created figure objects.
|
||||
graph_types = (graphviz.Source, graphviz.graphs.Digraph, graphviz.graphs.Graph)
|
||||
for obj in block_vars["example_globals"].values():
|
||||
if isinstance(obj, graphviz.Source) and id(obj) not in self.processed:
|
||||
if isinstance(obj, graph_types) and id(obj) not in self.processed:
|
||||
self.processed.add(id(obj))
|
||||
image_path = Path(next(image_path_iterator)).with_suffix(".svg")
|
||||
obj.format = "svg"
|
||||
|
||||
@@ -7,17 +7,21 @@ and the program 'dot' must be on your path for this example to work.
|
||||
Graphviz can be obtained from https://www.graphviz.org/ or (possibly) installed
|
||||
using your operating system's package manager.
|
||||
|
||||
Requires: cantera >= 2.5.0
|
||||
Requires: cantera >= 2.5.0, graphviz
|
||||
|
||||
.. tags:: Python, kinetics, reaction path analysis, pollutant formation
|
||||
"""
|
||||
|
||||
from subprocess import run
|
||||
import sys
|
||||
from pathlib import Path
|
||||
import graphviz
|
||||
|
||||
import cantera as ct
|
||||
|
||||
# %%
|
||||
# Generate a Solution with a state where reactions are occurring
|
||||
# --------------------------------------------------------------
|
||||
#
|
||||
# these lines can be replaced by any commands that generate
|
||||
# an object of a class derived from class Kinetics in some state.
|
||||
gas = ct.Solution('gri30.yaml')
|
||||
@@ -29,12 +33,18 @@ while T < 1900:
|
||||
net.step()
|
||||
T = r.T
|
||||
|
||||
# %%
|
||||
# Create a reaction path diagram following nitrogen
|
||||
# -------------------------------------------------
|
||||
element = 'N'
|
||||
|
||||
diagram = ct.ReactionPathDiagram(gas, element)
|
||||
diagram.title = 'Reaction path diagram following {0}'.format(element)
|
||||
diagram.label_threshold = 0.01
|
||||
|
||||
# %%
|
||||
# Save the reaction path diagram
|
||||
# ------------------------------
|
||||
dot_file = 'rxnpath.dot'
|
||||
img_file = 'rxnpath.png'
|
||||
img_path = Path.cwd().joinpath(img_file)
|
||||
@@ -42,11 +52,12 @@ img_path = Path.cwd().joinpath(img_file)
|
||||
diagram.write_dot(dot_file)
|
||||
print(diagram.get_data())
|
||||
|
||||
print("Wrote graphviz input file to '{0}'.".format(Path.cwd().joinpath(dot_file)))
|
||||
print(f"Wrote graphviz input file to '{Path.cwd().joinpath(dot_file)}'.")
|
||||
|
||||
run('dot {0} -Tpng -o{1} -Gdpi=200'.format(dot_file, img_file).split())
|
||||
print("Wrote graphviz output file to '{0}'.".format(img_path))
|
||||
run(f"dot {dot_file} -Tpng -o{img_file} -Gdpi=200".split())
|
||||
print(f"Wrote graphviz output file to '{img_path}'.")
|
||||
|
||||
if "-view" in sys.argv:
|
||||
import webbrowser
|
||||
webbrowser.open(f"file:///{img_path}")
|
||||
# %%
|
||||
# View the reaction path diagram
|
||||
# ------------------------------
|
||||
graphviz.Source(diagram.get_dot())
|
||||
|
||||
Reference in New Issue
Block a user