-
Notifications
You must be signed in to change notification settings - Fork 2
/
plotWannierBands.py
executable file
·231 lines (193 loc) · 6.26 KB
/
plotWannierBands.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
#!/usr/bin/env python
""" Wannier90 band structure plotter.
This script plots the wannier90 band structure and compares
it to the DFT band structure.
Authors: Uthpala Herath and Andres Tellez
"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
import re
import argparse
from argparse import RawTextHelpFormatter
import pyprocar
import os
import sys
import warnings
warnings.filterwarnings("ignore")
# Show no PyProcar output
# f = open(os.devnull, "w")
# sys.stdout = f
def kpoint_conversion(value, kpoints1, kpoints2):
"""Finds transformation between DFT k-points and
wannier90 k-points. Original implementation by Andres Tellez."""
idx = 0
for point in kpoints1:
if value <= point:
idx = kpoints1.index(point)
break
result = value - kpoints1[idx - 1]
try:
result *= (kpoints2[idx] - kpoints2[idx - 1]) / (
kpoints1[idx] - kpoints1[idx - 1]
)
except ZeroDivisionError:
result *= 1.0
result += kpoints2[idx - 1]
return result
def plot_bands(
outcar="OUTCAR",
savefig="wannier90_bands.pdf",
show=False,
elimit=None,
compare=False,
):
"""This method plots wannier bands"""
plt.rcParams["mathtext.default"] = "regular"
plt.rcParams["font.family"] = "Arial"
plt.rc("font", size=22) # controls default text sizes
plt.rc("axes", titlesize=22) # fontsize of the axes title
plt.rc("axes", labelsize=22) # fontsize of the x and y labels
plt.rc("xtick", labelsize=22) # fontsize of the tick labels
plt.rc("ytick", labelsize=22) # fontsize of the tick labels
# Reading Fermi from OUTCAR
fi = open(outcar, "r")
for line in fi:
if re.search("Fermi energy", line) or re.search("E-fermi", line):
line_fermi = line
val = re.search(r"(\-?\d+\.?\d*)", line_fermi)
EFERMI = float(val.group(1))
fi.close()
print("Fermi energy = {:4.4f} eV".format(EFERMI))
# Reading labels
fi = open("wannier90_band.gnu", "r")
data = fi.read()
fi.close()
label_line = re.findall(r'xtics\s*\(([\s"A-Z0-9.,|]*)\)', data)
label_line_split = label_line[0].split(",")
ticks = []
knames = []
for i in label_line_split:
knames.append(i.split()[0])
ticks.append(float(i.split()[1]))
knames = [i.strip('"') for i in knames]
# Getting wannier band data
x = []
y = []
with open("wannier90_band.dat", "r") as f:
lines = f.readlines()
x.append([])
y.append([])
i = 0
for line in lines:
if line != " \n":
x[i].append(float(line.split()[0]))
y[i].append(float(line.split()[1]))
else:
x.append([])
y.append([])
i += 1
# There sometimes is an extra line in wannier90_band.dat.
# We will remove it if it is there.
if not x[-1]:
x = np.array(x[:-1])
y = np.array(y[:-1])
else:
x = np.array(x)
y = np.array(y)
if not compare:
# Plotting
fig = plt.figure(figsize=(13, 9))
ax = fig.add_subplot(111)
fig.tight_layout()
for i in range(len(x)):
ax.plot(x[i], y[i] - EFERMI, color="blue")
ax.set_xlim(x.min(), x.max())
if elimit:
ax.set_ylim(elimit)
ax.set_xticks(ticks)
ax.set_xticklabels(knames)
ax.set_xlabel(r"$k$-path")
ax.set_ylabel(r"$E-E_F$ (eV)")
ax.axhline(y=0, color="black", ls="--")
ax.grid()
for xc in ticks:
ax.axvline(x=xc, color="k")
if show:
plt.show()
return None, None
else:
plt.savefig(savefig, bbox_inches="tight")
return fig, ax
else:
# Comparison with DFT bands from PyProcar
# Input axis from PyProcar plot to get x axis ticks
fig, ax = pyprocar.bandsplot(
"PROCAR",
outcar=outcar,
kpointsfile="KPOINTS",
elimit=elimit,
mode="plain",
color="red",
show=False,
verbose=False,
)
ticks_pyprocar = [ax.lines[-i].get_xdata()[0] for i in range(2, len(ticks) + 2)]
ticks_pyprocar.sort()
x_new = np.zeros((x.shape), dtype="float64")
for ix in range(len(x)):
for iix in range(x.shape[1]):
x_new[ix, iix] = kpoint_conversion(x[ix, iix], ticks, ticks_pyprocar)
# Plotting
for i in range(len(x_new)):
ax.plot(x_new[i], y[i] - EFERMI, color="blue", linewidth=2)
ax.set_xlim(x_new.min(), x_new.max())
if elimit:
ax.set_ylim(elimit)
ax.set_xticks(ticks)
ax.set_xticklabels(knames)
ax.set_xlabel(r"$k$-path")
ax.set_ylabel(r"$E-E_F$ (eV)")
ax.axhline(y=0, color="black", ls="--")
ax.grid()
# for xc in ticks:
# ax.axvline(x=xc, color="k")
# legend
custom_lines = [
Line2D([0], [0], color="red", lw=2),
Line2D([0], [0], color="blue", lw=2),
]
plt.legend(custom_lines, ["DFT", "wannier90"])
# replot with PyProcar with new axis
if show:
savefig = None
pyprocar.bandsplot(
"PROCAR",
outcar=outcar,
kpointsfile="KPOINTS",
elimit=elimit,
mode="plain",
color="red",
show=show,
ax=ax,
savefig=savefig,
verbose=False,
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=RawTextHelpFormatter
)
parser.add_argument("-outcar", type=str, help="SCF OUTCAR file.", default="OUTCAR")
parser.add_argument(
"-elimit", type=float, nargs=2, help="Energy axis range", default=None
)
parser.add_argument("-show", action="store_true", help="Flag to show plot.")
parser.add_argument(
"-compare",
action="store_true",
help="Flag to compare wannier90 bands with DFT bands (Requires PyProcar with PROCAR, KPOINTS and SCF OUTCAR).",
)
args = parser.parse_args()
plot_bands(
outcar=args.outcar, elimit=args.elimit, show=args.show, compare=args.compare
)