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

Reduce charge particle warnings; add static particle_charge method #1109

Merged
merged 24 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
1994a2d
add better notes about unit conversions
chrisjonesBSU Nov 22, 2022
eb93743
Merge branch 'main' of github.com:mosdef-hub/mbuild into fix/hoomd_docs
chrisjonesBSU Nov 22, 2022
793324a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 22, 2022
825a95c
adding a missing word
chrisjonesBSU Nov 22, 2022
ff00dfd
Merge branch 'fix/hoomd_docs' of github.com:chrisjonesBSU/mbuild into…
chrisjonesBSU Nov 22, 2022
eb2a82f
fix a couple formatting issues
chrisjonesBSU Nov 22, 2022
dbee990
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 22, 2022
d80b97f
Merge branch 'main' into fix/hoomd_docs
daico007 Dec 5, 2022
10cde25
Merge branch 'main' into fix/hoomd_docs
daico007 Dec 5, 2022
75685f4
fix typos, clean up formatting
chrisjonesBSU Dec 6, 2022
7053657
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 6, 2022
26a5325
Merge branch 'main' of github.com:mosdef-hub/mbuild into fix/hoomd_docs
chrisjonesBSU Dec 6, 2022
7a58873
Merge branch 'fix/hoomd_docs' of github.com:chrisjonesBSU/mbuild into…
chrisjonesBSU Dec 6, 2022
c12c9f2
Merge branch 'main' into fix/hoomd_docs
daico007 Dec 15, 2022
742af92
Merge branch 'main' of github.com:mosdef-hub/mbuild
chrisjonesBSU Jan 6, 2023
f728fec
Merge branch 'main' of github.com:mosdef-hub/mbuild
chrisjonesBSU Feb 13, 2023
9d0d3f9
Merge branch 'main' of github.com:mosdef-hub/mbuild
chrisjonesBSU Apr 26, 2023
a36284f
add static method for particle charge to reduce warnings
chrisjonesBSU Apr 26, 2023
67d8254
fix return statement line
chrisjonesBSU Apr 26, 2023
66a1696
add doc strings, add if statement to check for children
chrisjonesBSU Apr 27, 2023
7ac9a65
change == to is
chrisjonesBSU Apr 27, 2023
0469552
fix docstyle
daico007 Apr 28, 2023
443e92e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 28, 2023
2ae30a8
Merge branch 'main' into charge_warnings
daico007 Apr 28, 2023
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
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):
chrisjonesBSU marked this conversation as resolved.
Show resolved Hide resolved
"""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