| 1 |
cpuglis |
1189 |
#!/usr/bin/env python
|
| 2 |
|
|
"""principalAxisCalculator
|
| 3 |
|
|
|
| 4 |
|
|
Opens an XYZ file and computes the moments of inertia and principal axes
|
| 5 |
|
|
for the structure in the XYZ file.
|
| 6 |
|
|
|
| 7 |
|
|
Usage: principalAxisCalculator
|
| 8 |
|
|
|
| 9 |
|
|
Options:
|
| 10 |
|
|
-h, --help show this help
|
| 11 |
|
|
-x, --xyz=... use specified XYZ (.xyz) file for the structure
|
| 12 |
|
|
|
| 13 |
|
|
Example:
|
| 14 |
|
|
principalAxisCalculator -x junk.xyz
|
| 15 |
|
|
|
| 16 |
|
|
"""
|
| 17 |
|
|
|
| 18 |
|
|
__author__ = "Dan Gezelter (gezelter@nd.edu)"
|
| 19 |
|
|
__version__ = "$Revision: 1.1 $"
|
| 20 |
|
|
__date__ = "$Date: 2007-11-26 19:23:36 $"
|
| 21 |
|
|
__copyright__ = "Copyright (c) 2006 by the University of Notre Dame"
|
| 22 |
|
|
__license__ = "OOPSE"
|
| 23 |
|
|
|
| 24 |
|
|
import sys
|
| 25 |
|
|
import getopt
|
| 26 |
|
|
import string
|
| 27 |
|
|
import math
|
| 28 |
|
|
import random
|
| 29 |
|
|
from sets import *
|
| 30 |
|
|
import numarray as na
|
| 31 |
|
|
import numarray.linear_algebra as la
|
| 32 |
|
|
|
| 33 |
|
|
_haveXYZFileName = 0
|
| 34 |
|
|
|
| 35 |
|
|
positions = []
|
| 36 |
|
|
indices = []
|
| 37 |
|
|
atypes = []
|
| 38 |
|
|
|
| 39 |
|
|
|
| 40 |
|
|
def usage():
|
| 41 |
|
|
print __doc__
|
| 42 |
|
|
|
| 43 |
|
|
def add(x,y):
|
| 44 |
|
|
return x+y
|
| 45 |
|
|
|
| 46 |
|
|
def sum(seq):
|
| 47 |
|
|
return reduce(add, seq)
|
| 48 |
|
|
|
| 49 |
|
|
def readFile(XYZFileName):
|
| 50 |
|
|
print "reading XYZ file"
|
| 51 |
|
|
|
| 52 |
|
|
XYZFile = open(XYZFileName, 'r')
|
| 53 |
|
|
# Find number of atoms first
|
| 54 |
|
|
line = XYZFile.readline()
|
| 55 |
|
|
L = line.split()
|
| 56 |
|
|
nAtoms = int(L[0])
|
| 57 |
|
|
# skip comment line
|
| 58 |
|
|
line = XYZFile.readline()
|
| 59 |
|
|
for i in range(nAtoms):
|
| 60 |
|
|
line = XYZFile.readline()
|
| 61 |
|
|
L = line.split()
|
| 62 |
|
|
myIndex = i
|
| 63 |
|
|
indices.append(myIndex)
|
| 64 |
|
|
atomType = L[0]
|
| 65 |
|
|
atypes.append(atomType)
|
| 66 |
|
|
x = float(L[1])
|
| 67 |
|
|
y = float(L[2])
|
| 68 |
|
|
z = float(L[3])
|
| 69 |
|
|
positions.append([x, y, z])
|
| 70 |
|
|
XYZFile.close()
|
| 71 |
|
|
|
| 72 |
|
|
|
| 73 |
|
|
#find center of mass
|
| 74 |
|
|
Hmass = 1.0079
|
| 75 |
|
|
Cmass = 12.011
|
| 76 |
|
|
Omass = 15.999
|
| 77 |
|
|
Nmass = 14.007
|
| 78 |
|
|
Smass = 32.066
|
| 79 |
|
|
Xcom = 0.0
|
| 80 |
|
|
Ycom = 0.0
|
| 81 |
|
|
Zcom = 0.0
|
| 82 |
|
|
totalMass = 0.0
|
| 83 |
|
|
|
| 84 |
|
|
for i in range(0,int(nAtoms)):
|
| 85 |
|
|
if (atypes[i] == "H"):
|
| 86 |
|
|
myMass = Hmass
|
| 87 |
|
|
elif (atypes[i] == "C"):
|
| 88 |
|
|
myMass = Cmass
|
| 89 |
|
|
elif (atypes[i] == "O"):
|
| 90 |
|
|
myMass = Omass
|
| 91 |
|
|
elif (atypes[i] == "N"):
|
| 92 |
|
|
myMass = Nmass
|
| 93 |
|
|
elif (atypes[i] == "S"):
|
| 94 |
|
|
myMass = Smass
|
| 95 |
|
|
else:
|
| 96 |
|
|
print "unknown atom type!"
|
| 97 |
|
|
|
| 98 |
|
|
Xcom = Xcom + myMass * positions[i][0]
|
| 99 |
|
|
Ycom = Ycom + myMass * positions[i][1]
|
| 100 |
|
|
Zcom = Zcom + myMass * positions[i][2]
|
| 101 |
|
|
totalMass = totalMass + myMass
|
| 102 |
|
|
|
| 103 |
|
|
Xcom = Xcom / totalMass
|
| 104 |
|
|
Ycom = Ycom / totalMass
|
| 105 |
|
|
Zcom = Zcom / totalMass
|
| 106 |
|
|
|
| 107 |
|
|
COM = [Xcom, Ycom, Zcom]
|
| 108 |
|
|
|
| 109 |
|
|
#find inertia tensor matrix elements
|
| 110 |
|
|
|
| 111 |
|
|
I = na.zeros((3,3),type="Float")
|
| 112 |
|
|
|
| 113 |
|
|
for i in range(0,int(nAtoms)):
|
| 114 |
|
|
if (atypes[i] == "H"):
|
| 115 |
|
|
myMass = Hmass
|
| 116 |
|
|
elif (atypes[i] == "C"):
|
| 117 |
|
|
myMass = Cmass
|
| 118 |
|
|
elif (atypes[i] == "O"):
|
| 119 |
|
|
myMass = Omass
|
| 120 |
|
|
elif (atypes[i] == "N"):
|
| 121 |
|
|
myMass = Nmass
|
| 122 |
|
|
elif (atypes[i] == "S"):
|
| 123 |
|
|
myMass = Smass
|
| 124 |
|
|
else:
|
| 125 |
|
|
print "unknown atom type!"
|
| 126 |
|
|
|
| 127 |
|
|
dx = positions[i][0] - Xcom
|
| 128 |
|
|
dy = positions[i][1] - Ycom
|
| 129 |
|
|
dz = positions[i][2] - Zcom
|
| 130 |
|
|
|
| 131 |
|
|
I[0,0] = I[0,0] + myMass * ( dy * dy + dz * dz )
|
| 132 |
|
|
I[1,1] = I[1,1] + myMass * ( dx * dx + dz * dz )
|
| 133 |
|
|
I[2,2] = I[2,2] + myMass * ( dx * dx + dy * dy )
|
| 134 |
|
|
|
| 135 |
|
|
I[0,1] = I[0,1] - myMass * ( dx * dy )
|
| 136 |
|
|
I[0,2] = I[0,2] - myMass * ( dx * dz )
|
| 137 |
|
|
|
| 138 |
|
|
I[1,2] = I[1,2] - myMass * ( dy * dz )
|
| 139 |
|
|
|
| 140 |
|
|
I[1,0] = I[0,1]
|
| 141 |
|
|
I[2,0] = I[0,2]
|
| 142 |
|
|
I[2,1] = I[1,2]
|
| 143 |
|
|
|
| 144 |
|
|
print "Inertia Tensor:"
|
| 145 |
|
|
print I
|
| 146 |
|
|
print
|
| 147 |
|
|
|
| 148 |
|
|
(evals, evects) = la.eigenvectors(I)
|
| 149 |
|
|
print "evals:"
|
| 150 |
|
|
print evals
|
| 151 |
|
|
print
|
| 152 |
|
|
print "evects:"
|
| 153 |
|
|
print evects
|
| 154 |
|
|
print
|
| 155 |
|
|
|
| 156 |
|
|
print "COM = %f\t%f\t%f" % (Xcom, Ycom, Zcom)
|
| 157 |
|
|
for i in range(3):
|
| 158 |
|
|
myD = COM + 30*evects[i]
|
| 159 |
|
|
print "v%d = %f\t%f\t%f" % (i, myD[0], myD[1], myD[2])
|
| 160 |
|
|
|
| 161 |
|
|
|
| 162 |
|
|
|
| 163 |
|
|
def main(argv):
|
| 164 |
|
|
try:
|
| 165 |
|
|
opts, args = getopt.getopt(argv, "hx:", ["help", "xyz="])
|
| 166 |
|
|
except getopt.GetoptError:
|
| 167 |
|
|
usage()
|
| 168 |
|
|
sys.exit(2)
|
| 169 |
|
|
for opt, arg in opts:
|
| 170 |
|
|
if opt in ("-h", "--help"):
|
| 171 |
|
|
usage()
|
| 172 |
|
|
sys.exit()
|
| 173 |
|
|
elif opt in ("-x", "--xyz"):
|
| 174 |
|
|
XYZFileName = arg
|
| 175 |
|
|
global _haveXYZFileName
|
| 176 |
|
|
_haveXYZFileName = 1
|
| 177 |
|
|
|
| 178 |
|
|
|
| 179 |
|
|
if (_haveXYZFileName != 1):
|
| 180 |
|
|
usage()
|
| 181 |
|
|
print "No xyz file was specified"
|
| 182 |
|
|
sys.exit()
|
| 183 |
|
|
|
| 184 |
|
|
readFile(XYZFileName)
|
| 185 |
|
|
|
| 186 |
|
|
|
| 187 |
|
|
|
| 188 |
|
|
if __name__ == "__main__":
|
| 189 |
|
|
if len(sys.argv) == 1:
|
| 190 |
|
|
usage()
|
| 191 |
|
|
sys.exit()
|
| 192 |
|
|
main(sys.argv[1:])
|