From a4b7a4e51edf21089d27db69db74642e75dfaddd Mon Sep 17 00:00:00 2001 From: Lekia Prosper Date: Tue, 17 Jun 2025 19:20:51 -0400 Subject: [PATCH 1/2] Corrected the typo that repeated self.electrons twice in ReactionError in the _apply_CHE_model method in reaction.py --- rmgpy/reaction.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rmgpy/reaction.py b/rmgpy/reaction.py index 1997824afc..03877b5c2c 100644 --- a/rmgpy/reaction.py +++ b/rmgpy/reaction.py @@ -624,7 +624,7 @@ def _apply_CHE_model(self, T): if self.electrons != self.protons: raise ReactionError("Number of electrons must equal number of protons! " - f"{self} has {self.electrons} protons and {self.electrons} electrons") + f"{self} has {self.protons} protons and {self.electrons} electrons") H2_thermo = ThermoData(Tdata=([300,400,500,600,800,1000,1500],'K'), From 62dbf3a9342ad765e05d1003ac96e4ef52bdce05 Mon Sep 17 00:00:00 2001 From: Lekia Prosper Date: Tue, 17 Jun 2025 19:36:33 -0400 Subject: [PATCH 2/2] Add get_free_energy_of_non_charge_transfer_reaction method to reaction.py This new method consolidates a repeated block of code previously found in _get_free_energy_of_charge_transfer_reaction and get_free_energy_of_reaction. By abstracting it, we improve code maintainability and readability. Additionally, this method is now used in get_reversible_potential to prevent a potential circular dependency: set_reference_potentials currently links to _get_free_energy_of_charge_transfer_reaction, which may in the future depend on the reference potential being set. This refactor helps decouple that logic. --- rmgpy/reaction.py | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/rmgpy/reaction.py b/rmgpy/reaction.py index 03877b5c2c..462acee0e8 100644 --- a/rmgpy/reaction.py +++ b/rmgpy/reaction.py @@ -662,25 +662,29 @@ def get_entropy_of_reaction(self, T): for product in self.products: dSrxn += product.get_entropy(T) return dSrxn + + def get_free_energy_of_non_charge_transfer_reaction(self, T): - def _get_free_energy_of_charge_transfer_reaction(self, T, potential=0.): - - cython.declare(dGrxn=cython.double, reactant=Species, product=Species) - - dGrxn = 0 + dGrxn = 0.0 for reactant in self.reactants: try: dGrxn -= reactant.get_free_energy(T) except Exception: logging.error("Problem with reactant {!r} in reaction {!s}".format(reactant, self)) raise - for product in self.products: try: dGrxn += product.get_free_energy(T) except Exception: - logging.error("Problem with product {!r} in reaction {!s}".format(reactant, self)) + logging.error("Problem with product {!r} in reaction {!s}".format(product, self)) raise + return dGrxn + + def _get_free_energy_of_charge_transfer_reaction(self, T, potential=0.): + + cython.declare(dGrxn=cython.double, reactant=Species, product=Species) + + dGrxn = self.get_free_energy_of_non_charge_transfer_reaction(T) if potential != 0.: dGrxn -= self.electrons * constants.F * potential @@ -697,19 +701,8 @@ def get_free_energy_of_reaction(self, T, potential=0.): if self.is_charge_transfer_reaction(): return self._get_free_energy_of_charge_transfer_reaction(T, potential=potential) - dGrxn = 0.0 - for reactant in self.reactants: - try: - dGrxn -= reactant.get_free_energy(T) - except Exception: - logging.error("Problem with reactant {!r} in reaction {!s}".format(reactant, self)) - raise - for product in self.products: - try: - dGrxn += product.get_free_energy(T) - except Exception: - logging.error("Problem with product {!r} in reaction {!s}".format(reactant, self)) - raise + # For non charge transfer reactions, we can use the free energy of the species + dGrxn = self.get_enthalpy_of_reaction(T) - T * self.get_entropy_of_reaction(T) return dGrxn @@ -721,7 +714,7 @@ def get_reversible_potential(self, T): if not self.is_charge_transfer_reaction(): raise KineticsError("Cannot get reversible potential for non charge transfer reactions") - deltaG = self._get_free_energy_of_charge_transfer_reaction(T) #J/mol + deltaG = self.get_free_energy_of_non_charge_transfer_reaction(T) #J/mol V0 = deltaG / self.electrons / constants.F # V = deltaG / n / F return V0