网页 综合 更新时间 2026-11-14 10:01:43

Python Tutorial — Cantera 3.2.0 documentation

自动蜘蛛 cantera.org 正文约 6000 字
If you haven’t already installed Cantera, we have instructions for many platforms in our Installation section.
Start by opening an interactive Python session, for example by running IPython. Import the Cantera Python module and NumPy by running:
When using Cantera, the first thing you usually need is an object representing: some phase of matter. Here, we’ll create a gas mixture
To view the state of the mixture, call the gas1 object as if it were a function:
What you have just done is to create an object, gas1 that implements GRI-Mech 3.0, the 53-species, 325-reaction natural gas combustion mechanism developed by Smith et al. [1999].
The gas1 object has properties you would expect for a gas mixture - it has a temperature, a pressure, species mole and mass fractions, etc. As we’ll soon see, it has many more properties.
The summary of the state of gas1 printed above shows that new objects created from the gri30.yaml input file start out with a temperature of 300 K, a pressure of 1 atm, and have a composition that consists of only one species, in this case hydrogen. There is nothing special about H2 - it just happens to be the first species listed in the input file defining GRI-Mech 3.0. In general, whichever species is listed first will initially have a mole fraction of 1.0, and all of the others will be zero.
The state of the object can easily be changed. For example:
sets the temperature to 1200 K and the pressure to 101325 Pa (Cantera always uses SI units). After this statement, calling gas1() results in:
Thermodynamics generally requires that two properties in addition to composition information be specified to fix the intensive state of a substance (or mixture). The state of the mixture can be set using several combinations of two properties. The following are all equivalent:
In each case, the values of the extensive properties must be entered per unit mass.
Properties may be read independently or together:
The composition can be set in terms of either mole fractions (X) or mass fractions (Y):
Mass and mole fractions can also be set using dict objects, which is convenient in cases where the composition is stored in a variable or being computed:
When the composition alone is changed, the temperature and density are held constant. This means that the pressure and other intensive properties will change. The composition can also be set in conjunction with the intensive properties of the mixture:
The composition above was specified using a string. The format is a comma-separated list of <species name>:<relative mole numbers> pairs. The mole numbers will be normalized to produce the mole fractions, and therefore they are “relative” mole numbers. Mass fractions can be set in this way too by changing X to Y in the above statements.
The composition can also be set using an array, which must have the same size as the number of species. For example, to set all 53 mole fractions to the same value, do this:
Or, to set all the mass fractions to equal values:
When setting the state, you can control what properties are held constant by passing the special value None to the property setter. For example, to change the specific volume to 2.1 m\(^3\)/kg while holding entropy constant:
Or to set the mass fractions while holding temperature and pressure constant:
Many properties of a Solution provide values for each species present in the phase. If you want to get values only for a subset of these species, you can use Python’s “slicing” syntax to select data for just the species of interest. To get the mole fractions of just the major species in gas1, in the order specified, you can write:
If you want to use the same set of species repeatedly, you can keep a reference to the sliced phase object:
The slice object and the original object share the same internal state, so modifications to one will affect the other.
In previous example, we created an object that models an ideal gas mixture with the species and reactions of GRI-Mech 3.0, using the gri30.yaml input file included with Cantera. Several other reaction mechanism files are included with Cantera, including ones that model high- temperature air, a hydrogen/oxygen reaction mechanism, and a few surface reaction mechanisms. These files are usually located in the data subdirectory of the Cantera installation directory, for example C:\Program Files\Cantera\data on Windows or /usr/local/cantera/data/ on Unix/Linux/Mac OS X machines, depending on how you installed Cantera and the options you specified.
If for some reason Cantera has difficulty finding where these files are on your system, set environment variable CANTERA_DATA to the directory or directories (separated using ; on Windows or : on other operating systems) where they are located. Alternatively, you can call function add_data_directory() to add a directory to the Cantera search path:
Cantera input files are plain text files, and can be created with any text editor. See the page Introduction to YAML Input Files for more information.
A Cantera input file may contain more than one phase specification, and may contain specifications of interfaces (surfaces and edges). Here we import a surface phase and the two adjacent bulk phases from the file diamond.yaml:
See the page Converting Chemkin Format Files for information on how to convert from CK-format to Cantera’s YAML format.
In addition to the Sphinx-generated Python documentation, documentation of the Python classes and their methods can be accessed from within the Python interpreter as well.
Suppose you have created a Cantera object and want to know what methods are available for it, and get help on using the methods:
To get help on the Python class that this object is an instance of:
Show code cell output
Hide code cell output
For a simple list of the properties and methods of this object:
Show code cell output
Hide code cell output
To get help on a specific method, such as the species_index method:
For properties, getting the documentation is slig
访问原文 返回搜索 https://cantera.org/stable/userguide/python-tutorial.html
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。