-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
337 lines (304 loc) · 11.8 KB
/
common.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
import os
from VASP import VASP
from DEEPMD_LMP import DEEPMD_LMP
from MEAM_LMP import MEAM_LMP
from EAM_FS_LMP import EAM_FS_LMP
from EAM_ALLOY_LMP import EAM_ALLOY_LMP
from EOS import EOS
from Elastic import Elastic
from Vacancy import Vacancy
from Interstitial import Interstitial
from Surface import Surface
import dpgen.auto_test.lib.crys as crys
import glob, warnings, json
from dpgen.remote.decide_machine import decide_fp_machine, decide_model_devi_machine
import dpgen.auto_test.lib.util as util
from dpgen.dispatcher.Dispatcher import make_dispatcher
lammps_task_type = ['deepmd', 'meam', 'eam_fs', 'eam_alloy']
def make_task(inter_parameter,
path_to_poscar):
"""
Make an instance of Task
"""
inter_type = inter_parameter['type']
if inter_type == 'vasp':
return VASP(inter_parameter, path_to_poscar)
elif inter_type == 'deepmd':
return DEEPMD_LMP(inter_parameter, path_to_poscar)
elif inter_type == 'meam':
return MEAM_LMP(inter_parameter, path_to_poscar)
elif inter_type == 'eam_fs':
return EAM_FS_LMP(inter_parameter, path_to_poscar)
elif inter_type == 'eam_alloy':
return EAM_ALLOY_LMP(inter_parameter, path_to_poscar)
else:
raise RuntimeError(f'unknown interaction {inter_type}')
def make_task_trans_files(inter_parameter):
"""
Make the forward and backward file of an Task
"""
inter_type = inter_parameter['type']
if inter_type == 'vasp':
return VASP.forward_files, VASP.forward_common_files, VASP.backward_files
elif inter_type == 'deepmd':
return DEEPMD_LMP.forward_files, DEEPMD_LMP.forward_common_files, DEEPMD_LMP.backward_files
elif inter_type == 'meam':
return MEAM_LMP.forward_files, MEAM_LMP.forward_common_files, MEAM_LMP.backward_files
elif inter_type == 'eam_fs':
return EAM_FS_LMP.forward_files, EAM_FS_LMP.forward_common_files, EAM_FS_LMP.backward_files
elif inter_type == 'eam_alloy':
return EAM_ALLOY_LMP.forward_files, EAM_ALLOY_LMP.forward_common_files, EAM_ALLOY_LMP.backward_files
else:
raise RuntimeError(f'unknown interaction {inter_type}')
def make_property(paramters):
"""
Make an instance of Property
"""
prop_type = paramters['type']
if prop_type == 'eos':
return EOS(paramters)
elif prop_type == 'elastic':
return Elastic(paramters)
elif prop_type == 'vacancy':
return Vacancy(paramters)
elif prop_type == 'interstitial':
return Interstitial(paramters)
elif prop_type == 'surface':
return Surface(paramters)
else:
raise RuntimeError(f'unknown property type {prop_type}')
def make_equi(confs,
inter_param,
relax_param):
# find all POSCARs and their name like mp-xxx
# ...
ele_list = [key for key in inter_param['type_map'].keys()]
conf_dirs = glob.glob(confs)
conf_dirs.sort()
# generate a list of task names like mp-xxx/relaxation
# ...
cwd = os.getcwd()
# generate poscar for single element crystal
if len(ele_list) == 1:
for ii in conf_dirs:
os.chdir(ii)
crys_type = ii[3:]
if crys_type == 'fcc':
if not os.path.exists('POSCAR'):
crys.fcc(ele_list[0]).to('POSCAR', 'POSCAR')
elif crys_type == 'hcp':
if not os.path.exists('POSCAR'):
crys.hcp(ele_list[0]).to('POSCAR', 'POSCAR')
elif crys_type == 'dhcp':
if not os.path.exists('POSCAR'):
crys.dhcp(ele_list[0]).to('POSCAR', 'POSCAR')
elif crys_type == 'bcc':
if not os.path.exists('POSCAR'):
crys.bcc(ele_list[0]).to('POSCAR', 'POSCAR')
elif crys_type == 'diamond':
if not os.path.exists('POSCAR'):
crys.diamond(ele_list[0]).to('POSCAR', 'POSCAR')
elif crys_type == 'sc':
if not os.path.exists('POSCAR'):
crys.sc(ele_list[0]).to('POSCAR', 'POSCAR')
os.chdir(cwd)
task_dirs = []
# make task directories like mp-xxx/relaxation
# if mp-xxx/exists then print a warning and exit.
# ...
for ii in conf_dirs:
poscar = os.path.abspath(os.path.join(ii, 'POSCAR'))
if not os.path.exists(poscar):
raise FileNotFoundError('no configuration for autotest')
relax_dirs = os.path.abspath(os.path.join(ii, 'relaxation'))
if os.path.exists(relax_dirs):
warnings.warn('%s already exists' % relax_dirs)
else:
os.makedirs(relax_dirs)
task_dirs.append(relax_dirs)
os.chdir(relax_dirs)
# copy POSCARs to mp-xxx/relaxation
# ...
os.symlink(os.path.relpath(poscar), 'POSCAR')
os.chdir(cwd)
task_dirs.sort()
# generate task files
for ii in task_dirs:
poscar = os.path.join(ii, 'POSCAR')
inter = make_task(inter_param, poscar)
inter.make_potential_files(ii)
inter.make_input_file(ii, 'relaxation', relax_param)
def run_equi(confs,
inter_param,
mdata):
# find all POSCARs and their name like mp-xxx
# ...
conf_dirs = glob.glob(confs)
conf_dirs.sort()
# generate a list of task names like mp-xxx/relaxation
# ...
work_path_list = []
for ii in conf_dirs:
work_path_list.append(os.path.join(ii, 'relaxation'))
all_task = []
for ii in work_path_list:
all_task.append(os.path.join(ii, '.'))
inter_type = inter_param['type']
# vasp
if inter_type == "vasp":
mdata = decide_fp_machine(mdata)
elif inter_type in lammps_task_type:
mdata = decide_model_devi_machine(mdata)
else:
raise RuntimeError("unknown task %s, something wrong" % task_type)
# dispatch the tasks
forward_files, forward_common_files, backward_files = make_task_trans_files(inter_param)
# backward_files += logs
# ...
run_tasks = util.collect_task(all_task, inter_type)
if len(run_tasks) == 0:
return
else:
run_tasks = [os.path.basename(ii) for ii in all_task]
machine, resources, command, group_size = util.get_machine_info(mdata, inter_type)
for ii in range(len(work_path_list)):
work_path = work_path_list[ii]
disp = make_dispatcher(machine, resources, work_path, run_tasks[ii], group_size)
disp.run_jobs(resources,
command,
work_path,
run_tasks[ii],
group_size,
forward_common_files,
forward_files,
backward_files,
outlog=inter_type + '.out',
errlog=inter_type + '.err')
def post_equi(confs, inter_param):
# find all POSCARs and their name like mp-xxx
# ...
conf_dirs = glob.glob(confs)
conf_dirs.sort()
task_dirs = []
for ii in conf_dirs:
task_dirs.append(os.path.abspath(os.path.join(ii, 'relaxation')))
task_dirs.sort()
# generate a list of task names like mp-xxx/relaxation
# ...
# dump the relaxation result.
for ii in task_dirs:
poscar = os.path.join(ii, 'POSCAR')
inter = make_task(inter_param, poscar)
res = inter.compute(ii)
with open(os.path.join(ii, 'result.json'), 'w') as fp:
json.dump(res, fp, indent=4)
def make_property(confs,
inter_param,
property_list):
# find all POSCARs and their name like mp-xxx
# ...
conf_dirs = glob.glob(confs)
conf_dirs.sort()
for ii in conf_dirs:
for jj in property_list:
if 'init_from_suffix' and 'output_suffix' in jj:
do_refine = True
suffix = jj['output_suffix']
else:
do_refine = False
suffix = 0
# generate working directory like mp-xxx/eos_00 if jj['type'] == 'eos'
# handel the exception that the working directory exists
# ...
# determine the suffix: from scratch or refine
# ...
property_type = jj['type']
path_to_equi = os.path.join(ii, 'relaxation')
path_to_work = os.path.join(ii, property_type + '_' + suffix)
if os.path.exists(path_to_work):
warnings.warn('%s already exists' % path_to_work)
else:
os.makedirs(path_to_work)
prop = make_property(jj)
task_list = prop.make_confs(path_to_work, path_to_equi, do_refine)
for kk in task_list:
poscar = os.path.join(kk, 'POSCAR')
inter = make_task(inter_param, poscar)
inter.make_potential_files(kk)
inter.make_input_file(kk, prop.task_type, prop.task.pararm)
def run_property(confs,
inter_param,
property_list,
mdata):
# find all POSCARs and their name like mp-xxx
# ...
conf_dirs = glob.glob(confs)
conf_dirs.sort()
task_list = []
work_path_list = []
for ii in conf_dirs:
for jj in property_list:
# determine the suffix: from scratch or refine
# ...
if 'init_from_suffix' and 'output_suffix' in jj:
suffix = jj['output_suffix']
else:
suffix = 0
property_type = jj['type']
path_to_work = os.path.join(ii, property_type + '_' + suffix)
work_path_list.append(path_to_work)
tmp_task_list = glob.glob(os.path.join(path_to_work, 'task.[0-9]*[0-9]'))
tmp_task_list.sort()
task_list.append(tmp_task_list)
# dispatch the tasks
forward_files, forward_common_files, backward_files = make_task_trans_files(inter_param)
# backward_files += logs
# ...
task_type = inter_param['type']
# vasp
if task_type == "vasp":
mdata = decide_fp_machine(mdata)
elif task_type in lammps_task_type:
mdata = decide_model_devi_machine(mdata)
else:
raise RuntimeError("unknown task %s, something wrong" % task_type)
for ii in range(len(work_path_list)):
work_path = work_path_list[ii]
all_task = task_list[ii]
run_tasks = util.collect_task(all_task, task_type)
if len(run_tasks) == 0:
return
else:
run_tasks = [os.path.basename(ii) for ii in all_task]
machine, resources, command, group_size = util.get_machine_info(mdata, task_type)
disp = make_dispatcher(machine, resources, work_path, run_tasks, group_size)
disp.run_jobs(resources,
command,
work_path,
run_tasks,
group_size,
forward_common_files,
forward_files,
backward_files,
outlog=task_type + '.out',
errlog=task_type + '.err')
def post_property(confs,
# inter_param,
property_list):
# find all POSCARs and their name like mp-xxx
# ...
# task_list = []
conf_dirs = glob.glob(confs)
conf_dirs.sort()
for ii in conf_dirs:
for jj in property_list:
# determine the suffix: from scratch or refine
# ...
if 'init_from_suffix' and 'output_suffix' in jj:
suffix = jj['output_suffix']
else:
suffix = 0
property_type = jj['type']
path_to_work = os.path.join(ii, property_type + '_' + suffix)
prop = make_property(jj)
prop.compute('result.json', 'result.out', path_to_work)