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
Changes from 3 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
50 changes: 42 additions & 8 deletions mbuild/packing.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,50 @@
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 = """
{}
{}
{}
"""
if not any(fix_orientation): # No directions fixed
return None
if all(fix_orientation): # All directions fixed
PACKMOL_CONSTRAIN = CONSTRAIN.format(constraints[0], constraints[1], constraints[2])
elif fix_orientation.count(True) == 1: # Only 1 direction fixed
PACKMOL_CONSTRAIN = CONSTRAIN.format(constraints[fix_orientation.index(True)],"","")
else: # 2 directions are fixed
if fix_orientation[0] and fix_orientation[1]: # Only x and y are fixed
PACKMOL_CONSTRAIN = CONSTRAIN.format(constraints[0], constraints[1], "")
if fix_orientation[0] and fix_orientation[2]: # Only x and z are fixed
PACKMOL_CONSTRAIN = CONSTRAIN.format(constraints[0], constraints[2], "")
if fix_orientation[1] and fix_orientation[2]: # Only y and z are fixed
PACKMOL_CONSTRAIN = CONSTRAIN.format(constraints[1], constraints[2], "")
return PACKMOL_CONSTRAIN
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One other option that might be aligned with jenny's suggestion

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

Yours is more readable, but this removes all of the conditionals from line 74 onwards i think



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 @@ -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 Down Expand Up @@ -205,11 +238,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