Home > lattice > Converters > ELEGANT2AT > elegant2at.m

elegant2at

PURPOSE ^

function elegant2at(elegantlattice,E0,outfilename)

SYNOPSIS ^

function elegant2at(elegantlattice,E0,outfilename)

DESCRIPTION ^

function elegant2at(elegantlattice,E0,outfilename)
 tansform elegant %s.new file (save_lattice with output_seq=0) file into AT lattice structure.

 This procedure reads a saved elegant lattice and converts it to an AT lattice

 Elegant command to save the lattice sequence : 
 
  _______ filename.ele_________
  &save_lattice
   filename = %s.new,
   output_seq=0,
 &end
  ___________________________

  filename.new will contain the list of all the elements that make up the
  lattice in the correct format in a single file
 
 The routine outputs a Matlab macro with all the AT defitions and variables as
 in the elegant file  

 Works also with single elegant file not containing commands, only
 definitions.

 parameters:
    - elegantlattice = name of the elegant lattice file
    - E0  = design energy
    - outfilename (default: elegantlattice_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:

SOURCE CODE ^

0001 function elegant2at(elegantlattice,E0,outfilename)
0002 %function elegant2at(elegantlattice,E0,outfilename)
0003 % tansform elegant %s.new file (save_lattice with output_seq=0) file into AT lattice structure.
0004 %
0005 % This procedure reads a saved elegant lattice and converts it to an AT lattice
0006 %
0007 % Elegant command to save the lattice sequence :
0008 %
0009 %  _______ filename.ele_________
0010 %  &save_lattice
0011 %   filename = %s.new,
0012 %   output_seq=0,
0013 % &end
0014 %  ___________________________
0015 %
0016 %  filename.new will contain the list of all the elements that make up the
0017 %  lattice in the correct format in a single file
0018 %
0019 % The routine outputs a Matlab macro with all the AT defitions and variables as
0020 % in the elegant file
0021 %
0022 % Works also with single elegant file not containing commands, only
0023 % definitions.
0024 %
0025 % parameters:
0026 %    - elegantlattice = name of the elegant lattice file
0027 %    - E0  = design energy
0028 %    - outfilename (default: elegantlattice_AT_LATTICE.mat)
0029 %
0030 % default pass methods:
0031 %          quadrupoles : StrMPoleSymplectic4Pass
0032 %          dipole : BndMPoleSymplectic4Pass
0033 %          multipole : StrMPoleSymplectic4Pass
0034 %          sextupole : StrMPoleSymplectic4Pass
0035 %          thinmultipole : ThinMPolePass
0036 %          correctors : ThinMPolePass
0037 %          cavity : DriftPass
0038 %
0039 
0040 %% changes history
0041 % created 7-sep-2012 by S.M.Liuzzo @ ESRF as atfrommadx
0042 % modified 1-12-2015 by G. Campogiani @LNF as elegant2at
0043                   
0044 
0045 %% initial warnings
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 %% open madX sequence file
0058 
0059 % the : detect definitions. every : is preceded by a name
0060 % of an element.
0061 % between two : all the parameter of an elemetn or a line ar found.
0062 sX=fopen(elegantlattice,'r');
0063 
0064 %% get madX file in a cell array with a new elment at every space comma or new line.
0065 
0066 SXCELL=textscan(sX,'%s','delimiter','\n');
0067 % SXCELL=strrep(cellstr(SXCELL),'\n','&');
0068 
0069 SXSTRING=SXCELL{1}; % still a cell array but every space or new line now is stored as a new cell.
0070 
0071 % scroll and reshape to divide atributes
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},' ',''); % no spaces
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 %% open .m file to output matlab translated code
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']; %#ok<*NBRAK>
0116 lines=['\n\n%%%% LINES \n\n'];
0117 
0118 %% convert to a matlab macro
0119 j=1; % used in line block counter (element attributes counter)
0120 
0121 elemcount=0;
0122 i=1; 
0123 
0124 while i<length(SXSTRING)-2
0125     
0126     if SXSTRING{i}(end)==':' % new element or line
0127         def=[ def ...
0128             ]; % end line and go to newline add name
0129         SXSTRING{i}(1:end-1)=upper(SXSTRING{i}(1:end-1));
0130 
0131 %          not very clear what's the purpose of the following lines
0132 %         SXSTRING{i+j}=strrep(SXSTRING{i+j},'MARKER','MARKER,');
0133 %         SXSTRING{i+j}=strrep(SXSTRING{i+j},'MARKER,,','MARKER,');
0134 %         SXSTRING{i+j}=strrep(SXSTRING{i+j},'KICKER','KICKER,');
0135 %         SXSTRING{i+j}=strrep(SXSTRING{i+j},'KICKER,,','KICKER,');
0136 %         SXSTRING{i+j}=strrep(SXSTRING{i+j},'MONITOR','MONITOR,');
0137 %         SXSTRING{i+j}=strrep(SXSTRING{i+j},'MONITOR,,','MONITOR,');
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~=':' % loops atributes of this element definition
0157                     def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),SXSTRING{i+j},SXSTRING{i+j+1});
0158                     j=j+1; %go to new atribute
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~=':' % loops atributes of this element definition
0175                     def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),SXSTRING{i+j},SXSTRING{i+j+1});
0176                     
0177                     j=j+1; %go to new atribute
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~=':' % loops atributes of this element definition
0199                     def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),SXSTRING{i+j},SXSTRING{i+j+1});
0200                     
0201                     j=j+1; %go to new atribute
0202                     nwel=SXSTRING{i+j}(end);
0203                 end
0204                 % bendings are  sector by default. change to rectangular
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~=':' % loops atributes of this element definition
0228                     def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),...
0229                     SXSTRING{i+j},SXSTRING{i+j+1});
0230                     j=j+1; %go to new atribute
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~=':' % loops atributes of this element definition
0250                     def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),...
0251                     SXSTRING{i+j},SXSTRING{i+j+1});
0252                     
0253                     j=j+1; %go to new atribute
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~=':' % loops atributes of this element definition
0270                     def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),...
0271                     SXSTRING{i+j},SXSTRING{i+j+1});
0272                     
0273                     j=j+1; %go to new atribute
0274                     nwel=SXSTRING{i+j}(end);
0275                 end
0276             case {'MULT','MULTIPOLE','multipole'}
0277                 % multipoles should be StrMPoleSymplectic4Pass with short length
0278                 % to be compatible with MADX
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                 % MADX-->   ocf0: multipole,knl:={ 0, 0, 0,kocf0 };
0293                 while nwel~=':' % loops atributes of this element definition
0294                     
0295                 multipoles=[];
0296                     if strcmp(SXSTRING{i+j+1}(1),'{') % if open paerntesis found
0297                         multipoles=[multipoles '[' SXSTRING{i+j+1}(2:end)];
0298                         k=2;
0299                         while ~strcmp(SXSTRING{i+j+k}(end),'}') && k<10 % look for closed parentesis
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; %go to new atribute
0316                     nwel=SXSTRING{i+j}(end);
0317                     
0318                 end
0319                 def=[ def ...
0320                     SXSTRING{i}(1:end-1) '.(''Class'')=''Multipole''; \n'...% max order size polynomb -1
0321                     SXSTRING{i}(1:end-1) '.(''MaxOrder'')=numel(' ...
0322                     SXSTRING{i}(1:end-1) '.(''PolynomB'')' ')-1; \n'...% max order size polynomb -1
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~=':' % loops atributes of this element definition
0348                     def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),...
0349                     SXSTRING{i+j},SXSTRING{i+j+1});
0350                     
0351                     j=j+1; %go to new atribute
0352                     nwel=SXSTRING{i+j}(end);
0353                 end
0354                 
0355                 % fix madX-AT multipole coefficents
0356                 def=[ def ...
0357                     SXSTRING{i}(1:end-1) '.(''Class'')=''Octupole''; \n'...% max order size polynomb -1
0358                     SXSTRING{i}(1:end-1) '.(''MaxOrder'')=numel(' ...
0359                     SXSTRING{i}(1:end-1) '.(''PolynomB'')' ')-1; \n'...% max order size polynomb -1
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~=':' % loops atributes of this element definition
0384                     def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),...
0385                         SXSTRING{i+j},SXSTRING{i+j+1});
0386                     
0387                     j=j+1; %go to new atribute
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~=':' % loops atributes of this element definition
0403                     def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),...
0404                         SXSTRING{i+j},SXSTRING{i+j+1});
0405                     
0406                     j=j+1; %go to new atribute
0407                     nwel=SXSTRING{i+j}(end);
0408                 end
0409             case {'HMO','VMO','HMON','VMON'}
0410                 def=[def '\n'];
0411 %                      SXSTRING{i}(1:end-1) '.(''FamName'')=''' SXSTRING{i}(1:end-1) ''';\n' ...
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~=':' % loops atributes of this element definition
0427                     def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),...
0428                         SXSTRING{i+j},SXSTRING{i+j+1});
0429                     
0430                     j=j+1; %go to new atribute
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~=':' % loops atributes of this element definition
0443                     def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),...
0444                         SXSTRING{i+j},SXSTRING{i+j+1});
0445                     
0446                     j=j+1; %go to new atribute
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~=':' % loops atributes of this element definition
0470                     def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),...
0471                         SXSTRING{i+j},SXSTRING{i+j+1});
0472                     
0473                     j=j+1; %go to new atribute
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~=':' % loops atributes of this element definition
0484                     def=ParseAtributesELEGANT_2_AT(def,SXSTRING{i}(1:end-1),...
0485                         SXSTRING{i+j},SXSTRING{i+j+1});
0486                     
0487                     j=j+1; %go to new atribute
0488                     nwel=SXSTRING{i+j}(end);
0489                 end
0490                 
0491             case {'LINE','line'}
0492                 % next element is the length
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 %                 sposstring=[sposstring '];\n'];
0519 %                 sposstring(10)=[]; % remove extra comma
0520                 % call function that builds sequence from list of elements
0521                 % and total length of sequence
0522                 
0523             otherwise
0524                 disp(['Unknown element type: ' ElementType])
0525             end
0526     end % if new element
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 %% save close and exit
0537 
0538 macroconvertmadXAT=strrep([mst def lines],';;',';');
0539 fprintf(mafileout,macroconvertmadXAT);
0540 
0541 fclose('all');
0542 
0543 %% clean workspace
0544 clear macroconvertmadXAT def lines mst 
0545 clear SXSTRING i j SXCELL elemcount ElementType elname
0546 
0547 %% run macro file and save workspace.
0548 
0549 [~,elegantlattice] = fileparts(elegantlattice);
0550 
0551 try % try to run the macro generated
0552     %%%!!!!! THIS COMAND MAY FAIL! CHECK THE ORDER OF THE DECLARATIONS IN MADX!
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 %   delete(filemacroname);
0568     
0569 
0570 catch %#ok<CTCH>
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

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