Skip to content

Refactored reaction.py to add get_free_energy_of_non_charge_transfer_reaction for code reuse and circular dependency prevention #2810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
37 changes: 15 additions & 22 deletions rmgpy/reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link
Member

Choose a reason for hiding this comment

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

this typo fix looks good



H2_thermo = ThermoData(Tdata=([300,400,500,600,800,1000,1500],'K'),
Expand Down Expand Up @@ -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):
Copy link
Member

Choose a reason for hiding this comment

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

You'll notice the other method had cython.declare lines. This helps Cython create faster code.


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))
Copy link
Member

Choose a reason for hiding this comment

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

this typo fix looks good (but you could turn it into an f-string while you're at it)

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
Expand All @@ -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

Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

I don't like renaming _get_free_energy_of_charge_transfer_reaction into self.get_free_energy_of_non_charge_transfer_reaction and calling it a refactoring. They sound like opposites! Was the previous wrong? what is it actually doing?

V0 = deltaG / self.electrons / constants.F # V = deltaG / n / F
return V0

Expand Down
Loading