RigidBodyTrajectory.py 12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#71 avenue des Martyrs
#38000 Grenoble Cedex 9
#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 Jun 9, 2015

30
:author: Erc C. Pellegrini
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
'''

import collections
import operator

import numpy

from Scientific.Geometry import Vector
from Scientific.Geometry.Quaternion import Quaternion
from Scientific.Geometry.Transformation import Translation
from Scientific.IO.NetCDF import NetCDFFile

from MMTK.Collections import Collection
from MMTK.Trajectory import SnapshotGenerator, Trajectory, TrajectoryOutput

46
from MDANSE import REGISTRY
47 48 49 50 51 52 53 54
from MDANSE.Framework.Jobs.IJob import IJob, JobError

class RigidBodyTrajectory(IJob):
    """
    """
    
    label = 'Rigid Body Trajectory'

55
    category = ('Analysis','Trajectory',)
56
    
57
    ancestor = ["mmtk_trajectory","molecular_viewer"]
58 59 60 61
    
    settings = collections.OrderedDict()
    settings['trajectory']=('mmtk_trajectory',{})
    settings['frames']=('frames', {"dependencies":{'trajectory':'trajectory'}})
62 63
    settings['atom_selection']=('atom_selection',{"dependencies":{'trajectory':'trajectory'}})
    settings['grouping_level']=('grouping_level',{"dependencies":{'trajectory':'trajectory','atom_selection':'atom_selection'}})
64 65
    settings['stepwise']=('boolean',{'default':True})
    settings['reference']=('integer',{"mini":0})
66
    settings['remove_translation']=('boolean',{'default':False})
67 68 69 70 71 72
    settings['output_files']=('output_files', {"formats":["netcdf"]})
    
    def initialize(self):
        """
        """

73
        self.numberOfSteps = self.configuration['atom_selection']['selection_length'] 
74 75 76 77 78 79 80
         
        if (self.configuration['reference']['value'] >= self.configuration['trajectory']['length']):
            raise JobError(self,'Invalid reference frame. Must be an integer in [%d,%d[' % (0,self.configuration['trajectory']['length']))
                           
        self.nFrames = self.configuration['frames']['number']                  
        self._rbt = {'trajectory':{}}
        
81 82 83
        self._quaternions = numpy.zeros((self.configuration['atom_selection']['selection_length'],self.configuration['frames']['number'], 4),dtype=numpy.float64)
        self._coms = numpy.zeros((self.configuration['atom_selection']['selection_length'],self.configuration['frames']['number'], 3),dtype=numpy.float64)
        self._fits = numpy.zeros((self.configuration['atom_selection']['selection_length'],self.configuration['frames']['number']),dtype=numpy.float64)
84 85 86 87
            
    def run_step(self, index):
        '''
        '''
88

89
        atoms = sorted(self.configuration['trajectory']['universe'].atomList(), key=operator.attrgetter('index'))
90 91 92
        
        indexes = self.configuration['atom_selection']["indexes"][index]
         
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
        group = Collection([atoms[idx] for idx in indexes])
         
        rbtPerGroup = {}        
        # Those matrix will store the quaternions and the CMS coming from the RBT trajectory.
        rbtPerGroup['quaternions'] = numpy.zeros((self.configuration['frames']['number'], 4), dtype=numpy.float64)
        rbtPerGroup['com'] = numpy.zeros((self.configuration['frames']['number'], 3), dtype=numpy.float64)
        rbtPerGroup['fit'] = numpy.zeros((self.configuration['frames']['number'],), dtype=numpy.float64)
        rbtPerGroup['trajectory'] = {}
         
        # Case of a moving reference.
        if self.configuration['stepwise']['value']:
             
            # The reference configuration is always the one of the previous frame excepted for the first frame
            # where it is set by definition to the first frame (could we think about a cyclic alternative way ?).
            for comp in range(self.configuration['frames']['number']):
                                  
                if comp == 0:
                    previousFrame = self.configuration['frames']['value'][0]
                     
                else:
                    previousFrame = self.configuration['frames']['value'][comp-1]
                    
                currentFrame = self.configuration['frames']['value'][comp]                    
                     
                refConfig = self.configuration['trajectory']['instance'].configuration[previousFrame]
 
                # The RBT is created just for the current step.
                rbt = self.configuration['trajectory']['instance'].readRigidBodyTrajectory(group,
                                                                                           first=currentFrame,
                                                                                           last=currentFrame+1,
                                                                                           skip=1,
                                                                                           reference=refConfig)
 
                # The corresponding quaternions and cms are stored in their corresponding matrix.
                rbtPerGroup['quaternions'][comp,:] = rbt.quaternions
                rbtPerGroup['com'][comp,:] = rbt.cms
                rbtPerGroup['fit'][comp] = rbt.fit
                                             
        # The simplest case, the reference frame is fixed.
        # A unique RBT is performed from first to last skipping skip steps and using refConfig as the reference.
        else:
            
            # If a fixed reference has been set. We can already set the reference configuration here.
            refConfig = self.configuration['trajectory']['instance'].configuration[self.configuration['reference']['value']]
             
            # The RBT is created for the whole frame selection
            rbt = self.configuration['trajectory']['instance'].readRigidBodyTrajectory(group,
                                                                                       first=self.configuration['frames']['first'],
                                                                                       last=self.configuration['frames']['last']+1,
                                                                                       skip=self.configuration['frames']['step'],
                                                                                       reference=refConfig)
             
            # The corresponding quaternions and cms are stored in their corresponding matrix.
            rbtPerGroup['quaternions'] = rbt.quaternions
            rbtPerGroup['com'] = rbt.cms
            rbtPerGroup['fit'] = rbt.fit
 
        # Can not use the centers of mass defined by rbt.cms because the reference frame selected can be out of the selected frames for the Rigid Body Trajectory.
        centerOfMass = group.centerOfMass(refConfig)
 
        # Loop over the atoms of the group to set the RBT trajectory.
        for atom in group:
                         
            rbtPerGroup['trajectory'][atom.index] = numpy.zeros((self.configuration['frames']['number'],3), dtype=numpy.float64)
             
            # The coordinates of the atoms are centered around the center of mass of the group.
            xyz = refConfig[atom] - centerOfMass
 
            # Loop over the selected frames.
            for comp in range(self.configuration['frames']['number']):
                 
                # The rotation matrix corresponding to the selected frame in the RBT.
                transfo = Quaternion(rbtPerGroup['quaternions'][comp,:]).asRotation()
 
                if self.configuration['remove_translation']['value']:
                    # The transformation matrix corresponding to the selected frame in the RBT.
                    transfo = Translation(centerOfMass)*transfo
                         
                # Compose with the CMS translation if the removeTranslation flag is set off.
                else:
                    # The transformation matrix corresponding to the selected frame in the RBT.
                    transfo = Translation(Vector(rbtPerGroup['com'][comp,:]))*transfo
 
                # The RBT is performed on the CMS centered coordinates of atom at.
                rbtPerGroup['trajectory'][atom.index][comp,:] = transfo(Vector(xyz))
                                         
        return index, rbtPerGroup
    
    def combine(self, index, x):
        """
        """
        
        self._quaternions[index] = x['quaternions']
        self._coms[index] = x['com']
        self._fits[index] = x['fit']
        
        self._rbt['trajectory'].update(x['trajectory'])
        
    def finalize(self):
        '''
        '''
        
        atoms = sorted(self.configuration['trajectory']['universe'].atomList(), key=operator.attrgetter('index'))
196 197 198 199
        selectedAtoms = Collection()
        for indexes in self.configuration['atom_selection']['indexes']:
            for idx in indexes:
                selectedAtoms.addObject(atoms[idx])
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

        # Create trajectory
        outputFile = Trajectory(selectedAtoms, self.configuration['output_files']['files'][0], 'w')

        # Create the snapshot generator
        snapshot = SnapshotGenerator(self.configuration['trajectory']['universe'], actions = [TrajectoryOutput(outputFile, ["configuration","time"], 0, None, 1)])
                
        # The output is written
        for comp in range(self.configuration['frames']['number']):

            currentFrame = self.configuration['frames']['value'][comp]                    

            self.configuration['trajectory']['instance'].universe.setFromTrajectory(self.configuration['trajectory']['instance'], currentFrame)
            
            for atom in selectedAtoms:
                atom.setPosition(self._rbt['trajectory'][atom.index][comp,:])
            snapshot(data = {'time' : self.configuration['frames']['time'][comp]})
  
        outputFile.close()

        outputFile = NetCDFFile(self.configuration['output_files']['files'][0], 'a')

        outputFile.createDimension('NFRAMES', self.configuration['frames']['number'])
223
        outputFile.createDimension('NGROUPS', self.configuration['atom_selection']['selection_length'])
224
        outputFile.createDimension('QUATERNIONLENGTH',4)
225
        outputFile.createDimension('XYZ',3)
226 227 228 229 230
 
        # The NetCDF variable that stores the quaternions.
        QUATERNIONS = outputFile.createVariable('quaternion', numpy.dtype(numpy.float64).char, ('NGROUPS', 'NFRAMES','QUATERNIONLENGTH'))
 
        # The NetCDF variable that stores the centers of mass.
231
        COM = outputFile.createVariable('com', numpy.dtype(numpy.float64).char, ('NGROUPS','NFRAMES','XYZ'))
232 233 234 235
             
        # The NetCDF variable that stores the rigid-body fit.
        FIT = outputFile.createVariable('fit', numpy.dtype(numpy.float64).char, ('NGROUPS','NFRAMES'))
 
eric pellegrini's avatar
eric pellegrini committed
236
        outputFile.info = str(self)
237 238
 
        # Loop over the groups.
239
        for comp in range(self.configuration['atom_selection']['selection_length']):
240
             
241
            aIndexes = self.configuration['atom_selection']['indexes'][comp]
242 243 244 245 246 247 248
             
            outputFile.info += 'Group %s: %s\n' % (comp, [index for index in aIndexes])
 
            QUATERNIONS[comp,:,:] = self._quaternions[comp][:,:]
            COM[comp,:,:] = self._coms[comp][:,:]
            FIT[comp,:] = self._fits[comp][:]
                         
249 250 251
        outputFile.close()
        
REGISTRY['rbt'] = RigidBodyTrajectory