source: MML/trunk/mml/quadcenterinit.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: 7.9 KB
Line 
1function QMS = quadcenterinit(QuadFamily, QuadDev, QuadPlane)
2% QMS = quadcenterinit(Family, Device, QuadPlane)
3%
4% QuadFamily = Quadrupole family
5% QuadDev    = Quadrupole device
6% QuadPlane  = Plane (1=horizontal {default}, 2=vertical)
7%
8% QMS structure contains fields:
9% QMS.QuadFamily
10% QMS.QuadDev
11% QMS.QuadDelta
12% QMS.QuadPlane
13% QMS.BPMFamily
14% QMS.BPMDev
15% QMS.BPMDevList
16% QMS.CorrFamily
17% QMS.CorrDevList             % Often one magnet but bumps or anything else is fine
18% QMS.CorrDelta               % Scale factor for each magnet in CorrDevList
19% QMS.DataDirectory           % From AD or '.'
20% QMS.QuadraticFit = 1;       % 1=quadratic fit, else linear fit
21% QMS.OutlierFactor = 1;      % if abs(data - fit) > OutlierFactor * BPMstd, then remove that BPM [mm]
22% QMS.NumberOfPoints = 3;
23% QMS.ModulationMethod = 'bipolar'
24% QMS.CorrectOrbit 'yes' or 'no'
25% QMS.CreatedBy
26%
27% See Also quadcenter
28
29%
30% This file is machine specific
31% Written by Laurent S. Nadolski (adapted from ALS)
32
33
34if nargin < 1
35    FamilyList = getfamilylist;
36    [tmp,i] = ismemberof(FamilyList,'QUAD');
37    if ~isempty(i)
38        FamilyList = FamilyList(i,:);
39    end
40    [i,ok] = listdlg('PromptString', 'Select a quadrupole family:', ...
41        'SelectionMode', 'single', ...
42        'ListString', FamilyList);
43    drawnow;
44    if ok == 0
45        return
46    else
47        QuadFamily = deblank(FamilyList(i,:));
48    end
49end
50if ~isfamily(QuadFamily)
51    error(sprintf('Quadrupole family %s does not exist.  Make sure the middle layer had been initialized properly.', ...
52        QuadFamily));
53end
54if nargin < 2
55    QuadDev = editlist(family2dev(QuadFamily),QuadFamily,zeros(length(family2dev(QuadFamily)),1));
56end
57if nargin < 3
58    %QuadPlane = 1;  % Horizontal default
59    ButtonNumber = menu('Which Plane?', 'Horizontal','Vertical','Cancel'); 
60    drawnow;
61    switch ButtonNumber
62        case 1
63            QuadPlane = 1;
64        case 2
65            QuadPlane = 2;
66        otherwise
67            fprintf('   quadcenterinit cancelled');
68            return
69    end
70end
71
72% Initialize the QMS structure
73QMS.QuadPlane = QuadPlane;
74QMS.QuadFamily = QuadFamily;
75QMS.QuadDev = QuadDev;
76QMS.OutlierFactor = 6;       % BPM Outlier: abs(fit - measured data) > OutlierFactor * std(BPM)
77QMS.CorrectOrbit = 'Yes';    % 'yes' or 'no';  % Only do it if the orbit is reasonably close to the offset orbit
78
79
80% Note: DataDirectory must start with the root of the tree and end with filesep or be '.'
81QMSDirectory = getfamilydata('Directory','BBA');
82if isempty(QMSDirectory)
83    QMS.DataDirectory = '.';
84else
85    QMS.DataDirectory = QMSDirectory;
86end
87
88if QMS.QuadPlane==1       
89    % Horizontal
90
91    CorrMethod = 'MEC';
92    %CorrMethod = 'LocalBump';
93    %CorrMethod = 'Fixed';
94
95    QMS.BPMFamily  = 'BPMx';
96    QMS.CorrFamily = 'HCOR';
97    QMS.NumberOfPoints = 7;
98    QMS.QuadraticFit = 0;       % 0 = linear fit, else quadratic fit
99       
100    % Use all BPMs in the minimization
101    QMS.BPMDevList = family2dev(QMS.BPMFamily);
102
103    % Find the BPM closest to the quadrupole
104    [TmpFamily, QMS.BPMDev] = quad2bpm(QMS.QuadFamily, QMS.QuadDev);
105
106    % Pick the quadrupole and corrector delta
107    QMS.ModulationMethod = 'bipolar';
108    QMS.QuadDelta = 1.0;
109    QMS.CorrDelta = 1.5;
110   
111    % Find corrector the bump the beam in the quadrupole
112    switch CorrMethod
113        case 'Fixed' % Fixed choice
114            % Old QMS method
115            if strcmp(QMS.QuadFamily,'QF') ==1 || strcmp(QMS.QuadFamily,'QD') ==1
116                if QMS.QuadDev(1,2) == 1,
117                    QMS.CorrDevList = [QMS.QuadDev(1) 1];
118                else
119                    QMS.CorrDevList = [QMS.QuadDev(1) 8];
120                end
121                if (QMS.QuadDev(1,1)==1 & QMS.CorrDevList(1,2)==1)        % for sector1,  use HCM8 was HCM1
122                    QMS.CorrDevList = [QMS.QuadDev(1) 8];
123                elseif (QMS.QuadDev(1,1)==12 & QMS.CorrDevList(1,2)==8)   % for sector12, use HCM1 was HCM8
124                    QMS.CorrDevList = [QMS.QuadDev(1) 1];
125                end
126            elseif strcmp(QMS.QuadFamily,'Q1')==1
127                if QMS.QuadDev(1,2) == 1
128                    QMS.CorrDevList = [QMS.QuadDev(1) 4];
129                else
130                    QMS.CorrDevList = [QMS.QuadDev(1) 5];
131                end
132            elseif strcmp(QMS.QuadFamily,'Q2')==1
133                if QMS.QuadDev(1,2) == 1
134                    QMS.CorrDevList = [QMS.QuadDev(1) 8];
135                else
136                    QMS.CorrDevList = [QMS.QuadDev(1) 1];
137                end
138                if (QMS.QuadDev(1,1)==1 & QMS.CorrDevList(1,2)==1)
139                    QMS.CorrDevList = [QMS.QuadDev(1) 2];
140                elseif (QMS.QuadDev(1,1)==12 & QMS.CorrDevList(1,2)==8)
141                    QMS.CorrDevList = [QMS.QuadDev(1) 7];
142                end
143            end
144        case 'MEC' % Most effective corrector
145            % Pick the corrector based on the most effective corrector in the response matrix
146            R = getbpmresp('Struct');
147            [i, iNotFound] = findrowindex(QMS.BPMDev, R(1,1).Monitor.DeviceList);
148            m = R(1,1).Data(i,:);
149            [MaxValue, j] = max(abs(m));
150            QMS.CorrDevList = R(1,1).Actuator.DeviceList(j,:);
151        case 'LocalBump'
152            % Local bump corrector method
153            [OCS, RF, OCS0] = setorbitbump('BPMx',QMS.BPMDev,[.1],'HCOR',[-2 -1 1 2], 'NoSetSP');
154            QMS.CorrDelta = OCS.CM.Data - OCS0.CM.Data;
155            QMS.CorrDevList = OCS.CM.DeviceList;
156        otherwise
157            error('Corrector method unknown.');
158    end
159
160elseif QMS.QuadPlane==2
161    % Vertical
162
163    CorrMethod = 'MEC';
164    %CorrMethod = 'LocalBump';
165    %CorrMethod = 'Fixed';
166
167    QMS.BPMFamily  = 'BPMz';
168    QMS.CorrFamily = 'VCOR';
169    QMS.NumberOfPoints = 5;
170    QMS.QuadraticFit = 0;       % 0 = linear fit, else quadratic fit
171
172    % Use all BPMs in the minimization
173    QMS.BPMDevList = family2dev(QMS.BPMFamily);
174
175    % Find the BPM closest to the quadrupole
176    [TmpFamily, QMS.BPMDev] = quad2bpm(QMS.QuadFamily, QMS.QuadDev);
177
178    % Pick the quadrupole and corrector delta
179    QMS.ModulationMethod = 'bipolar';
180    QMS.QuadDelta = 1.0;
181    QMS.CorrDelta = 1.5;
182
183    % Find corrector the bump the beam in the quadrupole
184    switch CorrMethod
185        case 'Fixed'
186            % Old QMS method
187            if strcmp(QMS.QuadFamily,'QF')==1 | strcmp(QMS.QuadFamily,'QD')==1
188                if QMS.QuadDev(1,2) == 1
189                    QMS.CorrDevList = [QMS.QuadDev(1) 2];
190                else
191                    QMS.CorrDevList = [QMS.QuadDev(1) 7];
192                end
193            elseif strcmp(QMS.QuadFamily,'QDA')==1
194                if QMS.QuadDev(1,2) == 1
195                    QMS.CorrDevList = [QMS.QuadDev(1) 4];
196                else
197                    QMS.CorrDevList = [QMS.QuadDev(1) 5];
198                end
199            elseif strcmp(QMS.QuadFamily,'QFA')==1
200                if QMS.QuadDev(1,2) == 1
201                    QMS.CorrDevList = [QMS.QuadDev(1) 2];
202                else
203                    QMS.CorrDevList = [QMS.QuadDev(1) 7];
204                end
205            end
206        case 'MEC'
207            % Pick the corrector based on the most effective corrector in the response matrix
208            R = getbpmresp('Struct','Physics');
209            [i, iNotFound] = findrowindex(QMS.BPMDev, R(2,2).Monitor.DeviceList);
210            m = R(2,2).Data(i,:);
211            [MaxValue, j] = max(abs(m));
212            QMS.CorrDevList = R(2,2).Actuator.DeviceList(j,:);
213        case 'LocalBump'
214            % Local bump corrector method
215            [OCS, RF, OCS0] = setorbitbump('BPMz',QMS.BPMDev,[1/4],'VCM',[-2 -1 1 2], 'NoSetSP');
216            QMS.CorrDelta = OCS.CM.Data - OCS0.CM.Data;
217            %[OCS, RF, OCS0] = setorbitbump('BPMz',QMS.BPMDev,[1/4],'VCM',[-2 -1 1 2], 'Display');
218            %setsp(OCS0.CM);
219            %QMS.CorrDelta = OCS.CM.Data - OCS0.CM.Data;
220            QMS.CorrDevList = OCS.CM.DeviceList;
221        otherwise
222            error('Corrector method unknown.');
223    end
224
225else
226    error('QMS.QuadPlane must be 1 or 2');
227end
228
229
230QMS.CreatedBy = 'quadcenterinit';
231QMS = orderfields(QMS);
Note: See TracBrowser for help on using the repository browser.