Home > lattice > at2py.m

at2py

PURPOSE ^

ELSTR=AT2PY(ELEM) convert AT element tp pyat

SYNOPSIS ^

function elstr=at2py(elem)

DESCRIPTION ^

ELSTR=AT2PY(ELEM) convert AT element tp pyat

AT2PY Creates a pyat element

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function elstr=at2py(elem)
0002 %ELSTR=AT2PY(ELEM) convert AT element tp pyat
0003 %
0004 %AT2PY Creates a pyat element
0005 
0006 atclass=atguessclass(elem, 'UseClass');
0007 
0008 switch atclass
0009     case 'Drift'
0010         create=@atdrift;
0011         pycreate=@py.at.elements.Drift;
0012         [args,options]=doptions(elem,create,{'Length'});
0013     case 'Quadrupole'
0014         create=@atquadrupole;
0015         pycreate=@py.at.elements.Quadrupole;
0016         args={elem.Length,elem.PolynomB(2)};
0017         [args,options]=doptions(elem,create,{'Length','K'},{'PolynomB','PolynomA'},args);
0018         if ~any(abs(getfield2(elem,'PolynomA',0))>0)
0019             options=rmfield2(options,{'PolynomA'});
0020         end
0021         anom=abs(getfield2(elem,'PolynomB',0))~=0;
0022         anom(2)=false;
0023         if ~any(anom)
0024             options=rmfield2(options,{'PolynomB'});
0025         end
0026     case 'Sextupole'
0027         create=@atsextupole;
0028         pycreate=@py.at.elements.Sextupole;
0029         args={elem.Length,elem.PolynomB(3)};
0030         [args,options]=doptions(elem,create,{'Length'},{'PolynomB','PolynomA'},args);
0031         if ~any(abs(getfield2(elem,'PolynomA',0))>0)
0032             options=rmfield2(options,{'PolynomA'});
0033         end
0034         anom=abs(getfield2(elem,'PolynomB',0))~=0;
0035         anom(3)=false;
0036         if ~any(anom)
0037             options=rmfield2(options,{'PolynomB'});
0038         end
0039     case 'Bend'
0040         create=@atsbend;
0041         pycreate=@py.at.elements.Dipole;
0042         args={elem.Length,elem.BendingAngle,elem.PolynomB(2)};
0043         [args,options]=doptions(elem,create,{'Length','BendingAngle','K'},...
0044             {'EntranceAngle','ExitAngle','PolynomB','PolynomA'},args);
0045         angle1=getfield2(elem,'EntranceAngle',0);
0046         angle2=getfield2(elem,'ExitAngle',0);
0047         if angle1 == 0
0048             options=rmfield2(options,{'EntranceAngle'});
0049         end
0050         if angle2 == 0
0051             options=rmfield2(options,{'ExitAngle'});
0052         end
0053         if ~any(abs(getfield2(elem,'PolynomA',0))>0)
0054             options=rmfield2(options,{'PolynomA'});
0055         end
0056         anom=abs(getfield2(elem,'PolynomB',0))~=0;
0057         anom(2)=false;
0058         if ~any(anom)
0059             options=rmfield2(options,{'PolynomB'});
0060         end
0061     case 'Corrector'
0062         create=@atcorrector;
0063         pycreate=@py.at.elements.Drift;
0064         [args,options]=doptions(elem,create,{'Length','KickAngle'});
0065     case 'Multipole'
0066         create=@atmultipole;
0067         pycreate=@py.at.elements.Multipole;
0068         [args,options]=doptions(elem,create,{'Length','PolynomA','PolynomB'});
0069     case 'ThinMultipole'
0070         create=@atthinmultipole;
0071         pycreate=@py.at.elements.ThinMultipole;
0072         [args,options]=doptions(elem,create,{'PolynomA','PolynomB'});
0073     case 'RFCavity'
0074         create=@atrfcavity;
0075         pycreate=@py.at.elements.RFCavity;
0076         [args,options]=doptions(elem,create,{'Length','Voltage','Frequency','HarmNumber','Energy'});
0077     case 'RingParam'
0078         create=@atringparam;
0079         pycreate=@py.at.elements.RingParam;
0080         [args,options]=doptions(elem,create,{'Energy','Periodicity'});
0081     case 'Aperture'
0082         create=@ataperture;
0083         pycreate=@py.at.elements.Aperture;
0084         [args,options]=doptions(elem,create,{'Limits'});
0085     case 'QuantDiff'
0086         create=@atQuantDiff;
0087         pycreate=@py.at.elements.Marker;
0088         [args,options]=doptions(elem,create,{'Lmatp'});
0089     case 'Matrix66'
0090         create=@atM66;
0091         pycreate=@py.at.elements.Marker;
0092         [args,options]=doptions(elem,create,{'M66'});
0093     case 'MatrixTijkPass'
0094         create=@atM66Tijk;
0095         pycreate=@py.at.elements.Marker;
0096         [args,options]=doptions(elem,create,{'M66','Tijk'});
0097     case 'Monitor'
0098         create=@atmonitor;
0099         pycreate=@py.at.elements.Monitor;
0100         [args,options]=doptions(elem,create);
0101     otherwise %'Marker'
0102         create=@atmarker;
0103         pycreate=@py.at.elements.Marker;
0104         [args,options]=doptions(elem,create);
0105         if isfield(options,'Energy')
0106             options=rmfield(options,'Energy');
0107         end
0108 end
0109 % varg=[args,reshape(cat(2,fieldnames(options),struct2cell(options))',1,[])];
0110 % fmt=['%15s(%s' repmat(',%s',1,length(varg)-1) ')'];
0111 % %strargs=cellfun(@mat2str,varg,'UniformOutput',false);
0112 % strargs=cellfun(@(arg) mat2str(reshape(arg,[],size(arg,2))),varg,'UniformOutput',false);
0113 % elstr=sprintf(fmt,func2str(create),strargs{:});
0114 
0115 args=cellfun(@pyarray,args,'UniformOutput',false);
0116 keys=fieldnames(options);
0117 vals=cellfun(@(k) pyarray(options.(k)), keys, 'UniformOutput',false);
0118 v=reshape([keys vals]',1,[]);
0119 try
0120     elstr=pycreate(args{:},pyargs(v{:}));
0121 catch
0122     elstr=py.at.elements.Marker(args{1},pyargs(v{:}));
0123 end
0124 
0125     function s2 = rmfield2(s1,fieldnames)
0126         ok=cellfun(@(fn) isfield(s1,fn), fieldnames);
0127         s2=rmfield(s1,fieldnames(ok));
0128     end
0129 
0130     function result = getfield2(s,varargin)
0131         try
0132             result=getfield(s,varargin{1:end-1});
0133         catch
0134             result=varargin{end};
0135         end
0136     end
0137 
0138     function ret=pyarray(v)
0139         if isnumeric(v)
0140             if isscalar(v)
0141                 ret=v;
0142             elseif isvector(v)
0143                 ret=py.numpy.array(v(:)');
0144             else
0145                 ret=py.numpy.array(num2cell(v,2));
0146             end
0147         else
0148             ret=v;
0149         end
0150     end
0151 
0152     function [args,opts]=doptions(elem,create,argn,dontcheck,args)
0153         if nargin<4, dontcheck={}; end
0154         if nargin<3, argn={}; end
0155         if nargin<5, args=cellfun(@(field) elem.(field),argn,'UniformOutput',false); end
0156         args=[{elem.FamName},args];
0157         defelem=create(args{:});                        % Build sample element
0158         argnames=[{'FamName'},argn];
0159         if strcmp(elem.PassMethod,defelem.PassMethod)
0160             argnames=[argnames,{'PassMethod'}];
0161         end
0162         defel=rmfield(defelem,[argnames,dontcheck]);    % Keep only optional fields
0163         fnames=fieldnames(defel)';
0164         fvalues=struct2cell(defel)';
0165         ok=isfield(elem,fnames);                        % Check for default values
0166         ok(ok)=cellfun(@(fname,fval) isequal(elem.(fname),fval),fnames(ok),fvalues(ok));
0167         opts=rmfield2(elem,[argnames,fnames(ok)]);      % Keep only non-default values
0168     end
0169 end

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