Skip to content

Keep the balanceSpecies of each reactor - #3043

Open
natozenbilek wants to merge 1 commit into
ReactionMechanismGenerator:mainfrom
natozenbilek:per-reactor-balance-species
Open

natozenbilek wants to merge 1 commit into
ReactionMechanismGenerator:mainfrom
natozenbilek:per-reactor-balance-species

Conversation

@natozenbilek

@natozenbilek natozenbilek commented Nov 22, 2026 •

Copy link
Copy Markdown

Motivation or Problem

Fixes #2755. balanceSpecies is given per reactor block, but input.py stored it in a single RMG.balance_species that every simpleReactor, constantVIdealGasReactor and constantTPIdealGasReactor block overwrote. main.py then passed that one value to the RMG_Memory of every reactor, so all ranged reactors were balanced with the species of the last block (or with none, if that block had none). If that species was not in a reactor's initialMoleFractions, RMG_Memory.__init__ raised an IndexError. A balanceSpecies missing from its own block's initialMoleFractions failed with an UnboundLocalError.

Description of Changes

  • rmgpy/solver/simple.pyx: new cdef public str balance_species on SimpleReactor (default None).
  • rmgpy/rmg/input.py: the three reactor functions set system.balance_species instead of rmg.balance_species, and raise an InputError when balanceSpecies is not in initialMoleFractions.
  • rmgpy/rmg/main.py: RMG.balance_species removed; each RMG_Memory gets getattr(reaction_system, "balance_species", None), so reactor types without a balanceSpecies get None as before.
  • test/rmgpy/rmg/inputTest.py: two tests (each reactor keeps its own value; missing species raises InputError).
  • documentation/source/users/rmg/input.rst: one sentence saying the balance species applies to its own reactor.

Testing

Run in the reactionmechanismgenerator/rmg:latest image, whose /rmg/RMG-Py is at 032e9d20e, the base of this branch. The five files this PR changes are the same there as on main. After copying the changed files, python setup.py build_ext --inplace rebuilt only rmgpy/solver/simple.pyx.

  • The two new tests fail on main (AttributeError: ... has no attribute 'balance_species', and UnboundLocalError for xbspcs) and pass on this branch.
  • pytest test/rmgpy/rmg/inputTest.py test/rmgpy/solver: 75 passed, 1 skipped.
  • pytest test/rmgpy/rmg/mainTest.py (its input has a ranged simpleReactor with balanceSpecies='N2' and two liquidReactors): 19 passed.
  • Small H2/O2 input with two ranged simpleReactors (nSims=3), run with rmg.py:
    • Reactor 0 {'H2': [0.2, 0.3], 'O2': 0.1, 'N2': 0.7} with balanceSpecies='N2', reactor 1 the same with Ar instead of N2 and balanceSpecies='Ar': on main it stops with IndexError in RMG_Memory.__init__. On this branch it completes, and reactor 0 adjusts N2 while reactor 1 adjusts Ar (e.g. H2 = 0.2158, O2 = 0.1, N2 = 0.6842 and H2 = 0.2158, O2 = 0.1, Ar = 0.6842).
    • Both reactors with N2 and Ar (N2 0.4, Ar 0.3 and N2 0.3, Ar 0.4): on main both adjust Ar, so reactor 0 logs N2 = 0.4, Ar = 0.2842. On this branch reactor 0 logs N2 = 0.3842, Ar = 0.3 and reactor 1 still adjusts Ar.
    • Same as the first case, but without balanceSpecies in reactor 1: on main reactor 0 is not balanced with N2 (its chosen conditions only list H2). On this branch it adjusts N2.

Reviewer Tips

simple.pyx changes, so the extension has to be rebuilt (make build).

balanceSpecies is given per reactor block in the input file, but it was
stored in a single RMG.balance_species attribute that every simpleReactor,
constantVIdealGasReactor and constantTPIdealGasReactor block overwrote.
That one value was then passed to the RMG_Memory of every reactor, so all
ranged reactors were balanced with the balanceSpecies of the last of these
blocks (or with none, if that block had none). If that species was not in
the initialMoleFractions of a reactor, RMG stopped with an IndexError in
RMG_Memory.__init__.

Store the balance species on the reaction system and pass each reactor's
own value to RMG_Memory. Reactor types that do not take a balanceSpecies
get None. Also raise an InputError when balanceSpecies is not one of the
species in initialMoleFractions, which used to fail with an
UnboundLocalError.

Fixes ReactionMechanismGenerator#2755
@natozenbilek
natozenbilek marked this pull request as ready for review November 22, 2026 21:53

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Only One Balance Species

1 participant