Coverage for local_installation/dynasor/trajectory/trajectory.py: 95%

153 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2024-06-13 16:00 +0000

1__all__ = ['Trajectory', 'WindowIterator'] 

2 

3import numpy as np 

4 

5from collections import deque 

6from itertools import islice, chain 

7from os.path import isfile 

8from typing import Callable, Dict, Union, List 

9 

10from dynasor.trajectory.atomic_indices import parse_gromacs_index_file 

11from dynasor.trajectory.ase_trajectory_reader import ASETrajectoryReader 

12from dynasor.trajectory.extxyz_trajectory_reader import ExtxyzTrajectoryReader 

13from dynasor.trajectory.lammps_trajectory_reader import LammpsTrajectoryReader 

14from dynasor.trajectory.mdanalysis_trajectory_reader import MDAnalysisTrajectoryReader 

15from dynasor.trajectory.trajectory_frame import TrajectoryFrame 

16from dynasor.logging_tools import logger 

17 

18 

19class Trajectory: 

20 """Instances of this class hold trajectories in a format suitable for 

21 the computation of correlation functions. They behave as 

22 iterators, where each step returns the next frame as a 

23 :class:`TrajectoryFrame` object. The latter hold information 

24 regarding atomic positions, types, and velocities. 

25 

26 Parameters 

27 ---------- 

28 filename 

29 Name of input file 

30 trajectory_format 

31 Type of trajectory. Possible values are: 

32 ``'lammps_internal'``, ``'extxyz'``, ``'ase'`` or one of the formats supported by 

33 `MDAnalysis <https://www.mdanalysis.org/>`_ (except for ``'lammpsdump'``, 

34 which can be called by specifying ``'lammps_mdanalysis'`` to avoid ambiguity) 

35 atomic_indices 

36 Specify which indices belong to which atom type. Can be 

37 (1) a dictionary where the keys specicy species and the values are list of atomic indices, 

38 (2) ``'read_from_trajectory'``, in which case the species are read from the trajectory or 

39 (3) the path to an index file. 

40 length_unit 

41 Length unit of trajectory (``'Angstrom'``, ``'nm'``, ``'pm'``, ``'fm'``). Necessary for 

42 correct conversion to internal dynasor units if the trajectory file does not contain unit 

43 information. 

44 time_unit 

45 Time unit of trajectory (``'fs'``, ``'ps'``, ``'ns'``). Necessary for correct conversion to 

46 internal dynasor units if the trajectory file does not contain unit information. 

47 frame_start 

48 First frame to read; must be larger or equal ``0``. 

49 frame_stop 

50 Last frame to read. By default (``None``) the entire trajectory is read. 

51 frame_step 

52 Read every :attr:`frame_step`-th step of the input trajectory. 

53 By default (``1``) every frame is read. Must be larger than ``0``. 

54 

55 """ 

56 def __init__( 

57 self, 

58 filename: str, 

59 trajectory_format: str, 

60 atomic_indices: Union[str, Dict[str, List[int]]] = None, 

61 length_unit: str = 'Angstrom', 

62 time_unit: str = 'fs', 

63 frame_start: int = 0, 

64 frame_stop: int = None, 

65 frame_step: int = 1 

66 ): 

67 

68 if frame_start < 0: 

69 raise ValueError('frame_start should be positive') 

70 if frame_step < 0: 

71 raise ValueError('frame_step should be positive') 

72 

73 self._frame_number = frame_start 

74 self._frame_step = frame_step 

75 self._frame_stop = frame_stop 

76 

77 # setup trajectory reader 

78 if not isfile(filename): 

79 raise IOError(f'File {filename} does not exist') 

80 self._filename = filename 

81 

82 if trajectory_format == 'lammps_internal': 

83 reader = LammpsTrajectoryReader 

84 elif trajectory_format == 'extxyz': 

85 reader = ExtxyzTrajectoryReader 

86 elif trajectory_format == 'lammps_mdanalysis': 

87 reader = MDAnalysisTrajectoryReader 

88 trajectory_format = 'lammpsdump' 

89 elif trajectory_format == 'ase': 89 ↛ 90line 89 didn't jump to line 90, because the condition on line 89 was never true

90 reader = ASETrajectoryReader 

91 elif trajectory_format == 'lammps': 

