-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdos_all.py
273 lines (266 loc) · 10.7 KB
/
pdos_all.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
#!/usr/bin/python3
import os
import pymatgen
from pymatgen.core.periodic_table import get_el_sp
from sssp import pseudo_dict, ecutwfc_dict, ecutrho_dict, atomwfc_dict, band_dict
import subprocess
import numpy
import sys
def clean(prefix):
subprocess.call("rm -rf %s.save %s.dos %s.xml %s.wfc* %s.mix*"
% (prefix, prefix, prefix, prefix, prefix), shell=True)
def main():
#
args = sys.argv
with open(str(args[1]), "r") as f:
input_list = f.readlines()
n_proc = int(args[2])
#
# Read previous result
#
for input_file in input_list:
#
input_file = input_file.strip("\n")
prefix = input_file.split("/")[-1].split(".")[0]
#
structure = pymatgen.core.Structure.from_file(input_file)
avec = structure.lattice.matrix
nat = structure.num_sites
atom = [str(get_el_sp(iat)) for iat in structure.atomic_numbers]
typ = sorted(set(atom))
ntyp = structure.ntypesp
if nat > 100:
print("Too many atoms in ", prefix)
continue
#
# WFC and Rho cutoff
#
ecutwfc = 0.0
ecutrho = 0.0
unsupported_element = False
for ityp in typ:
if str(ityp) in ecutwfc_dict:
if ecutwfc < ecutwfc_dict[str(ityp)]:
ecutwfc = ecutwfc_dict[str(ityp)]
if ecutrho < ecutrho_dict[str(ityp)]:
ecutrho = ecutrho_dict[str(ityp)]
else:
unsupported_element = True
print("Unsupported element in ", prefix)
break
if unsupported_element:
continue
#
# k and q grid
# the number of grid proportional to the Height of b
# b_i * a_i / |a_i| = 2pi / |a_i| (a_i is perpendicular to other b's)
#
nk = numpy.zeros(3, numpy.int_)
for ii in range(3):
norm = numpy.sqrt(numpy.dot(avec[ii][:], avec[ii][:]))
nk[ii] = round(2.0 * numpy.pi / norm / 0.15)
if nk[ii] == 0:
nk[ii] = 1
#
scf_input = "scf_" + prefix + ".in"
scf_output = "scf_" + prefix + ".out"
nscf_input = "nscf_" + prefix + ".in"
nscf_output = "nscf_" + prefix + ".out"
pdos_input = "pdos_" + prefix + ".in"
pdos_output = "pdos_" + prefix + ".out"
#
# Number of valence band
#
nbnd = 0
for iat in atom:
nbnd += band_dict[iat] + 1
#
# SCF file
#
with open(scf_input, 'w') as f:
print("&CONTROL", file=f)
print(" calculation = \'scf\'", file=f)
print(" prefix = \'%s\'" % prefix, file=f)
print("/", file=f)
print("&SYSTEM", file=f)
print(" ibrav = 0", file=f)
print(" nat = %d" % nat, file=f)
print(" ntyp = %d" % ntyp, file=f)
print(" ecutwfc = %f" % ecutwfc, file=f)
print(" ecutrho = %f" % ecutrho, file=f)
print(" occupations = \'tetrahedra_opt\'", file=f)
print(" nspin = 2", file=f)
print(" nbnd = %d" % nbnd, file=f)
for ityp in range(ntyp):
print(" starting_magnetization(%d) = 1.0" % (ityp + 1), file=f)
print("/", file=f)
print("&ELECTRONS", file=f)
print(" diagonalization = \"rmm-davidson\"", file=f)
print(" mixing_beta = 0.1", file=f)
print(" conv_thr = %e" % (float(nat)*1.0e-7), file=f)
print("/", file=f)
print("CELL_PARAMETERS angstrom", file=f)
for ii in range(3):
print(" %25.15e %25.15e %25.15e" % (avec[ii, 0], avec[ii, 1], avec[ii, 2]), file=f)
print("ATOMIC_SPECIES", file=f)
for ityp in typ:
print(" %s %f %s" % (ityp, pymatgen.core.Element(ityp).atomic_mass, pseudo_dict[str(ityp)]), file=f)
print("ATOMIC_POSITIONS crystal", file=f)
for iat in range(nat):
print(" %s %25.15e %25.15e %25.15e" % (
atom[iat],
structure.frac_coords[iat][0], structure.frac_coords[iat][1], structure.frac_coords[iat][2]),
file=f)
print("K_POINTS automatic", file=f)
print(" %d %d %d 0 0 0" % (nk[0], nk[1], nk[2]), file=f)
#
# Run DFT (SCF)
#
try:
subprocess.check_call("mpiexec -n %d -of %s ~/bin/pw.x -nk %d -in %s"
% (n_proc, scf_output, n_proc, scf_input), shell=True)
except subprocess.CalledProcessError:
print("SCF error in ", prefix)
clean(prefix)
continue
#
# Un-converged case
#
try:
subprocess.check_call("grep \"convergence has been achieved in\" %s"
% scf_output, shell=True)
except subprocess.CalledProcessError:
print("SCF did not converge in ", prefix)
clean(prefix)
continue
#
# Non-SCF file
#
with open(nscf_input, 'w') as f:
print("&CONTROL", file=f)
print(" calculation = \'nscf\'", file=f)
print(" prefix = \'%s\'" % prefix, file=f)
print("/", file=f)
print("&SYSTEM", file=f)
print(" ibrav = 0", file=f)
print(" nat = %d" % nat, file=f)
print(" ntyp = %d" % ntyp, file=f)
print(" ecutwfc = %f" % ecutwfc, file=f)
print(" ecutrho = %f" % ecutrho, file=f)
print(" occupations = \'tetrahedra_opt\'", file=f)
print(" nspin = 2", file=f)
print(" nbnd = %d" % nbnd, file=f)
for ityp in range(ntyp):
print(" starting_magnetization(%d) = 1.0" % (ityp + 1), file=f)
print("/", file=f)
print("&ELECTRONS", file=f)
print(" diagonalization = \"ppcg\"", file=f)
print("/", file=f)
print("CELL_PARAMETERS angstrom", file=f)
for ii in range(3):
print(" %25.15e %25.15e %25.15e" % (avec[ii, 0], avec[ii, 1], avec[ii, 2]), file=f)
print("ATOMIC_SPECIES", file=f)
for ityp in typ:
print(" %s %f %s" % (ityp, pymatgen.core.Element(ityp).atomic_mass, pseudo_dict[str(ityp)]), file=f)
print("ATOMIC_POSITIONS crystal", file=f)
for iat in range(nat):
print(" %s %25.15e %25.15e %25.15e" % (
atom[iat],
structure.frac_coords[iat][0], structure.frac_coords[iat][1], structure.frac_coords[iat][2]),
file=f)
print("K_POINTS automatic", file=f)
print(" %d %d %d 0 0 0" % (nk[0]*2, nk[1]*2, nk[2]*2), file=f)
#
# Run DFT (non-SCF)
#
try:
subprocess.check_call("mpiexec -n %d -of %s ~/bin/pw.x -nk %d -in %s"
% (n_proc, nscf_output, n_proc, nscf_input), shell=True)
except subprocess.CalledProcessError:
print("Non-SCF error in ", prefix)
clean(prefix)
continue
#
# Fermi energy
#
efermi = None
with open(nscf_output, "r") as f:
output_lines = f.readlines()
for output_line in output_lines:
output_words = output_line.split("the Fermi energy is")
if len(output_words) > 1:
efermi = float(output_words[1].split()[0])
#
# proj.in : Read by projwfc.x
#
with open(pdos_input, 'w') as f:
print("&PROJWFC", file=f)
print(" prefix = \'%s\'" % prefix, file=f)
print(" emin = %f" % efermi, file=f)
print(" emax = %f" % efermi, file=f)
print(" deltae = 0.1", file=f)
print("/", file=f)
if efermi is None:
print("efermi error in ", prefix)
clean(prefix)
continue
#
# Run PDOS
#
try:
subprocess.check_call("mpiexec -n %d -of %s ~/bin/projwfc.x -nk %d -in %s"
% (n_proc, pdos_output, n_proc, pdos_input), shell=True)
except subprocess.CalledProcessError:
print("PDOS error in ", prefix)
clean(prefix)
continue
#
# Sum PDOS at each Atom and L
#
for ityp in typ:
nwfc = 1
for il in atomwfc_dict[ityp]:
try:
subprocess.check_call("mpiexec -n 1 -of %s.pdos_%s%s ~/bin/sumpdos.x %s.pdos_atm*\\(%s\\)_wfc#%d*"
% (prefix, ityp, il[0], prefix, ityp, nwfc), shell=True)
except subprocess.CalledProcessError:
print("Sum-PDOS error in ", prefix)
continue
nwfc += 1
#
# Atomwfc dictionary for fermi_proj.x
#
pfermi = {ityp: {il: [] for il in atomwfc_dict[ityp][:, 0]} for ityp in typ}
ii = 0
for iat in atom:
for il in atomwfc_dict[iat]:
for im in range(int(il[1])):
ii += 1
pfermi[iat][il[0]].append(ii)
#
# Fermi surface with atomic projection
#
for ityp in typ:
for il in atomwfc_dict[ityp]:
with open("fermi_proj.in", 'w') as f:
print("&PROJWFC", file=f)
print(" prefix = \'%s\'" % prefix, file=f)
print("/", file=f)
print(str(len(pfermi[ityp][il[0]])), file=f)
for ii in pfermi[ityp][il[0]]:
print(" %d" % ii, end="", file=f)
try:
subprocess.check_call("mpiexec -n 1 ~/bin/fermi_proj.x -in fermi_proj.in", shell=True)
except subprocess.CalledProcessError:
print("fermi_proj error in ", prefix)
continue
os.rename("./proj1.frmsf",
"./" + prefix + "_" + ityp + il[0] + "_1.frmsf")
os.rename("./proj2.frmsf",
"./" + prefix + "_" + ityp + il[0] + "_2.frmsf")
# os.rename("./" + prefix + "_proj1.frmsf",
# "./" + prefix + "_" + ityp + il[0] + "_1.frmsf")
# os.rename("./" + prefix + "_proj2.frmsf",
# "./" + prefix + "_" + ityp + il[0] + "_2.frmsf")
clean(prefix)
main()