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

Specify individual axes to constrain within PACKMOL in packing.py #734

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c43e28a
added new function packmol_constrain to allow contraints about each a…
chrisjonesBSU May 14, 2020
74fcdf8
added doc strings to packmol_constraints function
chrisjonesBSU May 14, 2020
7ee47df
updated the doc strings in the packmol_constrain function
chrisjonesBSU May 18, 2020
dc3d983
updated packmol_constraints using Justin's suggestion for string form…
chrisjonesBSU May 26, 2020
7f068d8
added warning message and behavior to handle non-iterable fix_orienta…
chrisjonesBSU May 26, 2020
41669bc
fixed quotations in warning message
chrisjonesBSU May 26, 2020
c6154e4
Merge branch 'master' of github.com:mosdef-hub/mbuild into fix_orient…
chrisjonesBSU Jun 7, 2020
033b576
added new unit test for new fix_orientation functionality
chrisjonesBSU Jun 8, 2020
3ba2b80
fixed unit test for specify_axis; now tests for all 6 remaining possi…
chrisjonesBSU Jun 8, 2020
cf4ba73
fixes to unit test per notes from Ray
chrisjonesBSU Jun 10, 2020
5611010
removed my old specify_axis unit test
chrisjonesBSU Jun 10, 2020
75ff322
Merge branch 'master' of github.com:mosdef-hub/mbuild into fix_orient…
chrisjonesBSU Jun 10, 2020
3a394b0
added new packmol_constrain functionality to all packing functions
chrisjonesBSU Jun 17, 2020
d5641a1
fixed inconsistencies in doc strings for fix_orientation
chrisjonesBSU Jun 18, 2020
475d17f
made warning messages consistent for each packing function
chrisjonesBSU Jun 18, 2020
2e55f88
added a leading underscore to the packmol_constrain function, and mov…
chrisjonesBSU Jun 30, 2020
3b6f19a
added the leadering underscore to the packmol_constrain function call…
chrisjonesBSU Jun 30, 2020
85cf398
fixed typos, more robust check of fix_orientation in the _packmol_con…
chrisjonesBSU Jul 16, 2020
d63f967
made changes to the checking and handling of lists of bools for the f…
chrisjonesBSU Jul 27, 2020
052be47
change compound to solvent in def solvate(); fixed failing solvate te…
chrisjonesBSU Aug 6, 2020
59cf662
fix conflicts
chrisjonesBSU Apr 14, 2021
2d90190
fixed a lot of little errors
chrisjonesBSU Apr 14, 2021
c363358
Merge branch 'master' of github.com:mosdef-hub/mbuild into fix_orient…
chrisjonesBSU Apr 14, 2021
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
62 changes: 52 additions & 10 deletions mbuild/packing.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,49 @@
end structure
"""

PACKMOL_CONSTRAIN = """
constrain_rotation x 0. 0.
constrain_rotation y 0. 0.
constrain_rotation z 0. 0.
"""
def packmol_constrain(fix_orientation):
"""
Provides information to PACKMOL about which axes to constrain rotation.
fix_orientation is generated within the `fill_box` function
Parameters
----------
fix_orientation : iterable of booleans, directions (x, y, z) about
which to constrain rotation. If True, no rotation
in that direction
Returns
-------
None if fix_orientation = (False, False, False)
string
rotation constraints to be added to PACKMOL input text
"""
constraints = ['constrain_rotation x 0. 0.',
'constrain_rotation y 0. 0.',
'constrain_rotation z 0. 0.']
CONSTRAIN = """
{}
{}
{}
"""
# Handles instances that are not iterable; defaults to True/Fales for all axes
if fix_orientation == True:
fix_orientation=(True, True, True)
if fix_orientation == False:
fix_orientation=(False, False, False)

if not any(fix_orientation):
return None
final_constraints = list()
for i, val in enumerate(fix_orientation):
if val:
final_constraints.append(constraints[i])
else:
final_constraints.append("")
return CONSTRAIN.format(*final_constraints)


def fill_box(compound, n_compounds=None, box=None, density=None, overlap=0.2,
seed=12345, edge=0.2, compound_ratio=None,
aspect_ratio=None, fix_orientation=False, temp_file=None,
aspect_ratio=None, fix_orientation=(False, False, False), temp_file=None,
update_port_locations=False):
"""Fill a box with a `mbuild.compound` or `Compound`s using PACKMOL.

