Skip to content

Commit

Permalink
Ensure that top is updated only once in from_mbuild (#470)
Browse files Browse the repository at this point in the history
Co-authored-by: Co Quach <[email protected]>
uppittu11 and daico007 authored Oct 29, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 78a3114 commit c4bfb8c
Showing 3 changed files with 18 additions and 10 deletions.
12 changes: 9 additions & 3 deletions gmso/core/subtopology.py
Original file line number Diff line number Diff line change
@@ -70,17 +70,23 @@ def parent(self, parent):
)
self._parent = _validate_parent(parent)

def add_site(self, site):
def add_site(self, site, update_types=True):
"""Add a site to this sub-topology
This method adds a site to the sub-topology.
If the sub-topology has a parent, the site will
also be added to the parent topology.
also be added to the parent topology. If the
update_types parameter is set to true (default
behavior), this method will also check if there
is an gmso.AtomType associated with the site and
it to the sub-topology's AtomTypes collection.
Parameters
----------
site : gmso.Atom
The site to be added to this sub-topology
update_types : (bool), default=True
If true, add this site's atom type to the sub-topology's set of AtomTypes
Raises
------
@@ -93,7 +99,7 @@ def add_site(self, site):
warnings.warn("Redundantly adding Site {}".format(site))
self._sites.add(site)
if self.parent:
self.parent.add_site(site)
self.parent.add_site(site, update_types=update_types)

def __repr__(self):
descr = list('<')
5 changes: 4 additions & 1 deletion gmso/core/topology.py
Original file line number Diff line number Diff line change
@@ -491,7 +491,7 @@ def update_atom_types(self):
site.atom_type = self._atom_types[site.atom_type]
self.is_typed(updated=True)

def add_subtopology(self, subtop):
def add_subtopology(self, subtop, update=True):
"""Add a sub-topology to this topology
This methods adds a gmso.Core.SubTopology object to the topology
@@ -502,6 +502,7 @@ def add_subtopology(self, subtop):
----------
subtop : gmso.SubTopology
The sub-topology object to be added.
update : bool, default=True
See Also
--------
@@ -510,6 +511,8 @@ def add_subtopology(self, subtop):
self._subtops.add(subtop)
subtop.parent = self
self._sites.union(subtop.sites)
if update:
self.update_topology()

def is_typed(self, updated=False):
if not updated:
11 changes: 5 additions & 6 deletions gmso/external/convert_mbuild.py
Original file line number Diff line number Diff line change
@@ -81,14 +81,13 @@ def from_mbuild(compound, box=None, search_method=element_by_symbol):
continue
else:
subtop = SubTopology(name=child.name)
top.add_subtopology(subtop)
top.add_subtopology(subtop, update=False)
for particle in child.particles():
pos = particle.xyz[0] * u.nanometer
ele = search_method(particle.name)
site = Atom(name=particle.name, position=pos, element=ele)
site_map[particle] = site
subtop.add_site(site)
top.update_topology()
subtop.add_site(site, update_types=False)

for particle in compound.particles():
already_added_site = site_map.get(particle, None)
@@ -106,14 +105,14 @@ def from_mbuild(compound, box=None, search_method=element_by_symbol):
if len(top.subtops) > 0:
subtop = SubTopology(name=particle.name)
top.add_subtopology(subtop)
subtop.add_site(site)
subtop.add_site(site, update_types=False)
else:
top.add_site(site)
top.add_site(site, update_types=False)

for b1, b2 in compound.bonds():
new_bond = Bond(connection_members=[site_map[b1], site_map[b2]],
bond_type=None)
top.add_connection(new_bond)
top.add_connection(new_bond, update_types=False)
top.update_topology()

if box:

0 comments on commit c4bfb8c

Please sign in to comment.