-
Notifications
You must be signed in to change notification settings - Fork 21
/
gcutil.py
303 lines (270 loc) · 10.5 KB
/
gcutil.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# UTILITY TO CONVERT BETWEEN XYZ AND Z-MATRIX GEOMETRIES
# Copyright 2017 Robert A Shaw
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the Software
# is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# Utilities for gc.py
import numpy as np
from scipy.spatial.distance import cdist
def replace_vars(vlist, variables):
""" Replaces a list of variable names (vlist) with their values
from a dictionary (variables).
"""
for i, v in enumerate(vlist):
if v in variables:
vlist[i] = variables[v]
else:
try:
# assume the "variable" is a number
vlist[i] = float(v)
except:
print("Problem with entry " + str(v))
def readxyz(filename):
""" Reads in a .xyz file in the standard format,
returning xyz coordinates as a numpy array
and a list of atom names.
"""
xyzf = open(filename, 'r')
xyzarr = np.zeros([1, 3])
atomnames = []
if not xyzf.closed:
# Read the first line to get the number of particles
npart = int(xyzf.readline())
# and next for title card
title = xyzf.readline()
# Make an N x 3 matrix of coordinates
xyzarr = np.zeros([npart, 3])
i = 0
for line in xyzf:
words = line.split()
if (len(words) > 3):
atomnames.append(words[0])
xyzarr[i][0] = float(words[1])
xyzarr[i][1] = float(words[2])
xyzarr[i][2] = float(words[3])
i = i + 1
return (xyzarr, atomnames)
def readzmat(filename):
""" Reads in a z-matrix in standard format,
returning a list of atoms and coordinates.
"""
zmatf = open(filename, 'r')
atomnames = []
rconnect = [] # bond connectivity
rlist = [] # list of bond length values
aconnect = [] # angle connectivity
alist = [] # list of bond angle values
dconnect = [] # dihedral connectivity
dlist = [] # list of dihedral values
variables = {} # dictionary of named variables
if not zmatf.closed:
for line in zmatf:
words = line.split()
eqwords = line.split('=')
if len(eqwords) > 1:
# named variable found
varname = str(eqwords[0]).strip()
try:
varval = float(eqwords[1])
variables[varname] = varval
except:
print("Invalid variable definition: " + line)
else:
# no variable, just a number
# valid line has form
# atomname index1 bond_length index2 bond_angle index3 dihedral
if len(words) > 0:
atomnames.append(words[0])
if len(words) > 1:
rconnect.append(int(words[1]))
if len(words) > 2:
rlist.append(words[2])
if len(words) > 3:
aconnect.append(int(words[3]))
if len(words) > 4:
alist.append(words[4])
if len(words) > 5:
dconnect.append(int(words[5]))
if len(words) > 6:
dlist.append(words[6])
# replace named variables with their values
replace_vars(rlist, variables)
replace_vars(alist, variables)
replace_vars(dlist, variables)
return (atomnames, rconnect, rlist, aconnect, alist, dconnect, dlist)
def distance_matrix(xyzarr):
"""Returns the pairwise distance matrix between atom
from a set of xyz coordinates
"""
return cdist(xyzarr, xyzarr)
def angle(xyzarr, i, j, k):
"""Return the bond angle in degrees between three atoms
with indices i, j, k given a set of xyz coordinates.
atom j is the central atom
"""
rij = xyzarr[i] - xyzarr[j]
rkj = xyzarr[k] - xyzarr[j]
cos_theta = np.dot(rij, rkj)
sin_theta = np.linalg.norm(np.cross(rij, rkj))
theta = np.arctan2(sin_theta, cos_theta)
theta = 180.0 * theta / np.pi
return theta
def dihedral(xyzarr, i, j, k, l):
"""Return the dihedral angle in degrees between four atoms
with indices i, j, k, l given a set of xyz coordinates.
connectivity is i->j->k->l
"""
rji = xyzarr[j] - xyzarr[i]
rkj = xyzarr[k] - xyzarr[j]
rlk = xyzarr[l] - xyzarr[k]
v1 = np.cross(rji, rkj)
v1 = v1 / np.linalg.norm(v1)
v2 = np.cross(rlk, rkj)
v2 = v2 / np.linalg.norm(v2)
m1 = np.cross(v1, rkj) / np.linalg.norm(rkj)
x = np.dot(v1, v2)
y = np.dot(m1, v2)
chi = np.arctan2(y, x)
chi = -180.0 - 180.0 * chi / np.pi
if (chi < -180.0):
chi = chi + 360.0
return chi
def write_zmat(xyzarr, distmat, atomnames, rvar=False, avar=False, dvar=False):
"""Prints a z-matrix from xyz coordinates, distances, and atomnames,
optionally with the coordinate values replaced with variables.
"""
npart, ncoord = xyzarr.shape
rlist = [] # list of bond lengths
alist = [] # list of bond angles (degrees)
dlist = [] # list of dihedral angles (degrees)
if npart > 0:
# Write the first atom
print(atomnames[0])
if npart > 1:
# and the second, with distance from first
n = atomnames[1]
rlist.append(distmat[0][1])
if (rvar):
r = 'R1'
else:
r = '{:>11.5f}'.format(rlist[0])
print('{:<3s} {:>4d} {:11s}'.format(n, 1, r))
if npart > 2:
n = atomnames[2]
rlist.append(distmat[0][2])
if (rvar):
r = 'R2'
else:
r = '{:>11.5f}'.format(rlist[1])
alist.append(angle(xyzarr, 2, 0, 1))
if (avar):
t = 'A1'
else:
t = '{:>11.5f}'.format(alist[0])
print('{:<3s} {:>4d} {:11s} {:>4d} {:11s}'.format(n, 1, r, 2, t))
if npart > 3:
for i in range(3, npart):
n = atomnames[i]
rlist.append(distmat[i-3][i])
if (rvar):
r = 'R{:<4d}'.format(i)
else:
r = '{:>11.5f}'.format(rlist[i-1])
alist.append(angle(xyzarr, i, i-3, i-2))
if (avar):
t = 'A{:<4d}'.format(i-1)
else:
t = '{:>11.5f}'.format(alist[i-2])
dlist.append(dihedral(xyzarr, i, i-3, i-2, i-1))
if (dvar):
d = 'D{:<4d}'.format(i-2)
else:
d = '{:>11.5f}'.format(dlist[i-3])
print('{:3s} {:>4d} {:11s} {:>4d} {:11s} {:>4d} {:11s}'.format(n, i-2, r, i-1, t, i, d))
if (rvar):
print(" ")
for i in range(npart-1):
print('R{:<4d} = {:>11.5f}'.format(i+1, rlist[i]))
if (avar):
print(" ")
for i in range(npart-2):
print('A{:<4d} = {:>11.5f}'.format(i+1, alist[i]))
if (dvar):
print(" ")
for i in range(npart-3):
print('D{:<4d} = {:>11.5f}'.format(i+1, dlist[i]))
def write_xyz(atomnames, rconnect, rlist, aconnect, alist, dconnect, dlist):
"""Prints out an xyz file from a decomposed z-matrix"""
npart = len(atomnames)
print(npart)
print('INSERT TITLE CARD HERE')
# put the first atom at the origin
xyzarr = np.zeros([npart, 3])
if (npart > 1):
# second atom at [r01, 0, 0]
xyzarr[1] = [rlist[0], 0.0, 0.0]
if (npart > 2):
# third atom in the xy-plane
# such that the angle a012 is correct
i = rconnect[1] - 1
j = aconnect[0] - 1
r = rlist[1]
theta = alist[0] * np.pi / 180.0
x = r * np.cos(theta)
y = r * np.sin(theta)
a_i = xyzarr[i]
b_ij = xyzarr[j] - xyzarr[i]
if (b_ij[0] < 0):
x = a_i[0] - x
y = a_i[1] - y
else:
x = a_i[0] + x
y = a_i[1] + y
xyzarr[2] = [x, y, 0.0]
for n in range(3, npart):
# back-compute the xyz coordinates
# from the positions of the last three atoms
r = rlist[n-1]
theta = alist[n-2] * np.pi / 180.0
phi = dlist[n-3] * np.pi / 180.0
sinTheta = np.sin(theta)
cosTheta = np.cos(theta)
sinPhi = np.sin(phi)
cosPhi = np.cos(phi)
x = r * cosTheta
y = r * cosPhi * sinTheta
z = r * sinPhi * sinTheta
i = rconnect[n-1] - 1
j = aconnect[n-2] - 1
k = dconnect[n-3] - 1
a = xyzarr[k]
b = xyzarr[j]
c = xyzarr[i]
ab = b - a
bc = c - b
bc = bc / np.linalg.norm(bc)
nv = np.cross(ab, bc)
nv = nv / np.linalg.norm(nv)
ncbc = np.cross(nv, bc)
new_x = c[0] - bc[0] * x + ncbc[0] * y + nv[0] * z
new_y = c[1] - bc[1] * x + ncbc[1] * y + nv[1] * z
new_z = c[2] - bc[2] * x + ncbc[2] * y + nv[2] * z
xyzarr[n] = [new_x, new_y, new_z]
# print results
for i in range(npart):
print('{:<4s}\t{:>11.5f}\t{:>11.5f}\t{:>11.5f}'.format(atomnames[i], xyzarr[i][0], xyzarr[i][1], xyzarr[i][2]))