dockonsurf / modules / calculation.py @ 2be92b2c
Historique | Voir | Annoter | Télécharger (6,35 ko)
1 |
import os |
---|---|
2 |
import logging |
3 |
|
4 |
logger = logging.getLogger('DockOnSurf')
|
5 |
|
6 |
|
7 |
def prep_cp2k(inp_file, run_type, atms_list): # TODO name to PROJECT_NAME |
8 |
"""Prepares the directories to run isolated calculation with CP2K.
|
9 |
|
10 |
@param inp_file: CP2K Input file to run the calculations with.
|
11 |
@param run_type: Type of calculation. 'isolated', 'screening' or
|
12 |
'refinement'
|
13 |
@param atms_list: list of ase.Atoms objects to run the calculation of.
|
14 |
@return: None
|
15 |
"""
|
16 |
from shutil import copy |
17 |
import ase.io |
18 |
from pycp2k import CP2K |
19 |
from modules.utilities import check_bak |
20 |
cp2k = CP2K() |
21 |
cp2k.parse(inp_file) |
22 |
force_eval = cp2k.CP2K_INPUT.FORCE_EVAL_list[0]
|
23 |
if force_eval.SUBSYS.TOPOLOGY.Coord_file_name is None: |
24 |
logger.warning("'COORD_FILE_NAME' not specified on CP2K input. Using\n"
|
25 |
"default name 'coord.xyz'. A new CP2K input file with "
|
26 |
"the 'COORD_FILE_NAME' variable is created. If there\n"
|
27 |
"is a name conflict the old file will be backed up.")
|
28 |
force_eval.SUBSYS.TOPOLOGY.Coord_file_name = 'coord.xyz'
|
29 |
print(inp_file.split('/')[-1]) |
30 |
check_bak(inp_file.split('/')[-1]) |
31 |
cp2k.write_input_file(inp_file.split('/')[-1]) |
32 |
|
33 |
coord_file = force_eval.SUBSYS.TOPOLOGY.Coord_file_name |
34 |
|
35 |
# Creating and setting up directories for every configuration.
|
36 |
for i, conf in enumerate(atms_list): |
37 |
os.mkdir(f'{run_type}/conf_{i}')
|
38 |
copy(inp_file, f'{run_type}/conf_{i}/')
|
39 |
ase.io.write(f'{run_type}/conf_{i}/{coord_file}', conf)
|
40 |
|
41 |
|
42 |
def get_jobs_status(job_ids, stat_cmd, stat_dict): |
43 |
"""Returns a list of job status for a list of job ids.
|
44 |
|
45 |
@param job_ids: list of all jobs to be checked their status.
|
46 |
@param stat_cmd: Command to check job status.
|
47 |
@param stat_dict: Dictionary with pairs of job status (r, p, f) and the
|
48 |
pattern it matches in the output of the stat_cmd.
|
49 |
@return: list of status for every job.
|
50 |
"""
|
51 |
from subprocess import PIPE, Popen |
52 |
status_list = [] |
53 |
for job in job_ids: |
54 |
stat_order = stat_cmd % job |
55 |
stat_msg = Popen(stat_order, shell=True,
|
56 |
stdout=PIPE).communicate()[0].decode('utf-8').strip() |
57 |
if stat_dict['r'] == stat_msg: |
58 |
status_list.append('r')
|
59 |
elif stat_dict['p'] == stat_msg: |
60 |
status_list.append('p')
|
61 |
elif stat_dict['f'] == stat_msg: |
62 |
status_list.append('f')
|
63 |
else:
|
64 |
logger.warning(f'Unrecognized job status: {job}')
|
65 |
return status_list
|
66 |
|
67 |
|
68 |
def submit_jobs(run_type, sub_cmd, sub_script, stat_cmd, stat_dict, max_pend, |
69 |
name): |
70 |
"""Submits jobs to a custom queuing system with the provided script
|
71 |
|
72 |
@param run_type: Type of calculation. 'isolated', 'screening', 'refinement'
|
73 |
@param sub_cmd: The command used to submit jobs.
|
74 |
@param sub_script: script for the job submission.
|
75 |
@param stat_cmd: Command to check job status.
|
76 |
@param stat_dict: Dictionary with pairs of job status (r, p, f) and the
|
77 |
pattern it matches in the output of the stat_cmd.
|
78 |
@param max_pend: Maximum number of simultaneous jobs waiting to be executed.
|
79 |
@param name: name of the project.
|
80 |
"""
|
81 |
from shutil import copy |
82 |
from time import sleep |
83 |
from subprocess import PIPE, Popen |
84 |
subm_jobs = [] |
85 |
init_dir = os.getcwd() |
86 |
for conf in os.listdir(run_type): |
87 |
i = conf.split('_')[1] |
88 |
while get_jobs_status(subm_jobs, stat_cmd, stat_dict).count('p') \ |
89 |
>= max_pend: |
90 |
sleep(30)
|
91 |
copy(sub_script, f"{run_type}/{conf}")
|
92 |
os.chdir(f"{run_type}/{conf}")
|
93 |
job_name = f'{name[:5].capitalize()}{run_type[:3].capitalize()}{i}'
|
94 |
sub_order = sub_cmd % (job_name, sub_script) |
95 |
subm_msg = Popen(sub_order, shell=True, stdout=PIPE).communicate()[0] |
96 |
job_id = None
|
97 |
for word in subm_msg.decode("utf-8").split(): |
98 |
try:
|
99 |
job_id = int(word)
|
100 |
break
|
101 |
except ValueError: |
102 |
continue
|
103 |
subm_jobs.append(job_id) |
104 |
os.chdir(init_dir) |
105 |
|
106 |
logger.info('All jobs have been submitted, waiting for them to finish.')
|
107 |
while not all([stat == 'f' for stat in |
108 |
get_jobs_status(subm_jobs, stat_cmd, stat_dict)]): |
109 |
sleep(30)
|
110 |
logger.info('All jobs have finished.')
|
111 |
|
112 |
|
113 |
def run_calc(run_type, inp_vars, atms_list): |
114 |
"""Directs the calculation run according to the provided arguments.
|
115 |
|
116 |
@param run_type: Type of calculation. 'isolated', 'screening' or
|
117 |
'refinement'
|
118 |
@param inp_vars: Calculation parameters from input file.
|
119 |
@param atms_list: List of ase.Atoms objects containing the sets of atoms
|
120 |
aimed to run the calculations of.
|
121 |
"""
|
122 |
from modules.utilities import check_bak |
123 |
run_types = ['isolated', 'screening', 'refinement'] |
124 |
run_type_err = f"'run_type' must be one of the following: {run_types}"
|
125 |
if not isinstance(run_type, str) or run_type.lower() not in run_types: |
126 |
logger.error(run_type_err) |
127 |
raise ValueError(run_type_err) |
128 |
|
129 |
if inp_vars['batch_q_sys']: |
130 |
logger.info(f"Running {run_type} calculation with {inp_vars['code']} on"
|
131 |
f" {inp_vars['batch_q_sys']}.")
|
132 |
else:
|
133 |
logger.info(f"Doing a dry run of {run_type}.")
|
134 |
check_bak(run_type) |
135 |
os.mkdir(run_type) |
136 |
if inp_vars['code'] == 'cp2k': |
137 |
if run_type == 'isolated': |
138 |
prep_cp2k(inp_vars['isol_inp_file'], run_type, atms_list)
|
139 |
elif run_type == 'screening': |
140 |
prep_cp2k(inp_vars['screen_inp_file'], run_type, atms_list)
|
141 |
elif run_type == 'refinement': |
142 |
prep_cp2k(inp_vars['refine_inp_file'], run_type, atms_list)
|
143 |
# elif: inp_vars['code'] == 'Other codes here'
|
144 |
|
145 |
if inp_vars['batch_q_sys'] == 'sge': |
146 |
stat_cmd = "qstat | grep %s | awk '{print $5}'"
|
147 |
stat_dict = {'r': 'r', 'p': 'qw', 'f': ''} |
148 |
submit_jobs(run_type, 'qsub -N %s %s', inp_vars['subm_script'], |
149 |
stat_cmd, stat_dict, inp_vars['max_qw'],
|
150 |
inp_vars['project_name'])
|
151 |
elif inp_vars['batch_q_sys'] == 'lsf': |
152 |
stat_cmd = "bjobs -w | grep %s | awk ''"
|
153 |
submit_jobs(run_type, 'bsub', inp_vars['subm_script'], |
154 |
inp_vars['max_qw'], inp_vars['project_name']) |
155 |
elif inp_vars['batch_q_sys'] == 'local': # TODO implement local |
156 |
pass # run_local |
157 |
elif inp_vars['batch_q_sys'] == 'none': |
158 |
pass
|