source: MML/trunk/mml/steptune.m @ 4

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

Initial import--MML version from SOLEIL@2013

File size: 4.7 KB
Line 
1function [DelQuad, ActuatorFamily] = steptune(varargin)
2%STEPTUNE - Step the tune
3%  [DelQuad, QuadFamily] = steptune(DeltaTune, TuneResponseMatrix);
4%
5%  Step change in storage ring tune using the default tune correctors (findmemberof('Tune Corrector'))
6
7%  INPUTS
8%  1.             | Change in Horizontal Tune |
9%     DeltaTune = |                           |
10%                 | Change in Vertical Tune   |
11%  2. TuneResponseMatrix - Tune response matrix {Default: findmemberof('Tune Corrector')}
12%  3. ActuatorFamily -  Quadrupole to vary, ex {'Q7', 'Q9'} {Default: findmemberof('Tune Corrector')}
13%  4. Optional override of the units:
14%     'Physics'  - Use physics  units
15%     'Hardware' - Use hardware units
16%  5. Optional override of the mode:
17%     'Online'    - Set/Get data online 
18%     'Model'     - Set/Get data on the simulated accelerator using AT
19%     'Simulator' - Set/Get data on the simulated accelerator using AT
20%     'Manual'    - Set/Get data manually
21%  6. Options:
22%     'NoSP'      - Computes but do no apply
23%
24%
25%  OUTPUTS
26%  1. DelQuad
27%  2. QuadFamily - Families used (cell array)
28%
29%  ALGORITHM 
30%     SVD method
31%  DelQuad = inv(TuneResponseMatrix) * DeltaTune
32
33%
34%  Written by Gregory J. Portmann
35%  Modified by Laurent S. Nadolski
36%   06/01/06 - Introduction of ActuatorFamily as a input
37
38% Initialize
39
40UnitsFlag = {};
41ModeFlag = {};
42SPFLAG = 1;
43
44for i = length(varargin):-1:1
45    if strcmpi(varargin{i},'physics')
46        UnitsFlag = varargin(i);
47        varargin(i) = [];
48    elseif strcmpi(varargin{i},'hardware')
49        UnitsFlag = varargin(i);
50        varargin(i) = [];
51    elseif strcmpi(varargin{i},'Simulator') | strcmpi(varargin{i},'Model')
52        ModeFlag = varargin(i);
53        varargin(i) = [];
54    elseif strcmpi(varargin{i},'online')
55        ModeFlag = varargin(i);
56        varargin(i) = [];
57    elseif strcmpi(varargin{i},'manual')
58        ModeFlag = varargin(i);
59        varargin(i) = [];
60    elseif strcmpi(varargin{i},'NoSP')
61        SPFLAG = 0;
62        varargin(i) = [];
63    end       
64end
65
66if length(varargin) >= 1
67    DeltaTune = varargin{1};
68else
69    DeltaTune = [];   
70end
71if isempty(DeltaTune)
72    answer = inputdlg({'Change the horizontal tune by', 'Change the vertical tune by'},'STEPTUNE',1,{'0','0'});
73    if isempty(answer)
74        return
75    end
76    DeltaTune(1,1) = str2num(answer{1});
77    DeltaTune(2,1) = str2num(answer{2});
78    fprintf('Tune variation wanted is dnux=%f dnuz=%f\n',DeltaTune(1,1), DeltaTune(2,1))
79end
80
81DeltaTune = DeltaTune(:);
82if size(DeltaTune,1) ~= 2
83        error('Input must be a 2x1 column vector.');
84end
85if DeltaTune(1)==0 && DeltaTune(2)==0
86    return
87end
88
89if length(varargin) >= 2
90    TuneResponseMatrix = varargin{2};
91else
92    TuneResponseMatrix = [];   
93end
94if isempty(TuneResponseMatrix)
95    TuneResponseMatrix = gettuneresp(UnitsFlag{:});
96end
97if isempty(TuneResponseMatrix)
98    error('The tune response matrix must be an input or available in one of the default response matrix files.');
99end
100
101% User ActuatorFamily
102if length(varargin) >= 3
103    ActuatorFamily = varargin{3};
104else
105    ActuatorFamily = findmemberof('Tune Corrector')';
106    if isempty(ActuatorFamily)
107        ActuatorFamily = {'QF','QD'};
108    end
109end
110
111% It's probably wise to check the .Units fields
112
113% 1. SVD Tune Correction
114% Decompose the tune response matrix:
115[U, S, V] = svd(TuneResponseMatrix, 'econ');
116% TuneResponseMatrix = U*S*V'
117%
118% The V matrix columns are the singular vectors in the quadrupole magnet space
119% The U matrix columns are the singular vectors in the TUNE space
120% U'*U=I and V*V'=I
121%
122% TUNECoef is the projection onto the columns of TuneResponseMatrix*V(:,Ivec) (same space as spanned by U)
123% Sometimes it's interesting to look at the size of these coefficients with singular value number.
124TUNECoef = diag(diag(S).^(-1)) * U' * DeltaTune;
125%
126% Convert the vector TUNECoef back to coefficents of TuneResponseMatrix
127DelQuad = V * TUNECoef;
128
129
130% 2. Square matrix solution
131% DelQuad = inv(TuneResponseMatrix) * DeltaTune;
132
133
134% 3. Least squares solution
135% DelQuad = inv(TuneResponseMatrix'*TuneResponseMatrix)*TuneResponseMatrix' * DeltaTune;
136%
137% see Matlab help for "Matrices and Linear Algebra" to see what this does
138% If overdetermined, then "\" is least squares
139%
140% If underdetermined (like more than 2 quadrupole families), then only the
141% columns with the 2 biggest norms will be keep.  The rest of the quadupole
142% families with have zero effect.  Hence, constraints would have to be added for
143% this method to work.
144% DelQuad = TuneResponseMatrix \ DeltaTune;
145
146
147if SPFLAG
148
149    % Make the setpoint change
150    SP = getsp(ActuatorFamily, UnitsFlag{:}, ModeFlag{:});
151
152    if iscell(SP)
153        for i = 1:length(SP)
154            SP{i} = SP{i} + DelQuad(i);
155        end
156    else
157        SP = SP + DelQuad;
158    end
159
160    setsp(ActuatorFamily, SP, UnitsFlag{:}, ModeFlag{:});
161
162end
Note: See TracBrowser for help on using the repository browser.