0% found this document useful (0 votes)
91 views

Reactionpathdiagram: 1 Cantera Day 5

The document discusses generating reaction path diagrams using Cantera and Python. It describes setting up a gas mixture, running it in a reactor, and using Cantera to generate a reaction path diagram following a specific element. It provides code to define the gas, reactor, diagram, and to write the diagram to a file and generate an image file from the diagram.

Uploaded by

Parth Gaikwad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

Reactionpathdiagram: 1 Cantera Day 5

The document discusses generating reaction path diagrams using Cantera and Python. It describes setting up a gas mixture, running it in a reactor, and using Cantera to generate a reaction path diagram following a specific element. It provides code to define the gas, reactor, diagram, and to write the diagram to a file and generate an image file from the diagram.

Uploaded by

Parth Gaikwad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ReactionPathDiagram

November 4, 2021

[2]: from IPython.display import Image


Image(filename='Cantera.png',width=1000,height=400)
[2]:

1 Cantera Day 5
Author: Nikhil Verma

Research Scholar

Department of Mechanical Enginnering

Indian Institute of Science

Bangalore

2 Content:
2.1 Reaction Path Diagram
2.1.1 Reference: https://www.tilmanbremer.de/2017/06/tutorial-generating-
reaction-path-diagrams-with-cantera-and-python/

[30]: import os
import cantera as ct
from matplotlib import pyplot as plt

# Define a gas mixture at a high temperature that will undergo a reaction:


gas = ct.Solution('gri30.xml')

1
gas.TPX = 1500, 100000, 'CH4:0.05,O2:0.21,N2:0.79'

# Define a reactor, let it react until the temperature reaches 1800 K:


r = ct.IdealGasReactor(gas)
net = ct.ReactorNet([r])
T = r.T
while T <1800:
net.step()
T = r.T

# Define the element to follow in the reaction path diagram:


element = 'N'

# Initiate the reaction path diagram:


diagram = ct.ReactionPathDiagram(gas, element)

# Options for cantera:


diagram.show_details = False
diagram.font='CMU Serif Roman'
diagram.threshold=0.01
diagram.dot_options='node[fontsize=20,shape="box"]'
diagram.title = 'Reaction path diagram following {0}'.format(element)

# Define the filenames:


dot_file = 'ReactionPathDiagram.dot'
img_file = 'ReactionPathDiagram.png'

# Write the dot-file first, then create the image from the dot-file with␣
,→customizable

# parameters:
diagram.write_dot(dot_file)
# The last command requires dot to be in your system path variables, or your␣
,→system

# will not undersand the command "dot".


# The command -Tpng defines the filetype and needs to fit your filename defined␣
,→above,

# or else you will get errors opening the file later.


# The command -Gdpi sets the resolution of the generated image in dpi.
os.system('dot {0} -Gdpi=300 -Tpng -o{1}'.format(dot_file, img_file))
fig=plt.figure(figsize=(10, 10))
img = plt.imread(img_file)
plt.imshow(img)
plt.show()

2
[29]: import os
import cantera as ct
from matplotlib import pyplot as plt

# Define a gas mixture at a high temperature that will undergo a reaction:


gas = ct.Solution('gri30.xml')
gas.TPX = 1500, 100000, 'CH4:0.1,O2:0.21,N2:0.79'

# Define a reactor, let it react until the temperature reaches 1800 K:


r = ct.IdealGasReactor(gas)
net = ct.ReactorNet([r])
T = r.T
while T <1800:

3
net.step()
T = r.T

# Define the element to follow in the reaction path diagram:


element = 'C'

# Initiate the reaction path diagram:


diagram = ct.ReactionPathDiagram(gas, element)

# Options for cantera:


diagram.show_details = False
diagram.font='CMU Serif Roman'
diagram.threshold=0.01
diagram.dot_options='node[fontsize=20,shape="box"]'
diagram.title = 'Reaction path diagram following {0}'.format(element)

# Define the filenames:


dot_file = 'ReactionPathDiagram.dot'
img_file = 'ReactionPathDiagram.png'
modified_dot_file = 'ReactionPathDiagramModified.dot'
# Write the dot-file first, then create the image from the dot-file with␣
,→customizable

# parameters:
diagram.write_dot(dot_file)
# The last command requires dot to be in your system path variables, or your␣
,→system

# will not undersand the command "dot".


# The command -Tpng defines the filetype and needs to fit your filename defined␣
,→above,

# or else you will get errors opening the file later.


# The command -Gdpi sets the resolution of the generated image in dpi.

# Let's open the just created dot file and make so adjustments before␣
,→generating the image

# The dot_file is opened and read, the adjustements are saved in the␣
,→modified_dot_file:

with open(modified_dot_file, 'wt') as outfile:


with open(dot_file, 'rt') as infile:
for row in infile:
# Remove the line with the label:
if row.startswith(' label'):
row = ""
# Replace the color statements:
row = row.replace(', 0.9"', ', 0.0"')
outfile.write(row)

4
os.system('dot {0} -Gdpi=300 -Tpng -o{1}'.format(modified_dot_file, img_file))
fig=plt.figure(figsize=(10, 10))
img = plt.imread(img_file)
plt.imshow(img)
plt.show()

5
[ ]:

You might also like