Discover.py 11.5 KB
Newer Older
1
2
3
4
5
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
6
7
#71 avenue des Martyrs
#38000 Grenoble Cedex 9
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

''' 
Created on Apr 10, 2015

30
:author: Eric C. Pellegrini
31
32
'''

33
import os
34
35
36
37
38
39
40
41
42
43
import collections

import struct

import numpy

from MMTK import Units
from MMTK.ParticleProperties import Configuration, ParticleVector
from MMTK.Trajectory import Trajectory, SnapshotGenerator, TrajectoryOutput

44
45
from MDANSE.Framework.Jobs.Converters.Converter import Converter
from MDANSE.Framework.Jobs.Converters.MaterialsStudio import XTDFile
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224

class HisFile(dict):

    def __init__(self, hisfilename):
                
        self['instance'] = open(hisfilename, "rb")
        
        self.parse_header()
        
    def parse_header(self):
        
        hisfile = self['instance']
        
        # Record 1
        rec = "!4x"
        recSize = struct.calcsize(rec)
        hisfile.read(recSize)
        
        rec = "!i8x"
        self._rec1Size = struct.calcsize(rec)
        rec,hisfile.read(self._rec1Size)
                        
        # Record 2
        rec = "!80sd8x"
        recSize = struct.calcsize(rec)
        VERSION_INFO,VERSION = struct.unpack(rec,hisfile.read(recSize))
        VERSION_INFO = VERSION_INFO.strip()

        # Record 3+4
        rec = "!80s8x80s8x"
        recSize = struct.calcsize(rec)
        self["title"] = struct.unpack(rec,hisfile.read(recSize))
        self["title"] = "\n".join(self["title"])

        # Record 5
        rec = "!i"
        recSize = struct.calcsize(rec)
        N_ATTYP = struct.unpack(rec,hisfile.read(recSize))[0]
        rec = "!" + "%ds" % (4*N_ATTYP) + "%dd" % N_ATTYP + "8x"
        recSize = struct.calcsize(rec)
        hisfile.read(recSize)

        # Record 6
        rec = "!i"
        recSize = struct.calcsize(rec)
        N_NMRES = struct.unpack(rec,hisfile.read(recSize))[0]
        rec = "!" + "%ds" % (4*N_NMRES) + "8x"
        recSize = struct.calcsize(rec)
        hisfile.read(recSize)
        
        # Record 7
        rec = "!i"
        recSize = struct.calcsize(rec)
        self["n_atoms"] = N_ATOMS = struct.unpack(rec,hisfile.read(recSize))[0]
        rec = "!" + "%di" % N_ATOMS
        if VERSION < 2.9:
            rec += "%ds" % (4*N_ATOMS)
        else:
            rec += "%ds" % (5*N_ATOMS)
        rec += "8x"            
        recSize = struct.calcsize(rec)
        hisfile.read(recSize)

        # Record 8
        rec = "!ii"
        recSize = struct.calcsize(rec)
        _, N_MOVAT = struct.unpack(rec,hisfile.read(recSize))
        if VERSION >= 2.6:
            rec = "!" + "%di" % N_MOVAT
            recSize = struct.calcsize(rec)
            self["movable_atoms"] = numpy.array(struct.unpack(rec,hisfile.read(recSize)), dtype=numpy.int32) - 1
        else:
            self["movable_atoms"] = range(self["n_atoms"])
        rec = "!8x"
        recSize = struct.calcsize(rec)
        hisfile.read(recSize)
        
        # Record 9
        rec = "!i"
        recSize = struct.calcsize(rec)
        N_MOL = struct.unpack(rec,hisfile.read(recSize))[0]
        rec = "!" + "%di" % N_MOL + "%di" % N_MOL + "8x"
        recSize = struct.calcsize(rec)
        hisfile.read(recSize)
        
        # Record 10
        rec = "!i"
        recSize = struct.calcsize(rec)
        N_RES = struct.unpack(rec,hisfile.read(recSize))[0]
        rec = "!" + "%di" % (2*N_RES) + "%di" % N_RES + "8x"
        recSize = struct.calcsize(rec)
        hisfile.read(recSize)

        # Record 11
        rec = "!i"
        recSize = struct.calcsize(rec)
        N_BONDS = struct.unpack(rec,hisfile.read(recSize))[0]
        if N_BONDS > 0:
            rec = "!" + "%di" % (2*N_BONDS)
            recSize = struct.calcsize(rec)
            _ = struct.unpack(rec,hisfile.read(recSize))
        rec = "!8x"
        recSize = struct.calcsize(rec)
        hisfile.read(recSize)

        # Record 12
        rec = "!6d"
        recSize = struct.calcsize(rec)
        _ = struct.unpack(rec,hisfile.read(recSize))
        rec = "!9d"
        recSize = struct.calcsize(rec)
        self["initial_cell"] = numpy.reshape(numpy.array(struct.unpack(rec,hisfile.read(recSize)),dtype=numpy.float64),(3,3))*Units.Ang
        
        rec = "!4134di4di6d6i8x"
        recSize = struct.calcsize(rec)
        hisfile.read(recSize)
        
        # Record 13
        rec = "!idii8x"
        recSize = struct.calcsize(rec)
        N_ENER, TIME_STEP, _, _ = struct.unpack(rec,hisfile.read(recSize))
        self["time_step"] = TIME_STEP*Units.fs

        # Record 14
        rec = "!3d" + "%dd" % N_ENER + "%dd" % N_MOL + "%dd" % (N_MOL*N_ENER) + "%dd" % (4*N_MOL+2+54) + "8x"
        self._rec14Size = struct.calcsize(rec)
        hisfile.read(self._rec14Size)

        # Record 15
        rec = "!" + "%df" % (3*N_ATOMS) + "8x"
        recSize = struct.calcsize(rec)
        self["initial_coordinates"] = numpy.reshape(struct.unpack(rec,hisfile.read(recSize)),(N_ATOMS,3))
            
        # Record 16
        rec = "!" + "%df" % (3*N_ATOMS) + "8x"
        recSize = struct.calcsize(rec)
        self["initial_velocities"] = numpy.reshape(struct.unpack(rec,hisfile.read(recSize)),(N_ATOMS,3))

        self._headerSize = hisfile.tell()
                                
        self._recN2 = "!15d8x"
        self._recN2Size = struct.calcsize(self._recN2)
                                
        if VERSION < 2.6:
            self["n_movable_atoms"] = N_ATOMS
        else:
            self["n_movable_atoms"] = N_MOVAT
        self._recCoord = "!" + "%df" % (3*self["n_movable_atoms"]) + "8x"
        self._recCoordSize = struct.calcsize(self._recCoord)
        self._recVel = "!" + "%df" % (3*self["n_movable_atoms"]) + "8x"
        self._recVelSize = struct.calcsize(self._recVel)
        
        self._frameSize = self._rec1Size + self._rec14Size + self._recN2Size + self._recCoordSize + self._recVelSize

        hisfile.seek(0,2)
        
        self["n_frames"] = (hisfile.tell()-self._headerSize)/self._frameSize
                
    def read_step(self, index):
        
        hisfile = self["instance"]
        
        time = index*self["time_step"]
        
        hisfile.seek(self._headerSize + index*self._frameSize + self._rec1Size + self._rec14Size)
        
        cell = numpy.reshape(numpy.array(struct.unpack(self._recN2,hisfile.read(self._recN2Size))[6:]),(3,3))*Units.Ang
        
        coords = numpy.reshape(struct.unpack(self._recCoord,hisfile.read(self._recCoordSize)),(self["n_movable_atoms"],3))
        coords *= Units.Ang
        
        vels = numpy.reshape(struct.unpack(self._recVel,hisfile.read(self._recVelSize)),(self["n_movable_atoms"],3))
        vels *= Units.Ang/Units.fs
        
        return time, cell, coords, vels
        
    def close(self):
        self["instance"].close()
        
