dockonsurf / modules / calculation.py @ a5f73b4c
Historique | Voir | Annoter | Télécharger (4,84 ko)
1 |
import os |
---|---|
2 |
import logging |
3 |
|
4 |
logger = logging.getLogger('DockOnSurf')
|
5 |
|
6 |
|
7 |
def check_bak(file_name): |
8 |
"""Checks if a file already exists and backs it up if so.
|
9 |
@param file_name: file to be checked if exists
|
10 |
"""
|
11 |
new_name = file_name |
12 |
bak_num = 0
|
13 |
while new_name in os.listdir("."): |
14 |
bak_num += 1
|
15 |
new_name = new_name.split(".bak")[0] + f".bak{bak_num}" |
16 |
if bak_num > 0: |
17 |
os.rename(file_name, new_name) |
18 |
logger.warning(f"'{file_name}' already present. Backed it up to "
|
19 |
f"{new_name}")
|
20 |
|
21 |
|
22 |
def prep_cp2k(inp_file, run_type, atms_list): |
23 |
"""Prepares the directories to run isolated calculation with CP2K.
|
24 |
|
25 |
@param inp_file: CP2K Input file to run the calculations with.
|
26 |
@param run_type: Type of calculation. 'isolated', 'screening' or
|
27 |
'refinement'
|
28 |
@param atms_list: list of ase.Atoms objects to run the calculation of.
|
29 |
@return: None
|
30 |
"""
|
31 |
from shutil import copy |
32 |
import ase.io |
33 |
from pycp2k import CP2K |
34 |
cp2k = CP2K() |
35 |
cp2k.parse(inp_file) |
36 |
force_eval = cp2k.CP2K_INPUT.FORCE_EVAL_list[0]
|
37 |
if force_eval.SUBSYS.TOPOLOGY.Coord_file_name is None: |
38 |
logger.warning("'COORD_FILE_NAME' not specified on CP2K input. Using "
|
39 |
"default name 'coord.xyz', a new CP2K input file with "
|
40 |
"the 'COORD_FILE_NAME' variable is created and older "
|
41 |
"file is backed up with the following name:")
|
42 |
force_eval.SUBSYS.TOPOLOGY.Coord_file_name = 'coord.xyz'
|
43 |
check_bak(inp_file) |
44 |
cp2k.write_input_file(inp_file) |
45 |
|
46 |
coord_file = force_eval.SUBSYS.TOPOLOGY.Coord_file_name |
47 |
|
48 |
# Creating and setting up directories for every configuration.
|
49 |
for i, conf in enumerate(atms_list): |
50 |
os.mkdir(f'{run_type}/conf_{i}')
|
51 |
copy(inp_file, f'{run_type}/conf_{i}/')
|
52 |
ase.io.write(f'{run_type}/conf_{i}/{coord_file}', conf)
|
53 |
|
54 |
|
55 |
def get_jobs_status_sge(job_ids): # TODO more elegant |
56 |
"""Returns a list of job status for a list of job ids.
|
57 |
|
58 |
@param job_ids: list of all jobs to be checked their status.
|
59 |
@return: list of status for every job.
|
60 |
"""
|
61 |
from gridtk.tools import qstat |
62 |
run_chk = 'usage 1'
|
63 |
status_list = [] |
64 |
for job in job_ids: |
65 |
if run_chk in qstat(job): |
66 |
status_list.append('r')
|
67 |
elif len(qstat(job)) > 0: |
68 |
status_list.append('q')
|
69 |
else:
|
70 |
status_list.append('f')
|
71 |
return status_list
|
72 |
|
73 |
|
74 |
def submit(run_type, q_sys, sub_script, max_qw, name): |
75 |
"""Submits jobs to the relevant queuing system with the provided script
|
76 |
|
77 |
@param run_type: Type of calculation. 'isolated', 'screening' or
|
78 |
'refinement'
|
79 |
@param q_sys: Batch queuing system used
|
80 |
@param sub_script: script for the job submission.
|
81 |
@param max_qw: Maximum number of simultaneous jobs waiting to be executed.
|
82 |
@param name: name of the project
|
83 |
"""
|
84 |
from shutil import copy |
85 |
from time import sleep |
86 |
subm_jobs = [] |
87 |
if q_sys == 'sge': # TODO Independent functions for every batch_q_sys |
88 |
from gridtk.tools import qsub # TODO CHANGE TO DRMAA |
89 |
init_dir = os.getcwd() |
90 |
for conf in os.listdir(run_type): |
91 |
i = conf.split('_')[1] |
92 |
while get_jobs_status_sge(subm_jobs).count('q') >= max_qw: |
93 |
sleep(30)
|
94 |
copy(sub_script, f"{run_type}/{conf}")
|
95 |
os.chdir(f"{run_type}/{conf}")
|
96 |
job_name = f'{name[:6].capitalize()}{run_type[:3].capitalize()}{i}'
|
97 |
subm_jobs.append(qsub(sub_script, name=job_name)) |
98 |
os.chdir(init_dir) |
99 |
|
100 |
logger.info('All jobs have been submitted, waiting them to finish')
|
101 |
while not all([stat == 'f' for stat in get_jobs_status_sge(subm_jobs)]): |
102 |
sleep(30)
|
103 |
logger.info('All jobs have finished')
|
104 |
|
105 |
|
106 |
def run_calc(run_type, inp_vars, atms_list): |
107 |
"""Directs the calculation run according to the provided arguments.
|
108 |
|
109 |
@param run_type: Type of calculation. 'isolated', 'screening' or
|
110 |
'refinement'
|
111 |
@param inp_vars: Calculation parameters from input file.
|
112 |
@param atms_list: List of ase.Atoms objects containing the sets of atoms
|
113 |
aimed to run the calculations of.
|
114 |
"""
|
115 |
run_types = ['isolated', 'screening', 'refinement'] |
116 |
run_type_err = f"'run_type' must be one of the following: {run_types}"
|
117 |
if not isinstance(run_type, str) or run_type.lower() not in run_types: |
118 |
logger.error(run_type_err) |
119 |
raise ValueError(run_type_err) |
120 |
|
121 |
logger.info(f"Running {run_type} calculation with {inp_vars['code']} on "
|
122 |
f"{inp_vars['batch_q_sys']}")
|
123 |
check_bak(run_type) |
124 |
os.mkdir(run_type) |
125 |
if inp_vars['code'] == 'cp2k': |
126 |
prep_cp2k(inp_vars['isol_inp_file'], run_type, atms_list)
|
127 |
# elif: inp_vars['code'] == 'Other codes here'
|
128 |
|
129 |
submit(run_type, inp_vars['batch_q_sys'], inp_vars['subm_script'], |
130 |
inp_vars['max_qw'], inp_vars['project_name']) |