Skip to content

Conversation

@alongd
Copy link
Member

@alongd alongd commented Dec 11, 2025

Motivation or Problem

SimpleReactor.get_const_spc_indices() was possibly effectively broken:

  • It reused the same loop variable name in nested loops.
  • It compared spc.label == spc, i.e. a string to a species object.

As a result, const_spc_indices was never populated and species listed in const_spc_names were perhaps not actually treated as constant.

Description of Changes

In SimpleReactor.get_const_spc_indices():

  • Use different variable names for const_spc_names and core_species.
  • Compare the constant-species name (string) against spc.label instead of spc itself.
  • Initialize const_spc_indices once and append core-species indices when labels match.
  • Return early if const_spc_names is None.

Testing

A small unit test was added.

Reviewer Tips

In a small reactor run with a constant species, check that its amount stays fixed over time.

@alongd alongd requested a review from Copilot December 11, 2025 06:13
@alongd alongd changed the title Simp.cnst Correct constant-species index mapping in SimpleReactor Dec 11, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a critical bug in SimpleReactor.get_const_spc_indices() that prevented constant species from being properly identified and treated as constant during simulation. The original implementation had two major issues: it reused the same loop variable spc in nested loops, and it incorrectly compared a string to a Species object (spc.label == spc instead of spc.label == name). As a result, species listed in const_spc_names were never actually marked as constant.

  • Fixed variable shadowing in nested loops by using distinct variable names (name for const_spc_names, spc for core_species)
  • Corrected the comparison logic to properly match species labels against constant species names
  • Added early return for None const_spc_names and moved initialization outside the loop for clarity
  • Added a break statement to stop searching once a match is found

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
rmgpy/solver/simple.pyx Fixed the get_const_spc_indices() method by correcting variable naming, comparison logic, and control flow
test/rmgpy/solver/simpleTest.py Added unit test test_get_const_spc_indices() to verify that constant species names are correctly mapped to core species indices

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@alongd
Copy link
Member Author

alongd commented Dec 11, 2025

A test unrelated to this PR failed:

FAILED test/rmgpy/reactionTest.py::TestReactionToCantera::test_arrhenius - AssertionError: assert {'rate-constant': {'A': 660000.0, 'b': 1.62, 'Ea': 45354560.0}} == {'rate-constant': {'A': 660000.0000000001, 'b': 1.62, 'Ea': 45354560.0}}
  
  Differing items:
  {'rate-constant': {'A': 660000.0, 'Ea': 45354560.0, 'b': 1.62}} != {'rate-constant': {'A': 660000.0000000001, 'Ea': 45354560.0, 'b': 1.62}}
  
  Full diff:
    {
        'rate-constant': {
  -         'A': 660000.0000000001,
  ?                      ---------
  +         'A': 660000.0,
            'Ea': 45354560.0,
            'b': 1.62,
        },
    }
FAILED test/rmgpy/reactionTest.py::TestReactionToCantera::test_multi_arrhenius - assert 0.001 == 0
 +  where 0.001 = round(0.0009765625, 3)
 +    where 0.0009765625 = abs((5000000000000.0 - 5000000000000.001))
 +      where 5000000000000.0 = <ArrheniusRate at 7f9319b52cf0>.pre_exponential_factor
 +        where <ArrheniusRate at 7f9319b52cf0> = HO2(5) + OH(4) <=> H2O(27) + O2(6)    <Reaction(Arrhenius)>.rate
 +      and   5000000000000.001 = <ArrheniusRate at 7f9319b529f0>.pre_exponential_factor
 +        where <ArrheniusRate at 7f9319b529f0> = HO2(5) + OH(4) <=> H2O(27) + O2(6)    <Reaction(Arrhenius)>.rate
FAILED test/rmgpy/reactionTest.py::TestChargeTransferReaction::test_get_rate_coeff - assert 0.0078125 < 1e-06
 +  where 0.0078125 = abs((43870506959778.99 - 43870506959779.0))
== 3 failed, 1968 passed, 39 skipped, 3939699 warnings in 2192.81s (0:36:32) ===

Added another commit to check isclose() for 660000.0000000001 vs. 660000.0 instead of identity

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.

2 participants