vibe-sketcherDocumentation
Source code for vibesketcher.document.defect_corrections
"""The point-charge image term of a charged defect in a periodic supercell (#195).
A charge q in a periodic cell interacts with its own images and with the
uniform compensating background the periodic Coulomb sum implies. Relative to
the isolated charge in the same dielectric medium, that costs::
E_periodic - E_isolated = (q^2 / 2) phi,
where ``phi`` is the Madelung potential at the charge site from all images and
the background, without the charge's own field. The formation energy is
corrected by ``E_PC = -(q^2 / 2) phi``. In a cubic cell of side L and a
scalar dielectric constant this is the leading Leslie-Gillan/Makov-Payne term
q^2 alpha / (2 eps L), alpha = 2.8373 (M. Leslie and M. J. Gillan, J. Phys. C
18, 973 (1985); G. Makov and M. C. Payne, Phys. Rev. B 51, 4014 (1995)).
For a dielectric tensor eps the Ewald sum is taken in the metric of eps
(R. Rurali and X. Cartoixa, Nano Lett. (2009), doi:10.1021/nl802847p; S. T. Murphy and
N. D. M. Hine, Phys. Rev. B 87, 094111 (2013); Y. Kumagai and F. Oba,
Phys. Rev. B 89, 195205 (2014))::
phi = sum_{R != 0} erfc(g d_R) / (sqrt(det eps) d_R)
+ (4 pi / V) sum_{G != 0} exp(-G.eps.G / (4 g^2)) / G.eps.G
- 2 g / sqrt(pi det eps) - pi / (V g^2),
with d_R = sqrt(R.eps^-1.R). The result does not depend on the splitting
parameter g, which the tests check. This is the point-charge limit of the
lattice term E_lat of C. Freysoldt, J. Neugebauer and C. G. Van de Walle,
Phys. Rev. Lett. 102, 016402 (2009). Their potential-alignment term
-q Delta_{q/b} needs the electrostatic potentials of the defective and pristine
runs, which the sketcher does not read; it is a stated input instead.
"""
from __future__ import annotations
import math
from typing import Any
import numpy as np
from numpy.typing import NDArray
from vibesketcher.units import BOHR_TO_ANGSTROM, HARTREE_TO_EV
#: e^2 / (4 pi eps_0) in eV angstrom, from the CODATA 2018 hartree and bohr.
COULOMB_EV_ANGSTROM = HARTREE_TO_EV * BOHR_TO_ANGSTROM
#: Lattice-sum terms beyond which a cell is refused rather than summed slowly.
MAX_TERMS = 4_000_000
_TAIL = 6.5 # erfc(6.5) ~ 4e-20 and exp(-6.5^2) ~ 5e-19: both tails below double precision
REFERENCES = (
"M. Leslie and M. J. Gillan, J. Phys. C 18, 973 (1985)",
"G. Makov and M. C. Payne, Phys. Rev. B 51, 4014 (1995)",
"Y. Kumagai and F. Oba, Phys. Rev. B 89, 195205 (2014) (anisotropic Ewald sum)",
"C. Freysoldt, J. Neugebauer and C. G. Van de Walle, Phys. Rev. Lett. 102, 016402 (2009)",
)
[docs]
def dielectric_tensor(value: float | list[list[float]]) -> NDArray[np.float64]:
"""A symmetric dielectric tensor with every eigenvalue at least 1, or a refusal."""
tensor = (
np.eye(3) * float(value)
if isinstance(value, int | float)
else np.asarray(value, dtype=float)
)
if tensor.shape != (3, 3) or not np.all(np.isfinite(tensor)):
raise ValueError("the dielectric constant is a number or a 3x3 tensor")
if not np.allclose(tensor, tensor.T, rtol=0, atol=1e-6 * float(np.max(np.abs(tensor)))):
raise ValueError("the dielectric tensor must be symmetric")
if float(np.linalg.eigvalsh(tensor).min()) < 1.0:
raise ValueError("a static dielectric tensor has no eigenvalue below 1")
return tensor
def _translations(rows: NDArray[np.float64], reach: float) -> NDArray[np.float64]:
"""Every lattice vector n.rows with |n.rows| <= reach (the origin included)."""
reciprocal = np.linalg.inv(rows).T # rows of b_i / (2 pi)
counts = [math.ceil(reach * float(np.linalg.norm(b))) for b in reciprocal]
if math.prod(2 * n + 1 for n in counts) > MAX_TERMS:
raise ValueError(
"the cell is too elongated for the point-charge sum; state the correction instead"
)
axes = [np.arange(-n, n + 1, dtype=float) for n in counts]
grid = np.stack(np.meshgrid(*axes, indexing="ij"), axis=-1).reshape(-1, 3)
vectors = grid @ rows
kept: NDArray[np.float64] = vectors[np.einsum("ij,ij->i", vectors, vectors) <= reach * reach]
return kept
[docs]
def madelung_potential(
lattice_angstrom: Any, dielectric: Any, *, splitting: float | None = None
) -> float:
"""``phi`` in 1/angstrom: images plus compensating background, at the charge site.
``splitting`` is the Ewald parameter g in 1/angstrom of the eps metric; the
default balances the real and reciprocal sums.
"""
rows = np.asarray(lattice_angstrom, dtype=float)
eps = dielectric_tensor(dielectric) if not isinstance(dielectric, np.ndarray) else dielectric
volume = abs(float(np.linalg.det(rows)))
if rows.shape != (3, 3) or volume < 1e-9:
raise ValueError("the point-charge term needs a three-dimensional cell")
det_eps = float(np.linalg.det(eps))
inverse = np.linalg.inv(eps)
low, high = (float(x) for x in np.linalg.eigvalsh(eps)[[0, -1]])
# The cell volume in the eps metric sets the natural length of the sum.
g = splitting or math.sqrt(math.pi) / (volume / math.sqrt(det_eps)) ** (1 / 3)
# Real space: d_R = sqrt(R.eps^-1.R) >= |R| / sqrt(high), so |R| <= reach.
vectors = _translations(rows, _TAIL / g * math.sqrt(high))
d = np.sqrt(np.einsum("ij,jk,ik->i", vectors, inverse, vectors))
d = d[d > 1e-12]
d = d[d * g <= _TAIL + 1]
real = sum(math.erfc(g * x) / x for x in d.tolist()) / math.sqrt(det_eps)
# Reciprocal space: G.eps.G >= low |G|^2, so |G| <= 2 g TAIL / sqrt(low).
reciprocal_rows = 2 * math.pi * np.linalg.inv(rows).T
waves = _translations(reciprocal_rows, 2 * g * _TAIL / math.sqrt(low))
q2 = np.einsum("ij,jk,ik->i", waves, eps, waves)
q2 = q2[q2 > 1e-12]
recip = 4 * math.pi / volume * float(np.sum(np.exp(-q2 / (4 * g * g)) / q2))
own = 2 * g / math.sqrt(math.pi * det_eps)
background = math.pi / (volume * g * g)
return float(real + recip - own - background)
[docs]
def point_charge_correction(
lattice_angstrom: Any, charge: int, dielectric: float | list[list[float]]
) -> dict[str, Any]:
"""The image-charge correction ``-(q^2/2) phi`` in eV, with what it rests on."""
eps = dielectric_tensor(dielectric)
phi = madelung_potential(lattice_angstrom, eps)
energy = -0.5 * charge * charge * phi * COULOMB_EV_ANGSTROM
return {
"point_charge_ev": energy,
"madelung_potential_per_angstrom": phi,
"dielectric_tensor": eps.tolist(),
}