0001 function elegant2at(elegantlattice,E0,outfilename)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046 disp('PRELIMINARY INFORMATION:')
0047 disp('');
0048 disp(['1) THE elegant FILE MUST BE OUTPUT OF save_lattice', ...
0049 '&save_lattice filname = %s.new, output_seq=0, &end'...
0050 ' THIS GARANTES AN APPROPRIATE FILE FORMAT']);
0051 disp('');
0052 disp(['2) THE elegant PROGRAM allows to use variable not previously defined.'...
0053 ' This is not known to AT!.'...
0054 ' If the program fails but generates a ..._macro.m file, '...
0055 ' please edit this file reordering the declarations and run it.']);
0056
0057
0058
0059
0060
0061
0062 sX=fopen(elegantlattice,'r');
0063
0064
0065
0066 SXCELL=textscan(sX,'%s','delimiter','\n');
0067
0068
0069 SXSTRING=SXCELL{1};
0070
0071
0072 tmp={};
0073
0074 for i=1:length(SXSTRING)
0075 if SXSTRING{i}(end)=='&'
0076 SXSTRING{i}=SXSTRING{i}(1:end-1);
0077 end;
0078
0079 SXSTRING{i}=strrep(SXSTRING{i},' ','');
0080
0081
0082
0083
0084 c=[1 sort([strfind(SXSTRING{i},',') strfind(SXSTRING{i},':')...
0085 strfind(SXSTRING{i},'=') strfind(SXSTRING{i},'(')...
0086 ]) length(SXSTRING{i})];
0087
0088 for jc=1:length(c)
0089 if jc==1
0090 tmp=[tmp SXSTRING{i}(c(jc):c(jc+1))];
0091 else
0092 if jc==length(c)
0093 tmp=[tmp SXSTRING{i}(c(jc))];
0094 else
0095 tmp=[tmp SXSTRING{i}(c(jc)+1:c(jc+1))];
0096 end
0097 end
0098 end
0099
0100
0101 end
0102 SXSTRING=tmp;
0103
0104
0105
0106
0107
0108 filemacroname=[tempname '.m'];
0109
0110 mafileout=fopen(filemacroname,'w+');
0111 mst=['%% this is a macro that converts to AT the madX lattice: '...
0112 elegantlattice '\n%%\n%% Created: ' datestr(now)...
0113 '\n%%\n%%\n\nglobal GLOBVAL;\nGLOBVAL.E0=' num2str(E0)...
0114 ';\n\n\n'];
0115 def=['\n\n%%%% DEFINITIONS \n\n'];
0116 lines=['\n\n%%%% LINES \n\n'];
0117
0118
0119 j=1;
0120
0121 elemcount=0;
0122 i=1;
0123
0124 while i<length(SXSTRING)-2
0125
0126 if SXSTRING{i}(end)==':'
0127 def=[ def ...
0128 ];
0129 SXSTRING{i}(1:end-1)=upper(SXSTRING{i}(1:end-1));
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139 ElementType=SXSTRING{i+j}(1:end-1);
0140
0141 nwel=SXSTRING{i+j}(end);
0142
0143 switch ElementType
0144 case {'QUAD','quadrupole','QUADRUPOLE', 'QUADRUPO'}
0145 def=[def '\n'];
0146 def=[ def ...
0147 SXSTRING{i}(1:end-1)...
0148 '=atquadrupole('''...
0149 SXSTRING{i}(1:end-1)...
0150 ''',0,0,''StrMPoleSymplectic4Pass'');\n'...
0151 SXSTRING{i}(1:end-1) '.(''NumIntSteps'')=10; \n'...
0152 ];
0153
0154
0155 elemcount=elemcount+1;
0156 while nwel~=':'
0157 def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),SXSTRING{i+j},SXSTRING{i+j+1});
0158 j=j+1;
0159 nwel=SXSTRING{i+j}(end);
0160 end
0161
0162 case {'SEXT','sextupole','SEXTUPOLE'}
0163 def=[def '\n'];
0164 def=[ def ...
0165 SXSTRING{i}(1:end-1)...
0166 '=atsextupole('''...
0167 SXSTRING{i}(1:end-1)...
0168 ''',0,0,''StrMPoleSymplectic4Pass'');\n'...
0169 ];
0170
0171
0172 elemcount=elemcount+1;
0173
0174 while nwel~=':'
0175 def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),SXSTRING{i+j},SXSTRING{i+j+1});
0176
0177 j=j+1;
0178 nwel=SXSTRING{i+j}(end);
0179 end
0180 def=[ def ...
0181 SXSTRING{i}(1:end-1) '.(''PolynomB'')=' SXSTRING{i}(1:end-1) '.(''PolynomB'')*1/2' ';\n' ...
0182 ];
0183
0184 case {'rbend','RBEND'}
0185
0186 def=[def '\n'];
0187
0188 def=[ def ...
0189 SXSTRING{i}(1:end-1)...
0190 '=atrbend('''...
0191 SXSTRING{i}(1:end-1)...
0192 ''',0,0,0,''BndMPoleSymplectic4Pass'');\n'...
0193 ];
0194
0195
0196 elemcount=elemcount+1;
0197
0198 while nwel~=':'
0199 def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),SXSTRING{i+j},SXSTRING{i+j+1});
0200
0201 j=j+1;
0202 nwel=SXSTRING{i+j}(end);
0203 end
0204
0205 def=[ def ...
0206 SXSTRING{i}(1:end-1) '.(''EntranceAngle'')=' SXSTRING{i}(1:end-1) '.(''EntranceAngle'')+' SXSTRING{i}(1:end-1) '.(''BendingAngle'')/2; \n'...
0207 SXSTRING{i}(1:end-1) '.(''ExitAngle'')=' SXSTRING{i}(1:end-1) '.(''ExitAngle'')+' SXSTRING{i}(1:end-1) '.(''BendingAngle'')/2; \n'...
0208 SXSTRING{i}(1:end-1) '.(''Length'')=' SXSTRING{i}(1:end-1)...
0209 '.(''Length'')*(' SXSTRING{i}(1:end-1)...
0210 '.(''BendingAngle'')/2)/sin(' SXSTRING{i}(1:end-1)...
0211 '.(''BendingAngle'')/2); \n'...
0212 SXSTRING{i}(1:end-1) '.(''MaxOrder'')=length(' SXSTRING{i}(1:end-1) '.(''PolynomB''))-1; \n'];
0213
0214 case {'SBEN','sbend','SBEND'}
0215 def=[def '\n'];
0216
0217 def=[ def ...
0218 SXSTRING{i}(1:end-1)...
0219 '=atsbend('...
0220 '''' SXSTRING{i}(1:end-1)...
0221 ''',0,0,0,''BndMPoleSymplectic4Pass'');\n'...
0222 SXSTRING{i}(1:end-1) '.(''NumIntSteps'')=10; \n'...
0223 ];
0224
0225 elemcount=elemcount+1;
0226
0227 while nwel~=':'
0228 def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),...
0229 SXSTRING{i+j},SXSTRING{i+j+1});
0230 j=j+1;
0231 nwel=SXSTRING{i+j}(end);
0232 end
0233
0234 def=[ def SXSTRING{i}(1:end-1) '.(''MaxOrder'')=length(' SXSTRING{i}(1:end-1) '.(''PolynomB''))-1; \n'];
0235
0236 case {'DRIF','DRIFT','drift'}
0237 def=[def '\n'];
0238
0239 def=[ def ...
0240 SXSTRING{i}(1:end-1)...
0241 '=atdrift('...
0242 '''' SXSTRING{i}(1:end-1)...
0243 ''',0,''DriftPass'');\n'...
0244 ];
0245
0246
0247 elemcount=elemcount+1;
0248
0249 while nwel~=':'
0250 def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),...
0251 SXSTRING{i+j},SXSTRING{i+j+1});
0252
0253 j=j+1;
0254 nwel=SXSTRING{i+j}(end);
0255 end
0256 case {'RFC','RFCA','RFCAVITY','rfcavity'}
0257 def=[def '\n'];
0258
0259 def=[ def ...
0260 SXSTRING{i}(1:end-1)...
0261 '=atrfcavity(''' SXSTRING{i}(1:end-1)...
0262 ''',0,0,0,0,' num2str(E0) ...
0263 ',''DriftPass'');\n'...
0264 ];
0265
0266 elemcount=elemcount+1;
0267
0268
0269 while nwel~=':'
0270 def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),...
0271 SXSTRING{i+j},SXSTRING{i+j+1});
0272
0273 j=j+1;
0274 nwel=SXSTRING{i+j}(end);
0275 end
0276 case {'MULT','MULTIPOLE','multipole'}
0277
0278
0279
0280 def=[def '\n'];
0281
0282 def=[ def ...
0283 SXSTRING{i}(1:end-1) '=atthinmultipole('...
0284 '''' SXSTRING{i}(1:end-1) ''''...
0285 ',[0 0 0 0],[0 0 0 0],'...
0286 '''ThinMPolePass'');\n'...
0287 SXSTRING{i}(1:end-1) '.(''Length'')=0; \n'...
0288 ];
0289
0290
0291 elemcount=elemcount+1;
0292
0293 while nwel~=':'
0294
0295 multipoles=[];
0296 if strcmp(SXSTRING{i+j+1}(1),'{')
0297 multipoles=[multipoles '[' SXSTRING{i+j+1}(2:end)];
0298 k=2;
0299 while ~strcmp(SXSTRING{i+j+k}(end),'}') && k<10
0300 multipoles=[multipoles SXSTRING{i+j+k}];
0301 k=k+1;
0302 end
0303 multipoles=[multipoles SXSTRING{i+j+k}(1:(end-1)) ']'];
0304
0305 end
0306
0307 if ~isempty(multipoles)
0308 def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),...
0309 SXSTRING{i+j},multipoles);
0310
0311 else
0312 def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),...
0313 SXSTRING{i+j},SXSTRING{i+j+1});
0314 end
0315 j=j+1;
0316 nwel=SXSTRING{i+j}(end);
0317
0318 end
0319 def=[ def ...
0320 SXSTRING{i}(1:end-1) '.(''Class'')=''Multipole''; \n'...
0321 SXSTRING{i}(1:end-1) '.(''MaxOrder'')=numel(' ...
0322 SXSTRING{i}(1:end-1) '.(''PolynomB'')' ')-1; \n'...
0323 'expansionCoefFixA=1./factorial([1: numel(' ...
0324 SXSTRING{i}(1:end-1) '.(''PolynomA''))]-1); \n'...
0325 'expansionCoefFixB=1./factorial([1: numel(' ...
0326 SXSTRING{i}(1:end-1) '.(''PolynomB''))]-1); \n'...
0327 SXSTRING{i}(1:end-1) '.(''PolynomB'')=(' ...
0328 SXSTRING{i}(1:end-1) '.(''PolynomB'')' ').*expansionCoefFixB; \n'...
0329 SXSTRING{i}(1:end-1) '.(''PolynomA'')=(' ...
0330 SXSTRING{i}(1:end-1) '.(''PolynomA'')' ').*expansionCoefFixA; \n'...
0331 ];
0332
0333 case {'OCTUPOLE','octupole'}
0334 def=[def '\n'];
0335
0336 def=[ def ...
0337 SXSTRING{i}(1:end-1) '=atmultipole('...
0338 '''' SXSTRING{i}(1:end-1) ''''...
0339 ',0,[0 0 0 0],[0 0 0 0],'...
0340 '''StrMPoleSymplectic4Pass'');\n'...
0341 ];
0342
0343
0344 elemcount=elemcount+1;
0345
0346
0347 while nwel~=':'
0348 def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),...
0349 SXSTRING{i+j},SXSTRING{i+j+1});
0350
0351 j=j+1;
0352 nwel=SXSTRING{i+j}(end);
0353 end
0354
0355
0356 def=[ def ...
0357 SXSTRING{i}(1:end-1) '.(''Class'')=''Octupole''; \n'...
0358 SXSTRING{i}(1:end-1) '.(''MaxOrder'')=numel(' ...
0359 SXSTRING{i}(1:end-1) '.(''PolynomB'')' ')-1; \n'...
0360 'expansionCoefFixA=1./factorial([1: numel(' ...
0361 SXSTRING{i}(1:end-1) '.(''PolynomA''))]-1); \n'...
0362 'expansionCoefFixB=1./factorial([1: numel(' ...
0363 SXSTRING{i}(1:end-1) '.(''PolynomB''))]-1); \n'...
0364 SXSTRING{i}(1:end-1) '.(''PolynomB'')=(' ...
0365 SXSTRING{i}(1:end-1) '.(''PolynomB'')' ').*expansionCoefFixB; \n'...
0366 SXSTRING{i}(1:end-1) '.(''PolynomA'')=(' ...
0367 SXSTRING{i}(1:end-1) '.(''PolynomA'')' ').*expansionCoefFixA; \n'...
0368 ];
0369
0370 case {'SOLENOID','solenoid'}
0371 def=[def '\n'];
0372
0373 def=[ def ...
0374 SXSTRING{i}(1:end-1) '=atsolenoid('...
0375 '''' SXSTRING{i}(1:end-1) ''''...
0376 ',0,0,'...
0377 '''SolenoidLinearPass'');\n'...
0378 ];
0379
0380
0381 elemcount=elemcount+1;
0382
0383 while nwel~=':'
0384 def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),...
0385 SXSTRING{i+j},SXSTRING{i+j+1});
0386
0387 j=j+1;
0388 nwel=SXSTRING{i+j}(end);
0389 end
0390 case {'MARKER','marker','marke'}
0391 def=[def '\n'];
0392
0393 def=[ def ...
0394 SXSTRING{i}(1:end-1) '=atmarker('...
0395 '''' SXSTRING{i}(1:end-1) ''');\n'...
0396 SXSTRING{i}(1:end-1) '.(''Length'')=0; \n'...
0397 SXSTRING{i}(1:end-1) '.(''Class'')=''Marker''; \n'...
0398 ];
0399
0400 elemcount=elemcount+1;
0401
0402 while nwel~=':'
0403 def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),...
0404 SXSTRING{i+j},SXSTRING{i+j+1});
0405
0406 j=j+1;
0407 nwel=SXSTRING{i+j}(end);
0408 end
0409 case {'HMO','VMO','HMON','VMON'}
0410 def=[def '\n'];
0411
0412 def=[ def ...
0413 SXSTRING{i}(1:end-1) '.(''FamName'')=''BPM'';\n' ...
0414 SXSTRING{i}(1:end-1) '.(''Class'')=''Monitor''; \n'...
0415 SXSTRING{i}(1:end-1) '.(''BetaCode'')=''PU''; \n'...
0416 SXSTRING{i}(1:end-1) '.(''PassMethod'')=''IdentityPass''; \n'...
0417 SXSTRING{i}(1:end-1) '.(''MaxOrder'')=1; \n'...
0418 SXSTRING{i}(1:end-1) '.(''NumIntSteps'')=2; \n'...
0419 SXSTRING{i}(1:end-1) '.(''Length'')=0; \n'...
0420 SXSTRING{i}(1:end-1) '.(''Energy'')=' num2str(E0) '; \n'...
0421 SXSTRING{i}(1:end-1) '.(''PolynomB'')=zeros(1,4); \n'...
0422 SXSTRING{i}(1:end-1) '.(''PolynomA'')=zeros(1,4); \n'...
0423 ];
0424 elemcount=elemcount+1;
0425
0426 while nwel~=':'
0427 def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),...
0428 SXSTRING{i+j},SXSTRING{i+j+1});
0429
0430 j=j+1;
0431 nwel=SXSTRING{i+j}(end);
0432 end
0433
0434 case {'MAR','MARK'}
0435
0436 def=[ def ...
0437 SXSTRING{i}(1:end-1) '.(''FamName'')=''' SXSTRING{i}(1:end-1) ''';\n' ...
0438 SXSTRING{i}(1:end-1) '.(''Class'')=''Monitor''; \n'...
0439 SXSTRING{i}(1:end-1) '.(''Length'')=0; \n'...
0440 SXSTRING{i}(1:end-1) '.(''PassMethod'')=''IdentityPass''; \n'...
0441 ];
0442 while nwel~=':'
0443 def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),...
0444 SXSTRING{i+j},SXSTRING{i+j+1});
0445
0446 j=j+1;
0447 nwel=SXSTRING{i+j}(end);
0448 end
0449
0450 case {'HKIC','VKIC','KIC','hkic','vkic','kic'}
0451
0452 def=[def '\n'];
0453 def=[ def ...
0454 SXSTRING{i}(1:end-1) '= atmultipole('...
0455 '''' SXSTRING{i}(1:end-2) ''''...
0456 ',0,[0 0 0 0],[0 0 0 0],'...
0457 '''StrMPoleSymplectic4Pass'');\n'...
0458 ];
0459
0460 def=[ def ...
0461 SXSTRING{i}(1:end-1) '.(''MaxOrder'')=1; \n'...
0462 SXSTRING{i}(1:end-1) '.(''Energy'')=' num2str(E0) '; \n'...
0463 SXSTRING{i}(1:end-1) '.(''NumIntSteps'')=10; \n'...
0464 ];
0465
0466
0467 elemcount=elemcount+1;
0468
0469 while nwel~=':'
0470 def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),...
0471 SXSTRING{i+j},SXSTRING{i+j+1});
0472
0473 j=j+1;
0474 nwel=SXSTRING{i+j}(end);
0475 end
0476
0477 case {'MAXAMP','maxamp'}
0478 def = [def SXSTRING{i}(1:end-1) '= ataperture('...
0479 '''' SXSTRING{i}(1:end-1) ''''...
0480 ',[0 0 0 0],AperturePass'');\n'...
0481 ];
0482
0483 while nwel~=':'
0484 def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),...
0485 SXSTRING{i+j},SXSTRING{i+j+1});
0486
0487 j=j+1;
0488 nwel=SXSTRING{i+j}(end);
0489 end
0490
0491 case {'LINE','line'}
0492
0493 seqname=SXSTRING{i}(1:end-1);
0494 j=j+1;
0495
0496 elname=SXSTRING{i+j};
0497 at='at =';
0498 lines=[lines seqname '= {'];
0499
0500 while strcmp(at,'at =') && ~strcmp(elname,')')
0501 elname= SXSTRING{i+j};
0502
0503 if ~strcmp(elname,')')
0504 elname=strrep(elname,' ','');
0505 elname=strrep(elname,' ','');
0506 elname=strrep(elname,',','');
0507 elname=strrep(elname,'(','');
0508 elname=strrep(elname,')','');
0509
0510 lines=[lines upper(elname) ' '];
0511
0512 j=j+1;
0513
0514 end
0515 end
0516 lines=[lines '};\n'];
0517
0518
0519
0520
0521
0522
0523 otherwise
0524 disp(['Unknown element type: ' ElementType])
0525 end
0526 end
0527
0528 i=i+j;
0529 j=1;
0530
0531 end
0532 lines = [lines '\n %% BUILD LATTICE \n'...
0533 '\nglobal THERING;\nTHERING = transpose(' seqname ');\n'];
0534
0535
0536
0537
0538 macroconvertmadXAT=strrep([mst def lines],';;',';');
0539 fprintf(mafileout,macroconvertmadXAT);
0540
0541 fclose('all');
0542
0543
0544 clear macroconvertmadXAT def lines mst
0545 clear SXSTRING i j SXCELL elemcount ElementType elname
0546
0547
0548
0549 [~,elegantlattice] = fileparts(elegantlattice);
0550
0551 try
0552
0553 run(filemacroname)
0554
0555 if nargin<3
0556 fileoutname = [elegantlattice '_AT.m'];
0557 end
0558
0559 name=strrep(fileoutname,'.m','');
0560 varoutname=sprintf('%s.mat',name);
0561
0562 save(varoutname,'THERING');
0563
0564 fclose('all');
0565
0566 movefile(filemacroname,fileoutname);
0567
0568
0569
0570 catch
0571
0572 fileoutname=[elegantlattice '_AT_macro.m'];
0573 movefile(filemacroname,fileoutname);
0574
0575 disp(['saved macro in : ' fileoutname]);
0576
0577 error(['Could not run the macro file.'...
0578 ' It is now in your current directory.'...
0579 ' Please check the order of the definitions']);
0580 end
0581
0582
0583 return