-
Notifications
You must be signed in to change notification settings - Fork 0
/
GaussianRunPack.py
487 lines (361 loc) · 11.1 KB
/
GaussianRunPack.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
import os, sys, math
from numpy import *
import subprocess
import AtomInfo
class GaussianDFTRun:
def __init__(self, functional, basis, nproc, value, sdf_file, error):
self.sdf_file = sdf_file
self.functional = functional.lower()
self.basis = basis.lower()
self.nproc = nproc
self.value = value.lower()
self.error = error
self.decomposed_Energy = 0.0
def Extract_values(self, infilename,nmr,uv,energy,gap,dipole,deen):
ifile = open(infilename,'r') #open file for reading
lines = ifile.readlines()
ifile.close()
output = {}
if gap == 1:
AlphaEigenVal = []
BetaEigenVal = []
for line in lines:
if line.find(" basis functions, ") >=0:
line_StateInfo = line.split()
# #print (line_StateInfo[0])
NumBasisFunc = int(line_StateInfo[0])
if line.find(" alpha electrons ") >=0:
line_StateInfo = line.split()
# #print (line_StateInfo[0])
# #print (line_StateInfo[3])
NumAlphaElec = int(line_StateInfo[0])
NumBetaElec = int(line_StateInfo[3])
if line.find("Alpha occ. eigenvalues --") >=0:
if len(AlphaEigenVal) == NumBasisFunc:
AlphaEigenVal=[]
line_removed = line.replace("Alpha occ. eigenvalues --", " ")
line_StateInfo = line_removed.split()
for i in range(len(line_StateInfo)):
AlphaEigenVal.append(float(line_StateInfo[i]))
if line.find("Alpha virt. eigenvalues --") >=0:
line_removed = line.replace("Alpha virt. eigenvalues --", " ")
line_StateInfo = line_removed.split()
for i in range(len(line_StateInfo)):
AlphaEigenVal.append(float(line_StateInfo[i]))
if line.find("Beta occ. eigenvalues --") >=0:
if len(BetaEigenVal) == NumBasisFunc:
BetaEigenVal=[]
line_removed = line.replace("Beta occ. eigenvalues --", " ")
line_StateInfo = line_removed.split()
for i in range(len(line_StateInfo)):
BetaEigenVal.append(float(line_StateInfo[i]))
if line.find("Beta virt. eigenvalues --") >=0:
line_removed = line.replace("Beta virt. eigenvalues --", " ")
line_StateInfo = line_removed.split()
for i in range(len(line_StateInfo)):
BetaEigenVal.append(float(line_StateInfo[i]))
if BetaEigenVal == []:
Alpha_gap = 27.211*(AlphaEigenVal[NumAlphaElec]-AlphaEigenVal[NumAlphaElec-1])
output["gap"] = Alpha_gap
# return Alpha_gap
else:
Alpha_gap = 27.211*(AlphaEigenVal[NumAlphaElec]-AlphaEigenVal[NumAlphaElec-1])
Beta_gap = 27.211*(BetaEigenVal[NumBetaElec]-BetaEigenVal[NumBetaElec-1])
output["gap"] = [a.pha_gap, beta_gap]
# return Alpha_gap, Beta_gap
if dipole == 1:
Dipole_X = []
Dipole_Y = []
Dipole_Z = []
Dipole_Total = []
for line in lines:
if line.find(" X= ") >=0:
line_StateInfo = line.split()
#print (line_StateInfo[1])
Dipole_X.append(float(line_StateInfo[1]))
#print (line_StateInfo[3])
Dipole_Y.append(float(line_StateInfo[3]))
#print (line_StateInfo[5])
Dipole_Z.append(float(line_StateInfo[5]))
#print (line_StateInfo[7])
Dipole_Total.append(float(line_StateInfo[7]))
output["dipole"] = [Dipole_X[-1], Dipole_Y[-1], Dipole_Z[-1], Dipole_Total[-1]]
# return Dipole_X[-1], Dipole_Y[-1], Dipole_Z[-1], Dipole_Total[-1]
if energy == 1:
Energy = []
for line in lines:
if line.find("SCF Done: ") >=0:
line_StateInfo = line.split()
#print (line_StateInfo[4])
Energy.append(float(line_StateInfo[4]))
# return Energy
output["Energy"] = Energy[-1]
if uv == 1:
WaveLength = []
V_OS = []
for line in lines:
if line.find("Excited State ") >=0:
line_StateInfo = line.split()
#print (line_StateInfo[6])
WaveLength.append(float(line_StateInfo[6]))
OS_info = line_StateInfo[8].split('=')
#print(OS_info[1])
V_OS.append(float(OS_info[1]))
# return WaveLength, V_OS
output["uv"] = [WaveLength, V_OS]
if nmr == 1:
Element = []
ppm = []
for line in lines:
if line.find("Isotropic = ") >=0:
line_Info = line.split()
#print (line_Info[1])
Element.append(line_Info[1])
#print(line_Info[4])
ppm.append(float(line_Info[4]))
# return Element, ppm
output["nmr"] = [Element, ppm]
#print (output)
if deen == 1:
try:
Energy = Energy
except NameError:
Energy = []
for line in lines:
if line.find("SCF Done: ") >=0:
line_StateInfo = line.split()
#print (line_StateInfo[4])
Energy.append(float(line_StateInfo[4]))
# return deen
output["deen"] = Energy[-1] - (self.decomposed_Energy)
return output
def run_gaussian(self):
infilename = self.sdf_file
option_line = self.value
options = option_line.split()
opt = 0
nmr = 0
uv = 0
energy = 0
gap = 0
dipole = 0
deen = 0
for i in range(len(options)):
# #print(options[i])
option = options[i]
if option == 'opt':
opt = 1
#print ('opt')
elif option == 'nmr':
nmr = 1
#print ('nmr')
elif option == 'uv':
uv = 1
#print ('uv')
elif option == 'energy':
energy = 1
#print ('energy')
elif option == 'homolumo':
gap = 1
#print ('HOMO/LUMO')
elif option == 'dipole':
dipole = 1
#print ('dipole')
elif option == 'deen':
deen = 1
#print ('Decomposition energy')
else:
print('invalid option: ', option)
Mol_atom, X, Y, Z, TotalCharge, SpinMulti = self.read_sdf()
PreGauInput = infilename.split('.')
GauInputName = PreGauInput[0]+'.com'
line_chk = '%chk='+PreGauInput[0]
line_method = '#'+self.functional+'/'+self.basis
line_comment = infilename
ofile = open(GauInputName ,'w')
if self.nproc > 1 :
line_proc = '%nproc='+str(self.nproc)
ofile.write(line_proc)
ofile.write('\n')
ofile.write(line_chk)
ofile.write('\n')
ofile.write(line_method)
ofile.write('\n')
if opt == 1:
ofile.write('Opt')
ofile.write('\n')
ofile.write('\n')
ofile.write(line_comment)
ofile.write('\n')
ofile.write('\n')
ofile.write('%5d %5d \n' % (TotalCharge, SpinMulti))
for j in range(len(Mol_atom)):
ofile.write('%-4s % 10.5f % 10.5f % 10.5f \n'
% (Mol_atom[j],X[j], Y[j], Z[j]))
ofile.write('\n')
if nmr == 1:
line_readMOGeom = 'Geom=AllCheck Guess=Read'
ofile.write('--Link1--')
ofile.write('\n')
if self.nproc > 1 :
line_proc = '%nproc='+str(self.nproc)
ofile.write(line_proc)
ofile.write('\n')
ofile.write(line_chk)
ofile.write('\n')
ofile.write(line_method)
ofile.write('\n')
line_method_nmr = 'NMR'
ofile.write(line_method_nmr)
ofile.write('\n')
ofile.write(line_readMOGeom)
ofile.write('\n')
ofile.write('\n')
if uv == 1:
line_readMOGeom = 'Geom=AllCheck Guess=Read'
ofile.write('--Link1--')
ofile.write('\n')
if self.nproc > 1 :
line_proc = '%nproc='+str(self.nproc)
ofile.write(line_proc)
ofile.write('\n')
ofile.write(line_chk)
ofile.write('\n')
ofile.write(line_method)
ofile.write('\n')
line_method_TD = 'TD(Nstate=20, Singlet)'
ofile.write(line_method_TD)
ofile.write('\n')
ofile.write(line_readMOGeom)
ofile.write('\n')
ofile.write('\n')
ofile.write('\n')
ofile.close()
############Run Gaussian##############################
#subprocess.call(["mkdir", PreGauInput[0]])
#subprocess.call(["mv", GauInputName,PreGauInput[0]])
#os.chdir(PreGauInput[0])
tmp_test=open(GauInputName,'r')
#tmp_test.close()
#route = tmp_test.readline()
#print (route)
subprocess.call(["g16", PreGauInput[0]])
logfile = PreGauInput[0]+'.log'
output_dic = self.Extract_values(logfile,nmr,uv,energy,gap,dipole,deen)
#os.chdir("..")
tmp_test.close()
return(output_dic)
#####################To get cartesian coordinates from sdf file###############
def read_sdf(self):
ifile = open(self.sdf_file, 'r')
count = 0
X = []
Y = []
Z = []
element_symbol = []
Bond_pair1 = []
Bond_pair2 = []
Bond_type = []
TotalCharge = 0
CHG_atom = []
CHG = []
for line in ifile:
if count == 0:
Header1 = line
count += 1
continue
if count == 1:
Header2 = line
count += 1
continue
if count == 2:
Header3 = line
count += 1
continue
if count == 3:
a = line.split()
N = int(a[0])
N_Bond = int(a[1])
count += 1
continue
if 3 < count <= N+4:
i_atom = line.split()
if len(i_atom) != 0:
X.append(float(i_atom[0]))
Y.append(float(i_atom[1]))
Z.append(float(i_atom[2]))
element_symbol.append(i_atom[3])
count += 1
continue
if N+4 < count <= N+N_Bond+3 :
bond_info = line.split()
#print (bond_info)
bond_info = line.split()
Bond_pair1.append(int(bond_info[0]))
Bond_pair2.append(int(bond_info[1]))
Bond_type.append(int(bond_info[2]))
count +=1
continue
if count > N+N_Bond+3:
mol_info = line.split()
#print (mol_info)
if (mol_info[0] == "M"):
if (mol_info[1] == "END"):
break
if (mol_info[1] == "CHG"):
Num_CHGInfo = int(mol_info[2])
for k in range(Num_CHGInfo):
CHG_atom.append(int(mol_info[3+2*k]))
CHG.append(int(mol_info[4+2*k]))
TotalCharge += int(mol_info[4+2*k])
else:
print("The sdf file is invalid!")
sys.exit()
count +=1
#Copy to array of numpy###########################
Mol_atom = []
Mol_CartX = zeros(N)
Mol_CartY = zeros(N)
Mol_CartZ = zeros(N)
CHG_atom = array(CHG_atom)
CHG = array(CHG)
for j in range(N):
Mol_CartX[j] = X[j]
Mol_CartY[j] = Y[j]
Mol_CartZ[j] = Z[j]
Mol_atom.append(element_symbol[j])
#del element_symbol[N:TotalStep]
del element_symbol[:]
del X[:]
del Y[:]
del Z[:]
##################################################
#print (Mol_atom)
#print (N)
#print (len(Mol_CartX))
#print('Reading the sdf file has finished')
###Calculating the total number of electrons#################
TotalNum_electron = 0
for j in range(N):
if (len(CHG_atom) != 0):
Judge = CHG_atom-j
if (any(Judge) == 0):
TotalNum_electron += AtomInfo.AtomicNumElec(Mol_atom[j])-CHG[where(Judge == 0)]
else:
TotalNum_electron += AtomInfo.AtomicNumElec(Mol_atom[j])
else:
TotalNum_electron += AtomInfo.AtomicNumElec(Mol_atom[j])
print('Total number of electron: %7d ' % (TotalNum_electron))
if (TotalNum_electron%2==0):
print ("This system is a closed shell!")
SpinMulti = 1
else:
print ("This system is a open shell!")
SpinMulti = 2
#######Calculating Decomposed atoms total energy#######################
for i in range(N):
#print (Mol_atom[i], AtomInfo.One_Atom_Energy(Mol_atom[i], self.functional, self.basis))
self.decomposed_Energy += AtomInfo.One_Atom_Energy(Mol_atom[i], self.functional, self.basis)
print("Decomposed energy: ", self.decomposed_Energy)
#############################################################
return Mol_atom, Mol_CartX, Mol_CartY, Mol_CartZ, TotalCharge, SpinMulti