微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python中的概率密度剖面动画

如何解决Python中的概率密度剖面动画

我正在尝试绘制一维非线性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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?