1 | function [Rmat, OutputFileName] = meastunerespFBT(varargin) |
---|
2 | %MEASTUNERESPFBT - Measures the response from quadrupole to tune measured |
---|
3 | %at large strorage current |
---|
4 | % [R, FileName] = meastunerespFBT(ActuatorFamily, ActuatorDeviceList, ActuatorDelta, ModulationMethod, WaitFlag, FileName, DirectoryName, ExtraDelay) |
---|
5 | % |
---|
6 | % INPUTS |
---|
7 | % 1. ActuatorFamily = Family name or cell array of family nams {Default: {'Q7','Q9'}} |
---|
8 | % 2. ActuatorDeviceList = Device list {Default: [], the entire family} |
---|
9 | % (Note: all devices in DeviceList are changed at the same time) |
---|
10 | % 3. ActuatorDelta = Change in magnet strength {Default: getfamilydata('ActuatorFamily','Setpoint','DeltaRespMat')} |
---|
11 | % 4. ModulationMethod - 'Unipolar' is the default since hysteresis is usually an issue (see help measrespmat) |
---|
12 | % 5. If WaitFlag >= 0, then WaitFlag is the delay before measuring the tune (sec) |
---|
13 | % = -3, then a delay of getfamilydata('TuneDelay') is used {Default} |
---|
14 | % = -4, then pause until keyboard input |
---|
15 | % = -5, then input the tune measurement manually by keyboard input |
---|
16 | % 6. Optional input to change the default filename and directory |
---|
17 | % FileName - Filename for the response matrix data |
---|
18 | % (No input or empty means data will be saved to the default file (except for model response matrices)) |
---|
19 | % DirectoryName - Directory name to store the response matrix data file |
---|
20 | % Note: a. FileName can include the path if DirectoryName is not used |
---|
21 | % b. For model response matrices, FileName must exist for a file save |
---|
22 | % c. When using the default FileName, a dialog box will prompt for changes |
---|
23 | % |
---|
24 | % 7. ExtraDelay - extra time delay [seconds] after a setpoint change |
---|
25 | % |
---|
26 | % 8. 'Struct' will return a response matrix structure |
---|
27 | % 'Numeric' will return a matrix output {Default} |
---|
28 | % 'Matrix' - Return a matrix output {Default} |
---|
29 | % |
---|
30 | % Note: 'Matrix' is only a valid flag for Numeric outputs |
---|
31 | % Often use with the model input. For example, to compare the model |
---|
32 | % tune response matrix to the default matrix, |
---|
33 | % Mmodel = meastuneresp('Model','Matrix'); |
---|
34 | % Mmeas = gettuneresp; |
---|
35 | % |
---|
36 | % 9. Optional override of the units: |
---|
37 | % 'Physics' - Use physics units |
---|
38 | % 'Hardware' - Use hardware units |
---|
39 | % |
---|
40 | % 10. Optional override of the mode: |
---|
41 | % 'Online' - Set/Get data online |
---|
42 | % 'Model' - Get the model chromaticity directly from AT (uses modeltune) |
---|
43 | % 'Simulator' - Set/Get data on the simulated accelerator using AT (ie, same commands as 'Online') |
---|
44 | % 'Manual' - Set/Get data manually |
---|
45 | % |
---|
46 | % 11. 'Display' - Prints status information to the command window {Default} |
---|
47 | % 'NoDisplay' - Nothing is printed to the command window |
---|
48 | % |
---|
49 | % 12. 'Archive' - Save the response matrix structure {Default, unless Mode='Model'} |
---|
50 | % 'NoArchive' - Save the response matrix structure |
---|
51 | % |
---|
52 | % OUTPUTS |
---|
53 | % R = Response matrix from delta quadrupole to delta tune |
---|
54 | % |
---|
55 | % If more than one quadrupole family is input, the R is a cell array indexed by |
---|
56 | % quadrupole family (ie, the number of families in ActuatorFamily). The default |
---|
57 | % is to make R{1} Q7 and R{2} Q9. R{1} or R.Data for structure output |
---|
58 | % is a 2x(number of quadrupole in family) array. The first row is horizontal |
---|
59 | % tuen and the second is vertical. |
---|
60 | % |
---|
61 | % Units: delta(Tune) / delta(Quadrupole) is set by the .Units field for the quadrupole family |
---|
62 | % Typically: Hardware units: fractional tune / Amp; |
---|
63 | % Physics units: fractional tune / K; |
---|
64 | % |
---|
65 | % EXAMPLES |
---|
66 | % 1. Gets 2x2 tune response matrix using the 2 default families |
---|
67 | % meastuneresp('Display') |
---|
68 | % 2. For all quad in model |
---|
69 | % RTune = meastunerespFBT({'Q1','Q2','Q3','Q4','Q5','Q6','Q7','Q8','Q9','Q10'}, 'Model') |
---|
70 | % |
---|
71 | % NOTES |
---|
72 | % 1. 'Physics' does not work at SOLEIL since polynomial fit does not work for small current !!! |
---|
73 | % |
---|
74 | % See Also gettune, steptune, settune |
---|
75 | |
---|
76 | |
---|
77 | % |
---|
78 | % Written by Gregory Portmann and Jeff Corbett |
---|
79 | % Modified for measurement at large current |
---|
80 | |
---|
81 | |
---|
82 | % Initialize |
---|
83 | ActuatorFamily = findmemberof('Tune Corrector')'; |
---|
84 | if isempty(ActuatorFamily) |
---|
85 | ActuatorFamily = {'Q7','Q9'}; |
---|
86 | end |
---|
87 | |
---|
88 | %ActuatorDelta = {getfamilydata('Q9','Setpoint','DeltaRespMat'),getfamilydata('Q10','Setpoint','DeltaRespMat')}; |
---|
89 | ActuatorDelta = {1,1}; |
---|
90 | |
---|
91 | ModulationMethod = 'unipolar'; |
---|
92 | DirectoryName = []; |
---|
93 | WaitFlag = 10; |
---|
94 | ExtraDelay = 0; |
---|
95 | StructOutputFlag = 0; |
---|
96 | MatrixFlag = 1; |
---|
97 | DisplayFlag = 1; |
---|
98 | ArchiveFlag = -1; |
---|
99 | FileName = -1; |
---|
100 | ModeFlag = ''; % model, online, manual, or '' for default mode |
---|
101 | UnitsFlag = ''; % hardware, physics, or '' for default units |
---|
102 | TimeStamp = clock; |
---|
103 | |
---|
104 | InputFlags = {}; |
---|
105 | for i = length(varargin):-1:1 |
---|
106 | if isstruct(varargin{i}) |
---|
107 | % Ignore structures |
---|
108 | elseif iscell(varargin{i}) |
---|
109 | % Ignore cells |
---|
110 | elseif strcmpi(varargin{i},'Struct') |
---|
111 | StructOutputFlag = 1; |
---|
112 | MatrixFlag = 0; |
---|
113 | varargin(i) = []; |
---|
114 | elseif strcmpi(varargin{i},'Numeric') |
---|
115 | StructOutputFlag = 0; |
---|
116 | NumericOutputFlag = 1; |
---|
117 | varargin(i) = []; |
---|
118 | elseif strcmpi(varargin{i},'Matrix') |
---|
119 | MatrixFlag = 1; |
---|
120 | StructOutputFlag = 0; |
---|
121 | NumericOutputFlag = 1; |
---|
122 | varargin(i) = []; |
---|
123 | elseif strcmpi(varargin{i},'Archive') |
---|
124 | ArchiveFlag = 1; |
---|
125 | if length(varargin) > i |
---|
126 | % Look for a filename as the next input |
---|
127 | if ischar(varargin{i+1}) |
---|
128 | FileName = varargin{i+1}; |
---|
129 | varargin(i+1) = []; |
---|
130 | end |
---|
131 | end |
---|
132 | varargin(i) = []; |
---|
133 | elseif strcmpi(varargin{i},'NoArchive') |
---|
134 | ArchiveFlag = 0; |
---|
135 | varargin(i) = []; |
---|
136 | elseif strcmpi(varargin{i},'unipolar') |
---|
137 | ModulationMethod = 'unipolar'; |
---|
138 | varargin(i) = []; |
---|
139 | elseif strcmpi(varargin{i},'bipolar') |
---|
140 | ModulationMethod = 'bipolar'; |
---|
141 | varargin(i) = []; |
---|
142 | elseif strcmpi(varargin{i},'Model') |
---|
143 | ModeFlag = varargin{i}; |
---|
144 | InputFlags = [InputFlags varargin(i)]; |
---|
145 | varargin(i) = []; |
---|
146 | elseif strcmpi(varargin{i},'Simulator') |
---|
147 | ModeFlag = varargin{i}; |
---|
148 | InputFlags = [InputFlags varargin(i)]; |
---|
149 | varargin(i) = []; |
---|
150 | elseif strcmpi(varargin{i},'Online') |
---|
151 | ModeFlag = varargin{i}; |
---|
152 | InputFlags = [InputFlags varargin(i)]; |
---|
153 | varargin(i) = []; |
---|
154 | elseif strcmpi(varargin{i},'Manual') |
---|
155 | ModeFlag = varargin{i}; |
---|
156 | InputFlags = [InputFlags varargin(i)]; |
---|
157 | varargin(i) = []; |
---|
158 | elseif strcmpi(varargin{i},'Physics') |
---|
159 | UnitsFlag = varargin{i}; |
---|
160 | InputFlags = [InputFlags varargin(i)]; |
---|
161 | varargin(i) = []; |
---|
162 | elseif strcmpi(varargin{i},'Hardware') |
---|
163 | UnitsFlag = varargin{i}; |
---|
164 | InputFlags = [InputFlags varargin(i)]; |
---|
165 | varargin(i) = []; |
---|
166 | elseif strcmpi(varargin{i},'NoDisplay') |
---|
167 | DisplayFlag = 0; |
---|
168 | varargin(i) = []; |
---|
169 | elseif strcmpi(varargin{i},'Display') |
---|
170 | DisplayFlag = 1; |
---|
171 | varargin(i) = []; |
---|
172 | end |
---|
173 | end |
---|
174 | |
---|
175 | if length(varargin) >= 1 |
---|
176 | ActuatorFamily = varargin{1}; |
---|
177 | end |
---|
178 | |
---|
179 | if length(varargin) >= 2 |
---|
180 | ActuatorDeviceList = varargin{2}; |
---|
181 | else |
---|
182 | if iscell(ActuatorFamily) |
---|
183 | for i = 1:length(ActuatorFamily) |
---|
184 | ActuatorDeviceList{i} = []; |
---|
185 | end |
---|
186 | else |
---|
187 | ActuatorDeviceList = []; |
---|
188 | end |
---|
189 | end |
---|
190 | |
---|
191 | if length(varargin) >= 3 |
---|
192 | ActuatorDelta = varargin{3}; |
---|
193 | else |
---|
194 | if iscell(ActuatorFamily) |
---|
195 | for i = 1:length(ActuatorFamily) |
---|
196 | ActuatorDelta{i} = []; |
---|
197 | end |
---|
198 | else |
---|
199 | ActuatorDelta = []; |
---|
200 | end |
---|
201 | end |
---|
202 | |
---|
203 | % Force to be a cells |
---|
204 | if ~iscell(ActuatorFamily) |
---|
205 | ActuatorFamily = {ActuatorFamily}; |
---|
206 | end |
---|
207 | if ~iscell(ActuatorDeviceList) |
---|
208 | ActuatorDeviceList = {ActuatorDeviceList}; |
---|
209 | end |
---|
210 | if ~iscell(ActuatorDelta) |
---|
211 | ActuatorDelta = {ActuatorDelta}; |
---|
212 | end |
---|
213 | |
---|
214 | if length(varargin) >= 4 |
---|
215 | ModulationMethod = varargin{4}; |
---|
216 | end |
---|
217 | |
---|
218 | % WaitFlag input |
---|
219 | if length(varargin) >= 5 |
---|
220 | WaitFlag = varargin{5}; |
---|
221 | end |
---|
222 | if isempty(WaitFlag) || WaitFlag == -3 |
---|
223 | WaitFlag = 2.2*getfamilydata('TuneDelay'); |
---|
224 | end |
---|
225 | if isempty(WaitFlag) |
---|
226 | WaitFlag = input(' Delay for Tune Measurement (Seconds, Keyboard Pause = -4, or Manual Tune Input = -5) = '); |
---|
227 | end |
---|
228 | |
---|
229 | if length(varargin) >= 6 |
---|
230 | FileName = varargin{6}; |
---|
231 | end |
---|
232 | if length(varargin) >= 7 |
---|
233 | if isempty(varargin{7}) |
---|
234 | % Use default |
---|
235 | DirectoryName = getfamilydata('Directory', 'TuneResponse'); |
---|
236 | elseif ischar(varargin{7}) |
---|
237 | DirectoryName = varargin{7}; |
---|
238 | else |
---|
239 | % Empty DirectoryName mean it will only be used if FileName is empty |
---|
240 | DirectoryName = []; |
---|
241 | end |
---|
242 | end |
---|
243 | |
---|
244 | % Look for ExtraDelay |
---|
245 | if length(varargin) >= 8 |
---|
246 | if isempty(varargin{8}) |
---|
247 | % Use default |
---|
248 | end |
---|
249 | if isnumeric(varargin{8}) |
---|
250 | ExtraDelay = varargin{8}; |
---|
251 | end |
---|
252 | end |
---|
253 | |
---|
254 | % Change defaults for the model (note: simulator mode mimics online) |
---|
255 | if strcmpi(ModeFlag,'Model') |
---|
256 | % Only archive data if ArchiveFlag==1 or FileName~=[] |
---|
257 | if ischar(FileName) || ArchiveFlag == 1 |
---|
258 | ArchiveFlag = 1; |
---|
259 | else |
---|
260 | ArchiveFlag = 0; |
---|
261 | end |
---|
262 | |
---|
263 | % Only display is it was turned on at the command line |
---|
264 | if DisplayFlag == 1 |
---|
265 | % Keep DisplayFlag = 1 |
---|
266 | else |
---|
267 | DisplayFlag = 0; |
---|
268 | end |
---|
269 | else |
---|
270 | % Online or Simulator: Archive unless ArchiveFlag was forced to zero |
---|
271 | if ArchiveFlag ~= 0 |
---|
272 | ArchiveFlag = 1; |
---|
273 | if FileName == -1 |
---|
274 | FileName = ''; |
---|
275 | end |
---|
276 | end |
---|
277 | end |
---|
278 | |
---|
279 | |
---|
280 | % Print setup information |
---|
281 | % if DisplayFlag |
---|
282 | % if ~strcmpi(ModeFlag,'Model') |
---|
283 | % fprintf('\n'); |
---|
284 | % fprintf(' MEASTUNERESP measures the tune response to the quadrupole magnet families.\n'); |
---|
285 | % fprintf(' The storage ring lattice and hardware should be setup for properly.\n'); |
---|
286 | % fprintf(' Make sure the following information is correct:\n'); |
---|
287 | % fprintf(' 1. Proper magnet lattice (including skew quadrupoles)\n'); |
---|
288 | % fprintf(' 2. Proper electron beam energy\n'); |
---|
289 | % fprintf(' 3. Proper electron bunch pattern\n'); |
---|
290 | % fprintf(' 4. Tune is functioning or in manual mode\n'); |
---|
291 | % fprintf(' 5. The injection bump magnets off\n\n'); |
---|
292 | % end |
---|
293 | % end |
---|
294 | |
---|
295 | |
---|
296 | % Browse for filename and directory if using default FileName |
---|
297 | if ArchiveFlag |
---|
298 | if isempty(FileName) |
---|
299 | FileName = appendtimestamp(getfamilydata('Default', 'TuneRespFile')); |
---|
300 | DirectoryName = getfamilydata('Directory', 'TuneResponse'); |
---|
301 | if isempty(DirectoryName) |
---|
302 | DirectoryName = [getfamilydata('Directory','DataRoot') 'Response', filesep, 'Dispersion', filesep]; |
---|
303 | else |
---|
304 | % Make sure default directory exists |
---|
305 | DirStart = pwd; |
---|
306 | [DirectoryName, ErrorFlag] = gotodirectory(DirectoryName); |
---|
307 | cd(DirStart); |
---|
308 | end |
---|
309 | [FileName, DirectoryName] = uiputfile('*.mat', 'Select a Dispersion Response File', [DirectoryName FileName]); |
---|
310 | if FileName == 0 |
---|
311 | ArchiveFlag = 0; |
---|
312 | disp(' Dispersion response measurement canceled.'); |
---|
313 | Rmat = []; OutputFileName=''; |
---|
314 | return |
---|
315 | end |
---|
316 | FileName = [DirectoryName, FileName]; |
---|
317 | elseif FileName == -1 |
---|
318 | FileName = appendtimestamp(getfamilydata('Default', 'TuneRespFile')); |
---|
319 | DirectoryName = getfamilydata('Directory', 'TuneResponse'); |
---|
320 | if isempty(DirectoryName) |
---|
321 | DirectoryName = [getfamilydata('Directory','DataRoot') 'Response', filesep, 'Dispersion', filesep]; |
---|
322 | end |
---|
323 | FileName = [DirectoryName, FileName]; |
---|
324 | end |
---|
325 | |
---|
326 | % Acquire initial data |
---|
327 | MachineConfig = getmachineconfig(InputFlags{:}); |
---|
328 | end |
---|
329 | |
---|
330 | |
---|
331 | % % Query to begin measurement |
---|
332 | % if DisplayFlag |
---|
333 | % tmp = questdlg('Begin response matrix measurement?','MEASTUNERESP','YES','NO','YES'); |
---|
334 | % if strcmp(tmp,'NO') |
---|
335 | % fprintf(' Response matrix measurement aborted\n'); |
---|
336 | % Rmat = []; |
---|
337 | % return |
---|
338 | % end |
---|
339 | % end |
---|
340 | |
---|
341 | |
---|
342 | % Acquire initial data |
---|
343 | if StructOutputFlag || ArchiveFlag |
---|
344 | X = getx('struct', InputFlags{:}); |
---|
345 | Y = gety('struct', InputFlags{:}); |
---|
346 | end |
---|
347 | |
---|
348 | |
---|
349 | % Begin main loop over actuators |
---|
350 | TimeStart = gettime; |
---|
351 | % TUNE must be a family for this to work |
---|
352 | if DisplayFlag |
---|
353 | Rmat = measrespmat('TUNEFBT', [1;2], ActuatorFamily, ActuatorDeviceList, ActuatorDelta, ModulationMethod, WaitFlag, ExtraDelay, 'Struct', 'Display', InputFlags{:}); |
---|
354 | else |
---|
355 | Rmat = measrespmat('TUNEFBT', [1;2], ActuatorFamily, ActuatorDeviceList, ActuatorDelta, ModulationMethod, WaitFlag, ExtraDelay, 'Struct', 'NoDisplay', InputFlags{:}); |
---|
356 | end |
---|
357 | if StructOutputFlag |
---|
358 | for i = 1:size(Rmat,1) |
---|
359 | for j = 1:size(Rmat,2) |
---|
360 | if iscell(Rmat) |
---|
361 | Rmat{i,j}.DataDescriptor = 'TUNEFBT Response Matrix'; |
---|
362 | Rmat{i,j}.CreatedBy = 'meastuneresp'; |
---|
363 | |
---|
364 | Rmat{i,j}.X = X; |
---|
365 | Rmat{i,j}.Y = Y; |
---|
366 | else |
---|
367 | Rmat(i,j).DataDescriptor = 'TUNEFBT Response Matrix'; |
---|
368 | Rmat(i,j).CreatedBy = 'meastuneresp'; |
---|
369 | |
---|
370 | Rmat(i,j).X = X; |
---|
371 | Rmat(i,j).Y = Y; |
---|
372 | end |
---|
373 | end |
---|
374 | end |
---|
375 | end |
---|
376 | |
---|
377 | |
---|
378 | % For one family inputs, there is no need for a cell output (probably already done in measrespmat) |
---|
379 | if length(Rmat) == 1 && iscell(Rmat) |
---|
380 | Rmat = Rmat{1}; |
---|
381 | end |
---|
382 | |
---|
383 | |
---|
384 | % Save data in the proper directory |
---|
385 | if ArchiveFlag || ischar(FileName) |
---|
386 | [DirectoryName, FileName, Ext] = fileparts(FileName); |
---|
387 | DirStart = pwd; |
---|
388 | [DirectoryName, ErrorFlag] = gotodirectory(DirectoryName); |
---|
389 | if ErrorFlag |
---|
390 | fprintf('\n There was a problem getting to the proper directory!\n\n'); |
---|
391 | end |
---|
392 | save(FileName, 'Rmat','MachineConfig'); |
---|
393 | cd(DirStart); |
---|
394 | OutputFileName = [DirectoryName, FileName, '.mat']; |
---|
395 | |
---|
396 | if DisplayFlag |
---|
397 | fprintf(' Tune response matrix data (''Rmat'') saved to disk\n'); |
---|
398 | fprintf(' Filename: %s\n', OutputFileName); |
---|
399 | fprintf(' The total response matrix measurement time: %.2f minutes.\n', (gettime-TimeStart)/60); |
---|
400 | end |
---|
401 | end |
---|
402 | |
---|
403 | |
---|
404 | % Put the data part in Rmat |
---|
405 | if ~StructOutputFlag |
---|
406 | if length(ActuatorFamily) == 1 |
---|
407 | Rmat = Rmat.Data; |
---|
408 | else |
---|
409 | for i = 1:size(Rmat,1) |
---|
410 | for j = 1:size(Rmat,2) |
---|
411 | Rmat{i,j} = Rmat{i,j}.Data; |
---|
412 | end |
---|
413 | end |
---|
414 | end |
---|
415 | end |
---|
416 | |
---|
417 | if MatrixFlag && StructOutputFlag |
---|
418 | error('Cannot ask for a matrix and a structure output.'); |
---|
419 | end |
---|
420 | |
---|
421 | |
---|
422 | if MatrixFlag |
---|
423 | if length(ActuatorFamily) == 1 |
---|
424 | R = sum(Rmat,2); |
---|
425 | else |
---|
426 | if size(Rmat,1) == 1 |
---|
427 | for j = 1:size(Rmat,2) |
---|
428 | R(:,j) = sum(Rmat{1,j},2); |
---|
429 | end |
---|
430 | else |
---|
431 | error('Tune response matrix cell array has more than one row (which is very odd)'); |
---|
432 | end |
---|
433 | end |
---|
434 | Rmat = R; |
---|
435 | end |
---|
436 | |
---|
437 | |
---|