0001 function [elelat,def,lines]=AT_2_mad8(AT_ring,linename)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 outfile=['' linename '_lattice.mad8'];
0013
0014 elelat=['!!\n!! mad8 lattice: ' linename '\n!! Created: ' datestr(now) '\n!!\n!!\n\n'...
0015 ...
0016 ...
0017 ...
0018 ];
0019
0020
0021 [families,ind_first_oc_ring]=...
0022 unique(getcellstruct(AT_ring,'FamName',1:length(AT_ring)),'first');
0023
0024 elelat=[elelat '! DEFINITIONS \n\n'];
0025
0026 format='%8.8f';
0027
0028
0029 for i=1:length(families)
0030 el= AT_ring{ind_first_oc_ring(i)};
0031 if isfield(el,'Class')
0032 switch el.('Class')
0033 case 'Bend'
0034
0035
0036 di=[el.('FamName') ': '...
0037 ' SBEND,L=' num2str(el.('Length'),format) ', '...
0038 ' ANGLE = ' num2str(el.('BendingAngle'),format) ', &\n'...
0039 ' K1 = ' num2str(el.('PolynomB')(2),format) ' '...
0040 ' E1= ' num2str(el.('EntranceAngle'),format) ', '...
0041 ' E2= ' num2str(el.('ExitAngle'),format) ' ;'...
0042 ' '];
0043
0044 elelat=[elelat di '\n'];
0045 case 'Quadrupole'
0046
0047 if strfind(el.PassMethod,'Fringe')
0048 qp=[el.('FamName') ': '...
0049 ' QUADRUPOLE, L= ' num2str(el.('Length'),format) ', '...
0050 ' K1= ' num2str(el.('PolynomB')(2),format) ',FRGF=.TRUE.;'...
0051 ];
0052 else
0053 qp=[el.('FamName') ': '...
0054 ' QUADRUPOLE, L= ' num2str(el.('Length'),format) ', '...
0055 ' K1= ' num2str(el.('PolynomB')(2),format) ';'...
0056 ];
0057 end
0058
0059 elelat=[elelat qp '\n'];
0060 case 'Sextupole'
0061 sx=[el.('FamName') ': '...
0062 ' Sextupole, L= ' num2str(el.('Length'),format) ','...
0063 ' K2= ' num2str(el.('PolynomB')(3)*2,format) ' ;'...
0064 ];
0065 elelat=[elelat sx '\n'];
0066 case 'Multipole'
0067
0068 ord=find(el.('PolynomB'));
0069 if isempty(ord)
0070 ord=length(el.('PolynomB'));
0071 end
0072 mp=[el.('FamName') ': '...
0073 ' Multipole, '...
0074 ...
0075 'K' num2str(ord(1)-1) 'L= ' num2str(el.('PolynomB')(ord(1))*factorial(ord(1)-1),format) '; \n'...
0076
0077
0078 ];
0079 elelat=[elelat mp '\n'];
0080 case {'ThinMultipole'}
0081 formatm='%10.5e';
0082
0083 ord=find(el.('PolynomB'));
0084 if isempty(ord)
0085 ord=length(el.('PolynomB'));
0086 end
0087 mp=[el.('FamName') '_n: '...
0088 ' Multipole, '...
0089 ...
0090 ];
0091
0092 if ord>9, ord=9; end;
0093
0094 for imult=1:ord
0095 mp=[mp 'K' num2str(imult-1) 'L= ' num2str(el.('PolynomB')(imult)*el.('Length')*factorial(imult-1),format) ', '...
0096 ];
0097 end
0098
0099 mp=[mp(1:end-2) '; \n'];
0100
0101
0102 mpa=[el.('FamName') '_s: '...
0103 ' Multipole, '...
0104 ...
0105 ];
0106
0107 if ord>9, ord=9; end;
0108
0109 for imult=1:ord
0110 mpa=[mpa 'K' num2str(imult-1) 'L= '...
0111 num2str(el.('PolynomB')(imult)*el.('Length')*factorial(imult-1),format) ', '...
0112 'T' num2str(imult-1) '= 3.142128/' num2str(2*imult+2) ', '...
0113 ];
0114 end
0115
0116 mpa=[mpa(1:end-2) '; \n'];
0117
0118
0119 m=[el.('FamName') ': LINE(' el.('FamName') '_n,' el.('FamName') '_s' ');\n'];
0120
0121 elelat=[elelat mp mpa m '\n'];
0122 case 'Octupole'
0123
0124 ord=find(el.('PolynomB'));
0125 if isempty(ord)
0126 ord=length(el.('PolynomB'));
0127 end
0128 mp=[el.('FamName') ': '...
0129 ' Octupole, L= ' num2str(el.('Length'),format) ', '...
0130 ...
0131 'K' num2str(ord(1)-1) '= ' num2str(el.('PolynomB')(ord(1))*factorial(ord(1)-1),format) '; \n'...
0132
0133
0134 ];
0135 elelat=[elelat mp '\n'];
0136 case 'Monitor'
0137 pu=[el.('FamName') ' :MONITOR' '; '...
0138 ];
0139 elelat=[elelat pu '\n'];
0140 case {'Marker','SkewQuadrupole'}
0141 mrk=[el.('FamName') ' :MARKER' '; '...
0142 ];
0143 elelat=[elelat mrk '\n'];
0144
0145 case 'Corrector'
0146 ki=[el.('FamName') ' :KICKER' '; '...
0147 ];
0148
0149 elelat=[elelat ki '\n'];
0150 case 'Drift'
0151 dr=[el.('FamName') ' : DRIFT, L= ' num2str(el.('Length'),format) '; '];
0152 elelat=[elelat dr '\n'];
0153 case 'RFCavity'
0154 rfc=['RF_ON:=1; \n'...
0155 ...
0156 el.('FamName') ' : RFCavity, L=' num2str(el.('Length'),format)...
0157 ',VOLT=' num2str(el.Voltage/1e6,format) ' &\n'...
0158 ', harm=' num2str(el.HarmNumber,'%3.3d') ', lag=0.0' '; \n'];
0159
0160 elelat=[elelat rfc '\n'];
0161
0162 otherwise
0163 warning(['Element: ' el.('FamName') ' was not converted, since it does not match any Class.'])
0164 end
0165 end
0166 end
0167
0168 def=elelat;
0169
0170 elelat=[];
0171
0172 elelat=[elelat '! LINE \n\n'];
0173
0174 elelat=[elelat linename ' : LINE = ('];
0175
0176
0177
0178 for i=1:length(AT_ring)
0179 if i==1
0180 elelat=[elelat '' AT_ring{i}.('FamName') ' &\n'];
0181 else
0182 if mod(i,5)==0
0183 elelat=[elelat ',' AT_ring{i}.('FamName') ' &\n'];
0184 else
0185 elelat=[elelat ',' AT_ring{i}.('FamName') ' '];
0186 end
0187 end
0188 end
0189
0190 elelat=[elelat ') \n'];
0191
0192
0193
0194 lines=elelat;
0195
0196 elelat=[def lines];
0197
0198
0199
0200 of=fopen(outfile,'w');
0201 fprintf(of,elelat);
0202
0203 fclose('all');
0204
0205
0206
0207
0208 return