Coverage for biobb_gromacs/gromacs/mdrun.py: 75%

53 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 MDrun class and the command line interface.""" 

4from typing import Optional 

5from pathlib import PurePath 

6from biobb_common.tools.file_utils import launchlogger 

7from biobb_gromacs.gromacs.mdrun_base import MdrunBase 

8 

9 

10class Mdrun(MdrunBase): 

11 """ 

12 | biobb_gromacs Mdrun 

13 | Wrapper of the `GROMACS mdrun <http://manual.gromacs.org/current/onlinehelp/gmx-mdrun.html>`_ module. 

14 | 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. 

15 

16 Args: 

17 input_tpr_path (str): Path to the portable binary run input file TPR. 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). 

18 output_gro_path (str): Path to the output GROMACS structure GRO file. File type: output. `Sample file <https://github.com/bioexcel/biobb_gromacs/raw/master/biobb_gromacs/test/reference/gromacs/ref_mdrun.gro>`_. Accepted formats: gro (edam:format_2033). 

19 output_edr_path (str): Path to the output GROMACS portable energy file EDR. File type: output. `Sample file <https://github.com/bioexcel/biobb_gromacs/raw/master/biobb_gromacs/test/reference/gromacs/ref_mdrun.edr>`_. Accepted formats: edr (edam:format_2330). 

20 output_log_path (str): Path to the output GROMACS trajectory log file LOG. File type: output. `Sample file <https://github.com/bioexcel/biobb_gromacs/raw/master/biobb_gromacs/test/reference/gromacs/ref_mdrun.log>`_. Accepted formats: log (edam:format_2330). 

21 output_trr_path (str) (Optional): Path to the GROMACS uncompressed raw trajectory file TRR. File type: output. `Sample file <https://github.com/bioexcel/biobb_gromacs/raw/master/biobb_gromacs/test/reference/gromacs/ref_mdrun.trr>`_. Accepted formats: trr (edam:format_3910). 

22 input_cpt_path (str) (Optional): Path to the input GROMACS checkpoint file CPT. File type: input. Accepted formats: cpt (edam:format_2333). 

23 output_xtc_path (str) (Optional): Path to the GROMACS compressed trajectory file XTC. File type: output. Accepted formats: xtc (edam:format_3875). 

24 output_cpt_path (str) (Optional): Path to the output GROMACS checkpoint file CPT. File type: output. Accepted formats: cpt (edam:format_2333). 

25 output_dhdl_path (str) (Optional): Path to the output dhdl.xvg file only used when free energy calculation is turned on. File type: output. Accepted formats: xvg (edam:format_2033). 

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

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

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

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

30 * **checkpoint_time** (*int*) - (15) [0~1000|1] Checkpoint writing interval in minutes. Only enabled if an output_cpt_path is provided. 

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

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

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

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

35 * **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. 

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

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

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

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

40 * **binary_path** (*str*) - ("gmx") Path to the GROMACS executable binary. 

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

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

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

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

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

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

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

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

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

50 

51 Examples: 

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

53 

54 from biobb_gromacs.gromacs.mdrun import mdrun 

55 prop = { 'num_threads': 0, 

56 'binary_path': 'gmx' } 

57 mdrun(input_tpr_path='/path/to/myPortableBinaryRunInputFile.tpr', 

58 output_trr_path='/path/to/newTrajectory.trr', 

59 output_gro_path='/path/to/newStructure.gro', 

60 output_edr_path='/path/to/newEnergy.edr', 

61 output_log_path='/path/to/newSimulationLog.log', 

62 properties=prop) 

63 

64 Info: 

65 * wrapped_software: 

66 * name: GROMACS Mdrun 

67 * version: 2025.2 

68 * license: LGPL 2.1 

69 * multinode: mpi 

70 * ontology: 

71 * name: EDAM 

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

