0001 function atfrommadx(seqfilemadX,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
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067 disp('important notice about conversion:')
0068 disp('');
0069 disp(['1) THE MADX FILE MUST BE OUTPUT OF save, file=seqfilemadX;'...
0070 ' THIS GARANTES AN APPROPRIATE FILE FORMAT']);
0071 disp('');
0072 disp(['2) THE MADX PROGRAM allows to use variable not previously defined.'...
0073 ' This is not known to AT!.'...
0074 ' If the program fails but generates a ..._macro.m file, '...
0075 ' please edit this file reordering the declarations and run it.']);
0076 disp('');
0077 disp(['3) If periodname is not specified' ...
0078 ' It is assumed the name of the file (up to the .) ' ...
0079 'to be the sequence name that you want to use']);
0080
0081
0082 sX=fopen(seqfilemadX,'r');
0083
0084
0085
0086
0087
0088
0089
0090 SXCELL=textscan(sX,'%s','CollectOutput',0,'delimiter','\n');
0091
0092 A=SXCELL{1};
0093 B={};
0094
0095 iia=1;
0096 iib=1;
0097
0098 B{1}=[];
0099
0100 while iia~=length(A)
0101
0102
0103 if ~strcmp(A{iia}(end),';')
0104
0105 B{iib}=[B{iib} A{iia}];
0106
0107 else
0108
0109 B{iib}=[B{iib} A{iia}(1:end-1)];
0110
0111 iib=iib+1;
0112 B{iib}=[];
0113 end
0114
0115 iia=iia+1;
0116
0117 end
0118
0119 SXSTRING=B';
0120
0121
0122 tmp={};
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148 tmp=cellfun(@(a)formatTextMADX(a),SXSTRING,'un',0);
0149
0150 SXSTRING=[tmp{:}];
0151
0152
0153
0154
0155 filemacroname=[tempname '.m'];
0156
0157 mafileout=fopen(filemacroname,'w+');
0158 mst=['%% this is a macro that converts to AT the madX lattice: '...
0159 seqfilemadX '\n%%\n%% Created: ' datestr(now)...
0160 '\n%%\n%%\n\nglobal GLOBVAL;\nGLOBVAL.E0=' num2str(E0)...
0161 ';\n\n\n'];
0162 def=['\n\n%%%% DEFINITIONS \n\n'];
0163 lines=['\n\n%%%% LINES \n\n'];
0164 var=['%%%% VARIABLES \n\n sxt_on=1;'];
0165 formulas=['\n\n%%%% RELATIONS \n\n'];
0166
0167
0168 j=1;
0169 h=waitbar(0,'Converting: ');
0170
0171 elemcount=0;
0172 i=1;
0173 while i<length(SXSTRING)-2
0174
0175
0176 if SXSTRING{i}(end)==':'
0177 def=[ def ...
0178 ];
0179 SXSTRING{i}(1:end-1)=upper(SXSTRING{i}(1:end-1));
0180
0181 SXSTRING{i+j}=strrep(SXSTRING{i+j},'MARKER','MARKER,');
0182 SXSTRING{i+j}=strrep(SXSTRING{i+j},'MARKER,,','MARKER,');
0183 SXSTRING{i+j}=strrep(SXSTRING{i+j},'KICKER','KICKER,');
0184 SXSTRING{i+j}=strrep(SXSTRING{i+j},'KICKER,,','KICKER,');
0185 SXSTRING{i+j}=strrep(SXSTRING{i+j},'MONITOR','MONITOR,');
0186 SXSTRING{i+j}=strrep(SXSTRING{i+j},'MONITOR,,','MONITOR,');
0187
0188 ElementType=SXSTRING{i+j}(1:end-1);
0189
0190
0191
0192 waitbar(i/(length(SXSTRING)-2),h,['Converting: ' ElementType]);
0193
0194
0195
0196 nwel=SXSTRING{i+j}(end);
0197
0198 switch ElementType
0199 case {'quadrupole','QUADRUPOLE', 'QUADRUPO'}
0200 def=[def '\n'];
0201 def=[ def ...
0202 SXSTRING{i}(1:end-1)...
0203 '=atquadrupole('''...
0204 SXSTRING{i}(1:end-1)...
0205 ''',0,0,''StrMPoleSymplectic4Pass'');\n'...
0206 ];
0207
0208
0209 elemcount=elemcount+1;
0210 while nwel~=':'
0211 def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),SXSTRING{i+j},SXSTRING{i+j+1});
0212
0213 j=j+1;
0214 nwel=SXSTRING{i+j}(end);
0215 end
0216
0217 case {'sextupole','SEXTUPOLE'}
0218 def=[def '\n'];
0219 def=[ def ...
0220 SXSTRING{i}(1:end-1)...
0221 '=atsextupole('''...
0222 SXSTRING{i}(1:end-1)...
0223 ''',0,0,''StrMPoleSymplectic4Pass'');\n'...
0224 ];
0225
0226
0227 elemcount=elemcount+1;
0228
0229 while nwel~=':'
0230 def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),SXSTRING{i+j},SXSTRING{i+j+1});
0231
0232 j=j+1;
0233 nwel=SXSTRING{i+j}(end);
0234 end
0235 def=[ def ...
0236 SXSTRING{i}(1:end-1) '.(''PolynomB'')=' SXSTRING{i}(1:end-1) '.(''PolynomB'')*1/2' ';\n' ...
0237 ];
0238
0239 case {'rbend','RBEND'}
0240
0241 def=[def '\n'];
0242
0243 def=[ def ...
0244 SXSTRING{i}(1:end-1)...
0245 '=atrbend('''...
0246 SXSTRING{i}(1:end-1)...
0247 ''',0,0,0,''BndMPoleSymplectic4Pass'');\n'...
0248 ];
0249
0250
0251 elemcount=elemcount+1;
0252
0253 while nwel~=':'
0254 def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),SXSTRING{i+j},SXSTRING{i+j+1});
0255
0256 j=j+1;
0257 nwel=SXSTRING{i+j}(end);
0258 end
0259
0260 def=[ def ...
0261 SXSTRING{i}(1:end-1) '.(''EntranceAngle'')=' SXSTRING{i}(1:end-1) '.(''EntranceAngle'')+' SXSTRING{i}(1:end-1) '.(''BendingAngle'')/2; \n'...
0262 SXSTRING{i}(1:end-1) '.(''ExitAngle'')=' SXSTRING{i}(1:end-1) '.(''ExitAngle'')+' SXSTRING{i}(1:end-1) '.(''BendingAngle'')/2; \n'...
0263 SXSTRING{i}(1:end-1) '.(''Length'')=' SXSTRING{i}(1:end-1)...
0264 '.(''Length'')*(' SXSTRING{i}(1:end-1)...
0265 '.(''BendingAngle'')/2)/sin(' SXSTRING{i}(1:end-1)...
0266 '.(''BendingAngle'')/2); \n'...
0267 SXSTRING{i}(1:end-1) '.(''MaxOrder'')=length(' SXSTRING{i}(1:end-1) '.(''PolynomB''))-1; \n'];
0268
0269 case {'sbend','SBEND'}
0270 def=[def '\n'];
0271
0272 def=[ def ...
0273 SXSTRING{i}(1:end-1)...
0274 '=atsbend('...
0275 '''' SXSTRING{i}(1:end-1)...
0276 ''',0,0,0,''BndMPoleSymplectic4Pass'');\n'...
0277 ];
0278
0279 elemcount=elemcount+1;
0280
0281 while nwel~=':'
0282 def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),...
0283 SXSTRING{i+j},SXSTRING{i+j+1});
0284
0285 j=j+1;
0286 nwel=SXSTRING{i+j}(end);
0287 end
0288
0289 def=[ def SXSTRING{i}(1:end-1) '.(''MaxOrder'')=length(' SXSTRING{i}(1:end-1) '.(''PolynomB''))-1; \n'];
0290
0291 case {'DRIFT','drift'}
0292 def=[def '\n'];
0293
0294 def=[ def ...
0295 SXSTRING{i}(1:end-1)...
0296 '=atdrift('...
0297 '''' SXSTRING{i}(1:end-1)...
0298 ''',0,''DriftPass'');\n'...
0299 ];
0300
0301
0302 elemcount=elemcount+1;
0303
0304 while nwel~=':'
0305 def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),...
0306 SXSTRING{i+j},SXSTRING{i+j+1});
0307
0308 j=j+1;
0309 nwel=SXSTRING{i+j}(end);
0310 end
0311 case {'RFCAVITY','rfcavity'}
0312 def=[def '\n'];
0313
0314 def=[ def ...
0315 SXSTRING{i}(1:end-1)...
0316 '=atrfcavity(''' SXSTRING{i}(1:end-1)...
0317 ''',0,0,0,0,' num2str(E0) ...
0318 ',''DriftPass'');\n'...
0319 ];
0320
0321 elemcount=elemcount+1;
0322
0323
0324 while nwel~=':'
0325 def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),...
0326 SXSTRING{i+j},SXSTRING{i+j+1});
0327
0328 j=j+1;
0329 nwel=SXSTRING{i+j}(end);
0330 end
0331 case {'MULTIPOLE','multipole'}
0332
0333
0334
0335 def=[def '\n'];
0336
0337 def=[ def ...
0338 SXSTRING{i}(1:end-1) '=atthinmultipole('...
0339 '''' SXSTRING{i}(1:end-1) ''''...
0340 ',[0 0 0 0],[0 0 0 0],'...
0341 '''ThinMPolePass'');\n'...
0342 SXSTRING{i}(1:end-1) '.(''Length'')=0; \n'...
0343 ];
0344
0345
0346 elemcount=elemcount+1;
0347
0348 while nwel~=':'
0349
0350
0351 multipoles=[];
0352 if strcmp(SXSTRING{i+j+1}(1),'{')
0353 multipoles=[multipoles '[' SXSTRING{i+j+1}(2:end)];
0354 k=2;
0355 while ~strcmp(SXSTRING{i+j+k}(end),'}') && k<10
0356 multipoles=[multipoles SXSTRING{i+j+k}];
0357 k=k+1;
0358 end
0359 multipoles=[multipoles SXSTRING{i+j+k}(1:(end-1)) ']'];
0360
0361 end
0362
0363 if ~isempty(multipoles)
0364 def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),...
0365 SXSTRING{i+j},multipoles);
0366
0367 else
0368 def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),...
0369 SXSTRING{i+j},SXSTRING{i+j+1});
0370 end
0371 j=j+1;
0372 nwel=SXSTRING{i+j}(end);
0373
0374 end
0375 def=[ def ...
0376 SXSTRING{i}(1:end-1) '.(''Class'')=''Multipole''; \n'...
0377 SXSTRING{i}(1:end-1) '.(''MaxOrder'')=numel(' ...
0378 SXSTRING{i}(1:end-1) '.(''PolynomB'')' ')-1; \n'...
0379 'expansionCoefFixA=1./factorial([1: numel(' ...
0380 SXSTRING{i}(1:end-1) '.(''PolynomA''))]-1); \n'...
0381 'expansionCoefFixB=1./factorial([1: numel(' ...
0382 SXSTRING{i}(1:end-1) '.(''PolynomB''))]-1); \n'...
0383 SXSTRING{i}(1:end-1) '.(''PolynomB'')=(' ...
0384 SXSTRING{i}(1:end-1) '.(''PolynomB'')' ').*expansionCoefFixB; \n'...
0385 SXSTRING{i}(1:end-1) '.(''PolynomA'')=(' ...
0386 SXSTRING{i}(1:end-1) '.(''PolynomA'')' ').*expansionCoefFixA; \n'...
0387 ];
0388
0389 case {'OCTUPOLE','octupole'}
0390 def=[def '\n'];
0391
0392 def=[ def ...
0393 SXSTRING{i}(1:end-1) '=atmultipole('...
0394 '''' SXSTRING{i}(1:end-1) ''''...
0395 ',0,[0 0 0 0],[0 0 0 0],'...
0396 '''StrMPoleSymplectic4Pass'');\n'...
0397 ];
0398
0399
0400 elemcount=elemcount+1;
0401
0402
0403 while nwel~=':'
0404 def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),...
0405 SXSTRING{i+j},SXSTRING{i+j+1});
0406
0407 j=j+1;
0408 nwel=SXSTRING{i+j}(end);
0409 end
0410
0411
0412 def=[ def ...
0413 SXSTRING{i}(1:end-1) '.(''Class'')=''Octupole''; \n'...
0414 SXSTRING{i}(1:end-1) '.(''MaxOrder'')=numel(' ...
0415 SXSTRING{i}(1:end-1) '.(''PolynomB'')' ')-1; \n'...
0416 'expansionCoefFixA=1./factorial([1: numel(' ...
0417 SXSTRING{i}(1:end-1) '.(''PolynomA''))]-1); \n'...
0418 'expansionCoefFixB=1./factorial([1: numel(' ...
0419 SXSTRING{i}(1:end-1) '.(''PolynomB''))]-1); \n'...
0420 SXSTRING{i}(1:end-1) '.(''PolynomB'')=(' ...
0421 SXSTRING{i}(1:end-1) '.(''PolynomB'')' ').*expansionCoefFixB; \n'...
0422 SXSTRING{i}(1:end-1) '.(''PolynomA'')=(' ...
0423 SXSTRING{i}(1:end-1) '.(''PolynomA'')' ').*expansionCoefFixA; \n'...
0424 ];
0425
0426 case {'SOLENOID','solenoid'}
0427 def=[def '\n'];
0428
0429 def=[ def ...
0430 SXSTRING{i}(1:end-1) '=atsolenoid('...
0431 '''' SXSTRING{i}(1:end-1) ''''...
0432 ',0,0,'...
0433 '''SolenoidLinearPass'');\n'...
0434 ];
0435
0436
0437 elemcount=elemcount+1;
0438
0439 while nwel~=':'
0440 def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),...
0441 SXSTRING{i+j},SXSTRING{i+j+1});
0442
0443 j=j+1;
0444 nwel=SXSTRING{i+j}(end);
0445 end
0446 case {'MARKER','marker','marke'}
0447 def=[def '\n'];
0448
0449 def=[ def ...
0450 SXSTRING{i}(1:end-1) '=atmarker('...
0451 '''' SXSTRING{i}(1:end-1) ''');\n'...
0452 SXSTRING{i}(1:end-1) '.(''Length'')=0; \n'...
0453 SXSTRING{i}(1:end-1) '.(''Class'')=''Marker''; \n'...
0454 ];
0455
0456 elemcount=elemcount+1;
0457
0458 while nwel~=':'
0459 def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),...
0460 SXSTRING{i+j},SXSTRING{i+j+1});
0461
0462 j=j+1;
0463 nwel=SXSTRING{i+j}(end);
0464 end
0465 case {'MATRIX','matrix','Matrix'}
0466 def=[def '\n'];
0467
0468 def=[ def ...
0469 SXSTRING{i}(1:end-1) '=atM66Tijk('...
0470 '''' SXSTRING{i}(1:end-1) ''');\n'...
0471 SXSTRING{i}(1:end-1) '.(''Length'')=0; \n'...
0472 SXSTRING{i}(1:end-1) '.(''Tijk'')=zeros(6,6,6); \n'...
0473 SXSTRING{i}(1:end-1) '.(''M66'')=eye(6,6); \n'...
0474 SXSTRING{i}(1:end-1) '.(''Class'')=''MatrixTijk''; \n'...
0475 ];
0476
0477 elemcount=elemcount+1;
0478
0479 while nwel~=':'
0480 def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),...
0481 SXSTRING{i+j},SXSTRING{i+j+1});
0482
0483 j=j+1;
0484 nwel=SXSTRING{i+j}(end);
0485 end
0486 case {'Collimator','collimator','COLLIMATOR'}
0487 def=[def '\n'];
0488
0489 def=[ def ...
0490 SXSTRING{i}(1:end-1) '=atmarker('...
0491 '''' SXSTRING{i}(1:end-1) ''');\n'...
0492 SXSTRING{i}(1:end-1) '.(''Length'')=0; \n'...
0493 SXSTRING{i}(1:end-1) '.(''Class'')=''Marker''; \n'...
0494 ];
0495
0496 elemcount=elemcount+1;
0497
0498 while nwel~=':'
0499 def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),...
0500 SXSTRING{i+j},SXSTRING{i+j+1});
0501
0502 j=j+1;
0503 nwel=SXSTRING{i+j}(end);
0504 end
0505 case {'MONITOR','monitor','monito'}
0506 def=[def '\n'];
0507 def=[ def ...
0508 SXSTRING{i}(1:end-1) '.(''FamName'')=''' SXSTRING{i}(1:end-1) ''';\n' ...
0509 SXSTRING{i}(1:end-1) '.(''Class'')=''Monitor''; \n'...
0510 SXSTRING{i}(1:end-1) '.(''BetaCode'')=''PU''; \n'...
0511 SXSTRING{i}(1:end-1) '.(''PassMethod'')=''IdentityPass''; \n'...
0512 SXSTRING{i}(1:end-1) '.(''MaxOrder'')=1; \n'...
0513 SXSTRING{i}(1:end-1) '.(''NumIntSteps'')=2; \n'...
0514 SXSTRING{i}(1:end-1) '.(''Length'')=0; \n'...
0515 SXSTRING{i}(1:end-1) '.(''Energy'')=' num2str(E0) '; \n'...
0516 SXSTRING{i}(1:end-1) '.(''PolynomB'')=zeros(1,4); \n'...
0517 SXSTRING{i}(1:end-1) '.(''PolynomA'')=zeros(1,4); \n'...
0518 ];
0519 elemcount=elemcount+1;
0520
0521 while nwel~=':'
0522 def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),...
0523 SXSTRING{i+j},SXSTRING{i+j+1});
0524
0525 j=j+1;
0526 nwel=SXSTRING{i+j}(end);
0527 end
0528 case {'KICKER','hkicker','vkicker','kicker'}
0529
0530 def=[def '\n'];
0531 def=[ def ...
0532 SXSTRING{i}(1:end-1) '=atmultipole('...
0533 '''' SXSTRING{i}(1:end-1) ''''...
0534 ',0,[0 0 0 0],[0 0 0 0],'...
0535 '''StrMPoleSymplectic4Pass'');\n'...
0536 ];
0537
0538 def=[ def ...
0539 SXSTRING{i}(1:end-1) '.(''MaxOrder'')=1; \n'...
0540 SXSTRING{i}(1:end-1) '.(''Energy'')=' num2str(E0) '; \n'...
0541 ];
0542
0543
0544 elemcount=elemcount+1;
0545
0546 while nwel~=':'
0547 def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),...
0548 SXSTRING{i+j},SXSTRING{i+j+1});
0549
0550 j=j+1;
0551 nwel=SXSTRING{i+j}(end);
0552 end
0553 case {'CONSTAN','constant'}
0554
0555
0556 var=[var SXSTRING{i}(1:end-1) '=' SXSTRING{i+3} '; \n '];
0557
0558 j=j+3;
0559 case {'sequence'}
0560
0561
0562 seqname=SXSTRING{i}(1:end-1);
0563 l=SXSTRING{i+j+2};
0564 j=j+2+1;
0565
0566 waitbar(i/(length(SXSTRING)-2),h,['Convert sequence: ' seqname ' ']);
0567
0568 elname=SXSTRING{i+j};
0569 at=SXSTRING{i+j+1};
0570 lines=[lines seqname '={'];
0571 sposstring=['\n spos=['];
0572
0573
0574 while strcmp(at,'at=') && ~strcmp(elname,'endsequence')
0575
0576 elname=SXSTRING{i+j};
0577
0578
0579
0580
0581
0582
0583 if ~strcmp(elname,'endsequence')&& ~isempty(elname)
0584 elname=strrep(elname,',','');
0585 at=SXSTRING{i+j+1};
0586 spos=SXSTRING{i+j+2};
0587
0588 lines=[lines upper(elname) ';...\n'];
0589 sposstring=[sposstring ',...\n ' num2str(spos)];
0590
0591 j=j+3;
0592 else
0593 j=j+1;
0594 end
0595 end
0596 lines=[lines '};\n'];
0597 sposstring=[sposstring '];\n'];
0598 sposstring(10)=[];
0599
0600
0601 lines=[lines sposstring '\n %% BUILD LATTICE \n'...
0602 seqname '=buildATLattice(' seqname ',spos,' l ');\n'...
0603 ...
0604 ...
0605 ];
0606
0607 otherwise
0608 disp(['Unknown element type: ' ElementType])
0609 end
0610
0611
0612
0613 else
0614 if SXSTRING{i}(1)~='!';
0615
0616
0617
0618
0619 if sum(ismember('()/*+-',SXSTRING{i+1}))>0
0620
0621 formulas=[formulas SXSTRING{i} ' ' SXSTRING{i+1} '; \n '];
0622 else
0623 var=[var SXSTRING{i} ' ' SXSTRING{i+1} '; \n '];
0624 end
0625 j=2;
0626 end
0627 end
0628
0629 i=i+j;
0630 j=1;
0631
0632 end
0633
0634
0635
0636
0637
0638 macroconvertmadXAT=strrep([mst var formulas def lines],';;',';');
0639 fprintf(mafileout,macroconvertmadXAT);
0640
0641 fclose('all');
0642
0643
0644 clear macroconvertmadXAT formulas def lines var mst
0645 clear SXSTRING i j SXCELL elemcount ElementType elname
0646
0647
0648
0649 [~,seqfilemadX]=fileparts(seqfilemadX);
0650
0651 try
0652
0653 run(filemacroname)
0654
0655 if nargin<3
0656 save([seqfilemadX '_AT_LATTICE']);
0657 else
0658 save(outfilename);
0659 end
0660
0661
0662 fileoutname=[seqfilemadX '_AT_macro.m'];
0663
0664 movefile(filemacroname,fileoutname);
0665
0666
0667 catch
0668
0669 fileoutname=[seqfilemadX '_AT_macro.m'];
0670
0671 movefile(filemacroname,fileoutname);
0672
0673 disp(['saved macro in : ' fileoutname]);
0674
0675 error(['could not run the macro file.'...
0676 ' It is now in your current directory.'...
0677 ' Please check the order of the definitions']);
0678 end
0679
0680 fclose('all');
0681 close(h);
0682
0683
0684 return
0685
0686
0687 function tmp=formatTextMADX(str)
0688
0689 tmp={};
0690
0691 str=strrep(str,' ','');
0692 str=strrep(str,':=','=');
0693 str=strrep(str,';','');
0694
0695 c=[1 sort([strfind(str,',') strfind(str,':')...
0696 strfind(str,'=')]) length(str)];
0697
0698
0699 for jc=1:length(c)-1
0700 if jc==1
0701 tmp{jc}=str(c(jc):c(jc+1));
0702 else
0703 tmp{jc}=str(c(jc)+1:c(jc+1));
0704 end
0705 end
0706
0707 return