Skip to content

Commit

Permalink
Adapt code to make links add interactions per batch, removing overlap…
Browse files Browse the repository at this point in the history
…ping interactions

Fixes #584
  • Loading branch information
pckroon committed May 6, 2024
1 parent e9bf1c9 commit 88e9cbd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
16 changes: 9 additions & 7 deletions vermouth/molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ def get_interaction(self, type_):
"""
return self.interactions[type_]

def remove_interaction(self, type_, atoms, version=0):
def remove_interaction(self, type_, atoms, version=None):
"""
Removes the specified interaction.
Expand All @@ -587,25 +587,27 @@ def remove_interaction(self, type_, atoms, version=0):
The type of interaction, such as 'bonds' or 'angles'.
atoms: collections.abc.Sequence
The atoms that are involved in this interaction.
version: int
version: Optional[int]
Sometimes there can be multiple distinct interactions between the
same group of atoms. This is reflected with their `version` meta
attribute.
If None, the version is not taken into account.
Raises
------
KeyError
If the specified interaction could not be found
"""
idx = 0
to_remove = []
for idx, interaction in enumerate(self.interactions[type_]):
if interaction.atoms == atoms and interaction.meta.get('version', 0) == version:
break
else: # no break
if interaction.atoms == atoms and (version is None or interaction.meta.get('version', 0) == version):
to_remove.append(idx)
if not to_remove:
msg = ("Can't find interaction of type {} between atoms {} "
"and with version {}")
raise KeyError(msg.format(type_, atoms, version))
del self.interactions[type_][idx]
for idx in reversed(to_remove):
del self.interactions[type_][idx]
if not self.interactions[type_]:
del self.interactions[type_]

Expand Down
7 changes: 7 additions & 0 deletions vermouth/processors/do_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,15 @@ def run_molecule(self, molecule):
except ValueError:
pass
for inter_type, interactions in link.interactions.items():
interactions_to_add = []
for interaction in interactions:
interaction = _build_link_interaction_from(molecule, interaction, match)
interactions_to_add.append(interaction)
try:
molecule.remove_interaction(inter_type, interaction.atoms, version=None)
except KeyError:
pass
for interaction in interactions_to_add:
molecule.add_or_replace_interaction(inter_type, *interaction, link.citations)

for loglevel, entries in link.log_entries.items():
Expand Down

0 comments on commit 88e9cbd

Please sign in to comment.