Repository: duartegroup/autodE
Pull request #194 (2022-11-05 12:46:18)

Add tests
Attempts to increase the patch codecov in https://github.com/duartegroup/autodE/pull/190

codecov[bot]: # [Codecov](https://codecov.io/gh/duartegroup/autodE/pull/194?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=duartegroup) Report
> Merging [#194](https://codecov.io/gh/duartegroup/autodE/pull/194?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=duartegroup) (1273a4a) into [v1.3.2](https://codecov.io/gh/duartegroup/autodE/commit/3839d2dce80913fdecfecb5ed8efa8552ee98276?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=duartegroup) (3839d2d) will **increase** coverage by `0.01%`.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff             @@
##           v1.3.2     #194      +/-   ##
==========================================
+ Coverage   96.96%   96.97%   +0.01%     
==========================================
  Files         195      195              
  Lines       19841    19845       +4     
==========================================
+ Hits        19238    19245       +7     
+ Misses        603      600       -3     
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `96.97% <100.00%> (+0.01%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=duartegroup#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/duartegroup/autodE/pull/194?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=duartegroup) | Coverage Δ | |
|---|---|---|
| [autode/geom.py](https://codecov.io/gh/duartegroup/autodE/pull/194/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=duartegroup#diff-YXV0b2RlL2dlb20ucHk=) | `98.94% <ø> (+1.83%)` | :arrow_up: |
| [tests/test\_conf\_gen.py](https://codecov.io/gh/duartegroup/autodE/pull/194/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=duartegroup#diff-dGVzdHMvdGVzdF9jb25mX2dlbi5weQ==) | `100.00% <ø> (ø)` | |
| [autode/transition\_states/base.py](https://codecov.io/gh/duartegroup/autodE/pull/194/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=duartegroup#diff-YXV0b2RlL3RyYW5zaXRpb25fc3RhdGVzL2Jhc2UucHk=) | `66.47% <100.00%> (+1.59%)` | :arrow_up: |
| [autode/transition\_states/transition\_state.py](https://codecov.io/gh/duartegroup/autodE/pull/194/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=duartegroup#diff-YXV0b2RlL3RyYW5zaXRpb25fc3RhdGVzL3RyYW5zaXRpb25fc3RhdGUucHk=) | `62.66% <100.00%> (ø)` | |
| [tests/test\_bond\_rearrangement.py](https://codecov.io/gh/duartegroup/autodE/pull/194/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=duartegroup#diff-dGVzdHMvdGVzdF9ib25kX3JlYXJyYW5nZW1lbnQucHk=) | `100.00% <100.00%> (ø)` | |
| [autode/bond\_rearrangement.py](https://codecov.io/gh/duartegroup/autodE/pull/194/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=duartegroup#diff-YXV0b2RlL2JvbmRfcmVhcnJhbmdlbWVudC5weQ==) | `96.28% <0.00%> (+0.33%)` | :arrow_up: |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=duartegroup). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=duartegroup)


Patch:
diff --git a/autode/geom.py b/autode/geom.py
index 9cfc103..72e76a9 100644
--- a/autode/geom.py
+++ b/autode/geom.py
@@ -167,40 +167,6 @@ def get_neighbour_list(
     return atom_label_neighbour_list
 
 
-def get_distance_constraints(
-    species: "autode.species.species.Species",
-) -> dict:
-    """
-    Set all the distance constraints required in an optimisation as the
-    active bonds
-
-    ---------------------------------------------------------------------------
-    Arguments:
-        species (autode.species.Species):
-
-    Returns:
-        (dict): Keyed with atom indexes for the active atoms (tuple) and
-                equal to the constrained value
-    """
-    distance_constraints = {}
-
-    if species.graph is None:
-        logger.warning(
-            "Molecular graph was not set cannot find any distance "
-            "constraints"
-        )
-        return None
-
-    # Add the active edges(/bonds) in the molecular graph to the dict, value
-    # being the current distance
-    for edge in species.graph.edges:
-
-        if species.graph.edges[edge]["active"]:
-            distance_constraints[edge] = species.distance(*edge)
-
-    return distance_constraints
-
-
 def calc_heavy_atom_rmsd(
     atoms1: "autode.atoms.Atoms", atoms2: "autode.atoms.Atoms"
 ) -> float:
diff --git a/autode/transition_states/base.py b/autode/transition_states/base.py
index 8789a58..6ce9b99 100644
--- a/autode/transition_states/base.py
+++ b/autode/transition_states/base.py
@@ -4,6 +4,7 @@ from typing import Optional
 import autode.exceptions as ex
 from autode.atoms import metals
 from autode.config import Config
+from autode.constraints import DistanceConstraints
 from autode.log import logger
 from autode.methods import get_hmethod, get_lmethod
 from autode.mol_graphs import make_graph, species_are_isomorphic
@@ -385,6 +386,26 @@ class TSbase(Species, ABC):
         logger.info(f"Backwards displaced edges {b_mol.graph.edges}")
         return False
 
+    @property
+    def active_bond_constraints(self) -> DistanceConstraints:
+        """
+        Set all the distance constraints required in an optimisation as the
+        active bonds
+
+        -----------------------------------------------------------------------
+        Returns:
+            (dict): Keyed with atom indexes for the active atoms (tuple) and
+                    equal to the constrained value
+        """
+        constraints = DistanceConstraints()
+
+        for edge in self.graph.edges:
+
+            if self.graph.edges[edge]["active"]:
+                constraints[edge] = self.distance(*edge)
+
+        return constraints
+
 
 def displaced_species_along_mode(
     species: Species,
diff --git a/autode/transition_states/transition_state.py b/autode/transition_states/transition_state.py
index 7310994..0d16186 100644
--- a/autode/transition_states/transition_state.py
+++ b/autode/transition_states/transition_state.py
@@ -8,7 +8,7 @@ from autode.input_output import atoms_to_xyz_file
 from autode.calculations import Calculation
 from autode.config import Config
 from autode.exceptions import CalculationException
-from autode.geom import get_distance_constraints, calc_heavy_atom_rmsd
+from autode.geom import calc_heavy_atom_rmsd
 from autode.log import logger
 from autode.methods import get_hmethod
 from autode.mol_graphs import get_truncated_active_mol_graph
@@ -136,7 +136,7 @@ class TransitionState(TSbase):
         n_confs = Config.num_conformers if n_confs is None else n_confs
         self.conformers = []
 
-        distance_consts = get_distance_constraints(self)
+        distance_consts = self.active_bond_constraints
 
         with Pool(processes=Config.n_cores) as pool:
             results = [
diff --git a/doc/changelog.rst b/doc/changelog.rst
index b2288d6..8818dee 100644
--- a/doc/changelog.rst
+++ b/doc/changelog.rst
@@ -8,9 +8,15 @@ Changelog
 Bugfix release.
 
 
+Usability improvements/Changes
+******************************
+* Removes :code:`autode.geom.get_distance_constraints` in favour of a better named method :code:`TSBase().active_bond_constraints`
+
+
 Bug Fixes
 *********
 - :code:`autode.transition_states.ts_guess.TSguess.from_species` now inherits solvent from the species
+- Fixes a possible race condition in I/O for XTB conformer optimisations
 
 
 1.3.1


Test patch:
diff --git a/tests/test_bond_rearrangement.py b/tests/test_bond_rearrangement.py
index 8a32086..ab5f476 100644
--- a/tests/test_bond_rearrangement.py
+++ b/tests/test_bond_rearrangement.py
@@ -1,15 +1,15 @@
 import os
 import pytest
-import networkx as nx
 import autode as ade
 from autode import bond_rearrangement as br
 from autode.mol_graphs import MolecularGraph
 from autode.species.molecule import Molecule
-from autode.bond_rearrangement import BondRearrangement
+from autode.bond_rearrangement import BondRearrangement, get_bond_rearrangs
 from autode.species.complex import ReactantComplex, ProductComplex
 from autode.atoms import Atom
 from autode.mol_graphs import is_isomorphic
 from autode.mol_graphs import make_graph
+from autode.utils import work_in_tmp_dir
 
 
 # Some of the 'reactions' here are not physical, hence for some the graph will
@@ -606,3 +606,36 @@ def test_br_from_file():
     assert saved_br.n_bbonds == 2
 
     os.remove("tmp.txt")
+
+
+@work_in_tmp_dir()
+def test_2b2f_single_bond_type():
+    """
+    Test that the bond rearrangement can be found where only OO bonds break
+    and only OH bonds form. Not a very realistic reaction
+    """
+
+    reac = Molecule(
+        atoms=[
+            Atom("O", 0.0, 0.0, 0.0),
+            Atom("O", 1.5, 0.0, 0.0),
+            Atom("O", 0.0, 1.5, 0.0),
+            Atom("O", 1.5, 1.5, 0.0),
+            Atom("H", 9.0, 0.0, 0.0),
+            Atom("H", 9.0, 2.0, 0.0),
+        ]
+    )
+
+    prod = Molecule(
+        atoms=[
+            Atom("O", -9.0, 0.0, 0.0),
+            Atom("O", 8.0, 0.0, 0.0),
+            Atom("O", -9.0, 1.0, 0.0),
+            Atom("O", 8.0, 1.0, 0.0),
+            Atom("H", 9.0, 0.0, 0.0),
+            Atom("H", 9.0, 1.0, 0.0),
+        ]
+    )
+
+    brs = get_bond_rearrangs(reac, prod, "test")
+    assert brs is not None and len(brs) == 1
diff --git a/tests/test_conf_gen.py b/tests/test_conf_gen.py
index d79df91..9a8da7a 100644
--- a/tests/test_conf_gen.py
+++ b/tests/test_conf_gen.py
@@ -9,7 +9,6 @@ from autode.geom import are_coords_reasonable
 from autode.transition_states.ts_guess import TSguess
 from autode.transition_states.transition_state import TransitionState
 from autode.bond_rearrangement import BondRearrangement
-from autode.geom import get_distance_constraints
 import numpy as np
 import os
 
@@ -283,11 +282,10 @@ def test_ts_conformer(tmpdir):
     f_ch3cl_ts = TransitionState(ts_guess=f_ch3cl_tsguess)
 
     atoms = conf_gen.get_simanl_atoms(
-        species=f_ch3cl_ts, dist_consts=get_distance_constraints(f_ch3cl_ts)
+        species=f_ch3cl_ts, dist_consts=f_ch3cl_ts.active_bond_constraints
     )
 
     regen = Molecule(name="regenerated_ts", charge=-1, mult=1, atoms=atoms)
-    # regen.print_xyz_file()
 
     # Ensure the making/breaking bonds retain their length
     regen_coords = regen.coordinates