Calling chemical kinetics solvers like CHEMKIN or Cantera from Julia

Hello Everyone. I am relatively new to Julia and I am using it to solve a chemically-reacting flow problem that requires extensive reaction-rate and species thermodynamic-data. I came across one such open-source software called Cantera, that has most of the features I am looking for. Note that it has pre-defined mechanisms, reaction-rate data, and thermodynamic data for some common reaction systems.
I am looking for either:

  1. Coupling a solver like Cantera (written in C++ & Python) with Julia and use it along with Julia. Cantera’s official documentation says it can be used with applications written in C++/C and Fortran90 and already existing support with MATLAB and Python.
  2. Another similar library/package in Julia like Cantera. After some exploration initially, I came across SciML’s Catalyst.jl. However, after going through the documentation, I learn that it is a chemical-kinetics solver coupled with Julia’s ODE solvers (DifferentialEquations.jl) for which I need reaction rate and thermodynamic data beforehand.

Any suggestions on how to couple Cantera with Julia or any existing support that exists for Julia?

Step 1: Go to the Cantera Downloads page and install Cantera in your system using Python installation. Follow the steps in the website as is and test if Cantera works in the Python IDLE. Copy the location of the python.exe.
Step 2: Open the Julia interpreter/REPL in your IDE (I used Juno) and use the following commands to add PyCall.

julia>>using Pkg
julia>>Pkg.add("PyCall")

Step 3: Once Julia is done updating its registries, update the local python environment that has the Cantera installation using the following commands:

julia>>using PyCall
julia>>ENV["PYTHON"] = "location of the python.exe"

(The location should look something like C:\Users\hi\AppData\Local\Programs\Python\Python37)
Step 4: Rebuild PyCall using:

julia>>using Pkg
julia>>Pkg.build("PyCall")

Step 5: STOP Julia and restart.
Step 6: Test run Cantera in the Julia REPL using the following commands


julia>>using PyCall
julia>>pyimport("cantera")
PyObject <cantera.composite.Solution object at 0x000000004581D908>
julia>>gas1=cantera.Solution("gri30.xml")
gri30:

       temperature             300  K
          pressure          101325  Pa
           density       0.0818891  kg/m^3
  mean mol. weight         2.01588  amu

                          1 kg            1 kmol
                       -----------      ------------
          enthalpy           26470        5.336e+04     J
   internal energy     -1.2109e+06       -2.441e+06     J
           entropy           64914        1.309e+05     J/K
    Gibbs function     -1.9448e+07        -3.92e+07     J
 heat capacity c_p           14312        2.885e+04     J/K
 heat capacity c_v           10187        2.054e+04     J/K

                           X                 Y          Chem. Pot. / RT
                     -------------     ------------     ------------
                H2              1                1         -15.7173
     [  +52 minor]              0                0

NOTE: Pls note that julia uses double quotes instead of single quotes inside the Solution() function. The python version works with single quotes.
Julia throws the following error if single quotes are used.
ERROR: syntax: character literal contains multiple characters

2 Likes

The test run step did not work for me in 2023, Julia 1.8.2.
Instead I had to write:

julia> using PyCall

julia> ct = pyimport("cantera")
PyObject <module 'cantera' from 'C:\\ .... \\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\cantera\\__init__.py'>

julia> gas1=ct.Solution("gri30.yaml")
PyObject <cantera.composite.Solution object at 0x0000000061CEFAC0>

julia> gas1()

  gri30:

       temperature   300 K
          pressure   1.0133e+05 Pa
           density   0.081894 kg/m^3
  mean mol. weight   2.016 kg/kmol
   phase of matter   gas

                          1 kg             1 kmol
                     ---------------   ---------------
          enthalpy             26469             53361  J
   internal energy       -1.2108e+06        -2.441e+06  J
           entropy             64910        1.3086e+05  J/K
    Gibbs function       -1.9447e+07       -3.9204e+07  J
 heat capacity c_p             14311             28851  J/K
 heat capacity c_v             10187             20536  J/K

                      mass frac. Y      mole frac. X     chem. pot. / RT
                     ---------------   ---------------   ---------------
                H2                 1                 1           -15.717
     [  +52 minor]                 0                 0


julia>```
1 Like