Home > pubtools > LatticeTuningFunctions > errors > atset_s_shift.m

atset_s_shift

PURPOSE ^

SYNOPSIS ^

function rerr=atset_s_shift(r,pos,DS)

DESCRIPTION ^

 implements DS longitudinal position drift
 by changing drifts at the sides of the
 elements defined by pos in r

 for dipoles the T2(1) field is also changed and the the out DS is
 modified:
 T2(1)=DS*sin(bendignangle)
 DSout=DS*cos(bendignangle)

 pos and DS must be the same size

see also: atsetshift atsettilt atsettiltdipole

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function rerr=atset_s_shift(r,pos,DS)
0002 %
0003 % implements DS longitudinal position drift
0004 % by changing drifts at the sides of the
0005 % elements defined by pos in r
0006 %
0007 % for dipoles the T2(1) field is also changed and the the out DS is
0008 % modified:
0009 % T2(1)=DS*sin(bendignangle)
0010 % DSout=DS*cos(bendignangle)
0011 %
0012 % pos and DS must be the same size
0013 %
0014 %see also: atsetshift atsettilt atsettiltdipole
0015 
0016 if length(pos)~=length(DS)
0017     error('pos and DS must be the same size');
0018 end
0019 
0020 rerr=r;
0021 
0022 %find dipoles
0023 dipind=findcells(r(pos),'BendingAngle');
0024 posmag=pos;
0025 dsmag=DS;
0026 posdip=pos(dipind);
0027 dsdip=DS(dipind);
0028 posmag(dipind)=[];
0029 dsmag(dipind)=[];
0030 
0031 %% STRAIGTH MAGNETS
0032 
0033 % find first drift before each element in pos
0034 % and  first drift after  each element in pos
0035 if ~isempty(posmag)
0036     [driftUP,driftDO]=finddriftsaroundpos(r,posmag);
0037     
0038     % shorten drift up
0039     LDUP0=atgetfieldvalues(r,driftUP,'Length',{1,1});
0040     rerr=atsetfieldvalues(rerr,driftUP,'Length',LDUP0-dsmag');
0041     % lengthen drift down
0042     LDDO0=atgetfieldvalues(r,driftDO,'Length',{1,1});
0043     rerr=atsetfieldvalues(rerr,driftDO,'Length',LDDO0+dsmag');
0044     rerr=atsetfieldvalues(rerr,posmag,'DeltaS',dsmag');
0045     
0046 end
0047 if ~isempty(posdip)
0048     %% DIPOLES
0049     
0050     % find first drift before each element in pos
0051     % and  first drift after  each element in pos
0052     [driftUP,driftDO]=finddriftsaroundpos(r,posdip);
0053     theta=atgetfieldvalues(rerr,posdip,'BendingAngle',{1,1});
0054     
0055     %if dipoles have the same MagNum, move each part and sum effect on T2
0056     %coordinate change. DS assigned to first slice is assumed as DS of the
0057     %whole magnet.
0058     
0059     maggr=getMagGroupsFromMagNum(r(posdip));
0060     
0061     for imaggr=1:length(maggr)
0062         dipind=maggr{imaggr}; % sliced dipole indexes in posdip
0063         
0064         % shorten drift up for first dipole in group
0065         LDUP0=atgetfieldvalues(rerr,driftUP(dipind(1)),'Length',{1,1});
0066         rerr=atsetfieldvalues(rerr,driftUP(dipind(1)),'Length',LDUP0-dsdip(dipind(1))');
0067         
0068         dsout=dsdip((dipind(1)))';
0069         dt2out=dsdip((dipind(1)))';
0070         
0071         for iiind=1:length(dipind)
0072             dsout=dsout.*cos(theta(dipind(iiind)));
0073             dt2out=dt2out.*sin(theta(dipind(iiind)));
0074         end
0075         
0076         try
0077             dt2out0=atgetfieldvalues(rerr,posdip(dipind(end)),'T2',{1,1}); % add to existing if exists
0078         catch
0079             dt2out0=zeros(dt2out);
0080         end
0081         
0082         % lengthen drift down FOR last DIPOLE in group. ALSO T2 changes!
0083         LDDO0=atgetfieldvalues(rerr,driftDO(dipind(end)),'Length',{1,1});
0084         rerr=atsetfieldvalues(rerr,driftDO(dipind(end)),'Length',LDDO0+dsout);%+dsdip(dipind(1)));%
0085         rerr=atsetfieldvalues(rerr,posdip(dipind(end)),'T2',{1,1},dt2out0-dt2out); %
0086         
0087         rerr=atsetfieldvalues(rerr,posdip(dipind),'DeltaS',dsdip(dipind(1))');
0088         rerr=atsetfieldvalues(rerr,posdip(dipind),'DeltaST2',...
0089             [zeros(1,length(dipind)-1) dt2out]);
0090         
0091     end
0092         
0093 end
0094 
0095 return
0096 
0097 
0098 function [dup,ddo]=finddriftsaroundpos(r,pos)
0099 dup=nan(size(pos));
0100 ddo=nan(size(pos));
0101 
0102 for indpos=1:length(pos)
0103     
0104     i=pos(indpos);
0105     
0106     NEL=length(r);
0107     while ~strcmp(r{i}.Class,'Drift')
0108         if i<NEL
0109             i=i+1;
0110         else
0111             i=1;
0112         end
0113     end
0114     dup(indpos)=i;
0115     
0116     i=pos(indpos);
0117     while ~strcmp(r{i}.Class,'Drift')
0118         if i>1
0119             i=i-1;
0120         else
0121             i=NEL;
0122         end
0123     end
0124     ddo(indpos)=i;
0125 end
0126 
0127 return
0128 
0129 
0130 function a=getmagnumdipole(r,ind)
0131 
0132 try
0133     a=r{ind}.MagNum;
0134 catch
0135     a=NaN;
0136 end
0137 
0138 return

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