Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix velph-selfenergy-generate (issue #28) #29

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions src/phelel/velph/cli/hints/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ def _show_hints(toml_filename: str):
click.echo("3. Run VASP.")
click.echo()
click.echo("# Supercell calculation")
click.echo(f'1. Modify [vasp.supercell] section in "{toml_filename}".')
click.echo('2. "velph supercell init": Prepare finite displacement calculation. ')
click.echo(f'1. Modify [vasp.phelel] section in "{toml_filename}".')
click.echo('2. "velph phelel init": Prepare finite displacement calculation. ')
click.echo(" NAC parameters are read when NAC calculation is available.")
click.echo('3. "velph supercell generate": Generate input files.')
click.echo('3. "velph phelel generate": Generate input files.')
click.echo("4. Run VASP.")
click.echo()
click.echo("# Phonon band structure calculation")
Expand All @@ -72,31 +72,29 @@ def _show_hints(toml_filename: str):
click.echo("3. Run VASP in ph_bands directories.")
click.echo('4. "velph ph_bands plot" to plot the phonon bands')
click.echo()
click.echo(
'# Electron self-energy calculation (optional, default for "velph selfenergy")'
)
click.echo("# Electron transport calculation")
click.echo(f'1. Write [vasp.transport] section in "{toml_filename}".')
click.echo('2. (optioanl) "velph transport generate": ' "Dry-run to find FFT-mesh.")
click.echo("3. (optioanl) Run VASP.")
click.echo('4. (optioanl) "velph transport check-fft": Check FFT grid.')
click.echo('5. (optioanl) Modify "fft_mesh" in [phelel] section manually.')
click.echo('6. "velph supercell differentiate": Generate derivatives hdf5 file')
click.echo('7. "velph transport generate": Generate input files')
click.echo()
click.echo("# Electron self-energy calculation")
click.echo(f'1. Modify [vasp.selfenergy] section in "{toml_filename}".')
click.echo(
'2. (optional) "velph selfenergy generate -d": ' "Dry-run to find FFT-mesh."
)
click.echo("3. (optional) Run VASP.")
click.echo('4. (optioanl) "velph selfenergy check-fft": Check FFT grid.')
click.echo('5. (optioanl) Modify "fft_mesh" in [phelel] section manually.')
click.echo('6. "velph supercell differentiate": Generate derivatives hdf5 file')
click.echo('6. "velph phelel differentiate": Generate derivatives hdf5 file')
click.echo('7. "velph selfenergy generate": Generate input files')
click.echo()
click.echo("# Other electron-phonon calculation, e.g., transport")
click.echo(f'1. Write [vasp.transport] section in "{toml_filename}".')
click.echo('2. (optioanl) "velph transport generate": ' "Dry-run to find FFT-mesh.")
click.echo("3. (optioanl) Run VASP.")
click.echo('4. (optioanl) "velph transport check-fft": Check FFT grid.')
click.echo('5. (optioanl) Modify "fft_mesh" in [phelel] section manually.')
click.echo('6. "velph supercell differentiate": Generate derivatives hdf5 file')
click.echo('7. "velph transport generate": Generate input files')
click.echo()
click.echo("# Different supercell size for phonon")
click.echo('1. Write "phonon_supercell_dimension" in [phelel] section and')
click.echo("2. Write [vasp.supercell.phonon.*] entry.")
click.echo('1. Write "supercell_dimension" in [phonopy] section and')
click.echo("2. Write [vasp.phelel.phonon.*] entry.")
click.echo(
"------------------------------- velph hints -------------------------------"
)
43 changes: 5 additions & 38 deletions src/phelel/velph/cli/selfenergy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from phelel.velph.cli.utils import check_fft


@cmd_root.group("elph")
@cmd_root.group("selfenergy")
@click.help_option("-h", "--help")
def cmd_selfenergy():
"""Choose selfenergy options."""
Expand All @@ -23,23 +23,6 @@ def cmd_selfenergy():
type=click.Path(),
default="velph.toml",
)
@click.option(
"--hdf5-filename",
"hdf5_filename",
nargs=1,
type=click.Path(),
default="supercell/phelel_params.hdf5",
show_default=True,
)
@click.option(
"-c",
"--calculation",
"calculation_name",
nargs=1,
type=str,
default="selfenergy",
show_default=True,
)
@click.option(
"--dry-run/--no-dry-run",
"-d",
Expand All @@ -49,19 +32,12 @@ def cmd_selfenergy():
show_default=True,
)
@click.help_option("-h", "--help")
def cmd_generate(
toml_filename: str, hdf5_filename: str, calculation_name: str, dry_run: bool
):
def cmd_generate(toml_filename: str, dry_run: bool):
"""Generate elph input files."""
if not pathlib.Path("POTCAR").exists():
click.echo('"POTCAR" not found in current directory.')

