"""BSF input-parameters definition."""
import numpy as np
from functools import cache
from typing import Any, List, Optional
from ...common.generated_configuration_definitions import Length
from ...common.grammar_types import Keyword, SetOf, Integer
from ...common.warnings import DataValidityError, DataValidityWarning
from ..input_parameters import InputSection
from ...physics.lattice_data import bravais_number
from ..input_parameters_definitions import InputParametersDefinition, InputValueDefinition as V
from ._file_manifests import tau_file
from ._sections import CONTROL, CPA, ENERGY, MODE, SITES, STRCONST, TASK, TAU
from ._energy import energy_relative_default, energy_absolute_default
EK = "EK"
KK = "KK"
EK_TASK_ITEMS = ("NK", "KPATH", "NKDIR", "KE")
KK_TASK_ITEMS = ("NK1", "NK2", "K1", "K2")
[docs]
def _requested_mode(parameters):
name = parameters.requested_task_name
if name is None:
return None
name = name.upper()
if name == "BSFEK":
return EK
if name == "BSFKK":
return KK
return None
[docs]
def bsf_mode(parameters):
"""Return the BSF mode selected by the number of energy points."""
ne = parameters["ENERGY"]["NE"]()
if ne is None or len(ne) == 0:
return _requested_mode(parameters) or KK
return EK if ne[0] > 1 else KK
[docs]
def _root(container):
return container._get_root_container()
[docs]
def _ek_item(_definition, container):
return bsf_mode(_root(container)) != KK
[docs]
def _ek_vectors_item(_definition, container):
return _ek_item(_definition, container) and container["KPATH"]() is None
[docs]
def _kk_item(_definition, container):
return bsf_mode(_root(container)) != EK
[docs]
def _ek_vectors_optional(option):
"""Predefined paths do not require explicit k-space segments."""
return not _ek_vectors_item(None, option._container)
[docs]
def _kk_optional(option):
"""The K-K grid is needed only in the single-energy mode."""
return bsf_mode(_root(option)) != KK
[docs]
def _ka_item(definition, container):
return _kk_item(definition, container) or _ek_vectors_item(definition, container)
[docs]
def _ka_numbered(option):
return bsf_mode(_root(option)) == EK
[docs]
def _grid_default(option):
return [3] if bsf_mode(_root(option)) == EK else [0]
[docs]
def _ne_default(option):
return [200] if _requested_mode(_root(option)) == EK else [1]
[docs]
def _emin_default(option):
return -0.2 if bsf_mode(_root(option)) == EK else 0.7
[docs]
def _emaxev_default(option):
if bsf_mode(_root(option)) == KK:
return option._container["EMINEV"]()
return None
[docs]
def _emax_default(option):
if bsf_mode(_root(option)) == KK:
return option._container["EMIN"]()
return -1.0
[docs]
def _validate_bsf(
parameters: Any, _values: Any, why: str
) -> Optional[List[DataValidityError]]:
issues = []
mode = bsf_mode(parameters)
hint = _requested_mode(parameters)
if hint and hint != mode:
issues.append(
DataValidityWarning(
f"ENERGY.NE selects BSF-{mode}, but BSF-{hint} was requested"
)
)
task = parameters["TASK"]
incompatible = KK_TASK_ITEMS if mode == EK else EK_TASK_ITEMS
incompatible = [name for name in incompatible if task[name].is_set()]
if incompatible:
issues.append(
DataValidityError(
f"TASK.{', TASK.'.join(incompatible)} cannot be used in BSF-{mode} mode. "
"The mode is chosen by setting number of energies in ENERGY.NE."
)
)
if mode == EK:
if why != "set" and task["KPATH"]() is None and (
task["KA"]() is None or task["KE"]() is None
):
issues.append(
DataValidityError(
"Please, specify either TASK.KPATH or TASK.KA and TASK.KE"
)
)
nkdir = task["NKDIR"]()
if nkdir is not None and nkdir > 9:
issues.append(DataValidityError("TASK.NKDIR cannot be greater than 9"))
elif why != "set":
missing = [name for name in KK_TASK_ITEMS if task[name]() is None]
if missing:
issues.append(
DataValidityError(
f"TASK.{', TASK.'.join(missing)} are required in BSFKK mode"
)
)
if why != "set" and mode == KK and not missing:
origin = task["KA"]()
origin = np.zeros(3) if origin is None else np.asarray(origin).reshape(-1, 3)[0]
first = np.asarray(task["K1"]()) - origin
second = np.asarray(task["K2"]()) - origin
if not np.all(np.isfinite(first)) or not np.all(np.isfinite(second)) or np.linalg.norm(np.cross(first, second)) == 0:
issues.append(DataValidityError("K1 and K2 are endpoints relative to KA and must span a finite nondegenerate plane."))
return issues or None
kpaths = {
0: "Default short path: Γ-X / Γ-H (Bravais 13, 14)",
1: "Full standard high-symmetry path (4, 11, 12, 13, 14)",
2: "Reduced standard high-symmetry path (4, 11, 12, 13, 14)",
3: "Short standard high-symmetry path (4, 11, 12, 13, 14)",
4: "Short path: Γ-Y-T-Z / Γ-M / Γ-X-M / Γ-X / Γ-H-N-Γ (4, 11, 12, 13, 14)",
5: "Selected path: Γ-X / K-Γ / Γ-X,Y,Z / Γ-L / Γ-H (4, 11, 12, 13, 14)",
6: "Selected directions: Γ-Y / Γ-X,Y,Z (4, 13, 14)",
7: "Selected paths: X-Γ + Γ-Y (4)",
10: "Alternative FCC path with L-K branch (13)",
}
kpaths_full = {
4: {
"name": "orthorhombic primitive",
"paths": {
1: ("full standard path",
"Γ-Σ-X-G-U-A-Z-Λ-Γ-∆-Y-H-T-B-Z + X-D-S-C-Y + U-P-R-E-T + S-Q-T"),
2: ("reduced standard path",
"Γ-Σ-X-G-U-A-Z-Λ-Γ-∆-Y-H-T-B-Z"),
3: ("short standard path",
"Γ-Σ-X-G-U-A-Z-Λ-Γ"),
4: ("secondary standard path",
"Γ-∆-Y-H-T-B-Z"),
5: ("Γ-X direction",
"Γ-Σ-X"),
6: ("Γ-Y direction",
"Γ-∆-Y"),
7: ("X-Γ and Γ-Y directions",
"X-Σ-Γ + Γ-∆-Y"),
},
},
11: {
"name": "hexagonal primitive",
"paths": {
1: ("full standard path",
"Γ-Σ-M-T'-K-T-Γ-∆-A-R-L-S'-H-S-A + M-U-L + K-P-H"),
2: ("reduced standard path",
"Γ-Σ-M-T'-K-T-Γ-∆-A-R-L-S'-H-S-A"),
3: ("short standard path",
"Γ-Σ-M-T'-K-T-Γ-∆-A"),
4: ("Γ-M direction",
"Γ-Σ-M"),
5: ("K-Γ direction",
"K-T-Γ"),
},
},
12: {
"name": "cubic primitive",
"paths": {
1: ("full standard path",
"Γ-∆-X-Y-M-V-R-Λ-Γ-Σ-M"),
2: ("reduced standard path",
"Γ-∆-X-Y-M-V-R-Λ-Γ"),
3: ("short standard path",
"Γ-∆-X-Y-M-V-R"),
4: ("Γ-X-M path",
"Γ-∆-X-Y-M"),
5: ("three principal Γ-axis directions",
"Γ-∆-X + Γ-∆-Y + Γ-∆-Z"),
},
},
13: {
"name": "cubic face-centered",
"paths": {
0: ("default path",
"Γ-∆-X"),
1: ("full standard path",
"X-∆-Γ-Λ-L-Q-W-N-K-Σ-Γ + L-M-U-S-X-Z-W-B-U"),
2: ("reduced standard path",
"X-∆-Γ-Λ-L-Q-W-N-K-Σ-Γ"),
3: ("short standard path",
"X-∆-Γ-Λ-L"),
4: ("Γ-X direction",
"Γ-∆-X"),
5: ("Γ-L direction",
"Γ-Λ-L"),
6: ("three principal Γ-axis directions",
"Γ-∆-X + Γ-∆-Y + Γ-∆-Z"),
10: ("alternative full path with L-K branch",
"X-∆-Γ-Λ-L + L-?-K + W-N-K-Σ-Γ + L-M-U-S-X-Z-W-B-U"),
},
},
14: {
"name": "cubic body-centered",
"paths": {
0: ("default path",
"Γ-D-H"),
1: ("full standard path",
"Γ-D-H-G-N-Σ-Γ-Λ-P-F-H + N-D-P"),
2: ("reduced standard path",
"Γ-D-H-G-N-Σ-Γ-Λ-P-F-H"),
3: ("short standard path",
"Γ-D-H-G-N-Σ-Γ-Λ-P"),
4: ("Γ-H-N-Γ path",
"Γ-D-H-G-N-Σ-Γ"),
5: ("Γ-H direction",
"Γ-D-H"),
6: ("three principal Γ-axis directions",
"Γ-∆-X + Γ-∆-Y + Γ-∆-Z"),
},
},
}
[docs]
def kpaths_for_atoms(atoms_or_bravais):
bravais = bravais_number(atoms_or_bravais)
paths = kpaths_full.get(bravais, {}).get('paths', {})
return {
number: label for number, label in paths.items()
}
[docs]
def _task_definition():
def kpaths_cli_help(kpaths_full=kpaths_full, width=88):
lines = [ ' Valid values for the given Bravais lattice',
' ------------------------------------------',
' ']
for bravais, data in kpaths_full.items():
lines.append(f"{bravais:2}: {data['name']}")
for kpath, (description, path) in data["paths"].items():
prefix = f" {kpath:2} {description}"
path_indent = " " * 8
if len(prefix) + 2 + len(path) <= width:
lines.append(f"{prefix:<40} {path}")
else:
lines.append(prefix)
lines.append(f"{path_indent}{path}")
lines.append("")
lines.append(" other Bravais lattices require manual k-path settings")
return "\n".join(lines).rstrip()
out = TASK(
"BSF",
add=[
V("NK", Integer(min=2), 300, condition=_ek_item, info="total number of k-points"),
V(
"KPATH",
Keyword(kpaths),
condition=_ek_item,
info="Predefined path in k-space",
description=kpaths_cli_help(),
is_optional=True,
),
V(
"NKDIR",
Length(
"KA",
"KE",
default_values=[[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]],
),
condition=_ek_vectors_item,
info="Number of directions treated in k-spaces",
is_optional=_ek_vectors_optional,
is_required=False,
),
V(
"KA",
SetOf(float, length=3),
condition=_ka_item,
is_repeated=V.Repeated.NUMBERED_IF(_ka_numbered),
info=(
"First k-vector segment in k-space in multiples of 2π/a and "
"rectangular coordinates with * = 1, ...,NKDIR"
),
is_optional=_ek_vectors_optional,
is_required=False,
),
V(
"KE",
SetOf(float, length=3),
condition=_ek_vectors_item,
is_repeated="NUMBERED",
info=(
"Last k-vector segment in k-space in multiples of 2π/a and "
"rectangular coordinates with * = 1, ...,NKDIR"
),
is_optional=_ek_vectors_optional,
is_required=False,
),
V(
"NK1",
Integer(min=2),
condition=_kk_item,
info="number of k-points along k1",
is_optional=_kk_optional,
is_required=False,
),
V(
"NK2",
Integer(min=2),
condition=_kk_item,
info="number of k-points along k2",
is_optional=_kk_optional,
is_required=False,
),
V(
"K1",
SetOf(float, length=3),
condition=_kk_item,
is_optional=_kk_optional,
is_required=False,
info="First endpoint of the K-K region; native subtracts KA, then orthogonalizes the second direction.",
),
V(
"K2",
SetOf(float, length=3),
condition=_kk_item,
is_optional=_kk_optional,
is_required=False,
info="Second endpoint of the K-K region; native subtracts KA and orthogonalizes relative to K1-KA.",
),
],
)
out['KPATH'].choices_for_atoms = kpaths_for_atoms
return out
[docs]
def _energy_definition():
energy = ENERGY(
emin=(-0.2, "The energy at which the BSF mesh starts", None),
emax=(-1.0, "The energy at which the BSF mesh ends", None),
defaults={"GRID": _grid_default, "NE": _ne_default, "ImE": 0.001},
)
energy.copy_member("NE", type=SetOf(Integer(min=1), min_length=1, max_length=2))
energy["EMIN"].default_value = energy_absolute_default('EMIN', _emin_default)
energy["EMAX"].default_value = energy_absolute_default('EMAX', _emax_default)
energy["EMAXEV"].default_value = energy_relative_default('EMAX', _emaxev_default)
return energy
[docs]
class BSFTaskSection(InputSection):
[docs]
def k_path_gui(self, atoms, *, parent=None):
from ase2sprkkr.gui.k_path import k_path_gui
out = k_path_gui(atoms, parent=parent)
if out:
self.KPATH.clear()
self.set(out)
[docs]
@cache
def bsf_definition():
"""Return the single definition shared by BSF task views."""
out = BSFInputParametersDefinition(
"bsf",
[
CONTROL("BLOCHSF"),
TAU,
_task_definition(),
_energy_definition(),
MODE,
STRCONST,
CPA,
SITES,
],
info="BSF - Bloch spectral functions in the E-K or K-K plane",
validators=_validate_bsf,
)
out["TASK"].result_class = BSFTaskSection
return out
input_parameters_definition = bsf_definition