|
8 | 8 | from __future__ import annotations |
9 | 9 |
|
10 | 10 | import os |
| 11 | +import sys |
11 | 12 | import tempfile |
12 | 13 |
|
| 14 | +import hydra |
13 | 15 | import numpy as np |
14 | 16 | import pytest |
15 | 17 | from ase import Atoms |
16 | | -from fairchem.lammps.lammps_fc import restricted_cell_from_lammps_box |
| 18 | + |
| 19 | +pytest.importorskip("lammps") |
| 20 | + |
| 21 | +from fairchem.lammps import lammps_fc # noqa: E402 |
| 22 | +from fairchem.lammps.lammps_fc import restricted_cell_from_lammps_box # noqa: E402 |
17 | 23 |
|
18 | 24 |
|
19 | 25 | def create_lammps_data_file(filepath, positions, cell, atom_types, masses): |
@@ -230,3 +236,63 @@ def test_cell_conversion_preserves_volume(box_name, boxlo, boxhi, xy, yz, xz): |
230 | 236 | f"Volume mismatch for {box_name}: boxlo={boxlo}, boxhi={boxhi}, xy={xy}, yz={yz}, xz={xz}.\n" |
231 | 237 | f"Expected: {expected_volume}, Actual: {actual_volume}" |
232 | 238 | ) |
| 239 | + |
| 240 | + |
| 241 | +def test_lammps_hydra_entrypoint_startup(monkeypatch, tmp_path): |
| 242 | + """ |
| 243 | + Test that the Hydra-decorated LAMMPS entry point composes config and starts. |
| 244 | + """ |
| 245 | + hydra.core.global_hydra.GlobalHydra.instance().clear() |
| 246 | + lammps_state = {} |
| 247 | + run_args = {} |
| 248 | + |
| 249 | + def fake_instantiate(_cfg): |
| 250 | + return object() |
| 251 | + |
| 252 | + def fake_run_lammps_with_fairchem( |
| 253 | + predictor, |
| 254 | + lammps_input_path, |
| 255 | + task_name, |
| 256 | + charge=0, |
| 257 | + spin=0, |
| 258 | + ): |
| 259 | + run_args.update( |
| 260 | + { |
| 261 | + "predictor": predictor, |
| 262 | + "lammps_input_path": lammps_input_path, |
| 263 | + "task_name": task_name, |
| 264 | + "charge": charge, |
| 265 | + "spin": spin, |
| 266 | + } |
| 267 | + ) |
| 268 | + |
| 269 | + class DummyLammps: |
| 270 | + pass |
| 271 | + |
| 272 | + lmp = DummyLammps() |
| 273 | + lmp._predictor = predictor |
| 274 | + lammps_state["lmp"] = lmp |
| 275 | + return lmp |
| 276 | + |
| 277 | + monkeypatch.setattr(lammps_fc.hydra.utils, "instantiate", fake_instantiate) |
| 278 | + monkeypatch.setattr( |
| 279 | + lammps_fc, "run_lammps_with_fairchem", fake_run_lammps_with_fairchem |
| 280 | + ) |
| 281 | + |
| 282 | + old_argv = sys.argv[:] |
| 283 | + try: |
| 284 | + sys.argv = [ |
| 285 | + "lammps_fc.py", |
| 286 | + f"hydra.run.dir={tmp_path}", |
| 287 | + "hydra.output_subdir=null", |
| 288 | + ] |
| 289 | + lammps_fc.main() |
| 290 | + finally: |
| 291 | + sys.argv = old_argv |
| 292 | + hydra.core.global_hydra.GlobalHydra.instance().clear() |
| 293 | + |
| 294 | + assert run_args["lammps_input_path"] == "lammps_in_example.file" |
| 295 | + assert run_args["task_name"] == "omol" |
| 296 | + assert run_args["charge"] == 0 |
| 297 | + assert run_args["spin"] == 0 |
| 298 | + assert not hasattr(lammps_state["lmp"], "_predictor") |
0 commit comments