Skip to content

Commit

Permalink
Add new option for packing.solvate (#1144)
Browse files Browse the repository at this point in the history
* add center_solute option for solvate method

* extend tests to cover center_solute option

* Update mbuild/packing.py

Co-authored-by: CalCraven <54594941+CalCraven@users.noreply.github.com>

---------

Co-authored-by: CalCraven <54594941+CalCraven@users.noreply.github.com>
  • Loading branch information
daico007 and CalCraven authored Sep 26, 2023
1 parent d94cc3a commit 1cf9df9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
9 changes: 7 additions & 2 deletions mbuild/packing.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,7 @@ def solvate(
fix_orientation=False,
temp_file=None,
update_port_locations=False,
center_solute=True,
):
"""Solvate a compound in a box of solvent using PACKMOL.
Expand Down Expand Up @@ -701,6 +702,8 @@ def solvate(
update_port_locations : bool, default=False
After packing, port locations can be updated, but since compounds can be
rotated, port orientation may be incorrect.
center_solute : bool, optional, default=True
Move solute center of mass to the center of the `mb.Box` used.
Returns
-------
Expand All @@ -724,8 +727,10 @@ def solvate(
box_mins = np.asarray(min_tmp) * 10
box_maxs = np.asarray(max_tmp) * 10
overlap *= 10
center_solute = (box_maxs + box_mins) / 2

if center_solute:
center_solute = (box_maxs + box_mins) / 2
else:
center_solute = solute.pos * 10
# Apply edge buffer
box_maxs = np.subtract(box_maxs, edge * 10)
box_mins = np.add(box_mins, edge * 10)
Expand Down
21 changes: 18 additions & 3 deletions mbuild/tests/test_packing.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,24 @@ def test_fill_box_multiple(self, ethane, h2o):

def test_solvate(self, ethane, h2o):
n_solvent = 100
solvated = mb.solvate(ethane, h2o, n_solvent=n_solvent, box=[4, 4, 4])
assert solvated.n_particles == 8 + n_solvent * 3
assert solvated.n_bonds == 7 + n_solvent * 2
ethane_pos = ethane.pos
solvated1 = mb.solvate(
ethane, h2o, n_solvent=n_solvent, box=[4, 4, 4], center_solute=True
)
solvated2 = mb.solvate(
ethane, h2o, n_solvent=n_solvent, box=[4, 4, 4], center_solute=False
)

print(ethane.xyz)
print(solvated2.children[0].xyz)

assert (
solvated1.n_particles == solvated2.n_particles == 8 + n_solvent * 3
)
assert solvated1.n_bonds == solvated2.n_bonds == 7 + n_solvent * 2
assert not np.isclose(solvated1.children[0].pos, ethane_pos).all()
assert np.isclose(solvated2.children[0].pos, ethane_pos).all()

assert ethane.parent == None

def test_solvate_multiple(self, methane, ethane, h2o):
Expand Down

0 comments on commit 1cf9df9

Please sign in to comment.