问题描述
我正在尝试绘制一维非线性Schrödinger方程https://en.wikipedia.org/wiki/Nonlinear_Schr%C3%B6dinger_equation的动画密度分布图,其广义系数受初始波函数的影响,该初始波函数是一个常数加一个扰动。运行代码时,出现错误:FileNotFoundError: [WinError 2] The system cannot find the file specified
。
我不明白为什么收到此消息。我确定要将mp4文件保存到我的项目文件夹中吗?
代码:
# computation and Plotting modules
import numpy as np
import matplotlib.pyplot as plt
# Animation modules
import matplotlib.animation as anim
# File modules
import os
N = 10000
# Coefficients
print("Example values might be -1,0 or 1.")
alpha = float(input("Choose a suitable value for the nonlinear coefficient: "))
beta = float(input("Choose a suitable value for the linear coefficient: "))
# First we need to establish the time and space steps.
nx = 256 # Number of uniform points on spatial axis
lx = 100 # Spatial axis domain length
dx = lx / nx # Space step
x = np.linspace(0,lx,nx,endpoint=False)
StepStart = 0 # First step
StepStop = 10000 # Final step
t = np.linspace(StepStart,StepStop,256)
dt = 1.e-8 # Integration time step
timeframes = np.linspace(0,4 * np.pi,1000)
# Plots Directory
'''
dirPlots = './Plots'
if not os.path.exists(dirPlots):
os.makedirs(dirPlots)
print('Plots file created. ')
'''
# Fourier space details
dk = 2 * np.pi / lx
k = np.fft.ifftshift(dk * np.arange(-nx/2,nx/2)) # FFT - Fast Fourier Transform
ksquare = np.square(k * x)
'''
Consider a stability check. We compare the integration time-step dt with the fastest linear period: the
wave with the maximal phase and therefore highest frequency. Since we want to trace out the solution,dt should
be smaller.
'''
MaxLinFreq = beta * np.max(ksquare)
FastestLinPeriod = 2 * np.pi / MaxLinFreq
if (dt > FastestLinPeriod):
print('Numerical instability detected. To achieve a stable result,choose a smaller dt.')
# Set the initial condition (initial wave function)
# Consider a constant initial wave function subjected to a minor perturbation.
A = float(input("Give an amplitude for the initial wave function: "))
eps = float(input("Choose a perturbation coefficient. Make it small!: "))
m = int(input("Choose an INTEGER. This ensures the perturbation is periodic on the boundaries: "))
k2 = float(2 * np.pi * m / lx)
psi_0 = A + eps * np.exp(1j * k2 * x)
psi = psi_0
framestep = StepStart
PsiStep = 1000 #
# Iterative formula (integration loop)
LinearPart = np.exp(-1j * beta * k * dt) # Linear component of numerical solution
for step in range(StepStart +1,StepStop + 1):
psi = psi * np.exp(1j * alpha * dt * np.abs(psi)**2) # Nonlinear component
psi = np.fft.fft(psi) # Map to Fourier space
psi = psi * LinearPart # Multiply by linear component
psi = np.fft.ifft(psi) # Invert FFT back to physical space
print('Numerical solution computed!')
# Now we computed the solutions,we can plot the density and phase profiles at different stages.
# Create data
my_step = 0.1
xdomain = np.linspace(0,endpoint=False)
sec = 1
densityfig = plt.figure(1,dpi=320)
plt.minorticks_on()
# Initial scatter plot
scatter = plt.scatter([],[],10)
def init():
scatter.set_offsets(np.c_[[],[]])
return(scatter)
# Create a function set data
def animate(i):
scatter.set_offsets(np.c_[[x],[np.abs(psi[i])**2]])
return scatter
animation = anim.FuncAnimation(densityfig,animate,init_func=init,frames = lx + 1,interval=1000*my_step,blit=False)
animation.save('Density_Profile_for_the_1D_Nonlinear_Schrödinger_Equation.mp4',writer=anim.FFMpegWriter(fps=1/my_step))
注意:对于输入值,我目前分别坚持1、1、1、0.0001和1。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)