我从Python开始,所以我可能会问一个不那么微妙的问题,但经过大量的研究后我无法解决这个错误。 我实际上是尝试使用Gray-Scott模型来解决物理问题,但是我被困在代码的最后:结果不被视为数字,并且在square,add,multiply和substract中遇到溢出
这里的任何人都对这来自哪里有任何想法?
谢谢!
这些是我试图解决的问题的初始条件:
n = 192
Du, Dv, F, k = 0.00016, 0.00008, 0.035, 0.065
dh = 5/(n-1)
T = 8000
dt = .9 * dh**2 / (4*max(Du,Dv))
nt = int(T/dt)
uvinitial = numpy.load('./uvinitial.npz')
Uin = uvinitial['U']
Vin = uvinitial['V']
现在这是我的职能:
def Nd1(U,V) :
return - U*(V)**2 + F*(1-U)
def Nd2(U,V) :
return U*(V)**2 -(F+k)*V
def gray_scott_solve(Du, Dv, dh, dt, nt, Uin, Vin, Nd1, Nd2):
Uplus = Uin.copy()
Vplus = Vin.copy()
for n in range(nt):
U = Uplus.copy()
V = Vplus.copy()
Uplus[1:-1,1:-1] = ( Nd1(U[1:-1,1:-1], V[1:-1,1:-1]) + Du/(dh**2) \
*(U[2:,1:-1] + U[:-2,1:-1] - 4*U[1:-1,1:-1]) \
+ U[1:-1,2:] + U[1:-1,:-2] )*dt \
+ U[1:-1,1:-1]
Uplus[:,-1] = Uplus[:,-2]
Uplus[-1,:] = Uplus[-2,:]
Uplus[:,0] = Uplus[:,1]
Uplus[0,:] = Uplus[1,:]
Vplus[1:-1,1:-1] = ( Nd2(U[1:-1,1:-1], V[1:-1,1:-1]) + Du/(dh**2) \
*(V[2:,1:-1] + V[:-2,1:-1] - 4*V[1:-1,1:-1]) \
+ V[1:-1,2:] + V[1:-1,:-2] )*dt \
+ V[1:-1,1:-1]
Vplus[:,-1] = Vplus[:,-2]
Vplus[-1,:] = Vplus[-2,:]
Vplus[:,0]= Vplus[:,1]
Vplus[0,:]= Vplus[1,:]
return U, V
我现在想要打印我正在寻找的结果:
U, V = gray_scott_solve(Du, Dv, dh, dt, nt, Uin, Vin, Nd1, Nd2)
print(U[100,::40])
我终于得到了这个错误:
[ nan nan nan nan nan]
C:\Users\Anto\Anaconda3\lib\site-packages\ipykernel\__main__.py:2: RuntimeWarning: overflow encountered in square from ipykernel import kernelapp as app
C:\Users\Anto\Anaconda3\lib\site-packages\ipykernel\__main__.py:2: RuntimeWarning: overflow encountered in multiply from ipykernel import kernelapp as app
C:\Users\Anto\Anaconda3\lib\site-packages\ipykernel\__main__.py:5: RuntimeWarning: overflow encountered in square
C:\Users\Anto\Anaconda3\lib\site-packages\ipykernel\__main__.py:5: RuntimeWarning: overflow encountered in multiply
C:\Users\Anto\Anaconda3\lib\site-packages\ipykernel\__main__.py:11: RuntimeWarning: invalid value encountered in add
C:\Users\Anto\Anaconda3\lib\site-packages\ipykernel\__main__.py:11: RuntimeWarning: overflow encountered in multiply
C:\Users\Anto\Anaconda3\lib\site-packages\ipykernel\__main__.py:11: RuntimeWarning: invalid value encountered in subtract
C:\Users\Anto\Anaconda3\lib\site-packages\ipykernel\__main__.py:18: RuntimeWarning: invalid value encountered in add
C:\Users\Anto\Anaconda3\lib\site-packages\ipykernel\__main__.py:18: RuntimeWarning: overflow encountered in multiply
C:\Users\Anto\Anaconda3\lib\site-packages\ipykernel\__main__.py:18: RuntimeWarning: invalid value encountered in subtract
答案 0 :(得分:1)
正如你所写,你的空间步骤dh
将在python 2中等于零:
n = 192
...
dh = 5 / ( n - 1 )
如果您正在使用python 3,那么dh将被正确地视为浮动。
否则,正如@WarrenWeckesser所说,你正在使用线条方法并使用前向欧拉方法进行积分,该方法与你的约束条件(你说你的教练指定你的时间步长和其他参数值)是显然不稳定。但是,使用Runge Kutta Two方法适用于dt
(我已经验证了这一点),但您的教师可能已经提到了您应该使用的时间集成。
无论如何,如果Runge Kutta Two看起来令人生畏,请使用二阶中心空间方法:
u_{n+1} = u_{n-1} + 2 * dt * f(t_n,u_n)
其中f(t,u)
是右侧,u_{n-1}
是时间u
的{{1}}值,或后向欧拉方法。