Skip to content

Commit

Permalink
Fix MD file names (stfc#287)
Browse files Browse the repository at this point in the history
* Fix MD file prefix

* Test post process default filename
  • Loading branch information
ElliottKasoar authored Aug 22, 2024
1 parent aa50400 commit 9a50c37
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 30 deletions.
68 changes: 38 additions & 30 deletions janus_core/calculations/md.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,29 +440,22 @@ def __init__(
if not self.struct.calc:
raise ValueError("Please attach a calculator to `struct`.")

self.param_prefix = self._set_param_prefix(file_prefix)

# Set output file names
self.final_file = self._build_filename(
"final.extxyz",
self._parameter_prefix if file_prefix is None else "",
filename=self.final_file,
"final.extxyz", self.param_prefix, filename=self.final_file
)
self.stats_file = self._build_filename(
"stats.dat",
self._parameter_prefix if file_prefix is None else "",
filename=self.stats_file,
"stats.dat", self.param_prefix, filename=self.stats_file
)
self.traj_file = self._build_filename(
"traj.extxyz",
self._parameter_prefix if file_prefix is None else "",
filename=self.traj_file,
"traj.extxyz", self.param_prefix, filename=self.traj_file
)

# If not specified otherwise, save optimized structure consistently with others
opt_file = self._build_filename(
"opt.extxyz",
self._parameter_prefix if file_prefix is None else "",
filename=None,
)
opt_file = self._build_filename("opt.extxyz", self.param_prefix, filename=None)

if "write_kwargs" in self.minimize_kwargs:
self.minimize_kwargs["write_kwargs"].setdefault("filename", opt_file)
# Assume if write_kwargs are specified that results should be written
Expand Down Expand Up @@ -533,16 +526,23 @@ def _optimize_structure(self) -> None:
optimizer = GeomOpt(self.struct, **self.minimize_kwargs)
optimizer.run()

@property
def _parameter_prefix(self) -> str:
def _set_param_prefix(self, file_prefix: Optional[PathLike] = None):
"""
Ensemble parameters for output files.
Set ensemble parameters for output files.
Parameters
----------
file_prefix : Optional[PathLike]
Prefix for output filenames on class init. If not None, param_prefix = "".
Returns
-------
str
Formatted temperature range if heating and target temp if running md.
Formatted ensemble parameters, including temp ramp range and/or and MD temp.
"""
if file_prefix is not None:
return ""

temperature_prefix = ""
if self.temp_start is not None and self.temp_end is not None:
temperature_prefix += f"-T{self.temp_start}-T{self.temp_end}"
Expand Down Expand Up @@ -585,9 +585,10 @@ def _attach_correlations(self) -> None:
def _write_correlations(self) -> None:
"""Write out the correlations."""
if self._correlations:
param_pref = self._parameter_prefix if self.file_prefix is None else ""
with open(
self._build_filename("cor.dat", param_pref), "w", encoding="utf-8"
self._build_filename("cor.dat", self.param_prefix),
"w",
encoding="utf-8",
) as out_file:
data = {}
for cor in self._correlations:
Expand Down Expand Up @@ -800,8 +801,6 @@ def _post_process(self) -> None:

ana = Analysis(data)

param_pref = self._parameter_prefix if self.file_prefix is None else ""

if self.post_process_kwargs.get("rdf_compute", False):
base_name = self.post_process_kwargs.get("rdf_output_file", None)
rdf_args = {
Expand Down Expand Up @@ -830,7 +829,7 @@ def _post_process(self) -> None:
out_paths = [
self._build_filename(
"rdf.dat",
param_pref,
self.param_prefix,
"_".join(element),
prefix_override=base_name,
)
Expand All @@ -840,7 +839,7 @@ def _post_process(self) -> None:
else:
out_paths = [
self._build_filename(
"rdf.dat", param_pref, prefix_override=base_name
"rdf.dat", self.param_prefix, prefix_override=base_name
)
]

Expand All @@ -851,7 +850,9 @@ def _post_process(self) -> None:
use_vel = self.post_process_kwargs.get("vaf_velocities", False)
fft = self.post_process_kwargs.get("vaf_fft", False)

out_path = self._build_filename("vaf.dat", param_pref, filename=file_name)
out_path = self._build_filename(
"vaf.dat", self.param_prefix, filename=file_name
)
slice_ = (
self.post_process_kwargs.get("vaf_start", 0),
self.post_process_kwargs.get("vaf_stop", None),
Expand Down Expand Up @@ -1087,18 +1088,25 @@ def __init__(
**ensemble_kwargs,
)

@property
def _parameter_prefix(self) -> str:
def _set_param_prefix(self, file_prefix: Optional[PathLike] = None):
"""
Ensemble parameters for output files.
Set ensemble parameters for output files.
Parameters
----------
file_prefix : Optional[PathLike]
Prefix for output filenames on class init. If not None, param_prefix = "".
Returns
-------
str
Formatted temperature range if heating and target temp if running md.
Formatted ensemble parameters, including pressure and temperature(s).
"""
if file_prefix is not None:
return ""

pressure = f"-p{self.pressure}" if not isinstance(self, NVT_NH) else ""
return f"{super()._parameter_prefix}{pressure}"
return f"{super()._set_param_prefix(file_prefix)}{pressure}"

@property
def _restart_file(self) -> str:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_md_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@ def test_md(ensemble):
restart_path = Path(f"{file_prefix[ensemble]}res-2.extxyz").absolute()
stats_path = Path(f"{file_prefix[ensemble]}stats.dat").absolute()
traj_path = Path(f"{file_prefix[ensemble]}traj.extxyz").absolute()
rdf_path = Path(f"{file_prefix[ensemble]}rdf.dat").absolute()
vaf_path = Path(f"{file_prefix[ensemble]}vaf.dat").absolute()
log_path = Path("./log.yml").absolute()
summary_path = Path("./summary.yml").absolute()

assert not final_path.exists()
assert not restart_path.exists()
assert not stats_path.exists()
assert not traj_path.exists()
assert not rdf_path.exists()
assert not vaf_path.exists()
assert not log_path.exists()
assert not summary_path.exists()

Expand All @@ -76,6 +80,8 @@ def test_md(ensemble):
2,
"--restart-every",
2,
"--post-process-kwargs",
"{'rdf_compute': True, 'vaf_compute': True}",
"--log",
log_path,
"--summary",
Expand All @@ -89,6 +95,8 @@ def test_md(ensemble):
assert restart_path.exists()
assert stats_path.exists()
assert traj_path.exists()
assert rdf_path.exists()
assert vaf_path.exists()
assert log_path.exists()
assert summary_path.exists()

Expand All @@ -101,6 +109,8 @@ def test_md(ensemble):
restart_path.unlink(missing_ok=True)
stats_path.unlink(missing_ok=True)
traj_path.unlink(missing_ok=True)
rdf_path.unlink(missing_ok=True)
vaf_path.unlink(missing_ok=True)
log_path.unlink(missing_ok=True)
summary_path.unlink(missing_ok=True)

Expand Down

0 comments on commit 9a50c37

Please sign in to comment.