92 raise IOError('Ambiguous trajectory format, ' 

93 'did you mean lammps_internal or lammps_mdanalysis?') 

94 else: 

95 reader = MDAnalysisTrajectoryReader 

96 

97 logger.debug(f'Using trajectory reader: {reader.__name__}') 

98 if reader == MDAnalysisTrajectoryReader: 

99 self._reader_obj = reader(self._filename, trajectory_format, 

100 length_unit=length_unit, time_unit=time_unit) 

101 else: 

102 self._reader_obj = reader(self._filename, length_unit=length_unit, time_unit=time_unit) 

103 

104 # Get two frames to set cell etc. 

105 frame0 = next(self._reader_obj) 

106 frame1 = next(self._reader_obj) 

107 self._cell = frame0.cell 

108 self._n_atoms = frame0.n_atoms 

109 

110 # Make sure cell is not changed during consecutive frames 

111 if not np.allclose(frame0.cell, frame1.cell): 

112 raise ValueError('The cell changes between the first and second frame. ' 

113 'The concept of q-points becomes muddy if the simulation cell is ' 

114 'changing, such as during NPT MD simulations, so trajectories where ' 

115 'the cell changes are not supported by dynasor.') 

116 

117 # setup iterator slice (reuse frame0 and frame1 via chain) 

118 self.number_of_frames_read = 0 

119 self._reader_obj = islice(chain([frame0, frame1], self._reader_obj), 

120 frame_start, self._frame_stop, self._frame_step) 

121 

122 # setup atomic indices 

123 if atomic_indices is None: # Default behaviour 

124 atomic_indices = {'X': np.arange(0, self.n_atoms)} 

125 elif isinstance(atomic_indices, str): # Str input 

126 if atomic_indices == 'read_from_trajectory': 

127 if frame0.atom_types is None: 

128 raise ValueError('Could not read atomic indices from the trajectory.') 

129 else: 

130 uniques = np.unique(frame0.atom_types) 

131 atomic_indices = {uniques[i]: (frame0.atom_types == uniques[i]).nonzero()[0] 

132 for i in range(len(uniques))} 

133 else: 

134 atomic_indices = parse_gromacs_index_file(atomic_indices) 

135 elif isinstance(atomic_indices, dict): # Dict input 

136 pass 

137 else: 

138 raise ValueError('Could not understand atomic_indices.') 

139 self._atomic_indices = atomic_indices 

140 

141 # sanity checks for atomic_indices 

142 for key, indices in self._atomic_indices.items(): 

143 if np.max(indices) > self.n_atoms: 

144 raise ValueError('maximum index in atomic_indices exceeds number of atoms') 

145 if np.min(indices) < 0: 

146 raise ValueError('minimum index in atomic_indices is negative') 

147 if '_' in key: 

148 # Since '_' is what we use to distinguish atom types in the results, e.g. Sqw_Cs_Pb 

149 raise ValueError('The char "_" is not allowed in atomic_indices.') 

150 

151 # log info on trajectory and atom types etc 

152 logger.info(f'Trajectory file: {self.filename}') 

153 logger.info(f'Total number of particles: {self.n_atoms}') 

154 logger.info(f'Number of atom types: {len(self.atom_types)}') 

155 for atom_type, indices in self._atomic_indices.items(): 

156 logger.info(f'Number of atoms of type {atom_type}: {len(indices)}') 

157 logger.info(f'Simulation cell (in Angstrom):\n{str(self._cell)}') 

158 

159 def __iter__(self): 

160 return self 

161 

162 def __next__(self): 

163 frame = next(self._reader_obj) 

164 new_frame = TrajectoryFrame(self.atomic_indices, frame.frame_index, frame.positions, 

165 frame.velocities) 

166 self.number_of_frames_read += 1 

167 return new_frame 

168 

169 def __str__(self) -> str: 

170 s = ['Trajectory'] 

171 s += ['{:12} : {}'.format('filename', self.filename)] 

172 s += ['{:12} : {}'.format('natoms', self.n_atoms)] 

173 s += ['{:12} : {}'.format('frame_step', self.frame_step)] 

174 s += ['{:12} : [{}\n {}\n {}]' 

175 .format('cell', self.cell[0], self.cell[1], self.cell[2])] 

