-
Notifications
You must be signed in to change notification settings - Fork 0
/
SDF2GauInput.py
286 lines (202 loc) · 5.51 KB
/
SDF2GauInput.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
#!/sw/bin/python3.6
import os, sys, math
import subprocess
from numpy import *
import AtomInfo, GetAdsorption
def Read_sdf(infilename):
ifile = open(infilename, 'r') #open file for reading
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 a 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
#############################################################
return Mol_atom, Mol_CartX, Mol_CartY, Mol_CartZ, TotalCharge, SpinMulti
def GauOnePoint_ForDFT(Functional, Basis, nproc, infilename):
Mol_atom, X, Y, Z, TotalCharge, SpinMulti = Read_sdf(infilename)
PreGauInput = infilename.split('.')
GauInputName = PreGauInput[0]+'.com'
line_rwf = '%rwf='+PreGauinput[0]
line_chk = '%chk='+PreGauInput[0]
line_method = '#'+Functional+'/'+Basis
line_comment = infilename
ofile = open(GauInputName ,'w')
if nproc > 1 :
line_proc = '%nproc='+str(nproc)
ofile.write(line_proc)
ofile.write('\n')
ofile.write(line_rwf)
ofile.write(line_chk)
ofile.write('\n')
ofile.write(line_method)
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')
ofile.close()
#To run Gaussian
subprocess.call(["g16", PreGauInput[0]])
def GauTDDFT_ForDFT(Functional, Basis, nproc, infilename):
Mol_atom, X, Y, Z, TotalCharge, SpinMulti = Read_sdf(infilename)
PreGauInput = infilename.split('.')
GauInputName = PreGauInput[0]+'.com'
line_chk = '%chk='+PreGauInput[0]
line_method = '#'+Functional+'/'+Basis
line_method1= 'Opt=(Maxcycle=100)'
line_method2= 'TD(Singlets, NState=20)'
line_method3= 'Geom=Check Guess=Read'
line_comment = infilename
ofile = open(GauInputName ,'w')
if nproc > 1 :
line_proc = '%nproc='+str(nproc)
ofile.write(line_proc)
ofile.write('\n')
ofile.write(line_chk)
ofile.write('\n')
ofile.write(line_method)
ofile.write('\n')
ofile.write(line_method1)
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')
ofile.write('--Link1--')
ofile.write('\n')
if nproc > 1 :
line_proc = '%nproc='+str(nproc)
ofile.write(line_proc)
ofile.write('\n')
ofile.write(line_chk)
ofile.write('\n')
ofile.write(line_method)
ofile.write('\n')
ofile.write(line_method2)
ofile.write('\n')
ofile.write(line_method3)
ofile.write('\n')
ofile.write('\n')
ofile.write(line_comment)
ofile.write('\n')
ofile.write('\n')
ofile.write('%5d %5d \n' % (TotalCharge, SpinMulti))
ofile.write('\n')
ofile.close()
#To run Gaussian
subprocess.call(["g16", PreGauInput[0]])
Gau_output = PreGauInput[0]+'.log'
Gau_chk = PreGauInput[0]+'.chk'
# waveLength = [400.0, 300, 250.0, 100.5]
WaveLength, V_OS = GetAdsorption.ExtractWaveOS_output(Gau_output)
subprocess.call(["rm", Gau_chk])
return WaveLength