Statistiques
| Branche: | Tag: | Révision :

dockonsurf / modules / calculation.py @ f3004731

Historique | Voir | Annoter | Télécharger (2,46 ko)

1
import os
2
import logging
3

    
4
logger = logging.getLogger('DockOnSurf')
5

    
6

    
7
def create_bak_calc_dir(run_type):
8
    """Checks if calculations directory already exists, backs it up if so and
9
    creates an empty one.
10
    @param run_type: Type of calculation. 'isolated', 'screening' or
11
    'refinement'
12
    """
13
    dir_name = run_type
14
    bak_num = 0
15
    while dir_name in os.listdir("."):
16
        bak_num += 1
17
        dir_name = dir_name.split(".")[0] + f".bak{bak_num}"
18
    if bak_num > 0:
19
        os.rename(run_type, dir_name)
20
        logger.warning(f"'{run_type}' directory already present. Moved former "
21
                       f"directory to {dir_name}")
22
    os.mkdir(run_type)
23

    
24

    
25
def prep_cp2k(inp_file, run_type, atms_list):
26
    """Prepares the directories to run isolated calculation with CP2K.
27

28
    @param inp_file: CP2K Input file to run the calculations with.
29
    @param run_type: Type of calculation. 'isolated', 'screening' or
30
        'refinement'
31
    @param atms_list: list of ase.Atoms objects to run the calculation of.
32
    @return: None
33
    """
34
    from shutil import copy
35
    import ase.io
36
    from pycp2k import CP2K
37
    cp2k = CP2K()
38
    cp2k.parse(inp_file)
39
    force_eval = cp2k.CP2K_INPUT.FORCE_EVAL_list[0]
40
    coord_file = force_eval.SUBSYS.TOPOLOGY.Coord_file_name
41

    
42
    # Creating and setting up directories for every atoms configuration.
43
    for i, conf in enumerate(atms_list):
44
        os.mkdir(f'{run_type}/conf_{i}')
45
        copy(inp_file, f'{run_type}/conf_{i}/')
46
        ase.io.write(f'{run_type}/conf_{i}/{coord_file}', conf)
47

    
48

    
49
def run_calc(run_type, inp_vars, atms_list):
50
    """Directs the calculation run according to the provided arguments.
51

52
    @param run_type: Type of calculation. 'isolated', 'screening' or
53
    'refinement'
54
    @param inp_vars: Calculation parameters from input file.
55
    @param atms_list: List of ase.Atoms objects containing the sets of atoms
56
    aimed to run the calculations of.
57
    """
58
    run_types = ['isolated', 'screening', 'refinement']
59
    run_type_err = f"'run_type' should be one of the following: {run_types}"
60
    if not isinstance(run_type, str) or run_type.lower() not in run_types:
61
        logger.error(run_type_err)
62
        raise ValueError(run_type_err)
63

    
64
    logger.info(f"Running {run_type} calculation with {inp_vars['code']} on "
65
                f"{inp_vars['batch_q_sys']}")
66
    create_bak_calc_dir(run_type)
67
    if run_type == 'isolated':
68
        if inp_vars['code'] == 'cp2k':
69
            prep_cp2k(inp_vars['isol_inp_file'], run_type, atms_list)