Forcite.py 13.1 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 collections
34
import os
35
36
37
38
39
40
41
42
43
import struct

import numpy

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

from MDANSE.Externals.magnitude.magnitude import mg
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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

FORCE_FACTOR = mg(1.0,"kcal_per_mole/ang","uma nm/ps2").toval()

class TrjFile(dict):

    def __init__(self, trjfilename):
                
        self['instance'] = open(trjfilename, "rb")
        
        self.parse_header()
        
    def parse_header(self):
        
        trjfile = self['instance']
        
        rec = "!4x"
        recSize = struct.calcsize(rec)
        trjfile.read(recSize)
        
        # Record 1
        rec = "!4s20i8x"
        recSize = struct.calcsize(rec)
        DATA = struct.unpack(rec,trjfile.read(recSize))
        VERSION = DATA[1]
        if VERSION < 2010:
            self._fp = "f"
        else:
            self._fp = "d"
            
        # Diff with doc --> NTRJTI and TRJTIC not in doc
        rec = '!i'
        recSize = struct.calcsize(rec)
        NTRJTI, = struct.unpack(rec, trjfile.read(recSize))
        rec = '!%ds8x' % (80*NTRJTI)
        recSize = struct.calcsize(rec)
        self["title"] = struct.unpack(rec, trjfile.read(recSize))
        self["title"] = "".join(self["title"])
        
        # Record 2
        rec = "!i"
        recSize = struct.calcsize(rec)
        NEEXTI = struct.unpack(rec,trjfile.read(recSize))[0]
        rec = "!" + "%ds" % (80*NEEXTI) + '8x'
        recSize = struct.calcsize(rec)
        trjfile.read(recSize)

        # Record 3
        rec = "!8i8x"
        recSize = struct.calcsize(rec)
        PERTYPE,_,LCANON,DEFCEL,_,_,LNPECAN,LTMPDAMP = struct.unpack(rec,trjfile.read(recSize))
        self["pertype"] = PERTYPE
        self["defcel"] = DEFCEL

        # Record 4
        rec = "!i"
        recSize = struct.calcsize(rec)
        NFLUSD = struct.unpack(rec,trjfile.read(recSize))[0]

        rec = '!%di%di%ds8x' % (NFLUSD,NFLUSD,8*NFLUSD)
        recSize = struct.calcsize(rec)
        trjfile.read(recSize)

        rec = '!i'
        recSize = struct.calcsize(rec)
        self["totmov"] = struct.unpack(rec,trjfile.read(recSize))[0]

        rec = '!%di8x' % self["totmov"]
        recSize = struct.calcsize(rec)
        self["mvofst"] = numpy.array(struct.unpack(rec,trjfile.read(recSize)), dtype=numpy.int32) - 1

        # Record 4a
        rec = "!i"
        recSize = struct.calcsize(rec)
        LEEXTI, = struct.unpack(rec,trjfile.read(recSize))
        rec = "!%ds8x" % LEEXTI
        recSize = struct.calcsize(rec)
        trjfile.read(recSize)        
        
        # Record 4b
        rec = "!i"
        recSize = struct.calcsize(rec)
        LPARTI, = struct.unpack(rec,trjfile.read(recSize))
        rec = "!%ds8x" % LPARTI
        recSize = struct.calcsize(rec)
        trjfile.read(recSize)        

        self._headerSize = trjfile.tell()

        # Frame record 1        
        if VERSION == 2000:
            rec1 = '!%si33%s5i8x' % (self._fp,self._fp)
        elif VERSION == 2010:
            rec1 = '!%si57%s6i8x' % (self._fp,self._fp)
        else:
            rec1 = '!%si58%s6i8x' % (self._fp,self._fp)

        recSize = struct.calcsize(rec1)
        DATA = struct.unpack(rec1,trjfile.read(recSize))
        
        if VERSION < 2010:
            self["velocities_written"] = DATA[-3]
            self["forces_written"] = 0
        else:
            self["velocities_written"] = DATA[-4]
            self["forces_written"] = DATA[-3]
        
        # Frame record 2        
        rec = '!12%s8x' % self._fp
        recSize = struct.calcsize(rec)
        trjfile.read(recSize)
        
        # Frame record 3    
        if LCANON:
            rec = '!4%s8x' % self._fp
            recSize = struct.calcsize(rec)
            trjfile.read(recSize)
        
        if PERTYPE > 0:
            # Frame record 4
            self._defCellRecPos = trjfile.tell() - self._headerSize
            self._defCellRec = '!22%s8x' % self._fp
            self._defCellRecSize = struct.calcsize(self._defCellRec)
            trjfile.read(self._defCellRecSize)

        if PERTYPE > 0:
            # Frame record 5
            rec = '!i14%s8x' % self._fp
            recSize = struct.calcsize(rec)
            trjfile.read(recSize)

        if LNPECAN:
            # Frame record 6
            rec = '!3%s8x' % self._fp
            recSize = struct.calcsize(rec)
            trjfile.read(recSize)
            
        if LTMPDAMP:
            # Frame record 7
            rec = '!%s8x' % self._fp
            recSize = struct.calcsize(rec)
            trjfile.read(recSize)
            
        self._configRecPos = trjfile.tell() - self._headerSize
                    
        if self["velocities_written"]:
            if self["forces_written"]:
                # Frame record 8,9,10,11,12,13,14,15,16
                self._configRec = '!' + ('%d%s8x'*9) % ((self['totmov'],self._fp)*9)
            else:
                # Frame record 8,9,10,11,12,13
                self._configRec = '!' + ('%d%s8x'*6) % ((self['totmov'],self._fp)*6)
        else:
            if self["forces_written"]:
                # Frame record 8,9,10,14,15,16
                self._configRec = '!' + ('%d%s8x'*6) % ((self['totmov'],self._fp)*6)
            else:
                # Frame record 8,9,10
                self._configRec = '!' + ('%d%s8x'*3) % ((self['totmov'],self._fp)*3)

        self._configRecSize = struct.calcsize(self._configRec)
        trjfile.read(self._configRecSize)
        
        self._frameSize = trjfile.tell() - self._headerSize
        
        trjfile.seek(0,2)
        
        self["n_frames"] = (trjfile.tell()-self._headerSize)/self._frameSize
        
    def read_step(self, index):
        """
        """

        trjfile = self["instance"]

        pos = self._headerSize + index*self._frameSize
        
        trjfile.seek(pos,0)

        rec = '!%s' % self._fp
        recSize = struct.calcsize(rec)
        timeStep, = struct.unpack(rec,trjfile.read(recSize))
        
        if self["defcel"]:
            trjfile.seek(pos + self._defCellRecPos,0)
            cell = numpy.zeros((3,3),dtype=numpy.float64)
            # ax,by,cz,bz,az,ay            
            cellData = numpy.array(struct.unpack(self._defCellRec,trjfile.read(self._defCellRecSize)),dtype=numpy.float64)[2:8]*Units.Ang
            cell[0,0] = cellData[0]
            cell[1,1] = cellData[1]
            cell[2,2] = cellData[2]
            cell[1,2] = cellData[3]
            cell[0,2] = cellData[4]
            cell[0,1] = cellData[5]
            
        else:
            cell = None

        trjfile.seek(pos + self._configRecPos,0)
        
        config = struct.unpack(self._configRec,trjfile.read(self._configRecSize))
        
        if self["velocities_written"]:
            if self["forces_written"]:
                config = numpy.transpose(numpy.reshape(config,(3,3, self['totmov'])))
                xyz    = config[:,:,0]*Units.Ang
                vel    = config[:,:,1]*Units.Ang
                forces = config[:,:,2]*FORCE_FACTOR
            else:
                config = numpy.transpose(numpy.reshape(config,(2,3, self['totmov'])))
                xyz    = config[:,:,0]*Units.Ang
                vel    = config[:,:,1]*Units.Ang
                forces = None
        else:
            if self["forces_written"]:
                config = numpy.transpose(numpy.reshape(config,(2,3, self['totmov'])))
                xyz    = config[:,:,0]*Units.Ang
                vel    = None
                forces = config[:,:,1]*FORCE_FACTOR
            else:
                config = numpy.transpose(numpy.reshape(config,(1,3, self['totmov'])))
                xyz    = config[:,:,0]*Units.Ang
                vel    = None
                forces = None
            
        return timeStep, cell, xyz, vel, forces

    def close(self):
        self["instance"].close()
        
