feat: 新增 plot_wave_save_gif/plot_wave_save_mp4 开关
- 所有 input.txt 新增 plot_wave_save_gif / plot_wave_save_mp4 参数 - dynamics.py 将参数传入 plot_wave() - plot_wave.py 根据 save_gif/save_mp4 标志条件保存文件 - 默认均为 0(不输出文件,只显示动画窗口)
This commit is contained in:
+40
-29
@@ -83,8 +83,14 @@ def compute_energy(x, y, z, vx, vy, vz, masses, mass_arr,
|
||||
return ek_sys, us_sys, ug_sys, ugr_sys
|
||||
|
||||
|
||||
def plot_wave(output_dir):
|
||||
"""主绘图函数:读取 display.txt 并生成波形+能量动画。"""
|
||||
def plot_wave(output_dir, save_gif=False, save_mp4=False):
|
||||
"""主绘图函数:读取 display.txt 并生成波形+能量动画。
|
||||
|
||||
Args:
|
||||
output_dir: 输出目录(含 display.txt)
|
||||
save_gif: 是否保存 GIF
|
||||
save_mp4: 是否保存 MP4
|
||||
"""
|
||||
data = load_disp_data(output_dir)
|
||||
|
||||
n_frames = int(data["n_frames"])
|
||||
@@ -234,36 +240,41 @@ def plot_wave(output_dir):
|
||||
|
||||
ani = FuncAnimation(fig, update, frames=n_frames, interval=50, blit=True)
|
||||
|
||||
# ── 先输出 GIF(自动循环)──
|
||||
gif_path = os.path.join(output_dir, "wave_animation.gif")
|
||||
ani.save(gif_path, writer="pillow", fps=min(20, max(1, n_frames // 5)))
|
||||
print(f"[plot_wave] GIF 已保存: {gif_path}")
|
||||
# ── 输出 GIF ──
|
||||
if save_gif:
|
||||
gif_path = os.path.join(output_dir, "wave_animation.gif")
|
||||
ani.save(gif_path, writer="pillow", fps=min(20, max(1, n_frames // 5)))
|
||||
print(f"[plot_wave] GIF 已保存: {gif_path}")
|
||||
|
||||
# ── 再输出 MP4(需要 ffmpeg)──
|
||||
try:
|
||||
import matplotlib.animation as manim
|
||||
import matplotlib.pyplot as _plt
|
||||
|
||||
# 尝试通过 imageio_ffmpeg 定位 ffmpeg
|
||||
ffmpeg_path = None
|
||||
# ── 输出 MP4(需要 ffmpeg)──
|
||||
gif_path = None
|
||||
if save_gif:
|
||||
gif_path = os.path.join(output_dir, "wave_animation.gif")
|
||||
if save_mp4:
|
||||
try:
|
||||
import imageio_ffmpeg
|
||||
ffmpeg_path = imageio_ffmpeg.get_ffmpeg_exe()
|
||||
except Exception:
|
||||
pass
|
||||
if ffmpeg_path and os.path.exists(ffmpeg_path):
|
||||
_plt.rcParams['animation.ffmpeg_path'] = ffmpeg_path
|
||||
import matplotlib.animation as manim
|
||||
import matplotlib.pyplot as _plt
|
||||
|
||||
ffps = min(20, max(1, n_frames // 5))
|
||||
writer = manim.FFMpegWriter(fps=ffps, codec="libx264",
|
||||
extra_args=["-pix_fmt", "yuv420p"])
|
||||
mp4_path = os.path.join(output_dir, "wave_animation.mp4")
|
||||
ani.save(mp4_path, writer=writer)
|
||||
print(f"[plot_wave] MP4 已保存: {mp4_path}")
|
||||
except FileNotFoundError:
|
||||
print("[plot_wave] 警告: 未找到 ffmpeg,跳过 MP4 输出")
|
||||
except Exception as e:
|
||||
print(f"[plot_wave] 警告: MP4 输出失败 ({e}),跳过")
|
||||
# 尝试通过 imageio_ffmpeg 定位 ffmpeg
|
||||
ffmpeg_path = None
|
||||
try:
|
||||
import imageio_ffmpeg
|
||||
ffmpeg_path = imageio_ffmpeg.get_ffmpeg_exe()
|
||||
except Exception:
|
||||
pass
|
||||
if ffmpeg_path and os.path.exists(ffmpeg_path):
|
||||
_plt.rcParams['animation.ffmpeg_path'] = ffmpeg_path
|
||||
|
||||
ffps = min(20, max(1, n_frames // 5))
|
||||
writer = manim.FFMpegWriter(fps=ffps, codec="libx264",
|
||||
extra_args=["-pix_fmt", "yuv420p"])
|
||||
mp4_path = os.path.join(output_dir, "wave_animation.mp4")
|
||||
ani.save(mp4_path, writer=writer)
|
||||
print(f"[plot_wave] MP4 已保存: {mp4_path}")
|
||||
except FileNotFoundError:
|
||||
print("[plot_wave] 警告: 未找到 ffmpeg,跳过 MP4 输出")
|
||||
except Exception as e:
|
||||
print(f"[plot_wave] 警告: MP4 输出失败 ({e}),跳过")
|
||||
|
||||
# ── 最后显示动画窗口 ──
|
||||
plt.show()
|
||||
|
||||
Reference in New Issue
Block a user