Skip to content

Commit

Permalink
Improve string parsing in reaction.delta method (duartegroup#325)
Browse files Browse the repository at this point in the history
* Improve string parsing in reaction.delta method

- Improved string parsing that all synonyms for double dagger can be used
- Added tests that determine if right energy is chosen

* Template (duartegroup#326)

* Added changelog

---------

Co-authored-by: Tom Young <[email protected]>
  • Loading branch information
NilsHeunemann and t-young31 authored Mar 9, 2024
1 parent 67ed012 commit c9c2c2b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,4 @@ If **autodE** is used in a publication please consider citing the [paper](https:
- Jonathon Vandezande ([@jevandezande](https://github.com/jevandezande))
- Shoubhik Maiti ([@shoubhikraj](https://github.com/shoubhikraj))
- Daniel Hollas ([@danielhollas](https://github.com/danielhollas))
- Nils Heunemann ([@nilsheunemann](https://github.com/NilsHeunemann))
11 changes: 9 additions & 2 deletions autode/reactions/reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,17 @@ def delta(self, delta_type: str) -> Optional[Energy]:
"""

def delta_type_matches(*args):
return any(s in delta_type.lower() for s in args)
return any(
s
in delta_type.lower()
.replace("ddagger", "")
.replace("double dagger", "")
for s in args
)

def is_ts_delta():
return delta_type_matches("ddagger", "‡", "double dagger")
ts_synonyms = ["ddagger", "‡", "double dagger"]
return any(s in delta_type.lower() for s in ts_synonyms)

# Determine the species on the left and right-hand sides of the equation
lhs: List[Species] = self.reacs
Expand Down
3 changes: 3 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Bug Fixes
*********
- ...

Usability improvements/Changes
******************************
- Added consistent aliases for double dagger across all energies in :code:`autode.reaction.delta`

1.4.2
------
Expand Down
30 changes: 30 additions & 0 deletions tests/test_reaction_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,36 @@ def test_barrierless_h_g():
assert np.isclose(rxn.delta("G‡"), 0.7) # -2+0.6 -> -1+0.3 --> ∆ = 0.7


@pytest.mark.parametrize("energy_syn", ["E", "Energy"])
@pytest.mark.parametrize("enthalpy_syn", ["H", "Enthalpy"])
@pytest.mark.parametrize("free_syn", ["G", "free energy", "free_energy"])
@pytest.mark.parametrize("ts_syn", ["ddagger", "‡", "double dagger"])
def test_energy_synonyms(energy_syn, enthalpy_syn, free_syn, ts_syn):
a = Reactant(atoms=[Atom("H"), Atom("H", x=0.7, y=0.7), Atom("H", x=1.0)])
a.energies.extend(
[PotentialEnergy(-2), EnthalpyCont(0.2), FreeEnergyCont(0.6)]
)

b = Product(atoms=[Atom("H"), Atom("H", x=-1.0), Atom("H", x=1.0)])
b.energies.extend(
[PotentialEnergy(-1), EnthalpyCont(0.1), FreeEnergyCont(0.3)]
)

rxn = reaction.Reaction(a, b)
# Test potential energies
assert np.isclose(rxn.delta(energy_syn + ts_syn), 1.0)

# Test enthalpies
assert np.isclose(
rxn.delta(enthalpy_syn + ts_syn), 0.9
) # -2+0.2 -> -1+0.1 --> ∆ = 0.9

# Test free energies
assert np.isclose(
rxn.delta(free_syn + ts_syn), 0.7
) # -2+0.6 -> -1+0.3 --> ∆ = 0.7


def test_same_composition():
r1 = reaction.Reaction(
Reactant(atoms=[Atom("C"), Atom("H", x=1)]),
Expand Down

0 comments on commit c9c2c2b

Please sign in to comment.