Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions documentation/source/users/rmg/input.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
15 changes: 12 additions & 3 deletions rmgpy/rmg/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions rmgpy/rmg/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions rmgpy/solver/simple.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down
51 changes: 51 additions & 0 deletions test/rmgpy/rmg/inputTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down