From a178e369fbbe89618d3e216c979c1bdda46731fc Mon Sep 17 00:00:00 2001 From: Atsushi Togo Date: Thu, 5 Sep 2024 10:58:29 +0900 Subject: [PATCH 1/3] Add pypolymlp CLI test --- phono3py/api_phono3py.py | 6 ++- phono3py/cui/create_force_constants.py | 6 ++- phono3py/cui/phono3py_script.py | 8 ++++ test/api/test_api_phono3py.py | 1 + test/cui/test_phono3py_load_script.py | 52 ++++++++++++++++++++++++++ 5 files changed, 70 insertions(+), 3 deletions(-) diff --git a/phono3py/api_phono3py.py b/phono3py/api_phono3py.py index 4dc46d22..9ded6132 100644 --- a/phono3py/api_phono3py.py +++ b/phono3py/api_phono3py.py @@ -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"] diff --git a/phono3py/cui/create_force_constants.py b/phono3py/cui/create_force_constants.py index c68c836a..94c82b46 100644 --- a/phono3py/cui/create_force_constants.py +++ b/phono3py/cui/create_force_constants.py @@ -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.""" @@ -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: @@ -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( diff --git a/phono3py/cui/phono3py_script.py b/phono3py/cui/phono3py_script.py index 3d767e3e..2f699db2 100644 --- a/phono3py/cui/phono3py_script.py +++ b/phono3py/cui/phono3py_script.py @@ -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( diff --git a/test/api/test_api_phono3py.py b/test/api/test_api_phono3py.py index beb63677..98fb2bfa 100644 --- a/test/api/test_api_phono3py.py +++ b/test/api/test_api_phono3py.py @@ -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( diff --git a/test/cui/test_phono3py_load_script.py b/test/cui/test_phono3py_load_script.py index 4437e06d..cd8a4d32 100644 --- a/test/cui/test_phono3py_load_script.py +++ b/test/cui/test_phono3py_load_script.py @@ -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 @@ -114,6 +115,54 @@ 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, @@ -121,6 +170,7 @@ def _get_phono3py_load_args( 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: @@ -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: @@ -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. From 92932847d6f3584cbea22010f8f13e7ded8647c2 Mon Sep 17 00:00:00 2001 From: Atsushi Togo Date: Thu, 5 Sep 2024 13:08:31 +0900 Subject: [PATCH 2/3] Fix test --- .github/workflows/phono3py-pytest-conda-mkl-phphmtblas.yml | 2 +- .github/workflows/phono3py-pytest-conda-mkl-v2.yml | 2 +- .github/workflows/phono3py-pytest-conda-mkl.yml | 2 +- .github/workflows/phono3py-pytest-conda-numpy2.yml | 2 +- .github/workflows/phono3py-pytest-conda-phphmtblas.yml | 2 +- .github/workflows/phono3py-pytest-conda.yml | 2 +- phono3py/api_phono3py.py | 2 +- test/cui/test_phono3py_load_script.py | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.github/workflows/phono3py-pytest-conda-mkl-phphmtblas.yml b/.github/workflows/phono3py-pytest-conda-mkl-phphmtblas.yml index f32a9b58..a262cdf6 100644 --- a/.github/workflows/phono3py-pytest-conda-mkl-phphmtblas.yml +++ b/.github/workflows/phono3py-pytest-conda-mkl-phphmtblas.yml @@ -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 .. diff --git a/.github/workflows/phono3py-pytest-conda-mkl-v2.yml b/.github/workflows/phono3py-pytest-conda-mkl-v2.yml index 7a285923..a6d5c8d5 100644 --- a/.github/workflows/phono3py-pytest-conda-mkl-v2.yml +++ b/.github/workflows/phono3py-pytest-conda-mkl-v2.yml @@ -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 .. diff --git a/.github/workflows/phono3py-pytest-conda-mkl.yml b/.github/workflows/phono3py-pytest-conda-mkl.yml index a9be299a..f42b8264 100644 --- a/.github/workflows/phono3py-pytest-conda-mkl.yml +++ b/.github/workflows/phono3py-pytest-conda-mkl.yml @@ -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 .. diff --git a/.github/workflows/phono3py-pytest-conda-numpy2.yml b/.github/workflows/phono3py-pytest-conda-numpy2.yml index fdb106b2..6265efaf 100644 --- a/.github/workflows/phono3py-pytest-conda-numpy2.yml +++ b/.github/workflows/phono3py-pytest-conda-numpy2.yml @@ -35,7 +35,7 @@ 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 .. diff --git a/.github/workflows/phono3py-pytest-conda-phphmtblas.yml b/.github/workflows/phono3py-pytest-conda-phphmtblas.yml index 59f41d31..5c2dc672 100644 --- a/.github/workflows/phono3py-pytest-conda-phphmtblas.yml +++ b/.github/workflows/phono3py-pytest-conda-phphmtblas.yml @@ -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 .. diff --git a/.github/workflows/phono3py-pytest-conda.yml b/.github/workflows/phono3py-pytest-conda.yml index ad620453..1df4515a 100644 --- a/.github/workflows/phono3py-pytest-conda.yml +++ b/.github/workflows/phono3py-pytest-conda.yml @@ -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 .. diff --git a/phono3py/api_phono3py.py b/phono3py/api_phono3py.py index 9ded6132..b42d4a16 100644 --- a/phono3py/api_phono3py.py +++ b/phono3py/api_phono3py.py @@ -2252,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) diff --git a/test/cui/test_phono3py_load_script.py b/test/cui/test_phono3py_load_script.py index cd8a4d32..c3b751b1 100644 --- a/test/cui/test_phono3py_load_script.py +++ b/test/cui/test_phono3py_load_script.py @@ -142,7 +142,7 @@ def test_phono3py_load_with_pypolymlp_si(): file_path.unlink() argparse_control = _get_phono3py_load_args( - cwd / "phono3py.yaml", + cwd_called / "phono3py.yaml", fc_calculator="symfc", use_pypolymlp=True, ) From 998a660e6a8d3865825432ea13158bcb64de6aeb Mon Sep 17 00:00:00 2001 From: Atsushi Togo Date: Thu, 5 Sep 2024 13:14:21 +0900 Subject: [PATCH 3/3] Fix github workflows --- .github/workflows/phono3py-pytest-conda-numpy2.yml | 2 +- .github/workflows/phono3py-pytest-conda-phphmtblas.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/phono3py-pytest-conda-numpy2.yml b/.github/workflows/phono3py-pytest-conda-numpy2.yml index 6265efaf..85aee8be 100644 --- a/.github/workflows/phono3py-pytest-conda-numpy2.yml +++ b/.github/workflows/phono3py-pytest-conda-numpy2.yml @@ -42,7 +42,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 .. diff --git a/.github/workflows/phono3py-pytest-conda-phphmtblas.yml b/.github/workflows/phono3py-pytest-conda-phphmtblas.yml index 5c2dc672..5771b33b 100644 --- a/.github/workflows/phono3py-pytest-conda-phphmtblas.yml +++ b/.github/workflows/phono3py-pytest-conda-phphmtblas.yml @@ -28,7 +28,7 @@ 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 ..