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

Change syntax to remove ghost ports #995

Merged
merged 6 commits into from
Mar 23, 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
19 changes: 13 additions & 6 deletions mbuild/compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -833,17 +833,24 @@ def _check_if_empty(child):
removed_part.parent.children.remove(removed_part)
self._remove_references(removed_part)

# Remove ghost ports
all_ports_list = list(self.all_ports())
for port in all_ports_list:
if port.anchor not in [i for i in self.particles()]:
port.parent.children.remove(port)

# Check and reorder rigid id
for _ in particles_to_remove:
if self.contains_rigid:
self.root._reorder_rigid_ids()

# Remove ghsot ports
self._prune_ghost_ports()

def _prune_ghost_ports(self):
"""Worker for remove(). Remove all ports whose anchor has been deleted."""
all_ports_list = list(self.all_ports())
particles = list(self.particles())
for port in all_ports_list:
if port.anchor not in particles:
self._remove(port)
port.parent.children.remove(port)
self._remove_references(port)

def _remove(self, removed_part):
"""Worker for remove(). Fixes rigid IDs and removes bonds."""
if removed_part.rigid_id is not None:
Expand Down
14 changes: 14 additions & 0 deletions mbuild/tests/test_compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,20 @@ def test_add_bond_remove_ports(self, hydrogen):
assert len(hydrogen.all_ports()) == 0
assert len(h_clone.all_ports()) == 0

def test_pruning_ghost_ports(self, ethane):
eth1 = ethane
h1 = eth1.particles_by_name("H")
eth1.remove(h1)
assert len(eth1.all_ports()) == 6
for port in eth1.all_ports():
assert port.anchor in list(eth1.particles())

eth2 = mb.load("CC", smiles=True)
h2 = eth2[-1]
eth2.remove(h2)
assert len(eth2.all_ports()) == len(eth2.available_ports()) == 1
assert eth2.all_ports()[0] is eth2.available_ports()[0]

def test_remove_bond_add_ports(self, hydrogen):
h_clone = mb.clone(hydrogen)
h2 = Compound(subcompounds=(hydrogen, h_clone))
Expand Down
8 changes: 4 additions & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ message = Bump to version {new_version}
tag_name = {new_version}

[coverage:run]
omit =
omit =
mbuild/examples/*
mbuild/tests/*

[coverage:report]
exclude_lines =
exclude_lines =
pragma: no cover

if 0:
if __name__ == .__main__.:
def __repr__
except ImportError
omit =
omit =
mbuild/examples/*
mbuild/tests/*

Expand Down