source: MML/trunk/machine/SOLEIL/StorageRing/loco/changes/buildlocofitparameters_new.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: 11.5 KB
Line 
1function [LocoMeasData, BPMData, CMData, RINGData, FitParameters, LocoFlags] = buildlocofitparameters(FileName)
2%BUILDLOCOFITPARAMETERS - SOLEIL LOCO fit parameters
3%
4%  [LocoMeasData, BPMData, CMData, RINGData, FitParameters, LocoFlags] = buildlocoinput(FileName)
5
6%
7%% Written By Laurent S. Nadolski
8
9%%%%%%%%%%%%%%
10% Input file %
11%%%%%%%%%%%%%%
12if nargin == 0
13    [FileName, DirectoryName, FilterIndex] = uigetfile('*.mat','Select a LOCO input file');
14    if FilterIndex == 0
15        return;
16    end
17    FileName = [DirectoryName, FileName];
18end
19
20load(FileName);
21
22%%%%%%%%%%%%%%%%%%%%%%
23% Remove bad devices %
24%%%%%%%%%%%%%%%%%%%%%%
25RemoveHCMDeviceList = [];
26RemoveVCMDeviceList = [];
27
28RemoveHBPMDeviceList = [];
29RemoveVBPMDeviceList = [];
30
31%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
32% This function only works on the first iteration %
33%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34if exist('BPMData','var') && length(BPMData)>1
35    BPMData = BPMData(1);
36end
37if exist('CMData','var') && length(CMData)>1
38    CMData = CMData(1);
39end
40if exist('FitParameters','var') && length(FitParameters)>1
41    FitParameters = FitParameters(1);
42end
43if exist('LocoFlags','var') && length(LocoFlags)>1
44    LocoFlags = LocoFlags(1);
45end
46if exist('LocoModel','var') && length(LocoModel)>1
47    LocoModel = LocoModel(1);
48end
49
50%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
51% Make sure the start point in loaded in the AT model %
52%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53if ~isempty(FitParameters)
54    for i = 1:length(FitParameters.Params)
55        RINGData = locosetlatticeparam(RINGData, FitParameters.Params{i}, FitParameters.Values(i));
56    end
57end
58
59
60%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61% LocoFlags to change from the default %
62%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63% LocoFlags.Threshold = 1e-5;
64% LocoFlags.OutlierFactor = 10;
65% LocoFlags.SVmethod = 1e-2;
66LocoFlags.SVmethod = 1:895;
67% LocoFlags.HorizontalDispersionWeight = 12.5;
68% LocoFlags.VerticalDispersionWeight = 12.5;
69% LocoFlags.AutoCorrectDelta = 'Yes';
70% LocoFlags.Coupling = 'No';
71% LocoFlags.Dispersion = 'No';
72% LocoFlags.Normalize = 'Yes';
73% LocoFlags.ResponseMatrixCalculatorFlag1 = 'Linear';
74% LocoFlags.ResponseMatrixCalculatorFlag2 = 'FixedPathLength';
75% LocoFlags.CalculateSigma = 'No';
76% LocoFlags.SinglePrecision = 'Yes';
77
78% CMData.FitKicks    = 'Yes';
79% CMData.FitCoupling = 'No';
80%
81% BPMData.FitGains    = 'Yes';
82% BPMData.FitCoupling = 'No';
83
84
85% Corrector magnets to disable
86j = findrowindex(RemoveHCMDeviceList, LocoMeasData.HCM.DeviceList);
87if ~isempty(j)
88    irm = findrowindex(j(:),CMData.HCMGoodDataIndex(:));
89    CMData.HCMGoodDataIndex(irm) = [];
90end
91
92j = findrowindex(RemoveVCMDeviceList, LocoMeasData.VCM.DeviceList);
93if ~isempty(j)
94    irm = findrowindex(j(:),CMData.VCMGoodDataIndex(:));
95    CMData.VCMGoodDataIndex(irm) = [];
96end
97
98
99% BPMs to disable
100j = findrowindex(RemoveHBPMDeviceList, LocoMeasData.HBPM.DeviceList);
101if ~isempty(j)
102    irm = findrowindex(j(:),BPMData.HBPMGoodDataIndex(:));
103    BPMData.HBPMGoodDataIndex(irm) = [];
104end
105
106j = findrowindex(RemoveVBPMDeviceList, LocoMeasData.VBPM.DeviceList);
107if ~isempty(j)
108    irm = findrowindex(j(:),BPMData.VBPMGoodDataIndex(:));
109    BPMData.VBPMGoodDataIndex(irm) = [];
110end
111
112%%%%%%%%%%%%%%%%%
113% FitParameters %
114%%%%%%%%%%%%%%%%%
115
116% Individual magnets
117% For each parameter which is fit in the model a numerical response matrix
118% gradient needs to be determined.  The FitParameters data structure determines what
119% parameter in the model get varied and how are they grouped.  For no parameter fits, set
120% FitParameters.Params to an empty vector.
121%     FitParameters.Params = parameter group definition (cell array for AT)
122%     FitParameters.Values = Starting value for the parameter fit
123%     FitParameters.Deltas = change in parameter value used to compute the gradient (NaN forces loco to choose, see auto-correct delta flag below)
124%     FitParameters.FitRFFrequency = ('Yes'/'No') Fit the RF frequency?
125%     FitParameters.DeltaRF = Change in RF frequency when measuring "dispersion".
126%                             If the RF frequency is being fit the output is stored here.
127%
128% The FitParameters structure also contains the standard deviations of the fits:
129%     LocoValuesSTD
130%     FitParameters.DeltaRFSTD
131%
132% Note: FitParameters.DeltaRF is used when including dispersion in the response matrix.
133%       LocoMeasData.DeltaRF is not used directly in loco.  Usually one would set
134%       FitParameters.DeltaRF = LocoMeasData.DeltaRF as a starting point for the RF frequency.
135
136
137ModeCell = {'Fit by family', 'Fit magnets by power supply', 'Fit selected quad'};
138[ButtonName, OKFlag] = listdlg('Name','BUILDLOCOINPUT','PromptString',{'Fit Parameter Selection:','(Not including skew quadrupoles)'}, 'SelectionMode','single', 'ListString', ModeCell, 'ListSize', [350 125], 'InitialValue', 2);
139if OKFlag ~= 1
140    ButtonName = 1;
141end
142drawnow;
143FitParameters = [];
144n = 0;
145switch ButtonName
146    case 1  % By family
147        global THERING; % TODO remove not clean at all
148
149        n = 0;
150        FitParameters.Deltas = [];
151        FitParameters.Values = [];
152
153        for k=1:10,
154            % Q1 K-values
155            n = n+1;
156            QI = findcells(THERING,'FamName',['Q' num2str(k)]);
157            FitParameters.Params{n} = mkparamgroup(THERING, QI, 'K');
158            FitParameters.Values = [FitParameters.Values; getcellstruct(THERING, 'K', QI(1))];
159            FitParameters.Deltas = [FitParameters.Deltas; NaN];
160        end
161
162
163    case 2  % Fit by power supply
164
165        n = 0;
166        FitParameters.Deltas = [];
167        FitParameters.Values = [];
168
169        global THERING; % TODO remove not clean at all
170        % K-values
171        for k=1:10,
172            QI = findcells(THERING,'FamName',['Q' num2str(k)]);
173            for loop=1:length(QI)
174                n = n + 1;
175                FitParameters.Params{n} = mkparamgroup(THERING, QI(loop), 'K');
176            end
177            FitParameters.Values = [FitParameters.Values; getcellstruct(THERING, 'K', QI)];
178            FitParameters.Deltas = [FitParameters.Deltas; NaN * ones(length(QI),1)];
179        end
180
181    case 3  % Fit by power supply less than 160
182
183        n = 0;
184        FitParameters.Deltas = [];
185        FitParameters.Values = [];
186
187        % K-values
188        for k= [1 2 4 7 9],
189            QI = findcells(THERING,'FamName',['Q' num2str(k)]);
190            for loop=1:length(QI)
191                n = n + 1;
192                FitParameters.Params{n} = mkparamgroup(THERING, QI(loop), 'K');
193            end
194            FitParameters.Values = [FitParameters.Values; getcellstruct(THERING, 'K', QI)];
195            FitParameters.Deltas = [FitParameters.Deltas; NaN * ones(length(QI),1)];
196        end
197
198    otherwise
199end
200
201%%%%%%%%%%%%%%%%%%%%%%%
202% Skew quadrupole fits
203%%%%%%%%%%%%%%%%%%%%%%%
204
205ModeCell = {'Fit at Sextupoles', 'Fit By Power Supply', 'Do Not Fit Skew Quadrupoles'};
206[ButtonName, OKFlag] = listdlg('Name','BUILDLOCOINPUT','PromptString','Skew Quadrupole Fits?', 'SelectionMode','single', 'ListString', ModeCell, 'ListSize', [350 125], 'InitialValue', 2);
207if OKFlag ~= 1
208    ButtonName = length(ModeCell);  % Default
209end
210
211switch ButtonName
212    case 1
213        fprintf('   Skew fit at Sextupoles needs work.\n\n');
214        %     case 1
215        %         % Fit skew quadrupoles in the sextupoles
216        %         SFI = findcells(RINGData.Lattice,'FamName','SF');
217        %         SFn = size(family2dev('SF'),1);
218        %
219        %         for loop = 1:length(SFI)
220        %             if length(SFI) == SFn
221        %                 n = n + 1;
222        %                 FitParameters.Params{n} = mkparamgroup(RINGData.Lattice,SFI(loop),'s');
223        %             elseif length(SFI) == 2*SFn
224        %                 % Split sextupoles
225        %                 N1 = 2*loop-1;
226        %                 N2 = 2*loop;
227        %                 n = n + 1;
228        %                 FitParameters.Params{n} = mkparamgroup(RINGData.Lattice,SFI([N1 N2]),'s');
229        %             else
230        %                 error('   Error setting the SF parameter group.');
231        %             end
232        %         end
233        %
234        %         FitParameters.Values = [FitParameters.Values; zeros(SFn,1)];
235        %         FitParameters.Deltas = [FitParameters.Deltas; 0.5e-2 * ones(SFn,1)];  % automatic delta determination does not work if starting value is 0
236        %
237        %
238        %         % Fit skew quadrupoles in the sextupoles
239        %         SDI = findcells(RINGData.Lattice,'FamName','SD');
240        %         SDn = size(family2dev('SD'),1);
241        %
242        %         for loop = 1:length(SDI)
243        %             if length(SDI) == SDn
244        %                 n = n + 1;
245        %                 FitParameters.Params{n} = mkparamgroup(RINGData.Lattice,SDI(loop),'s');
246        %             elseif length(SDI) == 2*SDn
247        %                 % Split sextupoles
248        %                 N1 = 2*loop-1;
249        %                 N2 = 2*loop;
250        %                 n = n + 1;
251        %                 FitParameters.Params{n} = mkparamgroup(RINGData.Lattice,SDI([N1 N2]),'s');
252        %             else
253        %                 error('   Error setting the SD parameter group.');
254        %             end
255        %         end
256        %
257        %         FitParameters.Values = [FitParameters.Values; zeros(SDn,1)];
258        %         FitParameters.Deltas = [FitParameters.Deltas; 0.5e-2 * ones(SDn,1)];  % automatic delta determination does not work if starting value is 0
259        %
260    case 2 % 'Fit by Power Supply'
261        % skew quad
262        QTI = family2atindex('QT',family2dev('QT'));
263        for loop=1:length(QTI),
264            n = n + 1;
265            FitParameters.Params{n} = mkparamgroup(THERING, QTI(loop), 'KS1');
266        end
267        FitParameters.Values = [FitParameters.Values; zeros(length(QTI),1)];
268        FitParameters.Deltas = [FitParameters.Deltas; 1 * ones(length(QTI),1)];
269
270    case 3 %'Don't Fit'
271        fprintf('   Skew quadrupole not fit.\n\n');
272
273    otherwise
274        fprintf('   Skew quadrupole not fit.\n\n');
275end
276
277fprintf('\n');
278
279
280%
281% %%%%%%%%%%%%%%%%%%%%%
282% % Parameter Weights %
283% %%%%%%%%%%%%%%%%%%%%%
284%
285% % For each cell of FitParameters.Weight.Value, a rows is added to the A matrix
286% % Index of the row of A:  FitParameters.Weight.Index{i}
287% % Value of the weight:    FitParameters.Weight.Value{i} / mean(Mstd)
288%
289% ModeCell = {'No Parameter Weigths', 'Add Parameter Weights'};
290% [ButtonName, OKFlag] = listdlg('Name','BUILDLOCOINPUT','PromptString','Weights?', 'SelectionMode','single', 'ListString', ModeCell, 'ListSize', [200 75], 'InitialValue', 2);
291% if OKFlag ~= 1
292%     ButtonName = 1;  % Default
293% end
294%
295% switch ButtonName
296%     case 1
297%         % No Weights
298%         if isfield(FitParameters, 'Weight')
299%             FitParameters = rmfield(FitParameters, 'Weight');
300%         end
301%     case 2
302%         %for ii = 1:length(FitParameters.Values)
303%         for ii = 1:160, % specific weight for K-values
304%             FitParameters.Weight.Index{ii,1} = ii;
305%             %FitParameters.Weight.Value{ii,1} = 0.1; % for old BPM noise
306%             FitParameters.Weight.Value{ii,1} = 0.001; % for new BPM noise (sept 07)
307%         end
308% end
309
310
311
312% RF parameter fit setup (There is a flag to actually do the fit)
313if isempty(LocoMeasData.DeltaRF)
314    FitParameters.DeltaRF = 100;  % It's good to have something here so that LOCO will compute a model dispersion
315else
316    FitParameters.DeltaRF = LocoMeasData.DeltaRF;
317end
318
319
320% File check
321[BPMData, CMData, LocoMeasData, LocoModel, FitParameters, LocoFlags, RINGData] = locofilecheck({BPMData, CMData, LocoMeasData, LocoModel, FitParameters, LocoFlags, RINGData});
322
323
324% Save
325save(FileName, 'LocoModel', 'FitParameters', 'BPMData', 'CMData', 'RINGData', 'LocoMeasData', 'LocoFlags');
326
Note: See TracBrowser for help on using the repository browser.