176 return '\n'.join(s) 

177 

178 def _repr_html_(self) -> str: 

179 s = [f'<h3>{self.__class__.__name__}</h3>'] 

180 s += ['<table border="1" class="dataframe">'] 

181 s += ['<thead><tr><th style="text-align: left;">Field</th><th>Value</th></tr></thead>'] 

182 s += ['<tbody>'] 

183 s += [f'<tr"><td style="text-align: left;">File name</td><td>{self.filename}</td></tr>'] 

184 s += [f'<tr><td style="text-align: left;">Number of atoms</td><td>{self.n_atoms}</td></tr>'] 

185 s += [f'<tr><td style="text-align: left;">Cell metric</td><td>{self.cell}</td></tr>'] 

186 s += [f'<tr><td style="text-align: left;">Frame step</td><td>{self.frame_step}</td></tr>'] 

187 s += [f'<tr><td style="text-align: left;">Atom types</td><td>{self.atom_types}</td></tr>'] 

188 s += ['</tbody>'] 

189 s += ['</table>'] 

190 return '\n'.join(s) 

191 

192 @property 

193 def cell(self): 

194 """ Simulation cell """ 

195 return self._cell 

196 

197 @property 

198 def n_atoms(self): 

199 """ Number of atoms """ 

200 return self._n_atoms 

201 

202 @property 

203 def filename(self): 

204 """ The trajectory filename """ 

205 return self._filename 

206 

207 @property 

208 def atomic_indices(self): 

209 """ Return copy of index arrays """ 

210 atomic_indices = dict() 

211 for name, inds in self._atomic_indices.items(): 

212 atomic_indices[name] = inds.copy() 

213 return atomic_indices 

214 

215 @property 

216 def atom_types(self) -> List[str]: 

217 return sorted(self._atomic_indices.keys()) 

218 

219 @property 

220 def frame_step(self): 

221 """ Frame to access, trajectory will return every :attr:`frame_step`-th snapshot """ 

222 return self._frame_step 

223 

224 

225def consume(iterator, n): 

226 """ Advance the iterator by :attr:`n` steps. If :attr:`n` is ``None``, consume entirely. """ 

227 # From the python.org 

228 if n is None: 228 ↛ 229line 228 didn't jump to line 229, because the condition on line 228 was never true

229 deque(iterator, maxlen=0) 

230 else: 

231 next(islice(iterator, n, n), None) 

232 

233 

234class WindowIterator: 

235 """Sliding window iterator. 

236 

237 Returns consecutive windows (a window is represented as a list 

238 of objects), created from an input iterator. 

239 

240 Parameters 

241 ---------- 

242 itraj 

243 Trajectory object 

244 width 

245 Length of window (``window_size`` + 1) 

246 window_step 

247 Distance between the start of two consecutive window frames 

248 element_processor 

249 Enables processing each non-discarded object; useful if ``window_step > 

250 width`` and ``map_item`` is expensive (as compared to directly passing 

251 ``map(fun, itraj)`` as ``itraj``); if ``window_step < width``, you could as 

252 well directly pass ``map(fun, itraj)``. 

253 """ 

254 def __init__(self, 

255 itraj: Trajectory, 

256 width: int, 

257 window_step: int = 1, 

258 element_processor: Callable = None): 

259 

260 self._raw_it = itraj 

261 if element_processor: 

262 self._it = map(element_processor, self._raw_it) 

263 else: 

264 self._it = self._raw_it 

265 assert window_step >= 1 

266 assert width >= 1 

267 self.width = width 

268 self.window_step = window_step 

269 self._window = None 

270 

271 def __iter__(self): 

272 return self 

273 

274 def __next__(self): 

275 """ Returns next element in sequence. """ 

276 if self._window is None: 

277 self._window = deque(islice(self._it, self.width), self.width) 

278 else: 

279 if self.window_step >= self.width: 

280 self._window.clear() 

281 consume(self._raw_it, self.window_step - self.width) 

282 else: 

283 for _ in range(min((self.window_step, len(self._window)))): 

284 self._window.popleft() 

285 for f in islice(self._it, min((self.window_step, self.width))): 

286 self._window.append(f) 

287 

288 if len(self._window) == 0: 

289 raise StopIteration 

290 

291 return list(self._window)