#f,g are the dH/dq and -dH/dp respectively
def f(v1,v2):
alpha = (a*v1 - b*v2)/k
beta = (a*v2 - c*v1)/k
dot1 = (c-a)/(3*k) + (a*np.exp(alpha)/k - c*np.exp(beta)/k)/(1 + np.exp(alpha) + np.exp(beta))
dot2 = (b-a)/(3*k) + (a*np.exp(beta)/k - b*np.exp(alpha)/k)/(1 + np.exp(alpha) + np.exp(beta))
return dot1, dot2
def g(u1,u2):
dot1 = -1/3 + np.exp(u1)/(1 + np.exp(u1)+ np.exp(u2))
dot2 = -1/3 + np.exp(u2)/(1 + np.exp(u1)+ np.exp(u2))
return -dot1, -dot2
#these are the constants for 4th order symplectic rk method where c1 is for canonical position and c2 for canonical momentum cordinates. (As given by Ruth and Forest)
c1 = np.array((1.0/(2.0*(2.0-2.0**(1.0/3.0))), (1.0-2.0**(1.0/3.0))/(2.0*(2.0-2.0**(1.0/3.0))),(1.0-2.0**(1.0/3.0))/(2.0*(2.0-2.0**(1.0/3.0))), 1.0/(2.0*(2.0-2.0**(1.0/3.0)))))
c2 = np.array((1.0/(2.0-2.0**(1.0/3.0)), -2.0**(1.0/3.0)/(2.0-2.0**(1.0/3.0)),1.0/(2.0-2.0**(1.0/3.0)), 0.0))
#this is the function where the integration is happening and returns the 4 arrays which are time series of the phase-space variables
def sol(x0):
u1 = np.zeros(n+1)
u2 = np.zeros(n+1)
v1 = np.zeros(n+1)
v2 = np.zeros(n+1)
u1[0] = x0[0]
u2[0] = x0[1]
v1[0] = x0[2]
v2[0] = x0[3]
for k in xrange (n):
u10 = u1[k]
u20 = u2[k]
v10 = v1[k]
v20 = v2[k]
for i in xrange(len(c2)):
U1 = u10 + c1[i]*h*f(v10,v20)[0]
U2 = u20 + c1[i]*h*f(v10,v20)[1]
V1 = v10 + c2[i]*h*g(U1,U2)[0]
V2 = v20 + c2[i]*h*g(U1,U2)[1]
u10 = U1
u20 = U2
v10 = V1
v20 = V2
U1 = u10 + h*c1[-1]*f(v10,v20)[0]
U2 = u20 + h*c1[-1]*f(v10,v20)[1]
u10 = U1
u20 = U2
u1[k+1] = U1
u2[k+1] = U2
v1[k+1] = V1
v2[k+1] = V2
return u1,u2,v1,v2
当我为某些参数值(如 a、b、c 等)运行代码时,在 dot2 中出现溢出错误。对于其他参数,我得到一个无界的相空间,但哈密顿系统应该有一个有界的轨迹。
我的算法有问题吗?请指出错误。我可以提供整个代码和论文,我试图重现其结果。