Skip to content

Commit

Permalink
Improve string parsing in reaction.delta method
Browse files Browse the repository at this point in the history
- Improved string parsing that all synonyms for double dagger can be used
- Added tests that determine if right energy is chosen
  • Loading branch information
NilsHeunemann committed Feb 27, 2024
1 parent 034743c commit 475764c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
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
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 475764c

Please sign in to comment.