source: MML/trunk/mml/at/calccoupling.m @ 4

Last change on this file since 4 was 4, checked in by zhangj, 11 years ago

Initial import--MML version from SOLEIL@2013

File size: 5.3 KB
Line 
1function [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
22DisplayFlag = 0;
23
24% Flag factory
25for 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
33end
34
35global THERING
36   
37L0 = findspos(THERING, length(THERING)+1);
38
39[PassMethod, ATIndex, FamName, PassMethodOld, ATIndexOld, FamNameOld] = setradiation('On');
40
41CavityState = getcavity;
42setcavity('On');
43
44
45%% Get all the AT elements that add radiation
46% bending magnets
47BendCell = findmemberof('BEND');
48iBend = family2atindex(BendCell);
49for 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
55end
56iBend = cell2mat(iBend(:));
57
58% quadrupoles
59QuadCell = findmemberof('QUAD');
60iQuad = family2atindex(QuadCell);
61for 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
67end
68iQuad = cell2mat(iQuad(:));
69
70% sextupoles
71SextCell = findmemberof('SEXT');
72iSext = family2atindex(SextCell);
73for 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
79end
80iSext = cell2mat(iSext(:));
81
82
83RadiationElemIndex = sort([iBend(:); iQuad(:); iSext(:)]');
84%RadiationElemIndex(find(isnan(RadiationElemIndex))) = [];
85
86[ENV, DP, DL] = ohmienvelope(THERING, RadiationElemIndex, 1:length(THERING)+1);
87
88sigmas = cat(2, ENV.Sigma);
89Tilt = cat(2, ENV.Tilt);
90spos = 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
96setpassmethod(ATIndexOld, PassMethodOld);
97setcavity(CavityState);
98
99
100% Calculate tilts
101Beta = cat(1,TwissData.beta);
102spos = cat(1,TwissData.SPos);
103
104% Apparent emittances
105Eta = cat(2,TwissData.Dispersion);
106EpsX = (sigmas(1,:).^2-Eta(1,:).^2*DP^2)./Beta(:,1)';
107EpsY = (sigmas(2,:).^2-Eta(3,:).^2*DP^2)./Beta(:,2)';
108
109% RMS tilt
110TiltRMS = norm(Tilt)/sqrt(length(Tilt));
111EtaY = Eta(3,:);
112
113EpsX0 = mean(EpsX);
114EpsY0 = mean(EpsY);
115EPS = [EpsX0, EpsY0];
116Ratio = EpsY0 / EpsX0;
117
118
119% Fix imaginary data
120% ohmienvelope seems to return complex when very close to zero
121if ~isreal(sigmas(1,:))
122    % Sigma is positive so this should be ok
123    sigmas(1,:) = abs(sigmas(1,:));
124end
125if ~isreal(sigmas(2,:))
126    % Sigma is positive so this should be ok
127    sigmas(2,:) = abs(sigmas(2,:));
128end
129       
130if 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);
138end
139
140
141if 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   
194end
195
Note: See TracBrowser for help on using the repository browser.