Skip to content

Commit bb3a9c4

Browse files
authored
Matlabcode for track quadrupole vs no quadrupole
1 parent 5f34fd9 commit bb3a9c4

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

CLS_collab/line_transfer_v01.m

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
% clear
2+
%%
3+
4+
lim=[-0.01,+0.01,-0.05,+0.05];
5+
aper = ataperture('ap01', lim, 'AperturePass');
6+
L1 = 0.5; % Drift length
7+
L2 = 1.0; % Quadrupole length
8+
K1 = 1.95; % Quadrupole strength
9+
theta = pi/12; % Bending angle
10+
11+
% Define elements
12+
D1 = atdrift('D1', L1);
13+
QF = atquadrupole('QF', L2/10, K1);
14+
QD = atquadrupole('QD', L2/5, -1*K1);
15+
BEND = atsbend('BEND', L2/5, theta/5, 'PassMethod', 'BendLinearPass');
16+
BEND2 = atsbend('BEND', L2/5, 1*theta, 'PassMethod', 'BendLinearPass');
17+
18+
RFC = atrfcavity('RFC', 0, 3e6, 500e6, 328);
19+
20+
% Define beamline
21+
%ring = {aper, D1, BEND,BEND,BEND,BEND,BEND,QF,QF,QF,QF,QF, ...
22+
% BEND2,BEND2,BEND2,QD,QD,QD,QD,QD, BEND,BEND,BEND,BEND,BEND,...
23+
% RFC, aper};
24+
% ring = {aper, D1, ...
25+
% BEND,BEND,BEND,BEND,BEND,D1,aper,BEND,BEND,BEND,BEND,BEND,aper, RFC};
26+
ring = {aper, QF,QF,QF,QF,QF, BEND,BEND,BEND,BEND,BEND,aper,QF,QF,QF,QF,QF,...
27+
aper,BEND,BEND,BEND,BEND,BEND, aper, RFC};
28+
29+
%%
30+
31+
% Initial particle coordinates
32+
init_cond = [1e-3; 0; 1e-3; 0; 0; 0]; % (x, x', y, y', δ, z)
33+
[rout, lossinfo] = linepass(ring, init_cond);
34+
%% 100 particles with random initial conditions
35+
N = 100;
36+
init_conditions = 1e-3 * randn(6, N);
37+
38+
final_states = linepass(ring, init_conditions);
39+
%% Track for Multiple Turns
40+
num_turns = 100;
41+
tracked_states = ringpass(ring, init_conditions, num_turns);
42+
43+
%%
44+
[twiss, tune, chrom] = atlinopt(ring, 0);
45+
twiss;
46+
%%
47+
init_coords = [-1e-3; 1e-3; 0; 0; 0; 0]; % (x = 1mm, all else 0)
48+
init_coords = [1e-3, 0, -1e-3;
49+
0, 0, 0;
50+
0, 0, 0;
51+
0, 0, 0;
52+
0, 0, 0;
53+
0.10, -0.1, -0.10]; % 6D phase space for each particle
54+
55+
num_particles = 100;
56+
57+
% Generate random initial conditions (Gaussian distribution)
58+
% x0 = 5e-3 * randn(1, num_particles); % x (horizontal position)
59+
% xp0 = 5e-3 * randn(1, num_particles); % x' (horizontal angle)
60+
% y0 = 1e-6 * randn(1, num_particles); % y (vertical position)
61+
% yp0 = 1e-6 * randn(1, num_particles); % y' (vertical angle)
62+
% dp0 = 1e-4 * randn(1, num_particles); % δ (energy deviation)
63+
% ct0 = 1e-6 * randn(1, num_particles); % ct (path length deviation)
64+
65+
% Create initial 6D coordinates matrix
66+
init_coords = [x0; xp0; y0; yp0; dp0; ct0];
67+
68+
[num_vars, num_particles] = size(init_coords);
69+
num_elements = length(ring);
70+
71+
% Store trajectory for each element
72+
trajectory = zeros(num_vars, num_elements, num_particles);
73+
74+
% Track element by element
75+
for i = 1:num_elements
76+
[trajectory(:, i, :) ,lost]= linepass(ring(1:i), init_coords);
77+
end
78+
%%
79+
spos = findspos(ring, 1:length(ring));
80+
figure;
81+
hold on;
82+
for j = 1:num_particles
83+
plot(spos , squeeze(trajectory(1, :, j)), '-ko', 'DisplayName', ['Particle ' num2str(j)]);
84+
end
85+
%legend;
86+
% Loop through elements and plot by type
87+
for i = 1:num_elements
88+
element = ring{i};
89+
90+
% Plot Dipoles
91+
if isfield(element, 'BendingAngle') && element.BendingAngle ~= 0
92+
plot([spos(i), spos(i+1)], [0, 0], 'b', 'LineWidth', 5); % Blue for Dipoles
93+
end
94+
95+
% Plot Quadrupoles
96+
if isfield(element, 'K') && element.K ~= 0
97+
plot([spos(i), spos(i+1)], [0, 0], 'r', 'LineWidth', 6); % Red for Quadrupoles
98+
end
99+
100+
% Plot Sextupoles
101+
if isfield(element, 'PolynomB') && length(element.PolynomB) > 2
102+
plot([spos(i), spos(i+1)], [0, 0], 'k', 'LineWidth', 2); % Green for Sextupoles
103+
end
104+
end
105+
106+
xlabel('Element position');
107+
ylim([-0.03 0.03]);
108+
xlim([0 3]);
109+
ylabel('x [m]');
110+
title('Particle Trajectories');
111+
sum(lost)
112+
grid on;
113+
hold off;
114+
%%

0 commit comments

Comments
 (0)