73 """ 

74 

75 def __init__(self, input_tpr_path: str, output_gro_path: str, output_edr_path: str, 

76 output_log_path: str, output_trr_path: Optional[str] = None, input_cpt_path: Optional[str] = None, 

77 output_xtc_path: Optional[str] = None, output_cpt_path: Optional[str] = None, 

78 output_dhdl_path: Optional[str] = None, properties: Optional[dict] = None, **kwargs) -> None: 

79 properties = properties or {} 

80 

81 # Call parent class constructor 

82 super().__init__(properties) 

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

84 

85 # Input/Output files 

86 self.io_dict = { 

87 "in": {"input_tpr_path": input_tpr_path, "input_cpt_path": input_cpt_path}, 

88 "out": {"output_trr_path": output_trr_path, "output_gro_path": output_gro_path, 

89 "output_edr_path": output_edr_path, "output_log_path": output_log_path, 

90 "output_xtc_path": output_xtc_path, "output_cpt_path": output_cpt_path, 

91 "output_dhdl_path": output_dhdl_path} 

92 } 

93 

94 # Properties specific for BB 

95 self._init_common_properties(properties) 

96 

97 # Check the properties 

98 self.check_properties(properties) 

99 self.check_arguments() 

100 

101 @launchlogger 

102 def launch(self) -> int: 

103 """Execute the :class:`Mdrun <gromacs.mdrun.Mdrun>` object.""" 

104 

105 # Setup Biobb 

106 if self.check_restart(): 

107 return 0 

108 

109 self.stage_files() 

110 

111 working_dir = self._get_working_dir() 

112 

113 self.cmd = [self.binary_path, 'mdrun', 

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

115 '-c', PurePath(self.stage_io_dict["out"]["output_gro_path"]).name, 

116 '-e', PurePath(self.stage_io_dict["out"]["output_edr_path"]).name, 

117 '-g', PurePath(self.stage_io_dict["out"]["output_log_path"]).name] 

118 

119 if self.stage_io_dict["out"].get("output_trr_path"): 

120 self.cmd.append('-o') 

121 self.cmd.append(PurePath(self.stage_io_dict["out"]["output_trr_path"]).name) 

122 

123 if self.stage_io_dict["in"].get("input_cpt_path"): 

124 self.cmd.append('-cpi') 

125 self.cmd.append(PurePath(self.stage_io_dict["in"]["input_cpt_path"]).name) 

126 if self.stage_io_dict["out"].get("output_xtc_path"): 

127 self.cmd.append('-x') 

128 self.cmd.append(PurePath(self.stage_io_dict["out"]["output_xtc_path"]).name) 

129 else: 

130 self.tmp_files.append('traj_comp.xtc') 

131 if self.stage_io_dict["out"].get("output_cpt_path"): 

132 self.cmd.append('-cpo') 

133 self.cmd.append(PurePath(self.stage_io_dict["out"]["output_cpt_path"]).name) 

134 if self.checkpoint_time: 

135 self.cmd.append('-cpt') 

136 self.cmd.append(str(self.checkpoint_time)) 

137 if self.stage_io_dict["out"].get("output_dhdl_path"): 

138 self.cmd.append('-dhdl') 

139 self.cmd.append(PurePath(self.stage_io_dict["out"]["output_dhdl_path"]).name) 

140 

141 # Shared mpi / working-directory / runtime flags 

142 self._prepend_mpi_runner() 

143 self.cmd = ["cd", working_dir, ";"] + self.cmd 

144 self._append_gmx_runtime_flags() 

145 

146 # Run Biobb block 

147 self.run_biobb() 

148 

149 # Copy files to host 

150 self.copy_to_host() 

151 

152 # Remove temporal files 

153 self.remove_tmp_files() 

154 

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

156 return self.return_code 

157 

158 

159def mdrun(input_tpr_path: str, output_gro_path: str, output_edr_path: str, 

160 output_log_path: str, output_trr_path: Optional[str] = None, input_cpt_path: Optional[str] = None, 

161 output_xtc_path: Optional[str] = None, output_cpt_path: Optional[str] = None, 

162 output_dhdl_path: Optional[str] = None, properties: Optional[dict] = None, **kwargs) -> int: 

163 """Create :class:`Mdrun <gromacs.mdrun.Mdrun>` class and 

164 execute the :meth:`launch() <gromacs.mdrun.Mdrun.launch>` method.""" 

165 return Mdrun(**dict(locals())).launch() 

166 

167 

168mdrun.__doc__ = Mdrun.__doc__ 

169main = Mdrun.get_main(mdrun, "Wrapper for the GROMACS mdrun module.") 

170 

171 

172if __name__ == '__main__': 

173 main()