Home > lattice > Converters > MADX2AT > atfrommadx.m

atfrommadx

PURPOSE ^

function atfrommadx(seqfilemadX,E0,outfilename)

SYNOPSIS ^

function atfrommadx(seqfilemadX,E0,outfilename)

DESCRIPTION ^

function atfrommadx(seqfilemadX,E0,outfilename)
 tansform madX sequence file (savesequence) file into AT lattice structure.

 This procedure reads a saved lattice (sequence in madx) in madX
 and converts it to an AT lattice

 (madx comands to save the sequences :

  _______ MADX code _________
  use,period=sequencename1;
  use,period=sequencename2;
  use,period=sequencename2;
  SAVE,FILE='seqfilemadX.seq';
  ___________________________

  seqfilemadX.seq will contain sequencename1 sequencename2 sequencename3
  in the correct format in a single file

 )

 The routine outputs a Matlab macro with all the AT defitions and variables as
 in the madX file

 The order of the declarations is the same in the two files.
 declarations that contain other variables are moved to the end. (this may not be enough)


 Works also with single madX files not containing comands, only
 definitions.

 parameters:
    - seqfilemadX=name of the mad8 lattice file
    - E0  = design energy
    - outfilename (default: seqfilemadX_AT_LATTICE.mat)

 default pass methods:
          quadrupoles : StrMPoleSymplectic4Pass
          dipole : BndMPoleSymplectic4Pass
          multipole : StrMPoleSymplectic4Pass
          sextupole : StrMPoleSymplectic4Pass
          thinmultipole : ThinMPolePass
          correctors : ThinMPolePass
          cavity : DriftPass

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function atfrommadx(seqfilemadX,E0,outfilename)
0002 %function atfrommadx(seqfilemadX,E0,outfilename)
0003 % tansform madX sequence file (savesequence) file into AT lattice structure.
0004 %
0005 % This procedure reads a saved lattice (sequence in madx) in madX
0006 % and converts it to an AT lattice
0007 %
0008 % (madx comands to save the sequences :
0009 %
0010 %  _______ MADX code _________
0011 %  use,period=sequencename1;
0012 %  use,period=sequencename2;
0013 %  use,period=sequencename2;
0014 %  SAVE,FILE='seqfilemadX.seq';
0015 %  ___________________________
0016 %
0017 %  seqfilemadX.seq will contain sequencename1 sequencename2 sequencename3
0018 %  in the correct format in a single file
0019 %
0020 % )
0021 %
0022 % The routine outputs a Matlab macro with all the AT defitions and variables as
0023 % in the madX file
0024 %
0025 % The order of the declarations is the same in the two files.
0026 % declarations that contain other variables are moved to the end. (this may not be enough)
0027 %
0028 %
0029 % Works also with single madX files not containing comands, only
0030 % definitions.
0031 %
0032 % parameters:
0033 %    - seqfilemadX=name of the mad8 lattice file
0034 %    - E0  = design energy
0035 %    - outfilename (default: seqfilemadX_AT_LATTICE.mat)
0036 %
0037 % default pass methods:
0038 %          quadrupoles : StrMPoleSymplectic4Pass
0039 %          dipole : BndMPoleSymplectic4Pass
0040 %          multipole : StrMPoleSymplectic4Pass
0041 %          sextupole : StrMPoleSymplectic4Pass
0042 %          thinmultipole : ThinMPolePass
0043 %          correctors : ThinMPolePass
0044 %          cavity : DriftPass
0045 %
0046 
0047 %% changes history
0048 % created 7-sep-2012 by S.M.Liuzzo @ ESRF
0049 %
0050 % updated 12-sep-2012 Cavity DriftPass (not IdentityPass)
0051 % updated 13-sep-2012 Capital letter names
0052 %                     CorrectorPass
0053 %                     _red reduced versions
0054 % updated 14-sep-2012 multipoles (1/n!) factor from madx to AT
0055 % updated 21-dec-2012 rbend-sbend length conversion corrected
0056 % updated 05-mar-2013 lattice output no fringes and with fringes (_FF).
0057 %
0058 % updated 20-mar-2013 removed output no fringes and with fringes (_FF).
0059 %                     removed output reduced (_red).
0060 %                     use atconstructors.
0061 %                     call macro directly
0062 %                     try-catch macro running
0063 %                     tempname file and fulfile.
0064 
0065 
0066 %% initial warnings
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 %% open madX sequence file
0082 sX=fopen(seqfilemadX,'r');
0083 
0084 % the : detect definitions. every : is preceded by a name
0085 % of an element.
0086 % between two : all the parameter of an elemetn or a line ar found.
0087 
0088 %% get madX file in a cell array with a new elment at every space comma or new line.
0089 %SXCELL=textscan(sX,'%s','delimiter',';');
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}]; % catenate until ; is found
0106         
0107     else
0108         
0109         B{iib}=[B{iib} A{iia}(1:end-1)]; % remove ;
0110         
0111         iib=iib+1;
0112         B{iib}=[];
0113     end
0114     
0115     iia=iia+1;
0116     
0117 end
0118 
0119 SXSTRING=B'; % still a cell array but every space or new line now is stored as a new cell.
0120 
0121 % scroll and reshape to divide atributes
0122 tmp={};
0123 
0124 % for i=1:length(SXSTRING)
0125 %
0126 %     waitbar(i/length(SXSTRING));
0127 %
0128 %     SXSTRING{i}=strrep(SXSTRING{i},' ',''); % no spaces
0129 %     SXSTRING{i}=strrep(SXSTRING{i},':=','=');
0130 %     SXSTRING{i}=strrep(SXSTRING{i},';','');
0131 %
0132 %     c=[1 sort([strfind(SXSTRING{i},',') strfind(SXSTRING{i},':')...
0133 %                      strfind(SXSTRING{i},'=')]) length(SXSTRING{i})];
0134 %
0135 %     % split sub strings
0136 %     for jc=1:length(c)-1
0137 %         if jc==1
0138 %             tmp=[tmp SXSTRING{i}(c(jc):c(jc+1))];
0139 %         else
0140 %             tmp=[tmp SXSTRING{i}(c(jc)+1:c(jc+1))];
0141 %         end
0142 %     end
0143 %
0144 % end
0145 % SXSTRING=tmp;
0146 
0147 
0148 tmp=cellfun(@(a)formatTextMADX(a),SXSTRING,'un',0);
0149 
0150 SXSTRING=[tmp{:}];
0151 
0152 
0153 %% open .m file to output matlab translated code
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']; %#ok<*NBRAK>
0163 lines=['\n\n%%%% LINES \n\n'];
0164 var=['%%%% VARIABLES \n\n sxt_on=1;'];
0165 formulas=['\n\n%%%% RELATIONS \n\n'];
0166 
0167 %% convert to a matlab macro
0168 j=1; % used in line block counter (element atributes counter)
0169 h=waitbar(0,'Converting: ');
0170 
0171 elemcount=0;
0172 i=1; % skip header in mad8 file   (element counter)
0173 while i<length(SXSTRING)-2
0174     
0175     
0176     if SXSTRING{i}(end)==':' % new element or line
0177         def=[ def ...
0178             ]; %#ok<*AGROW> % end line and go to newline add name
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         % display status of conversion
0192         waitbar(i/(length(SXSTRING)-2),h,['Converting: ' ElementType]);
0193         
0194         %  THERING=[THERING ' ' SXSTRING{i+1}];
0195         %j=2;
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~=':' % loops atributes of this element definition
0211                     def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),SXSTRING{i+j},SXSTRING{i+j+1});
0212                     
0213                     j=j+1; %go to new atribute
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~=':' % loops atributes of this element definition
0230                     def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),SXSTRING{i+j},SXSTRING{i+j+1});
0231                     
0232                     j=j+1; %go to new atribute
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~=':' % loops atributes of this element definition
0254                     def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),SXSTRING{i+j},SXSTRING{i+j+1});
0255                     
0256                     j=j+1; %go to new atribute
0257                     nwel=SXSTRING{i+j}(end);
0258                 end
0259                 % bendings are  sector by default. change to rectangular
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~=':' % loops atributes of this element definition
0282                     def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),...
0283                         SXSTRING{i+j},SXSTRING{i+j+1});
0284                     
0285                     j=j+1; %go to new atribute
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~=':' % loops atributes of this element definition
0305                     def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),...
0306                         SXSTRING{i+j},SXSTRING{i+j+1});
0307                     
0308                     j=j+1; %go to new atribute
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~=':' % loops atributes of this element definition
0325                     def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),...
0326                         SXSTRING{i+j},SXSTRING{i+j+1});
0327                     
0328                     j=j+1; %go to new atribute
0329                     nwel=SXSTRING{i+j}(end);
0330                 end
0331             case {'MULTIPOLE','multipole'}
0332                 % multipoles should be StrMPoleSymplectic4Pass with short length
0333                 % to be compatible with MADX
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                 % MADX-->   ocf0: multipole,knl:={ 0, 0, 0,kocf0 };
0348                 while nwel~=':' % loops atributes of this element definition
0349                     
0350                     
0351                     multipoles=[];
0352                     if strcmp(SXSTRING{i+j+1}(1),'{') % if open paerntesis found
0353                         multipoles=[multipoles '[' SXSTRING{i+j+1}(2:end)];
0354                         k=2;
0355                         while ~strcmp(SXSTRING{i+j+k}(end),'}') && k<10 % look for closed parentesis
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; %go to new atribute
0372                     nwel=SXSTRING{i+j}(end);
0373                     
0374                 end
0375                 def=[ def ...
0376                     SXSTRING{i}(1:end-1) '.(''Class'')=''Multipole''; \n'...% max order size polynomb -1
0377                     SXSTRING{i}(1:end-1) '.(''MaxOrder'')=numel(' ...
0378                     SXSTRING{i}(1:end-1) '.(''PolynomB'')' ')-1; \n'...% max order size polynomb -1
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~=':' % loops atributes of this element definition
0404                     def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),...
0405                         SXSTRING{i+j},SXSTRING{i+j+1});
0406                     
0407                     j=j+1; %go to new atribute
0408                     nwel=SXSTRING{i+j}(end);
0409                 end
0410                 
0411                 % fix madX-AT multipole coefficents
0412                 def=[ def ...
0413                     SXSTRING{i}(1:end-1) '.(''Class'')=''Octupole''; \n'...% max order size polynomb -1
0414                     SXSTRING{i}(1:end-1) '.(''MaxOrder'')=numel(' ...
0415                     SXSTRING{i}(1:end-1) '.(''PolynomB'')' ')-1; \n'...% max order size polynomb -1
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~=':' % loops atributes of this element definition
0440                     def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),...
0441                         SXSTRING{i+j},SXSTRING{i+j+1});
0442                     
0443                     j=j+1; %go to new atribute
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~=':' % loops atributes of this element definition
0459                     def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),...
0460                         SXSTRING{i+j},SXSTRING{i+j+1});
0461                     
0462                     j=j+1; %go to new atribute
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~=':' % loops atributes of this element definition
0480                     def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),...
0481                         SXSTRING{i+j},SXSTRING{i+j+1});
0482                     
0483                     j=j+1; %go to new atribute
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~=':' % loops atributes of this element definition
0499                     def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),...
0500                         SXSTRING{i+j},SXSTRING{i+j+1});
0501                     
0502                     j=j+1; %go to new atribute
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~=':' % loops atributes of this element definition
0522                     def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),...
0523                         SXSTRING{i+j},SXSTRING{i+j+1});
0524                     
0525                     j=j+1; %go to new atribute
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~=':' % loops atributes of this element definition
0547                     def=ParseAtributesMADX_2_AT(def,SXSTRING{i}(1:end-1),...
0548                         SXSTRING{i+j},SXSTRING{i+j+1});
0549                     
0550                     j=j+1; %go to new atribute
0551                     nwel=SXSTRING{i+j}(end);
0552                 end
0553             case {'CONSTAN','constant'}
0554                 % disp('constant')
0555                 
0556                 var=[var SXSTRING{i}(1:end-1) '=' SXSTRING{i+3} '; \n '];
0557                 
0558                 j=j+3;
0559             case {'sequence'}
0560                 
0561                 % next element is the length
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                 %zero=0;
0573                 %driftcounter=1;
0574                 while strcmp(at,'at=') && ~strcmp(elname,'endsequence')
0575                     
0576                     elname=SXSTRING{i+j};
0577                     % WARNING if fails here add RETURN after madx
0578                     % ...
0579                     % endsequence;
0580                     % RETURN
0581                     % -----eof------
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)=[]; % remove extra comma
0599                 % call function that builds sequence from list of elements
0600                 % and total length of sequence
0601                 lines=[lines sposstring '\n %% BUILD LATTICE \n'...
0602                     seqname '=buildATLattice(' seqname ',spos,' l ');\n'...
0603                     ... seqname '_red=atreduce(' seqname ');\n'...
0604                     ... seqname '_FF=FringeSwitch(' seqname ',1);\n'...
0605                     ];
0606                 
0607             otherwise
0608                 disp(['Unknown element type: ' ElementType])
0609         end
0610         
0611         
0612         
0613     else % variable declaration??
0614         if SXSTRING{i}(1)~='!';
0615             
0616             % in mad8 declaring a variable before using it is not compulsary.
0617             % check that all definitions are at the begining.
0618             %if sum(ismember('ABCDFGHILMNOPQRSTUVZWYK',SXSTRING{i+2}))>0
0619             if sum(ismember('()/*+-',SXSTRING{i+1}))>0
0620                 % if letters (but E for exponential) then it is a formula
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 % if new element
0628     
0629     i=i+j;
0630     j=1;
0631     
0632 end
0633 
0634 
0635 
0636 %% save close and exit
0637 
0638 macroconvertmadXAT=strrep([mst var formulas def lines],';;',';');
0639 fprintf(mafileout,macroconvertmadXAT);
0640 
0641 fclose('all');
0642 
0643 %% clean workspace
0644 clear macroconvertmadXAT formulas def lines var mst
0645 clear SXSTRING i j SXCELL elemcount ElementType elname
0646 
0647 %% run macro file and save workspace.
0648 
0649 [~,seqfilemadX]=fileparts(seqfilemadX);
0650 
0651 try % try to run the macro generated
0652     %%%!!!!! THIS COMAND MAY FAIL! CHECK THE ORDER OF THE DECLARATIONS IN MADX!
0653     run(filemacroname)
0654     
0655     if nargin<3
0656         save([seqfilemadX '_AT_LATTICE']);
0657     else
0658         save(outfilename);
0659     end
0660     
0661     %delete(filemacroname);
0662     fileoutname=[seqfilemadX '_AT_macro.m'];
0663     
0664     movefile(filemacroname,fileoutname);
0665     
0666     
0667 catch %#ok<CTCH>
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,' ',''); % no spaces
0692 str=strrep(str,':=','=');
0693 str=strrep(str,';','');
0694 
0695 c=[1 sort([strfind(str,',') strfind(str,':')...
0696     strfind(str,'=')]) length(str)];
0697 
0698 % split in substrings
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

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