CurrentCorrelationFunction.py 12 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 Mar 30, 2015

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

import collections
import itertools

import numpy

from MDANSE.Framework.Jobs.IJob import IJob
from MDANSE.Mathematics.Arithmetic import weight
from MDANSE.Mathematics.Signal import correlation, normalize, get_spectrum

class CurrentCorrelationFunction(IJob):
    """
    Computes the current correlation function for a set of atoms.
45 46
    The transverse and longitudinal current correlation functions are typically used to study the propagation of excitations in disordered systems.
    The longitudinal current is directly related to density fluctuations and the transverse current is linked to propagating 'shear modes'.
47
    
48
    For more information, see e.g. 'J.-P. Hansen and I. R. McDonald, Theory of Simple Liquids (3rd ed., Elsevier), chapter 7.4: Correlations
49 50 51 52 53 54 55
    in space and time)' 
    """

    type = 'ccf'
    
    label = "Current Correlation Function"

56
    category = ('Analysis','Scattering',)
57
    
58
    ancestor = ["mmtk_trajectory","molecular_viewer"]
59

60 61 62
    settings = collections.OrderedDict()
    settings['trajectory'] = ('mmtk_trajectory',{})
    settings['frames'] = ('frames', {'dependencies':{'trajectory':'trajectory'}})
63
    settings['instrument_resolution'] = ('instrument_resolution',{'dependencies':{'trajectory':'trajectory','frames' : 'frames'}})
64 65 66
    settings['q_vectors'] = ('q_vectors',{'dependencies':{'trajectory':'trajectory'}})
    settings['atom_selection'] = ('atom_selection',{'dependencies':{'trajectory':'trajectory'}})
    settings['normalize'] = ('boolean', {'default':False})
67 68
    settings['atom_transmutation'] = ('atom_transmutation',{'dependencies':{'trajectory':'trajectory','atom_selection':'atom_selection'}})
    settings['weights'] = ('weights', {'default':'b_coherent',"dependencies":{'trajectory':'trajectory','atom_selection':'atom_selection', 'atom_transmutation':'atom_transmutation'}})
69 70
    settings['output_files'] = ('output_files', {'formats':["netcdf","ascii"]})
    settings['running_mode'] = ('running_mode',{})
71 72 73 74 75 76 77 78 79 80 81 82 83 84

    def initialize(self):
        """
        Initialize the input parameters and analysis self variables
        """

        self.numberOfSteps = self.configuration['q_vectors']['n_shells']

        nQShells = self.configuration["q_vectors"]["n_shells"]
        
        self._nFrames = self.configuration['frames']['number']
        
        self._instrResolution = self.configuration["instrument_resolution"]
        
85
        self._nOmegas = self._instrResolution['n_omegas']
86 87 88
                
        self._outputData.add("q","line", numpy.array(self.configuration["q_vectors"]["shells"]), units="inv_nm") 

89 90
        self._outputData.add("time","line", self.configuration['frames']['time'], units='ps')
        self._outputData.add("time_window","line", self._instrResolution["time_window"], units="au") 
91

92 93
        self._outputData.add("omega","line", self._instrResolution["omega"],units='rad/ps')
        self._outputData.add("omega_window","line", self._instrResolution["omega_window"], axis="omega", units="au") 
94 95 96 97 98

        self._elements = self.configuration['atom_selection']['unique_names']
        self._elementsPairs = sorted(itertools.combinations_with_replacement(self._elements,2))
        
        self._indexesPerElement = self.configuration['atom_selection'].get_indexes()
99 100

        for pair in self._elementsPairs:
101 102 103 104 105 106 107 108 109
            self._outputData.add("j(q,t)_long_%s%s"  % pair,"surface", (nQShells,self._nFrames), axis="q|time", units="au")                                                 
            self._outputData.add("j(q,t)_trans_%s%s" % pair,"surface", (nQShells,self._nFrames), axis="q|time", units="au")                                                 
            self._outputData.add("J(q,f)_long_%s%s"  % pair,"surface", (nQShells,self._nOmegas), axis="q|omega", units="au") 
            self._outputData.add("J(q,f)_trans_%s%s" % pair,"surface", (nQShells,self._nOmegas), axis="q|omega", units="au") 

        self._outputData.add("j(q,t)_long_total","surface", (nQShells,self._nFrames), axis="q|time"    , units="au")                                                 
        self._outputData.add("J(q,f)_long_total","surface", (nQShells,self._nOmegas), axis="q|omega", units="au") 
        self._outputData.add("j(q,t)_trans_total","surface", (nQShells,self._nFrames), axis="q|time"    , units="au")                                                 
        self._outputData.add("J(q,f)_trans_total","surface", (nQShells,self._nOmegas), axis="q|omega", units="au") 
110 111 112 113 114 115 116 117 118 119 120
         
    def run_step(self, index):
        """
        Runs a single step of the job.\n
 
        :Parameters:
            #. index (int): The index of the step.
        :Returns:
            #. index (int): The index of the step. 
            #. rho (numpy.array): The exponential part of I(q,t)
        """
121

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        shell = self.configuration["q_vectors"]["shells"][index]
        
        if not shell in self.configuration["q_vectors"]["value"]:
            return index, None
            
        else:
            
            traj = self.configuration['trajectory']['instance']
            
            qVectors = self.configuration["q_vectors"]["value"][shell]["q_vectors"]
            
            qVectors = traj.universe._boxToRealPointArray(qVectors.T)
                                     
            qVectors = qVectors.T
            
            nQVectors = qVectors.shape[1]
            
            rho = {}
            rho_loop = {}
            rhoLong = {}
            rhoTrans = {}
            rhoLong_loop = {}
            rhoTrans_loop = {}
