Skip to content

Commit

Permalink
Merge pull request #4 from phonopy/encut-differentiate
Browse files Browse the repository at this point in the history
Add encut option to velph supercell differentiate
  • Loading branch information
atztogo authored Jul 19, 2024
2 parents ea816e6 + 449036b commit 4ad21ae
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 8 deletions.
34 changes: 31 additions & 3 deletions src/phelel/velph/cli/supercell/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""velph command line tool / velph-supercell."""

import pathlib
from typing import Optional

import click
import tomli
Expand All @@ -12,6 +13,7 @@
from phelel.velph.cli.supercell.generate import write_supercell_input_files
from phelel.velph.cli.supercell.init import run_init
from phelel.velph.cli.supercell.phonopy import create_phonopy_yaml
from phelel.velph.utils.vasp import CutoffToFFTMesh


@cmd_root.group("supercell")
Expand Down Expand Up @@ -99,11 +101,19 @@ def cmd_generate(toml_filename: str, yaml_filename: str):
type=click.Path(),
default="supercell/phelel_params.hdf5",
)
@click.option(
"--encut",
nargs=1,
type=float,
default=None,
help=(
"Cutoff energy corresponding to FFT mesh of local potential grid. "
"(encut: float, default=None)"
),
)
@click.help_option("-h", "--help")
def cmd_differentiate(
toml_filename: str,
yaml_filename: str,
hdf5_filename: str,
toml_filename: str, yaml_filename: str, hdf5_filename: str, encut: Optional[float]
) -> None:
"""Calculate derivatives and write phelel_params.hdf5."""
with open(toml_filename, "rb") as f:
Expand Down Expand Up @@ -131,6 +141,24 @@ def cmd_differentiate(
is_symmetry=is_symmetry,
)

if encut is not None:
try:
prec = toml_dict["vasp"]["selfenergy"]["incar"]["prec"]
except KeyError:
click.echo(f'[vasp.selfenergy.incar] not found in "{toml_filename}".')
click.echo('prec = "accurate" is assumed.')
prec = "accurate"
click.echo(f"FFT mesh is generated for encut={encut}.")
phe.fft_mesh = CutoffToFFTMesh.get_FFTMesh(encut, phe.primitive.cell, prec)

if phe.fft_mesh is None:
click.echo("FFT mesh is not specified.", err=True)
else:
if encut is None:
click.echo(f"FFT mesh: {phe.fft_mesh}.")
else:
click.echo(f"FFT mesh: {phe.fft_mesh} (encut={encut}).")

run_derivatives(phe, hdf5_filename=hdf5_filename)


Expand Down
56 changes: 55 additions & 1 deletion src/phelel/velph/cli/supercell/differentiate.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ def run_derivatives(
id_number = f"{i:0{nd}d}"
filepath = pathlib.Path(f"{dir_name}/disp-{id_number}")
if filepath.exists():
dir_names.append(filepath)
if _check_files_exist(filepath):
dir_names.append(filepath)
else:
click.echo(f'Necessary file not found in "{filepath}".', err=True)
return None
else:
click.echo(f'"{filepath}" does not exist.', err=True)
return None
Expand Down Expand Up @@ -65,3 +69,53 @@ def run_derivatives(
phe.save_hdf5(filename=hdf5_filename)

click.echo(f'"{hdf5_filename}" has been made.')


def _check_files_exist(filepath: pathlib.Path) -> bool:
if not _check_file_exists(filepath, "vasprun.xml"):
click.echo(f'"{filepath}/vasprun.xml" not found.', err=True)
return False
if _check_four_files_exist(filepath):
return True
else:
if (filepath / "vaspout.h5").exists():
click.echo(f'Found "{filepath}/vaspout.h5".', err=True)
return True
else:
for filename in (
"inwap.yaml",
"LOCAL-POTENTIAL.bin",
"PAW-STRENGTH.bin",
"PAW-OVERLAP.bin",
):
if not _check_file_exists(filepath, filename):
click.echo(f'"{filepath}/{filename}" not found.', err=True)
return False


def _check_four_files_exist(filepath: pathlib.Path) -> bool:
for filename in (
"inwap.yaml",
"LOCAL-POTENTIAL.bin",
"PAW-STRENGTH.bin",
"PAW-OVERLAP.bin",
):
if not _check_file_exists(filepath, filename):
return False
return True


def _check_file_exists(filepath: pathlib.Path, filename: str) -> bool:
"""Check if the necessary file exists.
The file can be compressed with xz, etc.
The file names that can be checked are:
inwap.yaml
LOCAL-POTENTIAL.bin
PAW-STRENGTH.bin
PAW-OVERLAP.bin
vasprun.xml
"""
return bool(list(pathlib.Path(filepath).glob(f"{filename}*")))
16 changes: 13 additions & 3 deletions src/phelel/velph/cli/transport/plot/plot_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,25 @@ def _show(transport: h5py._hl.group.Group, index: int):
print(f" {key[1]}: {transport[key[0]][()]}")
else:
print(f" {key}: {transport[key][()]}")
print(" data_array_shapes:")

print(" data_array_diagonal:")
for key in (
"e_conductivity",
"e_t_conductivity",
"energy",
("lab", "Onsager_coefficients"),
"mobility",
"peltier",
"seebeck",
):
v = transport[key][:].ravel()
print(
f" {key}: [{v[0]:.3e}, {v[4]:.3e}, {v[8]:.3e}, "
f"{v[5]:.3e}, {v[2]:.3e}, {v[1]:.3e}]"
)

print(" data_array_shapes:")
for key in (
"energy",
("lab", "Onsager_coefficients"),
"transport_function",
):
if isinstance(key, tuple):
Expand Down
5 changes: 4 additions & 1 deletion test/velph/cli/supercell/differentiate/test_differentiate.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ def test_run_derivatives():
"""
phe = phelel.load(cwd / "C111" / "phelel_disp_C111.yaml", fft_mesh=[9, 9, 9])
hdf5_filename = "phelel_params_test_run_derivatives.hdf5"
run_derivatives(phe, hdf5_filename=hdf5_filename, dir_name=cwd / "C111/supercell")
run_derivatives(
phe, hdf5_filename=hdf5_filename, dir_name=cwd / "C111" / "supercell"
)
file_path = pathlib.Path(cwd_called / hdf5_filename)
print(file_path)
assert file_path.exists()
file_path.unlink()

Expand Down

0 comments on commit 4ad21ae

Please sign in to comment.