Home > mml > at > setlocodata.m

setlocodata

PURPOSE ^

SETLOCODATA - Applies the LOCO calibration data to both the middle layer & the accelerator

SYNOPSIS ^

function setlocodata(CommandInput, FileName)

DESCRIPTION ^

SETLOCODATA - Applies the LOCO calibration data to both the middle layer & the accelerator
  setlocodata(CommandInput, FileName)

  INPUTS
  1. CommandInput
     'Nominal'    - Sets nominal gains (1) / rolls (0) to the model.
     'SetGains'   - Set gains/coupling from a LOCO file.
     'Symmetrize' - Symmetry correction of the lattice based on a LOCO file.
     'CorrectCoupling' - Coupling correction of the lattice based on a LOCO file.
     'SetModel'   - Set the model from a LOCO file.  But it only changes
                    the part of the model that does not get corrected
                    in 'Symmetrize' (also does a SetGains).
     'LOCO2Model' - Set the model from a LOCO file (also does a 'SetGains').
                    This sets all lattice machines fit in the LOCO run to the model.
     'EtaWave'    - Set a dispersion wave.
  2. FileName - LOCO file name {Default: getfamilydata('OpsData', 'LOCOFile')}
                '' to browse for a file

  NOTES
  How one uses this function depends on how LOCO was setup.
  1. Use setlocodata('Nominal') if no model calibration information is known.
  2. The most typical situation is to apply:
         setlocodata('Symmetrize') to the accelerator
         setlocodata('SetModel')   to the middle layer (usually done in setoperationalmode)
  3. If a LOCO run was done on the present lattice with no changes made to lattice
     after LOCO run, then setting all the LOCO fits to the model makes sense.
         setlocodata('LOCO2Model')
  4. This function obviously has machine dependent parts.

  Written by Greg Portmann

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function setlocodata(CommandInput, FileName)
0002 %SETLOCODATA - Applies the LOCO calibration data to both the middle layer & the accelerator
0003 %  setlocodata(CommandInput, FileName)
0004 %
0005 %  INPUTS
0006 %  1. CommandInput
0007 %     'Nominal'    - Sets nominal gains (1) / rolls (0) to the model.
0008 %     'SetGains'   - Set gains/coupling from a LOCO file.
0009 %     'Symmetrize' - Symmetry correction of the lattice based on a LOCO file.
0010 %     'CorrectCoupling' - Coupling correction of the lattice based on a LOCO file.
0011 %     'SetModel'   - Set the model from a LOCO file.  But it only changes
0012 %                    the part of the model that does not get corrected
0013 %                    in 'Symmetrize' (also does a SetGains).
0014 %     'LOCO2Model' - Set the model from a LOCO file (also does a 'SetGains').
0015 %                    This sets all lattice machines fit in the LOCO run to the model.
0016 %     'EtaWave'    - Set a dispersion wave.
0017 %  2. FileName - LOCO file name {Default: getfamilydata('OpsData', 'LOCOFile')}
0018 %                '' to browse for a file
0019 %
0020 %  NOTES
0021 %  How one uses this function depends on how LOCO was setup.
0022 %  1. Use setlocodata('Nominal') if no model calibration information is known.
0023 %  2. The most typical situation is to apply:
0024 %         setlocodata('Symmetrize') to the accelerator
0025 %         setlocodata('SetModel')   to the middle layer (usually done in setoperationalmode)
0026 %  3. If a LOCO run was done on the present lattice with no changes made to lattice
0027 %     after LOCO run, then setting all the LOCO fits to the model makes sense.
0028 %         setlocodata('LOCO2Model')
0029 %  4. This function obviously has machine dependent parts.
0030 %
0031 %  Written by Greg Portmann
0032 
0033 
0034 global THERING
0035 
0036 if nargin < 1
0037     %CommandInput = 'Default';
0038     ModeCell = {'Nominal - Set Gain=1 & Rolls=0 in the model', 'SetGains - Set gains/rolls from a LOCO file','Symmetrize - Symmetry correction of the lattice', 'CorrectCoupling - Coupling correction of the lattice', 'SetModel - Set the model from a LOCO file','LOCO2Model - Set the model from a LOCO file (also does a SetGains)', 'see "help setlocodata" for more details'};
0039     [ModeNumber, OKFlag] = listdlg('Name','Soleil','PromptString', ...
0040         'Select the proper set LOCO data command:', ...
0041         'SelectionMode','single', 'ListString', ModeCell, 'ListSize', [500 200]);
0042     if OKFlag ~= 1
0043         fprintf('   setlocodata cancelled\n');
0044         return
0045     end
0046     if ModeNumber == 1
0047         CommandInput = 'Nominal';
0048     elseif ModeNumber == 2
0049         CommandInput = 'SetGains';
0050     elseif ModeNumber == 3
0051         CommandInput = 'Symmetrize';
0052     elseif ModeNumber == 4
0053         CommandInput = 'CorrectCoupling';
0054     elseif ModeNumber == 5
0055         CommandInput = 'SetModel';
0056     elseif ModeNumber == 6
0057         CommandInput = 'LOCO2Model';
0058     elseif ModeNumber == 7
0059         help setlocodata;
0060         return
0061     end
0062 end
0063 
0064 
0065 if nargin < 2
0066     % Default (Golden) LOCO file
0067     % If empty, the user will be prompted if needed.
0068     FileName = getfamilydata('OpsData','LOCOFile');
0069 end
0070 
0071 
0072 % Device list
0073 BPMxDeviceList = family2dev('BPMx');
0074 BPMxDeviceListTotal = family2dev('BPMx',0);
0075 
0076 BPMzDeviceList = family2dev('BPMz');
0077 BPMzDeviceListTotal = family2dev('BPMz',0);
0078 
0079 HCORDeviceList = family2dev('HCOR');
0080 HCORDeviceListTotal = family2dev('HCOR',0);
0081 VCORDeviceList = family2dev('VCOR');
0082 VCORDeviceListTotal = family2dev('VCOR',0);
0083 
0084 
0085 if any(strcmpi(CommandInput, 'Nominal'))
0086     fprintf('   Using nominal BPM and corrector Gain=1 and Roll=0\n');
0087 
0088     % To speed things up, put Gains/Rolls/etc in the AO
0089     AO = getao;
0090 
0091     % Zero or one the gains and rolls
0092     AO.BPMx.Gain = ones(size(BPMxDeviceListTotal,1),1);
0093     AO.BPMz.Gain = ones(size(BPMzDeviceListTotal,1),1);
0094     AO.BPMx.Roll = zeros(size(BPMxDeviceListTotal,1),1);
0095     AO.BPMz.Roll = zeros(size(BPMzDeviceListTotal,1),1);
0096     AO.BPMx.Crunch = zeros(size(BPMxDeviceListTotal,1),1);
0097     AO.BPMz.Crunch = zeros(size(BPMzDeviceListTotal,1),1);
0098 
0099     AO.HCOR.Gain = ones(size(HCORDeviceListTotal,1),1);
0100     AO.VCOR.Gain = ones(size(VCORDeviceListTotal,1),1);
0101     AO.HCOR.Roll = zeros(size(HCORDeviceListTotal,1),1);
0102     AO.VCOR.Roll = zeros(size(VCORDeviceListTotal,1),1);
0103 
0104 
0105     % Set the roll, crunch to the AT model to be used by getpvmodel, setpvmodel, etc
0106     setatfield('BPMx', 'GCR', [AO.BPMx.Gain AO.BPMz.Gain AO.BPMx.Crunch AO.BPMx.Roll], BPMxDeviceListTotal);
0107 
0108     % Set the gains to the AT model to be used by getpvmodel, setpvmodel, etc
0109     % Make sure the Roll field is 1x2 even for single plane correctors
0110 
0111     % First set the cross planes to zero
0112     setatfield('HCOR', 'Roll', 0*AO.HCOR.Roll, HCORDeviceListTotal, 1, 2);
0113     setatfield('VCOR', 'Roll', 0*AO.VCOR.Roll, VCORDeviceListTotal, 1, 1);
0114 
0115     % Then set the roll field
0116     setatfield('HCOR', 'Roll', AO.HCOR.Roll, HCORDeviceListTotal, 1, 1);
0117     setatfield('VCOR', 'Roll', AO.VCOR.Roll, VCORDeviceListTotal, 1, 2);
0118 
0119     setao(AO);
0120 
0121 
0122 elseif any(strcmpi(CommandInput, 'SetGains'))
0123 
0124     if isempty(FileName) || strcmp(FileName, '.')
0125         if isempty(FileName)
0126             [FileName, DirectoryName] = uigetfile('*.mat', 'LOCO Output File Name?', [getfamilydata('Directory','DataRoot'), 'LOCO', filesep]);
0127         else
0128             [FileName, DirectoryName] = uigetfile('*.mat', 'LOCO Output File Name?');
0129         end
0130         drawnow;
0131         if isequal(FileName,0) || isequal(DirectoryName,0)
0132             fprintf('   setlocodata canceled\n');
0133             return
0134         end
0135         FileName = [DirectoryName FileName];
0136     end
0137 
0138     % Set the model gains
0139     setlocodata('Nominal');
0140 
0141     AO = getao;
0142 
0143     % Load the LOCO data
0144     fprintf('   Setting BPM and corrector gains and rolls based on %s.\n', FileName);
0145     load(FileName);
0146 
0147 
0148     % Get the device list from the LOCO file
0149     try
0150         BPMxDeviceList = LocoMeasData.HBPM.DeviceList;
0151         BPMzDeviceList = LocoMeasData.VBPM.DeviceList;
0152         HCORDeviceList  = LocoMeasData.HCOR.DeviceList;
0153         VCORDeviceList  = LocoMeasData.VCOR.DeviceList;
0154     catch
0155         % Legacy
0156         BPMxDeviceList = LocoMeasData.(BPMxFamily).DeviceList;
0157         BPMzDeviceList = LocoMeasData.(BPMzFamily).DeviceList;
0158         HCORDeviceList  = LocoMeasData.(HCORFamily).DeviceList;
0159         VCORDeviceList  = LocoMeasData.(VCORFamily).DeviceList;
0160     end
0161 
0162 
0163     % Change to Gain, Roll, Crunch system (Need to add a logic for single view BPMs???)
0164     i = findrowindex(BPMxDeviceList, BPMxDeviceListTotal);
0165     for j = 1:length(BPMData(end).HBPMGain)
0166         MLOCO = [BPMData(end).HBPMGain(j)     BPMData(end).HBPMCoupling(j)
0167             BPMData(end).VBPMCoupling(j) BPMData(end).VBPMGain(j) ];
0168 
0169         [AO.(BPMxFamily).Gain(i(j),:), AO.(BPMzFamily).Gain(i(j),:), AO.(BPMxFamily).Crunch(i(j),:), AO.(BPMxFamily).Roll(i(j),:)] = loco2gcr(MLOCO);
0170     end
0171     AO.(BPMzFamily).Roll   = AO.(BPMxFamily).Roll;
0172     AO.(BPMzFamily).Crunch = AO.(BPMxFamily).Crunch;
0173 
0174     if ~isreal(AO.(BPMxFamily).Gain)
0175         error('Horizontal BPM gain is complex.');
0176     end
0177     if ~isreal(AO.(BPMzFamily).Gain)
0178         error('Vertical BPM gain is complex.');
0179     end
0180     if ~isreal(AO.(BPMxFamily).Crunch)
0181         error('BPM Crunch is complex.');
0182     end
0183     if ~isreal(AO.(BPMxFamily).Roll)
0184         error('BPM roll is complex.');
0185     end
0186 
0187 
0188 
0189     %%%%%%%%%%%%%%
0190     % Correctors %
0191     %%%%%%%%%%%%%%
0192 
0193     % Kick strength (LOCO is in milliradian)
0194     % LOCO is run with the original gain in hw2physics (stored in LocoMeasData.VCORGain/LocoMeasData.HCORGain).
0195     % The new gain must combine the new CM gain and the one used in buildlocoinput.
0196     % hw2physics:  Rad = G * amps   (original)
0197     % LOCO gain:   Gloco = KickNew/KickStart
0198     % New hw2physics gain: Gloco * G
0199 
0200     % HCOR
0201     i = findrowindex(HCORDeviceList, HCORDeviceListTotal);
0202 
0203     HCORGainOldLOCO  = LocoMeasData.HCORGain .* cos(LocoMeasData.HCORRoll);
0204     HCORGainLOCO     = HCORGainOldLOCO .* CMData(end).HCORKicks ./ CMData(1).HCORKicks;
0205     HCORCouplingLOCO = HCORGainLOCO .* CMData(end).HCORCoupling;
0206 
0207     %AO.(HCORFamily).Roll(i) = atan2(-HCORCouplingLOCO, HCORGainLOCO);
0208     AO.(HCORFamily).Roll(i) = atan(HCORCouplingLOCO ./ abs(HCORGainLOCO));
0209     AO.(HCORFamily).Gain(i) = sign(HCORGainLOCO) .* sqrt(HCORCouplingLOCO.^2 + HCORGainLOCO.^2);
0210 
0211 
0212     % VCOR
0213     i = findrowindex(VCORDeviceList, VCORDeviceListTotal);
0214 
0215     VCORGainOldLOCO  = LocoMeasData.VCORGain .* cos(LocoMeasData.VCORRoll);
0216     VCORGainLOCO     = VCORGainOldLOCO .* CMData(end).VCORKicks ./ CMData(1).VCORKicks;
0217     VCORCouplingLOCO = VCORGainLOCO .* CMData(end).VCORCoupling;
0218 
0219     %AO.(VCORFamily).Roll(i) = atan2(-VCORCouplingLOCO, VCORGainLOCO);
0220     AO.(VCORFamily).Roll(i) = atan(-VCORCouplingLOCO ./ abs(VCORGainLOCO));
0221     AO.(VCORFamily).Gain(i) = sign(VCORGainLOCO) .* sqrt(VCORCouplingLOCO.^2 + VCORGainLOCO.^2);
0222 
0223 
0224     % Set the roll, crunch to the AT model to be used by getpvmodel, setpvmodel, etc
0225     setatfield(BPMxFamily, 'GCR', [AO.(BPMxFamily).Gain AO.(BPMzFamily).Gain AO.(BPMxFamily).Crunch AO.(BPMxFamily).Roll], BPMxDeviceListTotal);
0226 
0227     % Set the gains to the AT model to be used by getpvmodel, setpvmodel, etc
0228     % Make sure the Roll field is 1x2 even for single plane correctors
0229 
0230     % First set the cross planes to zero
0231     setatfield(HCORFamily, 'Roll', 0*AO.(HCORFamily).Roll, HCORDeviceListTotal, 1, 2);
0232     setatfield(VCORFamily, 'Roll', 0*AO.(VCORFamily).Roll, VCORDeviceListTotal, 1, 1);
0233 
0234     % Then set the roll field
0235     setatfield(HCORFamily, 'Roll', AO.(HCORFamily).Roll, HCORDeviceListTotal, 1, 1);
0236     setatfield(VCORFamily, 'Roll', AO.(VCORFamily).Roll, VCORDeviceListTotal, 1, 2);
0237 
0238 
0239     % If other magnet fits were done (like roll), it should be add to the AT model as well
0240 
0241     setao(AO);
0242 
0243 
0244 elseif any(strcmpi(CommandInput, 'SetModel'))
0245 
0246     error('   Function not complete.  Look at the ALS setlocodata for an example.');
0247 
0248 elseif any(strcmpi(CommandInput, 'SetMachine'))
0249 
0250     if isempty(FileName) || strcmp(FileName, '.')
0251         if isempty(FileName)
0252             [FileName, DirectoryName] = uigetfile('*.mat', 'LOCO Output File Name?', [getfamilydata('Directory','DataRoot'), 'LOCO', filesep]);
0253         else
0254             [FileName, DirectoryName] = uigetfile('*.mat', 'LOCO Output File Name?');
0255         end
0256         drawnow;
0257         if isequal(FileName,0) || isequal(DirectoryName,0)
0258             fprintf('   setlocodata canceled\n');
0259             return
0260         end
0261         FileName = [DirectoryName FileName];
0262     end
0263 
0264     % Load the LOCO data
0265     load(FileName);
0266 
0267 
0268     
0269     error('You need to edit here with your parameter setting program!')
0270     
0271     le = length(FitParameters);
0272     
0273     QFscale(1:6) = FitParameters(1).Values(1)/FitParameters(le).Values(1);
0274     QFscale(7:28) = FitParameters(1).Values(2:23)./FitParameters(le).Values(2:23);
0275     QDscale(1:6) = FitParameters(1).Values(24)/FitParameters(le).Values(24);
0276     QDscale(7:28) = FitParameters(1).Values(25:46)./FitParameters(le).Values(25:46);
0277     QFCscale = FitParameters(1).Values(47)/FitParameters(le).Values(47);
0278     QDXscale = FitParameters(1).Values(48)/FitParameters(le).Values(48);
0279     QFXscale = FitParameters(1).Values(49)/FitParameters(le).Values(49);
0280     QDYscale = FitParameters(1).Values(50)/FitParameters(le).Values(50);
0281     QFYscale = FitParameters(1).Values(51)/FitParameters(le).Values(51);
0282     QDZscale = FitParameters(1).Values(52)/FitParameters(le).Values(52);
0283     QFZscale = FitParameters(1).Values(53)/FitParameters(le).Values(53);
0284 
0285     QFnew  = QFscale'.*getsp('QF');  % I would base it on the LOCO data setpoint!!!
0286     QDnew  = QDscale'.*getsp('QD');
0287     QFCnew = QFCscale*getsp('QFC');
0288     QDXnew = QDXscale*getsp('QDX');
0289     QFXnew = QFXscale*getsp('QFX');
0290     QDYnew = QDYscale*getsp('QDY');
0291     QFYnew = QFYscale*getsp('QFY');
0292     QDZnew = QDZscale*getsp('QDZ');
0293     QFZnew = QFZscale*getsp('QFZ');
0294 
0295 
0296     setsp('QF', QFnew);
0297     setsp('QD', QDnew);
0298     setsp('QFC', QFCnew);
0299 
0300     setsp('QFX', QFXnew);
0301     setsp('QFY', QFYnew);
0302     setsp('QFZ', QFZnew);
0303 
0304     setsp('QDX', QDXnew);
0305     setsp('QDY', QDYnew);
0306     setsp('QDZ', QDZnew);
0307 
0308 
0309 elseif any(strcmpi(CommandInput, 'CorrectCoupling'))
0310 
0311     if isempty(FileName) || strcmp(FileName, '.')
0312         if isempty(FileName)
0313             [FileName, DirectoryName] = uigetfile('*.mat', 'LOCO Output File Name?', [getfamilydata('Directory','DataRoot'), 'LOCO', filesep]);
0314         else
0315             [FileName, DirectoryName] = uigetfile('*.mat', 'LOCO Output File Name?');
0316         end
0317         drawnow;
0318         if isequal(FileName,0) || isequal(DirectoryName,0)
0319             fprintf('   setlocodata canceled\n');
0320             return
0321         end
0322         FileName = [DirectoryName FileName];
0323     end
0324 
0325     
0326     % Load the LOCO data
0327     load(FileName);
0328 
0329     NQT = size(family2dev('QT'),1); 
0330     fprintf('   Correcting the coupling based on LOCO file %s.\n', FileName);
0331     fprintf('   It is assumed that the QT are in FitParameters.Values(end-%d:end)\n', NQT-1);
0332 
0333     QT = FitParameters(end).Values(end-(NQT-1):end);
0334 
0335     % Starting point for the skew quadrupoles when LOCO data was measured
0336     MachineConfig = LocoMeasData.MachineConfig;
0337     setpv(MachineConfig.QT.Setpoint);
0338 
0339     QThw = physics2hw('QT', 'Setpoint', -QT);
0340 
0341     % Maximum setpoint check
0342     if any(abs(MachineConfig.QT.Setpoint.Data+QThw)>maxsp('QT'))
0343         error('At least one of the QT would go beyond it''s limit ... aborting coupling correction');
0344     end
0345 
0346     % Make the setpoint change
0347     stepsp('QT', QThw);
0348 
0349     % Keep the change?
0350     CorrectFlag = questdlg('Keep the new skew quadrupole setpoints or return to the old values?','SETLOCOGAINS(''CorrectCoupling'')','Keep this lattice','Restore Old Lattice','Keep this lattice');
0351     if strcmpi(CorrectFlag, 'Restore Old Lattice') || isempty(CorrectFlag)
0352         fprintf('   Changing the skew quadrupole magnets back to the original setpoints.\n');
0353         stepsp('QT', QThw);
0354     end
0355 
0356 
0357 elseif any(strcmpi(CommandInput, 'LOCO2Model'))
0358 
0359     % LOCO is usually used to correct the model.  If the LOCO fit parameters are
0360     % not applied to the accelerator, then the entire model needs to be updated.
0361     % Ie, the machine lattice file is the same as it was when the LOCO data was
0362     % taken, then put the LOCO output settings in the model.
0363 
0364     if isempty(FileName) || strcmp(FileName, '.')
0365         if isempty(FileName)
0366             [FileName, DirectoryName] = uigetfile('*.mat', 'LOCO Output File Name?', [getfamilydata('Directory','DataRoot'), 'LOCO', filesep]);
0367         else
0368             [FileName, DirectoryName] = uigetfile('*.mat', 'LOCO Output File Name?');
0369         end
0370         drawnow;
0371         if isequal(FileName,0) || isequal(DirectoryName,0)
0372             fprintf('   setlocodata canceled\n');
0373             return
0374         end
0375         FileName = [DirectoryName FileName];
0376     end
0377 
0378 
0379     % Load the LOCO data
0380     load(FileName);
0381 
0382 
0383     % Use loco file for the lattice and the fit parameter
0384     % Using everything in the loco lattice may not be what you want!
0385     global THERING
0386     %RINGData.Lattice = THERING;
0387     for i = 1:length(FitParameters(end).Params)
0388         RINGData = locosetlatticeparam(RINGData, FitParameters(end).Params{i}, FitParameters(end).Values(i));
0389     end
0390     THERING = RINGData.Lattice;
0391 
0392 
0393     % Since the lattice may have changed
0394     %updateatindex;
0395 
0396 
0397     % Set the model gains (this added GCR field to lattice)
0398     setlocodata('SetGains', FileName);
0399 
0400 else
0401 
0402     error('   ');
0403 
0404 end

Generated on Mon 21-May-2007 15:29:18 by m2html © 2003