0001 function [Rmat, OutputFileName] = measbpmresp(varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
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
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153 BPMxFamily = gethbpmfamily;
0154 if isempty(BPMxFamily)
0155 error('"BPMx" needs to be a MemberOf some family.');
0156 end
0157 BPMxList = [];
0158
0159 BPMyFamily = getvbpmfamily;
0160 if isempty(BPMyFamily)
0161 error('"BPMy" needs to be a MemberOf some family.');
0162 end
0163 BPMyList = [];
0164
0165 HCMFamily = gethcmfamily;
0166 if isempty(HCMFamily)
0167 error('"HCM" needs to be a MemberOf some family.');
0168 end
0169 HCMList = [];
0170 HCMKicks = [];
0171 Default2HCMKick = .05e-3;
0172
0173 VCMFamily = getvcmfamily;
0174 if isempty(VCMFamily)
0175 error('"VCM" needs to be a MemberOf some family.');
0176 end
0177 VCMList = [];
0178 VCMKicks = [];
0179 Default2VCMKick = .05e-3;
0180 ModulationMethod = 'bipolar';
0181
0182
0183 if strcmpi(ModulationMethod, 'bipolar')
0184 LOCORespFlags.ResponseMatrixMeasurement = 'Bidirectional';
0185 else
0186 LOCORespFlags.DispersionMeasurement = 'Bidirectional';
0187 end
0188 if isstoragering
0189 LOCORespFlags.ResponseMatrixCalculator = 'Linear';
0190 LOCORespFlags.ClosedOrbitType = 'fixedpathlength';
0191 LOCORespFlags.MachineType = 'StorageRing';
0192 else
0193
0194 LOCORespFlags.ResponseMatrixCalculator = 'full';
0195 LOCORespFlags.ClosedOrbitType = 'fixedmomentum';
0196 LOCORespFlags.MachineType = 'Transport';
0197 end
0198
0199 WaitFlag = -2;
0200 ExtraDelay = 0;
0201 StructOutputFlag = 0;
0202 NumericOutputFlag = 0;
0203 DisplayFlag = -1;
0204 ArchiveFlag = -1;
0205 FileName = -1;
0206 DirectoryName = '';
0207 ModeFlag = '';
0208 UnitsFlag = '';
0209
0210 InputFlags = {};
0211 DCCTFlag = {};
0212 for i = length(varargin):-1:1
0213 if isstruct(varargin{i})
0214
0215 elseif iscell(varargin{i})
0216
0217 elseif strcmpi(varargin{i},'Struct')
0218 StructOutputFlag = 1;
0219 varargin(i) = [];
0220 elseif strcmpi(varargin{i},'Numeric')
0221 StructOutputFlag = 0;
0222 NumericOutputFlag = 1;
0223 varargin(i) = [];
0224 elseif strcmpi(varargin{i},'Model')
0225 ModeFlag = varargin{i};
0226 InputFlags = [InputFlags varargin(i)];
0227 varargin(i) = [];
0228 elseif strcmpi(varargin{i},'Simulator')
0229 ModeFlag = varargin{i};
0230 InputFlags = [InputFlags varargin(i)];
0231 varargin(i) = [];
0232 elseif strcmpi(varargin{i},'Online')
0233 ModeFlag = varargin{i};
0234 InputFlags = [InputFlags varargin(i)];
0235 varargin(i) = [];
0236 elseif strcmpi(varargin{i},'Manual')
0237 ModeFlag = varargin{i};
0238 InputFlags = [InputFlags varargin(i)];
0239 varargin(i) = [];
0240 elseif strcmpi(varargin{i},'Physics')
0241 UnitsFlag = varargin{i};
0242 InputFlags = [InputFlags varargin(i)];
0243 varargin(i) = [];
0244 elseif strcmpi(varargin{i},'Hardware')
0245 UnitsFlag = varargin{i};
0246 InputFlags = [InputFlags varargin(i)];
0247 varargin(i) = [];
0248 elseif strcmpi(varargin{i},'Archive')
0249 ArchiveFlag = 1;
0250 if length(varargin) > i
0251
0252 if ischar(varargin{i+1})
0253 FileName = varargin{i+1};
0254 varargin(i+1) = [];
0255 end
0256 end
0257 varargin(i) = [];
0258 elseif strcmpi(varargin{i},'NoArchive')
0259 ArchiveFlag = 0;
0260 varargin(i) = [];
0261 elseif strcmpi(varargin{i},'NoDisplay')
0262 DisplayFlag = 0;
0263 InputFlags = [InputFlags varargin(i)];
0264 varargin(i) = [];
0265 elseif strcmpi(varargin{i},'Display')
0266 DisplayFlag = 1;
0267 InputFlags = [InputFlags varargin(i)];
0268 varargin(i) = [];
0269
0270 elseif strcmpi(varargin{i},'unipolar') || strcmpi(varargin{i},'oneway')
0271 ModulationMethod = 'unipolar';
0272 LOCORespFlags.ResponseMatrixMeasurement = 'oneway';
0273 varargin(i) = [];
0274 elseif strcmpi(varargin{i},'bipolar') || strcmpi(varargin{i},'bidirectional')
0275 ModulationMethod = 'bipolar';
0276 LOCORespFlags.ResponseMatrixMeasurement = 'bidirectional';
0277 varargin(i) = [];
0278
0279 elseif strcmpi(varargin{i},'FixedPathLength')
0280 LOCORespFlags.ClosedOrbitType = 'FixedPathLength';
0281 varargin(i) = [];
0282 elseif strcmpi(varargin{i},'FixedMomentum')
0283 LOCORespFlags.ClosedOrbitType = 'FixedMomentum';
0284 varargin(i) = [];
0285 elseif strcmpi(varargin{i},'Linear')
0286 LOCORespFlags.ResponseMatrixCalculator = 'Linear';
0287 varargin(i) = [];
0288 elseif strcmpi(varargin{i},'Full')
0289 LOCORespFlags.ResponseMatrixCalculator = 'Full';
0290 varargin(i) = [];
0291
0292 elseif strcmpi(varargin{i},'MinimumBeamCurrent')
0293 DCCTFlag = [varargin(i) varargin(i+1)];
0294 varargin(i+1) = [];
0295 varargin(i) = [];
0296 end
0297 end
0298
0299
0300
0301
0302
0303
0304 if length(varargin) >= 1
0305 if isstruct(varargin{1})
0306 BPMxFamily = varargin{1}.FamilyName;
0307 BPMxList = varargin{1}.DeviceList;
0308
0309
0310 if isempty(UnitsFlag)
0311 UnitsFlag = varargin{1}.Units;
0312 end
0313
0314 varargin(1) = [];
0315
0316
0317 if ~NumericOutputFlag
0318 StructOutputFlag = 1;
0319 end
0320 elseif ischar(varargin{1})
0321 BPMxFamily = varargin{1};
0322 varargin(1) = [];
0323 if length(varargin) >= 1
0324 if isnumeric(varargin{1})
0325 BPMxList = varargin{1};
0326 varargin(1) = [];
0327 end
0328 end
0329 elseif isnumeric(varargin{1})
0330 BPMxList = varargin{1};
0331 varargin(1) = [];
0332 end
0333 end
0334 if isempty(BPMxList)
0335 BPMxList = family2dev(BPMxFamily, 1);
0336 end
0337
0338
0339 if length(varargin) >= 1
0340 if isstruct(varargin{1})
0341 BPMyFamily = varargin{1}.FamilyName;
0342 BPMyList = varargin{1}.DeviceList;
0343 varargin(1) = [];
0344 if ~NumericOutputFlag
0345 StructOutputFlag = 1;
0346 end
0347 elseif ischar(varargin{1})
0348 BPMyFamily = varargin{1};
0349 varargin(1) = [];
0350 if length(varargin) >= 1
0351 if isnumeric(varargin{1})
0352 BPMyList = varargin{1};
0353 varargin(1) = [];
0354 end
0355 end
0356 elseif isnumeric(varargin{1})
0357 BPMyList = varargin{1};
0358 varargin(1) = [];
0359 end
0360 end
0361 if isempty(BPMyList)
0362 BPMyList = family2dev(BPMyFamily, 1);
0363 end
0364
0365
0366 if length(varargin) >= 1
0367 if isstruct(varargin{1})
0368 HCMFamily = varargin{1}.FamilyName;
0369 HCMList = varargin{1}.DeviceList;
0370 varargin(1) = [];
0371 if ~NumericOutputFlag
0372 StructOutputFlag = 1;
0373 end
0374 elseif ischar(varargin{1})
0375 HCMFamily = varargin{1};
0376 varargin(1) = [];
0377 if length(varargin) >= 1
0378 if isnumeric(varargin{1})
0379 HCMList = varargin{1};
0380 varargin(1) = [];
0381 end
0382 end
0383 elseif isnumeric(varargin{1})
0384 HCMList = varargin{1};
0385 varargin(1) = [];
0386 end
0387 end
0388 if isempty(HCMList)
0389 HCMList = family2dev(HCMFamily, 1);
0390 end
0391
0392
0393 if length(varargin) >= 1
0394 if isstruct(varargin{1})
0395 VCMFamily = varargin{1}.FamilyName;
0396 VCMList = varargin{1}.DeviceList;
0397 varargin(1) = [];
0398 if ~NumericOutputFlag
0399 StructOutputFlag = 1;
0400 end
0401 elseif ischar(varargin{1})
0402 VCMFamily = varargin{1};
0403 varargin(1) = [];
0404 if length(varargin) >= 1
0405 if isnumeric(varargin{1})
0406 VCMList = varargin{1};
0407 varargin(1) = [];
0408 end
0409 end
0410 elseif isnumeric(varargin{1})
0411 VCMList = varargin{1};
0412 varargin(1) = [];
0413 end
0414 end
0415 if isempty(VCMList)
0416 VCMList = family2dev(VCMFamily, 1);
0417 end
0418
0419
0420 if length(varargin) >= 1
0421 if isempty(varargin{1})
0422
0423 varargin(1) = [];
0424 elseif isnumeric(varargin{1})
0425 HCMKicks = varargin{1};
0426 varargin(1) = [];
0427 end
0428 end
0429
0430
0431 if length(varargin) >= 1
0432 if isempty(varargin{1})
0433
0434 varargin(1) = [];
0435 elseif isnumeric(varargin{1})
0436 VCMKicks = varargin{1};
0437 varargin(1) = [];
0438 end
0439 end
0440
0441
0442
0443
0444
0445
0446
0447
0448
0449
0450
0451
0452
0453 if ~strcmpi(ModulationMethod, 'unipolar') && ~strcmpi(ModulationMethod, 'bipolar')
0454 error('ModulationMethod must be ''unipolar'' or ''bipolar''');
0455 end
0456
0457
0458
0459 if length(varargin) >= 1
0460 if isempty(varargin{1})
0461
0462 varargin(1) = [];
0463 end
0464 if isnumeric(varargin{1})
0465 WaitFlag = varargin{1};
0466 varargin(1) = [];
0467 end
0468 end
0469
0470
0471 if length(varargin) >= 1
0472 if isempty(varargin{1})
0473
0474 FileName = '';
0475 varargin(1) = [];
0476 end
0477 if ischar(varargin{1})
0478 FileName = varargin{1};
0479 varargin(1) = [];
0480 end
0481 end
0482 if length(varargin) >= 1
0483 if isempty(varargin{1})
0484
0485 DirectoryName = getfamilydata('Directory', 'BPMResponse');
0486 FileName = [DirectoryName, FileName];
0487 varargin(1) = [];
0488 elseif ischar(varargin{1})
0489 DirectoryName = varargin{1};
0490 if strcmp(DirectoryName, filesep)
0491 FileName = [DirectoryName, FileName];
0492 else
0493 FileName = [DirectoryName, filesep, FileName];
0494 end
0495 varargin(1) = [];
0496 end
0497 end
0498
0499
0500 if length(varargin) >= 1
0501 if isempty(varargin{1})
0502
0503 varargin(1) = [];
0504 end
0505 if isnumeric(varargin{1})
0506 ExtraDelay = varargin{1};
0507 varargin(1) = [];
0508 end
0509 end
0510
0511
0512 if isempty(UnitsFlag)
0513 if strcmpi(getfamilydata(BPMxFamily,'Monitor','Units'), getfamilydata(BPMyFamily,'Monitor','Units'))
0514 UnitsFlag = getfamilydata(BPMxFamily,'Monitor','Units');
0515 else
0516 error('Mixed Units for orbits');
0517 end
0518 end
0519 if isempty(UnitsFlag)
0520 error('Unknown Units');
0521 end
0522
0523
0524 if isempty(ModeFlag)
0525 if strcmpi(getfamilydata(BPMxFamily,'Monitor','Mode'), getfamilydata(BPMyFamily,'Monitor','Mode'))
0526
0527 else
0528 error('Mixed Mode for orbits');
0529 end
0530 if strcmpi(getfamilydata(HCMFamily,'Monitor','Mode'), getfamilydata(VCMFamily,'Monitor','Mode'))
0531 ModeFlag = getfamilydata(HCMFamily,'Monitor','Mode');
0532 else
0533 error('Mixed Mode for correctors');
0534 end
0535 end
0536 if isempty(ModeFlag)
0537 error('Unknown Mode');
0538 end
0539
0540
0541
0542
0543 TimeStart = gettime;
0544
0545
0546
0547 if strcmpi(ModeFlag,'Model')
0548
0549 if ischar(FileName) || ArchiveFlag == 1
0550 ArchiveFlag = 1;
0551 else
0552 ArchiveFlag = 0;
0553 end
0554
0555
0556 if DisplayFlag == 1
0557
0558 else
0559 DisplayFlag = 0;
0560 end
0561 else
0562
0563 if ArchiveFlag ~= 0
0564 ArchiveFlag = 1;
0565 if FileName == -1
0566 FileName = '';
0567 end
0568 end
0569 end
0570
0571
0572
0573
0574
0575
0576
0577
0578
0579
0580
0581
0582
0583
0584
0585
0586
0587
0588
0589
0590 if ArchiveFlag
0591 if isempty(FileName)
0592 FileName = appendtimestamp(getfamilydata('Default', 'BPMRespFile'));
0593 DirectoryName = getfamilydata('Directory', 'BPMResponse');
0594 if isempty(DirectoryName)
0595 DirectoryName = [getfamilydata('Directory','DataRoot'), 'Response', filesep, 'BPM', filesep];
0596 else
0597
0598 DirStart = pwd;
0599 [DirectoryName, ErrorFlag] = gotodirectory(DirectoryName);
0600 cd(DirStart);
0601 end
0602 [FileName, DirectoryName] = uiputfile('*.mat', 'Select a BPM Response File ("Save" starts measurement)', [DirectoryName FileName]);
0603 drawnow;
0604 if FileName == 0
0605 ArchiveFlag = 0;
0606 disp(' BPM response measurement canceled.');
0607 Rmat = []; OutputFileName='';
0608 return
0609 end
0610 FileName = [DirectoryName, FileName];
0611 elseif FileName == -1
0612 FileName = appendtimestamp(getfamilydata('Default', 'BPMRespFile'));
0613 DirectoryName = getfamilydata('Directory', 'BPMResponse');
0614 if isempty(DirectoryName)
0615 DirectoryName = [getfamilydata('Directory','DataRoot'), 'Response', filesep, 'BPM', filesep];
0616 end
0617 FileName = [DirectoryName, FileName];
0618 end
0619
0620
0621 MachineConfig = getmachineconfig(InputFlags{:});
0622 end
0623
0624
0625
0626 if strcmpi(ModeFlag,'Model')
0627
0628
0629
0630
0631
0632
0633 if isfamily(HCMFamily)
0634 tmp = getsp(HCMFamily, HCMList);
0635 end
0636
0637
0638
0639
0640
0641
0642
0643
0644
0645
0646
0647
0648
0649
0650
0651
0652
0653
0654
0655
0656
0657
0658
0659
0660 global THERING
0661
0662 RINGData.Lattice = THERING;
0663 iCavity = findcells(THERING, 'Frequency');
0664 if isempty(iCavity)
0665 if isstoragering
0666 RINGData.CavityFrequency = getrf('Model', 'Physics');
0667 RINGData.CavityHarmNumber = getfamilydata('HarmonicNumber');
0668 else
0669 RINGData.CavityFrequency = [];
0670 RINGData.CavityHarmNumber = [];
0671 end
0672 else
0673 RINGData.CavityFrequency = THERING{iCavity(1)}.Frequency;
0674 RINGData.CavityHarmNumber = THERING{iCavity(1)}.HarmNumber;
0675 end
0676
0677
0678
0679
0680
0681 if strcmpi(BPMxFamily,'All')
0682 BPMxATIndex = 1:length(THERING);
0683 elseif isfamily(BPMxFamily)
0684 BPMxATIndex = family2atindex(BPMxFamily, BPMxList);
0685 else
0686 BPMxATIndex = findcells(THERING, 'FamName', BPMxFamily);
0687 end
0688 if isempty(BPMxATIndex)
0689 error(sprintf('BPMxFamily=%s could not be found in the AO or AT deck', BPMxFamily));
0690 else
0691 BPMxATIndex = BPMxATIndex(:)';
0692 end
0693
0694 if strcmpi(BPMyFamily,'All')
0695 BPMyATIndex = 1:length(THERING);
0696 elseif isfamily(BPMyFamily)
0697 BPMyATIndex = family2atindex(BPMyFamily, BPMyList);
0698 else
0699 BPMyATIndex = findcells(THERING, 'FamName', BPMyFamily);
0700 end
0701 if isempty(BPMyATIndex)
0702 error(sprintf('BPMyFamily=%s could not be found in the AO or AT deck', BPMyFamily));
0703 else
0704 BPMyATIndex = BPMyATIndex(:)';
0705 end
0706
0707 BPMData.BPMIndex = unique([BPMxATIndex BPMyATIndex]);
0708 BPMData.HBPMIndex = findrowindex(BPMxATIndex', BPMData.BPMIndex');
0709 BPMData.VBPMIndex = findrowindex(BPMyATIndex', BPMData.BPMIndex');
0710
0711
0712
0713
0714
0715
0716
0717
0718
0719
0720
0721
0722 if strcmpi(HCMFamily,'All')
0723 ATIndex = 1:length(THERING);
0724 elseif isfamily(HCMFamily)
0725 ATIndex = family2atindex(HCMFamily, HCMList);
0726 else
0727 ATIndex = findcells(THERING, 'FamName', HCMFamily);
0728 end
0729 if isempty(ATIndex)
0730 error(sprintf('HCMFamily=%s could not be found in the middle layer or AT model', HCMFamily));
0731 else
0732 ATIndex = ATIndex(:)';
0733 end
0734 CMData.HCMIndex = ATIndex;
0735
0736 if strcmpi(VCMFamily,'All')
0737 ATIndex = 1:length(THERING);
0738 elseif isfamily(VCMFamily)
0739 ATIndex = family2atindex(VCMFamily, VCMList);
0740 else
0741 ATIndex = findcells(THERING, 'FamName', VCMFamily);
0742 end
0743 if isempty(ATIndex)
0744 error(sprintf('VCMFamily=%s could not be found in the middle layer or AT model', VCMFamily));
0745 else
0746 ATIndex = ATIndex(:)';
0747 end
0748 CMData.VCMIndex = ATIndex;
0749
0750
0751
0752
0753 if ismemberof(HCMFamily,'COR')
0754 if isempty(HCMKicks)
0755 HCMKicks = getfamilydata(HCMFamily, 'Setpoint', 'DeltaRespMat', HCMList);
0756 if isempty(HCMKicks)
0757 CMData.HCMKicks = Default2HCMKick;
0758 else
0759
0760 HCMsp = getsp(HCMFamily, HCMList, 'Numeric', ModeFlag, 'Hardware');
0761 CMData.HCMKicks = hw2physics(HCMFamily, 'Setpoint', HCMsp+HCMKicks, HCMList) - hw2physics(HCMFamily, 'Setpoint', HCMsp, HCMList);
0762
0763
0764
0765 end
0766 else
0767 if strcmpi(UnitsFlag, 'Hardware')
0768
0769 HCMsp = getsp(HCMFamily, HCMList, 'Numeric', ModeFlag, 'Hardware');
0770 CMData.HCMKicks = hw2physics(HCMFamily, 'Setpoint', HCMsp+HCMKicks, HCMList) - hw2physics(HCMFamily, 'Setpoint', HCMsp, HCMList);
0771 else
0772 CMData.HCMKicks = HCMKicks;
0773 end
0774 end
0775 else
0776
0777 if isempty(HCMKicks)
0778 CMData.HCMKicks = Default2HCMKick;
0779 else
0780
0781 if strcmpi(UnitsFlag, 'Hardware')
0782 fprintf('\n You are using a non-corrector magnet actuator type and using hardware units.\n');
0783 fprintf(' Unknown conversion method from hardware to physics.\n\n');
0784 fprintf(' Change to a physics units input scheme.\n');
0785 error('Hardware to physics conversion error');
0786 else
0787 CMData.HCMKicks = HCMKicks;
0788 end
0789 end
0790 end
0791
0792 if ismemberof(VCMFamily,'COR')
0793 if isempty(VCMKicks)
0794 VCMKicks = getfamilydata(VCMFamily, 'Setpoint', 'DeltaRespMat', VCMList);
0795 if isempty(VCMKicks)
0796 CMData.VCMKicks = Default2VCMKick;
0797 else
0798
0799 VCMsp = getsp(VCMFamily, VCMList, 'Numeric', ModeFlag, 'Hardware');
0800 CMData.VCMKicks = hw2physics(VCMFamily, 'Setpoint', VCMsp+VCMKicks, VCMList) - hw2physics(VCMFamily, 'Setpoint', VCMsp, VCMList);
0801
0802
0803
0804 end
0805 else
0806 if strcmpi(UnitsFlag, 'Hardware')
0807
0808 VCMsp = getsp(VCMFamily, VCMList, 'Numeric', ModeFlag, 'Hardware');
0809 CMData.VCMKicks = hw2physics(VCMFamily, 'Setpoint', VCMsp+VCMKicks, VCMList) - hw2physics(VCMFamily, 'Setpoint', VCMsp, VCMList);
0810 else
0811 CMData.VCMKicks = VCMKicks;
0812 end
0813 end
0814 else
0815
0816 if isempty(VCMKicks)
0817 CMData.VCMKicks = Default2VCMKick;
0818 else
0819
0820 if strcmpi(UnitsFlag, 'Hardware')
0821 fprintf('\n You are using a non-corrector magnet actuator type and using hardware units.\n');
0822 fprintf(' Unknown conversion method from hardware to physics!\n\n');
0823 fprintf(' Change to a physics units input scheme.\n');
0824 error('Hardware to physics conversion error');
0825 else
0826 CMData.VCMKicks = VCMKicks;
0827 end
0828 end
0829 end
0830
0831 CMData.HCMKicks = CMData.HCMKicks(:);
0832 if length(CMData.HCMKicks) == 1
0833 CMData.HCMKicks = CMData.HCMKicks * ones(length(CMData.HCMIndex),1);
0834 end
0835 CMData.VCMKicks = CMData.VCMKicks(:);
0836 if length(CMData.VCMKicks) == 1
0837 CMData.VCMKicks = CMData.VCMKicks * ones(length(CMData.VCMIndex),1);
0838 end
0839
0840
0841
0842
0843 for i = 1:length(CMData.HCMIndex)
0844 if isfield(THERING{CMData.HCMIndex(i)}, 'Roll')
0845 Roll = THERING{CMData.HCMIndex(i)}.Roll;
0846 else
0847 Roll = [0 0];
0848 end
0849 HCMRoll(i,1) = Roll(1);
0850 end
0851
0852
0853 for i = 1:length(CMData.VCMIndex)
0854 if isfield(THERING{CMData.VCMIndex(i)}, 'Roll')
0855 Roll = THERING{CMData.VCMIndex(i)}.Roll;
0856 else
0857 Roll = [0 0];
0858 end
0859 VCMRoll(i,1) = Roll(2);
0860 end
0861
0862
0863
0864 HCMKicks = CMData.HCMKicks;
0865 VCMKicks = CMData.VCMKicks;
0866 CMData.HCMKicks = CMData.HCMKicks .* cos(HCMRoll);
0867 CMData.VCMKicks = CMData.VCMKicks .* cos(VCMRoll);
0868
0869
0870
0871
0872 CMData.HCMCoupling = sin(HCMRoll) ./ cos(HCMRoll);
0873 CMData.VCMCoupling = -sin(VCMRoll) ./ cos(VCMRoll);
0874
0875
0876
0877
0878 R0 = locoresponsematrix(RINGData, BPMData, CMData, LOCORespFlags);
0879
0880
0881
0882
0883
0884
0885
0886
0887
0888
0889
0890
0891
0892
0893
0894
0895
0896
0897
0898
0899
0900
0901
0902
0903
0904
0905 NBPM = length(BPMData.BPMIndex);
0906 for i = 1:NBPM
0907 if isfield(THERING{BPMData.BPMIndex(i)}, 'GCR')
0908 GCR = THERING{BPMData.BPMIndex(i)}.GCR;
0909 else
0910 GCR = [1 1 0 0];
0911 end
0912
0913
0914 Crunch = GCR(3);
0915 Roll = GCR(4);
0916
0917 a(i,1) = ( Crunch * sin(Roll) + cos(Roll)) / sqrt(1 - Crunch^2);
0918 b(i,1) = (-Crunch * cos(Roll) + sin(Roll)) / sqrt(1 - Crunch^2);
0919 c(i,1) = (-Crunch * cos(Roll) - sin(Roll)) / sqrt(1 - Crunch^2);
0920 d(i,1) = (-Crunch * sin(Roll) + cos(Roll)) / sqrt(1 - Crunch^2);
0921
0922
0923
0924
0925
0926
0927
0928
0929 end
0930
0931
0932 C = [diag(a) diag(b)
0933 diag(c) diag(d)];
0934
0935
0936 R0 = C * R0;
0937
0938
0939
0940 Rmat(1,1).Data = R0( BPMData.HBPMIndex , 1:length(CMData.HCMIndex));
0941 Rmat(2,1).Data = R0(length(BPMData.BPMIndex)+BPMData.VBPMIndex , 1:length(CMData.HCMIndex));
0942 Rmat(1,2).Data = R0( BPMData.HBPMIndex , length(CMData.HCMIndex)+(1:length(CMData.VCMIndex)));
0943 Rmat(2,2).Data = R0(length(BPMData.BPMIndex)+BPMData.VBPMIndex , length(CMData.HCMIndex)+(1:length(CMData.VCMIndex)));
0944
0945
0946
0947 for i = 1:length(CMData.HCMKicks)
0948 Rmat(1,1).Data(:,i) = Rmat(1,1).Data(:,i) / HCMKicks(i);
0949 Rmat(2,1).Data(:,i) = Rmat(2,1).Data(:,i) / HCMKicks(i);
0950 end
0951
0952 for i = 1:length(CMData.VCMKicks)
0953 Rmat(1,2).Data(:,i) = Rmat(1,2).Data(:,i) / VCMKicks(i);
0954 Rmat(2,2).Data(:,i) = Rmat(2,2).Data(:,i) / VCMKicks(i);
0955 end
0956
0957
0958
0959
0960 if ismemberof(BPMxFamily,'BPM') && ismemberof(BPMyFamily,'BPM')
0961
0962 Xat = getam(BPMxFamily, BPMxList, 'Physics');
0963 Yat = getam(BPMyFamily, BPMyList, 'Physics');
0964 else
0965
0966 [Xat, Yat, Sx, Sy] = modeltwiss('ClosedOrbit', BPMxFamily, BPMxList, BPMyFamily, BPMyList);
0967 end
0968 X.Data = Xat(:);
0969 Y.Data = Yat(:);
0970 X.FamilyName = BPMxFamily;
0971 Y.FamilyName = BPMyFamily;
0972 X.Field = 'Monitor';
0973 Y.Field = 'Monitor';
0974 X.DeviceList = BPMxList;
0975 Y.DeviceList = BPMyList;
0976 X.Status = ones(length(Xat),1);
0977 Y.Status = ones(length(Yat),1);
0978 X.Mode = 'Model';
0979 Y.Mode = 'Model';
0980 X.t = 0;
0981 Y.t = 0;
0982 X.tout = 0;
0983 Y.tout = 0;
0984 X.TimeStamp = clock;
0985 Y.TimeStamp = X.TimeStamp;
0986 X.Units = 'Physics';
0987 Y.Units = 'Physics';
0988 X.UnitsString = 'm';
0989 Y.UnitsString = 'm';
0990 X.DataDescriptor = 'Horizontal Orbit';
0991 Y.DataDescriptor = 'Vertical Orbit';
0992 X.CreatedBy = 'getpv';
0993 Y.CreatedBy = 'getpv';
0994
0995 HCMsp = getsp(HCMFamily, HCMList, 'Struct', ModeFlag, 'Physics');
0996 VCMsp = getsp(VCMFamily, VCMList, 'Struct', ModeFlag, 'Physics');
0997 Rmat(1,1).Monitor = X;
0998 Rmat(1,1).Actuator = HCMsp;
0999 Rmat(1,2).Monitor = X;
1000 Rmat(1,2).Actuator = VCMsp;
1001 Rmat(2,1).Monitor = Y;
1002 Rmat(2,1).Actuator = HCMsp;
1003 Rmat(2,2).Monitor = Y;
1004 Rmat(2,2).Actuator = VCMsp;
1005
1006 Rmat(1,1).ActuatorDelta = HCMKicks;
1007 Rmat(2,1).ActuatorDelta = HCMKicks;
1008 Rmat(1,2).ActuatorDelta = VCMKicks;
1009 Rmat(2,2).ActuatorDelta = VCMKicks;
1010
1011 for i = 1:2
1012 for j = 1:2
1013 Rmat(i,j).GeV = getenergymodel;
1014 Rmat(i,j).TimeStamp = X.TimeStamp;
1015 Rmat(i,j).DCCT = [];
1016 Rmat(i,j).ModulationMethod = ModulationMethod;
1017 Rmat(i,j).WaitFlag = WaitFlag;
1018 Rmat(i,j).ExtraDelay = ExtraDelay;
1019 Rmat(i,j).Units = 'Physics';
1020 Rmat(i,j).UnitsString = [Rmat(1,1).Monitor.UnitsString, '/', Rmat(1,1).Actuator.UnitsString];
1021 Rmat(i,j).DataDescriptor = 'Response Matrix';
1022 Rmat(i,j).CreatedBy = 'measbpmresp';
1023 Rmat(i,j).OperationalMode = getfamilydata('OperationalMode');
1024 end
1025 end
1026
1027 if strcmpi(UnitsFlag, 'Hardware')
1028
1029 if ismemberof(BPMxFamily,'BPM') && ismemberof(BPMyFamily,'BPM') && ismemberof(HCMFamily,'COR') && ismemberof(VCMFamily,'COR')
1030 Rmat = physics2hw(Rmat, getenergymodel);
1031 else
1032 fprintf('\n You are asking for hardware units, but a nonstandard monitor and/or\n');
1033 fprintf(' actuator was used. The response matrix will stay in physics units!\n\n');
1034 end
1035 end
1036
1037 else
1038
1039
1040
1041
1042
1043
1044 if isempty(HCMKicks)
1045 HCMKicks = getfamilydata(HCMFamily, 'Setpoint', 'DeltaRespMat', HCMList);
1046 KickUnits = 'Hardware';
1047 if isempty(HCMKicks)
1048 HCMKicks = Default2HCMKick;
1049 KickUnits = 'Physics';
1050 end
1051 if isempty(KickUnits)
1052 error('Units for the kick strength are unknown. Try inputing units directly.');
1053 end
1054 if strcmpi(UnitsFlag, 'Physics') && strcmpi(KickUnits, 'Hardware')
1055 HCMsp = getsp(HCMFamily, HCMList, 'Numeric', ModeFlag, 'Hardware');
1056 HCMKicks = hw2physics(HCMFamily, 'Setpoint', HCMsp+HCMKicks, HCMList) - hw2physics(HCMFamily, 'Setpoint', HCMsp, HCMList);
1057 elseif strcmpi(UnitsFlag, 'Hardware') && strcmpi(KickUnits, 'Physics')
1058 HCMsp = getsp(HCMFamily, HCMList, 'Numeric', ModeFlag, 'Physics');
1059 HCMKicks = physics2hw(HCMFamily, 'Setpoint', HCMsp+HCMKicks, HCMList) - physics2hw(HCMFamily, 'Setpoint', HCMsp, HCMList);
1060 end
1061 end
1062 if isempty(VCMKicks)
1063 VCMKicks = getfamilydata(VCMFamily, 'Setpoint', 'DeltaRespMat', VCMList);
1064 KickUnits = 'Hardware';
1065 if isempty(VCMKicks)
1066 VCMKicks = Default2VCMKick;
1067 KickUnits = 'Physics';
1068 end
1069 if isempty(KickUnits)
1070 error('Units for the kick strength are unknown. Try inputing units directly.');
1071 end
1072 if strcmpi(UnitsFlag, 'Physics') && strcmpi(KickUnits, 'Hardware')
1073 VCMsp = getsp(VCMFamily, VCMList, 'Numeric', ModeFlag, 'Hardware');
1074 VCMKicks = hw2physics(VCMFamily, 'Setpoint', VCMsp+VCMKicks, VCMList) - hw2physics(VCMFamily, 'Setpoint', VCMsp, VCMList);
1075 elseif strcmpi(UnitsFlag, 'Hardware') && strcmpi(KickUnits, 'Physics')
1076 VCMsp = getsp(VCMFamily, VCMList, 'Numeric', ModeFlag, 'Physics');
1077 VCMKicks = physics2hw(VCMFamily, 'Setpoint', VCMsp+VCMKicks, VCMList) - physics2hw(VCMFamily, 'Setpoint', VCMsp, VCMList);
1078 end
1079 end
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092 BPMxStatus = getfamilydata(BPMxFamily,'Status', BPMxList);
1093 BPMxGoodIndex = find(BPMxStatus);
1094 BPMxList = BPMxList(BPMxGoodIndex,:);
1095
1096 BPMyStatus = getfamilydata(BPMyFamily,'Status', BPMyList);
1097 BPMyGoodIndex = find(BPMyStatus);
1098 BPMyList = BPMyList(BPMyGoodIndex,:);
1099
1100 HCMStatus = getfamilydata(HCMFamily,'Status', HCMList);
1101 HCMGoodIndex = find(HCMStatus);
1102 HCMList = HCMList(HCMGoodIndex,:);
1103 if length(HCMKicks) == 1
1104 HCMKicks = HCMKicks * ones(length(HCMGoodIndex),1);
1105 else
1106 HCMKicks = HCMKicks(HCMGoodIndex);
1107 end
1108
1109 VCMStatus = getfamilydata(VCMFamily,'Status', VCMList);
1110 VCMGoodIndex = find(VCMStatus);
1111 VCMList = VCMList(VCMGoodIndex,:);
1112 if length(VCMKicks) == 1
1113 VCMKicks = VCMKicks * ones(length(VCMGoodIndex),1);
1114 else
1115 VCMKicks = VCMKicks(VCMGoodIndex);
1116 end
1117
1118
1119
1120 if DisplayFlag
1121 fprintf(' Begin BPM response matrix measurement\n');
1122 end
1123
1124
1125
1126
1127
1128 Rcell = measrespmat({BPMxFamily,BPMyFamily}, {BPMxList,BPMyList}, {HCMFamily,VCMFamily}, {HCMList,VCMList}, {HCMKicks,VCMKicks}, 'Struct', ModulationMethod, WaitFlag, ExtraDelay, InputFlags{:}, DCCTFlag{:});
1129
1130 if DisplayFlag
1131 fprintf(' Measurement complete.\n\n');
1132 end
1133
1134
1135
1136
1137
1138
1139
1140 for i = 1:size(Rcell,1)
1141 for j = 1:size(Rcell,2)
1142 if istransport
1143
1144 for k = 1:size(Rcell{i,j}.Data,2)
1145 CMpos = getspos(Rcell{i,j}.Actuator.FamilyName, Rcell{i,j}.Actuator.DeviceList(k,:));
1146 BPMpos = getspos(Rcell{i,j}.Monitor);
1147 iUpStream = find(BPMpos < CMpos);
1148 if ~isempty(iUpStream)
1149 Rcell{i,j}.Data(iUpStream,k) = 0;
1150 end
1151 end
1152 end
1153 Rmat(i,j) = Rcell{i,j};
1154 end
1155 end
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183 end
1184
1185
1186 if ArchiveFlag || ischar(FileName)
1187 [DirectoryName, FileName, Ext] = fileparts(FileName);
1188 DirStart = pwd;
1189 [DirectoryName, ErrorFlag] = gotodirectory(DirectoryName);
1190 if ErrorFlag
1191 fprintf('\n There was a problem getting to the proper directory!\n\n');
1192 end
1193 save(FileName, 'Rmat','MachineConfig');
1194 cd(DirStart);
1195 OutputFileName = [DirectoryName, FileName, '.mat'];
1196
1197 if DisplayFlag
1198 fprintf(' BPM response matrix data structure ''Rmat'' saved to disk\n');
1199 fprintf(' Filename: %s\n', OutputFileName);
1200 fprintf(' The total response matrix measurement time was %.2f minutes.\n', (gettime-TimeStart)/60);
1201 end
1202 else
1203 OutputFileName = '';
1204 end
1205
1206
1207 if ~StructOutputFlag
1208
1209
1210
1211 RmatData = [];
1212 for i = 1:size(Rmat,1)
1213 Rrow = [];
1214 for j = 1:size(Rmat,2)
1215 Rrow = [Rrow Rmat(i,j).Data];
1216 end
1217 RmatData = [RmatData; Rrow];
1218 end
1219
1220 Rmat = RmatData;
1221 end