Skip to content

Commit

Permalink
Add pypolymlp CLI test
Browse files Browse the repository at this point in the history
  • Loading branch information
atztogo committed Sep 5, 2024
1 parent 591c822 commit a178e36
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
6 changes: 5 additions & 1 deletion phono3py/api_phono3py.py
Original file line number Diff line number Diff line change
Expand Up @@ -2209,7 +2209,11 @@ def develop_mlp(
else:
_params = params

if _params.ntrain is not None and _params.ntest is not None:
if (
_params is not None
and _params.ntrain is not None
and _params.ntest is not None
):
ntrain = _params.ntrain
ntest = _params.ntest
disps = self._mlp_dataset["displacements"]
Expand Down
6 changes: 4 additions & 2 deletions phono3py/cui/create_force_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ def run_pypolymlp_to_compute_forces(
displacement_distance: Optional[float] = None,
number_of_snapshots: Optional[int] = None,
random_seed: Optional[int] = None,
mlp_filename: str = "pypolymlp.mlp",
mlp_filename: str = "phono3py.pmlp",
log_level: int = 0,
):
"""Run pypolymlp to compute forces."""
Expand All @@ -533,6 +533,9 @@ def run_pypolymlp_to_compute_forces(
if log_level:
print("Developing MLPs by pypolymlp...", flush=True)
ph3py.develop_mlp(params=mlp_params)
ph3py.mlp.save_mlp(filename=mlp_filename)
if log_level:
print(f'MLPs were written into "{mlp_filename}"', flush=True)
else:
if pathlib.Path(mlp_filename).exists():
if log_level:
Expand Down Expand Up @@ -581,7 +584,6 @@ def run_pypolymlp_to_compute_forces(
raise RuntimeError("Displacements are not set. Run generate_displacements.")

ph3py.evaluate_mlp()
ph3py.save("phono3py_mlp_eval_dataset.yaml")


def run_pypolymlp_to_compute_phonon_forces(
Expand Down
8 changes: 8 additions & 0 deletions phono3py/cui/phono3py_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ def finalize_phono3py(
else:
yaml_filename = filename

if phono3py.mlp_dataset is not None:
mlp_eval_filename = "phono3py_mlp_eval_dataset.yaml"
if log_level:
print(
f'Dataset generated using MMLPs was written in "{mlp_eval_filename}".'
)
phono3py.save(mlp_eval_filename)

_physical_units = get_default_physical_units(phono3py.calculator)

ph3py_yaml = Phono3pyYaml(
Expand Down
1 change: 1 addition & 0 deletions test/api/test_api_phono3py.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ def test_type2_forces_energies_setter_Si(si_111_222_rd: Phono3py):
def test_use_pypolymlp_mgo(mgo_222rd_444rd: Phono3py):
"""Test use_pypolymlp in produce_fc3."""
pytest.importorskip("pypolymlp")
pytest.importorskip("symfc")

ph3_in = mgo_222rd_444rd
ph3 = Phono3py(
Expand Down
52 changes: 52 additions & 0 deletions test/cui/test_phono3py_load_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class MockArgs:
is_bterta: Optional[bool] = None
mesh_numbers: Optional[Sequence] = None
temperatures: Optional[Sequence] = None
use_pypolymlp: bool = False
input_filename = None
output_filename = None
input_output_filename = None
Expand Down Expand Up @@ -114,13 +115,62 @@ def test_phono3py_with_QE_calculator(load_phono3py_yaml):
file_path.unlink()


def test_phono3py_load_with_pypolymlp_si():
"""Test phono3py-load script with pypolymlp.
First run generates phono3py.pmlp.
Second run uses phono3py.pmlp.
"""
pytest.importorskip("pypolymlp")
pytest.importorskip("symfc")

argparse_control = _get_phono3py_load_args(
cwd / ".." / "phono3py_params_Si-111-222-rd.yaml.xz",
fc_calculator="symfc",
use_pypolymlp=True,
)

with pytest.raises(SystemExit) as excinfo:
main(**argparse_control)
assert excinfo.value.code == 0

# phono3py.yaml and fc2.hd5 are used in the next run. So they are not deleted.
for created_filename in ("fc3.hdf5", "phono3py_mlp_eval_dataset.yaml"):
file_path = pathlib.Path(cwd_called / created_filename)
if file_path.exists():
file_path.unlink()

argparse_control = _get_phono3py_load_args(
cwd / "phono3py.yaml",
fc_calculator="symfc",
use_pypolymlp=True,
)

with pytest.raises(SystemExit) as excinfo:
main(**argparse_control)
assert excinfo.value.code == 0

for created_filename in (
"phono3py.yaml",
"fc2.hdf5",
"fc3.hdf5",
"phono3py.pmlp",
"phono3py_mlp_eval_dataset.yaml",
):
file_path = pathlib.Path(cwd_called / created_filename)
if file_path.exists():
file_path.unlink()


def _get_phono3py_load_args(
phono3py_yaml_filepath: Union[str, pathlib.Path],
fc_calculator: Optional[str] = None,
load_phono3py_yaml: bool = True,
is_bterta: bool = False,
temperatures: Optional[Sequence] = None,
mesh_numbers: Optional[Sequence] = None,
use_pypolymlp: bool = False,
):
# Mock of ArgumentParser.args.
if load_phono3py_yaml:
Expand All @@ -130,6 +180,7 @@ def _get_phono3py_load_args(
is_bterta=is_bterta,
temperatures=temperatures,
mesh_numbers=mesh_numbers,
use_pypolymlp=use_pypolymlp,
log_level=1,
)
else:
Expand All @@ -141,6 +192,7 @@ def _get_phono3py_load_args(
is_bterta=is_bterta,
temperatures=temperatures,
mesh_numbers=mesh_numbers,
use_pypolymlp=use_pypolymlp,
)

# See phono3py-load script.
Expand Down

0 comments on commit a178e36

Please sign in to comment.