dockonsurf / modules / calculation.py @ 86cf3a38
Historique | Voir | Annoter | Télécharger (5,13 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_sge(job_ids): # TODO more elegant |
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 |
@return: list of status for every job.
|
47 |
"""
|
48 |
from gridtk.tools import qstat |
49 |
run_chk = 'usage 1'
|
50 |
status_list = [] |
51 |
for job in job_ids: |
52 |
if run_chk in qstat(job): |
53 |
status_list.append('r')
|
54 |
elif len(qstat(job)) > 0: |
55 |
status_list.append('q')
|
56 |
else:
|
57 |
status_list.append('f')
|
58 |
return status_list
|
59 |
|
60 |
|
61 |
def sub_sge(run_type, sub_script, max_qw, name): |
62 |
"""Submits jobs to the sge queuing system with the provided script
|
63 |
|
64 |
@param run_type: Type of calculation. 'isolated', 'screening', 'refinement'
|
65 |
@param sub_script: script for the job submission.
|
66 |
@param max_qw: Maximum number of simultaneous jobs waiting to be executed.
|
67 |
@param name: name of the project
|
68 |
"""
|
69 |
from shutil import copy |
70 |
from time import sleep |
71 |
from gridtk.tools import qsub # TODO CHANGE TO DRMAA |
72 |
subm_jobs = [] |
73 |
init_dir = os.getcwd() |
74 |
for conf in os.listdir(run_type): |
75 |
i = conf.split('_')[1] |
76 |
while get_jobs_status_sge(subm_jobs).count('q') >= max_qw: |
77 |
sleep(30)
|
78 |
copy(sub_script, f"{run_type}/{conf}")
|
79 |
os.chdir(f"{run_type}/{conf}")
|
80 |
job_name = f'{name[:6].capitalize()}{run_type[:3].capitalize()}{i}'
|
81 |
subm_jobs.append(qsub(sub_script, name=job_name)) |
82 |
os.chdir(init_dir) |
83 |
|
84 |
logger.info('All jobs have been submitted, waiting them to finish.')
|
85 |
while not all([stat == 'f' for stat in get_jobs_status_sge(subm_jobs)]): |
86 |
sleep(30)
|
87 |
logger.info('All jobs have finished.')
|
88 |
|
89 |
|
90 |
def sub_lsf(run_type, sub_script, max_qw, name): |
91 |
pass
|
92 |
|
93 |
|
94 |
def run_calc(run_type, inp_vars, atms_list): |
95 |
"""Directs the calculation run according to the provided arguments.
|
96 |
|
97 |
@param run_type: Type of calculation. 'isolated', 'screening' or
|
98 |
'refinement'
|
99 |
@param inp_vars: Calculation parameters from input file.
|
100 |
@param atms_list: List of ase.Atoms objects containing the sets of atoms
|
101 |
aimed to run the calculations of.
|
102 |
"""
|
103 |
from modules.utilities import check_bak |
104 |
run_types = ['isolated', 'screening', 'refinement'] |
105 |
run_type_err = f"'run_type' must be one of the following: {run_types}"
|
106 |
if not isinstance(run_type, str) or run_type.lower() not in run_types: |
107 |
logger.error(run_type_err) |
108 |
raise ValueError(run_type_err) |
109 |
|
110 |
if inp_vars['batch_q_sys']: |
111 |
logger.info(f"Running {run_type} calculation with {inp_vars['code']} on"
|
112 |
f" {inp_vars['batch_q_sys']}.")
|
113 |
else:
|
114 |
logger.info(f"Doing a dry run of {run_type}.")
|
115 |
check_bak(run_type) |
116 |
os.mkdir(run_type) |
117 |
if inp_vars['code'] == 'cp2k': |
118 |
if run_type == 'isolated': |
119 |
prep_cp2k(inp_vars['isol_inp_file'], run_type, atms_list)
|
120 |
elif run_type == 'screening': |
121 |
prep_cp2k(inp_vars['screen_inp_file'], run_type, atms_list)
|
122 |
elif run_type == 'refinement': |
123 |
prep_cp2k(inp_vars['refine_inp_file'], run_type, atms_list)
|
124 |
# elif: inp_vars['code'] == 'Other codes here'
|
125 |
|
126 |
if inp_vars['batch_q_sys'] == 'sge': |
127 |
sub_sge(run_type, inp_vars['subm_script'], inp_vars['max_qw'], |
128 |
inp_vars['project_name'])
|
129 |
elif inp_vars['batch_q_sys'] == 'lsf': # TODO implement lsf |
130 |
sub_lsf(run_type, inp_vars['subm_script'], inp_vars['max_qw'], |
131 |
inp_vars['project_name'])
|
132 |
elif inp_vars['batch_q_sys'] == 'local': # TODO implement local |
133 |
pass # run_local |
134 |
elif inp_vars['batch_q_sys'] == 'none': |
135 |
pass
|