Skip to content

Commit 495a5b0

Browse files
authored
Merge pull request #2867 to fix constant species index mapping in SimpleReactor
Correct constant-species index mapping in SimpleReactor
2 parents 8470953 + 5623427 commit 495a5b0

3 files changed

Lines changed: 40 additions & 9 deletions

File tree

rmgpy/rmg/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2328,7 +2328,7 @@ def calculate_cond(self, obj, Ndims, Ns=20):
23282328
Jout /= tot # normalize Jout
23292329
n = self.rand_state.uniform(0, 1, 1)[0] # draw a random number between 0 and 1
23302330
s = 0.0
2331-
for indexes in np.ndenumerate(Jout): # choose a coordinate such that grid[indexes] is choosen with probability Jout[indexes]
2331+
for indexes in np.ndenumerate(Jout): # choose a coordinate such that grid[indexes] is chosen with probability Jout[indexes]
23322332
s += Jout[indexes[0]]
23332333
if s > n:
23342334
break
@@ -2402,7 +2402,7 @@ def log_conditions(rmg_memories, index):
24022402
log newly generated reactor conditions
24032403
"""
24042404
if rmg_memories[index].get_cond() is not None:
2405-
s = "conditions choosen for reactor {0} were: ".format(index)
2405+
s = f"conditions chosen for reactor {index} were: "
24062406
for key, item in rmg_memories[index].get_cond().items():
24072407
if key == "T":
24082408
s += "T = {0} K, ".format(item)

rmgpy/solver/simple.pyx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,21 @@ cdef class SimpleReactor(ReactionSystem):
169169
conditions[species_dict[label]] = value
170170
self.sens_conditions = conditions
171171

172-
def get_const_spc_indices (self, core_species):
172+
def get_const_spc_indices(self, core_species):
173173
"""
174174
Allow to identify constant Species position in solver
175175
"""
176-
for spc in self.const_spc_names:
177-
if self.const_spc_indices is None: # initialize once the list if constant SPC declared
178-
self.const_spc_indices = []
179-
for spc in core_species: #Need to identify the species object corresponding to the the string written in the input file
180-
if spc.label == spc:
181-
self.const_spc_indices.append(core_species.index(spc))
176+
if self.const_spc_names is None:
177+
return
178+
if self.const_spc_indices is None:
179+
self.const_spc_indices = []
180+
else:
181+
return
182+
for name in self.const_spc_names:
183+
for spc in core_species:
184+
if spc.label == name:
185+
self.const_spc_indices.append(core_species.index(spc))
186+
break
182187

183188
cpdef initialize_model(self, list core_species, list core_reactions, list edge_species, list edge_reactions,
184189
list surface_species=None, list surface_reactions=None, list pdep_networks=None,

test/rmgpy/solver/simpleTest.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,3 +759,29 @@ def test_specific_collider_model(self):
759759
# order: Ar, N2, O2, H, CH3, CH4
760760
for i in range(len(simulated_mole_fracs)):
761761
assert round(abs(simulated_mole_fracs[i] - expected_mole_fracs[i]), 6) == 0
762+
763+
def test_get_const_spc_indices(self):
764+
"""
765+
Test that const_spc_names are correctly mapped to core species indices.
766+
"""
767+
# Dummy species with labels that should match const_spc_names
768+
a = Species(smiles='C', label="CH4")
769+
b = Species(smiles='[OH]', label="OH")
770+
core_species = [a, b]
771+
772+
T = 1000.0 # K
773+
P = 1.0e5 # Pa
774+
775+
rxn_system = SimpleReactor(
776+
T,
777+
P,
778+
initial_mole_fractions={a: 0.5, b: 0.5},
779+
n_sims=1,
780+
termination=[],
781+
const_spc_names=["CH4"])
782+
783+
# Populate const_spc_indices from labels
784+
rxn_system.get_const_spc_indices(core_species)
785+
786+
# Only "CH4" should be marked constant
787+
assert rxn_system.const_spc_indices == [core_species.index(a)]

0 commit comments

Comments
 (0)