Coverage for biobb_gromacs/gromacs/mdrun_multidir.py: 0%

43 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-07-27 16:28 +0000

1#!/usr/bin/env python3 

2 

3"""Module containing the MdrunMultidir class and the command line interface.""" 

4import os 

5import shutil 

6from typing import Optional 

7from pathlib import PurePath 

8from biobb_common.tools.file_utils import launchlogger 

9from biobb_gromacs.gromacs.mdrun_base import MdrunBase 

10 

11 

12class MdrunMultidir(MdrunBase): 

13 """ 

14 | biobb_gromacs MdrunMultidir 

15 | Wrapper of the `GROMACS mdrun <http://manual.gromacs.org/current/onlinehelp/gmx-mdrun.html>`_ module for `multidir setups <https://manual.gromacs.org/current/user-guide/mdrun-features.html#running-multi-simulations>`_. 

16 | MDRun is the main computational chemistry engine within GROMACS. It performs Molecular Dynamics simulations, but it can also perform Stochastic Dynamics, Energy Minimization, test particle insertion or (re)calculation of energies. 

17 

18 Args: 

19 input_tpr_path (str): Path to the portable binary run input file TPR in each of the subdirectories. File type: input. `Sample file <https://github.com/bioexcel/biobb_gromacs/raw/master/biobb_gromacs/test/data/gromacs/mdrun.tpr>`_. Accepted formats: tpr (edam:format_2333). 

20 input_multifolder (dir): Path to the folder with all subdirectories for the multidir setup. File type: input. Accepted formats: directory (edam:format_1915) 

21 output_multifolder (dir): Folder where the generated output files will be saved. File type: output. Accepted formats: directory (edam:format_1915) 

22 properties (dict - Python dictionary object containing the tool parameters, not input/output files): 

23 * **mpi_bin** (*str*) - (None) Path to the MPI runner. Usually "mpirun" or "srun". 

24 * **mpi_np** (*int*) - (0) [0~1000|1] Number of MPI processes. Usually an integer bigger than 1. 

25 * **mpi_flags** (*str*) - (None) Path to the MPI hostlist file. 

26 * **noappend** (*bool*) - (False) Include the noappend flag to open new output files and add the simulation part number to all output file names 

27 * **num_threads** (*int*) - (0) [0~1000|1] Let GROMACS guess. The number of threads that are going to be used. 

28 * **num_threads_mpi** (*int*) - (0) [0~1000|1] Let GROMACS guess. The number of GROMACS MPI threads that are going to be used. 

29 * **num_threads_omp** (*int*) - (0) [0~1000|1] Let GROMACS guess. The number of GROMACS OPENMP threads that are going to be used. 

30 * **num_threads_omp_pme** (*int*) - (0) [0~1000|1] Let GROMACS guess. The number of GROMACS OPENMP_PME threads that are going to be used. 

31 * **use_gpu** (*bool*) - (False) Use settings appropriate for GPU. Adds: -nb gpu -pme gpu 

32 * **gpu_id** (*str*) - (None) list of unique GPU device IDs available to use. 

33 * **gpu_tasks** (*str*) - (None) list of GPU device IDs, mapping each PP task on each node to a device. 

34 * **gmx_lib** (*str*) - (None) Path set GROMACS GMXLIB environment variable. 

35 * **binary_path** (*str*) - ("gmx_mpi") Path to the GROMACS executable binary. 

36 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files. 

37 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist. 

38 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory. 

39 * **container_path** (*str*) - (None) Path to the binary executable of your container. 

40 * **container_image** (*str*) - (None) Container Image identifier. 

41 * **container_volume_path** (*str*) - ("/data") Path to an internal directory in the container. 

42 * **container_working_dir** (*str*) - (None) Path to the internal CWD in the container. 

43 * **container_user_id** (*str*) - (None) User number id to be mapped inside the container. 

44 * **container_shell_path** (*str*) - ("/bin/bash") Path to the binary executable of the container shell. 

45 

46 Examples: 

47 This is a use example of how to use the building block from Python:: 

48 

49 from biobb_gromacs.gromacs.mdrun_multidir import mdrun_multidir 

50 prop = { 'num_threads': 0, 

51 'binary_path': 'gmx_bin' } 

52 mdrun_multidir(input_tpr_path='/path/to/myPortableBinaryRunInputFile.tpr', 

53 input_multifolder='/path/to/inputMultidirFolder', 

54 output_multifolder='/path/to/outputMultidirFolder', 

55 properties=prop) 

56 

57 Info: 

58 * wrapped_software: 

59 * name: GROMACS MdrunMultidir 

60 * version: 2025.2 

61 * license: LGPL 2.1 

62 * multinode: mpi 

63 * ontology: 

64 * name: EDAM 

65 * schema: http://edamontology.org/EDAM.owl 

66 """ 