275
class ForciteConverter(Converter):
276
277
278
279
280
281
282
283
    """
    Converts a Forcite trajectory to a MMTK trajectory.
    """

    type = 'forcite'
    
    label = "Forcite"

eric pellegrini's avatar
eric pellegrini committed
284
    category = ('Converters','Materials Studio')
285
    
286
    ancestor = None
287

288
    settings = collections.OrderedDict()
289
290
    settings['xtd_file'] = ('input_file',{'default':os.path.join('..','..','..','Data','Trajectories','Forcite','nylon66_rho100_500K_v300K.xtd')})
    settings['trj_file'] = ('input_file',{'default':os.path.join('..','..','..','Data','Trajectories','Forcite','nylon66_rho100_500K_v300K.trj')})
291
    settings['output_file'] = ('output_files', {'formats':["netcdf"]})
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
                
    def initialize(self):
        '''
        Initialize the job.
        '''
        
        self._xtdfile = XTDFile(self.configuration["xtd_file"]["filename"])
        
        self._xtdfile.build_universe()
        
        self._universe = self._xtdfile.universe
        
        self._trjfile = TrjFile(self.configuration["trj_file"]["filename"])

        # The number of steps of the analysis.
        self.numberOfSteps = self._trjfile['n_frames']
        
        self._velocities = None
        self._forces = None
                                                          
        if self._trjfile["velocities_written"]:
            self._velocities = ParticleVector(self._universe)
            self._velocities.array[:,:] = 0.00

        if self._trjfile["forces_written"]:
            self._forces = ParticleVector(self._universe)
            self._forces.array[:,:] = 0.00

        # A MMTK trajectory is opened for writing.
        self._trajectory = Trajectory(self._universe, self.configuration['output_file']['files'][0], mode='w', comment=self._trjfile["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, xyz, vel, forces = self._trjfile.read_step(index)
        
        # If the universe is periodic set its shape with the current dimensions of the unit cell.
        if self._universe.is_periodic and self._trjfile["defcel"]:
            self._universe.setShape(cell)
            
        movableAtoms = self._trjfile['mvofst']
                    
        self._universe.configuration().array[movableAtoms,:] = xyz
                   
        self._universe.foldCoordinatesIntoBox()
        
        data = {"time" : time}

        if self._velocities is not None:
            self._velocities.array[movableAtoms,:] = vel
            self._universe.setVelocities(self._velocities)

        if self._forces is not None:
            self._forces.array[movableAtoms,:] = forces
            data["forces"] = self._forces

        # 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._trjfile.close()

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