Skip to content

Commit

Permalink
Black
Browse files Browse the repository at this point in the history
  • Loading branch information
emarinri committed Nov 10, 2024
1 parent 2411156 commit 7336614
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 52 deletions.
2 changes: 1 addition & 1 deletion mosdef_cassandra/analysis/thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, filename):
# Read headers
prp_headers = []
with open(self.filename) as f:
for (idx, line) in enumerate(f):
for idx, line in enumerate(f):
if idx == 1 or idx == 2:
prp_headers.append(line)
if idx > 2:
Expand Down
29 changes: 17 additions & 12 deletions mosdef_cassandra/core/moveset.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,16 @@ def __init__(self, ensemble, species_topologies):
"species_topologies should be a " "list of species"
)

if not (all(isinstance(top, gmso.Topology) for top in species_topologies) or all(
isinstance(top, parmed.Structure) for top in species_topologies)):
if not (
all(isinstance(top, gmso.Topology) for top in species_topologies)
or all(
isinstance(top, parmed.Structure) for top in species_topologies
)
):

raise TypeError(
"Each species should be a " "parmed.Structure or gmso.Topology"
"Each species should be a "
"parmed.Structure or gmso.Topology"
"and must be of the same type"
)

Expand Down Expand Up @@ -745,46 +750,46 @@ def print(self):
contents += "======== ".format(idx=idx + 1)
contents += "\n"
contents += " Max translate (Ang): "
for (box, max_translate_box) in enumerate(self.max_translate):
for box, max_translate_box in enumerate(self.max_translate):
if box > 0:
contents += " "
for (idx, max_translate) in enumerate(max_translate_box):
for idx, max_translate in enumerate(max_translate_box):
contents += "{max_trans:4.2f} ".format(
max_trans=max_translate
)
contents += "(Box {box})".format(box=box + 1)
contents += "\n"
contents += " Max rotate (deg): "
for (box, max_rotate_box) in enumerate(self.max_rotate):
for box, max_rotate_box in enumerate(self.max_rotate):
if box > 0:
contents += " "
for (idx, max_rotate) in enumerate(max_rotate_box):
for idx, max_rotate in enumerate(max_rotate_box):
contents += "{max_rot:4.2f} ".format(
max_rot=max_rotate
)
contents += "(Box {box})".format(box=box + 1)
contents += "\n"
contents += " Insertable: "
for (idx, insert) in enumerate(self.insertable):
for idx, insert in enumerate(self.insertable):
contents += "{insert} ".format(insert=insert)
contents += "\n"
contents += " Max dihedral: "
for (idx, max_dih) in enumerate(self.max_dihedral):
for idx, max_dih in enumerate(self.max_dihedral):
contents += "{max_dih:4.2f} ".format(max_dih=max_dih)
contents += "\n"
contents += " Prob swap: "
for (idx, prob_swap) in enumerate(self.prob_swap_species):
for idx, prob_swap in enumerate(self.prob_swap_species):
contents += "{prob_swap:4.2f} ".format(
prob_swap=prob_swap
)
contents += "\n"
contents += " Prob regrow: "
for (idx, prob_regrow) in enumerate(self.prob_regrow_species):
for idx, prob_regrow in enumerate(self.prob_regrow_species):
contents += "{regrow:4.2f} ".format(regrow=prob_regrow)
contents += "\n"

contents += "\n\nMax volume (Ang^3):\n"
for (box, max_vol) in enumerate(self.max_volume):
for box, max_vol in enumerate(self.max_volume):
contents += " Box {box}: {max_vol}\n".format(
box=box + 1, max_vol=max_vol
)
Expand Down
37 changes: 25 additions & 12 deletions mosdef_cassandra/core/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,20 @@ def species_topologies(self, species_topologies):
"See help(mosdef_Cassandra.System) for details."
)

if not (all(isinstance(top, gmso.Topology) for top in species_topologies) or all(
isinstance(top, parmed.Structure) for top in species_topologies)):
if not (
all(
isinstance(top, gmso.Topology)
for top in species_topologies
)
or all(
isinstance(top, parmed.Structure)
for top in species_topologies
)
):

