mirror of https://github.com/phonopy/phono3py.git
Merge pull request #396 from phonopy/number-estimation-factor
Add number_estimation_factor parameter for rd displacement generation
This commit is contained in:
commit
200e1b92a9
|
@ -1253,13 +1253,14 @@ class Phono3py:
|
|||
|
||||
def generate_displacements(
|
||||
self,
|
||||
distance: Optional[float] = None,
|
||||
cutoff_pair_distance: Optional[float] = None,
|
||||
is_plusminus: Union[bool, str] = "auto",
|
||||
distance: float | None = None,
|
||||
cutoff_pair_distance: float | None = None,
|
||||
is_plusminus: bool | str = "auto",
|
||||
is_diagonal: bool = True,
|
||||
number_of_snapshots: Optional[Union[int, Literal["auto"]]] = None,
|
||||
random_seed: Optional[int] = None,
|
||||
max_distance: Optional[float] = None,
|
||||
number_of_snapshots: int | Literal["auto"] | None = None,
|
||||
random_seed: int | None = None,
|
||||
max_distance: float | None = None,
|
||||
number_estimation_factor: float | None = None,
|
||||
):
|
||||
"""Generate displacement dataset in supercell for fc3.
|
||||
|
||||
|
@ -1307,7 +1308,7 @@ class Phono3py:
|
|||
pair of displacements is considered to calculate fc3 or not. Default
|
||||
is None, which means cutoff is not used.
|
||||
is_plusminus : True, False, or 'auto', optional
|
||||
With True, atomis are displaced in both positive and negative
|
||||
With True, atoms are displaced in both positive and negative
|
||||
directions. With False, only one direction. With 'auto', mostly
|
||||
equivalent to is_plusminus=True, but only one direction is chosen
|
||||
when the displacements in both directions are symmetrically
|
||||
|
@ -1328,9 +1329,15 @@ class Phono3py:
|
|||
random_seed : int or None, optional
|
||||
Random seed for random displacements generation. Default is None.
|
||||
max_distance : float or None, optional
|
||||
In random direction and distance displacements generation, this
|
||||
value is specified. In random direction and random distance
|
||||
displacements generation, this value is used as `max_distance`.
|
||||
When specified, displacements are generated with random direction
|
||||
and random distance. This value serves as the maximum distance,
|
||||
while the `distance` parameter sets the minimum distance. The
|
||||
displacement distance is randomly sampled from a uniform
|
||||
distribution between these two bounds.
|
||||
number_estimation_factor : float, optional
|
||||
This factor multiplies the number of snapshots estimated by symfc
|
||||
when `number_of_snapshots` is set to "auto". Default is None, which
|
||||
sets this factor to 8 when `max_distance` is specified, otherwise 4.
|
||||
|
||||
"""
|
||||
if distance is None:
|
||||
|
@ -1346,14 +1353,19 @@ class Phono3py:
|
|||
options = None
|
||||
else:
|
||||
options = {"cutoff": {3: cutoff_pair_distance}}
|
||||
_number_of_snapshots = (
|
||||
SymfcFCSolver(
|
||||
self._supercell,
|
||||
symmetry=self._symmetry,
|
||||
options=options,
|
||||
).estimate_numbers_of_supercells(orders=[3])[3]
|
||||
* 2
|
||||
)
|
||||
_number_of_snapshots = SymfcFCSolver(
|
||||
self._supercell,
|
||||
symmetry=self._symmetry,
|
||||
options=options,
|
||||
).estimate_numbers_of_supercells(orders=[3])[3]
|
||||
if number_estimation_factor is None:
|
||||
if max_distance is None:
|
||||
_number_of_snapshots *= 4
|
||||
else:
|
||||
_number_of_snapshots *= 8
|
||||
else:
|
||||
_number_of_snapshots *= number_estimation_factor
|
||||
_number_of_snapshots = int(_number_of_snapshots)
|
||||
else:
|
||||
_number_of_snapshots = number_of_snapshots
|
||||
self._dataset = self._generate_random_displacements(
|
||||
|
@ -2733,8 +2745,8 @@ class Phono3py:
|
|||
number_of_atoms: int,
|
||||
distance: float = 0.03,
|
||||
is_plusminus: bool = False,
|
||||
random_seed: Optional[int] = None,
|
||||
max_distance: Optional[float] = None,
|
||||
random_seed: int | None = None,
|
||||
max_distance: float | None = None,
|
||||
):
|
||||
if random_seed is not None and random_seed >= 0 and random_seed < 2**32:
|
||||
_random_seed = random_seed
|
||||
|
|
|
@ -452,16 +452,17 @@ def _read_dataset_fc3(
|
|||
|
||||
def run_pypolymlp_to_compute_forces(
|
||||
ph3py: Phono3py,
|
||||
mlp_params: Union[str, dict, PypolymlpParams],
|
||||
displacement_distance: Optional[float] = None,
|
||||
number_of_snapshots: Optional[int] = None,
|
||||
random_seed: Optional[int] = None,
|
||||
mlp_params: str | dict | PypolymlpParams | None = None,
|
||||
displacement_distance: float | None = None,
|
||||
number_of_snapshots: int | None = None,
|
||||
number_estimation_factor: int | None = None,
|
||||
random_seed: int | None = None,
|
||||
prepare_dataset: bool = False,
|
||||
fc_calculator: Optional[str] = None,
|
||||
fc_calculator_options: Optional[str] = None,
|
||||
cutoff_pair_distance: Optional[float] = None,
|
||||
symfc_memory_size: Optional[float] = None,
|
||||
mlp_filename: Optional[str] = None,
|
||||
fc_calculator: str | None = None,
|
||||
fc_calculator_options: str | None = None,
|
||||
cutoff_pair_distance: float | None = None,
|
||||
symfc_memory_size: float | None = None,
|
||||
mlp_filename: str | None = None,
|
||||
log_level: int = 0,
|
||||
):
|
||||
"""Run pypolymlp to compute forces."""
|
||||
|
@ -557,6 +558,7 @@ def run_pypolymlp_to_compute_forces(
|
|||
is_plusminus=True,
|
||||
number_of_snapshots=number_of_snapshots,
|
||||
random_seed=random_seed,
|
||||
number_estimation_factor=number_estimation_factor,
|
||||
)
|
||||
|
||||
if log_level:
|
||||
|
|
|
@ -108,6 +108,7 @@ def create_phono3py_supercells(
|
|||
is_diagonal=settings.is_diagonal_displacement,
|
||||
number_of_snapshots=settings.random_displacements,
|
||||
random_seed=settings.random_seed,
|
||||
number_estimation_factor=settings.rd_number_estimation_factor,
|
||||
)
|
||||
|
||||
if (
|
||||
|
|
|
@ -676,6 +676,13 @@ def get_parser(fc_symmetry=False, is_nac=False, load_phono3py_yaml=False):
|
|||
default=None,
|
||||
help='Number of supercells with random displacements or "auto"',
|
||||
)
|
||||
parser.add_argument(
|
||||
"--rd-auto-factor",
|
||||
dest="rd_number_estimation_factor",
|
||||
type=float,
|
||||
default=None,
|
||||
help="Factor to estimate number of supercells with random displacements",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--rd-fc2",
|
||||
"--random-displacements-fc2",
|
||||
|
|
|
@ -1196,6 +1196,7 @@ def main(**argparse_control):
|
|||
mlp_params=settings.mlp_params,
|
||||
displacement_distance=settings.displacement_distance,
|
||||
number_of_snapshots=settings.random_displacements,
|
||||
number_estimation_factor=settings.rd_number_estimation_factor,
|
||||
random_seed=settings.random_seed,
|
||||
fc_calculator=settings.fc_calculator,
|
||||
fc_calculator_options=settings.fc_calculator_options,
|
||||
|
|
|
@ -4,9 +4,9 @@ from __future__ import annotations
|
|||
|
||||
import os
|
||||
import pathlib
|
||||
import tempfile
|
||||
from collections.abc import Sequence
|
||||
from dataclasses import dataclass, fields
|
||||
from typing import Optional, Union
|
||||
|
||||
import h5py
|
||||
import numpy as np
|
||||
|
@ -16,32 +16,32 @@ import phono3py
|
|||
from phono3py.cui.phono3py_script import main
|
||||
|
||||
cwd = pathlib.Path(__file__).parent
|
||||
cwd_called = pathlib.Path.cwd()
|
||||
|
||||
|
||||
@dataclass
|
||||
class MockArgs:
|
||||
"""Mock args of ArgumentParser."""
|
||||
|
||||
cell_filename: Optional[str] = None
|
||||
conf_filename: Optional[os.PathLike] = None
|
||||
fc_calculator: Optional[str] = None
|
||||
fc_calculator_options: Optional[str] = None
|
||||
cell_filename: str | None = None
|
||||
conf_filename: os.PathLike | None = None
|
||||
fc_calculator: str | None = None
|
||||
fc_calculator_options: str | None = None
|
||||
fc_symmetry: bool = True
|
||||
filename: Optional[Sequence[os.PathLike]] = None
|
||||
filename: Sequence[os.PathLike] | None = None
|
||||
force_sets_mode: bool = False
|
||||
force_sets_to_forces_fc2_mode: bool = False
|
||||
input_filename = None
|
||||
input_output_filename = None
|
||||
log_level: Optional[int] = None
|
||||
is_bterta: Optional[bool] = None
|
||||
mesh_numbers: Optional[Sequence] = None
|
||||
mlp_params: Optional[str] = None
|
||||
log_level: int | None = None
|
||||
is_bterta: bool | None = None
|
||||
mesh_numbers: Sequence | None = None
|
||||
mlp_params: str | None = None
|
||||
rd_number_estimation_factor: float | None = None
|
||||
output_filename = None
|
||||
output_yaml_filename: Optional[os.PathLike] = None
|
||||
random_displacements: Optional[Union[int, str]] = None
|
||||
output_yaml_filename: os.PathLike | None = None
|
||||
random_displacements: int | str | None = None
|
||||
show_num_triplets: bool = False
|
||||
temperatures: Optional[Sequence] = None
|
||||
temperatures: Sequence | None = None
|
||||
use_pypolymlp: bool = False
|
||||
write_grid_points: bool = False
|
||||
|
||||
|
@ -56,36 +56,44 @@ class MockArgs:
|
|||
|
||||
def test_phono3py_load():
|
||||
"""Test phono3py-load script."""
|
||||
# Check sys.exit(0)
|
||||
argparse_control = _get_phono3py_load_args(
|
||||
cwd / ".." / "phono3py_params_Si-111-222.yaml",
|
||||
)
|
||||
with pytest.raises(SystemExit) as excinfo:
|
||||
main(**argparse_control)
|
||||
assert excinfo.value.code == 0
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
original_cwd = pathlib.Path.cwd()
|
||||
os.chdir(temp_dir)
|
||||
|
||||
argparse_control = _get_phono3py_load_args(
|
||||
cwd_called / "phono3py.yaml",
|
||||
is_bterta=True,
|
||||
temperatures=[
|
||||
"300",
|
||||
],
|
||||
mesh_numbers=["5", "5", "5"],
|
||||
)
|
||||
with pytest.raises(SystemExit) as excinfo:
|
||||
main(**argparse_control)
|
||||
assert excinfo.value.code == 0
|
||||
try:
|
||||
# Check sys.exit(0)
|
||||
argparse_control = _get_phono3py_load_args(
|
||||
cwd / ".." / "phono3py_params_Si-111-222.yaml",
|
||||
)
|
||||
with pytest.raises(SystemExit) as excinfo:
|
||||
main(**argparse_control)
|
||||
assert excinfo.value.code == 0
|
||||
|
||||
# Clean files created by phono3py-load script.
|
||||
for created_filename in (
|
||||
"phono3py.yaml",
|
||||
"fc2.hdf5",
|
||||
"fc3.hdf5",
|
||||
"kappa-m555.hdf5",
|
||||
):
|
||||
file_path = cwd_called / created_filename
|
||||
if file_path.exists():
|
||||
file_path.unlink()
|
||||
argparse_control = _get_phono3py_load_args(
|
||||
"phono3py.yaml",
|
||||
is_bterta=True,
|
||||
temperatures=[
|
||||
"300",
|
||||
],
|
||||
mesh_numbers=["5", "5", "5"],
|
||||
)
|
||||
with pytest.raises(SystemExit) as excinfo:
|
||||
main(**argparse_control)
|
||||
assert excinfo.value.code == 0
|
||||
|
||||
# Clean files created by phono3py-load script.
|
||||
for created_filename in (
|
||||
"phono3py.yaml",
|
||||
"fc2.hdf5",
|
||||
"fc3.hdf5",
|
||||
"kappa-m555.hdf5",
|
||||
):
|
||||
file_path = pathlib.Path(created_filename)
|
||||
if file_path.exists():
|
||||
file_path.unlink()
|
||||
|
||||
finally:
|
||||
os.chdir(original_cwd)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
@ -108,59 +116,76 @@ def test_phono3py_load_with_typeII_dataset(
|
|||
|
||||
"""
|
||||
pytest.importorskip("symfc")
|
||||
argparse_control = _get_phono3py_load_args(
|
||||
cwd / ".." / "phono3py_params-Si111-rd.yaml.xz",
|
||||
load_phono3py_yaml=load_phono3py_yaml,
|
||||
fc_calculator=fc_calculator,
|
||||
fc_calculator_options=fc_calculator_options,
|
||||
)
|
||||
with pytest.raises(SystemExit) as excinfo:
|
||||
main(**argparse_control)
|
||||
assert excinfo.value.code == 0
|
||||
|
||||
# Clean files created by phono3py-load script.
|
||||
for created_filename in ("phono3py.yaml", "fc2.hdf5", "fc3.hdf5"):
|
||||
file_path = pathlib.Path(cwd_called / created_filename)
|
||||
if file_path.exists():
|
||||
if created_filename == "fc3.hdf5":
|
||||
with h5py.File(file_path, "r") as f:
|
||||
if fc_calculator_options is None:
|
||||
assert "fc3_nonzero_indices" not in f
|
||||
else:
|
||||
assert "fc3_nonzero_indices" in f
|
||||
assert "fc3_cutoff" in f
|
||||
assert f["fc3_cutoff"][()] == pytest.approx(4.0)
|
||||
file_path.unlink()
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
original_cwd = pathlib.Path.cwd()
|
||||
os.chdir(temp_dir)
|
||||
|
||||
try:
|
||||
argparse_control = _get_phono3py_load_args(
|
||||
cwd / ".." / "phono3py_params-Si111-rd.yaml.xz",
|
||||
load_phono3py_yaml=load_phono3py_yaml,
|
||||
fc_calculator=fc_calculator,
|
||||
fc_calculator_options=fc_calculator_options,
|
||||
)
|
||||
with pytest.raises(SystemExit) as excinfo:
|
||||
main(**argparse_control)
|
||||
assert excinfo.value.code == 0
|
||||
|
||||
# Clean files created by phono3py-load script.
|
||||
for created_filename in ("phono3py.yaml", "fc2.hdf5", "fc3.hdf5"):
|
||||
file_path = pathlib.Path(created_filename)
|
||||
if file_path.exists():
|
||||
if created_filename == "fc3.hdf5":
|
||||
with h5py.File(file_path, "r") as f:
|
||||
if fc_calculator_options is None:
|
||||
assert "fc3_nonzero_indices" not in f
|
||||
else:
|
||||
assert "fc3_nonzero_indices" in f
|
||||
assert "fc3_cutoff" in f
|
||||
assert f["fc3_cutoff"][()] == pytest.approx(4.0)
|
||||
file_path.unlink()
|
||||
|
||||
finally:
|
||||
os.chdir(original_cwd)
|
||||
|
||||
|
||||
@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 tempfile.TemporaryDirectory() as temp_dir:
|
||||
original_cwd = pathlib.Path.cwd()
|
||||
os.chdir(temp_dir)
|
||||
|
||||
with h5py.File(cwd_called / "kappa-m111111.hdf5", "r") as f:
|
||||
np.testing.assert_almost_equal(f["kappa"][0, 0], 118.93, decimal=1)
|
||||
try:
|
||||
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)
|
||||
|
||||
# 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()
|
||||
with h5py.File("kappa-m111111.hdf5", "r") 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(created_filename)
|
||||
if file_path.exists():
|
||||
file_path.unlink()
|
||||
|
||||
finally:
|
||||
os.chdir(original_cwd)
|
||||
|
||||
|
||||
def test_phono3py_load_with_pypolymlp_si():
|
||||
|
@ -173,56 +198,64 @@ def test_phono3py_load_with_pypolymlp_si():
|
|||
pytest.importorskip("pypolymlp", minversion="0.9.2")
|
||||
pytest.importorskip("symfc")
|
||||
|
||||
# Create fc2.hdf5
|
||||
argparse_control = _get_phono3py_load_args(
|
||||
cwd / ".." / "phono3py_params_Si-111-222-rd.yaml.xz",
|
||||
fc_calculator="symfc",
|
||||
)
|
||||
with pytest.raises(SystemExit) as excinfo:
|
||||
main(**argparse_control)
|
||||
assert excinfo.value.code == 0
|
||||
for created_filename in ("phono3py.yaml", "fc2.hdf5", "fc3.hdf5"):
|
||||
file_path = pathlib.Path(cwd_called / created_filename)
|
||||
assert file_path.exists()
|
||||
pathlib.Path(cwd_called / "fc3.hdf5").unlink()
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
original_cwd = pathlib.Path.cwd()
|
||||
os.chdir(temp_dir)
|
||||
|
||||
# Create MLP (polymlp.yaml)
|
||||
argparse_control = _get_phono3py_load_args(
|
||||
cwd / ".." / "phono3py_params_Si-111-222-rd.yaml.xz",
|
||||
use_pypolymlp=True,
|
||||
)
|
||||
with pytest.raises(SystemExit) as excinfo:
|
||||
main(**argparse_control)
|
||||
assert excinfo.value.code == 0
|
||||
for created_filename in ("phono3py.yaml", "polymlp.yaml"):
|
||||
file_path = pathlib.Path(cwd_called / created_filename)
|
||||
assert file_path.exists()
|
||||
try:
|
||||
# Create fc2.hdf5
|
||||
argparse_control = _get_phono3py_load_args(
|
||||
cwd / ".." / "phono3py_params_Si-111-222-rd.yaml.xz",
|
||||
fc_calculator="symfc",
|
||||
)
|
||||
with pytest.raises(SystemExit) as excinfo:
|
||||
main(**argparse_control)
|
||||
assert excinfo.value.code == 0
|
||||
for created_filename in ("phono3py.yaml", "fc2.hdf5", "fc3.hdf5"):
|
||||
file_path = pathlib.Path(created_filename)
|
||||
assert file_path.exists()
|
||||
pathlib.Path("fc3.hdf5").unlink()
|
||||
|
||||
# Create phono3py_mlp_eval_dataset.yaml
|
||||
argparse_control = _get_phono3py_load_args(
|
||||
cwd_called / "phono3py.yaml",
|
||||
fc_calculator="symfc",
|
||||
random_displacements="auto",
|
||||
use_pypolymlp=True,
|
||||
)
|
||||
# Create MLP (polymlp.yaml)
|
||||
argparse_control = _get_phono3py_load_args(
|
||||
cwd / ".." / "phono3py_params_Si-111-222-rd.yaml.xz",
|
||||
use_pypolymlp=True,
|
||||
)
|
||||
with pytest.raises(SystemExit) as excinfo:
|
||||
main(**argparse_control)
|
||||
assert excinfo.value.code == 0
|
||||
for created_filename in ("phono3py.yaml", "polymlp.yaml"):
|
||||
file_path = pathlib.Path(created_filename)
|
||||
assert file_path.exists()
|
||||
|
||||
with pytest.raises(SystemExit) as excinfo:
|
||||
main(**argparse_control)
|
||||
assert excinfo.value.code == 0
|
||||
# Create phono3py_mlp_eval_dataset.yaml
|
||||
argparse_control = _get_phono3py_load_args(
|
||||
"phono3py.yaml",
|
||||
fc_calculator="symfc",
|
||||
random_displacements="auto",
|
||||
use_pypolymlp=True,
|
||||
)
|
||||
|
||||
ph3 = phono3py.load(cwd_called / "phono3py_mlp_eval_dataset.yaml")
|
||||
assert len(ph3.displacements) == 4
|
||||
with pytest.raises(SystemExit) as excinfo:
|
||||
main(**argparse_control)
|
||||
assert excinfo.value.code == 0
|
||||
|
||||
for created_filename in (
|
||||
"phono3py.yaml",
|
||||
"fc2.hdf5",
|
||||
"fc3.hdf5",
|
||||
"polymlp.yaml",
|
||||
"phono3py_mlp_eval_dataset.yaml",
|
||||
):
|
||||
file_path = pathlib.Path(cwd_called / created_filename)
|
||||
assert file_path.exists()
|
||||
file_path.unlink()
|
||||
ph3 = phono3py.load("phono3py_mlp_eval_dataset.yaml")
|
||||
assert len(ph3.displacements) == 8
|
||||
|
||||
for created_filename in (
|
||||
"phono3py.yaml",
|
||||
"fc2.hdf5",
|
||||
"fc3.hdf5",
|
||||
"polymlp.yaml",
|
||||
"phono3py_mlp_eval_dataset.yaml",
|
||||
):
|
||||
file_path = pathlib.Path(created_filename)
|
||||
assert file_path.exists()
|
||||
file_path.unlink()
|
||||
|
||||
finally:
|
||||
os.chdir(original_cwd)
|
||||
|
||||
|
||||
def test_phono3py_load_with_pypolymlp_nacl():
|
||||
|
@ -235,109 +268,152 @@ def test_phono3py_load_with_pypolymlp_nacl():
|
|||
pytest.importorskip("pypolymlp", minversion="0.9.2")
|
||||
pytest.importorskip("symfc")
|
||||
|
||||
# Stage1 (preparation)
|
||||
argparse_control = _get_phono3py_load_args(
|
||||
cwd / ".." / "phono3py_params_MgO-222rd-444rd.yaml.xz",
|
||||
mlp_params="cutoff=4.0,gtinv_maxl=4 4,max_p=1,gtinv_order=2",
|
||||
fc_calculator="symfc",
|
||||
random_displacements="auto",
|
||||
use_pypolymlp=True,
|
||||
)
|
||||
with pytest.raises(SystemExit) as excinfo:
|
||||
main(**argparse_control)
|
||||
assert excinfo.value.code == 0
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
original_cwd = pathlib.Path.cwd()
|
||||
os.chdir(temp_dir)
|
||||
|
||||
ph3 = phono3py.load(cwd_called / "phono3py_mlp_eval_dataset.yaml")
|
||||
assert len(ph3.displacements) == 16
|
||||
try:
|
||||
# Stage1 (preparation)
|
||||
argparse_control = _get_phono3py_load_args(
|
||||
cwd / ".." / "phono3py_params_MgO-222rd-444rd.yaml.xz",
|
||||
mlp_params="cutoff=4.0,gtinv_maxl=4 4,max_p=1,gtinv_order=2",
|
||||
fc_calculator="symfc",
|
||||
random_displacements="auto",
|
||||
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",
|
||||
"polymlp.yaml",
|
||||
"phono3py_mlp_eval_dataset.yaml",
|
||||
):
|
||||
file_path = pathlib.Path(cwd_called / created_filename)
|
||||
assert file_path.exists()
|
||||
ph3 = phono3py.load("phono3py_mlp_eval_dataset.yaml")
|
||||
assert len(ph3.displacements) == 32
|
||||
|
||||
for created_filename in (
|
||||
"fc3.hdf5",
|
||||
"phono3py_mlp_eval_dataset.yaml",
|
||||
):
|
||||
file_path = pathlib.Path(cwd_called / created_filename)
|
||||
assert file_path.exists()
|
||||
file_path.unlink()
|
||||
for created_filename in (
|
||||
"phono3py.yaml",
|
||||
"fc2.hdf5",
|
||||
"fc3.hdf5",
|
||||
"polymlp.yaml",
|
||||
"phono3py_mlp_eval_dataset.yaml",
|
||||
):
|
||||
file_path = pathlib.Path(created_filename)
|
||||
assert file_path.exists()
|
||||
|
||||
# Stage2 (cutoff test)
|
||||
argparse_control = _get_phono3py_load_args(
|
||||
cwd_called / "phono3py.yaml",
|
||||
fc_calculator="symfc",
|
||||
fc_calculator_options="|cutoff=4.0",
|
||||
random_displacements="auto",
|
||||
use_pypolymlp=True,
|
||||
)
|
||||
with pytest.raises(SystemExit) as excinfo:
|
||||
main(**argparse_control)
|
||||
assert excinfo.value.code == 0
|
||||
for created_filename in (
|
||||
"fc3.hdf5",
|
||||
"phono3py_mlp_eval_dataset.yaml",
|
||||
):
|
||||
file_path = pathlib.Path(created_filename)
|
||||
assert file_path.exists()
|
||||
file_path.unlink()
|
||||
|
||||
ph3 = phono3py.load(cwd_called / "phono3py_mlp_eval_dataset.yaml")
|
||||
assert len(ph3.displacements) == 4
|
||||
# Stage2 (cutoff test)
|
||||
argparse_control = _get_phono3py_load_args(
|
||||
"phono3py.yaml",
|
||||
fc_calculator="symfc",
|
||||
fc_calculator_options="|cutoff=4.0",
|
||||
random_displacements="auto",
|
||||
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",
|
||||
"polymlp.yaml",
|
||||
"phono3py_mlp_eval_dataset.yaml",
|
||||
):
|
||||
file_path = pathlib.Path(cwd_called / created_filename)
|
||||
assert file_path.exists()
|
||||
ph3 = phono3py.load("phono3py_mlp_eval_dataset.yaml")
|
||||
assert len(ph3.displacements) == 8
|
||||
|
||||
for created_filename in (
|
||||
"fc3.hdf5",
|
||||
"phono3py_mlp_eval_dataset.yaml",
|
||||
):
|
||||
file_path = pathlib.Path(cwd_called / created_filename)
|
||||
assert file_path.exists()
|
||||
file_path.unlink()
|
||||
for created_filename in (
|
||||
"phono3py.yaml",
|
||||
"fc2.hdf5",
|
||||
"fc3.hdf5",
|
||||
"polymlp.yaml",
|
||||
"phono3py_mlp_eval_dataset.yaml",
|
||||
):
|
||||
file_path = pathlib.Path(created_filename)
|
||||
assert file_path.exists()
|
||||
|
||||
# Stage3 (memsize test)
|
||||
argparse_control = _get_phono3py_load_args(
|
||||
cwd_called / "phono3py.yaml",
|
||||
fc_calculator="symfc",
|
||||
fc_calculator_options="|memsize=0.05",
|
||||
random_displacements="auto",
|
||||
use_pypolymlp=True,
|
||||
)
|
||||
with pytest.raises(SystemExit) as excinfo:
|
||||
main(**argparse_control)
|
||||
assert excinfo.value.code == 0
|
||||
for created_filename in (
|
||||
"fc3.hdf5",
|
||||
"phono3py_mlp_eval_dataset.yaml",
|
||||
):
|
||||
file_path = pathlib.Path(created_filename)
|
||||
assert file_path.exists()
|
||||
file_path.unlink()
|
||||
|
||||
ph3 = phono3py.load(cwd_called / "phono3py_mlp_eval_dataset.yaml")
|
||||
assert len(ph3.displacements) == 8
|
||||
# Stage3 (memsize test)
|
||||
argparse_control = _get_phono3py_load_args(
|
||||
"phono3py.yaml",
|
||||
fc_calculator="symfc",
|
||||
fc_calculator_options="|memsize=0.05",
|
||||
random_displacements="auto",
|
||||
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",
|
||||
"polymlp.yaml",
|
||||
"phono3py_mlp_eval_dataset.yaml",
|
||||
):
|
||||
file_path = pathlib.Path(cwd_called / created_filename)
|
||||
assert file_path.exists()
|
||||
file_path.unlink()
|
||||
ph3 = phono3py.load("phono3py_mlp_eval_dataset.yaml")
|
||||
assert len(ph3.displacements) == 16
|
||||
|
||||
for created_filename in (
|
||||
"phono3py.yaml",
|
||||
"fc2.hdf5",
|
||||
"fc3.hdf5",
|
||||
"polymlp.yaml",
|
||||
"phono3py_mlp_eval_dataset.yaml",
|
||||
):
|
||||
file_path = pathlib.Path(created_filename)
|
||||
assert file_path.exists()
|
||||
|
||||
for created_filename in (
|
||||
"fc3.hdf5",
|
||||
"phono3py_mlp_eval_dataset.yaml",
|
||||
):
|
||||
file_path = pathlib.Path(created_filename)
|
||||
assert file_path.exists()
|
||||
file_path.unlink()
|
||||
|
||||
# Stage4 (number_estimation_factor)
|
||||
argparse_control = _get_phono3py_load_args(
|
||||
"phono3py.yaml",
|
||||
fc_calculator="symfc",
|
||||
fc_calculator_options="|cutoff=4.0",
|
||||
random_displacements="auto",
|
||||
rd_number_estimation_factor=2.0,
|
||||
use_pypolymlp=True,
|
||||
)
|
||||
with pytest.raises(SystemExit) as excinfo:
|
||||
main(**argparse_control)
|
||||
assert excinfo.value.code == 0
|
||||
|
||||
ph3 = phono3py.load("phono3py_mlp_eval_dataset.yaml")
|
||||
assert len(ph3.displacements) == 4
|
||||
|
||||
for created_filename in (
|
||||
"phono3py.yaml",
|
||||
"fc2.hdf5",
|
||||
"fc3.hdf5",
|
||||
"polymlp.yaml",
|
||||
"phono3py_mlp_eval_dataset.yaml",
|
||||
):
|
||||
file_path = pathlib.Path(created_filename)
|
||||
assert file_path.exists()
|
||||
file_path.unlink()
|
||||
|
||||
finally:
|
||||
os.chdir(original_cwd)
|
||||
|
||||
|
||||
def _get_phono3py_load_args(
|
||||
phono3py_yaml_filepath: Union[str, pathlib.Path],
|
||||
fc_calculator: Optional[str] = None,
|
||||
fc_calculator_options: Optional[str] = None,
|
||||
phono3py_yaml_filepath: str | pathlib.Path,
|
||||
fc_calculator: str | None = None,
|
||||
fc_calculator_options: str | None = None,
|
||||
load_phono3py_yaml: bool = True,
|
||||
is_bterta: bool = False,
|
||||
mesh_numbers: Optional[Sequence] = None,
|
||||
mlp_params: Optional[str] = None,
|
||||
random_displacements: Optional[Union[int, str]] = None,
|
||||
temperatures: Optional[Sequence] = None,
|
||||
mesh_numbers: Sequence | None = None,
|
||||
mlp_params: str | None = None,
|
||||
rd_number_estimation_factor: float | None = None,
|
||||
random_displacements: int | str | None = None,
|
||||
temperatures: Sequence | None = None,
|
||||
use_pypolymlp: bool = False,
|
||||
):
|
||||
# Mock of ArgumentParser.args.
|
||||
|
@ -350,6 +426,7 @@ def _get_phono3py_load_args(
|
|||
log_level=1,
|
||||
mesh_numbers=mesh_numbers,
|
||||
mlp_params=mlp_params,
|
||||
rd_number_estimation_factor=rd_number_estimation_factor,
|
||||
random_displacements=random_displacements,
|
||||
temperatures=temperatures,
|
||||
use_pypolymlp=use_pypolymlp,
|
||||
|
@ -364,6 +441,7 @@ def _get_phono3py_load_args(
|
|||
is_bterta=is_bterta,
|
||||
mesh_numbers=mesh_numbers,
|
||||
mlp_params=mlp_params,
|
||||
rd_number_estimation_factor=rd_number_estimation_factor,
|
||||
random_displacements=random_displacements,
|
||||
temperatures=temperatures,
|
||||
use_pypolymlp=use_pypolymlp,
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
"""Tests of Phono3py API."""
|
||||
|
||||
import os
|
||||
import pathlib
|
||||
import tempfile
|
||||
from collections.abc import Sequence
|
||||
from typing import Optional
|
||||
|
||||
|
@ -11,7 +13,6 @@ from phono3py import Phono3py
|
|||
from phono3py.file_IO import _get_filename_suffix
|
||||
|
||||
cwd = pathlib.Path(__file__).parent
|
||||
cwd_called = pathlib.Path.cwd()
|
||||
|
||||
|
||||
def test_kappa_filename():
|
||||
|
@ -60,16 +61,25 @@ def test_kappa_hdf5_with_boundary_mpf(si_pbesol: Phono3py):
|
|||
]
|
||||
|
||||
boundary_mfp = 10000.0
|
||||
kappa_filename = _set_kappa(
|
||||
si_pbesol, [4, 4, 4], write_kappa=True, boundary_mfp=boundary_mfp
|
||||
)
|
||||
file_path = pathlib.Path(cwd_called / kappa_filename)
|
||||
with h5py.File(file_path, "r") as f:
|
||||
np.testing.assert_almost_equal(f["boundary_mfp"][()], boundary_mfp)
|
||||
assert set(list(f)) == set(key_ref)
|
||||
|
||||
if file_path.exists():
|
||||
file_path.unlink()
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
original_cwd = pathlib.Path.cwd()
|
||||
os.chdir(temp_dir)
|
||||
|
||||
try:
|
||||
kappa_filename = _set_kappa(
|
||||
si_pbesol, [4, 4, 4], write_kappa=True, boundary_mfp=boundary_mfp
|
||||
)
|
||||
file_path = pathlib.Path(kappa_filename)
|
||||
with h5py.File(file_path, "r") as f:
|
||||
np.testing.assert_almost_equal(f["boundary_mfp"][()], boundary_mfp)
|
||||
assert set(list(f)) == set(key_ref)
|
||||
|
||||
if file_path.exists():
|
||||
file_path.unlink()
|
||||
|
||||
finally:
|
||||
os.chdir(original_cwd)
|
||||
|
||||
|
||||
def _set_kappa(
|
||||
|
|
|
@ -97,6 +97,7 @@ def test_random_disps_agno2(
|
|||
number_of_snapshots=number_of_snapshots,
|
||||
distance=distance,
|
||||
is_plusminus=is_plusminus,
|
||||
number_estimation_factor=2,
|
||||
)
|
||||
|
||||
ph3.generate_fc2_displacements(
|
||||
|
|
Loading…
Reference in New Issue