-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGifsMaker.py
More file actions
61 lines (47 loc) · 1.99 KB
/
Copy pathGifsMaker.py
File metadata and controls
61 lines (47 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib.animation import FuncAnimation
from Beams import *
def MakeGif_density(pos: np.ndarray, density: np.ndarray, beam=GaussianBeam(), file_name='density_vs_time'):
"""
Create a GIF animation of atomic density over time.
Parameters:
----------
file_name : str, optional
Name of the output GIF file.
"""
rho_array, zeta_array = pos
R, Z = np.meshgrid(rho_array * w0 * 1e3, zeta_array * beam.zR * 1e3)
fig, ax = plt.subplots(figsize=(7, 10))
# --- Density overlay with transparency ---
img_density = ax.imshow(density[0], extent=[R.min(), R.max(), Z.min(), Z.max()],
origin='lower', cmap='viridis', aspect='auto')
# --- Beam intensity as background ---
rho_dim = R / (w0 * 1e3)
zeta_dim = Z / (beam.zR * 1e3)
I = beam.intensity(rho_dim, zeta_dim)
I = I / I.max()
# img_intensity = ax.imshow(I, extent=[R.min(), R.max(), Z.min(), Z.max()],
# origin='lower', cmap='inferno', aspect='auto', alpha=0.1)
# overlay with alpha
cmap = plt.cm.inferno
cf = ax.contourf(R, Z, I, levels=50, cmap=cmap, alpha=0.1)
# make a mappable for the colorbar with opaque colors
sm = mpl.cm.ScalarMappable(norm=cf.norm, cmap=cmap)
sm.set_array([])
fig.colorbar(sm, ax=ax, label="Beam intensity")
ax.set_title(f'Density and Intensity distribution ({beam.name})')
ax.set_xlabel(r'$\rho$ (mm)')
ax.set_ylabel('z (mm)')
ax.set_ylim(0, Z.max())
# Colorbars
fig.colorbar(img_density, ax=ax, label="Atomic Density")
# fig.colorbar(img_intensity, ax=ax, label="Beam Intensity")
# --- Animation update ---
def update(i):
img_density.set_data(density[i])
return [img_density]
ani = FuncAnimation(fig, update, frames=int(0.7*len(density)), interval=100)
ani.save('./gifs/' + file_name + '.gif', writer='pillow', fps=10)
plt.close(fig)