Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
83 changes: 83 additions & 0 deletions source/lmp/tests/model_convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
"""Helpers for preparing converted TensorFlow graph files in LAMMPS tests."""

from __future__ import (
annotations,
)

import os
import subprocess as sp
import sys
import tempfile
import time
from pathlib import (
Path,
)

_LOCK_TIMEOUT_SECONDS = 60.0
_LOCK_POLL_SECONDS = 0.1


def _is_up_to_date(source: Path, output: Path) -> bool:
return output.exists() and output.stat().st_mtime_ns >= source.stat().st_mtime_ns


def ensure_converted_pb(source: Path, output: Path) -> Path:
"""Convert ``source`` into ``output`` only when the target is missing or stale.

The conversion is protected by a simple lock file and uses atomic replacement so
repeated imports across multiple test modules do not regenerate the same model
more than once.
"""
Comment thread
njzjz-bot marked this conversation as resolved.
source = source.resolve()
output = output.resolve()
output.parent.mkdir(parents=True, exist_ok=True)
lock_file = output.with_name(f".{output.name}.lock")
started = time.monotonic()

while True:
if _is_up_to_date(source, output):
return output
try:
fd = os.open(str(lock_file), os.O_CREAT | os.O_EXCL | os.O_WRONLY)
except FileExistsError:
if time.monotonic() - started >= _LOCK_TIMEOUT_SECONDS:
raise TimeoutError(f"Timed out waiting for {lock_file}")
Comment thread
njzjz-bot marked this conversation as resolved.
Outdated
time.sleep(_LOCK_POLL_SECONDS)
Comment thread
njzjz-bot marked this conversation as resolved.
continue
break
Comment thread
coderabbitai[bot] marked this conversation as resolved.

tmp_path: Path | None = None
try:
with os.fdopen(fd, "w", encoding="utf-8") as handle:
handle.write(f"pid={os.getpid()}\n")

if _is_up_to_date(source, output):
return output

tmp_fd, tmp_name = tempfile.mkstemp(
dir=output.parent,
prefix=f".{output.name}.",
)
os.close(tmp_fd)
tmp_path = Path(tmp_name)
sp.check_output(
[
sys.executable,
"-m",
"deepmd",
"convert-from",
"pbtxt",
"-i",
str(source),
"-o",
str(tmp_path),
]
)
Comment thread
njzjz-bot marked this conversation as resolved.
Outdated
Comment thread
coderabbitai[bot] marked this conversation as resolved.
tmp_path.replace(output)
tmp_path = None
return output
finally:
if tmp_path is not None:
tmp_path.unlink(missing_ok=True)
lock_file.unlink(missing_ok=True)
13 changes: 5 additions & 8 deletions source/lmp/tests/test_deeptensor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
import os
import subprocess as sp
import sys
from pathlib import (
Path,
)
Expand All @@ -12,6 +10,9 @@
from lammps import (
PyLammps,
)
from model_convert import (
ensure_converted_pb,
)
from write_lmp_data import (
write_lmp_data,
)
Expand Down Expand Up @@ -56,13 +57,9 @@
# type_HO = np.array([2, 1, 1, 2, 1, 1])


sp.check_output(
f"{sys.executable} -m deepmd convert-from pbtxt -i {pbtxt_file.resolve()} -o {pb_file.resolve()}".split()
)
ensure_converted_pb(pbtxt_file, pb_file)

sp.check_output(
f"{sys.executable} -m deepmd convert-from pbtxt -i {pbtxt_file2.resolve()} -o {pb_file2.resolve()}".split()
)
ensure_converted_pb(pbtxt_file2, pb_file2)


def setup_module() -> None:
Expand Down
10 changes: 5 additions & 5 deletions source/lmp/tests/test_dplr.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
import os
import subprocess as sp
import sys
from pathlib import (
Path,
)
Expand All @@ -12,6 +10,9 @@
from lammps import (
PyLammps,
)
from model_convert import (
ensure_converted_pb,
)
from write_lmp_data import (
write_lmp_data_full,
)
Expand Down Expand Up @@ -265,9 +266,8 @@
mesh = 10


sp.check_output(
f"{sys.executable} -m deepmd convert-from pbtxt -i {pbtxt_file.resolve()} -o {pb_file.resolve()}".split()
)
ensure_converted_pb(pbtxt_file, pb_file)
ensure_converted_pb(dipole_pbtxt_file, dipole_pb_file)