write_input_files(
pathlib.Path(toml_filename),
pathlib.Path(hdf5_filename),
calculation_name,
dry_run,
)
write_input_files(pathlib.Path(toml_filename), dry_run)


@cmd_selfenergy.command("check-fft")
Expand All @@ -71,16 +47,7 @@ def cmd_generate(
type=click.Path(),
default="velph.toml",
)
@click.option(
"-c",
"--calculation",
"calculation_name",
nargs=1,
type=click.Path(),
default="selfenergy",
show_default=True,
)
@click.help_option("-h", "--help")
def cmd_check_fft(toml_filename: str, calculation_name: str):
def cmd_check_fft(toml_filename: str):
"""Show [NGX, NGY, NGZ] in vasprun.xml."""
check_fft(toml_filename, calculation_name)
check_fft(toml_filename, "selfenergy")
50 changes: 28 additions & 22 deletions src/phelel/velph/cli/selfenergy/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,15 @@
)


def write_input_files(
toml_filepath: pathlib.Path,
hdf5_filepath: pathlib.Path,
calculation_name: str,
dry_run: bool,
):
def write_input_files(toml_filepath: pathlib.Path, dry_run: bool):
"""Generate el-ph input files."""
write_selfenergy_input_files(
toml_filepath, hdf5_filepath, calculation_name, dry_run
)
write_selfenergy_input_files(toml_filepath, dry_run, calc_type="selfenergy")


