Build reverse electronic reaction into a Cantera YAML file.#

Use ReverseTwoTemperaturePlasma to compute reverse reaction rates of electronic reactions.

Tags: kinetics cantera electron reaction reverse reaction

Import the required libraries.#

from pathlib import Path

import cantera as ct

from rizer import __version__
from rizer.kin.extensible_rate import (
    # Import extensible rate here, so that Cantera can parse yaml.
    ReverseTwoTemperaturePlasma,  # noqa: F401
    to_reverse_reaction_format,
)
from rizer.misc.utils import get_path_to_data, get_root

Load the mechanism.#

# Set to True to create reverse rate for electron excitation reaction only.
only_excited_state = True

# Mechanism with only forward reactions.
name = "CH4_to_C2H2_forward_reactions.yaml"
if only_excited_state:
    name = "electronic_excited_states/C_electronic_excitation_reactions.yaml"

forward_mechanism = get_path_to_data(
    "mechanisms",
    "Goutier2025",
    "builder",
    name,
)

name = "electron_reverse_reactions.yaml"
if only_excited_state:
    name = "electronic_excited_states/electron_heavy_backward_reactions_carbon_excited_states.yaml"

# Mechanism with only backward reactions.
backward_mechanism = get_path_to_data(
    "mechanisms",
    "Goutier2025",
    "builder",
    name,
    force_return=True,
)

# Load the gas.
gas = ct.Solution(forward_mechanism, name="gas", transport_model=None)

Write the reverse reaction.#

text = "description: |-\n"
text += r"  Reactions below are the reverse rate constant for each reaction in the following mechanism:\n"
relative_path_forward_mechanism = Path(forward_mechanism).relative_to(get_root())
text += f"{relative_path_forward_mechanism}\n"
text += r"""
  The reverse rate constant is computed using the detailed balance relation.
  The detailed balance relation states that the forward and backward rate constants are related by:

    k_b = k_f / K_eq

  where:

  - k_b is the backward rate constant,
  - k_f is the forward rate constant,
  - K_eq is the equilibrium constant.

  The equilibrium constant is computed using the Gibbs free energy:

    K_eq = exp(-ΔG / (R * T))

  where ΔG is the Gibbs free energy change, R is the universal gas constant, and T is the temperature.

  We use the Cantera function `equilibrium_constants` to compute the equilibrium constant
  and the forward rate constant. The backward rate constant is then computed using the detailed balance
  relation. This formula holds for all reactions, including those with electrons.
"""
text += f"  Rizer version: {__version__}\n"
relative_path = Path(__file__).relative_to(get_root().parent)
text += f"  Script: {relative_path}.\n"
text += "  >>>>> Do not edit this file manually. <<<<<\n"
text += "units: {length: cm, time: s, quantity: mol, activation-energy: kJ/mol}\n\n"
text += "reactions:\n"

for j, reaction in enumerate(gas.reactions()):
    text += to_reverse_reaction_format(
        forward_reaction=reaction,
        note="...",
        gas=gas,
    )

# Write the electronic reactions to the file.
output_file = backward_mechanism
print(f"Writing to {output_file}...")
with open(output_file, "w", encoding="utf-8") as f:
    f.write(text)
print(f"File written to {output_file}!")