-
Notifications
You must be signed in to change notification settings - Fork 0
/
high_throughput_dft_calculation.py
358 lines (287 loc) · 13.6 KB
/
high_throughput_dft_calculation.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
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 13 17:12:52 2023
@author: 18326
"""
import numpy as np
import os
import shutil
import sys
from ase.build import add_vacuum, sort
from ase.constraints import FixAtoms
from ase.io import read
from .generate_adsorption_sites import AddAtoms
from ...lib.model_lib import config_parser
from ...lib.adsorbate_poscar import adsorbate_poscar
from ...default_parameters import default_hp_dft_config
from ...lib.file_lib import modify_INCAR
from ...lib.high_throughput_lib import get_ase_atom_from_formula, get_v_per_atom, run_vasp
class HtDftAds(object):
"""High-throughput DFT calculations for adsorption. See details in: https://jzhang-github.github.io/AGAT/Default%20parameters.html#default_hp_dft_config
.. py:property:: root_dir
The root working directory of this object.
:param **hp_config: Configurations to control the process.
:type **hp_config: dict
Example::
HA = HpDftAds(calculation_index=0)
HA.run('NiCoFePdPt')
"""
def __init__(self, **hp_config):
self.hp_config = {**default_hp_dft_config, **config_parser(hp_config)}
self.root_dir = os.getcwd()
def bulk_opt(self, formula):
"""Structural optimization of the bulk structure with VASP.
:param formula: Chemical formula
:type formula: str
"""
# generate bulk structure
chemical_formula = formula
v_per_atom = get_v_per_atom(formula)
atoms = get_ase_atom_from_formula(chemical_formula, v_per_atom)
atoms = sort(atoms)
atoms.write('POSCAR')
# get POTCAR
os.system('getpotential_auto_zj.sh 6')
# get INCAR
with open('INCAR', 'w') as f:
f.write(self.hp_config['INCAR_static'])
os.system('modify_MAGMOM_auto.sh POSCAR')
modify_INCAR('ISIF', '3')
# get KPOINTS
with open('KPOINTS', 'w') as f:
f.write(self.hp_config['KPOINTS'])
# run vasp
run_vasp(vasp_bash_path=self.hp_config['vasp_bash_path'])
print('Bulk static optimization done.')
def surf_opt(self, bulk_structural_file='CONTCAR_bulk_opt'):
"""Structural optimization of the surface slab with VASP.
:param bulk_structural_file: optimized bulk structure, defaults to 'CONTCAR_bulk_opt'
:type bulk_structural_file: str, optional
"""
atoms_bulk = read(bulk_structural_file)
atoms_bulk.positions += 1.3 # avoid PBC error
atoms_bulk.wrap() # avoid PBC error
# add vacuum space and fix bottom atoms
len_z = atoms_bulk.cell.array[2][2]
c = FixAtoms(indices=np.where(atoms_bulk.positions[:,2] < len_z / 2)[0])
atoms_bulk.set_constraint(c)
add_vacuum(atoms_bulk, 10.0)
atoms_bulk.write('POSCAR') # Write POSCAR of the surface model.
# get POTCAR
os.system('getpotential_auto_zj.sh 6')
# get INCAR
with open('INCAR', 'w') as f:
f.write(self.hp_config['INCAR_static'])
os.system('modify_MAGMOM_auto.sh POSCAR')
modify_INCAR('ISIF', '2')
# get KPOINTS
with open('KPOINTS', 'w') as f:
f.write(self.hp_config['KPOINTS'])
# run vasp
run_vasp(vasp_bash_path=self.hp_config['vasp_bash_path'])
print('Surface static optimization done.')
def ads_opt(self, structural_file='CONTCAR_surf_opt', random_samples=5):
"""Structural optimization of the adsorption with VASP.
:param structural_file: Structural file name of optimized clean surface, defaults to 'CONTCAR_surf_opt'
:type structural_file: str, optional
:param random_samples: On one surface, many surface sites can be detected, this number controls how many individual calculations will be performed on this surface, defaults to 5
:type random_samples: int, optional
.. Note::
``random_samples`` cannot be larger than the number of detected surface sites.
"""
# generate structures
for ads in self.hp_config['adsorbates']:
# generate adsorption configurations
adder = AddAtoms(structural_file,
species=ads,
sites=self.hp_config['sites'],
dist_from_surf=self.hp_config['dist_from_surf'],
num_atomic_layer_along_Z=6)
all_sites = adder.write_file_with_adsorption_sites(adsorbate_poscar,
calculation_index=self.hp_config['calculation_index'])
if not os.path.exists('POSCARs'):
os.mkdir('POSCARs')
for i in range(all_sites):
os.rename(f'POSCAR_{self.hp_config["calculation_index"]}_{i}',
os.path.join('POSCARs', f'POSCAR_{self.hp_config["calculation_index"]}_{i}'))
for i in np.random.choice(range(all_sites), random_samples, replace=False):
if not os.path.exists(str(i)):
os.mkdir(str(i))
shutil.copyfile(os.path.join('POSCARs', f'POSCAR_{self.hp_config["calculation_index"]}_{i}'),
os.path.join(str(i), 'POSCAR'))
parent_dir = os.getcwd()
os.chdir(str(i))
# get POTCAR
os.system('getpotential_auto_zj.sh 6')
# get INCAR
with open('INCAR', 'w') as f:
f.write(self.hp_config['INCAR_static'])
os.system('modify_MAGMOM_auto.sh POSCAR')
modify_INCAR('ISIF', '2')
# get KPOINTS
with open('KPOINTS', 'w') as f:
f.write(self.hp_config['KPOINTS'])
# run vasp
run_vasp(vasp_bash_path=self.hp_config['vasp_bash_path'])
print(f'Adsorption static calculation for {ads}_{i} done.')
os.chdir(parent_dir)
def bulk_aimd(self, formula):
"""AIMD simulation for a bulk structure of given chemical formula.
:param formula: The given chemical formula.
:type formula: str
"""
# generate bulk structure
chemical_formula = formula
v_per_atom = get_v_per_atom(formula)
atoms = get_ase_atom_from_formula(chemical_formula, v_per_atom)
atoms = sort(atoms)
atoms.write('POSCAR')
# get POTCAR
os.system('getpotential_auto_zj.sh 6')
# get INCAR
with open('INCAR', 'w') as f:
f.write(self.hp_config['INCAR_aimd'])
os.system('modify_MAGMOM_auto.sh POSCAR')
modify_INCAR('ISIF', '3')
modify_INCAR('NSW', '100')
# get KPOINTS
with open('KPOINTS', 'w') as f:
f.write(self.hp_config['KPOINTS'])
# run vasp
run_vasp(vasp_bash_path=self.hp_config['vasp_bash_path'])
print('Bulk AIMD simulation done.')
def surface_aimd(self, bulk_structural_file='CONTCAR_bulk_opt'):
"""AIMD simulation for the clean surface.
:param bulk_structural_file: File name of the bulk structure, defaults to 'CONTCAR_bulk_opt'
:type bulk_structural_file: str, optional
"""
atoms_bulk = read(bulk_structural_file)
atoms_bulk.positions += 1.3 # avoid PBC error
atoms_bulk.wrap() # avoid PBC error
# add vacuum space and fix bottom atoms
len_z = atoms_bulk.cell.array[2][2]
c = FixAtoms(indices=np.where(atoms_bulk.positions[:,2] < len_z / 2)[0])
atoms_bulk.set_constraint(c)
add_vacuum(atoms_bulk, 10.0)
atoms_bulk.write('POSCAR') # Write POSCAR of the surface model.
# get POTCAR
os.system('getpotential_auto_zj.sh 6')
# get INCAR
with open('INCAR', 'w') as f:
f.write(self.hp_config['INCAR_aimd'])
os.system('modify_MAGMOM_auto.sh POSCAR')
modify_INCAR('ISIF', '2')
modify_INCAR('NSW', '100')
# get KPOINTS
with open('KPOINTS', 'w') as f:
f.write(self.hp_config['KPOINTS'])
# run vasp
run_vasp(vasp_bash_path=self.hp_config['vasp_bash_path'])
print('Surface AIMD simulation done.')
def ads_aimd(self, structural_file='CONTCAR_surf_opt', random_samples=2):
"""AIMD simulation for the adsorption.
:param structural_file: File name of the clean surface, defaults to 'CONTCAR_surf_opt'
:type structural_file: str, optional
:param random_samples: Randomly select surface sites for the simulation, defaults to 2
:type random_samples: int, optional
.. Note::
``random_samples`` cannot be larger than the number of detected surface sites.
"""
# generate structures
for ads in self.hp_config['adsorbates']:
# generate adsorption configurations
adder = AddAtoms(structural_file,
species=ads,
sites=self.hp_config['sites'],
dist_from_surf=self.hp_config['dist_from_surf'],
num_atomic_layer_along_Z=6)
all_sites = adder.write_file_with_adsorption_sites(adsorbate_poscar,
calculation_index=self.hp_config['calculation_index'])
if not os.path.exists('POSCARs'):
os.mkdir('POSCARs')
for i in range(all_sites):
os.rename(f'POSCAR_{self.hp_config["calculation_index"]}_{i}',
os.path.join('POSCARs', f'POSCAR_{self.hp_config["calculation_index"]}_{i}'))
for i in np.random.choice(range(all_sites), random_samples, replace=False):
if not os.path.exists(str(i)):
os.mkdir(str(i))
shutil.copyfile(os.path.join('POSCARs', f'POSCAR_{self.hp_config["calculation_index"]}_{i}'),
os.path.join(str(i), 'POSCAR'))
parent_dir = os.getcwd()
os.chdir(str(i))
# get POTCAR
os.system('getpotential_auto_zj.sh 6')
# get INCAR
with open('INCAR', 'w') as f:
f.write(self.hp_config['INCAR_aimd'])
os.system('modify_MAGMOM_auto.sh POSCAR')
modify_INCAR('ISIF', '2')
modify_INCAR('NSW', '100')
# get KPOINTS
with open('KPOINTS', 'w') as f:
f.write(self.hp_config['KPOINTS'])
# run vasp
run_vasp(vasp_bash_path=self.hp_config['vasp_bash_path'])
print(f'Adsorption AIMD simulation for {ads}_{i} done.')
os.chdir(parent_dir)
def run(self, formula, **kwargs):
"""
:param formula: Chemical formula.
:type formula: str
:param **kwargs: Configurations to control the process
:type **kwargs: dict
"""
self.hp_config = {**self.hp_config, **config_parser(kwargs)}
# bulk static calculation
if self.hp_config['include_bulk_static']:
if not os.path.exists('bulk_static'):
os.mkdir('bulk_static')
os.chdir('bulk_static')
self.bulk_opt(formula)
os.chdir(self.root_dir)
# surface static calculation
if self.hp_config['include_surface_static']:
if not os.path.exists('surface_static'):
os.mkdir('surface_static')
shutil.copyfile(os.path.join('bulk_static', 'CONTCAR'),
os.path.join('surface_static', 'CONTCAR_bulk_opt'))
os.chdir('surface_static')
self.surf_opt(bulk_structural_file='CONTCAR_bulk_opt')
os.chdir(self.root_dir)
# adsorption static calculation
if self.hp_config['include_adsorption_static']:
if not os.path.exists('adsorption_static'):
os.mkdir('adsorption_static')
shutil.copyfile(os.path.join('surface_static', 'CONTCAR'),
os.path.join('adsorption_static', 'CONTCAR_surf_opt'))
os.chdir('adsorption_static')
self.ads_opt(structural_file='CONTCAR_surf_opt', random_samples=self.hp_config['random_samples'])
os.chdir(self.root_dir)
# bulk aimd
if self.hp_config['include_bulk_aimd']:
if not os.path.exists('bulk_aimd'):
os.mkdir('bulk_aimd')
os.chdir('bulk_aimd')
self.bulk_aimd(formula)
os.chdir(self.root_dir)
# surface aimd calculation
if self.hp_config['include_surface_aimd']:
if not os.path.exists('surface_aimd'):
os.mkdir('surface_aimd')
shutil.copyfile(os.path.join('bulk_static', 'CONTCAR'),
os.path.join('surface_aimd', 'CONTCAR_bulk_opt'))
os.chdir('surface_aimd')
self.surface_aimd(bulk_structural_file='CONTCAR_bulk_opt')
os.chdir(self.root_dir)
# adsorption static calculation
if self.hp_config['include_adsorption_aimd']:
if not os.path.exists('adsorption_aimd'):
os.mkdir('adsorption_aimd')
shutil.copyfile(os.path.join('surface_static', 'CONTCAR'),
os.path.join('adsorption_aimd', 'CONTCAR_surf_opt'))
os.chdir('adsorption_aimd')
self.ads_aimd(structural_file='CONTCAR_surf_opt', random_samples=self.hp_config['random_samples'])
os.chdir(self.root_dir)
if __name__ == '__main__':
HT = HtDftAds(calculation_index=0)
HT.run(sys.argv[1]) # debug only