-
Notifications
You must be signed in to change notification settings - Fork 3
/
replicawork.py
576 lines (512 loc) · 21 KB
/
replicawork.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
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
571
572
573
574
575
576
from aiida.orm.data.structure import StructureData
from aiida.orm.data.parameter import ParameterData
from aiida.orm.data.base import Int, Str, Float
from aiida.orm.data.singlefile import SinglefileData
from aiida.orm.data.remote import RemoteData
from aiida.orm.code import Code
from aiida.work.workchain import WorkChain, ToContext, Calc, while_
from aiida.work.run import submit
from aiida_cp2k.calculations import Cp2kCalculation
import tempfile
import shutil
import numpy as np
class ReplicaWorkchain(WorkChain):
@classmethod
def define(cls, spec):
super(ReplicaWorkchain, cls).define(spec)
spec.input("cp2k_code", valid_type=Code)
spec.input("structure", valid_type=StructureData)
spec.input("num_machines", valid_type=Int, default=Int(54))
spec.input("replica_name", valid_type=Str)
spec.input("cell", valid_type=Str, default=Str(''))
spec.input("fixed_atoms", valid_type=Str, default=Str(''))
spec.input("colvar_targets", valid_type=Str)
spec.input("target_unit", valid_type=Str)
spec.input("spring", valid_type=Float, default=Float(75.0))
spec.input("spring_unit", valid_type=Str)
spec.input("subsys_colvar", valid_type=ParameterData)
spec.input("calc_type", valid_type=Str)
spec.outline(
cls.init,
while_(cls.next_replica)(
cls.generate_replica,
while_(cls.not_converged)(
cls.generate_replica
),
cls.store_replica
)
)
spec.dynamic_output()
# ==========================================================================
def not_converged(self):
try:
self.ctx.remote_calc_folder = self.ctx.replica.out.remote_folder
self.ctx.structure = self.ctx.replica.out.output_structure
self.report('Convergence check: {}'.format(self.ctx.replica.res.exceeded_walltime))
return self.ctx.replica.res.exceeded_walltime
except AttributeError:
return True
# ==========================================================================
def init(self):
self.report('Init generate replicas')
self.ctx.replica_list = str(self.inputs.colvar_targets).split()
self.ctx.replicas_done = 0
self.ctx.this_name = self.inputs.replica_name
self.report('#{} replicas'.format(len(self.ctx.replica_list)))
# ==========================================================================
def next_replica(self):
self.report('Go to replica - {}'.format(len(self.ctx.replica_list)))
self.report('Remaining list: {} ({})'.format(self.ctx.replica_list,
len(self.ctx.replica_list)))
if len(self.ctx.replica_list) > 0:
self.ctx.this_replica = self.ctx.replica_list.pop(0)
else:
return False
if self.ctx.replicas_done == 0:
self.ctx.remote_calc_folder = None
self.ctx.structure = self.inputs.structure
else:
self.ctx.remote_calc_folder = self.ctx.replica.out.remote_folder
self.ctx.structure = self.ctx.replica.out.output_structure
self.ctx.replicas_done += 1
return True
# ==========================================================================
def generate_replica(self):
self.report("Running CP2K geometry optimization - Target: "
.format(self.ctx.this_replica))
inputs = self.build_calc_inputs(self.ctx.structure,
self.inputs.cell,
self.inputs.cp2k_code,
self.ctx.this_replica,
self.inputs.fixed_atoms,
self.inputs.num_machines,
self.ctx.remote_calc_folder,
self.ctx.this_name,
self.inputs.spring,
self.inputs.spring_unit,
self.inputs.target_unit,
self.inputs.subsys_colvar,
self.inputs.calc_type)
self.report(" ")
self.report("inputs: "+str(inputs))
self.report(" ")
future = submit(Cp2kCalculation.process(), **inputs)
self.report("future: "+str(future))
self.report(" ")
return ToContext(replica=Calc(future))
# ==========================================================================
def store_replica(self):
return self.out('replica_{}_{}'.format(self.ctx.this_replica,
self.ctx.this_name),
self.ctx.replica.out.output_structure)
# ==========================================================================
@classmethod
def build_calc_inputs(cls, structure, cell, code, colvar_target,
fixed_atoms, num_machines, remote_calc_folder,
replica_name, spring, spring_unit, target_unit,
subsys_colvar, calc_type):
inputs = {}
inputs['_label'] = "replica_geo_opt"
inputs['_description'] = "replica_{}_{}".format(replica_name,
colvar_target)
inputs['code'] = code
inputs['file'] = {}
# make sure we're really dealing with a gold slab
atoms = structure.get_ase() # slow
try:
first_slab_atom = np.argwhere(atoms.numbers == 79)[0, 0] + 1
is_H = atoms.numbers[first_slab_atom-1:] == 1
is_Au = atoms.numbers[first_slab_atom-1:] == 79
assert np.all(np.logical_or(is_H, is_Au))
assert np.sum(is_Au) / np.sum(is_H) == 4
except AssertionError:
raise Exception("Structure is not a proper slab.")
# structure
molslab_f, mol_f = cls.mk_coord_files(atoms, first_slab_atom)
inputs['file']['molslab_coords'] = molslab_f
inputs['file']['mol_coords'] = mol_f
# Au potential
pot_f = SinglefileData(file='/project/apps/surfaces/slab/Au.pot')
inputs['file']['au_pot'] = pot_f
# parameters
# if no cell is given use the one from the xyz file.
if cell == '' or len(str(cell)) < 3:
cell_abc = "%f %f %f" % (atoms.cell[0, 0],
atoms.cell[1, 1],
atoms.cell[2, 2])
else:
cell_abc = cell
remote_computer = code.get_remote_computer()
machine_cores = remote_computer.get_default_mpiprocs_per_machine()
inp = cls.get_cp2k_input(cell_abc,
colvar_target,
fixed_atoms,
spring, spring_unit,
target_unit,
subsys_colvar,
calc_type,
machine_cores*num_machines,
first_slab_atom,
len(atoms))
if remote_calc_folder is not None:
inputs['parent_folder'] = remote_calc_folder
inputs['parameters'] = ParameterData(dict=inp)
# settings
settings = ParameterData(dict={'additional_retrieve_list': ['*.xyz']})
inputs['settings'] = settings
# resources
inputs['_options'] = {
"resources": {"num_machines": num_machines},
"max_wallclock_seconds": 86000,
}
return inputs
# ==========================================================================
@classmethod
def get_cp2k_input(cls, cell_abc,
colvar_target, fixed_atoms,
spring, spring_unit, target_unit, subsys_colvar,
calc_type, machine_cores, first_slab_atom,
last_slab_atom):
inp = {
'GLOBAL': {
'RUN_TYPE': 'GEO_OPT',
'WALLTIME': 85500,
'PRINT_LEVEL': 'LOW'
},
'MOTION': cls.get_motion(colvar_target, fixed_atoms, spring,
spring_unit, target_unit),
'FORCE_EVAL': [],
}
if calc_type == 'Mixed DFTB':
inp['FORCE_EVAL'] = [cls.force_eval_mixed(cell_abc,
first_slab_atom,
last_slab_atom,
machine_cores,
subsys_colvar),
cls.force_eval_fist(cell_abc),
cls.get_force_eval_qs_dftb(cell_abc)]
inp['MULTIPLE_FORCE_EVALS'] = {
'FORCE_EVAL_ORDER': '2 3',
'MULTIPLE_SUBSYS': 'T'
}
elif calc_type == 'Mixed DFT':
inp['FORCE_EVAL'] = [cls.force_eval_mixed(cell_abc,
first_slab_atom,
last_slab_atom,
machine_cores,
subsys_colvar),
cls.force_eval_fist(cell_abc),
cls.get_force_eval_qs_dft(cell_abc, only_molecule=True)]
inp['MULTIPLE_FORCE_EVALS'] = {
'FORCE_EVAL_ORDER': '2 3',
'MULTIPLE_SUBSYS': 'T'
}
elif calc_type == 'Full DFT':
inp['FORCE_EVAL'] = [cls.get_force_eval_qs_dft(cell_abc, only_molecule=False,
subsys_colvar=subsys_colvar)]
return inp
# ==========================================================================
@classmethod
def force_eval_mixed(cls, cell_abc, first_slab_atom, last_slab_atom,
machine_cores, subsys_colvar):
first_mol_atom = 1
last_mol_atom = first_slab_atom - 1
mol_delim = (first_mol_atom, last_mol_atom)
slab_delim = (first_slab_atom, last_slab_atom)
force_eval = {
'METHOD': 'MIXED',
'MIXED': {
'MIXING_TYPE': 'GENMIX',
'GROUP_PARTITION': '2 %d' % (machine_cores-2),
'GENERIC': {
'ERROR_LIMIT': '1.0E-10',
'MIXING_FUNCTION': 'E1+E2',
'VARIABLES': 'E1 E2'
},
'MAPPING': {
'FORCE_EVAL_MIXED': {
'FRAGMENT':
[{'_': '1', ' ': '%d %d' % mol_delim},
{'_': '2', ' ': '%d %d' % slab_delim}],
},
'FORCE_EVAL': [{'_': '1', 'DEFINE_FRAGMENTS': '1 2'},
{'_': '2', 'DEFINE_FRAGMENTS': '1'}],
}
},
'SUBSYS': {
'CELL': {'ABC': cell_abc},
'TOPOLOGY': {
'COORD_FILE_NAME': 'mol_on_slab.xyz',
'COORDINATE': 'XYZ',
'CONNECTIVITY': 'OFF',
},
'COLVAR': subsys_colvar.get_attrs()
}
}
return force_eval
# ==========================================================================
@classmethod
def force_eval_fist(cls, cell_abc):
ff = {
'SPLINE': {
'EPS_SPLINE': '1.30E-5',
'EMAX_SPLINE': '0.8',
},
'CHARGE': [],
'NONBONDED': {
'GENPOT': [],
'LENNARD-JONES': [],
'EAM': {
'ATOMS': 'Au Au',
'PARM_FILE_NAME': 'Au.pot',
},
},
}
for x in ('Au', 'H', 'C', 'O', 'N'):
ff['CHARGE'].append({'ATOM': x, 'CHARGE': 0.0})
genpot_fun = 'A*exp(-av*r)+B*exp(-ac*r)-C/(r^6)/( 1+exp(-20*(r/R-1)) )'
genpot_val = '4.13643 1.33747 115.82004 2.206825'\
' 113.96850410723008483218 5.84114'
for x in ('C', 'N', 'O', 'H'):
ff['NONBONDED']['GENPOT'].append(
{'ATOMS': 'Au ' + x,
'FUNCTION': genpot_fun,
'VARIABLES': 'r',
'PARAMETERS': 'A av B ac C R',
'VALUES': genpot_val,
'RCUT': '15'}
)
for x in ('C H', 'H H', 'H N', 'C C', 'C O', 'C N', 'N N', 'O H',
'O N', 'O O'):
ff['NONBONDED']['LENNARD-JONES'].append(
{'ATOMS': x,
'EPSILON': '0.0',
'SIGMA': '3.166',
'RCUT': '15'}
)
force_eval = {
'METHOD': 'FIST',
'MM': {
'FORCEFIELD': ff,
'POISSON': {
'EWALD': {
'EWALD_TYPE': 'none',
},
},
},
'SUBSYS': {
'CELL': {
'ABC': cell_abc,
},
'TOPOLOGY': {
'COORD_FILE_NAME': 'mol_on_slab.xyz',
'COORDINATE': 'XYZ',
'CONNECTIVITY': 'OFF',
},
},
}
return force_eval
# ==========================================================================
@classmethod
def get_force_eval_qs_dftb(cls, cell_abc):
force_eval = {
'METHOD': 'Quickstep',
'DFT': {
'QS': {
'METHOD': 'DFTB',
'EXTRAPOLATION': 'ASPC',
'EXTRAPOLATION_ORDER': '3',
'DFTB': {
'SELF_CONSISTENT': 'T',
'DISPERSION': 'T',
'ORTHOGONAL_BASIS': 'F',
'DO_EWALD': 'F',
'PARAMETER': {
'PARAM_FILE_PATH': 'DFTB/scc',
'PARAM_FILE_NAME': 'scc_parameter',
'UFF_FORCE_FIELD': '../uff_table',
},
},
},
'SCF': {
'MAX_SCF': '30',
'SCF_GUESS': 'RESTART',
'EPS_SCF': '1.0E-6',
'OT': {
'PRECONDITIONER': 'FULL_SINGLE_INVERSE',
'MINIMIZER': 'CG',
},
'OUTER_SCF': {
'MAX_SCF': '20',
'EPS_SCF': '1.0E-6',
},
'PRINT': {
'RESTART': {
'EACH': {
'QS_SCF': '0',
'GEO_OPT': '1',
},
'ADD_LAST': 'NUMERIC',
'FILENAME': 'RESTART'
},
'RESTART_HISTORY': {'_': 'OFF'}
}
}
},
'SUBSYS': {
'CELL': {'ABC': cell_abc},
'TOPOLOGY': {
'COORD_FILE_NAME': 'mol.xyz',
'COORDINATE': 'xyz'
}
}
}
return force_eval
# ==========================================================================
@classmethod
def get_motion(cls, colvar_target, fixed_atoms, spring, spring_unit,
target_unit):
motion = {
'CONSTRAINT': {
'COLLECTIVE': {
'COLVAR': 1,
'RESTRAINT': {
'K': '[{}] {}'.format(spring_unit, spring)
},
'TARGET': '[{}] {}'.format(target_unit, colvar_target),
'INTERMOLECULAR': ''
},
'FIXED_ATOMS': {
'LIST': '{}'.format(fixed_atoms)
}
},
'GEO_OPT': {
'MAX_FORCE': '0.0001',
'MAX_ITER': '5000',
'OPTIMIZER': 'BFGS',
'BFGS' : {
'TRUST_RADIUS' : '[bohr] 0.1'
}
},
}
return motion
# ==========================================================================
@classmethod
def get_force_eval_qs_dft(cls, cell_abc, only_molecule,
subsys_colvar=None):
force_eval = {
'METHOD': 'Quickstep',
'DFT': {
'BASIS_SET_FILE_NAME': 'BASIS_MOLOPT',
'POTENTIAL_FILE_NAME': 'POTENTIAL',
'RESTART_FILE_NAME': './parent_calc/aiida-RESTART.wfn',
'QS': {
'METHOD': 'GPW',
'EXTRAPOLATION': 'ASPC',
'EXTRAPOLATION_ORDER': '3',
'EPS_DEFAULT': '1.0E-14',
},
'MGRID': {
'CUTOFF': '600',
'NGRIDS': '5',
},
'SCF': {
'MAX_SCF': '20',
'SCF_GUESS': 'RESTART',
'EPS_SCF': '1.0E-7',
'OT': {
'PRECONDITIONER': 'FULL_SINGLE_INVERSE',
'MINIMIZER': 'CG',
},
'OUTER_SCF': {
'MAX_SCF': '15',
'EPS_SCF': '1.0E-7',
},
'PRINT': {
'RESTART': {
'EACH': {
'QS_SCF': '0',
'GEO_OPT': '1',
},
'ADD_LAST': 'NUMERIC',
'FILENAME': 'RESTART'
},
'RESTART_HISTORY': {'_': 'OFF'}
}
},
'XC': {
'XC_FUNCTIONAL': {'_': 'PBE'},
'VDW_POTENTIAL': {
'DISPERSION_FUNCTIONAL': 'PAIR_POTENTIAL',
'PAIR_POTENTIAL': {
'TYPE': 'DFTD3',
'CALCULATE_C9_TERM': '.TRUE.',
'PARAMETER_FILE_NAME': 'dftd3.dat',
'REFERENCE_FUNCTIONAL': 'PBE',
'R_CUTOFF': '[angstrom] 15',
}
}
},
},
'SUBSYS': {
'CELL': {'ABC': cell_abc},
'TOPOLOGY': {
'COORD_FILE_NAME': 'mol_on_slab.xyz',
'COORDINATE': 'xyz',
},
'KIND': [],
}
}
if only_molecule:
force_eval['SUBSYS']['TOPOLOGY']['COORD_FILE_NAME'] = 'mol.xyz'
if subsys_colvar is not None:
force_eval['SUBSYS']['COLVAR'] = subsys_colvar.get_attrs()
force_eval['SUBSYS']['KIND'].append({
'_': 'Au',
'BASIS_SET': 'DZVP-MOLOPT-SR-GTH',
'POTENTIAL': 'GTH-PBE-q11'
})
force_eval['SUBSYS']['KIND'].append({
'_': 'C',
'BASIS_SET': 'TZV2P-MOLOPT-GTH',
'POTENTIAL': 'GTH-PBE-q4'
})
force_eval['SUBSYS']['KIND'].append({
'_': 'Br',
'BASIS_SET': 'DZVP-MOLOPT-SR-GTH',
'POTENTIAL': 'GTH-PBE-q7'
})
force_eval['SUBSYS']['KIND'].append({
'_': 'O',
'BASIS_SET': 'TZV2P-MOLOPT-GTH',
'POTENTIAL': 'GTH-PBE-q6'
})
force_eval['SUBSYS']['KIND'].append({
'_': 'N',
'BASIS_SET': 'TZV2P-MOLOPT-GTH',
'POTENTIAL': 'GTH-PBE-q5'
})
force_eval['SUBSYS']['KIND'].append({
'_': 'I',
'BASIS_SET': 'DZVP-MOLOPT-SR-GTH',
'POTENTIAL': 'GTH-PBE-q7'
})
force_eval['SUBSYS']['KIND'].append({
'_': 'H',
'BASIS_SET': 'TZV2P-MOLOPT-GTH',
'POTENTIAL': 'GTH-PBE-q1'
})
return force_eval
# ==========================================================================
@classmethod
def mk_coord_files(cls, atoms, first_slab_atom):
mol = atoms[:first_slab_atom-1]
tmpdir = tempfile.mkdtemp()
molslab_fn = tmpdir + '/mol_on_slab.xyz'
mol_fn = tmpdir + '/mol.xyz'
atoms.write(molslab_fn)
mol.write(mol_fn)
molslab_f = SinglefileData(file=molslab_fn)
mol_f = SinglefileData(file=mol_fn)
shutil.rmtree(tmpdir)
return molslab_f, mol_f