67 

68 def __init__(self, 

69 input_tpr_path: str, 

70 input_multifolder: str, 

71 output_multifolder: str, 

72 properties: Optional[dict] = None, 

73 **kwargs) -> None: 

74 properties = properties or {} 

75 

76 # Call parent class constructor 

77 super().__init__(properties) 

78 self.locals_var_dict = locals().copy() 

79 

80 # Input/Output files 

81 self.io_dict = { 

82 "in": { 

83 "input_tpr_path": input_tpr_path, 

84 "input_multifolder": input_multifolder 

85 }, 

86 "out": { 

87 "output_multifolder": output_multifolder, 

88 } 

89 } 

90 

91 # Properties specific for BB 

92 self._init_common_properties(properties) 

93 self.binary_path: str = properties.get('binary_path', 'gmx_bin') 

94 

95 # Check the properties 

96 self.check_properties(properties) 

97 self.check_arguments() 

98 

99 @launchlogger 

100 def launch(self) -> int: 

101 """Execute the :class:`MdrunMultidir <gromacs.mdrun_multidir.MdrunMultidir>` object.""" 

102 

103 # Setup Biobb 

104 if self.check_restart(): 

105 return 0 

106 self.stage_files() 

107 

108 working_dir = self._get_working_dir() 

109 

110 multifolder_path = self.stage_io_dict["in"]["input_multifolder"] 

111 subdirs = sorted( 

112 entry for entry in os.listdir(multifolder_path) 

113 if os.path.isdir(os.path.join(multifolder_path, entry)) 

114 ) 

115 self.cmd = [ 

116 self.binary_path, 'mdrun', 

117 '-s', PurePath(self.stage_io_dict["in"]["input_tpr_path"]).name, 

118 '-multidir', ' '.join(subdirs), 

119 ] 

120 

121 # Shared mpi / working-directory / runtime flags. multidir also needs to 

122 # cd into the multifolder so the -multidir subdir names resolve. 

123 self._prepend_mpi_runner() 

124 self.cmd = ["cd", working_dir, ";", 'cd', 

125 PurePath(self.stage_io_dict["in"]["input_multifolder"]).name, ";"] + self.cmd 

126 self._append_gmx_runtime_flags() 

127 

128 # Run Biobb block 

129 self.run_biobb() 

130 

131 # Move files to output folder. When the input and output multifolders 

132 # share a basename they stage to the same sandbox path, so the move is 

133 # a no-op and must be skipped to avoid "move into itself" errors. 

134 input_multifolder = self.stage_io_dict["in"]["input_multifolder"] 

135 output_multifolder = self.stage_io_dict["out"]["output_multifolder"] 

136 if os.path.abspath(input_multifolder) != os.path.abspath(output_multifolder): 

137 shutil.copytree(input_multifolder, output_multifolder) 

138 # Copy files to host 

139 self.copy_to_host() 

140 

141 # Remove temporal files 

142 self.remove_tmp_files() 

143 

144 self.check_arguments(output_files_created=True, raise_exception=False) 

145 return self.return_code 

146 

147 

148def mdrun_multidir(input_tpr_path: str, input_multifolder: str, output_multifolder: str, 

149 properties: Optional[dict] = None, **kwargs) -> int: 

150 """Create :class:`MdrunMultidir <gromacs.mdrun_multidir.MdrunMultidir>` class and 

151 execute the :meth:`launch() <gromacs.mdrun_multidir.MdrunMultidir.launch>` method.""" 

152 return MdrunMultidir(**dict(locals())).launch() 

153 

154 

155mdrun_multidir.__doc__ = MdrunMultidir.__doc__ 

156main = MdrunMultidir.get_main(mdrun_multidir, "Wrapper for the GROMACS mdrun module with multidir support.") 

157 

158 

159if __name__ == '__main__': 

160 main()