Skip to content

Commit

Permalink
Merge pull request #88 from molssi-seamm/dev
Browse files Browse the repository at this point in the history
Added support for charge in chemical formulae
  • Loading branch information
seamm authored Nov 28, 2024
2 parents 89a1c8b + 53b024f commit f33ebfa
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
3 changes: 3 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
=======
History
=======
2024.11.27.1 -- Added support for charge in chemical formulae
* Added support for charge in chemical formulae, e.g. [H2 O]+.

2024.11.27 -- Bugfix: error with charge and multiplicity
* The charge and multiplicity of the system were not correctly set when creating a
system from a SMILES string using RDKit. More generally, the charge and
Expand Down
31 changes: 30 additions & 1 deletion molsystem/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,22 @@ def formula(self):
else:
formula.append(element)

formula = " ".join(formula)

# Handle any charge
if self.charge == -1:
formula = f"[{formula}]-"
counts.append(1)
elif self.charge == 1:
formula = f"[{formula}]+"
counts.append(1)
elif self.charge < 0:
formula = f"[{formula}]-{-self.charge}"
counts.append(-self.charge)
elif self.charge > 0:
formula = f"[{formula}]+{self.charge}"
counts.append(self.charge)

# And the empirical formula
Z = reduce(math.gcd, counts)
empirical_formula_list = []
Expand All @@ -367,7 +383,20 @@ def formula(self):
else:
empirical_formula.append(element)

return " ".join(formula), " ".join(empirical_formula), Z
empirical_formula = " ".join(empirical_formula)

# Handle any charge
charge = self.charge / Z
if charge == -1:
empirical_formula = f"[{empirical_formula}]-"
elif charge == 1:
empirical_formula = f"[{empirical_formula}]+"
elif charge < 0:
empirical_formula = f"[{empirical_formula}]-{-charge}"
elif charge > 0:
empirical_formula = f"[{empirical_formula}]+{charge}"

return formula, empirical_formula, Z

@property
def group(self):
Expand Down
28 changes: 28 additions & 0 deletions tests/test_formula.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Tests for `bonds` in the `molsystem` package."""

import pytest # noqa: F401


def test_simple(AceticAcid):
"""Simple test for acetic acid"""
assert AceticAcid.formula == ("C2 H4 O2", "C H2 O", 2)


def test_periodic(copper):
"""Test for copper crystal"""
assert copper.formula == ("Cu4", "Cu", 4)


def test_charged(configuration):
"""Test for charged configuration"""
configuration.from_smiles("[NH4+]")
assert configuration.formula == ("[H4 N]+", "[H4 N]+", 1)


def test_doubly_charged(configuration):
"""Test for charged configuration"""
configuration.from_smiles("[NH4+].[NH4+]")
assert configuration.formula == ("[H8 N2]+2", "[H4 N]+", 2)

0 comments on commit f33ebfa

Please sign in to comment.