RadiusOfGyration.py 4.59 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
'''

import collections

import numpy

from MDANSE import ELEMENTS
from MDANSE.Framework.Jobs.IJob import IJob
from MDANSE.MolecularDynamics.Analysis import radius_of_gyration

class RadiusOfGyration(IJob):
    """
43
    Radius Of Gyration (ROG) is a measure of the size of an object,
44
    a surface, or an ensemble of points. It is calculated as the Root Mean Square Distance between
45
    the system and a reference which, in MDANSE, is the centre of gravity of the system. 
46
47
48
49
50
51
52
53
    """ 

    type = 'rog'

    label = "Radius of Gyration"
    
    category = ('Structure',)
    
54
    ancestor = ["mmtk_trajectory"]
55

56
57
58
59
60
61
62
    settings = collections.OrderedDict()
    settings['trajectory'] = ('mmtk_trajectory',{})
    settings['frames'] = ('frames', {'dependencies':{'trajectory':'trajectory'}})
    settings['atom_selection'] = ('atom_selection', {'dependencies':{'trajectory':'trajectory'}})
    settings['weights'] = ('weights',{})
    settings['output_files'] = ('output_files', {'formats':["netcdf","ascii"]})
    settings['running_mode'] = ('running_mode',{})
63
64
65
66
67
68
69
70
71
72
                
    def initialize(self):
        """
        Computes the pair distribution function for a set of atoms.
        """
        self.numberOfSteps = self.configuration['frames']['number']
        
        # Will store the time.
        self._outputData.add("time","line", self.configuration['frames']['time'], units='ps')
        
73
        self._outputData.add('rog',"line", (self.configuration['frames']['number'],),axis='time', units="nm")
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

        self._indexes = self.configuration['atom_selection']['indexes']

        self._masses = numpy.array([ELEMENTS[el[0],self.configuration["weights"]["property"]] for el in self.configuration['atom_selection']['elements']],dtype=numpy.float64)

        
    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. 
            #. rog (float): The radius of gyration
        """                

        # get the Frame index
        frameIndex = self.configuration['frames']['value'][index] 
        
        self.configuration['trajectory']['instance'].universe.setFromTrajectory(self.configuration['trajectory']['instance'], frameIndex)
        
        # read the particle trajectory                                              
        series = self.configuration['trajectory']['instance'].universe.configuration().array[self._indexes,:]
                        
        rog = radius_of_gyration(series, masses=self._masses, root=True)
                                                 
        return index, rog
                
                        
    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
        """   

        self._outputData['rog'][index] = x
                
    def finalize(self):
        """
        Finalizes the calculations (e.g. averaging the total term, output files creations ...).
        """   
        # Write the output variables.
119
        self._outputData.write(self.configuration['output_files']['root'], self.configuration['output_files']['formats'], self._info)
120
121
122
123
        
        self.configuration['trajectory']['instance'].close()