Cantera is an open-source set of C++ codes used for calculating chemical kinetics, thermodynamic and
transport properties of reacting systems. The full functionality is only achieved by using the C++ codes, but
the majority of the features are available through the Python interface. This document will provide a very
basic introduction to using Cantera in the Python environment. Python is an interpreted language, like
Matlab, which means that you can either enter things directly at the command line, which can be inefficient,
or by creating a script.
Full
documentation
about
Cantera
can
be
found
at
http://cantera.github.io/devdocs/sphinx/html/index.html. One of the most useful things will be to look at the examples that are provided
with Cantera that can be found at, depending on your exact installation, c:\pyzo2015a\lib\sitepackages\cantera\examples.
Units
The unit system employed for Cantera is: temperature [K]; pressure [Pa]; mass [kg]; energy [J]; moles
[kmol]; volume [m3]; area [m2],
Setting up Python
In order to use Cantera in Python, you need to import the library so that it is available in Python. This is
done as:
import cantera as ct
This now enables the full use of Cantera functionality by using the ct. prefix as will be shown below.
In practice, it is best to include all of the external libraries that you will use in Python at the top of your
script. For ME 569, these include:
import sys
import csv
import string
import numpy as np
import matplotlib.pyplot as plt
General Overview
For the homework of ME 569 you will only be dealing with ideal gases although Cantera can handle real
gases and multi-phase mixtures. The general structure of Cantera is as follows:
1. A Solution object defines the chemical species available (and their thermochemical and transport
properties) and the chemical reaction mechanism. The Solution object only contains intensive
properties of the mixture (temperature, pressure, internal energy, etc.).
2. A Reactor describes a system that you can have interact with other reactors in ways that you specify.
A reactor is nothing by itself you need to insert a Solution object into the reactor and define the
boundary conditions for the Reactor. In addition to the Solution objects properties, the Reactor has
certain macroscopic properties such as Volume and Mass. A Reactor, however, does not consider
time.
3. The time evolution of a system is calculated by installing all of the Reactors into a Reactor Network.
This Reactor Network can be evolved in time to find the systems properties.
print(gas.report())
In order to ensure that you are reporting the properties on the mass/mole basis that you desire, you can use:
chemical_potentials; concentrations; cp_mass; cp_mole; cv_mass;
cv_mole; density_mass; density_mole; enthalpy_mass; enthalpy_mole;
entropy_mass; entropy_mole; gibbs_mass, gibbs_mole; int_energy_mass;
int_energy_mole; mean_molecular_weight; volume_mass; volume_mole
Information about the species that make up the gas mixture can be found from
name
returns
example
n_species
#of species
gas.n_species
species_names
gas.species_names
species_name(k)
name of species k
gas.species_name(5)
reaction_equation(k)
string of reaction k
gas.reaction_equation(13)
gas.species_index(oh)
n_reactions
gas.n_reactions
gas.X=a
print(gas.report())
Method 2
This method uses the assignment gas.X=string, where string has the form species_name:number
of moles; if multiple species are being specified they are separated by a comma. For a stoichiometric
methane-air mixture, the string would be CH4:1, O2:2, N2:7.52.
A=2
#O2:fuel mole ratio for methane
s='CH4:1,O2:', str(A), ',N2:', str(3.76*A)
z=''.join(s)
#concatenate the parts
gas.X=z
# set the mole fractions
print(gas.report())
Finally, you can set the thermodynamic state by a combination of the two-parameter combinations above
with either X or Y, e.g. gas.TPX sets the temperature, pressure and mole fractions.
Calculating Equilibrium
Once the initial thermodynamic state is set, it is very easy to find the equilibrium conditions (including
composition) given the choice of an appropriate constraint. This is accomplished by the following syntax
gas.equilibrate(AB)
where available constraints AB are:
TP constant temperature and pressure
TV constant temperature and volume
HP constant enthalpy and pressure
SP constant entropy and pressure
SV constant entropy and volume
UV constant internal energy and volume
1
500 1
P2
= 300 1
= 1774 K assuming a constant = 1:4
isentropic relation gives T2 = T1 P1
but at higher temperature decreases giving rise to the lower final temperature.
where is the overall heat transfer coefficient for conduction/convection, and is the emissivity. The
function
is a specified function of time. The heat flux is positive when heat flows from the reactor on
the left to the reactor on the right.
The syntax for defining a wall is
wall=ct. Wall(left, right, name=__, A=__, K=__, U=__, Q=__, velocity=__)
where the __ indicates an optional parameter:
By itself, you can not solve for the evolution of species in a reactor; it needs to be installed in a Reactor
Network, and the entire network is then solved. A Reactor Network containing reactors r1 and r2 is defined
as follows:
sim = ct.ReactorNet([r1, r2])
if there was only one reactor in the network, you would use sim = ct.ReactorNet([r1]). The
simulation of this reactor network starts by default at time zero. The simulation time can be found from
sim.time. The simulation can be advanced in time using two methods:
sim.advance(t) integrates the reaction mechanism up to time t. This will include several
intermediate time steps, but the results from these intermediate times is not accessible, only the final state
is returned.
sim.step(t) takes one internal step towards the time t. The size of the time step is determined by
Cantera. This method should be used when you are interested in short time-duration events.
Useful places to look for more information:
http://cantera.github.io/dev-docs/sphinx/html/reactors.html# [General equations solved]
http://cantera.github.io/dev-docs/sphinx/html/cython/zerodim.html# [Reactor syntax]