raise TypeError(
"Each species should be a " "parmed.Structure or gmso.Topology"
"Each species should be a "
"parmed.Structure or gmso.Topology"
"and must be of the same type"
)

Expand All @@ -149,14 +158,18 @@ def species_topologies(self, species_topologies):
subtops = []
if isinstance(top, gmso.Topology):
for molecule in top.unique_site_labels(name_only=True):
subtops.append(top.create_subtop("molecule", (molecule, 1)))
subtops.append(
top.create_subtop("molecule", (molecule, 1))
)

if len(subtops) > 1:
raise ValueError("GMSO Topology must contain only one molecule type. For example, "
"if you have a box of water, you must have a single water molecule "
"type in the topology. If you have a box of water and methane, you "
"must have two separate single molecule topologies, one for water "
" and one for methane.")
raise ValueError(
"GMSO Topology must contain only one molecule type. For example, "
"if you have a box of water, you must have a single water molecule "
"type in the topology. If you have a box of water and methane, you "
"must have two separate single molecule topologies, one for water "
" and one for methane."
)
subtops[0].box = top.box
top = to_parmed(subtops[0])
# top = to_parmed(top)
Expand Down Expand Up @@ -357,9 +370,9 @@ def fix_bonds(self):
if constrain is None:
start_idx = idx_offset
end_idx = idx_offset + n_mols * n_atoms
constrained_coordinates[
start_idx:end_idx
] = unconstrained_coordinates[start_idx:end_idx]
constrained_coordinates[start_idx:end_idx] = (
unconstrained_coordinates[start_idx:end_idx]
)
# Else we apply the constraints one molecule
# at a time
else:
Expand Down
18 changes: 10 additions & 8 deletions mosdef_cassandra/examples/nvt_gmso.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ def run_nvt_gmso(**custom_args):

# Run a simulation at 300 K for 10000 MC moves
mc.run(
system=system,
moveset=moveset,
run_type="equilibration",
run_length=10000,
temperature=300.0 * u.K,
seeds=[12345, 12345],
run_name="nvt_gmso",
**custom_args,
system=system,
moveset=moveset,
run_type="equilibration",
run_length=10000,
temperature=300.0 * u.K,
seeds=[12345, 12345],
run_name="nvt_gmso",
**custom_args,
)


#

if __name__ == "__main__":
Expand Down
1 change: 0 additions & 1 deletion mosdef_cassandra/runners/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@


1 change: 0 additions & 1 deletion mosdef_cassandra/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@


1 change: 1 addition & 0 deletions mosdef_cassandra/tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from mosdef_cassandra.utils.exceptions import *
from mosdef_cassandra.tests.base_test import BaseTest


@pytest.mark.long
class TestExamples(BaseTest):
def test_run_nvt(self):
Expand Down
8 changes: 5 additions & 3 deletions mosdef_cassandra/tests/test_moveset.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ def test_invalid_species_list(self, methane_oplsaa):

def test_invalid_species_type(self):
with pytest.raises(
TypeError, match=r"Each species should be a " "parmed.Structure or gmso.Topology"
"and must be of the same type"
TypeError,
match=r"Each species should be a "
"parmed.Structure or gmso.Topology"
"and must be of the same type",
):
moveset = mc.MoveSet("nvt", [1])

Expand Down Expand Up @@ -605,7 +607,7 @@ def test_invalid_restricted_type_and_species(self, methane_oplsaa):
[methane_oplsaa], [["slitpore", None]], [[1, None]]
)

