/
dos.py
297 lines (264 loc) · 11.1 KB
/
dos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# coding: utf-8
# Copyright (c) Max-Planck-Institut für Eisenforschung GmbH - Computational Materials Design (CM) Department
# Distributed under the terms of "New BSD License", see the LICENSE file.
from __future__ import print_function
import numpy as np
__author__ = "Sudarsan Surendralal"
__copyright__ = (
"Copyright 2020, Max-Planck-Institut für Eisenforschung GmbH - "
"Computational Materials Design (CM) Department"
)
__version__ = "1.0"
__maintainer__ = "Sudarsan Surendralal"
__email__ = "surendralal@mpie.de"
__status__ = "development"
__date__ = "Sep 1, 2017"
class Dos(object):
"""
The DOS class stores all information to store and retrieve the total and resolved density of states from an
electronic structure calculation.
Args:
n_bins (int): Number of histogram bins required to calculate the DOS
es_obj: The pyiron.objects.waves.core.ElectronicStructure instance for which the DOS has to be computed
eigenvalues (list/numpy.ndarray): If es-obj is None, the eigenvalues could be specified as a list
"""
def __init__(self, n_bins=100, es_obj=None, eigenvalues=None, bin_density=None):
self.orbital_dict = {"s": [0], "p": [1, 2, 3], "d": [4, 5, 6, 7, 8]}
self.n_bins = n_bins
self.es_obj = es_obj
dos_min = np.min(self.es_obj.eigenvalues)
dos_max = np.max(self.es_obj.eigenvalues)
if bin_density is not None:
n_bins = int((dos_max - dos_min) * bin_density)
if es_obj is not None:
self.t_dos, self.energies = np.histogram(
self.es_obj.eigenvalues, bins=int(n_bins), density=True
)
else:
self.t_dos, self.energies = np.histogram(
eigenvalues, bins=int(n_bins), density=True
)
self.energies = self.energies[1:] - (
(self.energies[1] - self.energies[0]) / 2.0
)
def plot_total_dos(self, **kwargs):
"""
Plots the total DOS
Args:
**kwargs: Variables for matplotlib.pylab.plot customization (linewidth, linestyle, etc.)
Returns:
matplotlib.pylab.plot
"""
try:
import matplotlib.pylab as plt
except ImportError:
import matplotlib.pyplot as plt
fig = plt.figure(1, figsize=(6, 4))
ax1 = fig.add_subplot(111)
ax1.set_xlabel("E (eV)", fontsize=14)
ax1.set_ylabel("DOS", fontsize=14)
plt.fill_between(self.energies, self.t_dos, **kwargs)
return plt
def plot_orbital_resolved_dos(self, **kwargs):
"""
Plots the orbital resolved DOS
Args:
**kwargs: Variable for matplotlib.pylab.plot customization (linewidth, linestyle, etc.)
Returns:
matplotlib.pylab.plot
"""
try:
import matplotlib.pylab as plt
except ImportError:
import matplotlib.pyplot as plt
if not (self.es_obj.grand_dos_matrix is not None):
raise NoResolvedDosError(
"Can not plot the orbital resolved dos since resolved dos values are not"
" available"
)
plot = self.plot_total_dos()
for key, val in self.orbital_dict.items():
r_dos = self.get_orbital_resolved_dos(val)
plt.plot(self.energies, r_dos, label=key, **kwargs)
plot.legend()
return plot
def get_spin_resolved_dos(self, spin_indices):
"""
Gives the dos contribution of a given indices of spin as arranged in the
pyiron.objects.waves.ElectronicStructure instance.
Args:
spin_indices (list/numpy.ndarray): The index/indices of the spins for which the dos contribution is required
Returns:
numpy.ndarray: The required dos
"""
if not (self.es_obj.grand_dos_matrix is not None):
raise NoResolvedDosError(
"Can not get the spin resolved dos since resolved dos values are not"
" available"
)
grand_sum = np.sum(self.es_obj.grand_dos_matrix)
tot_val = self.es_obj.grand_dos_matrix.copy() / grand_sum
n_spin, n_kpts, n_bands, _, _ = np.shape(tot_val)
k = 0
b = 0
r_dos = np.zeros_like(self.t_dos)
w_dos = np.zeros_like(self.t_dos)
for i, e in enumerate(self.es_obj.eigenvalues):
weight = np.sum(tot_val[spin_indices, k, b, :, :])
weight_sum = np.sum(tot_val[:, k, b, :, :])
if b < n_bands - 1:
b += 1
else:
b = 0
k += 1
index = len(self.energies[self.energies < e]) - 1
if index >= 0:
r_dos[index] = r_dos[index] + weight
w_dos[index] = w_dos[index] + weight_sum
else:
r_dos[0] = r_dos[0] + weight
w_dos[0] = w_dos[0] + weight_sum
ind_0 = np.argwhere(w_dos < 1e-8).flatten()
ind_1 = np.argwhere(w_dos >= 1e-8).flatten()
r_dos[ind_1] /= w_dos[ind_1]
r_dos[ind_0] = 0.0
return r_dos * self.t_dos
def get_spatially_resolved_dos(self, atom_indices, spin_indices=0):
"""
Gives the dos contribution of a given indices of atoms as arranged in the
pyiron.objects.waves.ElectronicStructure instance.
Args:
atom_indices (list/numpy.ndarray): The index/indices of the atoms for which the dos contribution is required
spin_indices (list/numpy.ndarray): The index/indices of the spins for which the dos contribution is required
Returns:
numpy.ndarray: The required dos
"""
if not (self.es_obj.grand_dos_matrix is not None):
raise NoResolvedDosError(
"Can not get the spatially resolved dos since resolved dos values are not"
" available"
)
grand_sum = np.sum(self.es_obj.grand_dos_matrix)
tot_val = self.es_obj.grand_dos_matrix.copy() / grand_sum
_, n_kpts, n_bands, _, _ = np.shape(tot_val)
k = 0
b = 0
r_dos = np.zeros_like(self.t_dos)
w_dos = np.zeros_like(self.t_dos)
for i, e in enumerate(self.es_obj.eigenvalues):
weight = np.sum(tot_val[spin_indices, k, b, atom_indices, :])
weight_sum = np.sum(tot_val[spin_indices, k, b, :, :])
if b < n_bands - 1:
b += 1
else:
b = 0
k += 1
index = len(self.energies[self.energies < e]) - 1
if index >= 0:
r_dos[index] = r_dos[index] + weight
w_dos[index] = w_dos[index] + weight_sum
else:
r_dos[0] = r_dos[0] + weight
w_dos[0] = w_dos[0] + weight_sum
ind_0 = np.argwhere(w_dos < 1e-8).flatten()
ind_1 = np.argwhere(w_dos >= 1e-8).flatten()
r_dos[ind_1] /= w_dos[ind_1]
r_dos[ind_0] = 0.0
return r_dos * self.t_dos
def get_orbital_resolved_dos(self, orbital_indices, spin_indices=0):
"""
Gives the dos contribution of a given indices of orbitals as arranged in the
pyiron.objects.waves.ElectronicStructure instance.
Args:
orbital_indices (list/numpy.ndarray): The index/indices of the orbitals for which the dos contribution is required
spin_indices (list/numpy.ndarray): The index/indices of the spins for which the dos contribution is required
Returns:
numpy.ndaray: The required dos
"""
if not (self.es_obj.grand_dos_matrix is not None):
raise NoResolvedDosError(
"Can not get the orbital resolved dos since resolved dos values are not"
" available"
)
grand_sum = np.sum(self.es_obj.grand_dos_matrix)
tot_val = self.es_obj.grand_dos_matrix.copy() / grand_sum
_, n_kpts, n_bands, _, _ = np.shape(tot_val)
k = 0
b = 0
r_dos = np.zeros_like(self.t_dos)
w_dos = np.zeros_like(self.t_dos)
for i, e in enumerate(self.es_obj.eigenvalues):
weight = np.sum(tot_val[spin_indices, k, b, :, orbital_indices])
weight_sum = np.sum(tot_val[spin_indices, k, b, :, :])
if b < n_bands - 1:
b += 1
else:
b = 0
k += 1
index = len(self.energies[self.energies < e]) - 1
if index >= 0:
r_dos[index] = r_dos[index] + weight
w_dos[index] = w_dos[index] + weight_sum
else:
r_dos[0] = r_dos[0] + weight
w_dos[0] = w_dos[0] + weight_sum
ind_0 = np.argwhere(w_dos < 1e-8).flatten()
ind_1 = np.argwhere(w_dos >= 1e-8).flatten()
r_dos[ind_1] /= w_dos[ind_1]
r_dos[ind_0] = 0.0
return r_dos * self.t_dos
def get_spatial_orbital_resolved_dos(
self, atom_indices, orbital_indices, spin_indices=0
):
"""
Gives the dos contribution of a given indices of atoms as well as orbitals as arranged in the
pyiron.objects.waves.ElectronicStructure instance.
Args:
atom_indices (list/numpy.ndarray): The index/indices of the atoms for which the dos contribution is required
orbital_indices (list/numpy.ndarray): The index/indices of the orbitals for which the dos contribution is required
spin_indices (list/numpy.ndarray): The index/indices of the spins for which the dos contribution is required
Returns:
numpy.ndaray: The required dos
"""
if not (self.es_obj.grand_dos_matrix is not None):
raise NoResolvedDosError(
"Can not get the resolved dos since resolved dos values are not"
" available"
)
grand_sum = np.sum(self.es_obj.grand_dos_matrix)
tot_val = self.es_obj.grand_dos_matrix.copy() / grand_sum
_, n_kpts, n_bands, _, _ = np.shape(tot_val)
k = 0
b = 0
r_dos = np.zeros_like(self.t_dos)
w_dos = np.zeros_like(self.t_dos)
for i, e in enumerate(self.es_obj.eigenvalues):
weight = np.sum(
[
np.sum(tot_val[spin_indices, k, b, atom_indices, o])
for o in orbital_indices
]
)
weight_sum = np.sum(tot_val[spin_indices, k, b, :, :])
if b < n_bands - 1:
b += 1
else:
b = 0
k += 1
index = len(self.energies[self.energies < e]) - 1
if index >= 0:
r_dos[index] = r_dos[index] + weight
w_dos[index] = w_dos[index] + weight_sum
else:
r_dos[0] = r_dos[0] + weight
w_dos[0] = w_dos[0] + weight_sum
ind_0 = np.argwhere(w_dos < 1e-8).flatten()
ind_1 = np.argwhere(w_dos >= 1e-8).flatten()
r_dos[ind_1] /= w_dos[ind_1]
r_dos[ind_0] = 0.0
return r_dos * self.t_dos
class NoResolvedDosError(Exception):
"""
Raised when information on the resolved dos in unavailable
"""
pass