1 | function [Tilt, Eta, Ratio, ENV, DP, DL, EPS] = calccoupling(varargin) |
---|
2 | %CALCCOUPLING - Calculates the coupling and tilt of the AT model |
---|
3 | % [Eta, Tilt, EmittanceRatio, ENV, DP, DL] = calccoupling |
---|
4 | % |
---|
5 | % OUTPUTS |
---|
6 | % 1. Tilt - Tilts of the emittance ellipse [radian] |
---|
7 | % 2. Eta - Emittance |
---|
8 | % 3. EmittanceRatio - median(EpsX) / median(EpsX) |
---|
9 | % 4-6. ENV, DP, DL - Output of ohmienvelope |
---|
10 | % 7. EPS - emmittance |
---|
11 | % |
---|
12 | % NOTES |
---|
13 | % 1. If there are no outputs, the coupling information will be plotted. |
---|
14 | % 2. It can be helpful the draw the lattice on top of a graph (hold on; drawlattice(Offset, Height);) |
---|
15 | % |
---|
16 | % See Also ohmienvelope |
---|
17 | |
---|
18 | % |
---|
19 | % Written by James Safranek |
---|
20 | % Modified by Laurent S. Nadolski |
---|
21 | |
---|
22 | DisplayFlag = 0; |
---|
23 | |
---|
24 | % Flag factory |
---|
25 | for i = length(varargin):-1:1 |
---|
26 | if strcmpi(varargin{i},'Display') |
---|
27 | DisplayFlag = 1; |
---|
28 | varargin(i) = []; |
---|
29 | elseif strcmpi(varargin{i},'NoDisplay') |
---|
30 | DisplayFlag = 0; |
---|
31 | varargin(i) = []; |
---|
32 | end |
---|
33 | end |
---|
34 | |
---|
35 | global THERING |
---|
36 | |
---|
37 | L0 = findspos(THERING, length(THERING)+1); |
---|
38 | |
---|
39 | [PassMethod, ATIndex, FamName, PassMethodOld, ATIndexOld, FamNameOld] = setradiation('On'); |
---|
40 | |
---|
41 | CavityState = getcavity; |
---|
42 | setcavity('On'); |
---|
43 | |
---|
44 | |
---|
45 | %% Get all the AT elements that add radiation |
---|
46 | % bending magnets |
---|
47 | BendCell = findmemberof('BEND'); |
---|
48 | iBend = family2atindex(BendCell); |
---|
49 | for ii = 1:length(iBend) |
---|
50 | if size(iBend{ii},2) > 1 |
---|
51 | iBend{ii} = sort(iBend{ii}(:)); |
---|
52 | nanindx = find(isnan(iBend{ii})); |
---|
53 | iBend{ii}(nanindx) = []; |
---|
54 | end |
---|
55 | end |
---|
56 | iBend = cell2mat(iBend(:)); |
---|
57 | |
---|
58 | % quadrupoles |
---|
59 | QuadCell = findmemberof('QUAD'); |
---|
60 | iQuad = family2atindex(QuadCell); |
---|
61 | for ii = 1:length(iQuad) |
---|
62 | if size(iQuad{ii},2) > 1 |
---|
63 | iQuad{ii} = sort(iQuad{ii}(:)); |
---|
64 | nanindx = find(isnan(iQuad{ii})); |
---|
65 | iQuad{ii}(nanindx) = []; |
---|
66 | end |
---|
67 | end |
---|
68 | iQuad = cell2mat(iQuad(:)); |
---|
69 | |
---|
70 | % sextupoles |
---|
71 | SextCell = findmemberof('SEXT'); |
---|
72 | iSext = family2atindex(SextCell); |
---|
73 | for ii = 1:length(iSext) |
---|
74 | if size(iSext{ii},2) > 1 |
---|
75 | iSext{ii} = sort(iSext{ii}(:)); |
---|
76 | nanindx = find(isnan(iSext{ii})); |
---|
77 | iSext{ii}(nanindx) = []; |
---|
78 | end |
---|
79 | end |
---|
80 | iSext = cell2mat(iSext(:)); |
---|
81 | |
---|
82 | |
---|
83 | RadiationElemIndex = sort([iBend(:); iQuad(:); iSext(:)]'); |
---|
84 | %RadiationElemIndex(find(isnan(RadiationElemIndex))) = []; |
---|
85 | |
---|
86 | [ENV, DP, DL] = ohmienvelope(THERING, RadiationElemIndex, 1:length(THERING)+1); |
---|
87 | |
---|
88 | sigmas = cat(2, ENV.Sigma); |
---|
89 | Tilt = cat(2, ENV.Tilt); |
---|
90 | spos = findspos(THERING, 1:length(THERING)+1); |
---|
91 | |
---|
92 | [TwissData, tune, chrom] = twissring(THERING, 0, 1:length(THERING)+1, 'chrom'); |
---|
93 | |
---|
94 | |
---|
95 | % Set the passmethods back |
---|
96 | setpassmethod(ATIndexOld, PassMethodOld); |
---|
97 | setcavity(CavityState); |
---|
98 | |
---|
99 | |
---|
100 | % Calculate tilts |
---|
101 | Beta = cat(1,TwissData.beta); |
---|
102 | spos = cat(1,TwissData.SPos); |
---|
103 | |
---|
104 | % Apparent emittances |
---|
105 | Eta = cat(2,TwissData.Dispersion); |
---|
106 | EpsX = (sigmas(1,:).^2-Eta(1,:).^2*DP^2)./Beta(:,1)'; |
---|
107 | EpsY = (sigmas(2,:).^2-Eta(3,:).^2*DP^2)./Beta(:,2)'; |
---|
108 | |
---|
109 | % RMS tilt |
---|
110 | TiltRMS = norm(Tilt)/sqrt(length(Tilt)); |
---|
111 | EtaY = Eta(3,:); |
---|
112 | |
---|
113 | EpsX0 = mean(EpsX); |
---|
114 | EpsY0 = mean(EpsY); |
---|
115 | EPS = [EpsX0, EpsY0]; |
---|
116 | Ratio = EpsY0 / EpsX0; |
---|
117 | |
---|
118 | |
---|
119 | % Fix imaginary data |
---|
120 | % ohmienvelope seems to return complex when very close to zero |
---|
121 | if ~isreal(sigmas(1,:)) |
---|
122 | % Sigma is positive so this should be ok |
---|
123 | sigmas(1,:) = abs(sigmas(1,:)); |
---|
124 | end |
---|
125 | if ~isreal(sigmas(2,:)) |
---|
126 | % Sigma is positive so this should be ok |
---|
127 | sigmas(2,:) = abs(sigmas(2,:)); |
---|
128 | end |
---|
129 | |
---|
130 | if nargout == 0 |
---|
131 | fprintf(' RMS Tilt = %f [degrees]\n', (180/pi) * TiltRMS); |
---|
132 | fprintf(' RMS Vertical Dispersion = %f [m]\n', norm(EtaY)/sqrt(length(EtaY))); |
---|
133 | fprintf(' Mean Horizontal Emittance = %f [nm rad]\n', 1e9*EpsX0); |
---|
134 | fprintf(' Mean Vertical Emittance = %f [nm rad]\n', 1e9*EpsY0); |
---|
135 | fprintf(' Emittance Ratio = %f%% \n', 100*Ratio); |
---|
136 | fprintf(' RMS energy spread =%.4e \n', DP); |
---|
137 | fprintf(' RMS bunch length = %.2e [mm]\n', DL*1e3); |
---|
138 | end |
---|
139 | |
---|
140 | |
---|
141 | if DisplayFlag |
---|
142 | figure |
---|
143 | subplot(2,2,1); |
---|
144 | plot(spos, Tilt*180/pi, '-') |
---|
145 | set(gca,'XLim',[0 spos(end)]) |
---|
146 | ylabel('Tilt [degrees]'); |
---|
147 | title(sprintf('Rotation Angle of the Beam Ellipse (RMS = %f)', (180/pi) * TiltRMS)); |
---|
148 | xlabel('s - Position [m]'); |
---|
149 | |
---|
150 | subplot(2,2,3); |
---|
151 | [AX, H1, H2] = plotyy(spos, 1e6*sigmas(1,:), spos, 1e6*sigmas(2,:)); |
---|
152 | |
---|
153 | set(AX(1),'XLim',[0 spos(end)]); |
---|
154 | set(AX(2),'XLim',[0 spos(end)]); |
---|
155 | |
---|
156 | title('Principal Axis of the Beam Ellipse'); |
---|
157 | set(get(AX(1),'Ylabel'), 'String', 'Horizontal [\mum]'); |
---|
158 | set(get(AX(2),'Ylabel'), 'String', 'Vertical [\mum]'); |
---|
159 | xlabel('s - Position [m]'); |
---|
160 | |
---|
161 | FontSize = get(get(AX(1),'Ylabel'),'FontSize'); |
---|
162 | set(get(AX(2),'Ylabel'), 'Color', 'r') |
---|
163 | set(H2,'Color', 'r'); |
---|
164 | set(AX(2),'Ycolor', 'r') |
---|
165 | |
---|
166 | |
---|
167 | subplot(2,2,2); |
---|
168 | plot(spos, 1e9 * EpsX); |
---|
169 | title('Horizontal Emittance'); |
---|
170 | ylabel(sprintf('\\fontsize{16}\\epsilon_x \\fontsize{%d}[nm rad]', FontSize)); |
---|
171 | xlabel('s - Position [m]'); |
---|
172 | xaxis([0 L0]); |
---|
173 | |
---|
174 | subplot(2,2,4); |
---|
175 | plot(spos, 1e9 * EpsY); |
---|
176 | title('Vertical Emittance'); |
---|
177 | ylabel(sprintf('\\fontsize{16}\\epsilon_y \\fontsize{%d}[nm rad]', FontSize)); |
---|
178 | xlabel('s - Position [m]'); |
---|
179 | xaxis([0 L0]); |
---|
180 | |
---|
181 | h = addlabel(.75, 0, sprintf(' Emittance Ratio = %f%% ', 100*Ratio), 10); |
---|
182 | set(h,'HorizontalAlignment','Center'); |
---|
183 | |
---|
184 | orient landscape; |
---|
185 | |
---|
186 | figure |
---|
187 | plot(spos,EpsY./EpsX*100) |
---|
188 | xlabel('s - Position [m]'); |
---|
189 | ylabel('Coupling value (%) from projected emittance'); |
---|
190 | title(sprintf('Mean Apparent emittance H:%f nm.rad V:%f pm.rad \n Mean coupling: %f %%', ... |
---|
191 | mean(EpsX)*1e9, mean(EpsY)*1e12, mean(EpsY./EpsX)*100)) |
---|
192 | axis tight |
---|
193 | |
---|
194 | end |
---|
195 | |
---|