1 | function ATLATTICE = readmad(FILENAME) |
---|
2 | %READMAD reads the file output of MAD commands |
---|
3 | % TWISS, STRUCTURE, SURVEY. |
---|
4 | % |
---|
5 | % READMAD reads the MAD file header to determine the number of elements |
---|
6 | % in the lattice, symmetry flag, the number of supperperiods etc. |
---|
7 | % |
---|
8 | % Then it interprets the entry for each element in the MAD output file. |
---|
9 | % The topology of the lattice is completely determined by |
---|
10 | % Length, Bending Angle, and Ttilt Angle in each element |
---|
11 | % |
---|
12 | % READMAD uses MAD TYPES and the values of to determine |
---|
13 | % which pass-method function in AT to use. |
---|
14 | % |
---|
15 | % MAD TYPE | AT PassMethod |
---|
16 | % ---------------------------------- |
---|
17 | % DRIFT | DriftPass |
---|
18 | % SBEND | BendLinearPass, BendLinearFringeTiltPass, BndMPoleSymplectic4Pass |
---|
19 | % QUADRUPOLE | QualdLinearPass |
---|
20 | % SEXTUPOLE | StrMPoleSymplectic4Pass |
---|
21 | % OCTUPOLE | StrMPoleSymplectic4Pass |
---|
22 | % MULTIPOLE | !!! Not implemented, in future - ThinMPolePass |
---|
23 | % RFCAVITY | ThinCavityPass |
---|
24 | % KICKER | CorrectorPass |
---|
25 | % HKICKER | CorrectorPass |
---|
26 | % VKICKER | CorrectorPass |
---|
27 | % MONITOR | IdentityPass |
---|
28 | % HMONITOR | IdentityPass |
---|
29 | % VMONITOR | IdentityPass |
---|
30 | % MARKER | IdentityPass |
---|
31 | % ----------------------------------- |
---|
32 | % all others | Length=0 -> IdentityPass, Length~=0 -> DriftPass |
---|
33 | |
---|
34 | [fid, errmsg] = fopen(FILENAME,'r'); |
---|
35 | if fid==-1 |
---|
36 | error('Could not open file'); |
---|
37 | end |
---|
38 | |
---|
39 | warnlevel = warning; |
---|
40 | warning on |
---|
41 | |
---|
42 | global READMADCAVITYFLAG |
---|
43 | READMADCAVITYFLAG = 0; |
---|
44 | |
---|
45 | LINE1 = fgetl(fid); |
---|
46 | LINE2 = fgetl(fid); |
---|
47 | |
---|
48 | S = LINE1(9:16); |
---|
49 | nonspaceindex = find(~isspace(S) & (S~=0)); |
---|
50 | MADFILETYPE = S(nonspaceindex); |
---|
51 | % The possiblilites for MADFILETYPE are |
---|
52 | % TWISS,SURVEY,STRUCTUR,ENVELOPE |
---|
53 | |
---|
54 | |
---|
55 | NSUPER = str2double(LINE1(41:48)); |
---|
56 | |
---|
57 | S = LINE1(56); |
---|
58 | SYMFLAG = eq(S,'T'); |
---|
59 | |
---|
60 | NPOS = str2double(LINE1(57:64)); |
---|
61 | |
---|
62 | disp(['MAD output file: ',FILENAME]); |
---|
63 | disp(' '); |
---|
64 | disp(['MAD file type: ',MADFILETYPE]); |
---|
65 | disp(['Symmetry flag: ',num2str(SYMFLAG)]); |
---|
66 | disp(['Number of superperiods: ',num2str(NSUPER)]); |
---|
67 | disp(['Number of elements : ',num2str(NPOS)]); |
---|
68 | disp(' '); |
---|
69 | |
---|
70 | |
---|
71 | % Allocate cell array to store AT lattice |
---|
72 | % MAD files heve one extra entry for the beginning of the lattice |
---|
73 | ATNumElements = NPOS-1; |
---|
74 | ATLATTICE = cell(1,ATNumElements); |
---|
75 | |
---|
76 | |
---|
77 | switch MADFILETYPE |
---|
78 | case {'STRUCTUR','SURVEY'} |
---|
79 | NumLinesPerElement = 4; |
---|
80 | case {'TWISS','CHROM'} |
---|
81 | NumLinesPerElement = 5; |
---|
82 | case 'ENVELOPE' |
---|
83 | NumLinesPerElement = 8; |
---|
84 | end |
---|
85 | |
---|
86 | ELEMENTDATA = cell(1,NumLinesPerElement); |
---|
87 | |
---|
88 | % Skip the INITIAL element in MAD file |
---|
89 | for i = 1:NumLinesPerElement; |
---|
90 | LINE = fgetl(fid); |
---|
91 | end |
---|
92 | |
---|
93 | for i = 1:ATNumElements |
---|
94 | % Read the first 2 lines of the element entry |
---|
95 | for j= 1:NumLinesPerElement |
---|
96 | ELEMENTDATA{j}=fgetl(fid); |
---|
97 | end |
---|
98 | |
---|
99 | ATLATTICE{i}=mad2at(ELEMENTDATA,MADFILETYPE); |
---|
100 | end |
---|
101 | |
---|
102 | |
---|
103 | |
---|
104 | |
---|
105 | fclose(fid); |
---|
106 | warning(warnlevel); |
---|
107 | |
---|
108 | disp(' '); |
---|
109 | disp(['AT cell array was successfully created from MAD output file ',FILENAME]); |
---|
110 | disp('Some information may be not available in MAD otput files') |
---|
111 | disp('Some elements may have to be further modified to be consistent with AT element models') |
---|
112 | disp(' '); |
---|
113 | disp('For RF cavities READMAD creates elements that use DriftPass or IdentityPass (if Length ==0)'); |
---|
114 | disp('Use CAVITYON(ENERGY) [eV] in order to turn them into cavities'); |
---|
115 | |
---|
116 | |
---|
117 | % --------------------------------------------------------------------------- |
---|
118 | |
---|
119 | function atelem = mad2at(elementdata,madfiletype) |
---|
120 | global READMADCAVITYFLAG |
---|
121 | MADTYPE = elementdata{1}(1:4); |
---|
122 | atelem.FamName = deblank(elementdata{1}(5:20)); |
---|
123 | atelem.Length = str2double(elementdata{1}(21:32)); |
---|
124 | % Type specific |
---|
125 | switch MADTYPE |
---|
126 | case 'DRIF' |
---|
127 | atelem.PassMethod = 'DriftPass'; |
---|
128 | case {'MARK','MONI','HMON','VMON'} |
---|
129 | atelem.PassMethod = 'IdentityPass'; |
---|
130 | case 'RFCA' |
---|
131 | % Note MAD determines the RF frequency from the harmonic number HARMON |
---|
132 | % defined by MAD stetement BEAM, and the total length of the closed orbit |
---|
133 | if ~READMADCAVITYFLAG |
---|
134 | warning('MAD lattice contains RF cavities') |
---|
135 | READMADCAVITYFLAG = 1; |
---|
136 | end |
---|
137 | atelem.Frequency = 1e6*str2double(elementdata{2}(17:32)); % MAD uses MHz |
---|
138 | atelem.Voltage = 1e6*str2double(elementdata{2}(33:48)); |
---|
139 | atelem.PhaseLag = str2double(elementdata{2}(49:64)); |
---|
140 | if atelem.Length |
---|
141 | atelem.PassMethod = 'DriftPass'; |
---|
142 | else |
---|
143 | atelem.PassMethod = 'IdentityPass'; |
---|
144 | end |
---|
145 | case 'SBEN' |
---|
146 | K1 = str2double(elementdata{1}(49:64)); |
---|
147 | K2 = str2double(elementdata{1}(65:80)); |
---|
148 | atelem.BendingAngle = str2double(elementdata{1}(33:48)); |
---|
149 | atelem.ByError = 0; |
---|
150 | atelem.MaxOrder = 3; |
---|
151 | atelem.NumIntSteps = 10; |
---|
152 | atelem.TiltAngle = str2double(elementdata{2}(1:16)); |
---|
153 | atelem.EntranceAngle = str2double(elementdata{2}(17:32)); |
---|
154 | atelem.ExitAngle = str2double(elementdata{2}(33:48)); |
---|
155 | atelem.K = K1; |
---|
156 | atelem.PolynomB = [0 K1 K2 0]; |
---|
157 | atelem.PolynomA = [0 0 0 0]; |
---|
158 | atelem.T1 = zeros(1,6); |
---|
159 | atelem.T2 = zeros(1,6); |
---|
160 | atelem.R1 = eye(6); |
---|
161 | atelem.R2 = eye(6); |
---|
162 | if atelem.BendingAngle |
---|
163 | if K2 |
---|
164 | atelem.PassMethod = 'BndMPoleSymplectic4Pass'; |
---|
165 | elseif atelem.TiltAngle |
---|
166 | atelem.PassMethod = 'BendLinearFringeTiltPass' |
---|
167 | else |
---|
168 | atelem.PassMethod = 'BendLinearPass'; |
---|
169 | end |
---|
170 | |
---|
171 | else |
---|
172 | if K2 |
---|
173 | atelem.PassMethod = 'StrMPoleSymplectic4Pass'; |
---|
174 | elseif K1 |
---|
175 | atelem.PassMethod = 'QuadLinearPass'; |
---|
176 | else |
---|
177 | atelem.PassMethod = 'DriftPass'; |
---|
178 | end |
---|
179 | end |
---|
180 | case 'QUAD' |
---|
181 | K1 = str2double(elementdata{1}(49:64)); |
---|
182 | atelem.MaxOrder = 3; |
---|
183 | atelem.NumIntSteps = 10; |
---|
184 | atelem.K = K1; |
---|
185 | atelem.PolynomB = [0 K1 0 0]; |
---|
186 | atelem.PolynomA = [0 0 0 0]; |
---|
187 | atelem.T1 = zeros(1,6); |
---|
188 | atelem.T2 = zeros(1,6); |
---|
189 | TILT = str2double(elementdata{2}(1:16)); |
---|
190 | atelem.R1 = mksrollmat(TILT); |
---|
191 | atelem.R2 = mksrollmat(-TILT); |
---|
192 | atelem.PassMethod = 'QuadLinearPass'; |
---|
193 | |
---|
194 | case 'SEXT' |
---|
195 | % MAD multipole strength coefficients K(n) are defined without 1/n! |
---|
196 | % Adjust to match AT |
---|
197 | K2 = str2double(elementdata{1}(65:80))/2; |
---|
198 | atelem.MaxOrder = 3; |
---|
199 | atelem.NumIntSteps = 10; |
---|
200 | atelem.PolynomB = [0 0 K2 0]; |
---|
201 | atelem.PolynomA = [0 0 0 0]; |
---|
202 | atelem.T1 = zeros(1,6); |
---|
203 | atelem.T2 = zeros(1,6); |
---|
204 | TILT = str2double(elementdata{2}(1:16)); |
---|
205 | atelem.R1 = mksrollmat(TILT); |
---|
206 | atelem.R2 = mksrollmat(-TILT); |
---|
207 | atelem.PassMethod = 'StrMPoleSymplectic4Pass'; |
---|
208 | |
---|
209 | case 'OCTU' |
---|
210 | % MAD multipole strength coefficients K(n) are defined without 1/n! |
---|
211 | % Adjust to match AT |
---|
212 | K3 = str2double(elementdata{2}(17:32))/6; |
---|
213 | atelem.MaxOrder = 3 ; |
---|
214 | atelem.NumIntSteps = 10; |
---|
215 | atelem.PolynomB = [0 0 0 K3]; |
---|
216 | atelem.PolynomA = [0 0 0 0]; |
---|
217 | atelem.T1 = zeros(1,6); |
---|
218 | atelem.T2 = zeros(1,6); |
---|
219 | TILT = str2double(elementdata{2}(1:16)); |
---|
220 | atelem.R1 = mksrollmat(TILT); |
---|
221 | atelem.R2 = mksrollmat(-TILT); |
---|
222 | atelem.PassMethod = 'StrMPoleSymplectic4Pass'; |
---|
223 | otherwise |
---|
224 | if atelem.Length |
---|
225 | atelem.PassMethod = 'DriftPass'; |
---|
226 | else |
---|
227 | atelem.PassMethod = 'IdentityPass'; |
---|
228 | end |
---|
229 | end |
---|