0001 function [rclosed,inCOD]=atfirstturntrajectory(...
0002 ropen,...
0003 inCOD,...
0004 indBPM,...
0005 indHCor,...
0006 indVCor,...
0007 lim,...
0008 maxcornum,...
0009 correctflags,...
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
0057
0058
0059
0060
0061
0062
0063 if nargin<12
0064 printouttext=true;
0065 end
0066
0067 if nargin<2
0068 inCOD=[0 0 0 0 0 0]';
0069 end
0070 if nargin<3
0071 if printouttext
0072 disp('get BPM and Correctors indexes');end;
0073 indBPM=find(atgetcells(ropen,'Class','Monitor'));
0074 end
0075 if nargin<4
0076 indHCor=find(atgetcells(ropen,'iscorH','H'));
0077 end
0078 if nargin<5
0079 indVCor=find(atgetcells(ropen,'iscorV','V'));
0080 end
0081 if nargin<6
0082 lim=4e-3;
0083 end
0084 if nargin<7
0085 maxcornum=length(indBPM);
0086 end
0087 if nargin<8
0088 correctflags=[true true];
0089 end
0090
0091 if nargin<10
0092 reforbit=zeros(2,length(indBPM));
0093 end
0094
0095 if nargin<11
0096 steererlimit=[];
0097 end
0098
0099 if iscell(ModelRM)
0100 r0=ModelRM;
0101 end
0102
0103 traj=0;
0104
0105 if printouttext
0106 if traj==1
0107 disp('correcting trajectory');
0108 elseif traj==0
0109 disp('correcting trajectory with model rm')
0110 end
0111 end
0112
0113
0114 if correctflags(1)
0115 rmsel=[4 5 6];
0116 else
0117 rmsel=[4 5];
0118 end
0119
0120 if iscell(ModelRM)
0121 if printouttext
0122 disp('computing model trajectory RM')
0123 end
0124
0125 ModelRM=getresponsematrices(...
0126 r0,...
0127 indBPM,...
0128 indHCor,...
0129 indVCor,...
0130 [],...
0131 [],...
0132 [],...
0133 inCOD,...
0134 rmsel...
0135 );
0136
0137 if ~correctflags(1)
0138 ModelRM.TrajHDPP{1}=[];
0139 ModelRM.TrajVDPP{3}=[];
0140 end
0141
0142 if printouttext
0143 disp('computed model trajectory RM')
0144 end
0145
0146 end
0147
0148
0149
0150 RMH=ModelRM.TrajHCor{1};
0151 RMV=ModelRM.TrajVCor{3};
0152 RMHd=ModelRM.TrajHDPP;
0153 RMVd=ModelRM.TrajVDPP;
0154
0155
0156 rclosed=[];
0157
0158 nbpmuprev=0;
0159 nbpmu=0;
0160
0161 lim1=lim(1);
0162
0163 countiter=0;
0164 countstack=0;
0165
0166 countatbpm=zeros(size(indBPM));
0167 fraceig=2;
0168 Nforcor=maxcornum;
0169
0170
0171
0172 while isempty(rclosed)
0173 if printouttext
0174 disp('Search closed orbit');
0175 end
0176 if countstack>6
0177 error(['correction of open trjectory looping at ' num2str(nbpmu) ' bpms'])
0178 end
0179
0180
0181
0182
0183 if (nbpmu==length(indBPM))
0184 if printouttext
0185 disp(['Trajectory closure with last 2 correctors']);
0186 end
0187
0188 ropen=MatchLast2CorForFirstBPM(ropen, inCOD,...
0189 indBPM, indHCor, indVCor);
0190
0191 COD=findorbit6Err(ropen,indBPM,inCOD);
0192
0193 if ~isempty(find(isnan(COD),1))
0194 if printouttext
0195 disp('no closed orbit, but all bpm see signal. Closing with last 2 correctors.'); end;
0196
0197 rclosed=MatchLast2CorForFirstBPM(ropen, inCOD,...
0198 indBPM, indHCor, indVCor);
0199 end
0200
0201 if isempty(find(isnan(COD),1))
0202 if printouttext
0203 disp('Found closed orbit'); end;
0204 rclosed=ropen;
0205 end
0206
0207 else
0208
0209 countiter=countiter+1;
0210
0211
0212
0213
0214
0215
0216
0217 [t]=findtrajectory6Err(ropen,indBPM,inCOD);
0218
0219 ox=t(1,:)-reforbit(1,:);
0220 oy=t(3,:)-reforbit(2,:);
0221
0222
0223 usebpm=~isnan(ox) & ~isnan(oy) & ox<lim(1) & ox>-lim(1) & oy<lim(1) & oy>-lim(1);
0224
0225
0226
0227
0228
0229 usebpm=indBPM(usebpm)<=max(indBPM(usebpm))*1;
0230
0231 if countiter>1
0232 nbpmuprev=nbpmu;
0233 end
0234
0235 nbpmu=length(find(usebpm));
0236
0237
0238 if nbpmuprev==nbpmu
0239 if countstack>1
0240 lim1=lim1+0.5e-3;
0241 if printouttext
0242 disp(['no improvement from last step. increasing lim to: ' num2str(lim1)]); end;
0243 usebpm=~isnan(ox) & ~isnan(oy) & ox<lim1 & ox>-lim1 & oy<lim1 & oy>-lim1;
0244
0245 if lim1>lim+2e-3
0246 if printouttext
0247 warning('Could not find closed orbit. Trajectory above 6mm. Aborting trajectory correction'); end;
0248 warning('Closed orbit NOT FOUND');
0249 rclosed=ropen;
0250 return
0251 end
0252
0253 if lim1>lim+1e-3 && numel(find(usebpm))<320
0254 if printouttext
0255 disp('update optimal injection point'); end;
0256 [inCOD]=Scan2x2DinCOD(ropen,inCOD,101,lim1,[]);
0257
0258 end
0259
0260
0261 end
0262 countstack=countstack+1;
0263
0264
0265
0266 if countstack==3
0267 traj=0;
0268 fraceig=5;
0269 end
0270 if countstack==4
0271 traj=0;
0272 fraceig=10;
0273 end
0274 if countstack==5
0275 traj=1;
0276 if printouttext
0277 disp('setting correction to measured trajectory response matrix.');end;
0278 fraceig=7;
0279 end
0280 if countstack==6
0281 traj=1;
0282 if printouttext
0283 disp('setting correction to measured trajectory response matrix.'); end;
0284 fraceig=10;
0285 end
0286
0287 else
0288 countstack=0;
0289
0290 end
0291
0292 if countstack==10
0293 error('Could not find closed orbit. Aborting trajectory correction');
0294 end
0295
0296 nbpmu=length(find(usebpm));
0297 if nbpmu==0
0298 error('NO BEAM AT ALL!')
0299 end
0300
0301
0302 usecorH=indHCor<max(indBPM(usebpm));
0303 usecorV=indVCor<max(indBPM(usebpm));
0304
0305 nhcu=length(find(usecorH));
0306 nvcu=length(find(usecorV));
0307
0308 countatbpm(nbpmu)=countatbpm(nbpmu)+1;
0309
0310 if nhcu==0 || nvcu==0
0311 error('NO BEAM AT ALL!')
0312 end
0313
0314 if printouttext
0315 disp(['Trajectory correction:'...
0316 ' nbpms= ' num2str(nbpmu)...
0317 ' ncor: ' num2str([nhcu nvcu],'%d, ')]);
0318 end
0319
0320
0321
0322 lastbpm=find(usebpm==1,1,'last');
0323 if lastbpm>Nforcor
0324 usebpm(1:lastbpm-Nforcor)=0;
0325 end
0326
0327 lastcor=find(usecorH==1,1,'last');
0328 if lastcor>Nforcor
0329 usecorH(1:lastcor-Nforcor)=0;
0330 end
0331
0332 lastcor=find(usecorV==1,1,'last');
0333 if lastcor>Nforcor
0334 usecorV(1:lastcor-Nforcor)=0;
0335 end
0336
0337 if printouttext
0338 disp('computing ORM for available trajectory');
0339 end
0340
0341 bpm=indBPM(usebpm);
0342
0343 X=ox(usebpm)';
0344 Y=oy(usebpm)';
0345
0346 HK=indHCor(usecorH);
0347 VK=indVCor(usecorV);
0348
0349 neigSteerer=floor(length(HK)/fraceig);
0350 fracapply=1;
0351
0352
0353
0354 if printouttext
0355 disp('H PLANE');
0356 end;
0357 corh0=getcellstruct(ropen,'PolynomB',HK,1,1);
0358
0359 if traj==1
0360
0361 ModelRM=getresponsematrices(...
0362 ropen,...
0363 bpm,...
0364 HK,...
0365 [],...
0366 [],...
0367 [],...
0368 [],...
0369 inCOD,...
0370 rmsel...
0371 );
0372 if ~correctflags(1)
0373 ModelRM.TrajHDPP{1}=[];
0374 end
0375 RespH=ModelRM.TrajHCor{1};
0376 RespHd=ModelRM.TrajHDPP;
0377 elseif traj==0
0378 RespH=RMH(usebpm,usecorH);
0379 if correctflags(1)
0380 RespHd=RMHd(usebpm);
0381 end
0382 end
0383
0384
0385 if correctflags(1) && correctflags(2)
0386 FRH=[ [RespH;ones(1,size(RespH,2))] [RespHd';0] ];
0387 elseif correctflags(1) && ~correctflags(2)
0388 FRH=[ RespH RespHd' ];
0389 elseif ~correctflags(1) && correctflags(2)
0390 FRH=[RespH;ones(1,size(RespH,2))];
0391 elseif ~correctflags(1) && ~correctflags(2)
0392 FRH=RespH;
0393 end
0394
0395
0396 if correctflags(2)
0397 dch=qemsvd_mod(FRH,[X;0],neigSteerer)*fracapply;
0398 else
0399 dch=qemsvd_mod(FRH,X,neigSteerer)*fracapply;
0400 end
0401
0402
0403 if correctflags(1)
0404
0405 dch=dch(1:end-1);
0406 deltacor=dch(end);
0407 end
0408
0409
0410 if ~isempty(steererlimit)
0411 dch(abs(dch)>steererlimit(1))=steererlimit(1);
0412 end
0413
0414 ropen=setcellstruct(ropen,'PolynomB',HK,corh0-dch,1,1);
0415
0416
0417
0418 if printouttext
0419 disp('correcting available V trajectory');
0420 disp('V PLANE');
0421 end;
0422
0423 corv0=getcellstruct(ropen,'PolynomA',VK,1,1);
0424
0425 if traj==1
0426
0427 ModelRM=getresponsematrices(...
0428 ropen,...
0429 bpm,...
0430 [],...
0431 VK,...
0432 [],...
0433 [],...
0434 [],...
0435 inCOD,...
0436 rmsel...
0437 );
0438 if ~correctflags(1)
0439 ModelRM.TrajVDPP{1}=[];
0440 end
0441 RespV=ModelRM.TrajVCor{3};
0442 RespVd=ModelRM.TrajVDPP;
0443 elseif traj==0
0444 RespV=RMV(usebpm,usecorH);
0445 if correctflags(1)
0446 RespVd=RMVd(usebpm);
0447 end
0448 end
0449
0450
0451 if correctflags(2)
0452 FRV=[RespV;ones(1,size(RespV,2))];
0453 elseif ~correctflags(2)
0454 FRV=RespV;
0455 end
0456
0457
0458 if correctflags(2)
0459 dcv=qemsvd_mod(FRV,[Y;0],neigSteerer)*fracapply;
0460 else
0461 dcv=qemsvd_mod(FRV,Y,neigSteerer)*fracapply;
0462 end
0463
0464
0465
0466 if ~isempty(steererlimit)
0467 dcv(abs(dcv)>steererlimit(2))=steererlimit(2);
0468 end
0469 ropen=setcellstruct(ropen,'PolynomA',VK,corv0-dcv,1,1);
0470
0471
0472 if printouttext
0473 disp('correcting available V trajectory'); end;
0474
0475
0476
0477 [t]=findtrajectory6Err(ropen,indBPM,inCOD);
0478
0479 oxc=t(1,:);
0480 oyc=t(3,:);
0481
0482
0483 if printouttext
0484
0485 disp(['X: ' num2str(std(ox(usebpm))*1e6,'%3.3f') ' -> '...
0486 num2str(std(oxc(usebpm))*1e6,'%3.3f') ' um'])
0487 disp(['Y: ' num2str(std(oy(usebpm))*1e6,'%3.3f') ' -> '...
0488 num2str(std(oyc(usebpm))*1e6,'%3.3f') ' um'])
0489 end;
0490
0491 end
0492
0493 end
0494
0495 if printouttext
0496
0497 [t]=findtrajectory6Err(ropen,indBPM,inCOD);
0498
0499 ox=t(1,:);
0500 oy=t(3,:);
0501
0502 [t]=findtrajectory6Err(rclosed,indBPM,inCOD);
0503
0504 oxc=t(1,:);
0505 oyc=t(3,:);
0506
0507
0508
0509 disp(['X: ' num2str(std(ox)*1e6,'%3.3f') ' -> '...
0510 num2str(std(oxc)*1e6,'%3.3f') ' um'])
0511 disp(['Y: ' num2str(std(oy)*1e6,'%3.3f') ' -> '...
0512 num2str(std(oyc)*1e6,'%3.3f') ' um'])
0513 end;
0514
0515
0516 [inCOD]=findorbit6Err(rclosed,1,inCOD);
0517
0518
0519 end