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

Fix 580 #581

Merged
merged 3 commits into from
Mar 28, 2024
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
8 changes: 5 additions & 3 deletions vermouth/molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,8 @@ def merge_molecule(self, molecule):
self.add_interaction(name, atoms, interaction.parameters, interaction.meta)
for node1, node2 in molecule.edges:
if correspondence[node1] != correspondence[node2]:
self.add_edge(correspondence[node1], correspondence[node2])
attrs = molecule.edges[(node1, node2)]
self.add_edge(correspondence[node1], correspondence[node2], **attrs)
# merge the citation sets
self.citations.update(molecule.citations)
# Merge the log entries
Expand Down Expand Up @@ -1227,8 +1228,9 @@ def to_molecule(self, atom_offset=0, offset_resid=0, offset_charge_group=0,
interaction.parameters,
meta=interaction.meta
)
for edge in self.edges:
mol.add_edge(*(name_to_idx[node] for node in edge))
for nodea, nodeb, attrs in self.edges(data=True):
edge = (nodea, nodeb)
mol.add_edge(*(name_to_idx[node] for node in edge), **attrs)

try:
mol.nrexcl = self.nrexcl
Expand Down
43 changes: 42 additions & 1 deletion vermouth/tests/test_molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,8 @@ def test_to_molecule():
"""
test_block = vermouth.molecule.Block()
test_block.add_edges_from([('A', 'B'), ('B', 'C')])
# make sure edge attributes are also propagated
test_block.edges[('A', 'B')]['attr'] = 1
test_block.interactions["bonds"] = [
Interaction(atoms=('A', 'B'),
parameters=['a', '0.2', '200'],
Expand All @@ -1278,6 +1280,7 @@ def test_to_molecule():
Interaction(atoms=(1, 2),
parameters=['a', '0.1', '300'],
meta={'b': 1}),]
assert test_molecule.edges[(0, 1)]['attr'] == 1
assert ref_bonds == test_molecule.interactions['bonds']


Expand Down Expand Up @@ -1333,4 +1336,42 @@ def test_remove_interaction(atoms, bonds, interactions, removed, expected):
if removed:
molecule.remove_interaction(**removed)

assert molecule.interactions == expected
assert molecule.interactions == expected


def test_merge_molecule():
"""
Test if the merge molecule function gives
expected results.
"""
test_block = vermouth.molecule.Block()
test_block.add_edges_from([('A', 'B'), ('B', 'C')])
# make sure edge attributes are also propagated
test_block.edges[('A', 'B')]['attr'] = 1
test_block.interactions["bonds"] = [
Interaction(atoms=('A', 'B'),
parameters=['a', '0.2', '200'],
meta={'a': 0}),
Interaction(atoms=('B', 'C'),
parameters=['a', '0.1', '300'],
meta={'b': 1}),]

test_molecule = test_block.to_molecule()
test_molecule.merge_molecule(test_block)

ref_bonds = [Interaction(atoms=(0, 1),
parameters=['a', '0.2', '200'],
meta={'a': 0}),
Interaction(atoms=(1, 2),
parameters=['a', '0.1', '300'],
meta={'b': 1}),
Interaction(atoms=(3, 4),
parameters=['a', '0.2', '200'],
meta={'a': 0}),
Interaction(atoms=(4, 5),
parameters=['a', '0.1', '300'],
meta={'b': 1})]

assert test_molecule.edges[(0, 1)]['attr'] == 1
assert test_molecule.edges[(3, 4)]['attr'] == 1
assert ref_bonds == test_molecule.interactions['bonds']
Loading