Skip to content

Commit

Permalink
Merge pull request #271 from phonopy/pmlp-cli-test
Browse files Browse the repository at this point in the history
Add pypolymlp CLI test
  • Loading branch information
atztogo authored Sep 5, 2024
2 parents 591c822 + 998a660 commit 853e3ce
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/phono3py-pytest-conda-mkl-phphmtblas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
- name: Install phonopy develop branch
run: |
conda activate test
git clone --depth 1 https://github.com/phonopy/phonopy.git
git clone https://github.com/phonopy/phonopy.git
cd phonopy
pip install -e . -vvv
cd ..
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/phono3py-pytest-conda-mkl-v2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
- name: Install phonopy develop branch
run: |
conda activate test
git clone --depth 1 https://github.com/phonopy/phonopy.git
git clone https://github.com/phonopy/phonopy.git
cd phonopy
pip install -e . -vvv
cd ..
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/phono3py-pytest-conda-mkl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
- name: Install phonopy develop branch
run: |
conda activate test
git clone --depth 1 https://github.com/phonopy/phonopy.git
git clone https://github.com/phonopy/phonopy.git
cd phonopy
pip install -e . -vvv
cd ..
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/phono3py-pytest-conda-numpy2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ jobs:
- name: Install symfc develop branch
run: |
conda activate test
git clone --depth 1 https://github.com/symfc/symfc.git
git clone https://github.com/symfc/symfc.git
cd symfc
pip install -e . -vvv
cd ..
- name: Install phonopy develop branch
run: |
conda activate test
git clone --depth 1 https://github.com/phonopy/phonopy.git
git clone https://github.com/phonopy/phonopy.git
cd phonopy
pip install -e . -vvv
cd ..
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/phono3py-pytest-conda-phphmtblas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ jobs:
- name: Install symfc develop branch
run: |
conda activate test
git clone --depth 1 https://github.com/symfc/symfc.git
git clone https://github.com/symfc/symfc.git
cd symfc
pip install -e . -vvv
cd ..
- name: Install phonopy develop branch
run: |
conda activate test
git clone --depth 1 https://github.com/phonopy/phonopy.git
git clone https://github.com/phonopy/phonopy.git
cd phonopy
pip install -e . -vvv
cd ..
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/phono3py-pytest-conda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Install phonopy develop branch
run: |
conda activate test
git clone --depth 1 https://github.com/phonopy/phonopy.git
git clone https://github.com/phonopy/phonopy.git
cd phonopy
pip install -e . -vvv
cd ..
Expand Down
8 changes: 6 additions & 2 deletions 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 Expand Up @@ -2248,7 +2252,7 @@ def develop_mlp(
verbose=self._log_level - 1 > 0,
)

def load_mlp(self, filename: str = "pypolymlp.mlp"):
def load_mlp(self, filename: str = "phono3py.pmlp"):
"""Load machine learning potential of pypolymlp."""
self._mlp = load_polymlp(filename=filename)

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_called / "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 853e3ce

Please sign in to comment.