Home > lattice > insertindrift.m

insertindrift

PURPOSE ^

INSERTINDRIFT inserts one or more elements into a drift element

SYNOPSIS ^

function ELEMSEQ = insertindrift(DRIFT0, ELEM1, POS1, varargin)

DESCRIPTION ^

 INSERTINDRIFT inserts one or more elements into a drift element
  and returns a sequence (cell array) of elements  ready to to be used 
  in AT lattice

 ELEMSEQ = INSERTELEM(DRIFT0, ELEM1, POS1, ... ELEMN, POSN)
 
 EXAMPLE: FODO cell

 --- 1. Declare elements

 D  = atelem('drift','Length',4.5);
 QF = atelem('quad','Length', 1, 'K',  1.234);
 QD = atelem('quad','Length', 1, 'K', -2.345);

 --- 2. Insert quadrupoles in the drift;

 FODOCELL = insertindrift(D, QF, 0.5, QD, 2, QF, 3.5);
 
 See also: SPLITELEM

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function ELEMSEQ = insertindrift(DRIFT0, ELEM1, POS1, varargin)
0002 % INSERTINDRIFT inserts one or more elements into a drift element
0003 %  and returns a sequence (cell array) of elements  ready to to be used
0004 %  in AT lattice
0005 %
0006 % ELEMSEQ = INSERTELEM(DRIFT0, ELEM1, POS1, ... ELEMN, POSN)
0007 %
0008 % EXAMPLE: FODO cell
0009 %
0010 % --- 1. Declare elements
0011 %
0012 % D  = atelem('drift','Length',4.5);
0013 % QF = atelem('quad','Length', 1, 'K',  1.234);
0014 % QD = atelem('quad','Length', 1, 'K', -2.345);
0015 %
0016 % --- 2. Insert quadrupoles in the drift;
0017 %
0018 % FODOCELL = insertindrift(D, QF, 0.5, QD, 2, QF, 3.5);
0019 %
0020 % See also: SPLITELEM
0021 
0022 
0023 if ~isatelem(DRIFT0)
0024     error('The first argument must be a valid Accelerator Toolbox drift element');
0025 end
0026 
0027 if ~isatelem(ELEM1)
0028     error('The second argument must be a valid Accelerator Toolbox element to insert');
0029 end
0030 
0031 if ~isnumeric(POS1)
0032     errorstr = sprintf('Incorrect syntax:\n');
0033     errorstr=[errorstr,sprintf('Elements to inserted must be followed by position [m]\n')];
0034     errorstr=[errorstr,sprintf('in the argument list: ELEM1, pos1, ... ELEMN, POSN ')];
0035     error(errorstr);
0036 end
0037         
0038     
0039 ELEMSEQ = {};
0040 if POS1>0
0041     ELEMSEQ{1} = atelem(DRIFT0,'Length',POS1);
0042 elseif POS1<0
0043     ('Inconsistent lengths and positions cause elements to overlap');
0044 end
0045     
0046     
0047 ELEMSEQ{end+1} = ELEM1;
0048 LCUM = POS1+ELEM1.Length;
0049 
0050 
0051 k = 1;
0052 while k < nargin-3 % Loop to extra
0053     if isatelem(varargin{k})
0054         if ~k<nargin & ~isnumeric(varargin{k+1})
0055             errorstr = sprintf('Incorrect syntax:\n');
0056             errorstr=[errorstr,sprintf('Elements to inserted must be followed by position [m]\n')];
0057             errorstr=[errorstr,sprintf('in the argument list: ELEM1, pos1, ... ELEMN, POSN ')];
0058             error(errorstr);
0059         else
0060             
0061             if (varargin{k+1}-LCUM)>0
0062                 ELEMSEQ{end+1} = atelem(DRIFT0,'Length', varargin{k+1} - LCUM);
0063             elseif (varargin{k+1}-LCUM)<0
0064                 error('Inconsistent lengths and positions cause elements to overlap');
0065             end
0066                
0067             ELEMSEQ{end+1} = varargin{k};
0068             LCUM = varargin{k+1}+varargin{k}.Length;
0069             k = k+2;
0070 
0071             
0072         end
0073     else
0074         error('Incorrect syntax');
0075     end   
0076 end
0077 
0078 if DRIFT0.Length-LCUM > 0
0079     ELEMSEQ{end+1} = atelem(DRIFT0,'Length',DRIFT0.Length - LCUM);
0080 elseif DRIFT0.Length-LCUM < 0
0081     error('Inconsistent lengths and positions cause elements to overlap');
0082 end

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