Home > lattice > Converters > readmad.m

readmad

PURPOSE ^

READMAD reads the file output of MAD commands

SYNOPSIS ^

function ATLATTICE = readmad(FILENAME)

DESCRIPTION ^

READMAD reads the file output of MAD commands
 TWISS, STRUCTURE, SURVEY.

 ATLATTICE = readmad(FILENAME)

 READMAD reads the MAD file header to determine the number of elements
 in the lattice, symmetry flag, the number of supperperiods etc.
 
 Then it interprets the entry for each element in the MAD output file.
 The topology of the lattice is completely determined by
 Length, Bending Angle, and Ttilt Angle in each element
 
 READMAD uses MAD TYPES and the values of to determine
 which pass-method function in AT to use.
 
 MAD TYPE      |  AT PassMethod
 ----------------------------------
 DRIFT         |  DriftPass
 SBEND         |  BendLinearPass, BndMPoleSymplectic4Pass
 QUADRUPOLE    |  QualdLinearPass
 SEXTUPOLE     |  StrMPoleSymplectic4Pass
 OCTUPOLE      |  StrMPoleSymplectic4Pass
 MULTIPOLE     |  !!! Not implemented, in future - ThinMPolePass
 RFCAVITY      |  ThinCavityPass
 KICKER        |  CorrectorPass
 HKICKER       |  CorrectorPass
 VKICKER       |  CorrectorPass
 MONITOR       |  IdentityPass
 HMONITOR      |  IdentityPass
 VMONITOR      |  IdentityPass
 MARKER        |  IdentityPass
 -----------------------------------
 all others    |  Length=0 -> IdentityPass, Length~=0 -> DriftPass

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function ATLATTICE = readmad(FILENAME)
0002 %READMAD reads the file output of MAD commands
0003 % TWISS, STRUCTURE, SURVEY.
0004 %
0005 % ATLATTICE = readmad(FILENAME)
0006 %
0007 % READMAD reads the MAD file header to determine the number of elements
0008 % in the lattice, symmetry flag, the number of supperperiods etc.
0009 %
0010 % Then it interprets the entry for each element in the MAD output file.
0011 % The topology of the lattice is completely determined by
0012 % Length, Bending Angle, and Ttilt Angle in each element
0013 %
0014 % READMAD uses MAD TYPES and the values of to determine
0015 % which pass-method function in AT to use.
0016 %
0017 % MAD TYPE      |  AT PassMethod
0018 % ----------------------------------
0019 % DRIFT         |  DriftPass
0020 % SBEND         |  BendLinearPass, BndMPoleSymplectic4Pass
0021 % QUADRUPOLE    |  QualdLinearPass
0022 % SEXTUPOLE     |  StrMPoleSymplectic4Pass
0023 % OCTUPOLE      |  StrMPoleSymplectic4Pass
0024 % MULTIPOLE     |  !!! Not implemented, in future - ThinMPolePass
0025 % RFCAVITY      |  ThinCavityPass
0026 % KICKER        |  CorrectorPass
0027 % HKICKER       |  CorrectorPass
0028 % VKICKER       |  CorrectorPass
0029 % MONITOR       |  IdentityPass
0030 % HMONITOR      |  IdentityPass
0031 % VMONITOR      |  IdentityPass
0032 % MARKER        |  IdentityPass
0033 % -----------------------------------
0034 % all others    |  Length=0 -> IdentityPass, Length~=0 -> DriftPass
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 % The possiblilites for MADFILETYPE are
0054 % TWISS,SURVEY,STRUCTUR,ENVELOPE
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 % Allocate cell array to store AT lattice
0074 % MAD files heve one extra entry for the beginning of the lattice
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 % Skip the INITIAL element in MAD file
0091 for i = 1:NumLinesPerElement;
0092     LINE = fgetl(fid);
0093 end
0094 
0095 for i = 1:ATNumElements
0096     % Read the first 2 lines of the element entry
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     % Type specific
0127     switch MADTYPE
0128     case 'DRIF'
0129         atelement.PassMethod = 'DriftPass';
0130     case {'MARK','MONI','HMON','VMON'}
0131         atelement.PassMethod = 'IdentityPass';
0132     case 'RFCA'
0133         % Note MAD determines the RF frequency from the harmonic number HARMON
0134         % defined by MAD stetement BEAM, and the total length of the closed orbit
0135         if ~READMADCAVITYFLAG
0136             warning('MAD lattice contains RF cavities')
0137             READMADCAVITYFLAG = 1;
0138         end
0139         atelement.Frequency = 1e6*str2double(elementdata{2}(17:32)); % MAD uses MHz
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         % MAD multipole strength coefficients K(n) are defined without 1/n!
0196         % Adjust to match AT
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         % MAD multipole strength coefficients K(n) are defined without 1/n!
0211         % Adjust to match AT
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

Generated on Thu 24-Aug-2017 18:47:33 by m2html © 2005