Home > lattice > Converters > AT2MAD8 > AT_2_mad8.m

AT_2_mad8

PURPOSE ^

function [elelat,def,lines]=AT_2_mad8(AT_ring,linename)

SYNOPSIS ^

function [elelat,def,lines]=AT_2_mad8(AT_ring,linename)

DESCRIPTION ^

 function [elelat,def,lines]=AT_2_mad8(AT_ring,linename)
 this functions converts the AT lattice AT_ring in mad8 format.
 
 a MAD8 LINE is generated.
 
 file ['' linename '_lattice.mad8'] is generated contiaining the lattice
 (elelat) elements definitions (defs) and the LINE (lines). no other comands introduced

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [elelat,def,lines]=AT_2_mad8(AT_ring,linename)
0002 % function [elelat,def,lines]=AT_2_mad8(AT_ring,linename)
0003 % this functions converts the AT lattice AT_ring in mad8 format.
0004 %
0005 % a MAD8 LINE is generated.
0006 %
0007 % file ['' linename '_lattice.mad8'] is generated contiaining the lattice
0008 % (elelat) elements definitions (defs) and the LINE (lines). no other comands introduced
0009 %
0010 %
0011 
0012 outfile=['' linename '_lattice.mad8'];
0013 
0014 elelat=['!!\n!!  mad8 lattice: ' linename '\n!!  Created: ' datestr(now) '\n!!\n!!\n\n'...
0015     ...'assign,print="' linename 'sliced.print"\n'...
0016 ...'assign,echo="' linename 'sliced.echo"\n'...
0017 ...'option, -echo\n'...
0018 ];
0019 
0020 %% get family names for definitions
0021 [families,ind_first_oc_ring]=...
0022     unique(getcellstruct(AT_ring,'FamName',1:length(AT_ring)),'first');
0023 
0024 elelat=[elelat '! DEFINITIONS \n\n'];
0025 
0026 format='%8.8f';
0027 
0028 %% loop families for definitions
0029 for i=1:length(families)
0030    el= AT_ring{ind_first_oc_ring(i)};
0031    if isfield(el,'Class')
0032     switch el.('Class')
0033         case 'Bend' % dipole
0034             %Ang=el.('BendingAngle');
0035             %disp('all dipoles are considered rbend')
0036             di=[el.('FamName')   ': '...*sin(Ang/2)/Ang/2
0037                 ' SBEND,L=' num2str(el.('Length'),format) ', '...&\n
0038                 ' ANGLE = ' num2str(el.('BendingAngle'),format) ', &\n'...
0039                 ' K1 = ' num2str(el.('PolynomB')(2),format) ' '...&\n
0040                 ' E1= ' num2str(el.('EntranceAngle'),format) ', '...&\n
0041                 ' E2= ' num2str(el.('ExitAngle'),format) ' ;'...\n\n
0042                ' '];
0043            
0044             elelat=[elelat di '\n']; %#ok<*AGROW>
0045         case 'Quadrupole' % quadrupole
0046             
0047             if strfind(el.PassMethod,'Fringe') 
0048             qp=[el.('FamName')   ': '...&\n
0049                 ' QUADRUPOLE,  L= ' num2str(el.('Length'),format)  ', '...&\n
0050                 ' K1= ' num2str(el.('PolynomB')(2),format) ',FRGF=.TRUE.;'...\n
0051                 ];
0052             else
0053             qp=[el.('FamName')   ': '...&\n
0054                 ' QUADRUPOLE,  L= ' num2str(el.('Length'),format)  ', '...&\n
0055                 ' K1= ' num2str(el.('PolynomB')(2),format) ';'...\n
0056                 ];
0057             end
0058             
0059             elelat=[elelat qp '\n'];
0060         case 'Sextupole' % sextupole
0061             sx=[el.('FamName')   ': '...&\n
0062                 ' Sextupole,  L= ' num2str(el.('Length'),format)  ','... &\n
0063                 ' K2= ' num2str(el.('PolynomB')(3)*2,format) ' ;'...\n
0064                 ];
0065             elelat=[elelat sx '\n'];
0066         case 'Multipole' % bpm
0067             % el.('PolynomB')
0068             ord=find(el.('PolynomB'));
0069             if isempty(ord)
0070                 ord=length(el.('PolynomB'));
0071             end
0072                 mp=[el.('FamName')   ': '...&\n
0073                     ' Multipole, '.... L= ' num2str(el.('Length'),format)  ', &\n'...
0074                     ...'ORDER= ' num2str(ord(1)-1) ', &\n' ... 
0075                    'K' num2str(ord(1)-1) 'L= ' num2str(el.('PolynomB')(ord(1))*factorial(ord(1)-1),format) '; \n'...
0076  %                   'KNL= ' num2str(factorial(ord(1)-1)*el.('PolynomB')(ord(1)),format) ', \n'...
0077                 %' N_KICKS= ' num2str(ceil(el.('Length')*nkickperL+1),'%d') ' \n'...
0078                 ];
0079             elelat=[elelat mp '\n'];
0080         case {'ThinMultipole'} % bpm
0081             formatm='%10.5e';
0082             % el.('PolynomB')
0083             ord=find(el.('PolynomB'));
0084             if isempty(ord)
0085                 ord=length(el.('PolynomB'));
0086             end
0087                 mp=[el.('FamName')   '_n: '...&\n
0088                     ' Multipole, '.... L= ' num2str(el.('Length'),format)  ', &\n'...
0089                     ...'ORDER= ' num2str(ord(1)-1) ', &\n' ... 
0090                    ];
0091                
0092                if ord>9, ord=9; end; % mad8 limit
0093                
0094                for imult=1:ord
0095                    mp=[mp 'K' num2str(imult-1) 'L= ' num2str(el.('PolynomB')(imult)*el.('Length')*factorial(imult-1),format) ', '...
0096                    ];
0097                end
0098                
0099                mp=[mp(1:end-2) '; \n'];
0100                
0101                % skew
0102                mpa=[el.('FamName')   '_s: '...&\n
0103                     ' Multipole, '.... L= ' num2str(el.('Length'),format)  ', &\n'...
0104                     ...'ORDER= ' num2str(ord(1)-1) ', &\n' ... 
0105                    ];
0106                
0107                if ord>9, ord=9; end; % mad8 limit
0108                
0109                for imult=1:ord
0110                    mpa=[mpa 'K' num2str(imult-1) 'L= '...
0111                        num2str(el.('PolynomB')(imult)*el.('Length')*factorial(imult-1),format) ', '...
0112                        'T' num2str(imult-1) '= 3.142128/' num2str(2*imult+2) ', '...
0113                    ];
0114                end
0115                
0116                mpa=[mpa(1:end-2) '; \n'];
0117 
0118                % multipole skew and normal to single multipole element
0119                m=[el.('FamName')   ': LINE(' el.('FamName') '_n,' el.('FamName') '_s' ');\n'];
0120                
0121             elelat=[elelat mp mpa m '\n'];
0122         case 'Octupole' % bpm
0123             % el.('PolynomB')
0124             ord=find(el.('PolynomB'));
0125             if isempty(ord)
0126                 ord=length(el.('PolynomB'));
0127             end
0128                 mp=[el.('FamName')   ': '...&\n
0129                     ' Octupole, L= ' num2str(el.('Length'),format)  ', '...&\n
0130                     ...'ORDER= ' num2str(ord(1)-1) ', &\n' ... 
0131                    'K' num2str(ord(1)-1) '= ' num2str(el.('PolynomB')(ord(1))*factorial(ord(1)-1),format) '; \n'...
0132  %                   'KNL= ' num2str(factorial(ord(1)-1)*el.('PolynomB')(ord(1)),format) ', \n'...
0133                 %' N_KICKS= ' num2str(ceil(el.('Length')*nkickperL+1),'%d') ' \n'...
0134                 ];
0135             elelat=[elelat mp '\n'];
0136         case 'Monitor' % bpm
0137             pu=[el.('FamName') ' :MONITOR' '; '...\n
0138                 ];
0139             elelat=[elelat pu '\n'];
0140         case {'Marker','SkewQuadrupole'}% marker
0141             mrk=[el.('FamName') ' :MARKER' '; '...\n
0142                 ];
0143             elelat=[elelat mrk '\n'];
0144         
0145        case 'Corrector' % kicker
0146             ki=[el.('FamName') ' :KICKER' '; '...\n
0147                 ];
0148             
0149             elelat=[elelat ki '\n'];
0150         case 'Drift' % drift
0151             dr=[el.('FamName') ' : DRIFT, L= ' num2str(el.('Length'),format) '; '];%\n
0152             elelat=[elelat dr '\n'];     
0153          case 'RFCavity' % drift
0154             rfc=['RF_ON:=1; \n'...
0155                 ...
0156                 el.('FamName') ' : RFCavity, L=' num2str(el.('Length'),format)...
0157                 ',VOLT=' num2str(el.Voltage/1e6,format) ' &\n'... %MV
0158                 ', harm=' num2str(el.HarmNumber,'%3.3d') ', lag=0.0' '; \n'];
0159       
0160             elelat=[elelat rfc '\n'];
0161            
0162         otherwise
0163             warning(['Element: ' el.('FamName') ' was not converted, since it does not match any Class.'])
0164     end
0165    end
0166 end
0167 
0168 def=elelat;
0169 
0170 elelat=[];
0171 
0172 elelat=[elelat '! LINE \n\n'];
0173 
0174 elelat=[elelat linename ' : LINE = ('];
0175 
0176 %% define lattice line
0177 % loop all elements
0178 for i=1:length(AT_ring)
0179     if i==1
0180         elelat=[elelat '' AT_ring{i}.('FamName') ' &\n'];
0181     else
0182         if mod(i,5)==0
0183         elelat=[elelat ',' AT_ring{i}.('FamName') ' &\n']; 
0184         else
0185         elelat=[elelat ',' AT_ring{i}.('FamName') ' ']; 
0186         end
0187     end
0188 end
0189 
0190 elelat=[elelat ') \n'];
0191 
0192 %elelat=[elelat ') \n use ' linename '\n \n twiss, chrom, save \n'];
0193 
0194 lines=elelat;
0195 
0196 elelat=[def lines];
0197 
0198 %% print to file
0199 
0200 of=fopen(outfile,'w');
0201 fprintf(of,elelat);
0202 
0203 fclose('all');
0204 
0205 
0206 
0207 
0208 return

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