说明
cantera是一个
开源化学反应动力学分析软件
是chemkin的替代品之一
chemkin最早也是开源的,但是现在被ANSYS收购了,所以商业化了。
cantera类似一个库。这个软件没有GUI。但是可以在python中运行,作为Python的一个模块。也可以作为MATLAB的一个库来运行。
因为Python是开源的,本文就用Python。
安装
可以通过conda安装
安装好conda后在终端输入
conda create -n cantera -c cantera cantera ipython matplotlib
然后就是自动下载的过程。
这就创建了一个名为cantera的conda环境,使用时激活该环境就行了。
参考
https://jingyan.baidu.com/article/a3761b2be079961576f9aad9.html
官网安装地址,还有其他安装方式
https://cantera.org/install/index.html
运行官方示例
这里演示一个机理缩减的例子
官方示例源代码如下
https://cantera.org/examples/python/kinetics/mechanism_reduction.py.html
import cantera as ct
import numpy as np
import matplotlib.pyplot as plt
gas = ct.Solution('gri30.yaml')
initial_state = 1200, 5 * ct.one_atm, 'CH4:0.35, O2:1.0, N2:3.76'
# Run a simulation with the full mechanism
gas.TPX = initial_state
r = ct.IdealGasConstPressureReactor(gas)
sim = ct.ReactorNet([r])
tt = []
TT = []
t = 0.0
# Rmax is the maximum relative reaction rate at any timestep
Rmax = np.zeros(gas.n_reactions)
while t < 0.02:
t = sim.step()
tt.append(1000 * t)
TT.append(r.T)
rnet = abs(gas.net_rates_of_progress)
rnet /= max(rnet)
Rmax = np.maximum(Rmax, rnet)
plt.plot(tt, TT, label='K=53, R=325', color='k', lw=3, zorder=100)
# Get the reaction objects, and sort them so the most active reactions are first
R = sorted(zip(Rmax, gas.reactions()), key=lambda x: -x[0])
# Test reduced mechanisms with different numbers of reactions
C = plt.cm.winter(np.linspace(0, 1, 5))
for i, N in enumerate([40, 50, 60, 70, 80]):
# Get the N most active reactions
reactions = [r[1] for r in R[:N]]
# find the species involved in these reactions. At a minimum, include all
# species in the reactant mixture
species_names = {'N2', 'CH4', 'O2'}
for reaction in reactions:
species_names.update(reaction.reactants)
species_names.update(reaction.products)
# Get the species objects
species = [gas.species(name) for name in species_names]
# create the new reduced mechanism
gas2 = ct.Solution(thermo='IdealGas', kinetics='GasKinetics',
species=species, reactions=reactions)
# Re-run the ignition problem with the reduced mechanism
gas2.TPX = initial_state
r = ct.IdealGasConstPressureReactor(gas2)
sim = ct.ReactorNet([r])
t = 0.0
tt = []
TT = []
while t < 0.02:
t = sim.step()
tt.append(1000 * t)
TT.append(r.T)
plt.plot(tt, TT, lw=2, color=C[i],
label='K={0}, R={1}'.format(gas2.n_species, N))
plt.xlabel('Time (ms)')
plt.ylabel('Temperature (K)')
plt.legend(loc='upper left')
plt.title('Reduced mechanism ignition delay times\n'
'K: number of species; R: number of reactions')
plt.xlim(0, 20)
plt.tight_layout()
plt.show()
复制到一个Python文件里,然后运行
得到结果