def setup_module() -> None:
Expand Down
12 changes: 5 additions & 7 deletions source/lmp/tests/test_lammps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import importlib
import os
import shutil
import subprocess as sp
import sys
import tempfile
from pathlib import (
Expand All @@ -15,6 +14,9 @@
from lammps import (
PyLammps,
)
from model_convert import (
ensure_converted_pb,
)
from write_lmp_data import (
write_lmp_data,
)
Expand Down Expand Up @@ -221,12 +223,8 @@
type_HO = np.array([2, 1, 1, 2, 1, 1])


sp.check_output(
f"{sys.executable} -m deepmd convert-from pbtxt -i {pbtxt_file.resolve()} -o {pb_file.resolve()}".split()
)
sp.check_output(
f"{sys.executable} -m deepmd convert-from pbtxt -i {pbtxt_file2.resolve()} -o {pb_file2.resolve()}".split()
)
ensure_converted_pb(pbtxt_file, pb_file)
ensure_converted_pb(pbtxt_file2, pb_file2)


def setup_module() -> None:
Expand Down
13 changes: 5 additions & 8 deletions source/lmp/tests/test_lammps_3types.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
import os
import subprocess as sp
import sys
from pathlib import (
Path,
)
Expand All @@ -11,6 +9,9 @@
from lammps import (
PyLammps,
)
from model_convert import (
ensure_converted_pb,
)
from write_lmp_data import (
write_lmp_data,
)
Expand Down Expand Up @@ -244,12 +245,8 @@
# https://github.com/lammps/lammps/blob/1e1311cf401c5fc2614b5d6d0ff3230642b76597/src/update.cpp#L193
nktv2p = 1.6021765e6

sp.check_output(
f"{sys.executable} -m deepmd convert-from pbtxt -i {pbtxt_file.resolve()} -o {pb_file.resolve()}".split()
)
sp.check_output(
f"{sys.executable} -m deepmd convert-from pbtxt -i {pbtxt_file2.resolve()} -o {pb_file2.resolve()}".split()
)
ensure_converted_pb(pbtxt_file, pb_file)
ensure_converted_pb(pbtxt_file2, pb_file2)


def setup_module() -> None:
Expand Down
8 changes: 4 additions & 4 deletions source/lmp/tests/test_lammps_dpa_jax.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import importlib
import os
import shutil
import subprocess as sp
import sys
import tempfile
from pathlib import (
Expand All @@ -15,6 +14,9 @@
from lammps import (
PyLammps,
)
from model_convert import (
ensure_converted_pb,
)
from write_lmp_data import (
write_lmp_data,
)
Expand Down Expand Up @@ -222,9 +224,7 @@
type_HO = np.array([2, 1, 1, 2, 1, 1])


sp.check_output(
f"{sys.executable} -m deepmd convert-from pbtxt -i {pbtxt_file2.resolve()} -o {pb_file2.resolve()}".split()
)
ensure_converted_pb(pbtxt_file2, pb_file2)


def setup_module():
Expand Down
8 changes: 4 additions & 4 deletions source/lmp/tests/test_lammps_dpa_pt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import importlib
import os
import shutil
import subprocess as sp
import sys
import tempfile
from pathlib import (
Expand All @@ -15,6 +14,9 @@
from lammps import (
PyLammps,
)
from model_convert import (
ensure_converted_pb,
)
from write_lmp_data import (
write_lmp_data,
)
Expand Down Expand Up @@ -220,9 +222,7 @@
type_HO = np.array([2, 1, 1, 2, 1, 1])


sp.check_output(
f"{sys.executable} -m deepmd convert-from pbtxt -i {pbtxt_file2.resolve()} -o {pb_file2.resolve()}".split()
)
ensure_converted_pb(pbtxt_file2, pb_file2)


def setup_module() -> None:
Expand Down
8 changes: 4 additions & 4 deletions source/lmp/tests/test_lammps_dpa_pt_nopbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import importlib
import os
import shutil
import subprocess as sp
import sys
import tempfile
from pathlib import (
Expand All @@ -15,6 +14,9 @@
from lammps import (
PyLammps,
)
from model_convert import (
ensure_converted_pb,
)
from write_lmp_data import (
write_lmp_data,
)
Expand Down Expand Up @@ -218,9 +220,7 @@
type_HO = np.array([2, 1, 1, 2, 1, 1])


sp.check_output(
f"{sys.executable} -m deepmd convert-from pbtxt -i {pbtxt_file2.resolve()} -o {pb_file2.resolve()}".split()
)
ensure_converted_pb(pbtxt_file2, pb_file2)


def setup_module() -> None:
Expand Down
8 changes: 4 additions & 4 deletions source/lmp/tests/test_lammps_dpa_sel_pt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import importlib
import os
import shutil
import subprocess as sp
import sys
import tempfile
from pathlib import (
Expand All @@ -15,6 +14,9 @@
from lammps import (
PyLammps,
)
from model_convert import (
ensure_converted_pb,
)
from write_lmp_data import (
write_lmp_data,
)
Expand Down Expand Up @@ -223,9 +225,7 @@
type_HO = np.array([2, 1, 1, 2, 1, 1])


sp.check_output(
f"{sys.executable} -m deepmd convert-from pbtxt -i {pbtxt_file2.resolve()} -o {pb_file2.resolve()}".split()
)
ensure_converted_pb(pbtxt_file2, pb_file2)


def setup_module() -> None:
Expand Down
9 changes: 4 additions & 5 deletions source/lmp/tests/test_lammps_faparam.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
"""Test LAMMPS fparam and aparam input."""

import os
import subprocess as sp
import sys
from pathlib import (
Path,
)
Expand All @@ -14,6 +12,9 @@
from lammps import (
PyLammps,
)
from model_convert import (
ensure_converted_pb,
)
from write_lmp_data import (
write_lmp_data,
)
Expand Down Expand Up @@ -134,9 +135,7 @@
type_OH = np.array([1, 1, 1, 1, 1, 1])


sp.check_output(
f"{sys.executable} -m deepmd convert-from pbtxt -i {pbtxt_file.resolve()} -o {pb_file.resolve()}".split()
)
ensure_converted_pb(pbtxt_file, pb_file)


def setup_module() -> None:
Expand Down
8 changes: 4 additions & 4 deletions source/lmp/tests/test_lammps_jax.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import importlib
import os
import shutil
import subprocess as sp
import sys
import tempfile
from pathlib import (
Expand All @@ -15,6 +14,9 @@
from lammps import (
PyLammps,
)
from model_convert import (
ensure_converted_pb,
)
from write_lmp_data import (
write_lmp_data,
)
Expand Down Expand Up @@ -222,9 +224,7 @@
type_HO = np.array([2, 1, 1, 2, 1, 1])


sp.check_output(
f"{sys.executable} -m deepmd convert-from pbtxt -i {pbtxt_file2.resolve()} -o {pb_file2.resolve()}".split()
)
ensure_converted_pb(pbtxt_file2, pb_file2)


def setup_module():
Expand Down
Loading
Loading