Hello, I am trying to use a reaction solver package of Python called Cantera in Julia using PyCall. In the following code, I am trying to extract all reactions that contain “CO” and “CO2” in the reactant side and product side respectively. gas
is a Python Object with gas.reactions()
producing an array of Python Objects containing all the reactions. The reactants and products are defined as dictionaries and can be accessed for each reaction using .reactants
and .products
.
This is the code I wrote for the task at hand:
using PyCall
@pyimport cantera as ct
gas=ct.Solution("gri30.xml");
II = [i for (i,r) in enumerate(gas.reactions()) if "CO" in keys(r.reactants) && "CO2" in keys(r.products)]
for i in II
print("\n")
print(gas.reaction(i).equation)
end
Surprisingly, I get the following output:
HCO + O <=> CO + OH
CH2O + O2 <=> HCO + HO2
HCO + OH <=> CO + H2O
CH2O + HO2 <=> H2O2 + HCO
However, if I change the i
inside the array II
to i-1
II = [i-1 for (i,r) in enumerate(gas.reactions()) if "CO" in keys(r.reactants) && "CO2" in keys(r.products)]
for i in II
print("\n")
print(gas.reaction(i).equation)
end
it works perfectly well:
CO + O (+M) <=> CO2 (+M)
CO + O2 <=> CO2 + O
CO + OH <=> CO2 + H
CO + HO2 <=> CO2 + OH
Any insights on why such a thing happens? My initial guess was python arrays start with 0 while Julia arrays start with 1.