[MMTK] modes to numpy arrays

Konrad Hinsen research at khinsen.fastmail.net
Thu Sep 1 07:45:05 UTC 2011


On 31 août 11, at 11:44, Ramon Crehuet wrote:

> I would like to convert a NormalModes object into a numpy array. I  
> tried:
> modes = NormalModes(universe)
> m_numpy = modes.array
> and
> m2_numpy = numpy.asarray(modes)
> which do not give the same... (though it gives arrays with the same  
> shape, so they are not simple a transposition).
> Curiously, modes[0].array and modes.array[0] are also different, and  
> in particular:
> np.all(modes[0].array==m2_numpy[0]) is True. So m2_numpy seems to  
> contain as vectors, each mode. By then, what is modes.array...?


It's an array that contains the normal modes vectors in some way, the  
details depending on how the modes were obtained (i.e. vibrational/ 
energetic/brownian, using a subspace or not, using a sparse  
representation or not, ...). In fact, it is the array containing the  
result from the last LAPACK call made in the normal mode calculation.

If you want to use that array directly (which I don't really  
recommend), the best guide to its interpretation is the code that  
extracts individual modes in MMTK. For a VibrationalModes object,  
those are:

     # mode vector in mass-weighted coordinates

     def rawMode(self, item):
         index = self.sort_index[item]
         f = self.frequencies[index]
         if self.imaginary[index]:
             f = f*1.j
         return VibrationalMode(self.universe, item, f,  
self.array[index])

     # mode vector in Cartesian coordinates scaled by a thermal  
amplitude

     def __getitem__(self, item):
         index = self.sort_index[item]
         f = self.frequencies[index]
         if self.imaginary[index]:
             f = f*1.j
         if self.temperature is None or item < 6:
             amplitude = 1.
         else:
             amplitude = N.sqrt(2.*self.temperature*Units.k_B/ 
Units.amu) / \
                         (2.*N.pi*f)
         return VibrationalMode(self.universe, item, f,
                                amplitude*self.array[index]/ 
self.weights)

This shows that you need at the very least modes.array AND  
modes.sort_index to extract any useful information from that array.

numpy.asarray() converts any sequence-like data into an array. It's  
similar to

    numpy.array([m for m in modes])

meaning that it ends up calling modes.__getitem__, which applies mass  
weighting and a thermal amplitude. It also sorts the modes by  
eigenvalue.

Konrad.
--
---------------------------------------------------------------------
Konrad Hinsen
Centre de Biophysique Moléculaire, CNRS Orléans
Synchrotron Soleil - Division Expériences
Saint Aubin - BP 48
91192 Gif sur Yvette Cedex, France
Tel. +33-1 69 35 97 15
E-Mail: research at  khinsen dot fastmail dot net
---------------------------------------------------------------------




More information about the mmtk mailing list