0001 function rerr=atset_s_shift(r,pos,DS)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
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
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
0032
0033
0034
0035 if ~isempty(posmag)
0036 [driftUP,driftDO]=finddriftsaroundpos(r,posmag);
0037
0038
0039 LDUP0=atgetfieldvalues(r,driftUP,'Length',{1,1});
0040 rerr=atsetfieldvalues(rerr,driftUP,'Length',LDUP0-dsmag');
0041
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
0049
0050
0051
0052 [driftUP,driftDO]=finddriftsaroundpos(r,posdip);
0053 theta=atgetfieldvalues(rerr,posdip,'BendingAngle',{1,1});
0054
0055
0056
0057
0058
0059 maggr=getMagGroupsFromMagNum(r(posdip));
0060
0061 for imaggr=1:length(maggr)
0062 dipind=maggr{imaggr};
0063
0064
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});
0078 catch
0079 dt2out0=zeros(dt2out);
0080 end
0081
0082
0083 LDDO0=atgetfieldvalues(rerr,driftDO(dipind(end)),'Length',{1,1});
0084 rerr=atsetfieldvalues(rerr,driftDO(dipind(end)),'Length',LDDO0+dsout);
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