Tip of the Month 25.07 - Chemistry optimization tool

Reaction rate UDFs are easier to write using strings to reference reaction and species names defined in the inputs file. For example, consider an inputs file that contains the following fluid species and reaction scheme

fluid.species = CH4 O2 CO2 H2O N2
	
chemistry.solve = AdiabaticFlame
chemistry.AdiabaticFlame.reaction = CH4(g) + 2O2(g) --> CO2(g) + 2H2O(g)

and reaction definition containing

amrex::Real const c_O2  = fluid.molar_concentration("O2");   // [mol/m^3]
amrex::Real const c_CH4 = fluid.molar_concentration("CH4");  // [mol/m^3]
	
amrex::Real k0(2.0e-1); // reaction rate coefficient
	
reactions.rate("AdiabaticFlame") = k0 * c_O2 * c_CH4;

At runtime, a “look up” is done to convert the strings to indices. This is inefficient on CPUs and very inefficient on GPUs. Therefore, MFIX-Exa offers a python script to replace the strings with the species index values.

The first argument is the inputs file while the second is the reaction rates header file.

python3 ~/mfix/tools/Chemistry/optimize_usr_rates.py inputs mfix_usr_reactions_rates_K.H
	
    > Updating header file: mfix_usr_reactions_rates_K.H
    > Backup created: mfix_usr_reactions_rates_K_1753193988.H
    > Header file updated and saved to mfix_usr_reactions_rates_K.H

The optimized file will contain something similar to the following:

struct fluid_idx {
  enum { CH4 = 0, O2 = 1, CO2 = 2, H2O = 3, N2 = 4};
};
	
struct reactions_idx {
  enum {AdiabaticFlame = 0};
};

amrex::Real const c_O2  = fluid.molar_concentration(fluid_idx::O2);   // [mol/m^3]
amrex::Real const c_CH4 = fluid.molar_concentration(fluid_idx::CH4);  // [mol/m^3]

amrex::Real k0(2.0e-1); // reaction rate coefficient

reactions.rate(reactions_idx::AdiabaticFlame) = k0 * c_O2 * c_CH4;

Additonal information can be found in the chemistry section of the MFIX-Exa documentation.

2 Likes