def write_selfenergy_input_files(
toml_filepath: pathlib.Path,
hdf5_filepath: pathlib.Path,
calculation_name: str,
dry_run: bool,
calc_type: Optional[Literal["transport"]] = None,
calc_type: Optional[Literal["transport", "selfenergy"]] = None,
current_directory: pathlib.Path = pathlib.Path(""),
energy_threshold: float = 0.5,
) -> None:
Expand All @@ -50,29 +41,44 @@ def write_selfenergy_input_files(
calc_type="transport".

"""
directory_path = pathlib.Path(calculation_name)
directory_path = pathlib.Path(calc_type)

with open(toml_filepath, "rb") as f:
toml_dict = tomli.load(f)

if calculation_name not in toml_dict["vasp"]:
if "vasp" not in toml_dict:
click.echo(f"[vasp] section not found in {toml_filepath}.", err=True)
return None

if "phelel" in toml_dict["vasp"]:
hdf5_filepath = pathlib.Path("phelel/phelel_params.hdf5")
elif "supercell" in toml_dict["vasp"]:
hdf5_filepath = pathlib.Path("supercell/phelel_params.hdf5")
else:
click.echo(
f'[vasp.phelel] section not found in "{toml_filepath}".',
err=True,
)
return None

if calc_type not in toml_dict["vasp"]:
click.echo(
f'[vasp.{calculation_name}.*] section not found in "{toml_filepath}".',
f'[vasp.{calc_type}] section not found in "{toml_filepath}".',
err=True,
)
return None

# Check phelel_params.hdf5
if not dry_run and not hdf5_filepath.exists():
click.echo(f'"{hdf5_filepath}" not found.', err=True)
click.echo('Run "velph supercell differentiate" if necessary.')
click.echo('Run "velph phelel differentiate" if necessary.', err=True)
return None

# mkdir, e.g., selfenergy
directory_path.mkdir(parents=True, exist_ok=True)

# Dry run
toml_incar_dict = toml_dict["vasp"][calculation_name]["incar"]
toml_incar_dict = toml_dict["vasp"][calc_type]["incar"]
if dry_run:
toml_incar_dict["nelm"] = 0
toml_incar_dict["elph_run"] = False
Expand All @@ -93,22 +99,22 @@ def write_selfenergy_input_files(
write_incar(toml_incar_dict, directory_path, cell=primitive)

# KPOINTS
kpoints_dict = toml_dict["vasp"][calculation_name]["kpoints"]
kpoints_dict = toml_dict["vasp"][calc_type]["kpoints"]
assert_kpoints_mesh_symmetry(toml_dict, kpoints_dict, primitive)
write_kpoints_mesh_mode(
toml_incar_dict,
directory_path,
f"vasp.{calculation_name}.kpoints",
f"vasp.{calc_type}.kpoints",
kpoints_dict,
)

# KPOINTS_DENSE
kpoints_dense_dict = toml_dict["vasp"][calculation_name]["kpoints_dense"]
kpoints_dense_dict = toml_dict["vasp"][calc_type]["kpoints_dense"]
assert_kpoints_mesh_symmetry(toml_dict, kpoints_dense_dict, primitive)
write_kpoints_mesh_mode(
toml_incar_dict,
directory_path,
f"vasp.{calculation_name}.kpoints_dense",
f"vasp.{calc_type}.kpoints_dense",
kpoints_dense_dict,
kpoints_filename="KPOINTS_DENSE",
kspacing_name="elph_kspacing",
Expand All @@ -125,7 +131,7 @@ def write_selfenergy_input_files(

# Scheduler launch script
if "scheduler" in toml_dict:
scheduler_dict = get_scheduler_dict(toml_dict, calculation_name)
scheduler_dict = get_scheduler_dict(toml_dict, calc_type)
write_launch_script(scheduler_dict, directory_path)

click.echo(f'VASP input files were generated in "{directory_path}".')
Expand Down
16 changes: 2 additions & 14 deletions src/phelel/velph/cli/transport/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ def cmd_transport():
type=click.Path(),
default="velph.toml",
)
@click.option(
"--hdf5-filename",
"hdf5_filename",
nargs=1,
type=click.Path(),
default="supercell/phelel_params.hdf5",
show_default=True,
)
@click.option(
"--dry-run/--no-dry-run",
"-d",
Expand All @@ -40,16 +32,12 @@ def cmd_transport():
show_default=True,
)
@click.help_option("-h", "--help")
def cmd_generate(toml_filename: str, hdf5_filename: str, dry_run: bool):
def cmd_generate(toml_filename: str, dry_run: bool):
"""Generate transport input files."""
if not pathlib.Path("POTCAR").exists():
click.echo('"POTCAR" not found in current directory.')

write_input_files(
pathlib.Path(toml_filename),
pathlib.Path(hdf5_filename),
dry_run,
)
write_input_files(pathlib.Path(toml_filename), dry_run)


@cmd_transport.command("check-fft")
Expand Down
10 changes: 2 additions & 8 deletions src/phelel/velph/cli/transport/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
from phelel.velph.cli.selfenergy.generate import write_selfenergy_input_files


def write_input_files(
toml_filepath: pathlib.Path,
hdf5_filepath: pathlib.Path,
dry_run: bool,
):
def write_input_files(toml_filepath: pathlib.Path, dry_run: bool):
"""Generate transport input files."""
write_selfenergy_input_files(
toml_filepath, hdf5_filepath, "transport", dry_run, calc_type="transport"
)
write_selfenergy_input_files(toml_filepath, dry_run, calc_type="transport")
6 changes: 3 additions & 3 deletions test/velph/cli/phelel/differentiate/test_differentiate.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_run_derivatives():

"""
phe = phelel.load(cwd / "C111" / "phelel_disp_C111.yaml", fft_mesh=[9, 9, 9])
assert run_derivatives(phe, dir_name=cwd / "C111" / "supercell")
assert run_derivatives(phe, dir_name=cwd / "C111" / "phelel")


def test_run_derivatives_with_wrong_supercell_matrix():
Expand All @@ -30,7 +30,7 @@ def test_run_derivatives_with_wrong_supercell_matrix():
"""
phe = phelel.load(cwd / "phelel_disp_C222.yaml", fft_mesh=[9, 9, 9])
with pytest.raises(ValueError):
run_derivatives(phe, dir_name=cwd / "C111/supercell")
run_derivatives(phe, dir_name=cwd / "C111" / "phelel")


def test_run_derivatives_with_wrong_phonon_supercell_matrix():
Expand All @@ -40,4 +40,4 @@ def test_run_derivatives_with_wrong_phonon_supercell_matrix():

"""
phe = phelel.load(cwd / "phelel_disp_C111-222.yaml", fft_mesh=[9, 9, 9])
assert not run_derivatives(phe, dir_name=cwd / "C111/supercell")
assert not run_derivatives(phe, dir_name=cwd / "C111" / "phelel")