Skip to content

Commit

Permalink
include port position when writing, adding test to load back compound…
Browse files Browse the repository at this point in the history
… with ports
  • Loading branch information
daico007 committed Dec 18, 2023
1 parent 25b794d commit babcb8d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mbuild/formats/json_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def _particle_info(cmpd, include_ports=False):
else:
port_info["anchor"] = None
port_info["label"] = None
port_info["pos"] = port.pos.tolist()
# Is this the most efficient way?
for key, val in cmpd.labels.items():
if (val == port) and val.port_particle:
Expand Down Expand Up @@ -242,6 +243,8 @@ def _add_ports(compound_dict, converted_dict):
for port in ports:
label_str = port["label"]
port_to_add = mb.Port(anchor=converted_dict[port["anchor"]])
if port.get("pos", None) is not None:
port_to_add.translate_to(port.get("pos"))
converted_dict[compound["id"]].add(
port_to_add, label_str, check_box_size=False
)
Expand Down
9 changes: 9 additions & 0 deletions mbuild/tests/test_json_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,12 @@ def test_float_64_position(self):
compound_to_json(ethane, "ethane.json", include_ports=True)
ethane_copy = compound_from_json("ethane.json")
assert np.allclose(ethane.xyz, ethane_copy.xyz, atol=10**-6)

def test_compound_with_port(self):
ch2 = mb.lib.moieties.CH2()
ch2.save("ch2.json", show_ports=True, overwrite=True)

loaded_ch2 = mb.load("ch2.json")
assert len(loaded_ch2.all_ports()) == 2
for port in loaded_ch2.all_ports():
assert port.separation == 0.07

0 comments on commit babcb8d

Please sign in to comment.