DistanceHistogram.py 7.36 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 itertools

import numpy

from MDANSE.Core.Error import Error 
from MDANSE.Extensions import distance_histogram
from MDANSE.Framework.Jobs.IJob import IJob
from MDANSE.MolecularDynamics.Trajectory import atomindex_to_moleculeindex

class DistanceHistogram(IJob):
    """
45
    Compute the Histogram of Distance, used by e.g. PDF, coordination number analysis
46
    """
47 48 49
    
    type = None
    
50 51 52
    settings = collections.OrderedDict()    
    settings['trajectory'] = ('mmtk_trajectory',{})
    settings['frames'] = ('frames', {'dependencies':{'trajectory':'trajectory'}})
53
    settings['r_values'] = ('range', {'label':'r values (nm)','valueType':float, 'includeLast':True, 'mini':0.0})
54
    settings['atom_selection'] = ('atom_selection', {'dependencies':{'trajectory':'trajectory'}})
55 56
    settings['atom_transmutation'] = ('atom_transmutation', {'dependencies':{'trajectory':'trajectory','atom_selection':'atom_selection'}})
    settings['weights']=('weights',{"dependencies":{"atom_selection":"atom_selection"}})
57 58
    settings['output_files'] = ('output_files', {'formats':["netcdf","ascii"]})
    settings['running_mode'] = ('running_mode',{})
59 60 61 62 63 64 65
    
    def initialize(self):
        """
        Initialize the input parameters and analysis self variables
        """

        self.numberOfSteps = self.configuration['frames']['number']
66 67 68

        self._indexes  = [idx for idxs in self.configuration['atom_selection']['indexes'] for idx in idxs]        
        self._indexes = numpy.array(self._indexes,dtype=numpy.int32)
69
        
70
        self.selectedElements = self.configuration['atom_selection']['unique_names']
71
                
72 73
        self.indexToSymbol = numpy.array([self.selectedElements.index(name) for name in self.configuration['atom_selection']['names']], dtype = numpy.int32)
                        
74 75
        lut = atomindex_to_moleculeindex(self.configuration['trajectory']['instance'].universe)
                        
76
        self.indexToMolecule = numpy.array([lut[i] for i in self._indexes], dtype=numpy.int32)
77
        
78
        nElements = len(self.selectedElements)
79 80 81 82 83 84 85
        
        # The histogram of the intramolecular distances.
        self.hIntra = numpy.zeros((nElements,nElements,len(self.configuration['r_values']['mid_points'])),dtype=numpy.float64)
        
        # The histogram of the intermolecular distances.
        self.hInter = numpy.zeros((nElements,nElements,len(self.configuration['r_values']['mid_points'])),dtype=numpy.float64)
        
86
        self.scaleconfig = numpy.zeros((self.configuration['atom_selection']['selection_length'],3), dtype=numpy.float64)
87 88 89

        self.averageDensity = 0.0
        
90
        self._nAtomsPerElement = self.configuration['atom_selection'].get_natoms()
91
        self._concentrations = {}
92
        for k in self._nAtomsPerElement.keys():
93 94
            self._concentrations[k] = 0.0
        
95
        self._elementsPairs = sorted(itertools.combinations_with_replacement(self.selectedElements,2))
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

        if not self.configuration['trajectory']['instance'].universe.is_periodic:
            raise Error("Pair distribution function cannot be calculated for infinite universe trajectories")
 
    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. 
            #. cellVolume (float): the volume of the current frame simulation box
            #. hIntraTemp (np.array): The calculated distance intra-molecular histogram
            #. hInterTemp (np.array): The calculated distance inter-molecular histogram
        """

113

114 115 116 117 118 119 120 121 122 123 124 125 126 127
        # get the Frame index
        frameIndex = self.configuration['frames']['value'][index]   

        universe = self.configuration['trajectory']['instance'].universe
        
        universe.setFromTrajectory(self.configuration['trajectory']['instance'], frameIndex)
    
        directCell = numpy.array(universe.basisVectors()).astype(numpy.float64)
        reverseCell = numpy.array(universe.reciprocalBasisVectors()).astype(numpy.float64)
        
        cellVolume = universe.cellVolume()
            
        hIntraTemp = numpy.zeros(self.hIntra.shape, dtype=numpy.float64)
        hInterTemp = numpy.zeros(self.hInter.shape, dtype=numpy.float64)
128
        
129
        distance_histogram.distance_histogram(universe.configuration().array[self._indexes,:],
130 131
                                              directCell,
                                              reverseCell,
132
                                              self._indexes,
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
                                              self.indexToMolecule,
                                              self.indexToSymbol,
                                              hIntraTemp,
                                              hInterTemp,
                                              self.scaleconfig,
                                              self.configuration['r_values']['first'],
                                              self.configuration['r_values']['step'])

        numpy.multiply(hIntraTemp, cellVolume, hIntraTemp)
        numpy.multiply(hInterTemp, cellVolume, hInterTemp)
                                                                                                
        return index, (cellVolume, hIntraTemp, hInterTemp)
                    
    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
        """
        
        nAtoms = self.configuration['trajectory']['instance'].universe.numberOfAtoms()
        
        self.averageDensity += nAtoms/x[0]

        # The temporary distance histograms are normalized by the volume. This is done for each step because the
        # volume can variate during the MD (e.g. NPT conditions). This volume is the one that intervene in the density
        # calculation.
        self.hIntra += x[1]
        self.hInter += x[2]
        
164
        for k,v in self._nAtomsPerElement.items():
165 166
            self._concentrations[k] += float(v)/nAtoms