0001 function [rcor,inCOD,hs,vs]=atcorrectorbit(...
0002 rerr,...
0003 indBPM,...
0004 indHCor,...
0005 indVCor,...
0006 inCOD,...
0007 neigSteerer,...
0008 correctflags,...
0009 scalefactor,...
0010 ModelRM,...
0011 reforbit,...
0012 steererlimit,...
0013 printouttext)
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056 kval=1e-5;
0057 delta=1e-3;
0058
0059 alpha=mcf(rerr);
0060 indrfc=find(atgetcells(rerr,'Frequency'));
0061 f0=rerr{indrfc(1)}.Frequency;
0062
0063
0064 if nargin<12
0065 printouttext=true;
0066 end
0067 if nargin<11
0068 steererlimit=[];
0069 end
0070
0071 if nargin<4
0072 if printouttext
0073 disp('get BPM and Correctors indexes'); end;
0074 indBPM=finc(atgetcells(rerr,'Class','Monitor'));
0075 indHCor=finc(atgetcells(rerr,'iscorH','H'));
0076 indVCor=finc(atgetcells(rerr,'iscorV','V'));
0077 end
0078
0079 if nargin<5
0080 inCOD=[0 0 0 0 0 0]';
0081 end
0082
0083 if nargin<6
0084 neigSteerer=[length(indHCor) length(indVCor)]/2;
0085 end
0086
0087 if nargin<7
0088 correctflags=[true true];
0089 end
0090
0091 if nargin<8
0092 if printouttext
0093 disp(' --- scale set to 1.0'); end;
0094 scalefactor=1.0;
0095 end
0096
0097 if nargin<9
0098 if printouttext, disp(' --- computing orbit Response matrix'); end;
0099 ModelRM=[];
0100 end
0101
0102 if nargin<10
0103 if printouttext, disp(' --- reference orbit = 0'); end;
0104 reforbit=zeros(2,length(indBPM));
0105 end
0106
0107 if scalefactor<0 || scalefactor>1
0108 if printouttext
0109 disp(' --- scale factor out of range. Set to 1.0'); end;
0110 scalefactor=1.0;
0111 end
0112
0113 if correctflags(1)
0114 rmsel=[1 2 3];
0115 else
0116 rmsel=[1 2];
0117 end
0118
0119
0120 if isempty(ModelRM)
0121
0122 if printouttext
0123 disp('get orbit RM'); end;
0124
0125 ModelRM=getresponsematrices(...
0126 rerr,...
0127 indBPM,...
0128 indHCor,...
0129 indVCor,...
0130 [],...
0131 [],...
0132 [],...
0133 inCOD,...
0134 rmsel...
0135 );
0136
0137
0138 if ~correctflags(1)
0139 ModelRM.OrbHDPP=[];
0140 ModelRM.OrbVDPP=[];
0141 end
0142
0143 end
0144
0145 ormH=ModelRM.OrbHCor;
0146 ormV=ModelRM.OrbVCor;
0147
0148 dppH=ModelRM.OrbHDPP;
0149 dppV=ModelRM.OrbVDPP;
0150
0151
0152
0153 o=findorbit6Err(rerr,indBPM,inCOD);
0154 ox0=o(1,:);
0155 oy0=o(3,:);
0156
0157
0158
0159
0160 Niter=size(neigSteerer,1);
0161 for iter=1:Niter
0162
0163 if printouttext
0164 disp(['Orbit correction iter ' num2str(iter,'%d, ') 'n-eig: ' num2str(neigSteerer(iter,:),'%d, ')]);
0165 end
0166
0167
0168 corh0=atgetfieldvalues(rerr,indHCor,'PolynomB',{1,1});
0169 corv0=atgetfieldvalues(rerr,indVCor,'PolynomA',{1,1});
0170
0171
0172 o=findorbit6Err(rerr,indBPM,inCOD);
0173 ox=o(1,:);
0174 oy=o(3,:);
0175
0176
0177 ox=ox-reforbit(1,:);
0178 oy=oy-reforbit(2,:);
0179
0180
0181 if correctflags(1) && correctflags(2)
0182 RMH=[ [ormH{1};ones(size(indHCor))] [dppH';0] ];
0183 RMV=[ [ormV{3};ones(size(indVCor))] [dppV';0] ];
0184 elseif correctflags(1) && ~correctflags(2)
0185 RMH=[ ormH{1} dppH' ];
0186 RMV=[ ormV{3} dppV' ];
0187 elseif ~correctflags(1) && correctflags(2)
0188 RMH=[ormH{1};ones(size(indHCor))];
0189 RMV=[ormV{3};ones(size(indVCor))];
0190 elseif ~correctflags(1) && ~correctflags(2)
0191 RMH=ormH{1};
0192 RMV=ormV{3};
0193 end
0194
0195
0196 if correctflags(2)
0197 dch=qemsvd_mod(RMH,[ox';0],neigSteerer(iter,1));
0198 dcv=qemsvd_mod(RMV,[oy';0],neigSteerer(iter,2));
0199 else
0200 dch=qemsvd_mod(RMH,ox',neigSteerer(iter,1));
0201 dcv=qemsvd_mod(RMV,oy',neigSteerer(iter,2));
0202 end
0203
0204
0205
0206 if correctflags(1)
0207 hs=corh0-dch(1:end-1)*scalefactor;
0208 vs=corv0-dcv(1:end-1)*scalefactor;
0209
0210 dd=-dch(end);
0211 else
0212 hs=corh0-dch*scalefactor;
0213 vs=corv0-dcv*scalefactor;
0214 end
0215
0216
0217 if ~isempty(steererlimit)
0218 hs(abs(hs)>steererlimit(1))=steererlimit(1);
0219 vs(abs(vs)>steererlimit(2))=steererlimit(2);
0220 end
0221
0222 rtest=atsetfieldvalues(rerr,indHCor,'PolynomB',{1,1},hs);
0223 rtest=atsetfieldvalues(rtest,indVCor,'PolynomA',{1,1},vs);
0224
0225
0226 if correctflags(1)
0227
0228 rtest=atsetfieldvalues(rtest,indrfc,'Frequency',f0-alpha*(dd)*f0);
0229
0230 if printouttext
0231 disp(['Delta RF : ' num2str(-alpha*(dd)*f0) ' Hz']);
0232 end
0233 end
0234
0235
0236 t=tunechrom(rtest,0);
0237 if not(isnan(t(1)) || isnan(t(2)))
0238
0239 rcor = rtest;
0240 else
0241 rcor = rerr;
0242 if printouttext
0243 disp('Orbit correction fails, stop at previous step.')
0244 end
0245 break
0246 end
0247
0248
0249 rerr=rcor;
0250 end
0251
0252
0253 o=findorbit6Err(rcor,indBPM,inCOD);
0254 oxc=o(1,:);
0255 oyc=o(3,:);
0256 Lh=atgetfieldvalues(rcor,indHCor,'Length');
0257 Lv=atgetfieldvalues(rcor,indVCor,'Length');
0258 hsL=hs.*Lh;
0259 vsL=vs.*Lv;
0260
0261 if printouttext
0262
0263 disp([' before' ' ' '-->' ' ' 'after'])
0264 disp(['oX: ' num2str(std(ox0-reforbit(1,:))*1e6,'%3.3f') ' -> ' num2str(std(oxc-reforbit(1,:))*1e6,'%3.3f') 'um']);
0265 disp(['oY: ' num2str(std(oy0-reforbit(2,:))*1e6,'%3.3f') ' -> ' num2str(std(oyc-reforbit(2,:))*1e6,'%3.3f') 'um']);
0266 disp([' ' 'min' ' ' 'mean' ' ' 'max'])
0267 disp(['hs:' num2str([min(hsL) mean(hsL) max(hsL)]*1e3,' %2.2f ') ' mrad'])
0268 disp(['vs:' num2str([min(vsL) mean(vsL) max(vsL)]*1e3,' %2.2f ') ' mrad'])
0269 disp(['dpp: ' num2str(inCOD(5))])
0270 end