0001 function [seqfilemadX]=mad8TOmadx(seqfilemad8,periodname)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 disp('if periodname is not specified I assume the name of the file (up to the .) to be the sequence name that you want to use')
0026 s8=fopen(seqfilemad8,'r');
0027
0028 sxs=[];
0029
0030
0031
0032
0033
0034
0035
0036 S8CELL=textscan(s8,'%s');
0037 S8STRING=S8CELL{1};
0038
0039
0040
0041
0042
0043
0044 j=1;
0045 sxs=['! MADX converted sequence from ' seqfilemad8 ' '];
0046 corrcount=1;
0047 i=12;
0048 S8STRING{12}
0049 while i<length(S8STRING)-2
0050
0051 if S8STRING{i}(end)==':'
0052 sxs=[sxs ';\n ' S8STRING{i}];
0053
0054 S8STRING{i+j}=strrep(S8STRING{i+j},'MARKER','MARKER,');
0055 S8STRING{i+j}=strrep(S8STRING{i+j},'MARKER,,','MARKER,');
0056 S8STRING{i+j}=strrep(S8STRING{i+j},'KICKER','KICKER,');
0057 S8STRING{i+j}=strrep(S8STRING{i+j},'KICKER,,','KICKER,');
0058 S8STRING{i+j}=strrep(S8STRING{i+j},'MONITOR','MONITOR,');
0059 S8STRING{i+j}=strrep(S8STRING{i+j},'MONITOR,,','MONITOR,');
0060
0061 ElementType=S8STRING{i+j}(1:end-1);
0062
0063
0064 nwel=S8STRING{i+j}(end);
0065 switch ElementType
0066 case 'QUADRUPOLE'
0067 while nwel~=':'
0068 sxs=[sxs ' ' S8STRING{i+j}];
0069 j=j+1;
0070 nwel=S8STRING{i+j}(end);
0071 end
0072
0073 elname=S8STRING{i}(1:end-1);
0074 if strcmp(elname(1:2),'QD')
0075 else
0076 Q_CM=[';\n'...
0077 'CH_' num2str(corrcount) ': Hkicker, kick:=0,L=0;\n'...
0078 'CV_' num2str(corrcount) ': Vkicker, kick:=0,L=0;\n'...
0079 'MH_' num2str(corrcount) ': Hmonitor;\n'...
0080 'MV_' num2str(corrcount) ': Vmonitor;\n'...
0081 'h_' num2str(corrcount) ': QUADRUPOLE, K1=' elname '->K1, L=(' elname '->L)/2;\n'...
0082 elname ': LINE=(h_' num2str(corrcount) ',CH_' num2str(corrcount) ',CV_' num2str(corrcount) ',MH_' num2str(corrcount) ', MV_' num2str(corrcount) ',h_' num2str(corrcount) ');\n'...
0083 ];
0084 corrcount=corrcount+1;
0085
0086
0087 end
0088 case 'SEXTUPOLE'
0089 while nwel~=':'
0090 sxs=[sxs ' ' S8STRING{i+j}];
0091 j=j+1;
0092 nwel=S8STRING{i+j}(end);
0093 end
0094
0095 elname=S8STRING{i}(1:end-1);
0096 if strcmp(elname(1:2),'SD')
0097 S_CM=[';\n'...
0098 'CH_' num2str(corrcount) ': Hkicker, kick:=0,L=0;\n'...
0099 'CV_' num2str(corrcount) ': Vkicker, kick:=0,L=0;\n'...
0100 'ksq' num2str(corrcount) ':=0;\n'...
0101 'SQ_' num2str(corrcount) ': MULTIPOLE, ksl:={0,ksq' num2str(corrcount) '},L=0;\n'...
0102 'MH_' num2str(corrcount) ': Hmonitor;\n'...
0103 'MV_' num2str(corrcount) ': Vmonitor;\n'...
0104 'h_' num2str(corrcount) ': SEXTUPOLE, K2:=' elname '->K2, L:=(' elname '->L)/2;\n'...
0105 elname ': LINE=(h_' num2str(corrcount) ',CH_' num2str(corrcount) ',CV_' num2str(corrcount) ',SQ_' num2str(corrcount) ',MH_' num2str(corrcount) ', MV_' num2str(corrcount) ',h_' num2str(corrcount) ');\n'...
0106 ];
0107
0108
0109 corrcount=corrcount+1;
0110 end
0111 case 'RBEND'
0112 while nwel~=':'
0113 sxs=[sxs ' ' S8STRING{i+j}];
0114 j=j+1;
0115 nwel=S8STRING{i+j}(end);
0116 end
0117 case 'SBEND'
0118 while nwel~=':'
0119 sxs=[sxs ' ' S8STRING{i+j}];
0120 j=j+1;
0121 nwel=S8STRING{i+j}(end);
0122 end
0123 case 'DRIFT'
0124 while nwel~=':'
0125 sxs=[sxs ' ' S8STRING{i+j}];
0126 j=j+1;
0127 nwel=S8STRING{i+j}(end);
0128 end
0129 case 'RFCAVITY'
0130 while nwel~=':'
0131 a=sum(strfind(S8STRING{i+j},'REMOVED'));
0132 if a==0
0133
0134 sxs=[sxs ' ' S8STRING{i+j}];
0135 else
0136 disp(S8STRING{i+j})
0137 end
0138 j=j+1;
0139 nwel=S8STRING{i+j}(end);
0140
0141 end
0142 case 'MULTIPOLE'
0143 KNL=['KNL:={'];
0144 while nwel~=':'
0145 if S8STRING{i+j}(1)~='K'
0146 sxs=[sxs ' ' S8STRING{i+j}];
0147 else
0148 for knl=1:str2double(S8STRING{i+j}(2))
0149 KNL=[KNL '0,'];
0150 end
0151 KNL=[KNL S8STRING{i+j}(5:end) '*OCT_ON}'];
0152 sxs=[sxs ' ' KNL ];
0153 end
0154
0155 j=j+1;
0156 nwel=S8STRING{i+j}(end);
0157 end
0158 case 'OCTUPOLE'
0159 while nwel~=':'
0160 sxs=[sxs ' ' S8STRING{i+j}];
0161 j=j+1;
0162 nwel=S8STRING{i+j}(end);
0163 end
0164 case 'SOLENOID'
0165 while nwel~=':'
0166 sxs=[sxs ' ' S8STRING{i+j}];
0167 j=j+1;
0168 nwel=S8STRING{i+j}(end);
0169 end
0170
0171 case 'MARKER'
0172 while nwel~=':'
0173
0174 sxs=[sxs ' ' S8STRING{i+j}];
0175 j=j+1;
0176 nwel=S8STRING{i+j}(end);
0177 end
0178 case 'MONITOR'
0179 while nwel~=':'
0180 sxs=[sxs ' ' S8STRING{i+j}];
0181 j=j+1;
0182 nwel=S8STRING{i+j}(end);
0183 end
0184 case 'KICKER'
0185 while nwel~=':'
0186 sxs=[sxs ' ' S8STRING{i+j}];
0187 j=j+1;
0188 nwel=S8STRING{i+j}(end);
0189 end
0190 case 'CONSTAN'
0191 sxs=[S8STRING{i}(1:end-1) '=' S8STRING{i+j+2} ';\n' sxs(1:end-length(S8STRING{i+j}))];
0192 j=j+3;
0193 otherwise
0194
0195 LineMAD=[];
0196 if strcmp(S8STRING{i+j}(1:5),'LINE=')
0197 nwel2=S8STRING{i+j+1}(end);
0198 while nwel~=':' && nwel2~='='
0199
0200 if S8STRING{i+j}(end)=='&';
0201 S8STRING{i+j}=S8STRING{i+j}(1:end-1);
0202 end
0203
0204
0205 LineMAD=[LineMAD ' ' S8STRING{i+j}];
0206 j=j+1;
0207 nwel=S8STRING{i+j}(end);
0208 nwel2=S8STRING{i+j+1}(end);
0209 end
0210
0211 multiplication=strfind(LineMAD,'*');
0212 k=0;
0213 for im=multiplication
0214 ind=im+k;
0215 comma=strfind(LineMAD(ind:end),',');
0216
0217 if length(comma)>=1 && ~isempty(LineMAD(ind:end))
0218 comma=comma(1);
0219 LineMAD=[LineMAD(1:ind) '(' LineMAD(ind+1:ind+comma-2) ')' LineMAD(ind+comma-1:end)];
0220 else
0221 LineMAD=[LineMAD(1:ind) '(' LineMAD(ind+1:end) ')'];
0222 end
0223 k=k+2;
0224 end
0225 sxs=[sxs ' ' LineMAD ';\n'];
0226
0227 if nwel=='='
0228 j=j-1;
0229
0230
0231
0232 end
0233
0234 else
0235 disp(['Unknown element type: ' ElementType])
0236 end
0237
0238
0239 end
0240
0241
0242
0243 else
0244
0245
0246 sxs=[S8STRING{i} S8STRING{i+1} S8STRING{i+2} ';\n ' sxs];
0247 j=3;
0248 end
0249
0250 i=i+j;
0251 j=1;
0252
0253 end
0254
0255 disp(corrcount)
0256
0257
0258
0259
0260 [~,seqfilemad8]=fileparts(seqfilemad8);
0261 seqfilemadX=[seqfilemad8 'X'];
0262 sX=fopen(seqfilemadX,'w+');
0263 sXt=fopen(['test_' seqfilemadX ],'w+');
0264
0265
0266 if (nargin == 1)
0267 sqname=strrep(seqfilemadX,'.mad8X','');
0268 sqname=strrep(sqname,'.madX','');
0269 sqname=strrep(seqfilemadX,'.MAD8X','');
0270 sqname=strrep(sqname,'.MADX','');
0271 sxs=strrep(sxs,'RING_FF',sqname);
0272 elseif (nargin == 2)
0273
0274 sqname=periodname;
0275
0276 sxs=strrep(sxs,'RING_FF',sqname);
0277 end
0278
0279 testtwiss=[...
0280 'setplot,ascale=2,lscale=2,sscale=1.5,rscale=2,xsize=28,ysize=20, \n'...
0281 ' post=2,font=-4,lwidth=10;\n'...
0282 '\n beam;'...
0283 '\n use, period=' sqname ';'...
0284 '\n twiss,chrom,file=test.twiss;'...
0285 '\n plot, haxis=s, vaxis=rbetx,rbety, interpolate, style=1,colour=100,file=' sqname ';'...
0286 ...
0287 ...
0288 ...
0289 ...
0290 ...
0291 ...
0292 ...
0293 ...
0294 ...
0295 ...
0296 ...
0297 ...
0298 ];
0299
0300 trackingLines=[...
0301 '\n select, flag=twiss,class=Hkicker,column=name,s; twiss,file=tt;\n\n'...
0302 '\n SURVEY, x0=0, y0=0, z0=0, theta0=0, phi0=0, psi0=0,file=surv, table=survey, sequence=sqname;\n'...
0303 'plot, table=survey,haxis=z,vaxis=x;\n'...
0304 'save, sequence=' sqname ', file=' seqfilemadX(~ismember(seqfilemadX,'.')) '.seq; ! savesequence \n '...
0305 ...
0306 ...
0307 ];
0308
0309 rootdrawlayout=[
0310 '\n use, period=' sqname ';\n'...
0311 'ptc_create_universe;\n'...
0312 'ptc_create_layout, model=1, method=6, nst=100, exact=false, closed_layout=false;\n'...
0313 'ptc_setswitch, debuglevel=2, maxacceleration=true, exact_mis=true, time=true, totalpath=true, fringe=true;\n'...
0314 'ptc_printframes, file="draw' sqname '.C", format=rootmacro;\n'...
0315 'ptc_end;\n'...
0316 ];
0317
0318
0319 sxs=strrep(sxs,'&','');
0320 sxs=strrep(sxs,';\n;',';\n\n');
0321 sxs=strrep(sxs,'K2=','K2=SXT_ON*');
0322 sxs=strrep(sxs,'=',':=');
0323 sxs=strrep(sxs,'::=',':=');
0324 sxs=strrep(sxs,']','');
0325 sxs=strrep(sxs,'[','->');
0326 sxs=strrep(sxs,'LINE:=','LINE=');
0327 sxs=strrep(sxs,', ',',');
0328
0329
0330
0331 fprintf(sX,[sxs]);
0332 fprintf(sXt,['call,file=' seqfilemadX ';\n' testtwiss trackingLines rootdrawlayout]);
0333
0334 system(['madxp < ' ['test_' seqfilemadX ] ])
0335
0336 fclose('all');
0337 clear all
0338 close all
0339
0340 return