-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecutor_UJ.py
More file actions
292 lines (227 loc) · 10.5 KB
/
Copy pathExecutor_UJ.py
File metadata and controls
292 lines (227 loc) · 10.5 KB
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
#!/usr/bin/env python
# Version 0.2
# Developers:
# -Aldo Romero : alromero@mail.wvu.edu
# -Guillermo Avendano-Franco : gufranco@mail.wvu.edu
# -Pedram Tavadze : petavazohi@mix.wvu.edu
import os
import argparse
import json
import time
import subprocess
import pychemia
import re
def analyze_output(code,params):
if code == 'vasp':
if os.path.isfile('results.json'):
rf=open('results.json')
full_json=json.load(rf)
rf.close()
else:
full_json={}
rf = open("OUTCAR","r")
outcar_data = rf.read()
rf.close()
# To store results in JSON format
jret={}
U = params['U']
J = params['J']
name="U%.3f_J%.3f" % (U,J)
if name in full_json:
wf=open('single.json','w')
json.dump(full_json[name], wf, sort_keys=True, indent=4, separators=(',', ': '))
wf.close()
else:
structure = pychemia.code.vasp.read_poscar("POSCAR.inp")
positions_before = structure.positions.tolist()
structure = pychemia.code.vasp.read_poscar("CONTCAR")
a = structure.lattice.a
b = structure.lattice.b
c = structure.lattice.c
volume = structure.volume
natom = structure.natom
species = structure.species
positions_after = structure.positions.tolist()
total_energy = float(re.findall("energy\swithout\sentropy.*",outcar_data)[-1].split("=")[1].split()[0])
inner_pressure_kbar = float(re.findall("external\spressure.*",outcar_data)[-1].split()[3])
e_fermi = float(re.findall("E-fermi.*",outcar_data)[-1].split()[2])
magnetic_moments = [map(float,x.split()[1:]) for x in re.findall("magnetization \(x\)[0-9a-z.\#\s\t\n-]*\ntot",outcar_data)[-1].split('\n')[4:4+natom]]
# finding the band gap from EIGENVAL
inputFile_EIGENVAL = open('EIGENVAL', 'r')
# next f lines do not have anything interesting
for i in range(5):
inputFile_EIGENVAL.readline()
line = inputFile_EIGENVAL.readline()
nelectrons = int(line.split()[0])
nkpt = int(line.split()[1])
neigen_per_kpt = int(line.split()[2])
eigenup = []
eigendown = []
for i in range(nkpt):
eigenup.append([])
eigendown.append([])
inputFile_EIGENVAL.readline() # skips line before data
inputFile_EIGENVAL.readline() # this has kpoint and float weight
for j in range(neigen_per_kpt):
eigenvalue = map(float,inputFile_EIGENVAL.readline().split()[1:3])
eigenup[-1].append(eigenvalue[0])
eigendown[-1].append(eigenvalue[1])
conduc_up = 100.0
conduc_down = 100.0
valen_up = -100.0
valen_down = -100.0
for i in range(nkpt):
for eigenvalue in eigenup[i]:
if ((eigenvalue-e_fermi)<0.0):
valen_up = max(valen_up,eigenvalue-e_fermi)
else:
conduc_up = min(conduc_up,eigenvalue-e_fermi)
for eigenvalue in eigendown[i]:
if ((eigenvalue-e_fermi)<0.0):
valen_down = max(valen_down,eigenvalue-e_fermi)
else:
conduc_down = min(conduc_down,eigenvalue-e_fermi)
inputFile_EIGENVAL.close()
jret['U'] = U
jret['J'] = J
jret['natom'] = natom
jret['species'] = species
jret['positions_before'] = positions_before
jret['positions_after'] = positions_after
jret['A_cell'] = a
jret['B_cell'] = b
jret['C_cell'] = c
jret['volume'] = volume
jret['total_energy'] = total_energy
jret['inner_pressure_kbar'] = inner_pressure_kbar
jret['e_fermi'] = e_fermi
jret['magnetic_moments'] = magnetic_moments
jret['electrons'] = nelectrons
jret['nkpt'] = nkpt
jret['neigenvalues_per_kpt'] = neigen_per_kpt
jret['valence_fermi_spin_down'] = valen_down
jret['conduction_fermi_spin_down'] = conduc_down
jret['gap_spin_down'] = conduc_down-valen_down
jret['valence_fermi_spin_up'] = valen_up
jret['conduction_fermi_spin_up'] = conduc_up
jret['gap_spin_up'] = conduc_up-valen_up
wf=open('single.json','w')
json.dump(jret, wf, sort_keys=True, indent=4, separators=(',', ': '))
wf.close()
wf=open('results.json','w')
full_json[name]=jret
json.dump(full_json, wf, sort_keys=True, indent=4, separators=(',', ': '))
wf.close()
else :
raise ValueError("Not implemented for %s" % code)
return full_json
def set_optimal_energy_cutoff(code, factor=1.4):
if code == 'vasp':
vi=pychemia.code.vasp.VaspInput('INCAR.inp')
vi.set_encut(factor,POTCAR='POTCAR')
vi.write('INCAR')
else:
raise ValueError("Not implemented for %s" % code)
def set_inputfile(code, params, calculation_type):
if code == 'vasp':
if calculation_type == "relaxation":
# POSCAR Change
st = pychemia.code.vasp.read_poscar('POSCAR.inp')
pychemia.code.vasp.write_poscar(st,'POSCAR')
# INCAR Change
vi=pychemia.code.vasp.VaspInput('INCAR.inp')
nspecies=st.nspecies
arr=nspecies*[0]
arr[0] = params['U']
vi['LDAUU']=arr
arr=nspecies*[0]
arr[0] = params['J']
vi['LDAUJ'] = arr
vi['ISIF'] = 7
vi['NSW'] = 100
vi['POTIM'] = 0.5
vi['IBRION'] = 2
vi.write('INCAR')
elif calculation_type == "ground_state":
# POSCAR Change
st=pychemia.code.vasp.read_poscar('CONTCAR')
pychemia.code.vasp.write_poscar(st,'POSCAR')
# INCAR Change
vi=pychemia.code.vasp.VaspInput('INCAR.inp')
nspecies=st.nspecies
arr=nspecies*[0]
arr[0] = params['U']
vi['LDAUU']=arr
arr=nspecies*[0]
arr[0] = params['J']
vi['LDAUJ']=arr
vi['ISYM'] = -1
vi.write('INCAR')
else:
raise ValueError("Not implemented for %s" % code)
return
def execute(code, nparal):
wf=open('RUNNING','w')
wf.write(time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()))
wf.close()
if code == 'vasp':
start_time=time.time()
status = subprocess.call("mpirun -np %d vasp_std "% nparal, shell=True)
end_time=time.time()
runtime=end_time-start_time
if status== 0:
print("VASP execution completed with returcode: %d runtime: %d secs" % (status, runtime))
else:
print("VASP execution failed with returcode: %d runtime: %d secs" % (status, runtime))
os.remove('RUNNING')
else:
raise ValueError("Not implemented for %s" % code)
return runtime
if __name__ == "__main__":
description = ("Script_Run_JU.py: a python script to run VASP for a single or several J,U and with a possible scale change of volume (only works for single J,U).")
parser = argparse.ArgumentParser(description=description)
parser.add_argument('-np', dest='np', type=int, action='store', help='Number of MPI processes for the code')
parser.add_argument('-code', dest='code', type=str, action='store', help='Code to use', default='vasp')
parser.add_argument('-walltime', dest='walltime', type=int, action='store', help='Walltime in minutes')
args=parser.parse_args()
code = args.code
walltime = args.walltime
set_optimal_energy_cutoff(code, factor=1.4)
jobstart=time.time()
jobend=jobstart+walltime*60
runtime=0
if os.path.isfile('results.json'):
rf=open('results.json')
full_json=json.load(rf)
rf.close()
else:
full_json={}
while True:
curtime=time.time()
if runtime > jobend-curtime:
print("Not enough time for one run")
break
if not os.path.isfile('input.json'):
print("Not input.json found, waiting 60 seconds. Time before wall: %d min" % int((jobend-time.time())/60))
time.sleep(60)
continue
rf=open('input.json')
data=json.load(rf)
rf.close()
print(data)
if data['kind'] == 'singleUJ':
U=float(data['U'])
J=float(data['J'])
name="U%.3f_J%.3f" % (U, J)
if not name in full_json:
set_inputfile(code, params={'U':U, 'J':J}, calculation_type = 'relaxation')
runtime = execute(code, args.np)
# set_inputfile(code, params={'U':U, 'J':J}, calculation_type = 'ground_state')
# runtime = execute(code, args.np)
else:
print("Already calculated for U: %f J: %f " % (U,J))
full_json=analyze_output(code,params={'U':U, 'J':J})
wf=open('COMPLETE','w')
wf.write(time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()))
wf.close()
os.remove('input.json')