@pytest.mark.skip(reason="The purpose of this test is not clear.")
@pytest.mark.skip(reason="The purpose of this test is not clear.")
def test_add_multiple_restricted_insertions(self, methane_oplsaa):
moveset = mc.MoveSet("gcmc", [methane_oplsaa])
moveset.add_restricted_insertions(
Expand Down
27 changes: 17 additions & 10 deletions mosdef_cassandra/tests/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,29 @@ def check_start_type_header(inp_contents):
if "# Start_Type" in line:
return idx
raise AssertionError("Missing '# Start_Type' header")

@staticmethod
def check_checkpoint_line(inp_contents, start_idx):
"""Helper function to check the checkpoint line format."""
checkpoint_line = inp_contents[start_idx + 1].strip()
parts = checkpoint_line.split()
assert len(parts) == 2, "The line following '# Start_Type' should have exactly two entries"
assert parts[0] == "checkpoint", "The first entry should be 'checkpoint'"
assert (
len(parts) == 2
), "The line following '# Start_Type' should have exactly two entries"
assert (
parts[0] == "checkpoint"
), "The first entry should be 'checkpoint'"

@staticmethod
def check_only_comments_or_whitespace(inp_contents, start_idx):
"""Helper function to check for only comments or whitespace until the next header."""
for line in inp_contents[start_idx + 2:]:
for line in inp_contents[start_idx + 2 :]:
line = line.strip()
if line.startswith("#"):
break
assert line == "" or line.startswith("!"), \
"Only spaces or comments are allowed between the checkpoint line and the next header"
assert line == "" or line.startswith(
"!"
), "Only spaces or comments are allowed between the checkpoint line and the next header"

@pytest.fixture
def onecomp_system(self, methane_oplsaa, box):
Expand Down Expand Up @@ -1848,17 +1853,17 @@ def test_rst_multiple_rst(self, request, system_fixture, base_name):
of an automatic equilibration detection loop, in which a simulation needs to
be restarted if its not equilibrated.
This test evaluates systems with one or two boxes. Two boxes might be
This test evaluates systems with one or two boxes. Two boxes might be
problematic because some start types require two lines in the # Start_Type
section.
This test ensures that the input files generated at each restart step:
1. Contain a valid # Start_Type header.
2. Follow the correct checkpoint line format (two entries, starting with "checkpoint").
3. Include only comments or whitespace between the checkpoint line and the next # header.
Parameters:
- system_fixture: The fixture name of the system setup, allowing tests with
- system_fixture: The fixture name of the system setup, allowing tests with
both one-component and two-box systems.
- base_name: The base name used for the generated files (e.g., "nvt" or "gemc").
"""
Expand Down Expand Up @@ -1900,4 +1905,6 @@ def test_rst_multiple_rst(self, request, system_fixture, base_name):
self.check_checkpoint_line(inp_contents, start_idx)

# Step 3: Ensure only comments or whitespace are present until the next '#' header
self.check_only_comments_or_whitespace(inp_contents, start_idx)
self.check_only_comments_or_whitespace(
inp_contents, start_idx
)
16 changes: 12 additions & 4 deletions mosdef_cassandra/writers/writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,18 @@ def write_mcfs(system, angle_style="harmonic"):

mcf_name = "species{}.mcf".format(species_count + 1)

if all(isinstance(top, parmed.Structure) for top in system.original_tops):
if all(
isinstance(top, parmed.Structure) for top in system.original_tops
):
write_mcf(
species,
mcf_name,
angle_style=angle_style[species_count],
dihedral_style=dihedral_style,
)
elif all(isinstance(top, gmso.Topology) for top in system.original_tops):
elif all(
isinstance(top, gmso.Topology) for top in system.original_tops
):
gmso_write_mcf(system.original_tops[species_count], mcf_name)


Expand Down Expand Up @@ -139,9 +143,13 @@ def _generate_restart_inp(restart_from, run_name, run_type, run_length):
if "# Start_Type" in line:
inp_contents[idx + 1] = "checkpoint " + restart_from + ".out.chk"
i = idx + 2
while i < len(inp_contents) and not inp_contents[i].strip().startswith("#"):
while i < len(inp_contents) and not inp_contents[
i
].strip().startswith("#"):
if not inp_contents[i].strip().startswith("!"):
inp_contents[i] = "" # Replace non-comment lines with an empty string
inp_contents[i] = (
"" # Replace non-comment lines with an empty string
)
i += 1
if run_type is not None:
if "# Run_Type" in line:
Expand Down

0 comments on commit 7336614

Please sign in to comment.