-
Notifications
You must be signed in to change notification settings - Fork 1
/
dis_win_suggest.py
324 lines (279 loc) · 14 KB
/
dis_win_suggest.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import numpy as np
import pandas as pd
import os, re
import matplotlib.pyplot as plt
import sys
import argparse
class W90():
def __init__(self, eig='EIGENVAL', path='.', win='wannier90.win', efermi=None, nbnds_excl=None, nwann=None, ndeg=1):
'''
Init
'''
self._fname = eig
self._win = win
# the directory containing the input file
self._dname = path
# if poscar is None:
# self.poscar = self._dname + '/POSCAR'
self.efermi = efermi
self.nbnds_excl = nbnds_excl
self.nwann = nwann
self.ndeg = ndeg # denegeracy of bands, actually only Kramers degeneracy counts
self.read_eigenval()
# if os.path.isfile(self._win):
# self.read_wannier90_win()
def read_eigenval(self):
'''
Read band energies from VASP EIGENVAL file or Wannier90 .eig file.
'''
if self._fname[-3:] == 'eig':
self.nspin = 1
self.nelect = None
data = np.loadtxt(f'{self._dname}/{self._fname}')
self.ir_kpath = None
self.ir_kptwt = None
self.ir_nkpts = int(data[:, 1].max())
self.nbnds = int(data[:, 0].max())
self.ir_ebands = data[:, 2].reshape((self.ir_nkpts, 1, -1)).swapaxes(0, 1)
else:
with open(f'{self._dname}/{self._fname}') as inp:
# read all the data.
dat = np.array([line.strip() for line in inp if line.strip()])
# extract the needed info in the header.
self.nspin = int(dat[0].split()[-1])
self.nelect, self.ir_nkpts, self.nbnds = map(int, dat[5].split())
# remove the header
dat = dat[6:]
# extract the k-points info
dump = np.array(
[xx.split() for xx in dat[::self.nspin * self.nbnds + 1]], dtype=float)
self.ir_kpath = dump[:self.ir_nkpts, :3]
self.ir_kptwt = dump[:self.ir_nkpts, -1]
# extract the ebands info
ebands_flag = np.ones(dat.size, dtype=bool)
ebands_flag[::self.nspin * self.nbnds + 1] = 0
if self.nspin == 1:
ebands = np.array([xx.split()[1] for xx in dat[ebands_flag]],
dtype=float)
else:
ebands = np.array([xx.split()[1:3] for xx in dat[ebands_flag]],
dtype=float)
ebands.shape = (self.ir_nkpts, self.nspin, self.nbnds)
self.ir_ebands = ebands.swapaxes(0, 1)
self.emax = self.ir_ebands.max()
self.emin = self.ir_ebands.min()
self.eband_max = np.max(self.ir_ebands, axis=1)[0]
self.eband_min = np.min(self.ir_ebands, axis=1)[0]
def read_wannier90_win(self):
'''
Read parameters from wannier90.win file.
'''
with open(self._win) as f:
dat = [line.strip() for line in f if line.strip()]
for l in dat:
r = re.match('num_wann\s*=\s*(\d+)', l)
if r:
self.nwann = eval(r.group(1))
r = re.match('dis_win_max\s*=\s*(-?\d+\.\d+)', l)
if r:
self.win_max = eval(r.group(1))
r = re.match('dis_froz_max\s*=\s*(-?\d+\.\d+)', l)
if r:
self.froz_max = eval(r.group(1))
r = re.match('dis_froz_min\s*=\s*(-?\d+\.\d+)', l)
if r:
self.froz_min = eval(r.group(1))
r = re.match('dis_win_min\s*=\s*(-?\d+\.\d+)', l)
if r:
self.win_min = eval(r.group(1))
def plot_eigenval(self, erange=None, separate=False, savefig='eigenval_dis.png'):
fig, ax = plt.subplots(figsize=(8, 6))
def label_bar(string, height, rect):
"""Attach a text label on top of bar."""
ax.annotate(f'{string}',
xy=(rect.get_x() + rect.get_width()/2, height),
xytext=(0, 0), # 4 points vertical offset.
textcoords='offset points',
ha='center', va='bottom')
if not separate: # 不区分单独的能带绘制分布图
idx = self.eband_min[1:] > self.eband_max[:-1]
idx = np.nonzero(idx)[0]
if self.nbnds_excl:
idx = idx[idx > (self.nbnds_excl - 1)]
idx_min, idx_max = [self.nbnds_excl]+list(idx+1), list(idx)+[self.nbnds-1]
else:
idx_min, idx_max = [0]+list(idx+1), list(idx)+[self.nbnds-1]
eplot_min, eplot_max = self.eband_min[idx_min], self.eband_max[idx_max]
ymin, ymax = erange if erange else (-1e4, 1e4)
for i, (emin, emax) in enumerate(zip(eplot_min, eplot_max)):
if emax > ymin and emin < ymax:
rect = ax.bar(i, emax-emin, width=0.6, bottom=emin-self.efermi, color='b')
label_bar(f'{idx_min[i]-self.nbnds_excl}', emax, rect[0])
else: # 区分单独的能带绘制分布
idx = np.arange(self.nbnds_excl, self.nbnds-1, self.ndeg) if self.nbnds_excl else np.arange(0, self.nbnds-1, self.ndeg)
ymin, ymax = erange if erange else (-1e4, 1e4)
for i in idx:
emin, emax = self.eband_min[i], self.eband_max[i]
if emax > ymin and emin < ymax:
rect = ax.bar(i, emax-emin, width=0.6, bottom=emin-self.efermi, color='b')
label_bar(f'{i-self.nbnds_excl}', emax, rect[0])
ax.set(ylabel='Energy / eV')
ax.grid()
plt.savefig(savefig, dpi=200, bbox_inches='tight', transparent=True)
# plt.show()
def report_eigenval(self, erange=None, separate=False):
print(f'EFERMI: {self.efermi: 2.6f}')
print('--------------------------------')
print('Band No. EMIN EMAX')
print('--------------------------------')
if not separate: # 不区分单独的能带绘制分布图
idx = self.eband_min[1:] > self.eband_max[:-1]
idx = np.nonzero(idx)[0]
if self.nbnds_excl:
idx = idx[idx > (self.nbnds_excl - 1)]
idx_min, idx_max = [self.nbnds_excl]+list(idx+1), list(idx)+[self.nbnds-1]
else:
idx_min, idx_max = [0]+list(idx+1), list(idx)+[self.nbnds-1]
eplot_min, eplot_max = self.eband_min[idx_min], self.eband_max[idx_max]
ymin, ymax = erange if erange else (-1e4, 1e4)
for i, (emin, emax) in enumerate(zip(eplot_min, eplot_max)):
if emax > ymin and emin < ymax:
print(f'{idx_min[i]:3d}~{idx_max[i]:3d} {emin:+10.5f} {emax:+10.5f}')
else: # 区分单独的能带绘制分布
idx = np.arange(self.nbnds_excl, self.nbnds-1, self.ndeg) if self.nbnds_excl else np.arange(0, self.nbnds-1, self.ndeg)
ymin, ymax = erange if erange else (-1e4, 1e4)
for i in idx:
emin, emax = self.eband_min[i], self.eband_max[i]
if emax > ymin and emin < ymax:
print(f' {i:3d} {emin:+10.5f} {emax:+10.5f}')
print('--------------------------------')
def count_states(self, erange):
emin, emax = erange
mask = np.logical_and(self.eband_min <= emax, self.eband_max >= emin)
return sum(mask)
def suggest_win_max(self, emin, nwann=None, eps=4e-3):
'''
Lower bound of dis_win_max for given froz_min and nwann
'''
nwann = nwann if nwann else self.nwann
mask = self.eband_min >= emin
idx = np.argmax(mask) + nwann - 1
if idx >= self.nbnds:
# TODO handle error case for no enough states
print(f'There is no enough states for {emin} with {nwann} WFs!')
res = int(self.emax) + 1.
else:
res = self.eband_max[idx] + eps
return res
def suggest_froz_min(self, emax, nwann=None, eps=4e-3):
'''
Lower bound of froz_min for given froz_max and nwann
'''
nwann = nwann if nwann else self.nwann
mask_emax = self.eband_min <= emax
idx = np.argmin(mask_emax) - nwann - 1
res = int(self.emin) - 1. if idx < 0 else self.eband_max[idx] + eps
return res
def suggest_froz_max(self, emin, nwann=None, eps=4e-3):
'''
Upper bound of froz_max for given froz_min and nwann
'''
nwann = nwann if nwann else self.nwann
mask_emin = self.eband_max >= emin
idx = np.argmax(mask_emin) + nwann
res = int(self.emax) + 1. if idx >= self.nbnds else self.eband_min[idx] - eps
return res
def get_dis_froz_df(self, erange, eps=4e-3):
# suggest frozen window with given energy interval
N = self.count_states(erange)
print(f'There are {N} states in {erange} with Fermi level at {self.efermi}.')
emin, emax = erange
dN = N - self.nwann
froz_min_list, froz_max_list = [], []
if self.nwann <= 0:
print(f'Please input vaild number of WF, now is {self.nwann}.')
return pd.DataFrame(columns=['dis_froz_min', 'dis_froz_max'])
elif dN > 0:
print('Suggest dis_froz_min & dis_froz_max as following:')
print(f'nwann: {self.nwann} degenercy: {self.ndeg} Fermi: {self.efermi:12.6f}')
for i in range(1, dN + 1, self.ndeg):
# First get froz_max for nwann = nwann_input + i, then get froz_min for nwann = nwann_input and froz_max
froz_max = self.suggest_froz_max(emin, nwann=self.nwann+i, eps=eps)
froz_min = self.suggest_froz_min(froz_max, eps=eps)
froz_max_list.append(froz_max)
froz_min_list.append(froz_min)
# number of missing states between `emin` and lowest `dis_froz_min`
num_missing = self.count_states((emin, min(froz_min_list)))
df = pd.DataFrame(zip(froz_min_list, froz_max_list), columns=['dis_froz_min', 'dis_froz_max'])
if num_missing > 0:
print(f'\nWANRING: There are states between given `emin`: {emin} and lowest `dis_froz_min`: {min(froz_min_list)}. Please carefully treat the suggestion of dis_froz_min / dis_froz_max and check energy range of each bands again. This situation usually happens in no-SOC system with many denegeracy point. But we still want to give you some useful energy window information with states less than number of WFs.\n')
for i in range(self.nwann - num_missing + 1, self.nwann, 1):
new = pd.DataFrame({"dis_froz_min" : emin,
"dis_froz_max" : self.suggest_froz_max(emin, nwann=i)}, index=[1])
df = df.append(new, ignore_index=True)
return df
else:
return pd.DataFrame(columns=['dis_froz_min', 'dis_froz_max'])
def get_efermi(args, direct=False):
if not direct and args.efermi:
efermi = float(args.efemri)
else:
efermi_str = os.popen(f'grep fermi {args.path}/vasprun.xml').read().strip()
m = re.match('.+ ((\-|\+)?\d+(\.\d+)?) .+', efermi_str)
efermi = float(m.groups()[0])
return efermi
def get_args():
'''
CML parser.
'''
parser = argparse.ArgumentParser(description='CLI Tool for W90 energy windows.', add_help=True)
parser.add_argument('mode', help='Mode: report, plot, count, suggest')
parser.add_argument('-i', dest='eig', action='store', type=str,
default='EIGENVAL',
help='Select wannier90.eig file or EIGENVAL file. Default: EIGENVAL')
parser.add_argument('--path', default='.',
help='Default: .')
parser.add_argument('--efermi', dest='efermi', action='store',
default=None,
help='Fermi level. Default value is generated from `vasprun.xml`.')
parser.add_argument('-w', dest='nwann', action='store', type=int,
default=0,
help='Number of Wannier Functions. Default: 0')
parser.add_argument('-n', dest='nbnds_excl', action='store', type=int,
default=0,
help='Number of bands excluded')
parser.add_argument('-d', dest='ndeg', action='store', type=int,
default=2,
help='Number of degeneracy')
parser.add_argument('-e', dest='erange', action='store', type=float,
default=None, nargs=2,
help='Energy range.')
parser.add_argument('--separate', default=False, action="store_true",
help='Calculate bands not separately.')
return parser.parse_args()
if __name__ == "__main__":
args = get_args()
w90 = W90(eig=args.eig,
path=args.path,
efermi=get_efermi(args),
nbnds_excl=args.nbnds_excl,
nwann=args.nwann,
ndeg=args.ndeg)
if args.mode[0].lower() == 'p': # plot
w90.plot_eigenval(erange=args.erange, separate=args.separate)
elif args.mode[0].lower() == 'r': # report
w90.report_eigenval(erange=args.erange, separate=args.separate)
elif args.mode[0].lower() == 'c': # count
# Count how many states inside the energy interval
print(f'There are {w90.count_states(args.erange)} states in {args.erange}.')
elif args.mode[0].lower() == 's': # suggest
# suggest frozen window with given energy interval
print(f'`dis_froz_min` and `dis_froz_max` Table:')
df = w90.get_dis_froz_df(args.erange, eps=4e-3)
if len(df) > 0:
print(df)
# dis_windows require energy window containing states larger than number of target WFs. This will also generate some constraint for dis_windows
dis_win_max = w90.suggest_win_max(args.erange[0])
print(f'\nLowest `dis_win_max` for {args.erange[0]}: {dis_win_max}')
else:
print(f'Unsupported mode: {args.mode}')