Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checking for incompatible charge #200

Merged
merged 3 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion autode/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
- Merge when tests pass
"""

__version__ = "1.3.2"
__version__ = "1.3.3"


__all__ = [
Expand Down
11 changes: 11 additions & 0 deletions autode/species/molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,20 @@ 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)

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}, "
Expand Down Expand Up @@ -242,3 +249,7 @@ class Reactant(Molecule):

class Product(Molecule):
"""Product molecule"""


def _is_default_charge(value) -> bool:
return value == 0
12 changes: 12 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
@@ -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
--------
----------
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

setup(
name="autode",
version="1.3.2",
version="1.3.3",
python_requires=">3.7",
packages=[
"autode",
Expand Down
15 changes: 15 additions & 0 deletions tests/test_molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -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