source: MML/trunk/applications/loco/setlocodata_example.m @ 4

Last change on this file since 4 was 4, checked in by zhangj, 10 years ago

Initial import--MML version from SOLEIL@2013

File size: 13.8 KB
Line 
1function setlocodata(CommandInput, FileName)
2%SETLOCODATA - Applies the LOCO output data to the the accelerator, MML setup, and AT model
3%  setlocodata(CommandInput, FileName)
4%
5%  INPUTS
6%  1. CommandInput
7%     'Nominal'    - Sets nominal gains (1) / rolls (0) to the model.
8%     'SetGains'   - Set gains/coupling from a LOCO file.
9%     'Symmetrize' - Symmetry correction of the lattice based on a LOCO file.
10%     'CorrectCoupling' - Coupling correction of the lattice based on a LOCO file.
11%                         Option to add and dispersion wave and bump.
12%     'SetModel'   - Set the model from a LOCO file.  But it only changes
13%                    the part of the model that does not get corrected
14%                    in 'Symmetrize' (also does a SetGains).
15%     'LOCO2Model' - Set the model from a LOCO file (also does a 'SetGains').
16%                    This sets all lattice machines fit in the LOCO run to the model.
17%  2. FileName - LOCO file name {Default: getfamilydata('OpsData', 'LOCOFile')}
18%                '' to browse for a file
19%
20%  NOTES
21%  How one uses this function depends on how LOCO was setup.
22%  1. Use setlocodata('Nominal') if no model calibration information is known.
23%  2. The most typical situation is to apply:
24%         setlocodata('Symmetrize') to the accelerator
25%         setlocodata('SetModel')   to the middle layer (usually done in setoperationalmode)
26%  3. If a LOCO run was done on the present lattice with no changes made to lattice
27%     after LOCO run, then setting all the LOCO fits to the model makes sense.
28%         setlocodata('LOCO2Model')
29%  4. This function obviously has machine dependent parts.
30
31%  Written by Greg Portmann
32
33
34global THERING
35
36if nargin < 1
37    CommandInput = '';
38end
39
40if isempty(CommandInput)
41    %CommandInput = 'Default';
42    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'};
43    [ModeNumber, OKFlag] = listdlg('Name',getfamilydata('Machine'),'PromptString', ...
44        'Select the proper set LOCO data command:', ...
45        'SelectionMode','single', 'ListString', ModeCell, 'ListSize', [500 200]);
46    if OKFlag ~= 1
47        fprintf('   setlocodata cancelled\n');
48        return
49    end
50    if ModeNumber == 1
51        CommandInput = 'Nominal';
52    elseif ModeNumber == 2
53        CommandInput = 'SetGains';
54    elseif ModeNumber == 3
55        CommandInput = 'Symmetrize';
56    elseif ModeNumber == 4
57        CommandInput = 'CorrectCoupling';
58    elseif ModeNumber == 5
59        CommandInput = 'SetModel';
60    elseif ModeNumber == 6
61        CommandInput = 'LOCO2Model';
62    elseif ModeNumber == 7
63        help setlocodata;
64        return
65    end
66end
67
68
69if nargin < 2
70    if any(strcmpi(CommandInput,{'SetGains','SetModel'}))
71        % Default (Golden) LOCO file (if empty, the user will be prompted)
72        FileName = getfamilydata('OpsData','LOCOFile');
73    else
74        FileName = '';
75    end
76end
77
78
79% Default families
80HBPMFamily = gethbpmfamily;
81VBPMFamily = getvbpmfamily;
82HCMFamily  = gethcmfamily;
83VCMFamily  = getvcmfamily;
84
85
86% Device list
87HBPMDeviceList      = family2dev(HBPMFamily);
88HBPMDeviceListTotal = family2dev(HBPMFamily,0);
89VBPMDeviceList      = family2dev(VBPMFamily);
90VBPMDeviceListTotal = family2dev(VBPMFamily,0);
91
92HCMDeviceList      = family2dev(HCMFamily);
93HCMDeviceListTotal = family2dev(HCMFamily,0);
94VCMDeviceList      = family2dev(VCMFamily);
95VCMDeviceListTotal = family2dev(VCMFamily,0);
96
97switch CommandInput
98    case 'Nominal'
99        fprintf('   Using nominal BPM and corrector gains (1) and rolls (0).\n');
100
101        % To speed things up, put Gains/Rolls/etc in the AO
102        AO = getao;
103
104        % Zero or one the gains and rolls
105        AO.(HBPMFamily).Gain   =  ones(size(HBPMDeviceListTotal,1),1);
106        AO.(VBPMFamily).Gain   =  ones(size(VBPMDeviceListTotal,1),1);
107        AO.(HBPMFamily).Roll   = zeros(size(HBPMDeviceListTotal,1),1);
108        AO.(VBPMFamily).Roll   = zeros(size(VBPMDeviceListTotal,1),1);
109        AO.(HBPMFamily).Crunch = zeros(size(HBPMDeviceListTotal,1),1);
110        AO.(VBPMFamily).Crunch = zeros(size(VBPMDeviceListTotal,1),1);
111
112        % Set the gain, roll, & crunch to the AT model to be used by getpvmodel, setpvmodel, etc
113        setatfield(HBPMFamily, 'GCR', [AO.(HBPMFamily).Gain AO.(VBPMFamily).Gain AO.(HBPMFamily).Crunch AO.(HBPMFamily).Roll], HBPMDeviceListTotal);
114
115
116        % Corrector magnet gains/rolls
117        AO.(HCMFamily).Gain =  ones(size(HCMDeviceListTotal,1),1);
118        AO.(VCMFamily).Gain =  ones(size(VCMDeviceListTotal,1),1);
119        AO.(HCMFamily).Roll = zeros(size(HCMDeviceListTotal,1),1);
120        AO.(VCMFamily).Roll = zeros(size(VCMDeviceListTotal,1),1);
121
122        % Set the AT model as well since they are used by getpvmodel, setpvmodel, etc
123        % Make sure the Roll field is 1x2 even for single plane correctors
124        % First set the cross planes to zero
125        setatfield(HCMFamily, 'Roll', 0*AO.(HCMFamily).Roll, HCMDeviceListTotal, 1, 2);
126        setatfield(VCMFamily, 'Roll', 0*AO.(VCMFamily).Roll, VCMDeviceListTotal, 1, 1);
127
128        % Then set the roll field
129        setatfield(HCMFamily, 'Roll', AO.(HCMFamily).Roll, HCMDeviceListTotal, 1, 1);
130        setatfield(VCMFamily, 'Roll', AO.(VCMFamily).Roll, VCMDeviceListTotal, 1, 2);
131
132        setao(AO);
133
134
135    case 'SetGains'
136
137        if isempty(FileName) || strcmp(FileName, '.')
138            if isempty(FileName)
139                [FileName, DirectoryName] = uigetfile('*.mat', 'LOCO Output File Name?', [getfamilydata('Directory','DataRoot'), 'LOCO', filesep]);
140            else
141                [FileName, DirectoryName] = uigetfile('*.mat', 'LOCO Output File Name?');
142            end
143            drawnow;
144            if FileName == 0
145                fprintf('   setlocodata canceled\n');
146                return
147            end
148            FileName = [DirectoryName FileName];
149        end
150
151        % Set the model gains
152        setlocodata('Nominal');
153
154        AO = getao;
155
156        % Load the LOCO data
157        fprintf('   Setting BPM and corrector gains and rolls based on %s.\n', FileName);
158        load(FileName);
159
160
161        % Get the device list from the LOCO file
162        try
163            HBPMDeviceList = LocoMeasData.HBPM.DeviceList;
164            VBPMDeviceList = LocoMeasData.VBPM.DeviceList;
165            HCMDeviceList  = LocoMeasData.HCM.DeviceList;
166            VCMDeviceList  = LocoMeasData.VCM.DeviceList;
167        catch
168            % Legacy
169            HBPMDeviceList = LocoMeasData.(HBPMFamily).DeviceList;
170            VBPMDeviceList = LocoMeasData.(VBPMFamily).DeviceList;
171            HCMDeviceList  = LocoMeasData.(HCMFamily).DeviceList;
172            VCMDeviceList  = LocoMeasData.(VCMFamily).DeviceList;
173        end
174
175
176        % Change to Gain, Roll, Crunch system (Need to add a logic for single view BPMs???)
177        i = findrowindex(HBPMDeviceList, HBPMDeviceListTotal);
178        for j = 1:length(BPMData(end).HBPMGain)
179            MLOCO = [BPMData(end).HBPMGain(j)     BPMData(end).HBPMCoupling(j)
180                     BPMData(end).VBPMCoupling(j) BPMData(end).VBPMGain(j) ];
181
182            [AO.(HBPMFamily).Gain(i(j),:), AO.(VBPMFamily).Gain(i(j),:), AO.(HBPMFamily).Crunch(i(j),:), AO.(HBPMFamily).Roll(i(j),:)] = loco2gcr(MLOCO);
183        end
184        AO.(VBPMFamily).Roll   = AO.(HBPMFamily).Roll;
185        AO.(VBPMFamily).Crunch = AO.(HBPMFamily).Crunch;
186
187        if ~isreal(AO.(HBPMFamily).Gain)
188            error('Horizontal BPM gain is complex.');
189        end
190        if ~isreal(AO.(VBPMFamily).Gain)
191            error('Vertical BPM gain is complex.');
192        end
193        if ~isreal(AO.(HBPMFamily).Crunch)
194            error('BPM Crunch is complex.');
195        end
196        if ~isreal(AO.(HBPMFamily).Roll)
197            error('BPM roll is complex.');
198        end
199
200       
201        %%%%%%%%%%%%%%
202        % Correctors %
203        %%%%%%%%%%%%%%
204
205        % Kick strength (LOCO is in milliradian)
206        % LOCO is run with the original gain in hw2physics (stored in LocoMeasData.VCMGain/LocoMeasData.HCMGain).
207        % The new gain must combine the new CM gain and the one used in buildlocoinput.
208        % hw2physics:  Rad = G * amps   (original)
209        % LOCO gain:   Gloco = KickNew/KickStart
210        % New hw2physics gain: Gloco * G
211
212        % HCM
213        i = findrowindex(HCMDeviceList, HCMDeviceListTotal);
214
215        HCMGainOldLOCO  = LocoMeasData.HCMGain .* cos(LocoMeasData.HCMRoll);
216        HCMGainLOCO     = HCMGainOldLOCO .* CMData(end).HCMKicks ./ CMData(1).HCMKicks;
217        HCMCouplingLOCO = HCMGainLOCO    .* CMData(end).HCMCoupling;
218
219        %AO.(HCMFamily).Roll(i) = atan2(-HCMCouplingLOCO, HCMGainLOCO);
220        AO.(HCMFamily).Roll(i) = atan(HCMCouplingLOCO ./ abs(HCMGainLOCO));
221        AO.(HCMFamily).Gain(i) = sign(HCMGainLOCO) .* sqrt(HCMCouplingLOCO.^2 + HCMGainLOCO.^2);
222
223        % VCM
224        i = findrowindex(VCMDeviceList, VCMDeviceListTotal);
225
226        VCMGainOldLOCO  = LocoMeasData.VCMGain .* cos(LocoMeasData.VCMRoll);
227        VCMGainLOCO     = VCMGainOldLOCO .* CMData(end).VCMKicks ./ CMData(1).VCMKicks;
228        VCMCouplingLOCO = VCMGainLOCO .* CMData(end).VCMCoupling;
229
230        %AO.(VCMFamily).Roll(i) = atan2(-VCMCouplingLOCO, VCMGainLOCO);
231        AO.(VCMFamily).Roll(i) = atan(-VCMCouplingLOCO ./ abs(VCMGainLOCO));
232        AO.(VCMFamily).Gain(i) = sign(VCMGainLOCO) .* sqrt(VCMCouplingLOCO.^2 + VCMGainLOCO.^2);
233
234
235        % Set the roll, crunch to the AT model to be used by getpvmodel, setpvmodel, etc
236        setatfield(HBPMFamily, 'GCR', [AO.(HBPMFamily).Gain AO.(VBPMFamily).Gain AO.(HBPMFamily).Crunch AO.(HBPMFamily).Roll], HBPMDeviceListTotal);
237
238        % Set the gains to the AT model to be used by getpvmodel, setpvmodel, etc
239        % Make sure the Roll field is 1x2 even for single plane correctors
240
241        % First set the cross planes to zero
242        setatfield(HCMFamily, 'Roll', 0*AO.(HCMFamily).Roll, HCMDeviceListTotal, 1, 2);
243        setatfield(VCMFamily, 'Roll', 0*AO.(VCMFamily).Roll, VCMDeviceListTotal, 1, 1);
244
245        % Then set the roll field
246        setatfield(HCMFamily, 'Roll', AO.(HCMFamily).Roll, HCMDeviceListTotal, 1, 1);
247        setatfield(VCMFamily, 'Roll', AO.(VCMFamily).Roll, VCMDeviceListTotal, 1, 2);
248
249        % If other magnet fits were done (like roll), it should be add to the AT model as well
250
251        setao(AO);
252
253
254    case 'SetModel'
255
256        % Some LOCO errors are applied to the accelerator 'SetMachine' and some
257        % go to the model.  If errors detected by LOCO are not applied to the accelerator,
258        % then include them in the AT and Middle Layer model.
259
260        % The assumption is that setlocodata('SetMachine') has already been run.
261        % So QF, QD, QFA, QFB have been changed in the accelerator to match the LOCO run.
262
263        if isempty(FileName) || strcmp(FileName, '.')
264            if isempty(FileName)
265                [FileName, DirectoryName] = uigetfile('*.mat', 'LOCO Output File Name?', [getfamilydata('Directory','DataRoot'), 'LOCO', filesep]);
266            else
267                [FileName, DirectoryName] = uigetfile('*.mat', 'LOCO Output File Name?');
268            end
269            drawnow;
270            if FileName == 0
271                fprintf('   setlocodata canceled\n');
272                return
273            end
274            FileName = [DirectoryName FileName];
275        end
276
277
278        % Load the LOCO data
279        load(FileName);
280
281
282        % Set the model gains
283        setlocodata('SetGains', FileName);
284
285
286        % Sextupoles (set to the values in the machine save since they are not fit)
287        if all(LocoMeasData.MachineConfig.SF.Setpoint.Data == 0)
288            fprintf('   Setting SF to zero in the AT model.\n');
289            setsp('SF', 0, 'Physics', 'Model');
290        else
291            %setpv(LocoMeasData.MachineConfig.SF.Setpoint, 'Model');
292        end
293        if all(LocoMeasData.MachineConfig.SD.Setpoint.Data == 0)
294            fprintf('   Setting SD to zero in the AT model.\n');
295            setsp('SD', 0, 'Physics', 'Model');
296        else
297            %setpv(LocoMeasData.MachineConfig.SD.Setpoint, 'Model');
298        end
299
300
301    case 'LOCO2Model'
302
303        % LOCO is usually used to correct the model.  If the LOCO fit parameters are
304        % not applied to the accelerator, then the entire model needs to be updated.
305        % Ie, the machine lattice file is the same as it was when the LOCO data was
306        % taken, then put the LOCO output settings in the model.
307
308        if isempty(FileName) || strcmp(FileName, '.')
309            if isempty(FileName)
310                [FileName, DirectoryName] = uigetfile('*.mat', 'LOCO Output File Name?', [getfamilydata('Directory','DataRoot'), 'LOCO', filesep]);
311            else
312                [FileName, DirectoryName] = uigetfile('*.mat', 'LOCO Output File Name?');
313            end
314            drawnow;
315            if FileName == 0
316                fprintf('   setlocodata canceled\n');
317                return
318            end
319            FileName = [DirectoryName FileName];
320        end
321
322
323        % Load the LOCO data
324        load(FileName);
325
326
327        % Use loco file for the lattice and the fit parameter
328        % Using the loco lattice may not be what you want???
329        global THERING
330        %RINGData.Lattice = THERING;
331        for i = 1:length(FitParameters(end).Params)
332            RINGData = locosetlatticeparam(RINGData, FitParameters(end).Params{i}, FitParameters(end).Values(i));
333        end
334        THERING = RINGData.Lattice;
335
336
337        % Since the lattice may have changed
338        updateatindex;
339
340
341        % Set the model gains (this added GCR field to lattice)
342        setlocodata('SetGains', FileName);
343
344
345    case 'Symmetrize'
346
347        error('   Function not complete.  Look at the ALS setlocodata for an example.');
348
349
350    case 'CorrectCoupling'
351
352        error('   Function not complete.  Look at the ALS setlocodata for an example.');
353
354    otherwise
355        error('   setlocodata command unknown.');
356end
357
358
359
Note: See TracBrowser for help on using the repository browser.