admin管理员组文章数量:1122846
When I run the code below in VSC or directly run a script via the command line in linux, I get the sought animation. Since I have done most part of the work in jupyter notebook, which I am running locally (not google colab), I want to get the animation in the jupyter notebook which unfortunately is not providing the animation. I see a plot which is remaining static. Can you help make the animation run in jupyter notebook ? I thank you. Here is the code:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from numpy.fft import fft, ifft
# Define the kinetic operator (Fourier space)
def kinetic_operator(psi, N, mu, epsilon, tau):
k_vals = np.fft.fftfreq(N, epsilon) * 2 * np.pi # Discrete k-values for FFT
K_vals = (2 * (np.sin(np.pi * k_vals / N)**2) / (mu * epsilon**2)) # Eigenvalues of K
psi_k = fft(psi) # Transform to Fourier space
psi_k = psi_k * np.exp(-1j * tau * K_vals) # Apply kinetic operator in k-space
return ifft(psi_k) # Transform back to real space
# Define the potential operator (diagonal in real space)
def potential_operator(psi, V, tau, half=False):
factor = np.exp(-1j * V * tau / 2) if half else np.exp(-1j * V * tau)
return factor * psi
# Strang splitting integrator
def strang_splitting_integrator(psi, V, N, tau, mu, epsilon):
# Apply half-step potential, full-step kinetic, and another half-step potential
psi_half_potential = potential_operator(psi, V, tau, half=True)
psi_kinetic = kinetic_operator(psi_half_potential, N, mu, epsilon, tau)
psi_new = potential_operator(psi_kinetic, V, tau, half=True)
return psi_new
# Animation function with optimizations
def animate_wavefunction(N, V, psi0, mu, epsilon, tau, steps=200, interval=50):
x = np.linspace(0, N * epsilon, N)
psi = np.copy(psi0) # Starting point for the wavefunction
fig, ax = plt.subplots()
line_prob, = ax.plot(x, np.abs(psi)**2, color='g', linestyle='--', label="Probability density")
ax.set_ylim(0, 1) # Adjust based on the scale of |psi|^2
ax.set_xlim(0, N * epsilon)
ax.set_title("Time Evolution of Wavefunction")
ax.set_xlabel("Position")
ax.set_ylabel("Probability Density")
#plt.show()
# The update function for the animation
def update(frame):
nonlocal psi # Ensure we update the original psi
# Update the wavefunction using Strang-Splitting integrator
psi = strang_splitting_integrator(psi, V, N, tau, mu, epsilon)
line_prob.set_ydata(np.abs(psi)**2) # Update probability density
return line_prob, # Return the updated line object for blitting
# Create the animation using FuncAnimation
ani = FuncAnimation(fig, update, frames=range(steps), interval=interval)#, blit=True)
plt.show()
return ani
# Set up parameters for the simulation
N = 200 # Decrease the number of lattice points to improve performance
mu = 1.0 # Mass of particle
epsilon = 0.1 # Small spatial grid spacing for a better approximation of the continuum
tau = 0.1 # Time step for accurate evolution
steps = 200 # Number of time steps for animation
# Initial wavefunction (Gaussian wave packet)
x = np.linspace(0, N * epsilon, N)
psi0 = np.exp(-(x - N * epsilon / 2)**2 / (2.0 * (N * epsilon / 20)**2))
psi0 /= np.sqrt(epsilon) * np.linalg.norm(psi0) # Normalize wavefunction
# Define potential V (free particle case)
V = np.zeros(N) # No potential energy (free particle)
# Run the animation
ani = animate_wavefunction(N, V, psi0, mu, epsilon, tau, steps=steps, interval=50)
本文标签: pythonI can not get an animation in jupyter notebookStack Overflow
版权声明:本文标题:python - I can not get an animation in jupyter notebook - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736304877a1932391.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论