-
Notifications
You must be signed in to change notification settings - Fork 1
/
calculate.py
350 lines (269 loc) · 13.9 KB
/
calculate.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
"""
Runs over all paths to do electornic calculations (DOS, BS, WF) for each case.
Run after optimization. Copies CONTCAR to BS/DOS/WF files and sends job to queue.
To use, specify the n (index) and T (termination) of the MXene:
python3 calculate.py [-h] -n N_INDEX [-T TERMINATION]
OR input a specific path:
python3 calculate.py [-h] -p PATH
If both -n and -p falgs are used. The -p one has preference.
Diego Ontiveros
"""
import os
import shutil
from argparse import ArgumentParser
from VASPread import OUTCAR
from DOS import DOSCAR
from structure import CONTCAR
from searcher import getStructure
def calculateMXT(n:int,T:str,calcError=True):
"""Performs optimization for all cases of terminated MXenes (Mn+1XnTx).
Parameters
----------
`n` : MXene index from the formula Mn+1XnT2.
`T` : Termination with the index (e.g 'O2').
`calcError` : To recalculate job if OUTCAR presents error.
"""
# For Terminated cases
for mx,mxt in zip(MX,MXT):
for j,stack in enumerate(stacking):
for k,hollow in enumerate(hollows[j]):
# Go to the MXT path
path = f"{home}/M{n+1}X{n}/{mx}/{mxt}/{stack}/{hollow}/"
os.chdir(path)
dirs = ["DOS/","DOS/PBE0/","BS/PBE/","BS/PBE0/"]
aimsBS_dirs = ["aBS/PBE/","aBS/PBE0/"]
# extra_dirs = ["BS/PBE/BS2/","BS/PBE0/BS2/","WF/"]
# Do the calculation in each calculation directory (DOS, BS)
for dir in dirs:
# In case tha calculation is already done
if os.path.exists(path+dir+"vasp.out"):
# And in case there has been an Error
if os.path.exists(path+dir+"OUTCAR") and calcError:
outcar = OUTCAR(path+dir+"OUTCAR")
info = outcar.getOpt()
if info[-1] == "error":
os.chdir(dir)
start = dir.split("/")[0].lower()
os.system(rf"sed -i '/-pe smp/c\#$ -pe smp 6' script")
os.system(rf"sed -i '/--ntasks/c\#SBATCH --ntasks=24' script")
os.system(f"{queue} {start}{mxt}_{j}{k} script")
os.chdir(path)
continue
# Move the CONTCAR and send the job to queue
try: shutil.copy("CONTCAR",dir+"POSCAR")
except FileNotFoundError: print(f"Passing {mxt}_{stack}_{hollow}"); break
os.chdir(dir)
start = dir.split("/")[0].lower()
os.system(f"{queue} {start}{mxt}_{j}{k} script")
os.chdir(path)
for dir in aimsBS_dirs:
# In case tha calculation is already done
if os.path.exists(path+dir+"fhi-aims.out"):
# Check Error
continue
# Move the CONTCAR and send the job to queue
try:
contcar = CONTCAR("CONTCAR")
contcar.toAIMS(dir+"geometry.in")
except FileNotFoundError: print(f"Passing {mxt}_{stack}_{hollow}"); break
os.chdir(dir)
start = dir.split("/")[0].lower()
os.system(f"{queue} {start}{mxt}_{j}{k} script")
os.chdir(path)
def calculateMX(n:int,calcError=True):
"""
Performs optimization for all cases of pristine MXenes (Mn+1Xn).
`n` : MXene index from the formula Mn+1Xn.
`calcError` : To recalculate job if OUTCAR presents error.
"""
# For pristine cases
for mx in MX:
for j,stack in enumerate(stacking):
path = f"{home}/M{n+1}X{n}/{mx}/{stack}/"
os.chdir(path)
dirs = ["DOS/","DOS/PBE0/","BS/"]
for dir in dirs:
# In case tha calculation is already done
if os.path.exists(path+dir+"vasp.out"):
# And in case there has been an Error
if os.path.exists(path+dir+"OUTCAR") and calcError:
outcar = OUTCAR(path+dir+"OUTCAR")
info = outcar.getOpt()
if info[-1] == "error":
os.chdir(dir)
start = dir.split("/")[0].lower()
os.system(rf"sed -i '/-pe smp/c\#$ -pe smp 6' script")
os.system(rf"sed -i '/--ntasks/c\#SBATCH --ntasks=24' script")
os.system(f"{queue} {start}{mx}_{j} script")
os.chdir(path)
continue
try: shutil.copy("CONTCAR",dir+"POSCAR")
except FileNotFoundError: print(f"Passing {mx}_{stack}"); break
# Move the CONTCAR and send the job to queue
os.chdir(dir)
start = dir.split("/")[0].lower()
os.system(f"{queue} {start}{mx}_{j} script")
os.chdir(path)
def calculateGeneral(paths,calcError:bool=True):
"""
Performs optimizations for a given list of paths.
"""
for i,path in enumerate(paths):
os.chdir(path)
print(path)
if any(h in path for h in ["/H/", "/HM/","/HMX/","/HX/"]):
dirs = ["DOS/","DOS/PBE0/","BS/PBE/","BS/PBE0/"]
aimsBS_dirs = ["aBS/PBE/","aBS/PBE0/"]
else:
dirs = ["DOS/","DOS/PBE0/","BS/"]
aimsBS_dirs = []
stack,hollow,pristine, queue_name = getStructure(path)
mxt = queue_name.split('/')[0]
# Do the calculation in each calculation directory (DOS, BS)
for dir in dirs:
# In case tha calculation is already done
if os.path.exists(path+dir+"vasp.out"):
# And in case there has been an Error
if os.path.exists(path+dir+"OUTCAR") and calcError:
outcar = OUTCAR(path+dir+"OUTCAR")
info = outcar.getOpt()
if info[-1] == "error":
os.chdir(dir)
start = dir.split("/")[0].lower()
os.system(rf"sed -i '/-pe smp/c\#$ -pe smp 6' script")
os.system(rf"sed -i '/--ntasks/c\#SBATCH --ntasks=24' script")
os.system(f"{queue} {start}{queue_name} script")
os.chdir(path)
continue
# Move the CONTCAR and send the job to queue
try: shutil.copy("CONTCAR",dir+"POSCAR")
except FileNotFoundError: print(f"Passing {mxt}_{stack}_{hollow}"); break
os.chdir(dir)
start = dir.split("/")[0].lower()
os.system(f"{queue} {start}{queue_name} script")
os.chdir(path)
for dir in aimsBS_dirs:
# In case tha calculation is already done
if os.path.exists(path+dir+"fhi-aims.out"):
# Check Error
continue
# Move the CONTCAR and send the job to queue
try:
contcar = CONTCAR("CONTCAR")
contcar.toAIMS(dir+"geometry.in")
except FileNotFoundError: print(f"Passing {mxt}_{stack}_{hollow}"); break
os.chdir(dir)
start = dir.split("/")[0].lower()
os.system(f"{queue} {start}{queue_name} script")
os.chdir(path)
def calculateWF(n:int,T:str,limit=1.23,calcError=True):
"""Performs optimization for all cases of terminated MXenes (Mn+1XnTx).
Parameters
----------
`n` : MXene index from the formula Mn+1XnT2.
`T` : Termination with the index (e.g 'O2').
`calcError` : To recalculate job if OUTCAR presents error.
"""
# For Terminated cases
for mx,mxt in zip(MX,MXT):
for j,stack in enumerate(stacking):
for k,hollow in enumerate(hollows[j]):
# Go to the MXT path
path = f"{home}/M{n+1}X{n}/{mx}/{mxt}/{stack}/{hollow}/"
os.chdir(path)
dir = "WF/"
# see DOS0
dos = DOSCAR(path+"DOS/PBE0/DOSCAR",short=True)
Eg,VBM,CBM = dos.getBandgap()
if Eg >= limit:
# print(f"{mxt} {stack} {hollow} : {Eg} {VBM} Correct.")
print(path,Eg,VBM,CBM,flush=True)
else: continue
# In case tha calculation is already done
if os.path.exists(path+dir+"vasp.out"):
# And in case there has been an Error
if os.path.exists(path+dir+"OUTCAR") and calcError:
outcar = OUTCAR(path+dir+"OUTCAR")
info = outcar.getOpt()
if info[-1] == "error":
os.chdir(dir)
start = dir.split("/")[0].lower()
os.system(rf"sed -i '/-pe smp/c\#$ -pe smp 6' script")
os.system(rf"sed -i '/--ntasks/c\#SBATCH --ntasks=24' script")
os.system(f"{queue} {start}{mxt}_{j}{k} script")
os.chdir(path)
continue
# Move the CONTCAR and send the job to queue
try: shutil.copy("CONTCAR",dir+"POSCAR")
except FileNotFoundError: print(f"Passing {mxt}_{stack}_{hollow}"); break
os.chdir(dir)
start = dir.split("/")[0].lower()
# os.system(rf"sed -i '/-pe smp/c\#$ -pe smp 6' script")
# os.system(rf"sed -i '/-q iqtc/c\#$ -q iqtc06.q' script")
os.system(f"{queue} {start}{mxt}_{j}{k} script")
os.chdir(path)
def parse_file(file:str):
data = []
with open(file,"r") as inFile:
for line in inFile:
data.append(line.strip())
return data
############################ MAIN PROGRAM #####################
# Cluster PATHS
cluster_home = os.path.realpath(os.path.expanduser("~"))
if "gpfs/" in cluster_home or "/ub" in cluster_home: queue = "sbatch -J"
else: queue = "qsub -N"
home = os.path.abspath("..")
# User arguments parsing
parser = ArgumentParser(description="Runs over all paths to do electronic calculations (DOS, BS, WF) for each case.\
Run this scripts after optimization (opt.py). It copies CONTCAR to the BS/DOS/WF folders and sends job to queue.",
usage="\n To use, specify the n (index) and T (termination) of the MXene: \n python3 calculate.py [-h] -n N_INDEX [-T TERMINATION]\n\
OR input a specific path: \n python3 calculate.py [-h] -p PATH\n\
If both -n and -p falgs are used. The -p one has preference.\n\
Moreover, to do LOCPOT calculations for WF, use the -WF flag and optionally the -l flag, along the specified -n and -T:\n\
python3 calculate.py [-h] -n N_INDEX [-T TERMINATION] [-WF] [-l LIMITWF]")
parser.add_argument("-p","--path",type=str,default=None,help="Individual MXene structure folder where the calculation will be done. Optional. Has preference over -n.")
parser.add_argument("-f","--file",type=str,default=None,help="File with path names that will be calculated. Optional. Has preference over -n.")
parser.add_argument("-n","--n_index",type=int,help="MXene n index (int) from the formula Mn+1XnT2.")
parser.add_argument("-T","--termination",type=str,default="",help="MXene termination (str) from the formula Mn+1XnT2. Specifyit with the index, i.e 'O2'. \
For pristine MXenes, don't use this or use None. Defaults to None.")
parser.add_argument("-WF","--workfunction",action="store_true",help="To send LOCPOT calculations. Use the -l flag to send only for a specified bandgap limit.")
parser.add_argument("-l","--limitWF",type=float,default=1.23,help="The structures with bandgap > limit will be calculated. Defaults to 0.")
args = parser.parse_args()
paths, file, n, T = args.path, args.file, args.n_index, args.termination
calc_wf, limitWF = args.workfunction, args.limitWF
if calc_wf and n is None: parser.error("-WF requires -n flag and optinally -T.")
elif paths is None and n is None and file is None: parser.error(f"Some arguments are needed. For help, run python3 calculate.py -h.")
if not paths is None: calculateGeneral(paths)
if not file is None:
paths = parse_file(file)
calculateGeneral(paths)
else:
# n = 2 # MXene n number (thickness)
# T = "O2" # Termination
# MXene cases
M = ["Cr","Hf","Mo","Nb","Sc","Ta","Ti","V","W","Y","Zr"]
mc = [m + str(n+1) + "C" + str(n) for m in M] # X = C cases
mn = [m + str(n+1) + "N" + str(n) for m in M] # X = N cases
MX = mc + mn # All studied MXenes (pristine)
MXT = [i + T for i in MX if i != ""] # All studied MXenes (terminated)
# Structure cases
stacking = ["ABC","ABA"]
hABA = ["H","HMX","HX"]
hABC = ["HM","HMX","HX"]
hollows = [hABC,hABA]
if calc_wf:
calculateWF(n,T,limitWF)
else:
if T == "":
accept = input(f"Are you sure you want to calculate pristine MXenes with n = {n}? (Y/n): ")
if accept == "Y": calculateMX(n)
else: print("Closing...")
else:
accept = input(f"Are you sure you want to calculate terminated MXenes with n = {n} and T = {T}? (Y/n): ")
if accept == "Y": calculateMXT(n,T)
else: print("Closing...")
#! Changes:
# import general searcher that provides paths and use general calculator.
# get mx/stack/hollows from name path or function gives it
# try except if mx/stack/hollows not found