225
class DiscoverConverter(Converter):
226
    """
eric pellegrini's avatar
eric pellegrini committed
227
    Converts a Discover trajectory to a MMTK trajectory.
228
229
230
231
232
233
    """

    type = 'discover'
    
    label = "Discover"

eric pellegrini's avatar
eric pellegrini committed
234
    category = ('Converters','Materials Studio')
235
    
236
    ancestor = None
237

238
    settings = collections.OrderedDict()
239
240
    settings['xtd_file'] = ('input_file',{'default':os.path.join('..','..','..','Data','Trajectories','Discover','sushi.xtd')})
    settings['his_file'] = ('input_file',{'default':os.path.join('..','..','..','Data','Trajectories','Discover','sushi.his')})
241
    settings['output_file'] = ('output_files', {'formats':["netcdf"]})
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
    
    def initialize(self):
        '''
        Initialize the job.
        '''
        
        self._xtdfile = XTDFile(self.configuration["xtd_file"]["filename"])
        
        self._xtdfile.build_universe()
        
        self._universe = self._xtdfile.universe
        
        self._hisfile = HisFile(self.configuration["his_file"]["filename"])

        # The number of steps of the analysis.
        self.numberOfSteps = self._hisfile['n_frames']
                             
        if self._universe.is_periodic:
            self._universe.setShape(self._hisfile['initial_cell'])

        conf = Configuration(self._universe, self._hisfile["initial_coordinates"])
        self._universe.setConfiguration(conf)        
                             
        self._universe.initializeVelocitiesToTemperature(0.)
        self._velocities = ParticleVector(self._universe)
        self._velocities.array = self._hisfile["initial_velocities"]
        self._universe.setVelocities(self._velocities)
        
        self._universe.foldCoordinatesIntoBox()
            
        # A MMTK trajectory is opened for writing.
        self._trajectory = Trajectory(self._universe, self.configuration['output_file']['files'][0], mode='w', comment=self._hisfile["title"])

        # A frame generator is created.
        self._snapshot = SnapshotGenerator(self._universe, actions = [TrajectoryOutput(self._trajectory, ["all"], 0, None, 1)])
        
    def run_step(self, index):
        """Runs a single step of the job.
        
        @param index: the index of the step.
        @type index: int.

        @note: the argument index is the index of the loop note the index of the frame.      
        """
                                                
        # The x, y and z values of the current frame.
        time, cell, config, vel = self._hisfile.read_step(index)
        
        # If the universe is periodic set its shape with the current dimensions of the unit cell.
        if self._universe.is_periodic:
            self._universe.setShape(cell)
            
        movableAtoms = self._hisfile['movable_atoms']
            
        self._universe.configuration().array[movableAtoms,:] = config
                   
        self._universe.foldCoordinatesIntoBox()
        
        data = {"time" : time}
        
        self._velocities.array[movableAtoms,:] = vel
        self._universe.setVelocities(self._velocities)

        # Store a snapshot of the current configuration in the output trajectory.
        self._snapshot(data=data)
                                                                        
        return index, None
        
    def combine(self, index, x):
        """
        @param index: the index of the step.
        @type index: int.
        
        @param x:
        @type x: any.
        """
        
        pass
    
    def finalize(self):
        """
        Finalize the job.
        """
        
        self._hisfile.close()

        # Close the output trajectory.
        self._trajectory.close()