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..f59ce0a80 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 = self._charge if any(metal in string for metal in metals for string in at_strings): init_smiles(self, smiles) @@ -94,6 +95,12 @@ def _init_smiles(self, smiles: str): else: init_organic_smiles(self, smiles) + 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 +249,7 @@ class Reactant(Molecule): class Product(Molecule): """Product molecule""" + + +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