我试图显示扩散的灰色斯科特模型。即使我觉得自己的代码确实很正确,我仍会收到许多运行时警告错误。我的离散化有问题吗?
import numpy as np
import matplotlib.pyplot as plt
#parameters
N=128
F=.042
k=.062
Du=(2**-5)*(N**2/6.25)
Dv=(1**-5)*(N**2/6.25)
tend=1000
dt=tend/N
t=0
#start arrays
U=np.ones((N,N))
V=np.zeros((N,N))
#Initial Value Boxes (20x20 in middle)
low=int(((N/2)-10))
high=int(((N/2)+10))+1
U[low:high,low:high]=.5
V[low:high,low:high]=.25
#Random Noise
U+=.01*np.random.random((N,N))
V+=.01*np.random.random((N,N))
#Laplace
def Laplace(f):
return np.roll(f,1)+np.roll(f,-1)+np.roll(f,1,axis=False)+np.roll(f,-1,axis=False)-4*f
#Solve
pstep=100
for t in range(tend):
U+=((Du*Laplace(U))-(U*V*V)+(F*(1-U)))
V+=((Dv*Laplace(V))+(U*V*V)-(V*(F+k)))
if t%pstep ==0:
print(t//pstep)
plt.imshow(U, interpolation='bicubic',cmap=plt.cm.jet)
答案 0 :(得分:1)
好吧,我通过更改计算中的一些内容来使其工作,但主要是通过大量减少扩散系数并减少时间步长来更改数值稳定性。最终结果是,整个模拟在每个步骤之间的变化较小,因此更改的值要小得多。
您得到的错误是由于dU
和dV
的计算中浮点数的溢出,通过放慢整个过程(更多的时间步长),您不需要如此大量的dU
和dV
import numpy as np
import matplotlib.pyplot as plt
# parameters
N = 128
F = .042
k = .062
# Du=(2**-5)*(N**2/6.25) # These were way too high for the
# numeric stability given the timestep
Du = 0.1
# Dv=(1**-5)*(N**2/6.25)
Dv = 0.5
tend = 1000
dt = tend / N
t = 0
dt = 0.1 # Timestep -
# the smaller you go here, the faster you can let the diffusion coefficients be
# start arrays
U = np.ones((N, N))
V = np.zeros((N, N))
# Initial Value Boxes (20x20 in middle)
low = int(((N / 2) - 10))
high = int(((N / 2) + 10)) + 1
U[low:high, low:high] = .5
V[low:high, low:high] = .25
# Random Noise
U += .01 * np.random.random((N, N))
V += .01 * np.random.random((N, N))
# Laplace
def Laplace(f):
return np.roll(f, 1) + np.roll(f, -1) + np.roll(f, 1, axis=False) + np.roll(f,-1, axis=False) - 4 * f
# Solve
pstep = 100
for t in range(tend):
dU = ((Du * Laplace(U)) - (U * V * V) + (F * (1 - U))) * dt
dV = ((Dv * Laplace(V)) + (U * V * V) - (V * (F + k))) * dt
U += dU
V += dV
if t % pstep == 0:
print(t // pstep)
plt.imshow(U, interpolation='bicubic', cmap=plt.cm.jet, vmin=0, vmax=1)
当然,我所做的更改会稍微改变物理性质,您将需要更改t
和pstep
,以便使这些值有意义。还要检查您如何计算Du
和Dv
。如果这些值实际上应该是8000,那么您需要的时间步长要小得多。
供其他参考,here解释了Gray Scott模型