Skip to content
Open
Show file tree
Hide file tree
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
13 changes: 12 additions & 1 deletion janus_core/calculations/md.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,18 @@ def _prepare_restart(self) -> None:
restart_stem = self._restart_stem

# Use restart_stem.name otherwise T300.0 etc. counts as extension
poss_restarts = restart_stem.parent.glob(f"{restart_stem.name}*.extxyz")
poss_restarts = list(
restart_stem.parent.glob(f"{restart_stem.name}*.extxyz")
)
Comment thread
oerc0122 marked this conversation as resolved.

# If no restarts found, check potential last results directory
if not poss_restarts:
results_dirs = sorted(restart_stem.parent.parent.glob("janus_results*"))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is lexicographic, not numeric sort.

if results_dirs:
poss_restarts = results_dirs[-1].glob(
f"{restart_stem.name}*.extxyz"
)

try:
last_restart = max(poss_restarts, key=getmtime)

Expand Down
18 changes: 17 additions & 1 deletion janus_core/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from io import StringIO
from logging import Logger
from pathlib import Path
import re
from typing import Any, Literal, TextIO

from ase import Atoms
Expand Down Expand Up @@ -110,8 +111,23 @@ def _get_default_prefix(
struct_name = struct.get_chemical_formula()

# Set default output directory
prefix = Path("./janus_results") / struct_name
results_dir = Path("./janus_results")
Comment thread
ElliottKasoar marked this conversation as resolved.
results_dirs = Path(".").glob("janus_results_*")

if results_dirs:
idx = (
int(match.group(0))
for dir in results_dirs
if (match := re.search("([0-9]+)$", str(dir)))
)
new_idx = max(idx, default=-1) + 1

if new_idx:
results_dir = Path(f"./janus_results_{new_idx}")
elif results_dir.exists():
results_dir = Path("./janus_results_1")
Comment thread
ElliottKasoar marked this conversation as resolved.

prefix = results_dir / struct_name
return "-".join((str(prefix), *filter(None, additional)))

def _build_filename(
Expand Down
33 changes: 33 additions & 0 deletions tests/test_filenamemixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pytest

from janus_core.helpers.utils import FileNameMixin
from tests.utils import chdir

DATA_PATH = Path(__file__).parent / "data"
STRUCT = read(DATA_PATH / "benzene.xyz")
Expand Down Expand Up @@ -125,3 +126,35 @@ def test_file_name_mixin_build(mixin_params, file_args, file_kwargs, file_name):
assert (
file_mix.build_filename(*file_args, **file_kwargs) == Path(file_name).absolute()
)


@pytest.mark.parametrize(
"existing_dirs, expected_new_dir",
(
((), "janus_results"),
(("janus_results",), "janus_results_1"),
(("janus_results", "janus_results_1"), "janus_results_2"),
(("janus_results", "janus_results_1", "janus_results_2"), "janus_results_3"),
# Check non-sequential output directories
(("janus_results_1",), "janus_results_2"),
(("janus_results_2",), "janus_results_3"),
(("janus_results_1", "janus_results_2"), "janus_results_3"),
(("janus_results", "janus_results_2"), "janus_results_3"),
# Check non-default output directories
(("janus_results1"), "janus_results"),
(("janus_results", "janus_results1"), "janus_results_1"),
(("janus_result"), "janus_results"),
(("janus_results_abc"), "janus_results"),
),
)
def test_new_results_dir(tmp_path, existing_dirs, expected_new_dir):
"""Test the creation of new results directories."""
with chdir(tmp_path):
for dir in existing_dirs:
Path(dir).mkdir(parents=True, exist_ok=True)

file_mix = DummyFileHandler(STRUCT, None, None)
assert (
file_mix.file_prefix.as_posix()
== (Path(expected_new_dir) / "C6H6").as_posix()
)
Loading