-
Notifications
You must be signed in to change notification settings - Fork 247
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
base: main
Are you sure you want to change the base?
Changes from all commits
a4b7a4e
62dbf3a
e410acb
6f9ab99
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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'), | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -662,25 +662,29 @@ def get_entropy_of_reaction(self, T): | |||||||||||||||||||||||||||||||||||||||||||
| for product in self.products: | ||||||||||||||||||||||||||||||||||||||||||||
| dSrxn += product.get_entropy(T) | ||||||||||||||||||||||||||||||||||||||||||||
| return dSrxn | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
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.
Copilot
AI
Nov 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing documentation for this new method. Consider adding a docstring that explains what this method calculates and when it should be used. Based on its usage in _get_free_energy_of_charge_transfer_reaction and get_reversible_potential, this method calculates the standard free energy of reaction (without the electrochemical potential term), which is used for both charge transfer and non-charge transfer reactions.
| def get_free_energy_of_non_charge_transfer_reaction(self, T): | |
| def get_free_energy_of_non_charge_transfer_reaction(self, T): | |
| """ | |
| Return the standard free energy of reaction (ΔG°) in J/mol at temperature `T` in K, | |
| excluding any electrochemical potential (i.e., without the -nFE term). | |
| This method sums the free energies of the products and subtracts those of the reactants, | |
| and is used as the base calculation for both charge transfer and non-charge transfer reactions. | |
| Parameters | |
| ---------- | |
| T : float | |
| Temperature in Kelvin at which to evaluate the free energy of reaction. | |
| Returns | |
| ------- | |
| float | |
| The standard free energy of reaction in J/mol. | |
| """ |
Copilot
AI
Nov 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing Cython type declarations. For consistency with other methods like get_enthalpy_of_reaction and get_entropy_of_reaction, this method should include Cython declarations. Consider adding:
cython.declare(dGrxn=cython.double, reactant=Species, product=Species)| cython.declare(dGrxn=cython.double, reactant=Species, product=Species) |
There was a problem hiding this comment.
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)
Copilot
AI
Nov 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused Cython variable declarations. The variables reactant and product are declared but no longer used in this method since it now delegates to get_free_energy_of_non_charge_transfer_reaction. Consider removing them from the declaration:
cython.declare(dGrxn=cython.double)| cython.declare(dGrxn=cython.double, reactant=Species, product=Species) | |
| cython.declare(dGrxn=cython.double) |
Copilot
AI
Nov 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment is misleading. The code calculates free energy from enthalpy and entropy (H - T*S) rather than directly using species free energies. Consider updating the comment to: "For non charge transfer reactions, calculate free energy from enthalpy and entropy"
| # For non charge transfer reactions, we can use the free energy of the species | |
| # For non charge transfer reactions, calculate free energy from enthalpy and entropy |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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