Skip to content

Commit

Permalink
Add test of force constants calculation with QE calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
atztogo committed Nov 19, 2023
1 parent 3d50a56 commit c487eab
Showing 1 changed file with 75 additions and 11 deletions.
86 changes: 75 additions & 11 deletions test/cui/test_phono3py_load_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import pathlib
from collections.abc import Sequence
from dataclasses import dataclass, fields
from typing import Optional
from typing import Optional, Union

import h5py
import numpy as np
import pytest

from phono3py.cui.phono3py_script import main
Expand All @@ -19,7 +21,7 @@
class MockArgs:
"""Mock args of ArgumentParser."""

filename: Sequence[os.PathLike]
filename: Optional[Sequence[os.PathLike]] = None
conf_filename: Optional[os.PathLike] = None
fc_calculator: Optional[str] = None
force_sets_mode: bool = False
Expand All @@ -28,11 +30,22 @@ class MockArgs:
output_yaml_filename: Optional[os.PathLike] = None
show_num_triplets: bool = False
write_grid_points: bool = False
fc_symmetry: bool = True
cell_filename: Optional[str] = None
is_bterta: Optional[bool] = None
mesh_numbers: Optional[Sequence] = None
temperatures: Optional[Sequence] = None
input_filename = None
output_filename = None
input_output_filename = None

def __iter__(self):
"""Make self iterable to support in."""
return (getattr(self, field.name) for field in fields(self))

def __contains__(self, item):
return item in (field.name for field in fields(self))


def test_phono3py_load():
"""Test phono3py-load script."""
Expand Down Expand Up @@ -71,19 +84,70 @@ def test_phono3py_load_with_typeII_dataset(fc_calculator, exit_code):
file_path.unlink()


def _get_phono3py_load_args(phono3py_yaml_filepath, fc_calculator=None):
# Mock of ArgumentParser.args.
mockargs = MockArgs(
filename=[phono3py_yaml_filepath],
fc_calculator=fc_calculator,
log_level=1,
@pytest.mark.parametrize("load_phono3py_yaml", [True, False])
def test_phono3py_with_QE_calculator(load_phono3py_yaml):
"""Test phono3py-load script with QE calculator."""
argparse_control = _get_phono3py_load_args(
cwd / "phono3py_params-qe-Si222.yaml.xz",
load_phono3py_yaml=load_phono3py_yaml,
is_bterta=True,
temperatures=[
"300",
],
mesh_numbers=["11", "11", "11"],
)
with pytest.raises(SystemExit):
main(**argparse_control)

with h5py.File("kappa-m111111.hdf5") as f:
np.testing.assert_almost_equal(f["kappa"][0, 0], 118.93, decimal=1)

# Clean files created by phono3py/phono3py-load script.
for created_filename in (
"phono3py.yaml",
"fc2.hdf5",
"fc3.hdf5",
"kappa-m111111.hdf5",
):
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,
):
# Mock of ArgumentParser.args.
if load_phono3py_yaml:
mockargs = MockArgs(
filename=[phono3py_yaml_filepath],
fc_calculator=fc_calculator,
is_bterta=is_bterta,
temperatures=temperatures,
mesh_numbers=mesh_numbers,
log_level=1,
)
else:
mockargs = MockArgs(
filename=[],
fc_calculator=fc_calculator,
log_level=1,
cell_filename=phono3py_yaml_filepath,
is_bterta=is_bterta,
temperatures=temperatures,
mesh_numbers=mesh_numbers,
)

# See phono3py-load script.
argparse_control = {
"fc_symmetry": True,
"is_nac": True,
"load_phono3py_yaml": True,
"fc_symmetry": load_phono3py_yaml,
"is_nac": load_phono3py_yaml,
"load_phono3py_yaml": load_phono3py_yaml,
"args": mockargs,
}
return argparse_control

0 comments on commit c487eab

Please sign in to comment.