Expand Down Expand Up @@ -104,9 +137,10 @@ def fill_box(compound, n_compounds=None, box=None, density=None, overlap=0.2,
aspect_ratio : list of float
If a non-cubic box is desired, the ratio of box lengths in the x, y,
and z directions.
fix_orientation : bool or list of bools
fix_orientation : iterable of booleans or list of iterables
Specify that compounds should not be rotated when filling the box,
default=False.
Rotation contraints are specified for each individual axis (x, y, z)
default=(False, False, False)
temp_file : str, default=None
File name to write PACKMOL's raw output to.
update_port_locations : bool, default=False
Expand All @@ -120,7 +154,6 @@ def fill_box(compound, n_compounds=None, box=None, density=None, overlap=0.2,
"""
# check that the user has the PACKMOL binary on their PATH
_check_packmol(PACKMOL)

arg_count = 3 - [n_compounds, box, density].count(None)
if arg_count != 2:
msg = ("Exactly 2 of `n_compounds`, `box`, and `density` "
Expand All @@ -133,8 +166,16 @@ def fill_box(compound, n_compounds=None, box=None, density=None, overlap=0.2,
compound = [compound]
if n_compounds is not None and not isinstance(n_compounds, (list, set)):
n_compounds = [n_compounds]

if not isinstance(fix_orientation, (list, set)):
fix_orientation = [fix_orientation]*len(compound)
try:
iter(fix_orientation[0])
except:
jennyfothergill marked this conversation as resolved.
Show resolved Hide resolved
chrisjonesBSU marked this conversation as resolved.
Show resolved Hide resolved
warnings.warn("fix_orientation can be given as an iterable of True/False values "
"for each compound to fix rotations about each x,y,z axis individually. "
"Using a single instance of True/False defaults to (True,True,True) "
"and (Fale,False,False) respectively")
chrisjonesBSU marked this conversation as resolved.
Show resolved Hide resolved

if compound is not None and n_compounds is not None:
if len(compound) != len(n_compounds):
Expand Down Expand Up @@ -205,11 +246,12 @@ def fill_box(compound, n_compounds=None, box=None, density=None, overlap=0.2,
compound_xyz_list.append(compound_xyz)

comp.save(compound_xyz.name, overwrite=True)
PACKMOL_CONSTRAIN = packmol_constrain(rotate)
input_text += PACKMOL_BOX.format(compound_xyz.name, m_compounds,
box_mins[0], box_mins[1],
box_mins[2], box_maxs[0],
box_maxs[1], box_maxs[2],
PACKMOL_CONSTRAIN if rotate else "")
PACKMOL_CONSTRAIN if PACKMOL_CONSTRAIN else "")

_run_packmol(input_text, filled_xyz, temp_file)
# Create the topology and update the coordinates.
Expand Down
16 changes: 16 additions & 0 deletions mbuild/tests/test_packing.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,22 @@ def test_no_rotate(self, h2o):
w0 -= w0.sum(0) / len(w0)
w1 -= w1.sum(0) / len(w1)
assert np.isclose(w0, w1).all() is not True

def specify_axis(self):
chrisjonesBSU marked this conversation as resolved.
Show resolved Hide resolved
arguments = [(True, False, False),
chrisjonesBSU marked this conversation as resolved.
Show resolved Hide resolved
(False, True, False),
(False, False, True),
(True, True, False),
(False, True, True),
(True, False, True)]
constraints = [["constrain_rotation x"],
["constrain_rotation y"],
["constrain_rotation z"],
["constrain_rotation x", "constrain_rotation y"],
["constrain_rotation y", "constrain_rotation z"],
["constrain_rotation x", "constrain_rotation z"]]
for i, arg in enumerate(arguments):
assert all(c in mb.packing.packmol_constrain(arg) for c in constraints[i])

def test_remove_port(self):
from mbuild.lib.recipes import Alkane
Expand Down