-
Notifications
You must be signed in to change notification settings - Fork 13
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
Add restricted insertions attribute for moves
#47
Merged
rsdefever
merged 25 commits into
MaginnGroup:master
from
rmatsum836:add_restriction_insert
Apr 17, 2020
Merged
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
f5e4b9b
Add initial restriction insertion attribute
rmatsum836 4e58a88
Restructrue restriction_insert setter
rmatsum836 a53da2c
Write out restricted insertions
rmatsum836 8cf5a58
Check valid ensemble with restricted insert, add printing
rmatsum836 9b1a2eb
Fix logic to support restrictions for 1 and 2 boxes
rmatsum836 578a0c8
Add restricted as a probability insertin
rmatsum836 fbf9f72
Add basic moves tests
rmatsum836 956995e
Add writers tests for restricted insertions
rmatsum836 b5ba8d6
Refactor restricted insertions as lists
rmatsum836 95efc00
Fix bugs so writer tests pass
rmatsum836 c741b0f
Rewrite restricted insertions to support multiple boxes and species
rmatsum836 decb013
parametrize resricted insertion test functions
rmatsum836 500ed06
Fix restriction tests in test_writers
rmatsum836 469a712
Add tests to ensure ValueError when invalid restricted insertion passed
rmatsum836 c941c4f
Fix conversion from nm to a and add example
rmatsum836 b2b1099
Remove chemical potential from gemc and reformat with black
rmatsum836 3edf6ab
Add docstring and reformat get_box_info in inp_functions
rmatsum836 bf3d6c2
Refactor restricted insertions into a method
rmatsum836 0113cda
Fix error messages
rmatsum836 fc1adae
Add gcmc_restriction to init file
rmatsum836 610202c
Add restricted insertion tests designed to fail
rmatsum836 7cb4640
Remove old implementation of restricted insertion
rmatsum836 264e0eb
Revert restricted insertions to being passed in as angstroms
rmatsum836 2db7490
Add restricted option for probability swap
rmatsum836 9181fad
Address Ryan's comments
rmatsum836 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,14 @@ def __init__(self, ensemble, species_topologies): | |
else: | ||
self._n_boxes = 2 | ||
|
||
# Set '_restricted_typed' and '_restricted_value' | ||
if self.ensemble in ["gcmc", "gemc", "gemc_npt"]: | ||
self._restricted_type = [[None] * self._n_species] * self._n_boxes | ||
self._restricted_value = [[None] * self._n_species] * self._n_boxes | ||
else: | ||
self._restricted_type = None | ||
self._restricted_value = None | ||
|
||
# Define default probabilities | ||
# Most are ensemble-dependent | ||
self.prob_angle = 0.0 | ||
|
@@ -173,6 +181,77 @@ def __init__(self, ensemble, species_topologies): | |
self.prob_translate += self.prob_rotate | ||
self.prob_rotate = 0.0 | ||
|
||
def add_restricted_insertions( | ||
self, species_topologies, restricted_type, restricted_value | ||
): | ||
"""Add restricted insertions for specific species and boxes | ||
|
||
Parameters | ||
---------- | ||
species_topologies : list | ||
list of ``parmed.Structures``, with one species per element | ||
restricted_type : list | ||
list of restricted insertion types, with one species per element. | ||
restricted_value : list | ||
list of restricted insertion values, with one species per element. | ||
""" | ||
if self.ensemble not in ["gcmc", "gemc", "gemc_npt"]: | ||
raise ValueError( | ||
"Restricted insertions are only valid for" | ||
" 'gcmc', 'gemc', and 'gemc_npt' ensembles." | ||
) | ||
if len(restricted_type) != len(restricted_value): | ||
raise ValueError( | ||
"Length of 'restricted_type' and " | ||
" 'restricted_value' must match." | ||
) | ||
for box in restricted_type: | ||
if len(box) != len(species_topologies): | ||
raise ValueError( | ||
"Length of 'species' and " | ||
" length of species list in 'restricted_type'" | ||
" must match." | ||
) | ||
for box in restricted_value: | ||
if len(box) != len(species_topologies): | ||
raise ValueError( | ||
"Length of 'species' and " | ||
" length of species list in 'restricted_value'" | ||
" must match." | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps these error messages can be made more verbose. If I do this: species_list = [methane]
moves = mc.Moves('gcmc', species_list)
moves.add_restricted_insertions(species_list, ['sphere'], [5.0]) The error message I get is:
I'm not sure that points me towards fixing my problem. |
||
if self.ensemble == "gcmc" and len(restricted_type) != 1: | ||
raise ValueError( | ||
"GCMC ensemble contains 1 box but" | ||
" `restricted_type` of length {}" | ||
" was passed.".format(len(restricted_type)) | ||
) | ||
if self.ensemble in ["gemc", "gemc_npt"] and len(restricted_type) != 2: | ||
raise ValueError( | ||
"GEMC ensembles contain 2 boxes but" | ||
" `restricted_type` of length {}" | ||
" was passed.".format(len(restricted_type)) | ||
) | ||
|
||
for types, values in zip(restricted_type, restricted_value): | ||
for typ, val in zip(types, values): | ||
if not typ and not val: | ||
pass | ||
elif typ and not val: | ||
raise ValueError( | ||
"`restricted_type` {} was passed" | ||
" but `restricted_value` is None.".format(typ, val) | ||
) | ||
elif val and not typ: | ||
raise ValueError( | ||
"`restricted_value` {} was passed" | ||
" but `restricted_type` is None.".format(val, typ) | ||
) | ||
else: | ||
_check_restriction_type(typ, val) | ||
|
||
self._restricted_type = restricted_type | ||
self._restricted_value = restricted_value | ||
|
||
@property | ||
def ensemble(self): | ||
return self._ensemble | ||
|
@@ -677,4 +756,65 @@ def print(self): | |
box=box + 1, max_vol=max_vol | ||
) | ||
|
||
if self._restricted_type != None: | ||
contents += "\nRestricted Insertions (Ang):\n" | ||
for box in range(self._n_boxes): | ||
for species, (typ, value) in enumerate( | ||
zip( | ||
self._restricted_type[box], self._restricted_value[box] | ||
) | ||
): | ||
if typ == "sphere": | ||
contents += "Box {box}, Species {species}: sphere, R = {r_value}\n".format( | ||
box=box + 1, species=species + 1, r_value=value | ||
) | ||
elif typ == "cylinder": | ||
contents += "Box {box}, Species {species}: cylinder, R = {r_value}\n".format( | ||
box=box + 1, species=species + 1, r_value=value | ||
) | ||
elif typ == "slitpore": | ||
contents += "Box {box}, Species {species}: slitpore, z_max = {z_max}\n".format( | ||
box=box + 1, species=species + 1, z_max=value | ||
) | ||
elif typ == "interface": | ||
contents += "Box {box}, Species {species}: interface, z_min = {z_min}, z_max = {z_max}\n".format( | ||
box=box + 1, | ||
species=species + 1, | ||
z_min=value[0], | ||
z_max=value[1], | ||
) | ||
else: | ||
rsdefever marked this conversation as resolved.
Show resolved
Hide resolved
|
||
contents += "Box {box}, Species {species}: None\n".format( | ||
box=box + 1, species=species + 1 | ||
) | ||
|
||
print(contents) | ||
|
||
|
||
def _check_restriction_type(restriction_type, restriction_value): | ||
valid_restrict_types = ["sphere", "cylinder", "slitpore", "interface"] | ||
# Check restriction insertion type | ||
if restriction_type not in valid_restrict_types: | ||
raise ValueError( | ||
'Invalid restriction type "{}". Supported ' | ||
"restriction types include {}".format( | ||
restriction_type, valid_restrict_types | ||
) | ||
) | ||
# Check if correct number of arguments passed | ||
if restriction_type == "interface": | ||
if len(restriction_value) != 2: | ||
raise ValueError( | ||
"Invalid number of arguments passed." | ||
"{} arguments for restriction type {}" | ||
"were passed. 2 are required".format( | ||
len(restriction_value), restriction_type | ||
) | ||
) | ||
else: | ||
if not isinstance(restriction_value, (float, int)): | ||
raise TypeError( | ||
"Restriction type is {}. A" | ||
' single argument of type "int"' | ||
'or "float" should be passed'.format(restriction_type) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import mbuild | ||
import foyer | ||
import mosdef_cassandra as mc | ||
|
||
|
||
def run_gcmc_restricted(): | ||
|
||
# Use mbuild to create molecules | ||
methane = mbuild.load("C", smiles=True) | ||
|
||
# Create an empty mbuild.Box | ||
box = mbuild.Box(lengths=[5.0, 5.0, 5.0]) | ||
|
||
# Load forcefields | ||
oplsaa = foyer.forcefields.load_OPLSAA() | ||
|
||
# Use foyer to apply forcefields | ||
methane_ff = oplsaa.apply(methane) | ||
|
||
# Create box and species list | ||
box_list = [box] | ||
species_list = [methane_ff] | ||
|
||
mols_to_add = [[10]] | ||
|
||
system = mc.System(box_list, species_list, mols_to_add=mols_to_add) | ||
moves = mc.Moves("gcmc", species_list) | ||
|
||
# Specify restricted insertions | ||
moves.add_restricted_insertions(species_list, [["sphere"]], [[20]]) | ||
|
||
mc.run( | ||
system=system, | ||
moves=moves, | ||
run_type="equilibration", | ||
run_length=100, | ||
temperature=300.0, | ||
chemical_potentials=[-35.0], | ||
prop_freq=10, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
run_gcmc_restricted() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these could be more clear; i.e. WRT the nesting of lists. One list per box and then one list per species.