-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbsRedox.py
More file actions
576 lines (416 loc) · 19.1 KB
/
Copy pathAbsRedox.py
File metadata and controls
576 lines (416 loc) · 19.1 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
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
import os
#import numpy as np
import csv
from scm.plams import Settings, Molecule, Units, AMSJob, MultiJob, CRSJob, KFFile, ADFResults, JobRunner, GridRunner
import multiprocessing
wd = os.getcwd()
"""
Before running the script adjust the following:
1. The paths to the folder with starting geometries, a folder with
optimized and not optimized structures, and to the cosmo-rs solvent coskf file
2. The configurations - local and remote - for a parallel job running
3. Version of ADF (to be loaded on remote nodes)
4. At the bottom of the script calculation settings
"""
starting_geometries = os.path.join(wd,'start')
# Path to optimized and non-optimized structures
preopt = os.path.join(wd, 'Preoptimized')
non_preopt = os.path.join(wd, 'Not_Preoptimized')
GeoOpt = os.path.join(wd, "Optimized_Geo")
NotGeoOpt = os.path.join(wd, "Not_Optimized_Geo")
# Solvent path
database_path = os.path.join(wd,'ADFCRS', 'Dichloromethane.coskf')
# Configure parallel job running locally
maxjobs = multiprocessing.cpu_count()
jr = JobRunner(parallel=True, maxjobs=maxjobs)
# Configure parallel job running remote
gr = GridRunner(maxjobs=10)
# A version of ADF (to be loaded on remote nodes)
module_load = "module load 2021 \n module load AMS/2021.101-intelmpi"
# Settings for preoptimization of the molecules that haven't been
# previously optimized or optimized on FF level
################################################################################################################
############################################# Settings #########################################################
################################################################################################################
# Optimization on the DFTB level
def DFTB_optimization(model = ["DFTB3", 'DFTB.org/3ob-3-1'], nf_tolerance = -20, charge = None):
settings = Settings()
settings.input.ams.task = 'GeometryOptimization'
settings.input.ams.Properties.NormalModes = 'Yes'
settings.input.ams.Properties.PESPointCharacter = 'Yes'
settings.input.ams.NormalModes.ReScanFreqRange = '-1000 0'
settings.input.ams.PESPointCharacter.NegativeFrequenciesTolerance = nf_tolerance
settings.input.DFTB
settings.input.DFTB.Model = model[0]
if len(model)==2:
settings.input.DFTB.ResourcesDir = model[1]
if charge:
settings.input.ams.System.Charge = charge
settings.runscript.nproc = 1
return settings
# Single point calculation
def SinglePoint(xc_fun=['LibXC', 'CAM-B3LYP'], basis='DZP', charge=0, tddft=False, solvent=False):
settings = Settings()
settings.runscript.pre = module_load
settings.input.ams.task = 'SinglePoint'
settings.input.adf.basis.type = basis
settings.input.adf.basis.core = 'None'
settings.input.adf.xc[xc_fun[0]] = xc_fun[1]
settings.input.adf.xc.Dispersion = 'GRIMME3 BJDAMP'
settings.input.adf.Relativity.Level = 'None'
settings.input.adf.NumericalQuality = 'Normal'
settings.input.ams.UseSymmetry = 'No'
settings.input.adf.RIHartreeFock.UseMe = 'Yes'
settings.input.adf.RIHartreeFock.Quality = 'Normal'
if charge:
settings.input.adf.Unrestricted = 'Yes'
settings.input.adf.SpinPolarization = charge
settings.input.ams.System.Charge = charge
if tddft:
settings.input.adf.Excitations.lowest = 10
settings.input.adf.Excitations.OnlySing = ''
if tddft == 'Davidson':
settings.input.adf.Excitations[tddft]
else:
settings.input.adf.Excitations[tddft] = ''
if tddft == 'stddft':
settings.input.adf.ModifyExcitation.GrimmeAlpha = 0.9
settings.input.adf.ModifyExcitation.GrimmeBeta = 1.86
settings.input.adf.ModifyExcitation.GrimmeAex = 0.38
if solvent:
if solvent == 'CRS':
solvation_block = {
'surf': 'Delley',
'solv': 'name=CRS cav0=0.0 cav1=0.0',
'charged': 'method=Conj corr',
'c-mat': 'Exact',
'scf': 'Var All',
'radii': {
'H': 1.30,
'C': 2.00,
'N': 1.83,
'O': 1.72,
'F': 1.72,
'Si': 2.48,
'P': 2.13,
'S': 2.16,
'Cl': 2.05,
'Br': 2.16,
'I': 2.32
}
}
else:
solvation_block = solvent
settings.input.adf.solvation = solvation_block
return settings
# COSMO-RS calculation
def COSMORSsett(solute_path, solvent_path, num_compounds =2):
actcoef = Settings()
actcoef.input.property._h = 'ACTIVITYCOEF'
compounds = [Settings() for i in range(num_compounds)]
compounds[0]._h = solvent_path
compounds[1]._h = solute_path
#set compound mole fractions
compounds[0].frac1 = 1
compounds[1].frac1 = 0
actcoef.input.temperature = "298.15"
actcoef.input.compound = compounds
return actcoef
#################################################################################################################
####################################### PREOPTIMISATION ########################################################
#################################################################################################################
def make_csv(file_name, header, data):
"""
file_name : string
name of new csv file
header : list
list of strings
data : dic
a key is a string with a name of the molecule
value is a list of floats
"""
with open(file_name + '.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow(header)
for n in data:
x = [n] + data[n]
writer.writerow(x)
def export_coords(geometries, path):
"""
geometries: dictionary with PLAMS Molecules
The dictionary contains geometries to be placed in the corresponding folder
path : string
Path to the folder where geometries will be written
"""
try:
if not os.path.exists(path):
os.makedirs(path)
for name, mol in geometries.items():
mol.write(os.path.join(path, name + '.xyz' ))
except AttributeError:
print("Geometries for exporting not found")
def list_AMSjobs(job_sett, prefix, molecules):
"""
job_sett : PLAMS Settings
prefix : string
to be added to molecules name to indicate the type of job
molecules : dictionary
the key is the name of a molecule, value is PLAMS Molecule
"""
list_of_jobs = [ AMSJob(name = prefix + m,
molecule = molecules[m],
settings = job_sett)
for m in molecules
]
return list_of_jobs
def select_opt_mol(results, prefix, path):
"""
results : PLAMS Result object
prefix : string
to be added to molecules name to indicate the type of job
path : string
optimized molecules are exported to this path
"""
success_jobs = [job for job in results if job.ok()]
unsuccess_jobs = [job for job in results if job not in success_jobs]
opt_mol = {res.name.replace(prefix,'').replace('.002','') : res.get_main_molecule() for res in success_jobs}
export_coords(opt_mol, os.path.join(path))
return success_jobs, unsuccess_jobs
def parallel_preopt(settings, prefix, mols, maxjobs=False):
"""
maxjobs : integer
number of jobs allowed to run in parallel
if not specified, it will equal the number of CPU's
prefix : string
to be added to molecules name to indicate the type of job
mols : dictionary
the key is the name of a molecule, value is PLAMS Molecule
"""
if maxjobs:
pass
else:
maxjobs = multiprocessing.cpu_count()
jobs = list_AMSjobs(settings, prefix, mols)
results = [job.run(jr) for job in jobs]
return results
def optimization(mols, prefix, settings, path):
"""
Runs optimization with a semiempirical model of the initial structures.
Once the calculation is done it checks the status of the calculation,
extracts final geometries - successfully and unsuccessfully optimised
mols : dictionary
the key is the name of a molecule, value is PLAMS Molecule
prefix : string
to be added to molecules name to indicate the type of job
settings : PLAMS Settings
path : string
optimized molecules are exported to this path
"""
opt_mol, nonopt_mol = {}, {}
results = parallel_preopt(settings, prefix, mols)
opt_mol, fail = select_opt_mol(results, prefix, os.path.join(path))
if fail:
not_converged = [job for job in fail if job.grep_output(
'ERROR: Geometry optimization failed! (Converged, but did not find a minimum on the PES.)')]
not_converged_mols = {res.name : res.get_main_molecule() for res in not_converged}
if not_converged:
increase_accuracy = Settings()
increase_accuracy.input.ams.GeometryOptimization.Convergence.Gradients = 0.0001
increase_accuracy.input.ams.GeometryOptimization.Convergence.Energy = 1e-06
settings += increase_accuracy
results = parallel_preopt(settings, prefix, not_converged_mols)
opt_mol, fail = select_opt_mol(results, prefix, path)
if fail:
nonopt_mol = {res.name : res.get_main_molecule() for res in fail}
export_coords(nonopt_mol, non_preopt)
else:
nonopt_mol = {res.name.replace(prefix,'') : res.get_main_molecule() for res in fail}
export_coords(nonopt_mol, os.path.join(path, non_preopt))
###############################################################################################################
####################################### MAIN CALCULATIONS ####################################################
###############################################################################################################
# TDDFT calculations
##############################################################################################################
class TDDFT_jobs(MultiJob):
def postrun(self):
"""
Reads the computational details: the number of molecules, the time (available from AMS2021), and final
status of the calculation and writes it in csv file
Reads the values of excitation energies and oscillator strengths
and writes it in csv file
"""
data = {}
tddft_CD = {}
for job in self.children:
if job.ok():
try:
numA = job.results.readrkf('Molecule','nAtoms')
Etime = job.results.readrkf('General', 'ElapsedTime')
Tstatus = job.results.readrkf('General', 'termination status')
cd = [Tstatus] + [numA] + [Etime]
a = {job.name : cd}
tddft_CD.update(a)
except:
cd = []
a = {job.name : cd}
tddft_CD.update(a)
else:
a = {job.name : []}
tddft_CD.update(a)
if job.ok():
try:
ExcEnergies = Units.convert(job.results.readrkf('Excitations SS A', 'excenergies','adf'), 'au', 'eV')
OscillatorStr = job.results.readrkf('Excitations SS A', 'oscillator strengths','adf')
OutExc, OutOsc = [0]*10, [0]*10
OutExc[:len(ExcEnergies)] = ExcEnergies
OutOsc[:len(OscillatorStr)] = OscillatorStr
i = {job.name : OutExc + OutOsc}
data.update(i)
except:
i = {job.name : []}
data.update(i)
else:
i = {job.name : []}
data.update(i)
header = ['Name'] + list(range(1, 11)) + list(range(1, 11))
print (header, data)
make_csv(self.name, header, data)
header_cd = ['Name', 'TermStatus', 'NAtoms']#, 'Etime']
make_csv('td_comput_details', header_cd, tddft_CD)
def getExcitations(dirname, molecules, settings):
"""
Creates the TDDFT_jobs MultiJob of and runs them with prefered jobrunner
"""
jobs = [ AMSJob(name = m,
molecule = molecules[m],
settings = settings[s])
for m in molecules
for s in settings
]
ManyJobs = TDDFT_jobs(name=dirname, children=jobs)
ManyJobs.run(jobrunner=gr, nodes=1, cores=32, walltime='00-01:00:00')
#ManyJobs.run(jr)
# ADF with COSMO to create the starting surface for COSMO-RS
"""
# COSMO-RS
############################################################################################################
class MultipleCRS(MultiJob):
def prerun():
print('starting cosmo-rs calculations')
def calculateGibbsFree(dirname, solute, solvent, settings):
CRS_jobs = [CRSJob( settings = COSMORSsett(solute_path=v, solvent_path=solvent),
name = 'crs' + n)
for n,v in solute.items()
]
ManyCRSJobs = MultipleCRS(name=dirname, children=CRS_jobs)
#ManyJobs.settings.save = ['-', '$CH/adf.rkf']
ManyCRSJobs.run(jr)
"""
############################################################################################################
class ADFwithCOSMO(MultiJob):
def postrun(self):
data = {}
comp_data = {}
for job in self.children:
"""
Reads the details of the time (available from AMS2021) and the final status of the calculation
and writes it in csv file
Reads the COSMO section and creates a file with its copy
Makes the COSMO-RS job scripts and runs COSMO-RS calculations
Reads the value of solution-phase Gibbs free energy and writes it in csv file
"""
if job.ok():
try:
numA = job.results.readrkf('Molecule','nAtoms')
Etime = job.results.readrkf('General', 'ElapsedTime')
Tstatus = job.results.readrkf('General', 'termination status')
cd = [Tstatus] + [numA] + [Etime]
a = {job.name : cd}
comp_data.update(a)
except:
cd = []
a = {job.name : cd}
comp_data.update(a)
try:
# read COSMO section of adf.rkf file for a child job
resfile = KFFile(job.path + '/adf.rkf')
cosmo = resfile.read_section("COSMO")
# make a new file .coskf
coskf_path = os.path.join(job.path, job.name + '.coskf')
coskf = KFFile(coskf_path)
for k,v in cosmo.items():
coskf.write("COSMO",k,v)
crs_sett = COSMORSsett(solute_path=coskf_path, solvent_path=database_path)
crs_job = CRSJob(settings = crs_sett,
name = 'crs' + job.name)
crs_res = crs_job.run()
#extract data and update data dictionary
extracted = crs_res.get_results()
if extracted:
i = {job.name: [Units.convert( float(extracted["G solute"][1]), 'kcal/mol', 'eV')]}
data.update(i)
else:
i = {job.name : []}
data.update(i)
except:
i = {job.name : []}
data.update(i)
else:
i = {job.name : []}
data.update(i)
header = ['Name', 'G_'+self.name]
make_csv(self.name, header, data)
header_cd = ['Name', 'TermStatus', 'NAtoms' + 'Etime']
make_csv('comput_details', header_cd, comp_data)
def getGibbsFreeEnergy(dirname, molecules, settings):
"""
Creates the ADFwithCOSMO MultiJob of and runs them with preferred jobrunner
"""
jobs = [ AMSJob(name = m,
molecule = molecules[m],
settings = settings[s])
for m in molecules
for s in settings
]
ManyJobs = ADFwithCOSMO(name=dirname, children=jobs)
ManyJobs.run(jobrunner=gr, nodes=1, cores=32, walltime='00-01:00:00')
#ManyJobs.run(jr)
############################################################################################################
############################################# Run #################################################
############################################################################################################
# Preoptimisation
in_molecules = read_molecules(starting_geometries)
optimization(mols = in_molecules,
prefix = '',
settings = DFTB_optimization(model = ["DFTB3", 'DFTB.org/3ob-3-1']),
path = preopt
)
# Geometry optimisation
# GNV - Geometry optimization for Neutral molecule in Vacuum
# GOV - Geometry optimization for Oxidised molecule in Vacuum
GO = { 'gnv' : DFTB_optimization(model = ['GFN1-xTB']),
'gov' : DFTB_optimization(model = ['GFN1-xTB'], charge = 1)
}
molecules = read_molecules(preopt)
for job_name, settings in GO.items():
optimization(mols = molecules,
prefix = job_name,
settings = settings,
path = os.path.join(GeoOpt,job_name)
)
#Calculate Gibbs free energies
# get settings
neu = { 'ns' : SinglePoint(xc_fun=['GGA', 'BLYP'], basis='DZ', solvent='CRS') }
ox = { 'os' : SinglePoint(xc_fun=['GGA', 'BLYP'], basis='DZ', solvent='CRS', charge=1)}
# get the optimized structures for neutral and oxidised dyes
molecules_neu = read_molecules(os.path.join(GeoOpt,'gnv'))
molecules_ox = read_molecules(os.path.join(GeoOpt,'gov'))
#
available_structures = list(set(list(molecules_neu.keys())).intersection(list(molecules_ox.keys())))
molecules_neu = {mol: molecules_neu[mol] for mol in available_structures}
molecules_ox = {mol: molecules_ox[mol] for mol in available_structures}
oxi_data = getGibbsFreeEnergy(dirname='oxi', molecules=molecules_ox, settings = ox )
neutral_data = getGibbsFreeEnergy(dirname='neu', molecules=molecules_neu, settings = neu )
# Calculate excitations
stddft = { 'exc' : SinglePoint(tddft='stddft')}
excitations = getExcitations(dirname='exc', molecules=molecules_neu, settings=stddft)