RuntimeWarning:在true_divide

时间:2019-07-03 20:06:15

标签: python division zero

所以基本上我有随时间变化的值(坐标)。我在func()中定义了它们,并用odeint和起始值对其进行了求解。现在,我试图检查它们是否每次都满足条件H = 0(H在代码中定义),否则我的轨迹是错误的。但是我看不到错误的来源,我也不明白为什么这行不通。有人有主意吗?如果需要更多信息,我正在对Schwarzschild黑洞周围的灯光轨道进行编程。 (如果H≠0,则说明它们不是在光子轨道上运行)。

我已经尝试过更改值并移动括号,但老实说,我真的不知道还有什么尝试

import numpy as np
from scipy.integrate import odeint
from sympy import *
import sympy as sp



# coordinates who won't change
ptheta = 0
rs = 2
t0 = 0
theta = sp.pi/2

def func(f, T, rs):
       # f
       t, r, theta, phi, pt, pr, ptheta, pphi = f
       # derivatives
       dtdT = (1+(rs/r))*pt # 1/(1-rs/r)
       drdT = (1-(rs/r))*pr
       dthetadT = 0 #ptheta/(r**2)
       dphidT = pphi/((r**2))#*(sp.sin(theta)**2))
       dptdT = 0
       dprdT = ((pphi**2)/(r**3))-((rs/2)/((r-rs)**2))-(((rs/2)*    (pr**2))/(r**2))
       dpthetadT = 0 #(cos(theta)*(ptheta)**2)/((r**2)*sp.sin(phi)**3)
       dpphidT = 0 #pphi/((r**2)*(sp.sin(theta)**2)) #0
       return(dtdT, drdT, dthetadT, dphidT, dptdT, dprdT, dpthetadT, dpphidT)

# initial conditions  
r0 = 12*rs
phi0 = sp.pi
pr0 = 0
pphi0 = 5 
pt0 = sqrt((((1-(rs/r0))**2)*(pr0**2)) + (pphi0**2)*(1-rs/r0)/(r0**2))
f0=[t0, r0, theta, phi0, pt0, pr0, ptheta, pphi0]
T = np.linspace(0, 2250, 9000)
xx=odeint(func, f0, T, args=(rs,))

# calling coordinates from odeint
r00 = xx[:, 1]
theta00 = xx[:, 2]
phi00 = xx[:, 3]
pt00 = xx[:, 4]
pr00 = xx[:, 5]
pphi00 = xx[:, 7]

# hamiltonian
H0 = -((1-rs/r00)**-1)*(pt00**2)/2 + (1-rs/r00)*(pr00**2)/2 +    (pphi00**2)/(2*(r00**2))

print(H0)
if np.amax(H0) < 6e-12:
print("Your results are correct")
else:
print("Your results are wrong")

我应该得到很小的汉密尔顿值(x e-17或更小),但我不断得到n e-02,这太大了以至于不能正确

0 个答案:

没有答案