0001 function AT_2_Elegant(AT_ring,linename)
0002
0003
0004
0005 outfile='elegantconvertedlattice.lte';
0006 elelat=['!!\n!! Elegant lattice: ' linename '\n!! Created: ' datestr(now) '\n!!\n!!\n\n'];
0007 nkickperL=40;
0008
0009
0010 [families,ind_first_oc_ring]=...
0011 unique(getcellstruct(AT_ring,'FamName',1:length(AT_ring)),'first');
0012
0013 elelat=[elelat '! DEFINITIONS \n\n'];
0014
0015 form='%8.16f';
0016
0017
0018 for i=1:length(families)
0019 el= AT_ring{ind_first_oc_ring(i)};
0020 switch el.('Class')
0021 case 'Bend'
0022 di=[el.('FamName') ': &\n'...
0023 ' CSBEND,L=' num2str(el.('Length'),form) ', &\n'...
0024 ...
0025 ' ANGLE = ' num2str(el.('BendingAngle'),form) ', &\n'...
0026 ' K1 = ' num2str(el.('PolynomB')(2),form) ', &\n'...
0027 ' E1= ' num2str(el.('EntranceAngle'),form) ', &\n'...
0028 ' E2= ' num2str(el.('ExitAngle'),form) ', &\n' ];
0029 ...
0030 if(isfield(el,'T1'))
0031 di= [ di ' DX= ' num2str(-1*el.('T1')(1),form) ', &\n'...
0032 ' DY= ' num2str(-1*el.('T1')(3),form) ', &\n' ] ;
0033 end
0034 di= [di ...
0035 ' N_KICKS= 40 \n'...
0036 ];
0037 elelat=[elelat di '\n'];
0038
0039 case 'Corrector'
0040 hc= [el.('FamName') ' : KICKER \n'];
0041 elelat=[elelat hc '\n'];
0042
0043 case 'Quadrupole'
0044
0045 qp=[el.('FamName') ': &\n'...
0046 ' KQUAD, L= ' num2str(el.('Length'),form) ', &\n'...
0047 ' K1= ' num2str(el.('PolynomB')(2),form) ', &\n'];
0048 ...
0049 if(isfield(el,'Tilt'))
0050 qp= [qp ' TILT= ' num2str(el.('Tilt')) ' & \n'] ;
0051 end
0052
0053 if(isfield(el,'T1'))
0054 qp = [ qp ' DX= ' num2str(-1*el.('T1')(1),form) ', &\n'...
0055 ' DY= ' num2str(-1*el.('T1')(3),form) ', &\n' ] ;
0056 end
0057 qp= [qp ...
0058 ' N_KICKS= 40 \n'...
0059 ];
0060
0061 elelat=[elelat qp '\n'];
0062 case 'Sextupole'
0063 sx=[el.('FamName') ': &\n'...
0064 ' KSEXT, L= ' num2str(el.('Length'),form) ', &\n'...
0065 ...
0066 ' K2= ' num2str(2*el.('PolynomB')(3),form) ', &\n'];
0067 ...
0068 if(isfield(el,'T1'))
0069 sx=[sx ' DX= ' num2str(-1*el.('T1')(1),form) ', &\n'...
0070 ' DY= ' num2str(-1*el.('T1')(3),form) ', &\n'];
0071 end
0072 if(isfield(el,'Tilt'))
0073 sx = [ sx ' TILT= ' num2str(el.('Tilt')) ', &\n' ];
0074 end
0075 sx = [ sx ' N_KICKS= 40 \n'];
0076
0077 elelat=[elelat sx '\n'];
0078 case 'Octupole'
0079 sx=[el.('FamName') ': &\n'...
0080 ' KOCT, L= ' num2str(el.('Length'),form) ', &\n'...
0081 ...
0082 ' K3= ' num2str(6*el.('PolynomB')(4),form) ', &\n'];
0083 if(isfield(el,'T1'))
0084 sx=[sx ' DX= ' num2str(-1*el.('T1')(1),form) ', &\n'...
0085 ' DY= ' num2str(-1*el.('T1')(3),form) ', &\n'];
0086 end
0087 if(isfield(el,'Tilt'))
0088 sx = [ sx ' TILT= ' num2str(el.('Tilt')) ', &\n' ];
0089 end
0090 sx = [ sx ' N_KICKS= 40 \n'];
0091
0092 elelat=[elelat sx '\n'];
0093 case 'Multipole'
0094
0095 ord=find(el.('PolynomB'));
0096 if isempty(ord)
0097 ord=length(el.('PolynomB'));
0098 end
0099 mp=[el.('FamName') ': &\n'...
0100 ' MULT, L= ' num2str(el.('Length'),form) ', &\n'...
0101 'ORDER= ' num2str(ord(1)-1) ', &\n' ...
0102 'KNL= ' num2str(factorial(ord-1)*el.('PolynomB')(ord(1)),form) ', \n'];
0103 ...
0104 if(isfield(el,'T1'))
0105 mp=[mp ' DX= ' num2str(-1*el.('T1')(1),form) ', &\n'...
0106 ' DY= ' num2str(-1*el.('T1')(3),form) ', &\n'];
0107 end
0108 if(isfield(el,'Tilt'))
0109 mp = [ mp ' TILT= ' num2str(el.('Tilt')) ', &\n' ];
0110 end
0111 mp = [ mp ' N_KICKS= 40 \n'];
0112
0113 elelat=[elelat mp '\n'];
0114 case 'Monitor'
0115 pu=[el.('FamName') ' : MONI, L= ' num2str(el.('Length'),form) ' \n'];
0116 elelat=[elelat pu '\n'];
0117 case 'Marker'
0118 mrk=[el.('FamName') ' :MARK' ' \n'...
0119 ];
0120 elelat=[elelat mrk '\n'];
0121 case 'Kicker'
0122 ki=[el.('FamName') ' :KICKER' ' \n'...
0123 ];
0124
0125 elelat=[elelat ki '\n'];
0126 case 'Drift'
0127 dr=[el.('FamName') ' : EDRIFT, L= ' num2str(el.('Length'),form) ' \n'];
0128 elelat=[elelat dr '\n'];
0129 case 'RFCavity'
0130 rfc=[el.('FamName') ' : RFCA, L=' num2str(el.('Length'),form)...
0131 ',VOLT=' num2str(el.('Voltage'),form) ' &\n'...
0132 ',phase="180 2.278145 4 / dasin -",' ' &\n'...
0133 ' freq="c_mks 844.8112 / ' num2str(el.('HarmNumber'),form) ' *" ' ' \n'];
0134
0135 elelat=[elelat rfc '\n'];
0136
0137 otherwise
0138 end
0139
0140 end
0141
0142 otherlines=['!Malign and watch \n'...
0143 'M1: MALIGN,on_pass=0\n'...
0144 'W1: watch, filename="%%s.w1", mode ="centroid"\n\n'...
0145 '!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~! \n'...
0146 '! full 32-cell ring(line): ! \n'...
0147 '! ! \n'...
0148 '!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~! \n\n'
0149 ];
0150 elelat=[elelat otherlines '\n'];
0151
0152
0153 elelat=[elelat '! LINE \n\n'];
0154
0155 elelat=[elelat linename ' : LINE = (M1,W1,' ' &\n'];
0156
0157
0158
0159 for i=1:(length(AT_ring)-1)
0160
0161 elelat=[elelat AT_ring{i}.('FamName') ', '];
0162 if (floor(i/8)==ceil(i/8))
0163 elelat = [elelat '&\n'];
0164 end
0165 end
0166
0167 elelat=[elelat AT_ring{length(AT_ring)}.('FamName') ') \n\n'];
0168
0169
0170
0171
0172 of=fopen(outfile,'w');
0173 fprintf(of,elelat);
0174
0175 fclose('all');
0176
0177
0178
0179
0180 return