Add test of phono3py.load that reads displacements

This commit is contained in:
Atsushi Togo 2024-07-22 13:32:30 +09:00
parent b4019dd312
commit f96dfab5f7
7 changed files with 55 additions and 20 deletions

View File

@ -81,7 +81,7 @@ from phono3py.conductivity.rta import get_thermal_conductivity_RTA
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 @@ class Phono3py:
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 @@ class Phono3py:
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

View File

@ -68,6 +68,7 @@ from phono3py.file_IO import (
)
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

View File

@ -49,13 +49,13 @@ from phonopy.structure.cells import determinant
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

View File

@ -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]
)

View File

@ -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.

View File

@ -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

View File

@ -1,4 +1,4 @@
"""Tests of Phono3py API."""
"""Tests of phono3py-load script."""
from __future__ import annotations