GaussianDynamicIncoherentStructureFactor.py 7.78 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
34
35
36
37
38
39
40
41
42
43
44
'''

import collections

import numpy

from MDANSE.Framework.Jobs.IJob import IJob
from MDANSE.Mathematics.Arithmetic import weight
from MDANSE.Mathematics.Signal import get_spectrum
from MDANSE.MolecularDynamics.Analysis import mean_square_displacement
from MDANSE.MolecularDynamics.Trajectory import read_atoms_trajectory

class GaussianDynamicIncoherentStructureFactor(IJob):
    """
45
    Computes the dynamic incoherent structure factor S_inc(Q,w) for a set of atoms in the Gaussian approximation.
46
47
48
49
50
51
    """
    
    type = 'gdisf'
    
    label = "Gaussian Dynamic Incoherent Structure Factor"

52
    category = ('Analysis','Scattering',)
53
    
54
    ancestor = ["mmtk_trajectory","molecular_viewer"]
55
    
56
57
58
59
    settings = collections.OrderedDict()
    settings['trajectory'] = ('mmtk_trajectory',{})
    settings['frames'] = ('frames', {'dependencies' : {'trajectory':'trajectory'}})
    settings['q_shells'] = ('range', {'valueType':float, 'includeLast':True, 'mini':0.0})
60
    settings['instrument_resolution'] = ('instrument_resolution', {'dependencies':{'trajectory':'trajectory','frames' : 'frames'}})
61
    settings['projection'] = ('projection', {'label':"project coordinates"})
62
63
64
    settings['atom_selection'] = ('atom_selection', {'dependencies':{'trajectory':'trajectory'}})
    settings['grouping_level']=('grouping_level',{'dependencies':{'trajectory':'trajectory','atom_selection':'atom_selection', 'atom_transmutation':'atom_transmutation'}})
    settings['atom_transmutation'] = ('atom_transmutation', {'dependencies':{'trajectory':'trajectory','atom_selection':'atom_selection'}})
65
    settings['weights'] = ('weights', {'default':'b_incoherent2','dependencies':{'atom_selection':'atom_selection'}})
66
67
    settings['output_files'] = ('output_files', {'formats':["netcdf","ascii"]})
    settings['running_mode'] = ('running_mode',{})
68
69
70
71
72
73
    
    def initialize(self):
        """
        Initialize the input parameters and analysis self variables
        """

74
        self.numberOfSteps = self.configuration['atom_selection']['selection_length']
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

        self._nQShells = self.configuration["q_shells"]["number"]
        
        self._nFrames = self.configuration['frames']['number']
        
        self._instrResolution = self.configuration["instrument_resolution"]
        
        self._nFrequencies = self._instrResolution['n_frequencies']
        
        self._kSquare = self.configuration["q_shells"]["value"]**2
        
        self._outputData.add("q","line",self.configuration["q_shells"]["value"], units="inv_nm") 

        self._outputData.add("q2","line",self._kSquare,units="inv_nm**2") 

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

        self._outputData.add("frequency","line",self.configuration["instrument_resolution"]["frequencies"], units='THz')
        self._outputData.add("frequency_window","line",self._instrResolution["frequency_window"], axis="frequency", units="au") 

96
        for element in self.configuration['atom_selection']['unique_names']:
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
            self._outputData.add("f(q,t)_%s" % element,"surface", (self._nQShells,self._nFrames),axis="q|times", units="au")                                                 
            self._outputData.add("s(q,f)_%s" % element,"surface", (self._nQShells,self._nFrequencies), axis="q|frequency", units="nm2/ps") 

        self._outputData.add("f(q,t)_total","surface",(self._nQShells,self._nFrames), axis="q|times", units="au")                                                 
        self._outputData.add("s(q,f)_total","surface",(self._nQShells,self._nFrequencies), axis="q|frequency", units="nm2/ps") 
        
    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. 
            #. atomicSF (numpy.array): The atomic structure factor
        """

        # get atom index
115
116
        indexes = self.configuration['atom_selection']["indexes"][index]
        masses = self.configuration['atom_selection']["masses"][index]
117
118
119
120
121
                
        series = read_atoms_trajectory(self.configuration["trajectory"]["instance"],
                                       indexes,
                                       first=self.configuration['frames']['first'],
                                       last=self.configuration['frames']['last']+1,
122
123
                                       step=self.configuration['frames']['step'],
                                       weights=masses)
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
        
        series = self.configuration['projection']["projector"](series)

        atomicSF = numpy.zeros((self._nQShells,self._nFrames), dtype=numpy.float64)
                
        msd  = mean_square_displacement(series)

        for i, q2 in enumerate(self._kSquare):
            
            gaussian = numpy.exp(-msd*q2/6.0)

            atomicSF[i,:] += gaussian

        return index, atomicSF
    
    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
        """

        # The symbol of the atom.
148
        element = self.configuration['atom_selection']["names"][index]
149
150
151
152
153
154
155
156
        
        self._outputData["f(q,t)_%s" % element] += x
                
    def finalize(self):
        """
        Finalizes the calculations (e.g. averaging the total term, output files creations ...)
        """
        
157
158
        nAtomsPerElement = self.configuration['atom_selection'].get_natoms()        
        for element, number in nAtomsPerElement.items():
159
160
161
162
163
            self._outputData["f(q,t)_%s" % element][:] /= number
            self._outputData["s(q,f)_%s" % element][:] = get_spectrum(self._outputData["f(q,t)_%s" % element],
                                                                      self.configuration["instrument_resolution"]["time_window"],
                                                                      self.configuration["instrument_resolution"]["time_step"],
                                                                      axis=1)
164
        weights = self.configuration["weights"].get_weights()
165
        
166
        self._outputData["f(q,t)_total"][:] = weight(weights,self._outputData,nAtomsPerElement,1,"f(q,t)_%s")
167
        
168
        self._outputData["s(q,f)_total"][:] = weight(weights,self._outputData,nAtomsPerElement,1,"s(q,f)_%s")
169
    
170
        self._outputData.write(self.configuration['output_files']['root'], self.configuration['output_files']['formats'], self._info)
171
172
        
        self.configuration['trajectory']['instance'].close()