Home > lattice > Converters > MAD82MADX > mad8TOmadx.m

mad8TOmadx

PURPOSE ^

converts mad8 sequence files to madX

SYNOPSIS ^

function [seqfilemadX]=mad8TOmadx(seqfilemad8,periodname)

DESCRIPTION ^

 converts mad8 sequence files to madX

function [seqfileMADX]=mad8TOmadx(seqfilemad8)

 This procedure reads a saved sequence in
 mad8 (SAVE,FILE='seqfilemad8';)
 and converts it to madx sequence
 every = goes to :=
 the order of the declarations is the same in the two files.

 works also with single mad8 files not containing comands, only
 definitions.
 does not translate call to files since those may change name
 
 parameters:
    - seqdilemad8=name of the mad8 lattice file
    - periodname (optional)= name of the period to use in madx (default is the filename)
 
 Simone Maria Liuzzo PhD@LNF 25-11-2011
     update 29-2-2012 : corrected a bug that would not translate correctly
     markers, kickers and monitor declared only by element name ("BPM: monitor" would not convet properly)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [seqfilemadX]=mad8TOmadx(seqfilemad8,periodname)
0002 % converts mad8 sequence files to madX
0003 %
0004 %function [seqfileMADX]=mad8TOmadx(seqfilemad8)
0005 %
0006 % This procedure reads a saved sequence in
0007 % mad8 (SAVE,FILE='seqfilemad8';)
0008 % and converts it to madx sequence
0009 % every = goes to :=
0010 % the order of the declarations is the same in the two files.
0011 %
0012 % works also with single mad8 files not containing comands, only
0013 % definitions.
0014 % does not translate call to files since those may change name
0015 %
0016 % parameters:
0017 %    - seqdilemad8=name of the mad8 lattice file
0018 %    - periodname (optional)= name of the period to use in madx (default is the filename)
0019 %
0020 % Simone Maria Liuzzo PhD@LNF 25-11-2011
0021 %     update 29-2-2012 : corrected a bug that would not translate correctly
0022 %     markers, kickers and monitor declared only by element name ("BPM: monitor" would not convet properly)
0023 
0024 %% open mad8 sequence file and madx sequence output file.
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=[]; % string to input to madx file
0029 
0030 % i : segnano tutte le definizioni. ogni : ??? preceduto dal nome
0031 % dell'oggetto e seguito dal tipo di oggetto.
0032 % tra un : e il successivo trovo tutti i parametri di un oggetto e il nome
0033 % dell'oggetto successivo, che sia definizione o linea.
0034 
0035 
0036 S8CELL=textscan(s8,'%s');
0037 S8STRING=S8CELL{1}; % still a cell array but every space or new line now is stored as a new cell.
0038 
0039 
0040 %save('1');
0041 
0042 %return
0043 %% convert to madx
0044 j=1; % used in line block counter
0045 sxs=['! MADX converted sequence from ' seqfilemad8 ' '];
0046 corrcount=1;
0047 i=12; % skip header
0048 S8STRING{12}
0049 while i<length(S8STRING)-2
0050     
0051     if S8STRING{i}(end)==':' % new element or line
0052         sxs=[sxs ';\n ' S8STRING{i}]; %#ok<*AGROW> % end line and go to newline add name
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         %  sxs=[sxs ' ' S8STRING{i+1}];
0063         %j=2;
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                 % build in monitor, and corrector
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                    % sxs=[sxs ' ' Q_CM];
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                    % sxs=[sxs ' ' S_CM];
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'  %%%%% CHANGE TO GOOD 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                     %        sxs=[sxs ' ' S8STRING{i+j}];
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'  %%%%% CHANGE TO GOOD MULTIPOLE!
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'  %%%%% CHANGE TO GOOD MULTIPOLE!
0179                 while nwel~=':'
0180                     sxs=[sxs ' ' S8STRING{i+j}];
0181                     j=j+1;
0182                     nwel=S8STRING{i+j}(end);
0183                 end
0184             case 'KICKER'  %%%%% CHANGE TO GOOD MULTIPOLE!
0185                 while nwel~=':'
0186                     sxs=[sxs ' ' S8STRING{i+j}];
0187                     j=j+1;
0188                     nwel=S8STRING{i+j}(end);
0189                 end
0190             case 'CONSTAN'  %%%%% CHANGE TO GOOD MULTIPOLE!
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                 %%% unrecognized LINE comand
0195                 LineMAD=[];
0196                 if strcmp(S8STRING{i+j}(1:5),'LINE=')
0197                     nwel2=S8STRING{i+j+1}(end);
0198                     while nwel~=':' && nwel2~='=' %second case is the value definition
0199                         % clean from &
0200                         if S8STRING{i+j}(end)=='&';
0201                             S8STRING{i+j}=S8STRING{i+j}(1:end-1);
0202                         end
0203                         
0204                         % apend to madx file
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                     % transform ,n*QDF, in ,n*(QDF),
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); % get first comma after multiplication
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; % length is encreased by 2 units at every loop
0224                     end
0225                     sxs=[sxs ' ' LineMAD ';\n'];
0226                     
0227                     if  nwel=='='
0228                         j=j-1;
0229                         %                    S8STRING{i}
0230                         %                    j
0231                         %                    S8STRING{i+j}
0232                     end
0233                     
0234                 else
0235                     disp(['Unknown element type: ' ElementType])
0236                 end
0237                 
0238                 
0239         end
0240         
0241         
0242         
0243     else % variable declaration??
0244         
0245         % copy 3 blocks on top of all variables
0246         sxs=[S8STRING{i} S8STRING{i+1} S8STRING{i+2} ';\n ' sxs];
0247         j=3;
0248     end % if new element
0249     
0250     i=i+j;
0251     j=1;
0252     
0253 end
0254 
0255 disp(corrcount)
0256 
0257 %% run madx sequence file to check tunes, length betas and dispersion.
0258 
0259 %% out madX sequence file
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) % only 1 argument provided
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) % provide also name of period to use in test
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 ...     '\n plot, haxis=s, vaxis=Wx,Wy, interpolate,style=1,colour=100;'...
0287 ...     '\n plot, haxis=s, vaxis=dx,dy, interpolate,style=1,colour=100;'...
0288 ...     '\n plot, haxis=s, vaxis=x,y, interpolate,style=1,colour=100;'...
0289 ...     '\n plot, range=#s/SF1L[2], haxis=s, vaxis1=betx,bety, vaxis2=dx,dy, interpolate, style=1,colour=100;'...
0290 ...     '\n plot, range=SF1L[4]/SF1L[8], haxis=s, vaxis1=betx,bety, vaxis2=dx,dy, interpolate, style=1,colour=100;'...
0291 ...     '\n plot, range=SF1L[8]/SF1L[12], haxis=s, vaxis1=betx,bety, vaxis2=dx,dy, interpolate, style=1,colour=100;'...
0292 ...     '\n plot, range=SF1L[12]/SF1[2], haxis=s, vaxis1=betx,bety, vaxis2=dx,dy, interpolate, style=1,colour=100;'...
0293 ...     '\n plot, range=SF1[2]/SF1[8], haxis=s, vaxis1=betx,bety, vaxis2=dx,dy,  interpolate,style=1,colour=100;'...
0294 ...     '\n plot, range=SF1[8]/SF1[12], haxis=s, vaxis1=betx,bety, vaxis2=dx,dy,  interpolate,style=1,colour=100;'...
0295 ...     '\n plot, range=SF1[12]/#e, haxis=s, vaxis1=betx,bety, vaxis2=dx,dy, interpolate, style=1,colour=100;'...
0296 ...     '\n plot, range=OC1[1]/CRABL[2], haxis=s, vaxis1=betx,bety, vaxis2=dx,dy, interpolate, style=1,colour=100;'...
0297 ...     '\n  plot, range=CRABL[2]/IP[1], haxis=s, vaxis1=betx,bety, vaxis2=dx,dy, interpolate, style=1,colour=100;\n'...
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'...,hmin=-250,hmax=250,vmin=-450,vmax=50;\n'...
0304     'save, sequence=' sqname ', file=' seqfilemadX(~ismember(seqfilemadX,'.')) '.seq; ! savesequence \n '...
0305     ...'use,period=ring_noff;\nsave, sequence=RING_noff, file=' seqfilemadX(~ismember(seqfilemadX,'.')) 'noff.seq,bare; ! savesequence \n '...
0306     ... '    savebeta,label=HER,place=#e;\n'...
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,'&',''); % remove &
0320 sxs=strrep(sxs,';\n;',';\n\n'); % remove useless ;
0321 sxs=strrep(sxs,'K2=','K2=SXT_ON*');  % add sextupole on switch
0322 sxs=strrep(sxs,'=',':='); % add :=
0323 sxs=strrep(sxs,'::=',':=');
0324 sxs=strrep(sxs,']',''); % convert [] to ->
0325 sxs=strrep(sxs,'[','->');
0326 sxs=strrep(sxs,'LINE:=','LINE=');  % restore Line definitions to
0327 sxs=strrep(sxs,', ',',');
0328 
0329 
0330 %% save close and exit
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

Generated on Thu 24-Aug-2017 18:47:33 by m2html © 2005