Coverage for ase/dft/dos.py: 79.41%
136 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
1from math import pi, sqrt
3import numpy as np
5from ase.dft.kpoints import get_monkhorst_pack_size_and_offset
6from ase.parallel import world
7from ase.utils.cext import cextension
10class DOS:
11 def __init__(self, calc, width=0.1, window=None, npts=401, comm=world):
12 """Electronic Density Of States object.
14 calc: calculator object
15 Any ASE compliant calculator object.
16 width: float
17 Width of guassian smearing. Use width=0.0 for linear tetrahedron
18 interpolation.
19 window: tuple of two float
20 Use ``window=(emin, emax)``. If not specified, a window
21 big enough to hold all the eigenvalues will be used.
22 npts: int
23 Number of points.
24 comm: communicator object
25 MPI communicator for lti_dos
27 """
29 self.comm = comm
30 self.npts = npts
31 self.width = width
32 self.w_k = calc.get_k_point_weights()
33 self.nspins = calc.get_number_of_spins()
34 self.e_skn = np.array(
35 [
36 [
37 calc.get_eigenvalues(kpt=k, spin=s)
38 for k in range(len(self.w_k))
39 ]
40 for s in range(self.nspins)
41 ]
42 )
43 try: # two Fermi levels
44 for i, eF in enumerate(calc.get_fermi_level()):
45 self.e_skn[i] -= eF
46 except TypeError: # a single Fermi level
47 self.e_skn -= calc.get_fermi_level()
49 if window is None:
50 emin = None
51 emax = None
52 else:
53 emin, emax = window
55 if emin is None:
56 emin = self.e_skn.min() - 5 * self.width
57 if emax is None:
58 emax = self.e_skn.max() + 5 * self.width
60 self.energies = np.linspace(emin, emax, npts)
62 if width == 0.0:
63 bzkpts = calc.get_bz_k_points()
64 size, _offset = get_monkhorst_pack_size_and_offset(bzkpts)
65 bz2ibz = calc.get_bz_to_ibz_map()
66 shape = (self.nspins,) + tuple(size) + (-1,)
67 self.e_skn = self.e_skn[:, bz2ibz].reshape(shape)
68 self.cell = calc.atoms.cell
70 def get_energies(self):
71 """Return the array of energies used to sample the DOS.
73 The energies are reported relative to the Fermi level.
74 """
75 return self.energies
77 def delta(self, energy):
78 """Return a delta-function centered at 'energy'."""
79 x = -(((self.energies - energy) / self.width) ** 2)
80 return np.exp(x) / (sqrt(pi) * self.width)
82 def get_dos(self, spin=None):
83 """Get array of DOS values.
85 The *spin* argument can be 0 or 1 (spin up or down) - if not
86 specified, the total DOS is returned.
87 """
89 if spin is None:
90 if self.nspins == 2:
91 # Return the total DOS
92 return self.get_dos(spin=0) + self.get_dos(spin=1)
93 else:
94 return 2 * self.get_dos(spin=0)
95 elif spin == 1 and self.nspins == 1:
96 # For an unpolarized calculation, spin up and down are equivalent
97 spin = 0
99 if self.width == 0.0:
100 dos = linear_tetrahedron_integration(
101 self.cell, self.e_skn[spin], self.energies, comm=self.comm
102 )
103 return dos
105 dos = np.zeros(self.npts)
106 for w, e_n in zip(self.w_k, self.e_skn[spin]):
107 for e in e_n:
108 dos += w * self.delta(e)
109 return dos
112def linear_tetrahedron_integration(
113 cell, eigs, energies, weights=None, comm=world
114):
115 """DOS from linear tetrahedron interpolation.
117 cell: 3x3 ndarray-like
118 Unit cell.
119 eigs: (n1, n2, n3, nbands)-shaped ndarray
120 Eigenvalues on a Monkhorst-Pack grid (not reduced).
121 energies: 1-d array-like
122 Energies where the DOS is calculated (must be a uniform grid).
123 weights: ndarray of shape (n1, n2, n3, nbands) or (n1, n2, n3, nbands, nw)
124 Weights. Defaults to a (n1, n2, n3, nbands)-shaped ndarray
125 filled with ones. Can also have an extra dimednsion if there are
126 nw weights.
127 comm: communicator object
128 MPI communicator for lti_dos
130 Returns
131 -------
133 DOS as an ndarray of same length as energies or as an
134 ndarray of shape (nw, len(energies)).
136 See:
138 Extensions of the tetrahedron method for evaluating
139 spectral properties of solids,
140 A. H. MacDonald, S. H. Vosko and P. T. Coleridge,
141 1979 J. Phys. C: Solid State Phys. 12 2991,
142 :doi:`10.1088/0022-3719/12/15/008`
143 """
145 from scipy.spatial import Delaunay
147 # Find the 6 tetrahedra:
148 size = eigs.shape[:3]
149 B = (np.linalg.inv(cell) / size).T
150 indices = np.array(
151 [[i, j, k] for i in [0, 1] for j in [0, 1] for k in [0, 1]]
152 )
153 dt = Delaunay(np.dot(indices, B))
155 if weights is None:
156 weights = np.ones_like(eigs)
158 if weights.ndim == 4:
159 extra_dimension_added = True
160 weights = weights[:, :, :, :, np.newaxis]
161 else:
162 extra_dimension_added = False
164 nweights = weights.shape[4]
165 dos = np.empty((nweights, len(energies)))
167 lti_dos(indices[dt.simplices], eigs, weights, energies, dos, comm)
169 dos /= np.prod(size)
171 if extra_dimension_added:
172 return dos[0]
173 return dos
176@cextension
177def lti_dos(simplices, eigs, weights, energies, dos, world):
178 shape = eigs.shape[:3]
179 nweights = weights.shape[-1]
180 dos[:] = 0.0
181 n = -1
182 for index in np.indices(shape).reshape((3, -1)).T:
183 n += 1
184 if n % world.size != world.rank:
185 continue
186 i = ((index + simplices) % shape).T
187 E = eigs[i[0], i[1], i[2]].reshape((4, -1))
188 W = weights[i[0], i[1], i[2]].reshape((4, -1, nweights))
189 for e, w in zip(E.T, W.transpose((1, 0, 2))):
190 lti_dos1(e, w, energies, dos)
192 dos /= 6.0
193 world.sum(dos)
196def lti_dos1(e, w, energies, dos):
197 i = e.argsort()
198 e0, e1, e2, e3 = en = e[i]
199 w = w[i]
201 zero = energies[0]
202 if len(energies) > 1:
203 de = energies[1] - zero
204 nn = (np.floor((en - zero) / de).astype(int) + 1).clip(0, len(energies))
205 else:
206 nn = (en > zero).astype(int)
208 n0, n1, n2, n3 = nn
210 if n1 > n0:
211 s = slice(n0, n1)
212 x = energies[s] - e0
213 f10 = x / (e1 - e0)
214 f20 = x / (e2 - e0)
215 f30 = x / (e3 - e0)
216 f01 = 1 - f10
217 f02 = 1 - f20
218 f03 = 1 - f30
219 g = f20 * f30 / (e1 - e0)
220 dos[:, s] += w.T.dot([f01 + f02 + f03, f10, f20, f30]) * g
221 if n2 > n1:
222 delta = e3 - e0
223 s = slice(n1, n2)
224 x = energies[s]
225 f20 = (x - e0) / (e2 - e0)
226 f30 = (x - e0) / (e3 - e0)
227 f21 = (x - e1) / (e2 - e1)
228 f31 = (x - e1) / (e3 - e1)
229 f02 = 1 - f20
230 f03 = 1 - f30
231 f12 = 1 - f21
232 f13 = 1 - f31
233 g = 3 / delta * (f12 * f20 + f21 * f13)
234 dos[:, s] += w.T.dot(
235 [
236 g * f03 / 3 + f02 * f20 * f12 / delta,
237 g * f12 / 3 + f13 * f13 * f21 / delta,
238 g * f21 / 3 + f20 * f20 * f12 / delta,
239 g * f30 / 3 + f31 * f13 * f21 / delta,
240 ]
241 )
242 if n3 > n2:
243 s = slice(n2, n3)
244 x = energies[s] - e3
245 f03 = x / (e0 - e3)
246 f13 = x / (e1 - e3)
247 f23 = x / (e2 - e3)
248 f30 = 1 - f03
249 f31 = 1 - f13
250 f32 = 1 - f23
251 g = f03 * f13 / (e3 - e2)
252 dos[:, s] += w.T.dot([f03, f13, f23, f30 + f31 + f32]) * g