/
asym_rotor.py
362 lines (258 loc) · 9.13 KB
/
asym_rotor.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import scipy as sp
import pylab as pl
import numpy as nm
import ctypes as ct
from scipy import exp, sin, cos, tan, log, array, zeros, ones, r_, c_, dot, \
pi, newaxis, rand, randn, sqrt, flatnonzero, arange, cumsum
# useful physical constants
amu = 1.67e-27
debye = 3.33e-30
eps0 = 8.85e-12
fpe0 = 4. * pi * eps0
kB = 1.3806e-23
eV = 1.602e-19
ang = 1e-10
muB = 9.2741e-24 # J/T
mu0 = 4. * pi * 1e-7
# in our nice unit system hbar = 6.311 amu A^2 / ps
hbar_au = 6.311
kB_au = 0.82
# these are python routines to work with rigid body animations
# we want to convert quaternions into orientation vectors etc..
_libar = nm.ctypeslib.load_library('libar.dylib', '.')
_libar.asym_rotor.argtypes = [
ct.c_double, ct.c_double, ct.c_double,
nm.ctypeslib.ndpointer(dtype = nm.double),
nm.ctypeslib.ndpointer(dtype = nm.double),
ct.c_ulong,
ct.c_double,
ct.c_double, ct.c_double, ct.c_double,
ct.c_double, ct.c_double, ct.c_double
]
_libar.asym_rotor.restype = ct.c_int
_libar.asym_rotor_muz.argtypes = [
ct.c_double, ct.c_double, ct.c_double,
nm.ctypeslib.ndpointer(dtype = nm.double),
ct.POINTER(ct.c_double),
ct.c_ulong,
ct.c_double,
ct.c_double, ct.c_double, ct.c_double,
ct.c_double, ct.c_double, ct.c_double
]
_libar.asym_rotor_muz.restype = ct.c_int
_libar.test_byref.argtypes = [ct.POINTER(ct.c_double)]
_libar.test_byref.restype = ct.c_int
def test_byref():
t = ct.c_double()
_libar.test_byref(ct.byref(t))
return t
def asym_rotor(t0, t1, t2, max_tstep, qt0, F, mu, I):
qt0 = nm.asarray(qt0, dtype=nm.double)
if len(qt0) != 7:
print "qt0 must have 7 arguments"
return
# this should be a max_tstep x 8 array
qt = nm.zeros((max_tstep,8), dtype=nm.double)
mu1, mu2, mu3 = mu
I1, I2, I3 = I
ts = _libar.asym_rotor(t0, t1, t2,
qt0,
qt, max_tstep,
F,
mu1, mu2, mu3,
I1, I2, I3)
print "ts = ", ts
if ts < max_tstep:
return qt[:ts,:]
else:
return qt
def asym_rotor_muz(t0, t1, t2, max_tstep, qt0, F, mu, I):
qt0 = nm.asarray(qt0, dtype=nm.double)
if len(qt0) != 7:
print "qt0 must have 7 arguments"
return
muz = ct.c_double()
mu1, mu2, mu3 = mu
I1, I2, I3 = I
ts = _libar.asym_rotor_muz(t0, t1,t2, qt0, ct.byref(muz), max_tstep, F, mu1,
mu2, mu3, I1, I2, I3)
print "ts = ", ts
return muz
def inertia_tensor(q, m):
I = zeros((3,3))
for (i,j) in [(a,b) for a in range(3) for b in range(3)]:
if i == j:
I[i,j] = sum(m[:] * (q[:,0]**2.0 + q[:,1]**2.0 + q[:,2]**2.0 -
q[:,i]*q[:,j]))
else:
I[i,j] = - sum(m[:]*q[:,i]*q[:,j])
return I
# routines to generate an array of random vectors
# making use of the built-in uniform and normal generators in numpy
def rand_vect_in_sphere(dim, n):
qs = zeros((dim, n))
rep = array(range(n))
while(True):
if len(rep) == 0:
return qs
qs[:,rep] = 2.0*rand(dim, len(rep)) - 1.0
rep = rep[flatnonzero(sqrt(sum(qs[:,rep]**2.0, 0)) > 1.0)]
# I've been told that you can just take a nd normal vector and normalize it
# I would really like to take a few minutes to try and prove this however
def rand_unit_vect(dim, n):
uvs = randn(dim,n)
uvs /= sqrt(sum(uvs**2.0,0))
return uvs
#def q_to_rotmatrix(q):
# """ map a quaternion to a rotation matrix """
# return array([[dot(q**2.0, array([+1.0, +1.0, -1.0, -1.0])),
# 2.0*(q[1]*q[2] + q[0]*q[3]),
# 2.0*(q[1]*q[3] - q[0]*q[2])],
# [2.0*(q[1]*q[2] - q[0]*q[3]),
# dot(q**2.0,array([+1.0, -1.0, +1.0, -1.0])),
# 2.0*(q[2]*q[3] + q[0]*q[1])],
# [2.0*(q[1]*q[3] + q[0]*q[2]),
# 2.0*(q[2]*q[3] - q[0]*q[1]),
# dot(q**2.0, array([+1.0, -1.0, -1.0, +1.0]))]])
def q_to_rotmatrix(q):
""" map a quaternion to a rotation matrix """
Q = array(
[[ dot(q**2.0, array([+1.0, +1.0, -1.0, -1.0])),
2.0*(q[:,1]*q[:,2] + q[:,0]*q[:,3]),
2.0*(q[:,1]*q[:,3] - q[:,0]*q[:,2])],
[ 2.0*(q[:,1]*q[:,2] - q[:,0]*q[:,3]),
dot(q**2.0,array([+1.0, -1.0, +1.0, -1.0])),
2.0*(q[:,2]*q[:,3] + q[:,0]*q[:,1])],
[ 2.0*(q[:,1]*q[:,3] + q[:,0]*q[:,2]),
2.0*(q[:,2]*q[:,3] - q[:,0]*q[:,1]),
dot(q**2.0, array([+1.0, -1.0, -1.0, +1.0]))]])
return Q.swapaxes(0,2).swapaxes(1,2)
# this should not change with time
def total_energy(qt, F, mu, mol_I):
""" compute the total energy """
t = qt[0]
q = qt[1:5]
w = qt[5:8]
Q = q_to_rotmatrix(q)
T = dot(mol_I, w**2.0)/2.0 # kinetic energy
V = dot(mu, dot(Q.T, array([0.0, 0.0, F]))) # potential energy of dipole
return (T - V) * kB_au
def muz_bar(qt, F, mu, mol_I):
t = qt[:,0]
q = qt[:,1:5]
w = qt[:,5:8]
Q = q_to_rotmatrix(q)
# transform the dipole to space fixed coords
fsp = dot(Q, mu)
n = arange(fsp.shape[0])+1.0
fspav = cumsum(fsp,axis=0) / n[:,newaxis]
return fspav[-1,2]
def stats(qt, F, mu, mol_I):
"""
follow the motion of a trajectory
compute
kinetic energy
potential energy
total energy
projected dipole
total angular momentum
Jx
Jy
Jz
"""
t = qt[:,0]
q = qt[:,1:5]
w = qt[:,5:8]
print "w.shape = ", w.shape
print "q.shape = ", q.shape
print "mol_I.shape = ", mol_I.shape
# compute the third column of the inverse orientation matrix
invQz = array([
2.0 * (q[:,1]*q[:,3] + q[:,0]*q[:,2]),
2.0 * (q[:,2]*q[:,3] - q[:,0]*q[:,1]),
q[:,0]**2.0 - q[:,1]**2.0 - q[:,2]**2.0 + q[:,3]**2.0])
# kinetic energy
T = dot(w**2.0, mol_I)/2.0
print "T.shape", T.shape
V = dot(mu, F*invQz)
print "V.shape", V.shape
U = T + V
# projected dipole
Q = q_to_rotmatrix(q)
print "Q.shape = ", Q.shape
# transform the dipole to space fixed coords
fsp = dot(Q, mu)
n = arange(fsp.shape[0])+1.0
fspav = cumsum(fsp,axis=0) / n[:,newaxis]
print "fsp.shape = ", fsp.shape
J = w * mol_I[newaxis,:]
#print "Jbf.shape = ", Jbf.shape
#J = zeros((len(t), 3))
#J[:,0] = nm.sum(Q[:,0,:] * Jbf, axis=1)
#J[:,1] = nm.sum(Q[:,1,:] * Jbf, axis=1)
#J[:,2] = nm.sum(Q[:,2,:] * Jbf, axis=1)
print "J.shape = ", J.shape
return t, T, V, U, J[:,0], J[:,1], J[:,2], fsp, fspav
def angular_momentum(qt, F, mu, mol_I):
t = qt[0]
q = qt[1:5]
w = qt[5:8]
Q = q_to_rotmatrix(q)
return dot(Q.T, mol_I*w)
# in all qt is a state variable
# qt = array([t, q0, q1, q2, q3, w1, w2, w3])
if __name__ == "__main__":
print "running asym_rotor test"
# parameters of PABN molecule
au9I = array([ 6385.54243039, 8817.03576255, 15202.57819294])
au9mu = array([ 0.285, 0, 0 ]) * 0.208
F = 60.10
#mu = array([1.24, 0.0, 0.214])
mu = array([1.0/sqrt(3), 1.0/sqrt(3), 1.0/sqrt(3)])
#rot_consts = array([0.230, 0.0405, 0.034]) # given in amu A^2 / ps^2
rot_consts = array([0.1, 0.1, 0.1]) # given in amu A^2 / ps^2
mol_I = hbar_au / 2 / rot_consts
mol_I = au9I
mu = au9mu
print "mol_I = ", mol_I
#q0 = array([0.0, 1.0, 0.0, 0.0])
q0 = rand_unit_vect(4,1).ravel()
w0 = 0.2*rand_vect_in_sphere(3,1).ravel()
t0 = 200.0e3
t1 = 300.0e3
t2 = 1000.0e3
qt = asym_rotor(t0, t1, t2, 1000000, r_[q0, w0], F, mu, mol_I)
print "qt.shape = ", qt.shape
print "I = ", mol_I
print "kinetic energy = (K)", 0.5 * dot(w0.T, mol_I*w0) / kB_au
print "pF / kT = ", sqrt(sum(mu**2.0)) * F / 0.5 / dot(w0.T, mol_I*w0)
print "time at final time step is (ns) = ", qt[-1,0] / 1000.
print "now computing asym_rotor_muz: "
muzcomp = asym_rotor_muz(t0, t1, t2, 1000000, r_[q0, w0], F, mu, mol_I)
print "muzcomp = ", muzcomp
tsr = qt[:,0]
# field in rk time steps
ft = F * (tsr - t0) / (t1 - t0)
ft[flatnonzero(tsr < t0)] = 0.0;
ft[flatnonzero(tsr > t1)] = F
t, T, V, U, Jx, Jy, Jz, fsp, fspav = stats(qt, ft, mu, mol_I)
#t, T, V, U = stats(qt, ft, mu, mol_I)
pl.figure(figsize=(4,4))
pl.subplot(311)
pl.plot(t/1e3, T, 'g-', lw=0.5, alpha=0.5, label="T")
pl.plot(t/1e3, V, 'b-', lw=0.5, alpha=0.5, label="V")
pl.plot(t/1e3, U, 'r-', lw=1.5, label="U")
pl.subplot(312)
#pl.plot(t/1e3, sqrt(nm.sum(qt[:,1:5]**2.0,axis=1)), 'r-', label="|q|")
pl.plot(t/1e3, ft, 'r-', label="|q|")
pl.subplot(313)
#pl.plot(t/1e3, Jx, 'r-', alpha=0.5, label="Jx")
#pl.plot(t/1e3, Jy, 'g-', alpha=0.5, label="Jy")
#pl.plot(t/1e3, Jz, 'b-', alpha=0.5, label="Jz")
#pl.subplot(414)
pl.plot(t/1e3, fsp[:,2], 'b-', alpha=0.5, label="mu_z")
pl.plot(t/1e3, fspav[:,2], 'r-', label="<mu_z>")
#pl.plot(t/1e3, fsp[:,1], 'g-', label="mu_y")
#pl.plot(t/1e3, fsp[:,2], 'b-', label="mu_z")
pl.legend()
pl.show()