1 | function [TD, varargout] = twissline(LINE,DP,TWISSDATAIN,varargin); |
---|
2 | %TWISSLINE calculates linear optics functions for an UNCOUPLED transport line |
---|
3 | % |
---|
4 | % TwissData = TWISSLINE(LATTICE,DP,TWISSDATAIN) propagates twiss |
---|
5 | % parameters and closed orbit coordinates from the LINE entrance |
---|
6 | % given by TWISSDATAIN assuming constant energy deviation DP. |
---|
7 | % TWISSDATAIN is a 1-by-1 structure with the same field names |
---|
8 | % as the return argument. (See below) |
---|
9 | % !!! IMPORTANT: Since TWISSLINE does not search for closed orbit |
---|
10 | % its value at the entrance must be supplied in the |
---|
11 | % ClosedOrbit field of TWISSDATAIN structure. |
---|
12 | % |
---|
13 | % TwissData = TWISSLINE(LATTICE,DP,TWISSDATAIN,REFPTS) calculates Twiss parameters |
---|
14 | % and closed orbit coordinates at specified reference points REFPTS |
---|
15 | % |
---|
16 | % Note: REFPTS is an array of increasing indexes that |
---|
17 | % select elements from range 1 to length(LATTICE)+1. |
---|
18 | % See further explanation of REFPTS in the 'help' for FINDSPOS |
---|
19 | % |
---|
20 | % TwissData = TWISSLINE(...,'chrom', DDP) also calculates |
---|
21 | % linear dispersion. Dispersion is returned as one |
---|
22 | % of the fields in TwissData. |
---|
23 | % !!! Last argument DDP is a momentum deviation on top |
---|
24 | % of DP (the second argument) used to calculate and normalize |
---|
25 | % dispersion. If not supplied |
---|
26 | % the default value of 1e-8 is used. |
---|
27 | % |
---|
28 | % TwisData is a 1-by-REFPTS (1-by-1 if no REFPTS specified) structure array with fields: |
---|
29 | % ElemIndex - integer (element number) in the LINE |
---|
30 | % SPos - longitudinal position [m] |
---|
31 | % ClosedOrbit - closed orbit column vector with |
---|
32 | % components x, px, y, py (momentums, NOT angles) |
---|
33 | % Dispersion - dispersion orbit position 4-by-1 vector with |
---|
34 | % components [eta_x, eta_prime_x, eta_y, eta_prime_y]' |
---|
35 | % calculated with respect to the closed orbit with |
---|
36 | % momentum deviation DP |
---|
37 | % M44 - 4x4 transfer matrix M from the beginning of LINE |
---|
38 | % to the entrance of the element for specified DP [2] |
---|
39 | % beta - [betax, betay] horizontal and vertical Twiss parameter beta |
---|
40 | % alpha - [alphax, alphay] horizontal and vertical Twiss parameter alpha |
---|
41 | % mu - [mux, muy] horizontal and vertical betatron phase |
---|
42 | % !!! NOT 2*PI normalized |
---|
43 | % |
---|
44 | % Use CAT to get the data from fields of TwissData into MATLAB arrays. |
---|
45 | % Example: |
---|
46 | % >> twissdatain.ElemIndex=1; |
---|
47 | % >> twissdatain.SPos=0; |
---|
48 | % >> twissdatain.ClosedOrbit=zeros(4,1); |
---|
49 | % >> twissdatain.M44=eye(4); |
---|
50 | % >> twissdatain.beta= [8.1 8.1]; |
---|
51 | % >> twissdatain.alpha= [0 0]; |
---|
52 | % >> twissdatain.mu= [0 0]; |
---|
53 | % >> TD=twissline(THERING,0.0,twissdatain,1:length(THERING)); |
---|
54 | % >> BETA = cat(1,TD.beta); |
---|
55 | % >> S = cat(1,TD.SPos); |
---|
56 | % >> plot(S,BETA(:,1)) |
---|
57 | % |
---|
58 | % See also TWISSRING, LINOPT, TUNECHROM. |
---|
59 | |
---|
60 | % Laurent S. Nadolski, July, 9th 2004 |
---|
61 | % Correction bug dipersion computation |
---|
62 | |
---|
63 | DDP_default = 1e-8; |
---|
64 | NE = length(LINE); % Number of elements |
---|
65 | |
---|
66 | % Process input arguments |
---|
67 | switch nargin |
---|
68 | case 3 |
---|
69 | REFPTS = NE+1; |
---|
70 | CHROMFLAG = 0; |
---|
71 | case 4 |
---|
72 | if isnumeric(varargin{1}) |
---|
73 | REFPTS = varargin{1}; |
---|
74 | CHROMFLAG = 0; |
---|
75 | elseif ischar(varargin{1}) & strncmp(lower(varargin{1}),'chrom',5) |
---|
76 | CHROMFLAG = 1; |
---|
77 | REFPTS = NE+1; |
---|
78 | DDP = DDP_default; |
---|
79 | else |
---|
80 | error('Third argument must be a numeric array or string'); |
---|
81 | end |
---|
82 | case 5 |
---|
83 | if isnumeric(varargin{1}) |
---|
84 | REFPTS = varargin{1}; |
---|
85 | if ischar(varargin{2}) & strncmp(lower(varargin{2}),'chrom',5) |
---|
86 | CHROMFLAG = 1; |
---|
87 | DDP = DDP_default; |
---|
88 | else |
---|
89 | error('Fourth argument - wrong type'); |
---|
90 | end |
---|
91 | elseif ischar(varargin{1}) & strncmp(lower(varargin{1}),'chrom',5) |
---|
92 | CHROMFLAG = 1; |
---|
93 | REFPTS = NE+1; |
---|
94 | if isnumeric(varargin{2}) |
---|
95 | DDP = varargin{2}; |
---|
96 | else |
---|
97 | error('Fourth argument - wrong type'); |
---|
98 | end |
---|
99 | end |
---|
100 | case 6 |
---|
101 | if isnumeric(varargin{1}) |
---|
102 | REFPTS = varargin{1}; |
---|
103 | else |
---|
104 | error('Fourth argument - wrong type'); |
---|
105 | end |
---|
106 | if ischar(varargin{2}) & strncmp(lower(varargin{2}),'chrom',5) |
---|
107 | CHROMFLAG = 1; |
---|
108 | else |
---|
109 | error('Fifth argument - wrong type'); |
---|
110 | end |
---|
111 | if isnumeric(varargin{3}) |
---|
112 | DDP = varargin{3}; |
---|
113 | else |
---|
114 | error('Sixth argument - wrong type'); |
---|
115 | end |
---|
116 | otherwise |
---|
117 | error('Wrong number of arguments'); |
---|
118 | end |
---|
119 | |
---|
120 | %% retrieve twiss function at the line entrance |
---|
121 | if isfield(TWISSDATAIN,'alpha') |
---|
122 | ax = TWISSDATAIN(end).alpha(1); |
---|
123 | ay = TWISSDATAIN(end).alpha(2); |
---|
124 | else |
---|
125 | error('TWISSDATAIN structure does not have field ''alpha'''); |
---|
126 | end |
---|
127 | |
---|
128 | if isfield(TWISSDATAIN,'beta') |
---|
129 | bx = TWISSDATAIN(end).beta(1); |
---|
130 | by = TWISSDATAIN(end).beta(2); |
---|
131 | else |
---|
132 | error('TWISSDATAIN structure does not have field ''beta'''); |
---|
133 | end |
---|
134 | |
---|
135 | if isfield(TWISSDATAIN,'mu') |
---|
136 | mux = TWISSDATAIN(end).mu(1); |
---|
137 | muy = TWISSDATAIN(end).mu(2); |
---|
138 | else |
---|
139 | error('TWISSDATAIN structure does not have field ''mu'''); |
---|
140 | end |
---|
141 | |
---|
142 | %% Starting orbit is the user defined closed orbit at the line entrance |
---|
143 | R0 = [TWISSDATAIN(end).ClosedOrbit;DP;0]; |
---|
144 | |
---|
145 | [M44, MS, orb] = findm44(LINE,DP,REFPTS,R0); |
---|
146 | |
---|
147 | %% Betafunction |
---|
148 | BX = squeeze((MS(1,1,:)*bx-MS(1,2,:)*ax).^2 + MS(1,2,:).^2)/bx; |
---|
149 | BY = squeeze((MS(3,3,:)*by-MS(3,4,:)*ay).^2 + MS(3,4,:).^2)/by; |
---|
150 | |
---|
151 | %% Alpha functions |
---|
152 | AX = -squeeze((MS(1,1,:)*bx-MS(1,2,:)*ax).*(MS(2,1,:)*bx-MS(2,2,:)*ax) + ... |
---|
153 | MS(1,2,:).*MS(2,2,:))/bx; |
---|
154 | AY = -squeeze((MS(3,3,:)*by-MS(3,4,:)*ay).*(MS(4,3,:)*by-MS(4,4,:)*ay) + ... |
---|
155 | MS(3,4,:).*MS(4,4,:))/by; |
---|
156 | |
---|
157 | %% Phase advance function |
---|
158 | MX = atan(squeeze(MS(1,2,:)./(MS(1,1,:)*bx-MS(1,2,:)*ax))); |
---|
159 | MY = atan(squeeze(MS(3,4,:)./(MS(3,3,:)*by-MS(3,4,:)*ay))); |
---|
160 | |
---|
161 | MX = BetatronPhaseUnwrap(MX); |
---|
162 | MY = BetatronPhaseUnwrap(MY); |
---|
163 | |
---|
164 | TD = struct('ElemIndex',num2cell(REFPTS),... |
---|
165 | 'SPos',num2cell(findspos(LINE,REFPTS)),... |
---|
166 | 'ClosedOrbit',num2cell(orb,1),... |
---|
167 | 'M44', squeeze(num2cell(MS,[1 2]))',... |
---|
168 | 'beta', num2cell([BX,BY],2)',... |
---|
169 | 'alpha', num2cell([AX,AY],2)',... |
---|
170 | 'mu', num2cell([MX,MY],2)'); |
---|
171 | |
---|
172 | if CHROMFLAG |
---|
173 | % TD_DDP = twissring(LINE,DP+DDP,REFPTS); |
---|
174 | % DORBIT = reshape(cat(1,TD_DDP.ClosedOrbit),4,[]); |
---|
175 | [M44, MS, orb1] = findm44(LINE,DDP,REFPTS,R0); |
---|
176 | DISPERSION = num2cell((orb1-orb)/DDP,1); |
---|
177 | [TD.Dispersion] = deal( DISPERSION{:}); |
---|
178 | end |
---|
179 | |
---|
180 | function UP = BetatronPhaseUnwrap(P) |
---|
181 | % unwrap negative jumps in betatron |
---|
182 | DP = diff(P); |
---|
183 | JUMPS = [0; diff(P)] < 0; |
---|
184 | UP = P+cumsum(JUMPS)*pi; |
---|