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

Enabled custom placeholders in sbatch script #29

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
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
29 changes: 27 additions & 2 deletions src/asyncmd/gromacs/mdengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import copy
import shlex
import random
import re
import string
import shutil
import typing
Expand Down Expand Up @@ -1025,7 +1026,7 @@
# although the mdrun was successfull.

def __init__(self, mdconfig, gro_file, top_file, sbatch_script, ndx_file=None,
**kwargs):
sbatch_placeholders_values=None, **kwargs):
"""
Initialize a :class:`SlurmGmxEngine`.

Expand All @@ -1047,6 +1048,15 @@

ndx_file: str or None
Optional, absolute or relative path to a gromacs index file.
sbatch_placeholders_values : dict or None
A dictionary containing values for placeholders defined in the
`sbatch_script`.

- Keys must match the placeholders in `sbatch_script` (e.g., `"mem"`
corresponds to `{mem}` and `"nodes"` to `{nodes}`).
- Values should be strings or numbers that will replace the
placeholders when preparing the script for `gmx mdrun`.
- If `None`, only `{mdrun_cmd}` will be replaced.

Note that all attributes can be set at intialization by passing keyword
arguments with their name, e.g. mdrun_extra_args="-ntomp 2" to instruct
Expand All @@ -1062,7 +1072,15 @@
# probably path to a file, lets try to read it
with open(sbatch_script, 'r') as f:
sbatch_script = f.read()
# check if all placeholders exist in submit script
if sbatch_placeholders_values is not None:
existing_placeholders = set(re.findall(r"\{(\w+)\}", sbatch_script))

Check warning on line 1077 in src/asyncmd/gromacs/mdengine.py

View check run for this annotation

Codecov / codecov/patch

src/asyncmd/gromacs/mdengine.py#L1077

Added line #L1077 was not covered by tests
for key in sbatch_placeholders_values.keys():
if key not in existing_placeholders:
logger.error("Specified key '%s' doesn't exist in Slurm sbatch script!", key)
raise RuntimeError("Invalid Slurm sbatch script placeholders specified")

Check warning on line 1081 in src/asyncmd/gromacs/mdengine.py

View check run for this annotation

Codecov / codecov/patch

src/asyncmd/gromacs/mdengine.py#L1080-L1081

Added lines #L1080 - L1081 were not covered by tests
self.sbatch_script = sbatch_script
self.sbatch_placeholders_values = sbatch_placeholders_values

Check warning on line 1083 in src/asyncmd/gromacs/mdengine.py

View check run for this annotation

Codecov / codecov/patch

src/asyncmd/gromacs/mdengine.py#L1083

Added line #L1083 was not covered by tests

def _name_from_name_or_none(self, run_name: typing.Optional[str]) -> str:
if run_name is not None:
Expand All @@ -1076,7 +1094,11 @@
run_name=None, **kwargs):
name = self._name_from_name_or_none(run_name=run_name)
# substitute placeholders in submit script
script = self.sbatch_script.format(mdrun_cmd=cmd_str)
try:
script = self.sbatch_script.format(mdrun_cmd=cmd_str, **self.sbatch_placeholders_values)
except KeyError as k:
logger.error(f"Specified key '%s' doesn't have a value defined!", k.args[0])
RuntimeError("Invalid Slurm sbatch script placeholders specified")

Check warning on line 1101 in src/asyncmd/gromacs/mdengine.py

View check run for this annotation

Codecov / codecov/patch

src/asyncmd/gromacs/mdengine.py#L1097-L1101

Added lines #L1097 - L1101 were not covered by tests
# write it out
fname = os.path.join(workdir, name + ".slurm")
if await aiofiles.ospath.exists(fname):
Expand Down Expand Up @@ -1118,6 +1140,9 @@
except FileNotFoundError:
pass

def slum_set_variable_value(self, placeholder, value):
self.sbatch_placeholders_values[placeholder] = value

Check warning on line 1144 in src/asyncmd/gromacs/mdengine.py

View check run for this annotation

Codecov / codecov/patch

src/asyncmd/gromacs/mdengine.py#L1144

Added line #L1144 was not covered by tests

# TODO: do we even need/want this?
@property
def slurm_job_state(self) -> str | None:
Expand Down
Loading