pythonpython-3.xphysicschemistrymdanalysis

How to use MDAnalysis to principal_axes and moment_of_inertia with a group of atoms?


I am trying to use MDAnalysis (MDAnalysis.__version__ == 0.17.0) API functions principal_axes() and moment_of_inertia() to calculate these matrices for a group of selected atoms as described in the doc:

import MDAnalysis
from MDAnalysis.tests.datafiles import PSF, DCD
import numpy as np

u = MDAnalysis.Universe(PSF, DCD)

CA = u.select_atoms("protein and name CA")

I = np.matrix(CA.moment_of_inertia())
U = np.matrix(CA.principal_axes())
print("center of mass", CA.center_of_mass())
print("moment of inertia", I)
print("principal axes", U)
print("Lambda = U'IU", np.transpose(U)*I*U)

The output:

center of mass [ 0.06873595 -0.04605918 -0.24643682]
moment of inertia [[ 393842.2070687     -963.01376005   -6050.68541811]
 [   -963.01376005  474434.9289629    -3902.61617054]
 [  -6050.68541811   -3902.61617054  520207.91703069]]
principal axes [[-0.04680878 -0.08278738  0.99546732]
 [ 0.01813292 -0.9964659  -0.08201778]
 [-0.99873927 -0.01421157 -0.04814453]]
Lambda = U'IU [[ 519493.24344558   -4093.3268841    11620.96444297]
 [  -4093.3268841   473608.1536763     7491.56715845]
 [  11620.96444297    7491.56715845  395383.6559404 ]]

This looks wrong, one of the reason is that U'IU isn't diagonal as mentioned in the doc: enter image description here


Maybe I need to align the protein to the center of mass to calculate the moment of inertia with respect to that.


Solution

  • The docs in the tutorial on AtomGroup.principal_axes() are in principle correct but it is confusing that the return value of AtomGroup.principal_axes() is not the matrix U but its transpose, U.T:

    The AtomGroup.principal_axes() method returns an array [p1, p2, p3] where the principal axes p1, p2, p3 are arrays of length 3; this layout as row vectors was chosen for convenience (so that one can extract the vectors with p1, p2, p3 = ag.principal_axes()). To form a matrix U where the principal axes are the column vectors as in the usual treatment of the principal axes one has to transpose. For example:

    import MDAnalysis
    from MDAnalysis.tests.datafiles import PSF, DCD
    import numpy as np
    
    u = MDAnalysis.Universe(PSF, DCD)
    
    CA = u.select_atoms("protein and name CA")
    
    I = CA.moment_of_inertia()
    UT = CA.principal_axes()
    
    # transpose the row-vector layout UT = [p1, p2, p3]
    U = UT.T
    
    # test that U diagonalizes I
    Lambda = U.T.dot(I.dot(U))
    
    print(Lambda)
    
    # check that it is diagonal (to machine precision)
    print(np.allclose(Lambda - np.diag(np.diagonal(Lambda)), 0))
    

    The matrix Lambda should be diagonal (the last print should show True):

    [[ 5.20816990e+05 -6.56706349e-10 -2.83491351e-12]
    [-6.62283524e-10  4.74131234e+05 -2.06979926e-11]
    [-6.56687024e-12 -2.07159142e-11  3.93536829e+05]]
    True
    

    Finally, if you want to calculate "by hand":

    values, evecs = np.linalg.eigh(I)
    indices = np.argsort(values)
    U = evecs[:, indices]
    

    This gives U with the principal axes as column vectors.