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

Improve velph-phono3py #41

Merged
merged 1 commit into from
Dec 5, 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
16 changes: 9 additions & 7 deletions src/phelel/cui/create_supercells.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
from phonopy.interface.calculator import write_supercells_with_displacements

from phelel.api_phelel import Phelel
from phelel.cui.settings import PhelelSettings
from phelel.interface.phelel_yaml import PhelelYaml


def create_phelel_supercells(
cell_info,
settings,
symprec,
interface_mode="vasp",
load_phelel_yaml=False,
log_level=1,
cell_info: dict,
settings: PhelelSettings,
symprec: float,
interface_mode: Optional[str] = "vasp",
load_phelel_yaml: bool = False,
log_level: int = 1,
):
"""Create displacements and supercells.

Expand All @@ -30,7 +32,7 @@ def create_phelel_supercells(
"""
optional_structure_info = cell_info["optional_structure_info"]
unitcell_filename = cell_info["optional_structure_info"][0]
phe_yml = cell_info["phonopy_yaml"]
phe_yml: Optional[PhelelYaml] = cell_info["phonopy_yaml"]

phelel = Phelel(
cell_info["unitcell"],
Expand Down
21 changes: 11 additions & 10 deletions src/phelel/velph/cli/init/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@
),
)
@click.option(
"--diagonal/--parallel-to-axes",
"--diagonal",
"diagonal",
type=bool,
default=None,
help=(
"Generate displacements only along axes or not."
"Generate displacements in diagonal directions or only along axes."
f"(diagonal: bool, default={VelphInitParams.diagonal})"
),
)
Expand Down Expand Up @@ -146,16 +146,17 @@
),
)
@click.option(
"--phonon-max-num-atoms",
"phonon_max_num_atoms",
"--phono3py-max-num-atoms",
"phono3py_max_num_atoms",
nargs=1,
default=None,
type=int,
help=(
"Determine phonon supercell dimension so that number of atoms in supercell "
"for phonon is less than this number if different one from electron-phonon "
"(phelel) or phonon-phonon (phono3py) is expected. "
f"(phonon_max_num_atoms: int, default={VelphInitParams.phonon_max_num_atoms})"
"Determine phono3py supercell dimension so that number of atoms in supercell "
"for phono3py is less than this number if different dimension from "
"that of electron-phonon (phelel) is expected. "
"(phono3py_max_num_atoms: int, "
f"default={VelphInitParams.phono3py_max_num_atoms})"
),
)
@click.option(
Expand Down Expand Up @@ -245,7 +246,7 @@ def cmd_init(
max_num_atoms: Optional[int],
phelel_dir_name: str,
phelel_nosym: Optional[bool],
phonon_max_num_atoms: Optional[int],
phono3py_max_num_atoms: Optional[int],
primitive_cell_choice: Optional[str],
project_folder: str,
symmetrize_cell: Optional[bool],
Expand Down Expand Up @@ -286,7 +287,7 @@ def cmd_init(
"magmom": magmom,
"max_num_atoms": max_num_atoms,
"phelel_nosym": phelel_nosym,
"phonon_max_num_atoms": phonon_max_num_atoms,
"phono3py_max_num_atoms": phono3py_max_num_atoms,
"primitive_cell_choice": primitive_cell_choice,
"symmetrize_cell": symmetrize_cell,
"tolerance": tolerance,
Expand Down
26 changes: 13 additions & 13 deletions src/phelel/velph/cli/init/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,19 +629,19 @@ def _get_toml_lines(
)
return None

phonon_supercell_dimension = _get_supercell_dimension(
velph_dict.get("phonopy", {}),
vip.phonon_max_num_atoms,
phono3py_supercell_dimension = _get_supercell_dimension(
velph_dict.get("phono3py", {}),
vip.phono3py_max_num_atoms,
sym_dataset,
vip.find_primitive,
)

click.echo("[phelel]")
_show_supercell_dimension(supercell_dimension)

if phonon_supercell_dimension is not None:
click.echo("[phonopy]")
_show_supercell_dimension(phonon_supercell_dimension)
if phono3py_supercell_dimension is not None:
click.echo("[phono3py]")
_show_supercell_dimension(phono3py_supercell_dimension)

(
kpoints_dict,
Expand All @@ -657,7 +657,7 @@ def _get_toml_lines(
primitive,
sym_dataset,
supercell_dimension,
phonon_supercell_dimension,
phono3py_supercell_dimension,
cell_choices["nac"],
cell_choices["relax"],
phelel_dir_name=phelel_dir_name,
Expand Down Expand Up @@ -696,12 +696,12 @@ def _get_toml_lines(
vip.phelel_nosym,
)

# [phonopy]
if phonon_supercell_dimension is not None:
lines += ["[phonopy]"]
lines += _get_supercell_dimension_lines(phonon_supercell_dimension)
# [phono3py]
if phono3py_supercell_dimension is not None:
lines += ["[phono3py]"]
lines += _get_supercell_dimension_lines(phono3py_supercell_dimension)
lines += _get_displacement_settings_lines(
velph_dict, "phonopy", vip.amplitude, vip.diagonal, vip.plusminus
velph_dict, "phono3py", vip.amplitude, vip.diagonal, vip.plusminus
)
lines.append("")

Expand Down Expand Up @@ -1333,7 +1333,7 @@ def _get_supercell_dimension_lines(

def _get_displacement_settings_lines(
velph_dict: dict,
calc_type: Literal["phelel", "phonopy"],
calc_type: Literal["phelel", "phono3py"],
amplitude: Optional[float],
diagonal: Optional[bool],
plusminus: Optional[bool],
Expand Down
23 changes: 21 additions & 2 deletions src/phelel/velph/cli/phono3py/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,32 @@ def cmd_phono3py():
type=int,
help="Number of snapshots of supercells with random directional displacement.",
)
@click.option(
"--rd-fc2",
"random_displacements_fc2",
nargs=1,
default=None,
type=int,
help=(
"Number of snapshots of phonon supercells "
"with random directional displacement."
),
)
@click.help_option("-h", "--help")
def cmd_init(toml_filename: str, random_displacements: Optional[int]):
def cmd_init(
toml_filename: str,
random_displacements: Optional[int],
random_displacements_fc2: Optional[int],
):
"""Generate displacements and write phelel_disp.yaml."""
with open(toml_filename, "rb") as f:
toml_dict = tomli.load(f)

ph3py = run_init(toml_dict, number_of_snapshots=random_displacements)
ph3py = run_init(
toml_dict,
number_of_snapshots=random_displacements,
number_of_snapshots_fc2=random_displacements_fc2,
)

phono3py_yaml_filename = pathlib.Path("phono3py/phono3py_disp.yaml")
phono3py_yaml_filename.parent.mkdir(parents=True, exist_ok=True)
Expand Down
24 changes: 15 additions & 9 deletions src/phelel/velph/cli/phono3py/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def run_init(
toml_dict: dict,
current_directory: pathlib.Path = pathlib.Path(""),
number_of_snapshots: Optional[int] = None,
number_of_snapshots_fc2: Optional[int] = None,
) -> Optional[Phono3py]:
"""Generate displacements and write phono3py_disp.yaml.

Expand All @@ -27,8 +28,9 @@ def run_init(
"""
convcell = parse_cell_dict(toml_dict["unitcell"])
supercell_matrix = toml_dict["phelel"].get("supercell_dimension", None)
if "phonopy" in toml_dict:
phonon_supercell_matrix = toml_dict["phonopy"].get("supercell_dimension", None)
if "phono3py" in toml_dict:
phonon_supercell_matrix = supercell_matrix
supercell_matrix = toml_dict["phono3py"].get("supercell_dimension", None)
else:
phonon_supercell_matrix = None
if "primitive_cell" in toml_dict:
Expand Down Expand Up @@ -70,6 +72,7 @@ def run_init(
is_plusminus=is_plusminus,
is_diagonal=is_diagonal,
number_of_snapshots=number_of_snapshots,
number_of_snapshots_fc2=number_of_snapshots_fc2,
)

nac_directory = current_directory / "nac"
Expand Down Expand Up @@ -99,6 +102,7 @@ def _generate_phono3py_supercells(
is_plusminus: Union[str, bool] = "auto",
is_diagonal: bool = True,
number_of_snapshots: Optional[int] = None,
number_of_snapshots_fc2: Optional[int] = None,
):
"""Generate phelel supercells."""
if distance is None:
Expand All @@ -119,12 +123,14 @@ def _generate_phono3py_supercells(

if phono3py.phonon_supercell_matrix is not None:
# For estimating number of displacements for harmonic phonon
phono3py.generate_fc2_displacements(
distance=distance, is_plusminus=False, is_diagonal=False
)
n_disps = len(phono3py.phonon_supercells_with_displacements)
phono3py.generate_fc2_displacements(
distance=distance, number_of_snapshots=n_disps, is_plusminus=True
)
if number_of_snapshots_fc2 is None:
phono3py.generate_fc2_displacements(
distance=distance, is_plusminus="auto", is_diagonal=False
)
else:
phono3py.generate_fc2_displacements(
distance=distance,
number_of_snapshots=number_of_snapshots_fc2,
)
n_snapshots = len(phono3py.phonon_supercells_with_displacements)
click.echo(f"Number of displacements for phonon: {n_snapshots}")
4 changes: 2 additions & 2 deletions src/phelel/velph/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ class VelphInitParams:
cell_for_nac: Optional[CellChoice] = CellChoice.UNSPECIFIED
cell_for_relax: Optional[CellChoice] = CellChoice.UNSPECIFIED
find_primitive: Optional[bool] = True
diagonal: Optional[bool] = True
diagonal: Optional[bool] = False
plusminus: Optional[bool] = True
kspacing: Optional[float] = 0.1
kspacing_dense: Optional[float] = 0.05
magmom: Optional[str] = None
max_num_atoms: Optional[int] = None
phonon_max_num_atoms: Optional[int] = None
phono3py_max_num_atoms: Optional[int] = None
phelel_nosym: Optional[bool] = False
primitive_cell_choice: Optional[PrimitiveCellChoice] = (
PrimitiveCellChoice.STANDARDIZED
Expand Down
49 changes: 25 additions & 24 deletions test/velph/cli/phono3py/init/test_phono3py_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,20 @@
@pytest.mark.parametrize("distance", [0.03, 0.05])
def test_phono3py_init_random_displacements(distance: float):
"""Test of plusminus and diagonal with Ti."""
toml_str = f"""title = "VASP el-ph settings"
phelel_str = f"""title = "VASP el-ph settings"

[phelel]
supercell_dimension = [4, 4, 2]
amplitude = {distance}
fft_mesh = [18, 18, 28]
"""

[vasp.phonopy.incar]
lwap = true
isym = 0
kpar = 2
ncore = 24
ismear = 0
sigma = 0.2
ediff = 1e-08
encut = 329.532
prec = "accurate"
lreal = false
lwave = false
lcharg = false
addgrid = true
lsorbit = true
[vasp.phonopy.kpoints]
mesh = [6, 6, 7]
phono3py_str = f"""[phono3py]
supercell_dimension = [2, 2, 1]
amplitude = {distance}
"""

unitcell_str = """
[unitcell]
lattice = [
[ 2.930720886111760, 0.000000000000000, 0.000000000000000 ], # a
Expand All @@ -48,13 +37,25 @@ def test_phono3py_init_random_displacements(distance: float):
[[unitcell.points]] # 2
symbol = "Ti"
coordinates = [ 0.666666666666664, 0.333333333333336, 0.750000000000000 ]
magnetic_moment = [ 0.00000000, 0.00000000, 0.00000000 ]
"""
magnetic_moment = [ 0.00000000, 0.00000000, 0.00000000 ]"""

toml_dict = tomli.loads(toml_str)
toml_dict = tomli.loads(phelel_str + unitcell_str)
ph3 = run_init(toml_dict, number_of_snapshots=10)
np.testing.assert_array_equal(ph3.supercell_matrix, np.diag([4, 4, 2]))
natom = len(ph3.supercell)
assert natom == 64
assert ph3.displacements.shape == (10, natom, 3)
assert len(ph3.supercell) == 64
assert ph3.displacements.shape == (10, 64, 3)
np.testing.assert_allclose(np.linalg.norm(ph3.displacements, axis=2), distance)

toml_dict = tomli.loads(phelel_str + phono3py_str + unitcell_str)
ph3 = run_init(toml_dict, number_of_snapshots=10, number_of_snapshots_fc2=4)
np.testing.assert_array_equal(ph3.supercell_matrix, np.diag([2, 2, 1]))
np.testing.assert_array_equal(ph3.phonon_supercell_matrix, np.diag([4, 4, 2]))

assert len(ph3.supercell) == 8
assert ph3.displacements.shape == (10, 8, 3)
assert len(ph3.phonon_supercell) == 64
assert ph3.phonon_displacements.shape == (4, 64, 3)
np.testing.assert_allclose(np.linalg.norm(ph3.displacements, axis=2), distance)
np.testing.assert_allclose(
np.linalg.norm(ph3.phonon_displacements, axis=2), distance
)
Loading