Home > lattice > Converters > AT2MADX > AT_2_madX.m

AT_2_madX

PURPOSE ^

function [elelat,defs,lines]=AT_2_madX(AT_ring,linename)

SYNOPSIS ^

function [elelat,defs,lines]=AT_2_madX(AT_ring,linename)

DESCRIPTION ^

 function [elelat,defs,lines]=AT_2_madX(AT_ring,linename)
 this functions converts the AT lattice AT_ring in madX format.
 
 a MADX LINE is generated.
 
 file ['' linename '_lattice.madx'] is generated contiaining the lattice
 (elelat)  elements definitions (defs) and the LINE (lines). 
 no other comands are introduced

 to test in MADX run (replace linename with apropriate name)
 madx < linename_lattice.madx 
 
 to test with twiss and plot add following lines at the end of file:
 beam;
 use, period=linename;
 twiss;
 plot, haxis=s, vaxis1=betx,bety, vaxis2=dx;

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [elelat,defs,lines]=AT_2_madX(AT_ring,linename)
0002 % function [elelat,defs,lines]=AT_2_madX(AT_ring,linename)
0003 % this functions converts the AT lattice AT_ring in madX format.
0004 %
0005 % a MADX LINE is generated.
0006 %
0007 % file ['' linename '_lattice.madx'] is generated contiaining the lattice
0008 % (elelat)  elements definitions (defs) and the LINE (lines).
0009 % no other comands are introduced
0010 %
0011 % to test in MADX run (replace linename with apropriate name)
0012 % madx < linename_lattice.madx
0013 %
0014 % to test with twiss and plot add following lines at the end of file:
0015 % beam;
0016 % use, period=linename;
0017 % twiss;
0018 % plot, haxis=s, vaxis1=betx,bety, vaxis2=dx;
0019 %
0020 
0021 outfile=['' linename '_lattice.madx'];
0022 
0023 %outfile='madXelemdef.elem';
0024 elelat=['!!\n!!  madX lattice elements: ' linename '\n!!  Created: ' datestr(now) '\n!!\n!!\n\n'];
0025 
0026 %% get family names for definitions
0027 [families,ind_first_oc_ring]=...
0028     unique(getcellstruct(AT_ring,'FamName',1:length(AT_ring)),'first');
0029 formatvalues='%10.12f';
0030 
0031 elelat=[elelat '! DEFINITIONS \n\n BEAM, PARTICLE=ELECTRON, ENERGY=' num2str(atenergy(AT_ring)*1e-9,formatvalues) ';\n OCT_ON:=1;\n SXT_ON:=1;\n RF_ON:=0;'];
0032 
0033 
0034 %% loop families for definitions
0035 for i=1:length(families)
0036     el= AT_ring{ind_first_oc_ring(i)};
0037     
0038     if isfield(el,'BetaCode')
0039         type=el.BetaCode;
0040     elseif isfield(el,'Class')
0041         type=el.Class;
0042     else
0043         type='Marker';
0044     end
0045     
0046     switch type
0047         case {'DI','Dipole','Bend'} % dipole
0048             
0049             di=[el.('FamName')   ': '...
0050                 ' SBEND,L=' num2str(el.('Length'),formatvalues) ', '...
0051                 ' ANGLE = ' num2str(el.('BendingAngle'),formatvalues) ', '...
0052                 ' K0 = ' num2str(el.('PolynomB')(1),formatvalues) ', '...
0053                 ' K1 = ' num2str(el.('PolynomB')(2),formatvalues) ', '...
0054                 ' E1= ' num2str(el.('EntranceAngle'),formatvalues) ', '...
0055                 ' E2= ' num2str(el.('ExitAngle'),formatvalues) ', '...
0056                 ];
0057             
0058             if isfield(el,'FringeInt1')
0059             di=[di ' FINT= ' num2str(el.('FringeInt1'),formatvalues) ', '...
0060                 ' FINTX= ' num2str(el.('FringeInt2'),formatvalues) ', '...
0061                 ' hgap= ' num2str(el.('FullGap'),formatvalues) '/2, '...
0062                 ];
0063             end
0064             
0065             di=[di ';'];
0066             
0067             elelat=[elelat di '\n']; %#ok<*AGROW>
0068         case {'QP','Quadrupole'} % quadrupole
0069             if el.('MaxOrder')==3
0070                 if el.('PolynomB')(4)==0
0071                     qp=[' ' el.('FamName') ': '...
0072                         ' quadrupole,  L = ' num2str(el.('Length'),formatvalues)  ', '...
0073                         'K1 = ' num2str(el.('PolynomB')(2),formatvalues) '; '...
0074                         ];
0075                 else % if octupole in quadrupole, split quadrupole as in mad8 q oc q q oc q
0076                     nslice=10;
0077                     qp=[' ' el.('FamName') '_sl: '...
0078                         ' quadrupole,  L = ' num2str(el.('Length')/nslice/2,formatvalues)  ', '...
0079                         'K1 = ' num2str(el.('PolynomB')(2),formatvalues) '; '...
0080                         '\n'...
0081                         ];
0082                     polB=el.('PolynomB');
0083                     polA=el.('PolynomA');
0084                     polB(2)=0;
0085                     polA(2)=0;
0086                     
0087                     qp=[qp ' ' el.('FamName')   '_oc: '...
0088                         ' MULTIPOLE,' ...  LRAD= ' num2str(el.('Length'),format)  ', '...
0089                         'KNL:= {' num2str(polB.*(factorial(0:length(polB)-1))*el.('Length')/(nslice),[formatvalues ', ']) ' 0.0} ,'...
0090                         'KSL:= {' num2str(polA.*(factorial(0:length(polA)-1))*el.('Length')/(nslice),[formatvalues ', ']) ' 0.0};'...
0091                         '\n'...
0092                         ];
0093                     
0094                     qp=[qp el.('FamName') ': line=('...
0095                         repmat([el.('FamName') '_sl,'...
0096                         el.('FamName') '_oc,'...
0097                         el.('FamName') '_sl,'...
0098                         ],1,nslice)...
0099                         '\n'...
0100                         ];
0101                     qp(end-2:end)=[];
0102                     qp=[qp ');\n'];
0103                 end
0104             else
0105                 qp=[' ' el.('FamName') ': '...
0106                     ' quadrupole,  L = ' num2str(el.('Length'),formatvalues)  ', '...
0107                     'K1 = ' num2str(el.('PolynomB')(2),formatvalues) '; '...
0108                     ];
0109                 
0110             end
0111             
0112             
0113             elelat=[elelat qp '\n\n'];
0114             %             qp=[el.('FamName')   ': '...
0115             %                 ' QUADRUPOLE,  L= ' num2str(el.('Length'),format)  ','...
0116             %                 ' K1= ' num2str(el.('PolynomB')(2),format) ';'...
0117             %                 ];
0118             %
0119             %             elelat=[elelat qp '\n'];
0120         case {'SX','Sextupole'} % sextupole
0121             sx=[el.('FamName')   ': '...
0122                 ' SEXTUPOLE,  L= ' num2str(el.('Length'),formatvalues)  ', '...
0123                 ' K2= ' num2str(el.('PolynomB')(3)*2,formatvalues) '*SXT_ON ;'...
0124                 ];
0125             elelat=[elelat sx '\n'];
0126         case {'OC','Octupole'} % sextupole
0127             sx=[el.('FamName')   ': '...
0128                 ' OCTUPOLE,  L= ' num2str(el.('Length'),formatvalues)  ', '...
0129                 ' K3=' num2str(el.('PolynomB')(4)*6,formatvalues) '*OCT_ON ;'...
0130                 ];
0131             elelat=[elelat sx '\n'];
0132         case {'MP','Multipole'} % multipole
0133             el.('PolynomB')(isnan(el.('PolynomB')))=0.0;
0134             if el.Length==0
0135             mp=[el.('FamName')   ': '...
0136                 ' MULTIPOLE,  LRAD= ' num2str(el.('Length'),formatvalues)  ', '...
0137                 ' L= ' num2str(el.('Length'),formatvalues)  ', '...
0138                 'KNL:= {' num2str(el.('PolynomB').*(factorial(0:length(el.('PolynomB'))-1)),[formatvalues ', ']) '0.0} ,'...
0139                 'KSL:= {' num2str(el.('PolynomA').*(factorial(0:length(el.('PolynomA'))-1)),[formatvalues ', ']) '0.0};'...
0140                 ];
0141             else
0142             mp=[el.('FamName')   ': '...
0143                 ' Drift,  L= ' num2str(el.('Length'),formatvalues)  '; '...
0144                 ];
0145             warning('Multipoles have zero length in MADX, CONVERTED TO DRIFT. please model in AT as Drift-lense-Drift.')
0146             end
0147             elelat=[elelat mp '\n'];
0148         case {'ThinMultipole'} % multipole
0149             formatm='%10.5e';
0150             mp=[el.('FamName')   ': '...
0151                 ' MULTIPOLE,  LRAD= ' num2str(el.('Length'),formatvalues)  ', '...
0152                 'KNL:= {' num2str(el.('PolynomB').*(factorial(0:length(el.('PolynomB'))-1)),[formatm ', ']) '0.0} ,'...
0153                 'KSL:= {' num2str(el.('PolynomA').*(factorial(0:length(el.('PolynomA'))-1)),[formatm ', ']) '0.0};'...
0154                 ];
0155             elelat=[elelat mp '\n'];
0156         case {'PU','Monitor'} % bpm
0157             pu=[el.('FamName') ' :MONITOR' ' ;'...
0158                 ];
0159             elelat=[elelat pu '\n'];
0160         case {'MRK','Marker'}% marker
0161             mrk=[el.('FamName') ' :MARKER' ' ;'...
0162                 ];
0163             elelat=[elelat mrk '\n'];
0164         case {'KI','Corrector'} % kicker
0165             ki=[el.('FamName') ' :KICKER, L= ' num2str(el.('Length'),formatvalues)  ' '...
0166                 ', HKick:=' num2str(el.PolynomB(1))...
0167                 ', VKick:=' num2str(el.PolynomA(1)) ' ;'...
0168                 ];
0169             
0170             elelat=[elelat ki '\n'];
0171         case {'SKW','SkewQuadrupole'} % kicker
0172             skw=[el.('FamName') ' :Multipole, KSL:={0,' num2str(el.PolynomA(2))...
0173                  '} ;'...
0174                 ];
0175             
0176             elelat=[elelat skw '\n'];
0177         case {'DR','Drift'} % drift
0178             dr=[el.('FamName') ' : DRIFT, L= ' num2str(el.('Length'),formatvalues) ' ;'];
0179             elelat=[elelat dr '\n'];
0180         case {'CAV','RFCavity'} % drift
0181             rfc=[el.('FamName') ' : RFCavity, L=' num2str(el.('Length'),formatvalues)...
0182                 ',VOLT=RF_ON*'  num2str(el.('Voltage')/1e6,formatvalues) ' '...% MV
0183                 ...', NO_CAVITY_TOTALPATH = false'...
0184                 ', freq=' num2str(el.('Frequency')*1e-6,formatvalues) ''... MHz
0185                 ' ;'];
0186             
0187             elelat=[elelat rfc '\n'];
0188             
0189         otherwise
0190             warning(['Element: ' el.('FamName') ' was not converted (marker), since it does not match any Class.'])
0191             elelat=[elelat el.('FamName') ' : marker; \n'];
0192            
0193             mrk=[el.('FamName') ' :MARKER' ' ;'...
0194                 ];
0195             elelat=[elelat mrk '\n'];
0196     end
0197     
0198 end
0199 defs=elelat;
0200 
0201 elelat=[];
0202 
0203 elelat=[elelat '! LINE \n\n'];
0204 
0205 elelat=[elelat linename ' : LINE = (' ' '];
0206 
0207 %% define lattice line
0208 % loop all elements
0209 for i=1:length(AT_ring)
0210     if i~=1
0211         elelat=[elelat ',' AT_ring{i}.('FamName') '\n '];
0212     else
0213         elelat=[elelat ' ' AT_ring{i}.('FamName') '\n '];
0214     end
0215 end
0216 
0217 elelat=[elelat ') ;'];
0218 lines=elelat;
0219 
0220 elelat=[defs lines];
0221 
0222 %% print to file
0223 
0224 of=fopen(outfile,'w');
0225 fprintf(of,elelat);
0226 
0227 fclose('all');
0228 
0229 
0230 
0231 
0232 return

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