Skip to content

Commit 0d10196

Browse files
authored
Create plot_quadrupole_strength_v01.py
1 parent d5eb79a commit 0d10196

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import at
2+
import at.plot
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
6+
7+
file_path = 'dba.mat'
8+
# Load lattice file (can be .mat or .lat)
9+
lattice = at.load_mat(file_path, mat_key='RING')
10+
plt.rcParams["figure.figsize"] = [10, 6]
11+
plt.rcParams["figure.dpi"] = 600
12+
plt.rcParams['lines.linewidth'] = 6
13+
14+
plt.rcParams["xtick.direction"] = 'inout'
15+
plt.rcParams["ytick.direction"] = 'inout'
16+
plt.rcParams["xtick.minor.visible"] = True
17+
plt.rcParams["ytick.minor.visible"] = True
18+
19+
plt.rcParams['xtick.major.size'] = 10
20+
plt.rcParams['ytick.major.size'] = 10
21+
22+
plt.rcParams['xtick.minor.size'] = 5
23+
plt.rcParams['xtick.labelsize'] = 24
24+
plt.rcParams['ytick.labelsize'] = 24
25+
plt.rcParams['xtick.top'] = True
26+
plt.rcParams['ytick.right'] = True
27+
plt.rcParams['axes.titlesize'] = 26
28+
plt.rcParams['font.size'] =20
29+
plt.rcParams['lines.linewidth'] = 2.3
30+
plt.rcParams['font.family']= 'Times New Roman'
31+
plt.rcParams['legend.loc']= 'upper right'
32+
plt.rcParams['legend.fontsize']= 'small'
33+
plt.rcParams['xtick.major.width']=1
34+
plt.rcParams['ytick.major.width']=1
35+
plt.rcParams['axes.grid'] = False
36+
plt.rcParams['axes.grid.which'] = 'major'
37+
custom_colors = ['blue', 'red', 'green']
38+
# Set the custom color cycle
39+
plt.rcParams['axes.prop_cycle'] = plt.cycler(color=custom_colors)
40+
# Plot beta functions
41+
lattice.plot_beta(s_range=[0,24])
42+
43+
# Step 2: Generate initial particle positions using a Gaussian distribution
44+
x = np.random.normal(0, sigma_x, num_particles) # x position
45+
y = np.random.normal(0, sigma_y, num_particles) # y position
46+
z = np.zeros(num_particles) # z position (assumed zero for simplicity)
47+
px = np.random.normal(0, sigma_px, num_particles) # x momentum
48+
py = np.random.normal(0, sigma_py, num_particles) # y momentum
49+
pz = np.random.normal(0, sigma_pz, num_particles) # z momentum
50+
51+
# Initialize particles in phase space (x, y, z, px, py, pz)
52+
particles = np.array([x, y, z, px, py, pz])
53+
r_in0 = particles.reshape(6, 1000)
54+
55+
plt.plot(x, y, marker='o', ls='none')
56+
# Track the particles through the lattice
57+
#tracked_particles = at.track(lattice, r_in0, 1000, 0)
58+
quad_positions = []
59+
quad_lengths = []
60+
quad_strengths = []
61+
position = 0.0
62+
for elem in range(len(lattice)//12):
63+
position += lattice[elem].Length
64+
if isinstance(lattice[elem], at.Quadrupole):
65+
print(lattice[elem].FamName, lattice[elem].K,' ', lattice[elem].Length)
66+
quad_positions.append(position- lattice[elem].Length/2)
67+
quad_lengths.append(lattice[elem].Length)
68+
quad_strengths.append(lattice[elem].K)
69+
# Plot the quadrupole strengths as vertical lines
70+
plt.plot( figsize=(14, 12))
71+
for pos, length, strength in zip(quad_positions, quad_lengths, quad_strengths):
72+
plt.plot([pos, pos], [0, strength], color="k", label="Quadrupole Strength" if pos == quad_positions[0] else "")
73+
#plt.plot([pos - length / 2, pos + length / 2], [strength, strength], color="red", linestyle="--", label="Length of Quadrupole" if pos == quad_positions[0] else "")
74+
plt.axhline(0, color="black", linestyle=":", linewidth=1)
75+
76+
# Add labels and legend
77+
plt.xlabel("s position [m]")
78+
plt.ylabel("Quadrupole Strength (K)")
79+
plt.title("Quadrupole Strengths and Lengths Along the Lattice")
80+
plt.legend()
81+
# Plot beta functions
82+
plt.plot(figsize=(10, 15))
83+
lattice.plot_beta(s_range=[0,len(lattice)//12])
84+
#plt.grid(True)

0 commit comments

Comments
 (0)