forked from ryyesterday/VASP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readDOSCAR.py
118 lines (94 loc) · 3.13 KB
/
readDOSCAR.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
#!/usr/bin/python
# Ryan Valenza
# 2014-11-06
# This script is the start of a project to create a series of
# modules to interpret VASP outfiles. The end goal is to be able
# to quickly create plots and format data for external use.
# Note: It has been assumed that the VASP calculation was non
# collinear (non spin-polarized). This may be implemented
# by setting ISPIN = 1 in INCAR. Therefore, the default
# format is [energy] [DOS-total] [DOS-integrated]
import matplotlib.pyplot as plt
import sys
def readDOSCAR(filename):
doscar = open(filename,"r")
filename = filename.split("\\")[-1]
# e.g. /home/ryval/DOSCAR, grab DOSCAR
print "# Filename: " + filename
# Skip file header
doscar.readline() # N/A
doscar.readline() # N/A
doscar.readline() # N/A
doscar.readline() # Cartesian/Direct
# System name w/ stripped newline character
name = doscar.readline().rstrip()
print "# System: " + name
first = doscar.readline()
(emax, emin, ndos, efermi, weight) = first.split()
print "# Max Energy: %.4f" % float(emax) + " eV"
print "# Min Energy: %.4f" % float(emin) + " eV"
print "# Fermi Energy: %.4f" % float(efermi) + " eV"
print "# " + ndos + " points calculated"
print # Buffer
es = []
tds = []
ids = []
# Loop through the rest of the file and grab DOS - energy pairs
for i in range(int(ndos)):
line = doscar.readline()
(energy, tdos, idos) = line.split()
try:
energy = float(energy)
tdos = float(tdos)
idos = float(idos)
except ValueError: # For when VASP misses the E in a float
continue
tds.append(tdos)
es.append(energy-float(efermi))
ids.append(idos)
plt.subplot(2,1,1) # The first subplot in the first figure
plt.plot(es,tds,'-',label=filename)
plt.ylabel('Total DOS')
plt.subplot(2,1,2) # The second subplot in the first figure
plt.plot(es,ids,'-')
plt.ylabel('Integrated DOS')
plt.xlabel(r'$E - E_f$ (eV)')
plt.subplot(2,1,1) # Make first subplot current (for title)
plt.title(name + " DOS")
plt.legend()
def main():
"""
readDOSCAR.py is used to generate plots of the total
and integrated density of states calculated using VASP.
it can be passed a list of filenames via CLI or a file
that contains a list of filenames, using the -f option.
readDOSCAR.py DOSCAR1 DOSCAR2 DOSCAR3 ...
readDOSCAR.py -f [DOS-file]
Example DOS-file:
/path/to/DOSCAR1
/path/to/DOSCAR2
/path/to/DOSCAR3
"""
print # Buffer
plt.figure('Density of States', figsize = (10,10)) # Create figure 1
# Could use argparse if number of options increases
# Ideas: look for OUTCAR, find number of electrons
# and plot a horizontal line @ that number
if sys.argv[1] == '-f':
filelist = open(sys.argv[2],"r")
for dosfile in filelist:
readDOSCAR(dosfile.rstrip())
else:
for cl_arg in sys.argv:
if cl_arg != sys.argv[0]: # Skip readDOSCAR.py from CL
readDOSCAR(cl_arg)
plt.show() # Display the figure
# Run the script, if it breaks, print a usage statement
try:
main()
except IOError: # File error
print "ERROR - Unable to open DOSCAR file"
print main.__doc__
except: # Anything else
print "ERROR - Are you using readDOSCAR.py correctly?"
print main.__doc__