Python : Runtime Warning : Overflow encountered in square, add, multiply, substract
Question: I'm beginning with Python so I might be asking a not so subtle question, but
after quite a lot of research I couldn't resolve this error. I'm actually
trying to solve a physics problem using the Gray-Scott model, but I'm stuck at
the very end of the code : the results are not considered as numbers and
there's an overflow encountered in square, add, multiply and substract.

Anyone here have the slightest idea of where this comes from ?

Thanks !

These are the initial conditions of the problem I'm trying to solve :

    
    
    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']
    

Now here are my functions :

    
    
    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
    

I now want to print the results I'm looking for :

    
    
    U, V = gray_scott_solve(Du, Dv, dh, dt, nt, Uin, Vin, Nd1, Nd2)
    
    print(U[100,::40])
    

And I finally get this error :

    
    
    [ 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
    


Answer: As you have it written, your spatial step, `dh`, will be equal to zero in
python 2:

    
    
    n  = 192
    ...
    dh = 5 / ( n - 1 )
    

If you are using python 3 then dh will properly be treated as a float.

Otherwise, as was stated by @WarrenWeckesser, you are using the Method of
lines and integrating in time with the Forward Euler method, which with your
set of constraints (you said that your instructor specified your timestep and
other parameter values) is apparently unstable. However, using a Runge Kutta
Two method will work for your `dt` (I verified this), but your instructor may
have mentioned the time integration that you should use.

Regardless, if Runge Kutta Two seems daunting, use a second order central
space method:

    
    
    u_{n+1} = u_{n-1} + 2 * dt * f(t_n,u_n)
    

where `f(t,u)` is the right hand side and `u_{n-1}` is the value of `u` at
time `t_{n-1}`, or the backward euler method.

