Skip to content

Commit

Permalink
Reduce charge particle warnings; add static particle_charge method (#…
Browse files Browse the repository at this point in the history
…1109)

* add better notes about unit conversions

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* adding a missing word

* fix a couple formatting issues

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix typos, clean up formatting

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* add static method for particle charge to reduce warnings

* fix return statement line

* add doc strings, add if statement to check for children

* change == to is

* fix docstyle

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Co Quach <[email protected]>
Co-authored-by: Co Quach <[email protected]>
  • Loading branch information
4 people committed Apr 28, 2023
1 parent 08918c7 commit e79447b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
24 changes: 20 additions & 4 deletions mbuild/compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,10 @@ def _n_particles(self, include_ports=False):
return sum(1 for _ in self._particles(include_ports))

def _contains_only_ports(self):
for part in self.children:
if not part.port_particle:
return False
if self.children:
for part in self.children:
if not part.port_particle:
return False
return True

def print_hierarchy(self, print_full=False, index=None, show_tree=True):
Expand Down Expand Up @@ -547,7 +548,17 @@ def mass(self, value):

@property
def charge(self):
"""Get the charge of the Compound."""
"""Return the total charge of a compound.
If the compound contains children compouds, the total charge of all
children compounds is returned.
If the charge of a particle has not been explicitly set
then the particle's charge is None, and are not used when
calculating the total charge.
"""
if self._contains_only_ports():
return self._particle_charge(self)
charges = [p._charge for p in self.particles()]
if None in charges:
warn(
Expand All @@ -557,6 +568,11 @@ def charge(self):
filtered_charges = [charge for charge in charges if charge is not None]
return sum(filtered_charges) if filtered_charges else None

@staticmethod
def _particle_charge(particle):
"""Return charge of a Compound with no children."""
return particle._charge

@charge.setter
def charge(self, value):
if self._contains_only_ports():
Expand Down
7 changes: 3 additions & 4 deletions mbuild/tests/test_compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -1702,16 +1702,15 @@ def test_charge_neutrality_warn(self, benzene):

def test_none_charge(self):
A = mb.Compound()
with pytest.warns(UserWarning):
A.charge
assert A.charge is None

A.charge = 1
B = mb.Compound()
container = mb.Compound(subcompounds=[A, B])
assert A.charge == 1
assert B.charge is None
with pytest.warns(UserWarning):
container_charge = container.charge
assert A.charge == 1
assert B.charge == None
assert container_charge == 1

@pytest.mark.skipif(not has_openbabel, reason="Open Babel not installed")
Expand Down

0 comments on commit e79447b

Please sign in to comment.