我求解非线性Schrodinger(NLS)方程RK4算法:错误在Python
(1): i*u_t + 0.5*u_xx + abs(u)^2 * u = 0
施加傅立叶变换后,就变成:
(2): uhat_t = -0.5*i*k^2 * uhat + i * fft(abs(u)^2 * u)
其中uhat
是傅立叶变换的u
。上面的公式(2)是一个相当规范的IVP,可以用第四或者Runge-Kutta方法求解。这里是我的解决方程(2)代码:
import numpy as np
import math
from matplotlib import pyplot as plt
from matplotlib import animation
#----- Numerical integration of ODE via fixed-step classical Runge-Kutta -----
def RK4(TimeSpan,uhat0,nt):
h = float(TimeSpan[1]-TimeSpan[0])/nt
print h
t = np.empty(nt+1)
print np.size(t) # nt+1 vector
w = np.empty(t.shape+uhat0.shape,dtype=uhat0.dtype)
print np.shape(w) # nt+1 by nx matrix
t[0] = TimeSpan[0]
w[0,:] = uhat0 # enter initial conditions in w
for i in range(nt):
t[i+1] = t[i]+h
w[i+1,:] = RK4Step(t[i], w[i,:],h)
return w
def RK4Step(t,w,h):
k1 = h * uhatprime(t,w)
k2 = h * uhatprime(t+0.5*h, w+0.5*k1*h)
k3 = h * uhatprime(t+0.5*h, w+0.5*k2*h)
k4 = h * uhatprime(t+h, w+k3*h)
return w + (k1+2*k2+2*k3+k4)/6.
#----- Constructing the grid and kernel functions -----
L = 40
nx = 512
x = np.linspace(-L/2,L/2, nx+1)
x = x[:nx]
kx1 = np.linspace(0,nx/2-1,nx/2)
kx2 = np.linspace(1,nx/2, nx/2)
kx2 = -1*kx2[::-1]
kx = (2.* np.pi/L)*np.concatenate((kx1,kx2))
#----- Define RHS -----
def uhatprime(t, uhat):
u = np.fft.ifft(uhat)
z = -(1j/2.) * (kx**2) * uhat + 1j * np.fft.fft((abs(u)**2) * u)
return z
#------ Initial Conditions -----
u0 = 1./np.cosh(x)#+1./np.cosh(x-0.4*L)
uhat0 = np.fft.fft(u0)
#------ Solving for ODE -----
TimeSpan = [0,10.]
nt = 100
uhatsol = RK4(TimeSpan,uhat0,nt)
print np.shape(uhatsol)
print uhatsol[:6,:]
我打印出来的拳头6个步骤的迭代,在第6步发生的错误,我不明白为什么会这样。 6个步骤的结果是:
nls.py:44: RuntimeWarning: overflow encountered in square
z = -(1j/2.) * (kx**2) * uhat + 1j * np.fft.fft((abs(u)**2) * u)
(101, 512)
[[ 4.02123859e+01 +0.00000000e+00j -3.90186082e+01 +3.16101312e-14j
3.57681095e+01 -1.43322854e-14j ..., -3.12522653e+01 +1.18074871e-13j
3.57681095e+01 -1.20028987e-13j -3.90186082e+01 +1.62245217e-13j]
[ 4.02073593e+01 +2.01061092e+00j -3.90137309e+01 -1.95092228e+00j
3.57636385e+01 +1.78839803e+00j ..., -3.12483587e+01 -1.56260675e+00j
3.57636385e+01 +1.78839803e+00j -3.90137309e+01 -1.95092228e+00j]
[ 4.01015488e+01 +4.02524105e+00j -3.89110557e+01 -3.90585271e+00j
3.56695007e+01 +3.58076808e+00j ..., -3.11660830e+01 -3.12911766e+00j
3.56695007e+01 +3.58076808e+00j -3.89110557e+01 -3.90585271e+00j]
[ 3.98941946e+01 +6.03886019e+00j -3.87098310e+01 -5.85991079e+00j
3.54849686e+01 +5.37263725e+00j ..., -3.10047495e+01 -4.69562640e+00j
3.54849686e+01 +5.37263725e+00j -3.87098310e+01 -5.85991079e+00j]
[ 3.95847537e+01 +8.04663227e+00j -3.84095149e+01 -7.80840256e+00j
3.52095058e+01 +7.15970026e+00j ..., -3.07638375e+01 -6.25837011e+00j
3.52095070e+01 +7.15970040e+00j -3.84095155e+01 -7.80840264e+00j]
[ 1.47696187e+22 -7.55759947e+22j 1.47709575e+22 -7.55843420e+22j
1.47749677e+22 -7.56093844e+22j ..., 1.47816312e+22 -7.56511230e+22j
1.47749559e+22 -7.56093867e+22j 1.47709516e+22 -7.55843432e+22j]]
在第6步,迭代的值是疯狂的。 Aslo,溢出错误发生在这里。
任何帮助?谢谢!!!!
工作码在该行:'Z = - (1J/2)*(KX ** 2)* + uhat 1J * np.fft.fft((ABS(u)的** 2)* u)',它与您提供的函数(2)不同。 uhat和1j之间是否有'+'或'*'? –
我在方程(2)中犯了一个错误,它应该是'uhat_t = -0.5 * i * k^2 * uhat + i * fft(abs(u)^ 2 * u)'。 @桓昱曾。谢谢。 – Jundong