Python:用双_标量捕获运行时溢出
我目前正在用RK4算法对一个相当深奥的系统进行物理模拟 它涉及奇点(例如r->0),并除以其中一个方程式中的半径会有奇点,发生这种情况后,我需要程序停止进一步的计算。一旦程序太接近奇点,我就内置了一个>中断操作,但有时用户可能无法选择足够好的阈值在无穷大出现之前中断。该阈值的值无法事先确定-系统在动力学上明显混乱。我已经决定尝试捕获异常(具体来说,RuntimeWarning:overflow在双_标量中遇到),以便用户知道他们选择的奇点阈值太低Python:用双_标量捕获运行时溢出,python,exception,overflow,physics,division,Python,Exception,Overflow,Physics,Division,我目前正在用RK4算法对一个相当深奥的系统进行物理模拟 它涉及奇点(例如r->0),并除以其中一个方程式中的半径会有奇点,发生这种情况后,我需要程序停止进一步的计算。一旦程序太接近奇点,我就内置了一个>中断操作,但有时用户可能无法选择足够好的阈值在无穷大出现之前中断。该阈值的值无法事先确定-系统在动力学上明显混乱。我已经决定尝试捕获异常(具体来说,RuntimeWarning:overflow在双_标量中遇到),以便用户知道他们选择的奇点阈值太低 for i in range(indices -
for i in range(indices - 1):
try:
if((theta0 == 0 or theta0 == pi) and i == 0):
print('Unstable or stable equilibrium chosen as initial value. Please change the initial angle.')
flag = True
break
if(variables[i][0] <= (singularitythreshold * r0)):
print('The trajectory came within the threshold for identifying a singularity (%.5f%%). The program has finished early to avoid infinities.' % (singularitythreshold * r0 * 100))
break
k1 = step * RKaccel(variables[i], times[i])
k2 = step * RKaccel(variables[i] + k1 / 2, times[i] + step/2)
k3 = step * RKaccel(variables[i] + k2 / 2, times[i] + step/2)
k4 = step * RKaccel(variables[i] + k3, times[i] + step)
variables[i + 1] = variables[i] + k1/6 + k2/3 + k3/3 + k4/6
except RuntimeWarning:
print('A Runtime Warning was triggered, indicating infinities as r -> 0. Increase the singularity threshold.')
flag = True
print('Plotting procedures have been abandoned to avoid nonsensical data.')
break
我已经解决了我的问题。该键正在使用seterr()。加入
seterr(all = 'raise')
改变
except RuntimeWarning:
到
我的异常被触发,所有问题都得到了解决。希望这对将来的人有所帮助
except RuntimeWarning:
except FloatingPointError: