From 1e58b4db5fd37b3c2f9d5ccc01c04288b4d14cdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nezih=20Arhan=20T=C3=B6zenbilek?= Date: Sun, 22 Nov 2026 23:58:08 +0300 Subject: [PATCH] Keep the balanceSpecies of each reactor 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 #2755 --- documentation/source/users/rmg/input.rst | 2 + rmgpy/rmg/input.py | 15 +++++-- rmgpy/rmg/main.py | 3 +- rmgpy/solver/simple.pyx | 2 + test/rmgpy/rmg/inputTest.py | 51 ++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 5 deletions(-) diff --git a/documentation/source/users/rmg/input.rst b/documentation/source/users/rmg/input.rst index 16dc4ffce5f..6aa1056f802 100644 --- a/documentation/source/users/rmg/input.rst +++ b/documentation/source/users/rmg/input.rst @@ -512,6 +512,8 @@ For gas phase reactors if normalization of the ranged mole fractions is undesira fractions needs to be kept constant) one can use a ``balanceSpecies``. When a ``balanceSpecies`` is used instead of normalizing the mole fractions the concentration of the defined ``balanceSpecies`` is adjusted to maintain an overall mole fraction of one. This ensures that all species except the ``balanceSpecies`` have mole fractions within the range specified. +A ``balanceSpecies`` applies only to the reactor it is given in, and it has to be one of the species in the +``initialMoleFractions`` of that reactor. .. _simulatortolerances: diff --git a/rmgpy/rmg/input.py b/rmgpy/rmg/input.py index 8ba5c23d6d6..43f9b3ae8d3 100644 --- a/rmgpy/rmg/input.py +++ b/rmgpy/rmg/input.py @@ -478,8 +478,11 @@ def simple_reactor(temperature, rmg.reaction_systems.append(system) assert balanceSpecies is None or isinstance(balanceSpecies, str), 'balanceSpecies should be the string corresponding to a single species' - rmg.balance_species = balanceSpecies + system.balance_species = balanceSpecies if balanceSpecies: # check that the balanceSpecies can't be taken to zero + if balanceSpecies not in initialMoleFractions: + raise InputError('balanceSpecies {0} must be one of the species in the initialMoleFractions of the same ' + 'reactor'.format(balanceSpecies)) total = 0.0 for key, item in initialMoleFractions.items(): if key == balanceSpecies: @@ -579,8 +582,11 @@ def constant_V_ideal_gas_reactor(temperature, rmg.reaction_systems.append(system) assert balanceSpecies is None or isinstance(balanceSpecies, str), 'balanceSpecies should be the string corresponding to a single species' - rmg.balance_species = balanceSpecies + system.balance_species = balanceSpecies if balanceSpecies: # check that the balanceSpecies can't be taken to zero + if balanceSpecies not in initialMoleFractions: + raise InputError('balanceSpecies {0} must be one of the species in the initialMoleFractions of the same ' + 'reactor'.format(balanceSpecies)) total = 0.0 for key, item in initialMoleFractions.items(): if key == balanceSpecies: @@ -680,8 +686,11 @@ def constant_TP_ideal_gas_reactor(temperature, rmg.reaction_systems.append(system) assert balanceSpecies is None or isinstance(balanceSpecies, str), 'balanceSpecies should be the string corresponding to a single species' - rmg.balance_species = balanceSpecies + system.balance_species = balanceSpecies if balanceSpecies: # check that the balanceSpecies can't be taken to zero + if balanceSpecies not in initialMoleFractions: + raise InputError('balanceSpecies {0} must be one of the species in the initialMoleFractions of the same ' + 'reactor'.format(balanceSpecies)) total = 0.0 for key, item in initialMoleFractions.items(): if key == balanceSpecies: diff --git a/rmgpy/rmg/main.py b/rmgpy/rmg/main.py index eaa4a732eba..2f79b8675e3 100644 --- a/rmgpy/rmg/main.py +++ b/rmgpy/rmg/main.py @@ -215,7 +215,6 @@ def clear(self): self.model_settings_list = [] self.simulator_settings_list = [] - self.balance_species = None self.filter_reactions = False self.init_react_tuples = [] @@ -891,7 +890,7 @@ def execute(self, initialize=True, **kwargs): # Initiate first reaction discovery step after adding all core species for index, reaction_system in enumerate(self.reaction_systems): # Initialize memory object to track conditions for ranged reactors - self.rmg_memories.append(RMG_Memory(reaction_system, self.balance_species)) + self.rmg_memories.append(RMG_Memory(reaction_system, getattr(reaction_system, "balance_species", None))) self.rmg_memories[index].generate_cond() log_conditions(self.rmg_memories, index) diff --git a/rmgpy/solver/simple.pyx b/rmgpy/solver/simple.pyx index f446443f0c9..b962032d5fe 100644 --- a/rmgpy/solver/simple.pyx +++ b/rmgpy/solver/simple.pyx @@ -106,6 +106,7 @@ cdef class SimpleReactor(ReactionSystem): cdef public np.ndarray pdep_specific_collider_reaction_indices cdef public dict sens_conditions + cdef public str balance_species cdef public list Trange cdef public list Prange @@ -142,6 +143,7 @@ cdef class SimpleReactor(ReactionSystem): self.specific_collider_species = None self.sens_conditions = sens_conditions self.n_sims = n_sims + self.balance_species = None # set by the input file, see rmgpy.rmg.input def __reduce__(self): """ diff --git a/test/rmgpy/rmg/inputTest.py b/test/rmgpy/rmg/inputTest.py index 1daf399cb23..221ce19d77a 100644 --- a/test/rmgpy/rmg/inputTest.py +++ b/test/rmgpy/rmg/inputTest.py @@ -358,6 +358,57 @@ def test_simple_reactor_mole_fractions_ranged(self): assert reactor.initial_mole_fractions["B"] == 3 assert reactor.initial_mole_fractions["C"] == 2 + def test_simple_reactor_balance_species(self): + """Test that each SimpleReactor keeps its own balanceSpecies""" + inp.simple_reactor( + temperature=(1000, "K"), + pressure=(1, "atm"), + initialMoleFractions={ + "A": [0.1, 0.2], + "B": 0.8, + }, + terminationTime=(1, "s"), + balanceSpecies="B", + ) + inp.simple_reactor( + temperature=(1000, "K"), + pressure=(1, "atm"), + initialMoleFractions={ + "A": [0.1, 0.2], + "C": 0.8, + }, + terminationTime=(1, "s"), + balanceSpecies="C", + ) + inp.simple_reactor( + temperature=(1000, "K"), + pressure=(1, "atm"), + initialMoleFractions={ + "A": 0.2, + "B": 0.8, + }, + terminationTime=(1, "s"), + ) + + global rmg + assert rmg.reaction_systems[0].balance_species == "B" + assert rmg.reaction_systems[1].balance_species == "C" + assert rmg.reaction_systems[2].balance_species is None + + def test_simple_reactor_balance_species_not_in_mole_fractions(self): + """Test that a balanceSpecies missing from initialMoleFractions raises an InputError""" + with pytest.raises(InputError): + inp.simple_reactor( + temperature=(1000, "K"), + pressure=(1, "atm"), + initialMoleFractions={ + "A": [0.1, 0.2], + "B": 0.8, + }, + terminationTime=(1, "s"), + balanceSpecies="C", + ) + def test_liquid_reactor_concentrations(self): """Test that LiquidReactor concentrations are set properly""" inp.liquid_reactor(