0001 function ATLATTICE = readmad(FILENAME)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 [fid, errmsg] = fopen(FILENAME,'r');
0037 if fid==-1
0038 error('Could not open file');
0039 end
0040
0041 warnlevel = warning;
0042 warning on
0043
0044 global READMADCAVITYFLAG
0045 READMADCAVITYFLAG = 0;
0046
0047 LINE1 = fgetl(fid);
0048 LINE2 = fgetl(fid);
0049
0050 S = LINE1(9:16);
0051 nonspaceindex = find(~isspace(S) & (S~=0));
0052 MADFILETYPE = S(nonspaceindex);
0053
0054
0055
0056
0057 NSUPER = str2double(LINE1(41:48));
0058
0059 S = LINE1(56);
0060 SYMFLAG = eq(S,'T');
0061
0062 NPOS = str2double(LINE1(57:64));
0063
0064 disp(['MAD output file: ',FILENAME]);
0065 disp(' ');
0066 disp(['MAD file type: ',MADFILETYPE]);
0067 disp(['Symmetry flag: ',num2str(SYMFLAG)]);
0068 disp(['Number of superperiods: ',num2str(NSUPER)]);
0069 disp(['Number of elements : ',num2str(NPOS)]);
0070 disp(' ');
0071
0072
0073
0074
0075 ATNumElements = NPOS-1;
0076 ATLATTICE = cell(1,ATNumElements);
0077
0078
0079 switch MADFILETYPE
0080 case {'STRUCTUR','SURVEY'}
0081 NumLinesPerElement = 4;
0082 case {'TWISS','CHROM'}
0083 NumLinesPerElement = 5;
0084 case 'ENVELOPE'
0085 NumLinesPerElement = 8;
0086 end
0087
0088 ELEMENTDATA = cell(1,NumLinesPerElement);
0089
0090
0091 for i = 1:NumLinesPerElement;
0092 LINE = fgetl(fid);
0093 end
0094
0095 for i = 1:ATNumElements
0096
0097 for j= 1:NumLinesPerElement
0098 ELEMENTDATA{j}=fgetl(fid);
0099 end
0100
0101 ATLATTICE{i}=mad2at(ELEMENTDATA,MADFILETYPE);
0102 end
0103
0104
0105
0106
0107 fclose(fid);
0108 warning(warnlevel);
0109
0110 disp(' ');
0111 disp(['AT cell array was successfully created from MAD output file ',FILENAME]);
0112 disp('Some information may be not available in MAD otput files')
0113 disp('Some elements may have to be further modified to be consistent with AT element models')
0114 disp(' ');
0115 disp('For RF cavities READMAD creates elements that use DriftPass or IdentityPass (if Length ==0)');
0116 disp('Use CAVITYON(ENERGY) [eV] in order to turn them into cavities');
0117
0118
0119
0120
0121 function atelement = mad2at(elementdata,madfiletype)
0122 global READMADCAVITYFLAG
0123 MADTYPE = elementdata{1}(1:4);
0124 atelement.FamName = deblank(elementdata{1}(5:20));
0125 atelement.Length = str2double(elementdata{1}(21:32));
0126
0127 switch MADTYPE
0128 case 'DRIF'
0129 atelement.PassMethod = 'DriftPass';
0130 case {'MARK','MONI','HMON','VMON'}
0131 atelement.PassMethod = 'IdentityPass';
0132 case 'RFCA'
0133
0134
0135 if ~READMADCAVITYFLAG
0136 warning('MAD lattice contains RF cavities')
0137 READMADCAVITYFLAG = 1;
0138 end
0139 atelement.Frequency = 1e6*str2double(elementdata{2}(17:32));
0140 atelement.Voltage = 1e6*str2double(elementdata{2}(33:48));
0141 atelement.PhaseLag = str2double(elementdata{2}(49:64));
0142 if atelement.Length
0143 atelement.PassMethod = 'DriftPass';
0144 else
0145 atelement.PassMethod = 'IdentityPass';
0146 end
0147 case 'SBEN'
0148 K1 = str2double(elementdata{1}(49:64));
0149 K2 = str2double(elementdata{1}(65:80));
0150 atelement.BendingAngle = str2double(elementdata{1}(33:48));
0151 atelement.ByError = 0;
0152 atelement.MaxOrder = 3;
0153 atelement.NumIntSteps = 10;
0154 atelement.TiltAngle = str2double(elementdata{2}(1:16));
0155 atelement.EntranceAngle = str2double(elementdata{2}(17:32));
0156 atelement.ExitAngle = str2double(elementdata{2}(33:48));
0157 atelement.K = K1;
0158 atelement.PolynomB = [0 K1 K2 0];
0159 atelement.PolynomA = [0 0 0 0];
0160 atelement.T1 = zeros(1,6);
0161 atelement.T2 = zeros(1,6);
0162 atelement.R1 = eye(6);
0163 atelement.R2 = eye(6);
0164 if atelement.BendingAngle
0165 if K2
0166 atelement.PassMethod = 'BndMPoleSymplectic4Pass';
0167 else
0168 atelement.PassMethod = 'BendLinearPass';
0169 end
0170
0171 else
0172 if K2
0173 atelement.PassMethod = 'StrMPoleSymplectic4Pass';
0174 elseif K1
0175 atelement.PassMethod = 'QuadLinearPass';
0176 else
0177 atelement.PassMethod = 'DriftPass';
0178 end
0179 end
0180 case 'QUAD'
0181 K1 = str2double(elementdata{1}(49:64));
0182 atelement.MaxOrder = 3;
0183 atelement.NumIntSteps = 10;
0184 atelement.K = K1;
0185 atelement.PolynomB = [0 K1 0 0];
0186 atelement.PolynomA = [0 0 0 0];
0187 atelement.T1 = zeros(1,6);
0188 atelement.T2 = zeros(1,6);
0189 TILT = str2double(elementdata{2}(1:16));
0190 atelement.R1 = mkSRotationMatrix(TILT);
0191 atelement.R2 = mkSRotationMatrix(-TILT);
0192 atelement.PassMethod = 'QuadLinearPass';
0193
0194 case 'SEXT'
0195
0196
0197 K2 = str2double(elementdata{1}(65:80))/2;
0198 atelement.MaxOrder = 3;
0199 atelement.NumIntSteps = 10;
0200 atelement.PolynomB = [0 0 K2 0];
0201 atelement.PolynomA = [0 0 0 0];
0202 atelement.T1 = zeros(1,6);
0203 atelement.T2 = zeros(1,6);
0204 TILT = str2double(elementdata{2}(1:16));
0205 atelement.R1 = mkSRotationMatrix(TILT);
0206 atelement.R2 = mkSRotationMatrix(-TILT);
0207 atelement.PassMethod = 'StrMPoleSymplectic4Pass';
0208
0209 case 'OCTU'
0210
0211
0212 K3 = str2double(elementdata{2}(17:32))/6;
0213 atelement.MaxOrder = 3 ;
0214 atelement.NumIntSteps = 10;
0215 atelement.PolynomB = [0 0 0 K3];
0216 atelement.PolynomA = [0 0 0 0];
0217 atelement.T1 = zeros(1,6);
0218 atelement.T2 = zeros(1,6);
0219 TILT = str2double(elementdata{2}(1:16));
0220 atelement.R1 = mkSRotationMatrix(TILT);
0221 atelement.R2 = mkSRotationMatrix(-TILT);
0222 atelement.PassMethod = 'StrMPoleSymplectic4Pass';
0223 otherwise
0224 if atelement.Length
0225 atelement.PassMethod = 'DriftPass';
0226 else
0227 atelement.PassMethod = 'IdentityPass';
0228 end
0229 end