Skip to content

Commit

Permalink
Add test of phono3py.load that reads displacements
Browse files Browse the repository at this point in the history
  • Loading branch information
atztogo committed Jul 22, 2024
1 parent b4019dd commit f96dfab
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 20 deletions.
9 changes: 7 additions & 2 deletions phono3py/api_phono3py.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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
Expand Down
17 changes: 1 addition & 16 deletions phono3py/cui/create_force_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion phono3py/cui/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
11 changes: 11 additions & 0 deletions phono3py/phonon3/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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]
)
16 changes: 16 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions test/cui/test_phono3py_load.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion test/cui/test_phono3py_load_script.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Tests of Phono3py API."""
"""Tests of phono3py-load script."""

from __future__ import annotations

Expand Down

0 comments on commit f96dfab

Please sign in to comment.