From f96dfab5f7c702796a1f276736419b667336a953 Mon Sep 17 00:00:00 2001 From: Atsushi Togo Date: Mon, 22 Jul 2024 13:32:30 +0900 Subject: [PATCH] Add test of phono3py.load that reads displacements --- phono3py/api_phono3py.py | 9 +++++++-- phono3py/cui/create_force_constants.py | 17 +---------------- phono3py/cui/load.py | 2 +- phono3py/phonon3/dataset.py | 11 +++++++++++ test/conftest.py | 16 ++++++++++++++++ test/cui/test_phono3py_load.py | 18 ++++++++++++++++++ test/cui/test_phono3py_load_script.py | 2 +- 7 files changed, 55 insertions(+), 20 deletions(-) create mode 100644 test/cui/test_phono3py_load.py diff --git a/phono3py/api_phono3py.py b/phono3py/api_phono3py.py index 51c5b592..294ac2dd 100644 --- a/phono3py/api_phono3py.py +++ b/phono3py/api_phono3py.py @@ -81,7 +81,7 @@ from phono3py.interface.fc_calculator import get_fc3 from phono3py.interface.phono3py_yaml import Phono3pyYaml from phono3py.phonon.grid import BZGrid -from phono3py.phonon3.dataset import get_displacements_and_forces_fc3 +from phono3py.phonon3.dataset import forces_in_dataset, get_displacements_and_forces_fc3 from phono3py.phonon3.displacement_fc3 import ( direction_to_displacement, get_third_order_displacements, @@ -866,7 +866,7 @@ def displacements(self): for disp2 in disp1["second_atoms"]: displacements[i, disp2["number"]] = disp2["displacement"] i += 1 - elif "forces" in dataset or "displacements" in dataset: + elif "displacements" in dataset: displacements = dataset["displacements"] else: raise RuntimeError("displacement dataset has wrong format.") @@ -2546,6 +2546,11 @@ def _get_forces_energies( Return None if tagert data is not found rather than raising exception. """ + if self._dataset is None: + return None + if not forces_in_dataset(self._dataset): + return None + if target in self._dataset: # type-2 return self._dataset[target] elif "first_atoms" in self._dataset: # type-1 diff --git a/phono3py/cui/create_force_constants.py b/phono3py/cui/create_force_constants.py index d0fb4d44..fbe9a5e3 100644 --- a/phono3py/cui/create_force_constants.py +++ b/phono3py/cui/create_force_constants.py @@ -68,6 +68,7 @@ ) from phono3py.interface.fc_calculator import extract_fc2_fc3_calculators from phono3py.interface.phono3py_yaml import Phono3pyYaml +from phono3py.phonon3.dataset import forces_in_dataset from phono3py.phonon3.fc3 import ( set_permutation_symmetry_fc3, set_translational_invariance_fc3, @@ -316,22 +317,6 @@ def parse_forces( return dataset -def forces_in_dataset(dataset: dict) -> bool: - """Return whether forces in dataset or not.""" - if dataset is None: - return False - return "forces" in dataset or ( - "first_atoms" in dataset and "forces" in dataset["first_atoms"][0] - ) - - -def displacements_in_dataset(dataset: Optional[dict]) -> bool: - """Return whether displacements in dataset or not.""" - if dataset is None: - return False - return "displacements" in dataset or "first_atoms" in dataset - - def get_fc_calculator_params(settings, log_level=0): """Return fc_calculator and fc_calculator_params from settings.""" fc_calculator = None diff --git a/phono3py/cui/load.py b/phono3py/cui/load.py index 12fb9cbf..a5acd263 100644 --- a/phono3py/cui/load.py +++ b/phono3py/cui/load.py @@ -49,13 +49,13 @@ from phono3py import Phono3py from phono3py.cui.create_force_constants import ( - forces_in_dataset, parse_forces, run_pypolymlp_to_compute_forces, ) from phono3py.file_IO import read_fc2_from_hdf5, read_fc3_from_hdf5 from phono3py.interface.fc_calculator import extract_fc2_fc3_calculators from phono3py.interface.phono3py_yaml import Phono3pyYaml +from phono3py.phonon3.dataset import forces_in_dataset from phono3py.phonon3.fc3 import show_drift_fc3 diff --git a/phono3py/phonon3/dataset.py b/phono3py/phonon3/dataset.py index 74fdea86..777910f9 100644 --- a/phono3py/phonon3/dataset.py +++ b/phono3py/phonon3/dataset.py @@ -34,6 +34,8 @@ # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. +from typing import Optional + import numpy as np @@ -94,3 +96,12 @@ def get_displacements_and_forces_fc3(disp_dataset): return disp_dataset["displacements"], disp_dataset["forces"] else: raise RuntimeError("disp_dataset doesn't contain correct information.") + + +def forces_in_dataset(dataset: Optional[dict]) -> bool: + """Return whether forces in dataset or not.""" + if dataset is None: + return False + return "forces" in dataset or ( + "first_atoms" in dataset and "forces" in dataset["first_atoms"][0] + ) diff --git a/test/conftest.py b/test/conftest.py index 61c2408d..340eda03 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -69,6 +69,22 @@ def si_pbesol(request) -> Phono3py: ) +@pytest.fixture(scope="session") +def si_pbesol_without_forcesets(request) -> Phono3py: + """Return Phono3py instance of Si 2x2x2 without force sets. + + * with symmetry + + """ + yaml_filename = cwd / "phono3py_si_pbesol.yaml" + enable_v2 = request.config.getoption("--v2") + return phono3py.load( + yaml_filename, + make_r0_average=not enable_v2, + log_level=1, + ) + + @pytest.fixture(scope="session") def si_pbesol_grg(request) -> Phono3py: """Return Phono3py instance of Si 2x2x2. diff --git a/test/cui/test_phono3py_load.py b/test/cui/test_phono3py_load.py new file mode 100644 index 00000000..4f7df4aa --- /dev/null +++ b/test/cui/test_phono3py_load.py @@ -0,0 +1,18 @@ +"""Tests of Phono3py load.""" + +from __future__ import annotations + +from phono3py import Phono3py + + +def test_phono3py_load(si_pbesol_without_forcesets: Phono3py): + """Test phono3py.load. + + Check phono3py.load can read displacements from phono3py_disp.yaml like file that + doesn't contain forces. + + """ + ph3 = si_pbesol_without_forcesets + assert ph3.dataset is not None + assert ph3.displacements.shape == (111, 64, 3) + assert ph3.forces is None diff --git a/test/cui/test_phono3py_load_script.py b/test/cui/test_phono3py_load_script.py index e6987194..4437e06d 100644 --- a/test/cui/test_phono3py_load_script.py +++ b/test/cui/test_phono3py_load_script.py @@ -1,4 +1,4 @@ -"""Tests of Phono3py API.""" +"""Tests of phono3py-load script.""" from __future__ import annotations