From f3834087fb427d52f5213afad1002050c0a8105d Mon Sep 17 00:00:00 2001 From: tom Date: Sat, 12 Nov 2022 11:16:16 +0000 Subject: [PATCH 1/3] Add checking for incompatible charge/mult --- autode/__init__.py | 2 +- autode/species/molecule.py | 22 ++++++++++++++++++++++ doc/changelog.rst | 12 ++++++++++++ setup.py | 2 +- tests/test_molecule.py | 15 +++++++++++++++ 5 files changed, 51 insertions(+), 2 deletions(-) diff --git a/autode/__init__.py b/autode/__init__.py index af21c8a59..eff4ba3b0 100644 --- a/autode/__init__.py +++ b/autode/__init__.py @@ -38,7 +38,7 @@ - Merge when tests pass """ -__version__ = "1.3.2" +__version__ = "1.3.3" __all__ = [ diff --git a/autode/species/molecule.py b/autode/species/molecule.py index e2983506d..964f650ea 100644 --- a/autode/species/molecule.py +++ b/autode/species/molecule.py @@ -87,6 +87,7 @@ def _init_smiles(self, smiles: str): smiles (str): """ at_strings = re.findall(r"\[.*?]", smiles) + init_charge, init_mult = self._charge, self._mult if any(metal in string for metal in metals for string in at_strings): init_smiles(self, smiles) @@ -94,6 +95,19 @@ def _init_smiles(self, smiles: str): else: init_organic_smiles(self, smiles) + if not _is_default_mult(init_mult) and init_mult != self._mult: + logger.warning( + "Specified multiplicity did not match SMILES " + "calculated value - using the former" + ) + self._mult = init_mult + + if not _is_default_charge(init_charge) and init_charge != self._charge: + raise ValueError( + "SMILES charge was not the same as the " + f"defined value. {self._charge} ≠ {init_charge}" + ) + logger.info( f"Initialisation with SMILES successful. " f"Charge={self.charge}, Multiplicity={self.mult}, " @@ -242,3 +256,11 @@ class Reactant(Molecule): class Product(Molecule): """Product molecule""" + + +def _is_default_mult(value) -> bool: + return value == 1 + + +def _is_default_charge(value) -> bool: + return value == 0 diff --git a/doc/changelog.rst b/doc/changelog.rst index 8818deed9..1f215ddc8 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -1,6 +1,18 @@ Changelog ========= +1.3.3 +-------- +---------- + +Bugfix release. + + +Bug Fixes +********* +- Adds checking of SMILES-defined charge and multiplicity against user-specified values + + 1.3.2 -------- ---------- diff --git a/setup.py b/setup.py index 9822fe342..b8206c6dd 100644 --- a/setup.py +++ b/setup.py @@ -24,7 +24,7 @@ setup( name="autode", - version="1.3.2", + version="1.3.3", python_requires=">3.7", packages=[ "autode", diff --git a/tests/test_molecule.py b/tests/test_molecule.py index 4372605af..10c9e4302 100644 --- a/tests/test_molecule.py +++ b/tests/test_molecule.py @@ -271,3 +271,18 @@ def test_atom_class_defined_for_organic(): mol = Molecule(smiles="[Br-:1]") assert mol.atoms[0].atom_class is not None + + +def test_smiles_and_user_defined_charge_raises_exception(): + + with pytest.raises(Exception): + _ = Molecule(smiles="[Cl-]", charge=1) + + +def test_user_defined_charge_overrides_smiles_mult(): + + ch2 = Molecule(smiles="[H][C][H]") + default_mult = ch2.mult + + ch2_alt = Molecule(smiles="[H][C][H]", mult=3 if default_mult == 1 else 1) + assert ch2_alt.mult != default_mult From b8ce7afa6b5025fabf188936b40139a9037bfa5e Mon Sep 17 00:00:00 2001 From: tom Date: Tue, 15 Nov 2022 18:53:44 +0000 Subject: [PATCH 2/3] Remove non-required warning --- autode/species/molecule.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/autode/species/molecule.py b/autode/species/molecule.py index 964f650ea..59964e739 100644 --- a/autode/species/molecule.py +++ b/autode/species/molecule.py @@ -87,7 +87,7 @@ def _init_smiles(self, smiles: str): smiles (str): """ at_strings = re.findall(r"\[.*?]", smiles) - init_charge, init_mult = self._charge, self._mult + init_charge = self._charge if any(metal in string for metal in metals for string in at_strings): init_smiles(self, smiles) @@ -95,13 +95,6 @@ def _init_smiles(self, smiles: str): else: init_organic_smiles(self, smiles) - if not _is_default_mult(init_mult) and init_mult != self._mult: - logger.warning( - "Specified multiplicity did not match SMILES " - "calculated value - using the former" - ) - self._mult = init_mult - if not _is_default_charge(init_charge) and init_charge != self._charge: raise ValueError( "SMILES charge was not the same as the " From 80d96748e86776df4393a28dbc6f510a926c38e3 Mon Sep 17 00:00:00 2001 From: tom Date: Tue, 15 Nov 2022 19:55:25 +0000 Subject: [PATCH 3/3] Remove unused function --- autode/species/molecule.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/autode/species/molecule.py b/autode/species/molecule.py index 59964e739..f59ce0a80 100644 --- a/autode/species/molecule.py +++ b/autode/species/molecule.py @@ -251,9 +251,5 @@ class Product(Molecule): """Product molecule""" -def _is_default_mult(value) -> bool: - return value == 1 - - def _is_default_charge(value) -> bool: return value == 0