145
            for element in self._elements:
146 147 148 149 150 151 152 153 154 155
                rho[element] = numpy.zeros((self._nFrames, 3, nQVectors), dtype = numpy.complex64)
                rho_loop[element] = numpy.zeros((self._nFrames, 3, nQVectors), dtype = numpy.complex64)
                rhoLong_loop[element] = numpy.zeros((self._nFrames, 3, nQVectors), dtype = numpy.complex64)
                rhoTrans_loop[element] = numpy.zeros((self._nFrames, 3, nQVectors), dtype = numpy.complex64)

            # loop over the trajectory time steps
            for i, frame in enumerate(self.configuration['frames']['value']):
                conf = traj.configuration[frame]
                vel = traj.velocities[frame]
                
156
                for element,idxs in self._indexesPerElement.items():
157 158 159 160 161 162 163
                    selectedCoordinates = conf.array[idxs,:]
                    selectedVelocities =  vel.array[idxs,:]
                    selectedVelocities = numpy.transpose(selectedVelocities)[:,:,numpy.newaxis]
                    tmp = numpy.exp(1j*numpy.dot(selectedCoordinates, qVectors))[numpy.newaxis,:,:]
                    rho[element][i,:,:] = numpy.add.reduce(selectedVelocities*tmp,1)

            Q2 = numpy.sum(qVectors**2,axis=0)
164
            
165
            for element in self._elements:
166 167 168 169 170 171 172 173 174 175 176 177 178
                qj = numpy.sum(rho[element]*qVectors,axis=1)
                rhoLong[element] = (qj[:,numpy.newaxis,:]*qVectors[numpy.newaxis,:,:])/Q2
                rhoTrans[element] = rho[element] - rhoLong[element]

            return index, (rhoLong, rhoTrans)
    
    def combine(self, index, x):
        """
        Combines returned results of run_step.\n
        :Parameters:
            #. index (int): The index of the step.\n
            #. x (any): The returned result(s) of run_step
        """
179

180 181 182 183 184
        if x is None:
            return
        
        jLong, jTrans = x
        
185
        for at1,at2 in self._elementsPairs:
186 187 188 189 190
            
            corrLong = numpy.zeros((self._nFrames,),dtype=numpy.float64)
            corrTrans = numpy.zeros((self._nFrames,),dtype=numpy.float64)
            
            for i in range(3):
191 192
                corrLong += correlation(jLong[at1][:,i,:],jLong[at2][:,i,:], axis=0, average=1)
                corrTrans += correlation(jTrans[at1][:,i,:],jTrans[at2][:,i,:], axis=0, average=1)
193
                            
194 195
            self._outputData["j(q,t)_long_%s%s" % (at1,at2)][index,:] += corrLong
            self._outputData["j(q,t)_trans_%s%s" % (at1,at2)][index,:] += corrTrans
196 197 198 199 200
                                        
    def finalize(self):
        """
        Finalizes the calculations (e.g. averaging the total term, output files creations ...)
        """
201 202
                        
        nAtomsPerElement = self.configuration['atom_selection'].get_natoms()
203
        for pair in self._elementsPairs:
204 205 206
            at1,at2 = pair
            ni = nAtomsPerElement[at1]
            nj = nAtomsPerElement[at2]
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
            self._outputData["j(q,t)_long_%s%s" % pair][:] /= ni*nj
            self._outputData["j(q,t)_trans_%s%s" % pair][:] /= ni*nj
            self._outputData["J(q,f)_long_%s%s" % pair][:] = get_spectrum(self._outputData["j(q,t)_long_%s%s" % pair],
                                                                          self.configuration["instrument_resolution"]["time_window"],
                                                                          self.configuration["instrument_resolution"]["time_step"],
                                                                          axis=1)        
            self._outputData["J(q,f)_trans_%s%s" % pair][:] = get_spectrum(self._outputData["j(q,t)_trans_%s%s" % pair],
                                                                           self.configuration["instrument_resolution"]["time_window"],
                                                                           self.configuration["instrument_resolution"]["time_step"],
                                                                           axis=1)        

        if self.configuration['normalize']["value"]:
            for pair in self._elementsPairs:
                self._outputData["j(q,t)_long_%s%s" % pair] = normalize(self._outputData["j(q,t)_long_%s%s" % pair],axis=1)
                self._outputData["j(q,t)_trans_%s%s" % pair] = normalize(self._outputData["j(q,t)_trans_%s%s" % pair],axis=1)

223
        jqtLongTotal = weight(self.configuration["weights"].get_weights(),self._outputData,nAtomsPerElement,2,"j(q,t)_long_%s%s")
224 225
        self._outputData["j(q,t)_long_total"][:] = jqtLongTotal

226
        jqtTransTotal = weight(self.configuration["weights"].get_weights(),self._outputData,nAtomsPerElement,2,"j(q,t)_trans_%s%s")
227 228
        self._outputData["j(q,t)_trans_total"][:] = jqtTransTotal
        
229
        sqfLongTotal = weight(self.configuration["weights"].get_weights(),self._outputData,nAtomsPerElement,2,"J(q,f)_long_%s%s")
230 231
        self._outputData["J(q,f)_long_total"][:] = sqfLongTotal

232
        sqfTransTotal = weight(self.configuration["weights"].get_weights(),self._outputData,nAtomsPerElement,2,"J(q,f)_trans_%s%s")
233 234
        self._outputData["J(q,f)_trans_total"][:] = sqfTransTotal
    
235
        self._outputData.write(self.configuration['output_files']['root'], self.configuration['output_files']['formats'], self._info)
236 237
        
        self.configuration['trajectory']['instance'].close()