Home > lattice > atsplitelem.m

atsplitelem

PURPOSE ^

ATSPLITELEM Creates a line by inserting one or more elements into a base element

SYNOPSIS ^

function line=atsplitelem(baseelem,varargin)

DESCRIPTION ^

ATSPLITELEM Creates a line by inserting one or more elements into a base element

LINE=ATSPLITELEM(BASEELEM,FRAC1,ELEM1[,FRAC2,ELEM2...])
   Each inserted element is associated with a location given by 0<=FRAC<=1
   LINE is a cell array containing the sequence of resulting elements

 FRACi may be a scalar or a line vector of locations. ELEMi must be a
 single element, or a cell array of elements with the same length as FRACi.

 if FRAC = 0, the element is inserted before BASEELEM (no splitting)
 if FRAC = 1, the element is inserted after BASEELEM (no splitting)

 if ELEMi = [], nothing is inserted, only the splitting takes place

 ATSPLITELEM will split BASEELEM in elements with negative lengths if
 necessary. Elements with length 0 are not generated. When splitting
 dipoles, bending angles are distributed as lengths, and face angles are
 set to zero at splitting points.


 Examples:

>> dr=atdrift('DR',2);       % Insert quadrupoles inside a drift
>> qf=atquadrupole('QF',0.1,0.5);
>> qd=atquadrupole('QD',0.1,-0.5);
>> line=atsplitelem(dr,0.2,qf,0.8,qd);

>> mk=atmarker('MK');
>> line=atsplitelem(qf,0,mk);   % Insert a marker before a quadrupole

>> line=atsplitelem(qf,0.5,[]); % Split a quadrupole in two halves

 See also ATINSERTELEMS ATDIVELEM

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function line=atsplitelem(baseelem,varargin)
0002 %ATSPLITELEM Creates a line by inserting one or more elements into a base element
0003 %
0004 %LINE=ATSPLITELEM(BASEELEM,FRAC1,ELEM1[,FRAC2,ELEM2...])
0005 %   Each inserted element is associated with a location given by 0<=FRAC<=1
0006 %   LINE is a cell array containing the sequence of resulting elements
0007 %
0008 % FRACi may be a scalar or a line vector of locations. ELEMi must be a
0009 % single element, or a cell array of elements with the same length as FRACi.
0010 %
0011 % if FRAC = 0, the element is inserted before BASEELEM (no splitting)
0012 % if FRAC = 1, the element is inserted after BASEELEM (no splitting)
0013 %
0014 % if ELEMi = [], nothing is inserted, only the splitting takes place
0015 %
0016 % ATSPLITELEM will split BASEELEM in elements with negative lengths if
0017 % necessary. Elements with length 0 are not generated. When splitting
0018 % dipoles, bending angles are distributed as lengths, and face angles are
0019 % set to zero at splitting points.
0020 %
0021 %
0022 % Examples:
0023 %
0024 %>> dr=atdrift('DR',2);       % Insert quadrupoles inside a drift
0025 %>> qf=atquadrupole('QF',0.1,0.5);
0026 %>> qd=atquadrupole('QD',0.1,-0.5);
0027 %>> line=atsplitelem(dr,0.2,qf,0.8,qd);
0028 %
0029 %>> mk=atmarker('MK');
0030 %>> line=atsplitelem(qf,0,mk);   % Insert a marker before a quadrupole
0031 %
0032 %>> line=atsplitelem(qf,0.5,[]); % Split a quadrupole in two halves
0033 %
0034 % See also ATINSERTELEMS ATDIVELEM
0035 
0036 elfrac=varargin(1:2:end);
0037 elems=cell(size(elfrac));
0038 for i=1:length(elfrac)
0039     ellist=varargin{2*i};
0040     if ~iscell(ellist)
0041         ellist={ellist};
0042     end
0043     elems{i}(1:length(elfrac{i}))=ellist(:);
0044 end
0045 elems=cat(2,elems{:})';
0046 elfrac=cat(2,elfrac{:})';
0047 ellg=0.5*atgetfieldvalues(elems,'Length')./baseelem.Length;
0048 ellg(isnan(ellg))=0;
0049 drfrac=[elfrac-ellg;1]-[0;elfrac+ellg];
0050 long=drfrac~=0;
0051 
0052 drifts=cell(size(drfrac));
0053 drifts(long)=atdivelem(baseelem,drfrac(long));
0054 
0055 list=cell(length(drifts)+length(elems),1);
0056 list(1:2:end)=drifts;
0057 list(2:2:end)=elems;
0058 
0059 keep=true(size(list));
0060 keep(1:2:end)=long;                         % remove useless elements
0061 keep(2:2:end)=atgetcells(elems,'FamName');  % remove dummy inserts
0062 
0063 line=list(keep);
0064 end

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