-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdc_dft.py
296 lines (235 loc) · 8.58 KB
/
dc_dft.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
import yaml
from os import path,system
from analysis import BH76_analysis
from SP_CSV_to_YAML import conversion
eH_to_eV = 27.211386245988
eV_to_kcalmol = 23.06054783061903
eH_to_kcalmol = eH_to_eV*eV_to_kcalmol
#yaml.Dumper.ignore_aliases = lambda *args : True
conversion() # ensures FLOSIC errors are recomputed
no_flo_dat = ['LCwPBE','BLYP','B3LYP', 'M06-L','MN15-L']
rdir = path.dirname(path.realpath(__file__)) + '/'
refs = yaml.load(open(rdir + 'BH76_ref_energies.yaml','r'), Loader=yaml.Loader)
cmd = yaml.load(open(rdir + 'BH76_chg_2s.yaml','r'), Loader=yaml.Loader)
def str_rep_rules(instr):
rules = {
'r2SCAN': '\\rrscan{}',
'LCwPBE': '\\lcwpbe{}',
'S50X': 'SX-0.5'
}
outstr = instr
for arule in rules:
outstr = outstr.replace(arule,rules[arule])
return outstr
def get_wgts():
rpf = {}
rpb = {}
ts = {}
for ibh, abh in enumerate(refs):
for asys in refs[abh]['Stoich']:
if refs[abh]['Stoich'][asys] > 0:
wdct = ts
else:
if ibh%2 == 0:
wdct = rpf
else:
wdct = rpb
if asys in wdct:
wdct[asys] += 1
else:
wdct[asys] = 1
for wdct in [rpf, rpb]:
for akey in wdct:
wdct[akey] /= 38.
for akey in ts:
ts[akey] /= 76.
return ts, rpf, rpb
ts_d, rpf_d, rpb_d = get_wgts()
out_dir = './dc_dft_analysis/'
if not path.isdir(out_dir):
system('mkdir '+out_dir)
scf_dir = './results_aug-cc-pvqz/'
nrlmol_basis_dir = './results_NRLMOL_cart/'
refs = {
#'SCAN@HF': './results_aug-cc-pvqz/',
'LCwPBE': './results_aug-cc-pvqz/',
'S50X': './scan_hybs/S50X/',
'SCAN-FLOSIC': './FLOSIC/'
#'S25X': './scan_hybs/S25X/',
}
Nref = len(refs.keys())
refd = {}
dfas = ['LSDA','PBE','BLYP','SCAN','r2SCAN','M06-L','MN15-L','B3LYP','LCwPBE','LSDA@HF','PBE@HF','SCAN@HF','r2SCAN@HF']
metrics = ['MFE','MAFE','MDE','MADE']
for aref in refs:
if aref in ['S25X','S50X']:
frac = aref[1:-1]
tdir = refs[aref]+'SCAN_{:}_EXX_BH76/'.format(frac)
elif aref in ['SCAN@HF']:
tdir = refs[aref] + 'SCAN/' + aref +'_BH76/'
else:
tdir = refs[aref] + aref + '/' + aref +'_BH76/'
tfl = tdir + 'BH76_total.yaml'
if aref != 'SCAN-FLOSIC': # conversion() takes care of SCAN-FLOSIC
BH76_analysis(cdir=tdir)
refd[aref] = yaml.load(open(tfl,'r'), Loader=yaml.Loader)#['SP']
tstr = {'TS': '', 'RPF': '', 'RPB': '', 'BH': '', 'RC': ''}
for akey in tstr:
tstr[akey] += akey
for amet in metrics:
for aref in refs:
for akey in tstr:
tstr[akey] += ', {:}'.format(aref)
for akey in tstr:
tstr[akey] += '\n'
for dfa in dfas:
for akey in tstr:
tstr[akey] += '{:}'.format(dfa)
wd = {}
for amet in metrics:
wd[amet] = {}
for aref in refs:
wd[amet][aref] = {
'TS': 0.0, 'RPF': 0.0, 'RPB': 0.0, 'BH': 0.0, 'RC': 0.0
}
dsplit = dfa.split('@')
tdfa = dsplit[0]
if len(dsplit) > 1:
at_dfa = dsplit[1]
tdir = scf_dir+'{:}/{:}_BH76/'.format(tdfa,dfa)
tfl = tdir + 'BH76_total.yaml'
td_scf_default = BH76_analysis(cdir=tdir)
for aref in refs:
if dfa == aref:
continue
if (aref == 'SCAN-FLOSIC') and (dfa in no_flo_dat):
continue
if aref == 'SCAN-FLOSIC':
if '@' in dfa:
tdir_scf = nrlmol_basis_dir+'{:}/{:}_BH76/'.format(tdfa,dfa)
td_scf = BH76_analysis(cdir=tdir_scf)
else:
tdir_scf = './FLOSIC/{:}/{:}_BH76/'.format(dfa,dfa)
td_scf = yaml.load(open(tdir_scf + 'BH76_total.yaml','r'),\
Loader=yaml.Loader)
tdir_nscf = './FLOSIC/{:}/{:}@{:}_BH76/'.format(tdfa,tdfa,aref)
td_nscf = yaml.load(open(tdir_nscf+'BH76_total.yaml','r'), Loader=yaml.Loader)
else:
td_scf = td_scf_default.copy()
tref = aref
if aref == 'SCAN@HF':
tref = 'HF'
if aref in ['S25X','S50X']:
if '@' in dfa:
tdir = refs[aref]+'{:}@{:}_BH76/'.format(tdfa,tref)
else:
tdir = refs[aref]+'{:}@{:}_BH76/'.format(dfa,tref)
else:
if '@' in dfa:
tdir = refs[aref]+'{:}/{:}@{:}_BH76/'.format(tdfa,tdfa,tref)
else:
tdir = refs[aref]+'{:}/{:}@{:}_BH76/'.format(dfa,dfa,tref)
#print(aref,dfa,tdir)
tfl = tdir + 'BH76_total.yaml'
BH76_analysis(cdir=tdir)
td_nscf = yaml.load(open(tfl,'r'), Loader=yaml.Loader)
for idict,adict in enumerate([ts_d,rpf_d,rpb_d]):
if idict == 0:
didx = 'SP'
idx = 'TS'
elif idict == 1:
didx = 'SP'
idx = 'RPF'
elif idict == 2:
didx = 'SP'
idx = 'RPB'
for asys in adict:
tfe = (td_nscf[didx][asys] - refd[aref][didx][asys])*eH_to_kcalmol
tde = (td_scf[didx][asys] - td_nscf[didx][asys])*eH_to_kcalmol
wd['MFE'][aref][idx] += tfe*adict[asys]
wd['MAFE'][aref][idx] += abs(tfe)*adict[asys]
wd['MDE'][aref][idx] += tde*adict[asys]
wd['MADE'][aref][idx] += abs(tde)*adict[asys]
for idict in range(2):
if idict == 0:
didx = 'RX'
idx = 'BH'
elif idict == 1:
didx = 'RC'
idx = 'RC'
for asys in td_scf[didx]:
tfe = (td_nscf[didx][asys]['Energy'] - refd[aref][didx][asys]['Energy'])
tde = (td_scf[didx][asys]['Energy'] - td_nscf[didx][asys]['Energy'])
wd['MFE'][aref][idx] += tfe
wd['MAFE'][aref][idx] += abs(tfe)
wd['MDE'][aref][idx] += tde
wd['MADE'][aref][idx] += abs(tde)
for amet in metrics:
wd[amet][aref]['BH'] /= (1.0*len(td_scf['RX'].keys()))
wd[amet][aref]['RC'] /= (1.0*len(td_scf['RC'].keys()))
ofl = out_dir + dfa + '_BH76_dc_dft.yaml'
with open(ofl,'w+') as tfl:
yaml.dump(wd,tfl,Dumper=yaml.Dumper)
for amet in metrics:
for aref in refs:
for akey in tstr:
tstr[akey] += ', {:}'.format(wd[amet][aref][akey])
for akey in tstr:
tstr[akey] += '\n'
with open(out_dir+'dc_dft_analysis.csv','w+') as tfl:
for akey in tstr:
tfl.write(tstr[akey])
lbld = {'BH': 'BH76', 'RC': 'BH76RC', 'RPF': 'Reactants and products',
'RPB': 'Reactants and products',
'TS': 'Transition states'
}
for i in range(2):
if i == 0:
nstr = {'TS': '', 'RPF': '', 'RPB': ''}
tchar = 'SP'
elif i == 1:
nstr = {'BH': '', 'RC': ''}
tchar = 'delta_E'
for akey in nstr:
if akey == 'RPF':
nstr[akey] += '\\textit{Forwards}'
elif akey == 'RPB':
nstr[akey] += '\\textit{Reverse}'
for aref in refd:
if aref == 'LCwPBE':
aref_str = '\\lcwpbe{}'
else:
aref_str = aref
nstr[akey] += ' & \\multicolumn{2}{c}{'+'{:}'.format(aref_str) + '}'
nstr[akey] += ' \\\\ \n'
nstr[akey] += '\\textit{'+lbld[akey]+'}'
for aref in refd:
nstr[akey] += ' & MFE & MDE'
nstr[akey] += ' \\\\ \\hline \n'
for idfa,dfa in enumerate(dfas):
ofl = out_dir + dfa + '_BH76_dc_dft.yaml'
wd = yaml.load(open(ofl,'r'), Loader=yaml.Loader)
for akey in nstr:
if dfa == 'r2SCAN':
dfa_str = '\\rrscan{}'
elif dfa == 'LCwPBE':
dfa_str = '\\lcwpbe{}'
else:
dfa_str = dfa
nstr[akey] += dfa_str
for aref in refd:
for akey in nstr:
#pe_rat = 100*wd['MADE'][aref][akey]/max(1.e-15,wd['MAFE'][aref][akey])
if (dfa == aref) or \
( (aref == 'SCAN-FLOSIC') and (dfa in no_flo_dat)):
nstr[akey] += ' & & '
else:
nstr[akey] += ' & {:.2f} & {:.2f}'.format(wd['MFE'][aref][akey],wd['MDE'][aref][akey])
lchar = ''
if idfa == len(dfas)-1:
lchar = '\\hline'
for akey in nstr:
nstr[akey] += ' \\\\ '+lchar+' \n'
with open(out_dir+'mde_mfe_{:}.tex'.format(tchar),'w+') as tfl:
for akey in nstr:
tfl.write(str_rep_rules(nstr[akey]))