1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" |
---|
2 | "http://www.w3.org/TR/REC-html40/loose.dtd"> |
---|
3 | <html> |
---|
4 | <head> |
---|
5 | <title>Description of orbitcorrectionmethods</title> |
---|
6 | <meta name="keywords" content="orbitcorrectionmethods"> |
---|
7 | <meta name="description" content="ORBITCORRECTIONMETHODS - Some the orbit correction methods used on light sources"> |
---|
8 | <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
---|
9 | <meta name="generator" content="m2html © 2003 Guillaume Flandin"> |
---|
10 | <meta name="robots" content="index, follow"> |
---|
11 | <link type="text/css" rel="stylesheet" href="../m2html.css"> |
---|
12 | </head> |
---|
13 | <body> |
---|
14 | <a name="_top"></a> |
---|
15 | <div><a href="../index.html">Home</a> > <a href="index.html">mml</a> > orbitcorrectionmethods.m</div> |
---|
16 | |
---|
17 | <!--<table width="100%"><tr><td align="left"><a href="../index.html"><img alt="<" border="0" src="../left.png"> Master index</a></td> |
---|
18 | <td align="right"><a href="index.html">Index for mml <img alt=">" border="0" src="../right.png"></a></td></tr></table>--> |
---|
19 | |
---|
20 | <h1>orbitcorrectionmethods |
---|
21 | </h1> |
---|
22 | |
---|
23 | <h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2> |
---|
24 | <div class="box"><strong>ORBITCORRECTIONMETHODS - Some the orbit correction methods used on light sources</strong></div> |
---|
25 | |
---|
26 | <h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2> |
---|
27 | <div class="box"><strong>function [OCS, SmatNoWeights, S, U, V] = orbitcorrectionmethods(OCS, Smat, S, U, V) </strong></div> |
---|
28 | |
---|
29 | <h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2> |
---|
30 | <div class="fragment"><pre class="comment">ORBITCORRECTIONMETHODS - Some the orbit correction methods used on light sources |
---|
31 | |
---|
32 | [OCS, Smat, S, U, V] = orbitcorrectionmethods(OCS) % Get response matrix & compute SVD correction |
---|
33 | [OCS, Smat, S, U, V] = orbitcorrectionmethods(OCS, Smat) % Compute SVD & correction |
---|
34 | [OCS, Smat, S, U, V] = orbitcorrectionmethods(OCS, Smat, S, U, V) % Compute correction |
---|
35 | |
---|
36 | OCS.FitRF - Determines which RF freqency method to use |
---|
37 | See setorbit for information on all the different flags. |
---|
38 | |
---|
39 | NOTES |
---|
40 | 1. OCS.CM.Data is not changed by this function |
---|
41 | OCS.CM.Delta is the delta correction |
---|
42 | 2. Both row and column weighting of the response matrix can be done. |
---|
43 | The default is unity row weights and column weight by the std of each column. |
---|
44 | |
---|
45 | See also <a href="setorbit.html" class="code" title="function [OCS, OCS0, V, S, ErrorFlag] = setorbit(varargin)">setorbit</a>, <a href="setorbitbump.html" class="code" title="function [OCS, OCS0, V, S, ErrorFlag] = setorbitbump(varargin)">setorbitbump</a>, <a href="rmdisp.html" class="code" title="function [DeltaRF, BPM, c, DispOrbit] = rmdisp(varargin)">rmdisp</a>, <a href="plotcm.html" class="code" title="function [DeltaRF, HCMEnergyChangeTotal, DeltaL] = plotcm(varargin)">plotcm</a></pre></div> |
---|
46 | |
---|
47 | <!-- crossreference --> |
---|
48 | <h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2> |
---|
49 | This function calls: |
---|
50 | <ul style="list-style-image:url(../matlabicon.gif)"> |
---|
51 | <li><a href="getdisp.html" class="code" title="function [Data, FileName] = getdisp(varargin)">getdisp</a> GETDISP - Returns the dispersion for a family (from file)</li><li><a href="getfamilydata.html" class="code" title="function [Data, ErrorFlag] = getfamilydata(Family, Field1, Field2, DeviceList)">getfamilydata</a> GETFAMILYDATA - Gets data associated with the accelerator control</li><li><a href="gethbpmfamily.html" class="code" title="function Family = gethbpmfamily">gethbpmfamily</a> GETHBPMFAMILY - Return the default horizontal BPM family</li><li><a href="gethcmfamily.html" class="code" title="function Family = gethcmfamily">gethcmfamily</a> GETHCMFAMILY - Returns the default horizontal corrector family</li><li><a href="getmcf.html" class="code" title="function Alpha = getmcf(ModelString)">getmcf</a> GETMCF - Returns the momentum compaction factor (MCF) stored in the AD or the model</li><li><a href="getrespmat.html" class="code" title="function [S, FileName] = getrespmat(varargin)">getrespmat</a> GETRESPMAT - Get response matrix data from a file</li><li><a href="getsp.html" class="code" title="function [SP, tout, DataTime, ErrorFlag] = getsp(Family, varargin)">getsp</a> GETSP - Gets setpoint channels</li><li><a href="getvbpmfamily.html" class="code" title="function Family = getvbpmfamily">getvbpmfamily</a> GETVBPMFAMILY - Return the default vertical BPM family</li><li><a href="getvcmfamily.html" class="code" title="function Family = getvcmfamily">getvcmfamily</a> GETVCMFAMILY - Returns the default vertical corrector family</li><li><a href="hw2physics.html" class="code" title="function S = hw2physics(Family, Field, value, DeviceList, Energy)">hw2physics</a> HW2PHYSICS - Converts from 'Hardware' units to 'Physics' units</li><li><a href="ismemberof.html" class="code" title="function [IsTest, Index] = ismemberof(FamilyName, Field, MemberString)">ismemberof</a> ISMEMBEROF - Returns turn if the membership information of a family (cell of strings)</li><li><a href="measbpmresp.html" class="code" title="function [Rmat, OutputFileName] = measbpmresp(varargin)">measbpmresp</a> MEASBPMRESP - Measures the BPM response matrix in the horizontal and vertical planes</li><li><a href="measdisp.html" class="code" title="function [Dx, Dy, FileName] = measdisp(varargin);">measdisp</a> MEASDISP - Measures the dispersion function</li><li><a href="physics2hw.html" class="code" title="function S = physics2hw(Family, Field, value, DeviceList, Energy)">physics2hw</a> PHYSICS2HW - Converts from 'Physics' units to 'Hardware' units</li></ul> |
---|
52 | This function is called by: |
---|
53 | <ul style="list-style-image:url(../matlabicon.gif)"> |
---|
54 | <li><a href="setorbit.html" class="code" title="function [OCS, OCS0, V, S, ErrorFlag] = setorbit(varargin)">setorbit</a> SETORBIT - Orbit correction function</li></ul> |
---|
55 | <!-- crossreference --> |
---|
56 | |
---|
57 | |
---|
58 | <h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2> |
---|
59 | <div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function [OCS, SmatNoWeights, S, U, V] = orbitcorrectionmethods(OCS, Smat, S, U, V)</a> |
---|
60 | 0002 <span class="comment">%ORBITCORRECTIONMETHODS - Some the orbit correction methods used on light sources</span> |
---|
61 | 0003 <span class="comment">%</span> |
---|
62 | 0004 <span class="comment">% [OCS, Smat, S, U, V] = orbitcorrectionmethods(OCS) % Get response matrix & compute SVD correction</span> |
---|
63 | 0005 <span class="comment">% [OCS, Smat, S, U, V] = orbitcorrectionmethods(OCS, Smat) % Compute SVD & correction</span> |
---|
64 | 0006 <span class="comment">% [OCS, Smat, S, U, V] = orbitcorrectionmethods(OCS, Smat, S, U, V) % Compute correction</span> |
---|
65 | 0007 <span class="comment">%</span> |
---|
66 | 0008 <span class="comment">% OCS.FitRF - Determines which RF freqency method to use</span> |
---|
67 | 0009 <span class="comment">% See setorbit for information on all the different flags.</span> |
---|
68 | 0010 <span class="comment">%</span> |
---|
69 | 0011 <span class="comment">% NOTES</span> |
---|
70 | 0012 <span class="comment">% 1. OCS.CM.Data is not changed by this function</span> |
---|
71 | 0013 <span class="comment">% OCS.CM.Delta is the delta correction</span> |
---|
72 | 0014 <span class="comment">% 2. Both row and column weighting of the response matrix can be done.</span> |
---|
73 | 0015 <span class="comment">% The default is unity row weights and column weight by the std of each column.</span> |
---|
74 | 0016 <span class="comment">%</span> |
---|
75 | 0017 <span class="comment">% See also setorbit, setorbitbump, rmdisp, plotcm</span> |
---|
76 | 0018 |
---|
77 | 0019 <span class="comment">%</span> |
---|
78 | 0020 <span class="comment">% Written by Gregory J. Portmann</span> |
---|
79 | 0021 <span class="comment">% Addition for Soleil by Laurent S. Nadolski</span> |
---|
80 | 0022 <span class="comment">% Constraints on min and max RF frequency variations</span> |
---|
81 | 0023 |
---|
82 | 0024 <span class="comment">% T = V(:,OCS.SVDIndex) * diag(S(OCS.SVDIndex).^(-1)) * U(:,OCS.SVDIndex)';</span> |
---|
83 | 0025 <span class="comment">% Delcm = T * (BPMWeight .* (GoalOrbitVec - StartOrbitVec));</span> |
---|
84 | 0026 <span class="comment">%</span> |
---|
85 | 0027 |
---|
86 | 0028 |
---|
87 | 0029 <span class="comment">% Initialize</span> |
---|
88 | 0030 FitRFDefault = 0; |
---|
89 | 0031 |
---|
90 | 0032 <span class="comment">% Machine dependent</span> |
---|
91 | 0033 <span class="keyword">if</span> strcmpi(<a href="getfamilydata.html" class="code" title="function [Data, ErrorFlag] = getfamilydata(Family, Field1, Field2, DeviceList)">getfamilydata</a>(<span class="string">'SubMachine'</span>),<span class="string">'Soleil'</span>) |
---|
92 | 0034 DeltaRFmin = 1e-7; <span class="comment">% MHz minimum variation allowed by Master oscillator</span> |
---|
93 | 0035 DeltaRFmax = 100e-6; <span class="comment">% MHz maximum variation allowed in one step</span> |
---|
94 | 0036 <span class="keyword">end</span> |
---|
95 | 0037 |
---|
96 | 0038 |
---|
97 | 0039 <span class="comment">% Input checking (should add more)</span> |
---|
98 | 0040 <span class="keyword">if</span> nargin < 2 |
---|
99 | 0041 Smat = []; |
---|
100 | 0042 <span class="keyword">end</span> |
---|
101 | 0043 <span class="keyword">if</span> nargin < 3 |
---|
102 | 0044 S = []; |
---|
103 | 0045 U = []; |
---|
104 | 0046 V = []; |
---|
105 | 0047 <span class="keyword">end</span> |
---|
106 | 0048 |
---|
107 | 0049 |
---|
108 | 0050 <span class="comment">% This function expects .BPM and .CM to be cell arrays.</span> |
---|
109 | 0051 <span class="comment">% This gets put back at the end of the function.</span> |
---|
110 | 0052 <span class="keyword">if</span> ~iscell(OCS.BPM) |
---|
111 | 0053 NoCellBPMFlag = 1; |
---|
112 | 0054 OCS.BPM = {OCS.BPM}; |
---|
113 | 0055 OCS.GoalOrbit = {OCS.GoalOrbit}; |
---|
114 | 0056 <span class="keyword">if</span> isfield(OCS, <span class="string">'BPMWeight'</span>) |
---|
115 | 0057 OCS.BPMWeight = {OCS.BPMWeight}; |
---|
116 | 0058 <span class="keyword">end</span> |
---|
117 | 0059 <span class="keyword">else</span> |
---|
118 | 0060 NoCellBPMFlag = 0; |
---|
119 | 0061 <span class="keyword">end</span> |
---|
120 | 0062 <span class="keyword">if</span> ~iscell(OCS.CM) |
---|
121 | 0063 NoCellCMFlag = 1; |
---|
122 | 0064 OCS.CM = {OCS.CM}; |
---|
123 | 0065 <span class="keyword">if</span> isfield(OCS, <span class="string">'CMWeight'</span>) |
---|
124 | 0066 <span class="keyword">if</span> ~iscell(OCS.CMWeight) |
---|
125 | 0067 OCS.CMWeight = {OCS.CMWeight}; |
---|
126 | 0068 <span class="keyword">end</span> |
---|
127 | 0069 <span class="keyword">end</span> |
---|
128 | 0070 <span class="keyword">else</span> |
---|
129 | 0071 NoCellCMFlag = 0; |
---|
130 | 0072 <span class="keyword">end</span> |
---|
131 | 0073 |
---|
132 | 0074 |
---|
133 | 0075 <span class="comment">% RF frequency methods in orbit correction</span> |
---|
134 | 0076 <span class="keyword">if</span> ~isfield(OCS, <span class="string">'FitRF'</span>) |
---|
135 | 0077 OCS.FitRF = FitRFDefault; |
---|
136 | 0078 <span class="keyword">end</span> |
---|
137 | 0079 <span class="keyword">if</span> isempty(OCS.FitRF) |
---|
138 | 0080 OCS.FitRF = FitRFDefault; |
---|
139 | 0081 <span class="keyword">end</span> |
---|
140 | 0082 |
---|
141 | 0083 |
---|
142 | 0084 <span class="comment">%%%%%%%%%%%%%%%%</span> |
---|
143 | 0085 <span class="comment">% The SVD Part %</span> |
---|
144 | 0086 <span class="comment">%%%%%%%%%%%%%%%%</span> |
---|
145 | 0087 <span class="keyword">if</span> isempty(U) |
---|
146 | 0088 <span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%</span> |
---|
147 | 0089 <span class="comment">% Get the response matrix %</span> |
---|
148 | 0090 <span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%</span> |
---|
149 | 0091 <span class="keyword">if</span> isempty(Smat) |
---|
150 | 0092 <span class="keyword">for</span> i = 1:length(OCS.BPM) |
---|
151 | 0093 Srow = []; |
---|
152 | 0094 <span class="keyword">for</span> j = 1:length(OCS.CM) |
---|
153 | 0095 <span class="comment">%if ModelRespFlag == 1</span> |
---|
154 | 0096 <span class="keyword">if</span> any(strcmpi(OCS.Flags,<span class="string">'ModelResp'</span>)) |
---|
155 | 0097 <span class="comment">% When using measbpmresp('Model')</span> |
---|
156 | 0098 <span class="comment">% Rmat(1,1) is always horizontal</span> |
---|
157 | 0099 <span class="comment">% Rmat(2,2) is always vertical</span> |
---|
158 | 0100 Rmat = <a href="measbpmresp.html" class="code" title="function [Rmat, OutputFileName] = measbpmresp(varargin)">measbpmresp</a>(<span class="string">'model'</span>,OCS.BPM{i},OCS.BPM{i}, OCS.CM{j},OCS.CM{j}, OCS.BPM{i}.Units); |
---|
159 | 0101 <span class="keyword">if</span> <a href="ismemberof.html" class="code" title="function [IsTest, Index] = ismemberof(FamilyName, Field, MemberString)">ismemberof</a>(OCS.BPM{i}.FamilyName, <a href="gethbpmfamily.html" class="code" title="function Family = gethbpmfamily">gethbpmfamily</a>) && <a href="ismemberof.html" class="code" title="function [IsTest, Index] = ismemberof(FamilyName, Field, MemberString)">ismemberof</a>(OCS.CM{j}.FamilyName,<a href="gethcmfamily.html" class="code" title="function Family = gethcmfamily">gethcmfamily</a>) |
---|
160 | 0102 S = Rmat(1,1).Data; |
---|
161 | 0103 <span class="keyword">elseif</span> <a href="ismemberof.html" class="code" title="function [IsTest, Index] = ismemberof(FamilyName, Field, MemberString)">ismemberof</a>(OCS.BPM{i}.FamilyName, <a href="getvbpmfamily.html" class="code" title="function Family = getvbpmfamily">getvbpmfamily</a>) && <a href="ismemberof.html" class="code" title="function [IsTest, Index] = ismemberof(FamilyName, Field, MemberString)">ismemberof</a>(OCS.CM{j}.FamilyName,<a href="gethcmfamily.html" class="code" title="function Family = gethcmfamily">gethcmfamily</a>) |
---|
162 | 0104 S = Rmat(2,1).Data; |
---|
163 | 0105 <span class="keyword">elseif</span> <a href="ismemberof.html" class="code" title="function [IsTest, Index] = ismemberof(FamilyName, Field, MemberString)">ismemberof</a>(OCS.BPM{i}.FamilyName, <a href="gethbpmfamily.html" class="code" title="function Family = gethbpmfamily">gethbpmfamily</a>) && <a href="ismemberof.html" class="code" title="function [IsTest, Index] = ismemberof(FamilyName, Field, MemberString)">ismemberof</a>(OCS.CM{j}.FamilyName,<a href="getvcmfamily.html" class="code" title="function Family = getvcmfamily">getvcmfamily</a>) |
---|
164 | 0106 S = Rmat(1,2).Data; |
---|
165 | 0107 <span class="keyword">elseif</span> <a href="ismemberof.html" class="code" title="function [IsTest, Index] = ismemberof(FamilyName, Field, MemberString)">ismemberof</a>(OCS.BPM{i}.FamilyName, <a href="getvbpmfamily.html" class="code" title="function Family = getvbpmfamily">getvbpmfamily</a>) && <a href="ismemberof.html" class="code" title="function [IsTest, Index] = ismemberof(FamilyName, Field, MemberString)">ismemberof</a>(OCS.CM{j}.FamilyName,<a href="getvcmfamily.html" class="code" title="function Family = getvcmfamily">getvcmfamily</a>) |
---|
166 | 0108 S = Rmat(2,2).Data; |
---|
167 | 0109 <span class="keyword">else</span> |
---|
168 | 0110 error(<span class="string">'Problem getting the model response matrix'</span>); |
---|
169 | 0111 <span class="keyword">end</span> |
---|
170 | 0112 <span class="keyword">else</span> |
---|
171 | 0113 <span class="comment">% Get the golden BPM response matrix</span> |
---|
172 | 0114 [S, FileName] = <a href="getrespmat.html" class="code" title="function [S, FileName] = getrespmat(varargin)">getrespmat</a>(OCS.BPM{i}, OCS.CM{j}, <span class="string">'Numeric'</span>); |
---|
173 | 0115 <span class="keyword">if</span> any(find(isnan(S))) |
---|
174 | 0116 error(<span class="string">'Not all BPMs and correctors exist in the response matrix.'</span>); |
---|
175 | 0117 <span class="keyword">end</span> |
---|
176 | 0118 <span class="keyword">end</span> |
---|
177 | 0119 <span class="keyword">if</span> length(OCS.GoalOrbit{i}) ~= size(S,1) |
---|
178 | 0120 error(<span class="string">'The goal orbit vector length does not equal the response matrix row length'</span>); |
---|
179 | 0121 <span class="keyword">end</span> |
---|
180 | 0122 |
---|
181 | 0123 Srow = [Srow S]; |
---|
182 | 0124 <span class="keyword">end</span> |
---|
183 | 0125 Smat = [Smat; Srow]; |
---|
184 | 0126 <span class="keyword">end</span> |
---|
185 | 0127 <span class="comment">%SmatCheck = measbpmresp('Model','Numeric');</span> |
---|
186 | 0128 |
---|
187 | 0129 |
---|
188 | 0130 <span class="comment">%%%%%%%%%%%%%%%%%%</span> |
---|
189 | 0131 <span class="comment">% Get Dispersion %</span> |
---|
190 | 0132 <span class="comment">%%%%%%%%%%%%%%%%%%</span> |
---|
191 | 0133 Eta = []; |
---|
192 | 0134 <span class="keyword">if</span> OCS.FitRF |
---|
193 | 0135 <span class="keyword">for</span> i = 1:length(OCS.BPM) |
---|
194 | 0136 <span class="keyword">if</span> any(strcmpi(OCS.Flags,<span class="string">'ModelDisp'</span>)) |
---|
195 | 0137 <span class="comment">%if strcmpi(DispFlag,'ModelDisp')</span> |
---|
196 | 0138 DispOrbit = <a href="measdisp.html" class="code" title="function [Dx, Dy, FileName] = measdisp(varargin);">measdisp</a>(OCS.BPM{i}, <span class="string">'Hardware'</span>, <span class="string">'Numeric'</span>, <span class="string">'Model'</span>); |
---|
197 | 0139 <span class="keyword">elseif</span> any(strcmpi(OCS.Flags,<span class="string">'MeasDisp'</span>)) |
---|
198 | 0140 <span class="comment">%elseif strcmpi(DispFlag,'MeasDisp')</span> |
---|
199 | 0141 DispOrbit = <a href="measdisp.html" class="code" title="function [Dx, Dy, FileName] = measdisp(varargin);">measdisp</a>(OCS.BPM{i}, <span class="string">'Hardware'</span>, <span class="string">'Numeric'</span>); |
---|
200 | 0142 <span class="keyword">elseif</span> any(strcmpi(OCS.Flags,<span class="string">'GoldenDisp'</span>)) |
---|
201 | 0143 <span class="comment">%elseif strcmpi(DispFlag,'GoldenDisp')</span> |
---|
202 | 0144 DispOrbit = <a href="getdisp.html" class="code" title="function [Data, FileName] = getdisp(varargin)">getdisp</a>(OCS.BPM{i}, <span class="string">'Hardware'</span>, <span class="string">'Numeric'</span>); |
---|
203 | 0145 <span class="keyword">end</span> |
---|
204 | 0146 |
---|
205 | 0147 <span class="keyword">if</span> strcmpi(OCS.BPM{i}.Units, <span class="string">'Physics'</span>) |
---|
206 | 0148 <span class="comment">% Put the RF change in physics units, not energy change</span> |
---|
207 | 0149 RFhw = <a href="physics2hw.html" class="code" title="function S = physics2hw(Family, Field, value, DeviceList, Energy)">physics2hw</a>(OCS.RF); |
---|
208 | 0150 DispOrbit = DispOrbit / (OCS.RF.Data / RFhw.Data); |
---|
209 | 0151 <span class="keyword">end</span> |
---|
210 | 0152 |
---|
211 | 0153 <span class="keyword">if</span> isempty(DispOrbit) |
---|
212 | 0154 error(<span class="string">'Did not find or generate dispersion orbit properly'</span>); |
---|
213 | 0155 <span class="keyword">end</span> |
---|
214 | 0156 <span class="keyword">if</span> any(isnan(DispOrbit)) |
---|
215 | 0157 error(<span class="string">'The dispersion data has at least one NaN.'</span>); |
---|
216 | 0158 <span class="keyword">end</span> |
---|
217 | 0159 |
---|
218 | 0160 Eta = [Eta; DispOrbit]; |
---|
219 | 0161 <span class="keyword">end</span> |
---|
220 | 0162 <span class="keyword">end</span> |
---|
221 | 0163 OCS.Eta = Eta; |
---|
222 | 0164 <span class="keyword">end</span> |
---|
223 | 0165 <span class="keyword">end</span> |
---|
224 | 0166 |
---|
225 | 0167 |
---|
226 | 0168 <span class="comment">% Save a weight free response matrix</span> |
---|
227 | 0169 SmatNoWeights = Smat; |
---|
228 | 0170 |
---|
229 | 0171 |
---|
230 | 0172 <span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%</span> |
---|
231 | 0173 <span class="comment">% Build the orbit vectors %</span> |
---|
232 | 0174 <span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%</span> |
---|
233 | 0175 StartOrbitVec = []; |
---|
234 | 0176 GoalOrbitVec = []; |
---|
235 | 0177 BPMWeight = []; |
---|
236 | 0178 <span class="keyword">for</span> i = 1:length(OCS.BPM) |
---|
237 | 0179 StartOrbitVec = [StartOrbitVec; OCS.BPM{i}.Data(:)]; |
---|
238 | 0180 GoalOrbitVec = [GoalOrbitVec; OCS.GoalOrbit{i}(:)]; |
---|
239 | 0181 |
---|
240 | 0182 <span class="keyword">if</span> ~isfield(OCS, <span class="string">'BPMWeight'</span>) || isempty(OCS.BPMWeight{i}) |
---|
241 | 0183 BPMWeight = [BPMWeight; ones(length(OCS.BPM{i}.Data),1)]; |
---|
242 | 0184 <span class="keyword">elseif</span> isscalar(OCS.BPMWeight{i}) |
---|
243 | 0185 BPMWeight = [BPMWeight; ones(length(OCS.BPM{i}.Data),1) * OCS.BPMWeight{i}]; |
---|
244 | 0186 <span class="keyword">else</span> |
---|
245 | 0187 BPMWeight = [BPMWeight; OCS.BPMWeight{i}(:)]; |
---|
246 | 0188 <span class="keyword">end</span> |
---|
247 | 0189 <span class="keyword">end</span> |
---|
248 | 0190 |
---|
249 | 0191 |
---|
250 | 0192 <span class="comment">% Build column weight vector</span> |
---|
251 | 0193 CMWeight = []; |
---|
252 | 0194 <span class="keyword">for</span> i = 1:length(OCS.CM) |
---|
253 | 0195 <span class="keyword">if</span> ~isfield(OCS, <span class="string">'CMWeight'</span>) || isempty(OCS.CMWeight{i}) |
---|
254 | 0196 <span class="comment">% When using all singular values this does not do anything</span> |
---|
255 | 0197 <span class="comment">% The amp to radian conversion is probably a better normalizer</span> |
---|
256 | 0198 <span class="comment">%CMWeight = 1 ./ sqrt(sum(Smat.^2));</span> |
---|
257 | 0199 CMWeight = 1./std(Smat)'; |
---|
258 | 0200 |
---|
259 | 0201 <span class="comment">% No weight</span> |
---|
260 | 0202 <span class="comment">%CMWeight = ones(size(Smat,2),1);</span> |
---|
261 | 0203 |
---|
262 | 0204 <span class="keyword">break</span>; |
---|
263 | 0205 <span class="comment">%CMWeight = [CMWeight; ones(length(OCS.CM{i}.Data),1)];</span> |
---|
264 | 0206 |
---|
265 | 0207 <span class="keyword">elseif</span> isscalar(OCS.CMWeight{i}) |
---|
266 | 0208 CMWeight = [CMWeight; ones(length(OCS.CM{i}.Data),1) * OCS.CMWeight{i}]; |
---|
267 | 0209 |
---|
268 | 0210 <span class="keyword">else</span> |
---|
269 | 0211 CMWeight = [CMWeight; OCS.CMWeight{i}(:)]; |
---|
270 | 0212 <span class="keyword">end</span> |
---|
271 | 0213 <span class="keyword">end</span> |
---|
272 | 0214 |
---|
273 | 0215 |
---|
274 | 0216 <span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span> |
---|
275 | 0217 <span class="comment">% Add a column weight to the response matrix %</span> |
---|
276 | 0218 <span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span> |
---|
277 | 0219 <span class="comment">%if isempty(U)</span> |
---|
278 | 0220 <span class="keyword">for</span> j = 1:length(CMWeight) |
---|
279 | 0221 Smat(:,j) = Smat(:,j) * CMWeight(j); |
---|
280 | 0222 <span class="keyword">end</span> |
---|
281 | 0223 <span class="comment">%end</span> |
---|
282 | 0224 |
---|
283 | 0225 |
---|
284 | 0226 <span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span> |
---|
285 | 0227 <span class="comment">% Add a weighted disperion as a column of the response matrix %</span> |
---|
286 | 0228 <span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span> |
---|
287 | 0229 <span class="keyword">if</span> OCS.FitRF == 0 |
---|
288 | 0230 <span class="comment">% Don't fit the RF frequency</span> |
---|
289 | 0231 OCS.DeltaRF = 0; |
---|
290 | 0232 <span class="keyword">elseif</span> any(OCS.FitRF == [1 2 3 4 5]) |
---|
291 | 0233 <span class="comment">% Column weighting (changing the units or sensitivity) seems to be a good thing.</span> |
---|
292 | 0234 <span class="comment">% (At least at the ALS working in hardware units.)</span> |
---|
293 | 0235 <span class="comment">% Weight by more than the average std of Smat also seems to give good results but</span> |
---|
294 | 0236 <span class="comment">% I'm not sure it's necessary or should be done.</span> |
---|
295 | 0237 RFWeight = 10 * mean(std(Smat)) / std(OCS.Eta); |
---|
296 | 0238 Smat = [Smat RFWeight*OCS.Eta]; |
---|
297 | 0239 <span class="keyword">else</span> |
---|
298 | 0240 error(<span class="string">'Unknown RF correction method.'</span>); |
---|
299 | 0241 <span class="keyword">end</span> |
---|
300 | 0242 |
---|
301 | 0243 |
---|
302 | 0244 |
---|
303 | 0245 <span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span> |
---|
304 | 0246 <span class="comment">% Add a row weight to the response matrix %</span> |
---|
305 | 0247 <span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span> |
---|
306 | 0248 <span class="comment">% Weighted LS</span> |
---|
307 | 0249 <span class="comment">% Weighted LS was developed for heteroscedastic errors but it's used here for other reasons</span> |
---|
308 | 0250 <span class="comment">% W*(Mmeas - Mmodel) = W*A*b + W*e, where the e's are gausian, zero mean, independent, but not constant variance</span> |
---|
309 | 0251 <span class="comment">% W is chosen to make E(Wee'W') = sigma * I (ie, W * e has a constant variance)</span> |
---|
310 | 0252 <span class="comment">%</span> |
---|
311 | 0253 <span class="comment">% The new LS equations are,</span> |
---|
312 | 0254 <span class="comment">% b = inv(Amod'*W' * W*Amod) * Amod'*W' * W*(Xgoal - Xmeasured); % Parameter fit</span> |
---|
313 | 0255 <span class="comment">% b = V(:,Ivec) * b;</span> |
---|
314 | 0256 <span class="comment">% bvar = inv(Amod'*W'*W*Amod);</span> |
---|
315 | 0257 <span class="comment">%</span> |
---|
316 | 0258 <span class="comment">% Since the weight matrix, W, is only going to be diagonal, it is less memory</span> |
---|
317 | 0259 <span class="comment">% to scaled Amod and (Mmeas-Model) by the diagonal term and not create the matrix W</span> |
---|
318 | 0260 <span class="keyword">if</span> ~all(BPMWeight == 1) |
---|
319 | 0261 <span class="keyword">for</span> i = 1:size(Smat,1) |
---|
320 | 0262 Smat(i,:) = BPMWeight(i) * Smat(i,:); |
---|
321 | 0263 <span class="keyword">end</span> |
---|
322 | 0264 <span class="keyword">end</span> |
---|
323 | 0265 |
---|
324 | 0266 |
---|
325 | 0267 <span class="comment">%%%%%%%%%%%%%%%%</span> |
---|
326 | 0268 <span class="comment">% The SVD Part %</span> |
---|
327 | 0269 <span class="comment">%%%%%%%%%%%%%%%%</span> |
---|
328 | 0270 <span class="keyword">if</span> isempty(U) |
---|
329 | 0271 <span class="keyword">if</span> any(any(isnan(Smat))) |
---|
330 | 0272 error(<span class="string">'The response matrix has at least one NaN.'</span>); |
---|
331 | 0273 <span class="keyword">end</span> |
---|
332 | 0274 |
---|
333 | 0275 <span class="comment">% Do the SVD</span> |
---|
334 | 0276 [U, S, V] = svd(Smat, 0); <span class="comment">%'econ');</span> |
---|
335 | 0277 S = diag(S); |
---|
336 | 0278 <span class="keyword">end</span> |
---|
337 | 0279 |
---|
338 | 0280 |
---|
339 | 0281 <span class="comment">% Determine the singular vector and error check</span> |
---|
340 | 0282 <span class="keyword">if</span> isnumeric(OCS.SVDIndex) |
---|
341 | 0283 <span class="keyword">if</span> length(OCS.SVDIndex) == 1 |
---|
342 | 0284 <span class="keyword">if</span> rem(OCS.SVDIndex,1) == 0 |
---|
343 | 0285 OCS.SVDIndex = 1:OCS.SVDIndex; |
---|
344 | 0286 <span class="keyword">else</span> |
---|
345 | 0287 <span class="comment">% Use threshold</span> |
---|
346 | 0288 Ivec = find(S > max(S)*OCS.SVDIndex); |
---|
347 | 0289 OCS.SVDIndex = Ivec; |
---|
348 | 0290 <span class="keyword">end</span> |
---|
349 | 0291 <span class="keyword">end</span> |
---|
350 | 0292 <span class="keyword">elseif</span> ischar(OCS.SVDIndex) |
---|
351 | 0293 <span class="keyword">if</span> strcmpi(OCS.SVDIndex, <span class="string">'All'</span>) |
---|
352 | 0294 OCS.SVDIndex = 1:length(S); |
---|
353 | 0295 <span class="keyword">else</span> |
---|
354 | 0296 error(<span class="string">'OCS.SVDIndex unknown'</span>); |
---|
355 | 0297 <span class="keyword">end</span> |
---|
356 | 0298 <span class="keyword">else</span> |
---|
357 | 0299 error(<span class="string">'OCS.SVDIndex unknown'</span>); |
---|
358 | 0300 <span class="keyword">end</span> |
---|
359 | 0301 |
---|
360 | 0302 <span class="comment">% Nothing greater then the total number is singular values</span> |
---|
361 | 0303 i = find(OCS.SVDIndex > length(S)); |
---|
362 | 0304 <span class="keyword">if</span> ~isempty(i) |
---|
363 | 0305 warning(<span class="string">'Requested number of singular values is greater than the total. Removing %d values.'</span>,length(i)); |
---|
364 | 0306 OCS.SVDIndex(i) = length(S); |
---|
365 | 0307 <span class="keyword">end</span> |
---|
366 | 0308 |
---|
367 | 0309 <span class="comment">% No non-integers</span> |
---|
368 | 0310 OCS.SVDIndex = round(OCS.SVDIndex); |
---|
369 | 0311 |
---|
370 | 0312 <span class="comment">% Nothing less than zero</span> |
---|
371 | 0313 i = find(OCS.SVDIndex <= 0); |
---|
372 | 0314 OCS.SVDIndex(i) = length(S); |
---|
373 | 0315 |
---|
374 | 0316 <span class="comment">% No repeats</span> |
---|
375 | 0317 OCS.SVDIndex = sort(OCS.SVDIndex); |
---|
376 | 0318 i = find(diff(OCS.SVDIndex)==0); |
---|
377 | 0319 OCS.SVDIndex(i) = []; |
---|
378 | 0320 |
---|
379 | 0321 <span class="keyword">if</span> isempty(OCS.SVDIndex) |
---|
380 | 0322 error(<span class="string">'Singular values vector is empty'</span>); |
---|
381 | 0323 <span class="keyword">end</span> |
---|
382 | 0324 |
---|
383 | 0325 |
---|
384 | 0326 <span class="keyword">if</span> OCS.FitRF == 2 || OCS.FitRF == 3 |
---|
385 | 0327 <span class="comment">% Constrained L.S. with SVD</span> |
---|
386 | 0328 <span class="comment">% Constraint: Sum of the energy change to zero</span> |
---|
387 | 0329 <span class="comment">% Note: Model required</span> |
---|
388 | 0330 |
---|
389 | 0331 R = []; |
---|
390 | 0332 HCMEnergyChangeTotal = 0; |
---|
391 | 0333 <span class="keyword">for</span> i = 1:length(OCS.CM) |
---|
392 | 0334 <span class="keyword">if</span> <a href="ismemberof.html" class="code" title="function [IsTest, Index] = ismemberof(FamilyName, Field, MemberString)">ismemberof</a>(OCS.CM{i}.FamilyName, <span class="string">'HCM'</span>) |
---|
393 | 0335 L = <a href="getfamilydata.html" class="code" title="function [Data, ErrorFlag] = getfamilydata(Family, Field1, Field2, DeviceList)">getfamilydata</a>(<span class="string">'Circumference'</span>); |
---|
394 | 0336 |
---|
395 | 0337 <span class="keyword">if</span> ~isfield(OCS.CM{i}, <span class="string">'Dispersion'</span>) |
---|
396 | 0338 <span class="comment">% Dispersion at the correctors in physics units</span> |
---|
397 | 0339 [OCS.CM{i}.Dispersion, DyHCM] = modeldisp([], OCS.CM{i}.FamilyName, OCS.CM{i}.DeviceList, <span class="string">'Numeric'</span>, <span class="string">'Physics'</span>); |
---|
398 | 0340 <span class="keyword">end</span> |
---|
399 | 0341 |
---|
400 | 0342 <span class="comment">% Present corrector setpoint in physics units</span> |
---|
401 | 0343 HCM = <a href="hw2physics.html" class="code" title="function S = hw2physics(Family, Field, value, DeviceList, Energy)">hw2physics</a>(OCS.CM{i}); |
---|
402 | 0344 |
---|
403 | 0345 |
---|
404 | 0346 <span class="comment">% Move ALS specific code to setorbitdefault ???</span> |
---|
405 | 0347 <span class="keyword">if</span> strcmpi(<a href="getfamilydata.html" class="code" title="function [Data, ErrorFlag] = getfamilydata(Family, Field1, Field2, DeviceList)">getfamilydata</a>(<span class="string">'Machine'</span>),<span class="string">'ALS'</span>) |
---|
406 | 0348 <span class="comment">% For the ALS, either the HCMCHICANE families need to be included or</span> |
---|
407 | 0349 <span class="comment">% the chicane "part" of the HCMs needs to be removed. This will remove the chicane.</span> |
---|
408 | 0350 Energy = <a href="getfamilydata.html" class="code" title="function [Data, ErrorFlag] = getfamilydata(Family, Field1, Field2, DeviceList)">getfamilydata</a>(<span class="string">'Energy'</span>); |
---|
409 | 0351 |
---|
410 | 0352 <span class="comment">% Sector 6</span> |
---|
411 | 0353 <span class="comment">% Off 1.9 GeV 1.5 GeV</span> |
---|
412 | 0354 <span class="comment">% HCMCHICANEM(6,1) 80.0 18.0 ?</span> |
---|
413 | 0355 <span class="comment">% HCMCHICANEM(6,1) 80.0 20.0 ?</span> |
---|
414 | 0356 <span class="comment">% HCM(6,1) 0.0 18.8 ?</span> |
---|
415 | 0357 ihcm = findrowindex([6 1], OCS.CM{i}.DeviceList); |
---|
416 | 0358 <span class="keyword">if</span> length(ihcm) == 1 |
---|
417 | 0359 <span class="keyword">try</span> |
---|
418 | 0360 <span class="keyword">if</span> <a href="getsp.html" class="code" title="function [SP, tout, DataTime, ErrorFlag] = getsp(Family, varargin)">getsp</a>(<span class="string">'HCMCHICANEM'</span>,[6 1]) < 70 |
---|
419 | 0361 <span class="comment">% Assume sector 6 chicane is on</span> |
---|
420 | 0362 <span class="keyword">if</span> Energy == 1.9 |
---|
421 | 0363 HCM.Data(ihcm) = HCM.Data(ihcm) + <a href="hw2physics.html" class="code" title="function S = hw2physics(Family, Field, value, DeviceList, Energy)">hw2physics</a>(OCS.CM{i}.FamilyName, OCS.CM{i}.Field, -18.8, [6 1]); |
---|
422 | 0364 <span class="keyword">else</span> |
---|
423 | 0365 HCM.Data(ihcm) = HCM.Data(ihcm) + <a href="hw2physics.html" class="code" title="function S = hw2physics(Family, Field, value, DeviceList, Energy)">hw2physics</a>(OCS.CM{i}.FamilyName, OCS.CM{i}.Field, -18.8*1.5/1.9, [6 1]); |
---|
424 | 0366 <span class="keyword">end</span> |
---|
425 | 0367 <span class="keyword">end</span> |
---|
426 | 0368 <span class="keyword">catch</span> |
---|
427 | 0369 fprintf(<span class="string">'%s\n'</span>, lasterr); |
---|
428 | 0370 fprintf(<span class="string">'Problem reading HCMCHICANEM(6,1). The chicane "offset" on HCM(6,1) will not be removed.\n\n'</span>); |
---|
429 | 0371 <span class="keyword">end</span> |
---|
430 | 0372 <span class="keyword">end</span> |
---|
431 | 0373 |
---|
432 | 0374 <span class="comment">% Sector 11</span> |
---|
433 | 0375 <span class="comment">% Off 1.9 GeV 1.5 GeV</span> |
---|
434 | 0376 <span class="comment">% HCMCHICANEM(11,1) 80.0 40.5 52.0</span> |
---|
435 | 0377 <span class="comment">% HCMCHICANEM(11,1) 80.0 40.5 52.0</span> |
---|
436 | 0378 <span class="comment">% HCM(10,8) 0.0 -17.0 -14.0</span> |
---|
437 | 0379 <span class="comment">% HCM(11,1) 0.0 -17.0 -14.0</span> |
---|
438 | 0380 ihcm = findrowindex([10 8], OCS.CM{i}.DeviceList); |
---|
439 | 0381 <span class="keyword">if</span> length(ihcm) == 1 |
---|
440 | 0382 <span class="keyword">try</span> |
---|
441 | 0383 <span class="keyword">if</span> <a href="getsp.html" class="code" title="function [SP, tout, DataTime, ErrorFlag] = getsp(Family, varargin)">getsp</a>(<span class="string">'HCMCHICANEM'</span>,[11 1]) < 60 |
---|
442 | 0384 <span class="comment">% Assume sector 11 chicane is on</span> |
---|
443 | 0385 <span class="keyword">if</span> Energy == 1.9 |
---|
444 | 0386 HCM.Data(ihcm) = HCM.Data(ihcm) + <a href="hw2physics.html" class="code" title="function S = hw2physics(Family, Field, value, DeviceList, Energy)">hw2physics</a>(OCS.CM{i}.FamilyName, OCS.CM{i}.Field, [17; 17], [10 8;11 1]); |
---|
445 | 0387 <span class="keyword">else</span> |
---|
446 | 0388 HCM.Data(ihcm) = HCM.Data(ihcm) + <a href="hw2physics.html" class="code" title="function S = hw2physics(Family, Field, value, DeviceList, Energy)">hw2physics</a>(OCS.CM{i}.FamilyName, OCS.CM{i}.Field, [14; 14], [10 8;11 1]); |
---|
447 | 0389 <span class="keyword">end</span> |
---|
448 | 0390 <span class="keyword">end</span> |
---|
449 | 0391 <span class="keyword">catch</span> |
---|
450 | 0392 fprintf(<span class="string">'%s\n'</span>, lasterr); |
---|
451 | 0393 fprintf(<span class="string">'Due to an error, the chicane "offset" on HCM(10,8) will not be removed.n\n'</span>); |
---|
452 | 0394 <span class="keyword">end</span> |
---|
453 | 0395 <span class="keyword">end</span> |
---|
454 | 0396 ihcm = findrowindex([11 1], OCS.CM{i}.DeviceList); |
---|
455 | 0397 <span class="keyword">if</span> length(ihcm) == 1 |
---|
456 | 0398 <span class="keyword">try</span> |
---|
457 | 0399 <span class="keyword">if</span> <a href="getsp.html" class="code" title="function [SP, tout, DataTime, ErrorFlag] = getsp(Family, varargin)">getsp</a>(<span class="string">'HCMCHICANEM'</span>,[11 1]) < 60 |
---|
458 | 0400 <span class="comment">% Assume sector 11 chicane is on</span> |
---|
459 | 0401 <span class="keyword">if</span> Energy == 1.9 |
---|
460 | 0402 HCM.Data(ihcm) = HCM.Data(ihcm) + <a href="hw2physics.html" class="code" title="function S = hw2physics(Family, Field, value, DeviceList, Energy)">hw2physics</a>(OCS.CM{i}.FamilyName, OCS.CM{i}.Field, [17; 17], [10 8;11 1]); |
---|
461 | 0403 <span class="keyword">else</span> |
---|
462 | 0404 HCM.Data(ihcm) = HCM.Data(ihcm) + <a href="hw2physics.html" class="code" title="function S = hw2physics(Family, Field, value, DeviceList, Energy)">hw2physics</a>(OCS.CM{i}.FamilyName, OCS.CM{i}.Field, [14; 14], [10 8;11 1]); |
---|
463 | 0405 <span class="keyword">end</span> |
---|
464 | 0406 <span class="keyword">end</span> |
---|
465 | 0407 <span class="keyword">catch</span> |
---|
466 | 0408 fprintf(<span class="string">'%s\n'</span>, lasterr); |
---|
467 | 0409 fprintf(<span class="string">'Due to an error, the chicane "offset" on HCM(11,1) will not be removed.n\n'</span>); |
---|
468 | 0410 <span class="keyword">end</span> |
---|
469 | 0411 <span class="keyword">end</span> |
---|
470 | 0412 <span class="keyword">end</span> |
---|
471 | 0413 <span class="comment">% END ALS only</span> |
---|
472 | 0414 |
---|
473 | 0415 |
---|
474 | 0416 <span class="comment">% Energy change to remove</span> |
---|
475 | 0417 HCMEnergyChangeTotal = HCMEnergyChangeTotal + sum(-1 * HCM.Data .* OCS.CM{i}.Dispersion / <a href="getmcf.html" class="code" title="function Alpha = getmcf(ModelString)">getmcf</a> / L); |
---|
476 | 0418 |
---|
477 | 0419 <span class="comment">% Energy scaling must be in physics units, hence the (HCM.Data./OCS.CM{i}.Data)</span> |
---|
478 | 0420 <span class="keyword">if</span> strcmpi(OCS.CM{i}.Units, <span class="string">'Hardware'</span>) |
---|
479 | 0421 <span class="comment">% PhysicsUnitsScaling = (HCM.Data./OCS.CM{i}.Data); %This does not work for zero setpoints</span> |
---|
480 | 0422 PhysicsUnitsScaling = <a href="hw2physics.html" class="code" title="function S = hw2physics(Family, Field, value, DeviceList, Energy)">hw2physics</a>(OCS.CM{i}. FamilyName,OCS.CM{i}.Field, 1, OCS.CM{i}.DeviceList); |
---|
481 | 0423 <span class="keyword">else</span> |
---|
482 | 0424 PhysicsUnitsScaling = 1; |
---|
483 | 0425 <span class="keyword">end</span> |
---|
484 | 0426 HCMEnergyChangeScalar = -1 * PhysicsUnitsScaling .* OCS.CM{i}.Dispersion / <a href="getmcf.html" class="code" title="function Alpha = getmcf(ModelString)">getmcf</a> / L; |
---|
485 | 0427 R = [R HCMEnergyChangeScalar(:)']; |
---|
486 | 0428 |
---|
487 | 0429 <span class="comment">% Sum of correctors to zero</span> |
---|
488 | 0430 <span class="comment">%R = [R ones(size(HCMEnergyChange(:)'))];</span> |
---|
489 | 0431 <span class="keyword">else</span> |
---|
490 | 0432 R = [R zeros(1,size(OCS.CM{i}.DeviceList,1))]; |
---|
491 | 0433 <span class="keyword">end</span> |
---|
492 | 0434 <span class="keyword">end</span> |
---|
493 | 0435 |
---|
494 | 0436 <span class="keyword">if</span> OCS.FitRF == 2 |
---|
495 | 0437 <span class="comment">% Also remove the energy change due to the present corrector setpoints</span> |
---|
496 | 0438 r = -HCMEnergyChangeTotal/2; <span class="comment">% Not sure about the divide by 2</span> |
---|
497 | 0439 <span class="keyword">elseif</span> OCS.FitRF == 3 |
---|
498 | 0440 <span class="comment">% Only remove energy change due to the incremental change in the correctors</span> |
---|
499 | 0441 r = 0; |
---|
500 | 0442 <span class="keyword">end</span> |
---|
501 | 0443 |
---|
502 | 0444 <span class="comment">% Add a zero for no constaint on the RF</span> |
---|
503 | 0445 R = [R 0]; |
---|
504 | 0446 |
---|
505 | 0447 |
---|
506 | 0448 <span class="comment">% Projection onto the columns of Smat*V (or U)</span> |
---|
507 | 0449 <span class="comment">% Since the correctors are V*b, the restriction in corrector strength is R*V</span> |
---|
508 | 0450 X = Smat * V(:,OCS.SVDIndex); |
---|
509 | 0451 R = R * V(:,OCS.SVDIndex); |
---|
510 | 0452 b = inv(X'*X)*X' * (BPMWeight .* (GoalOrbitVec - StartOrbitVec)); |
---|
511 | 0453 br = b - inv(X'*X)*R'*inv(R*inv(X'*X)*R')*(R*b-r); |
---|
512 | 0454 Delcm = V(:,OCS.SVDIndex) * br; |
---|
513 | 0455 |
---|
514 | 0456 <span class="keyword">elseif</span> OCS.FitRF == 4 |
---|
515 | 0457 <span class="comment">% Constrained L.S. with SVD</span> |
---|
516 | 0458 <span class="comment">% Constraint: dot(DispersionX, Smat * dHCM) = 0</span> |
---|
517 | 0459 <span class="comment">% or dot(DispersionX' * Smat, dHCM) = 0 or total dot(DisperionX, Smat * HCM)</span> |
---|
518 | 0460 R = []; |
---|
519 | 0461 DispDotSmatHCM = 0; |
---|
520 | 0462 <span class="keyword">for</span> i = 1:length(OCS.BPM) |
---|
521 | 0463 <span class="keyword">if</span> strcmpi(OCS.BPM{i}.FamilyName, <span class="string">'BPMx'</span>) || <a href="ismemberof.html" class="code" title="function [IsTest, Index] = ismemberof(FamilyName, Field, MemberString)">ismemberof</a>(OCS.BPM{i}.FamilyName, <span class="string">'BPMx'</span>) |
---|
522 | 0464 <span class="keyword">if</span> ~isfield(OCS.BPM{i}, <span class="string">'Dispersion'</span>) |
---|
523 | 0465 <span class="comment">% Dispersion at the correctors in physics units</span> |
---|
524 | 0466 [OCS.BPM{i}.Dispersion, Dy] = <a href="getdisp.html" class="code" title="function [Data, FileName] = getdisp(varargin)">getdisp</a>(OCS.BPM{i}.FamilyName, OCS.BPM{i}.DeviceList, <span class="string">'Numeric'</span>, OCS.BPM{i}.Units); |
---|
525 | 0467 <span class="keyword">end</span> |
---|
526 | 0468 R = [R OCS.BPM{i}.Dispersion(:)']; |
---|
527 | 0469 |
---|
528 | 0470 HCM = <a href="physics2hw.html" class="code" title="function S = physics2hw(Family, Field, value, DeviceList, Energy)">physics2hw</a>(OCS.CM{i}); |
---|
529 | 0471 |
---|
530 | 0472 |
---|
531 | 0473 <span class="comment">% Move ALS specific code to setorbitdefault ???</span> |
---|
532 | 0474 <span class="keyword">if</span> strcmpi(<a href="getfamilydata.html" class="code" title="function [Data, ErrorFlag] = getfamilydata(Family, Field1, Field2, DeviceList)">getfamilydata</a>(<span class="string">'Machine'</span>),<span class="string">'ALS'</span>) |
---|
533 | 0475 <span class="comment">% For the ALS, either the HCMCHICANE families need to be included or</span> |
---|
534 | 0476 <span class="comment">% the chicane "part" of the HCMs needs to be removed. This will remove the chicane.</span> |
---|
535 | 0477 Energy = <a href="getfamilydata.html" class="code" title="function [Data, ErrorFlag] = getfamilydata(Family, Field1, Field2, DeviceList)">getfamilydata</a>(<span class="string">'Energy'</span>); |
---|
536 | 0478 |
---|
537 | 0479 <span class="comment">% Sector 6</span> |
---|
538 | 0480 <span class="comment">% Off 1.9 GeV 1.5 GeV</span> |
---|
539 | 0481 <span class="comment">% HCMCHICANEM(6,1) 80.0 18.0 ?</span> |
---|
540 | 0482 <span class="comment">% HCMCHICANEM(6,1) 80.0 20.0 ?</span> |
---|
541 | 0483 <span class="comment">% HCM(6,1) 0.0 18.8 ?</span> |
---|
542 | 0484 ihcm = findrowindex([6 1], OCS.CM{i}.DeviceList); |
---|
543 | 0485 <span class="keyword">if</span> length(ihcm) == 1 |
---|
544 | 0486 <span class="keyword">try</span> |
---|
545 | 0487 <span class="keyword">if</span> <a href="getsp.html" class="code" title="function [SP, tout, DataTime, ErrorFlag] = getsp(Family, varargin)">getsp</a>(<span class="string">'HCMCHICANEM'</span>,[6 1]) < 70 |
---|
546 | 0488 <span class="comment">% Assume sector 6 chicane is on</span> |
---|
547 | 0489 <span class="keyword">if</span> Energy == 1.9 |
---|
548 | 0490 HCM.Data(ihcm) = HCM.Data(ihcm) - 18.8; |
---|
549 | 0491 <span class="keyword">else</span> |
---|
550 | 0492 HCM.Data(ihcm) = HCM.Data(ihcm) - 18.8*1.5/1.9; |
---|
551 | 0493 <span class="keyword">end</span> |
---|
552 | 0494 <span class="keyword">end</span> |
---|
553 | 0495 <span class="keyword">catch</span> |
---|
554 | 0496 fprintf(<span class="string">'%s\n'</span>, lasterr); |
---|
555 | 0497 fprintf(<span class="string">'Due to an error, the chicane "offset" on HCM(6,1) will not be removed.\n\n'</span>); |
---|
556 | 0498 <span class="keyword">end</span> |
---|
557 | 0499 <span class="keyword">end</span> |
---|
558 | 0500 |
---|
559 | 0501 <span class="comment">% Sector 11</span> |
---|
560 | 0502 <span class="comment">% Off 1.9 GeV 1.5 GeV</span> |
---|
561 | 0503 <span class="comment">% HCMCHICANEM(11,1) 80.0 40.5 52.0</span> |
---|
562 | 0504 <span class="comment">% HCMCHICANEM(11,1) 80.0 40.5 52.0</span> |
---|
563 | 0505 <span class="comment">% HCM(10,8) 0.0 -17.0 -14.0</span> |
---|
564 | 0506 <span class="comment">% HCM(11,1) 0.0 -17.0 -14.0</span> |
---|
565 | 0507 ihcm = findrowindex([10 8], OCS.CM{i}.DeviceList); |
---|
566 | 0508 <span class="keyword">if</span> length(ihcm) == 1 |
---|
567 | 0509 <span class="keyword">try</span> |
---|
568 | 0510 <span class="keyword">if</span> <a href="getsp.html" class="code" title="function [SP, tout, DataTime, ErrorFlag] = getsp(Family, varargin)">getsp</a>(<span class="string">'HCMCHICANEM'</span>,[11 1]) < 60 |
---|
569 | 0511 <span class="comment">% Assume sector 11 chicane is on</span> |
---|
570 | 0512 <span class="keyword">if</span> Energy == 1.9 |
---|
571 | 0513 HCM.Data(ihcm) = HCM.Data(ihcm) + 17; |
---|
572 | 0514 <span class="keyword">else</span> |
---|
573 | 0515 HCM.Data(ihcm) = HCM.Data(ihcm) + 14; |
---|
574 | 0516 <span class="keyword">end</span> |
---|
575 | 0517 <span class="keyword">end</span> |
---|
576 | 0518 <span class="keyword">catch</span> |
---|
577 | 0519 fprintf(<span class="string">'%s\n'</span>, lasterr); |
---|
578 | 0520 fprintf(<span class="string">'Problem reading HCMCHICANEM(11,1). The chicane "offset" on HCM(10,8) will not be removed.n\n'</span>); |
---|
579 | 0521 <span class="keyword">end</span> |
---|
580 | 0522 <span class="keyword">end</span> |
---|
581 | 0523 ihcm = findrowindex([11 1], OCS.CM{i}.DeviceList); |
---|
582 | 0524 <span class="keyword">if</span> length(ihcm) == 1 |
---|
583 | 0525 <span class="keyword">try</span> |
---|
584 | 0526 <span class="keyword">if</span> <a href="getsp.html" class="code" title="function [SP, tout, DataTime, ErrorFlag] = getsp(Family, varargin)">getsp</a>(<span class="string">'HCMCHICANEM'</span>,[11 1]) < 60 |
---|
585 | 0527 <span class="comment">% Assume sector 11 chicane is on</span> |
---|
586 | 0528 <span class="keyword">if</span> Energy == 1.9 |
---|
587 | 0529 HCM.Data(ihcm) = HCM.Data(ihcm) + 17; |
---|
588 | 0530 <span class="keyword">else</span> |
---|
589 | 0531 HCM.Data(ihcm) = HCM.Data(ihcm) + 14; |
---|
590 | 0532 <span class="keyword">end</span> |
---|
591 | 0533 <span class="keyword">end</span> |
---|
592 | 0534 <span class="keyword">catch</span> |
---|
593 | 0535 fprintf(<span class="string">'%s\n'</span>, lasterr); |
---|
594 | 0536 fprintf(<span class="string">'Due to an error, the chicane "offset" on HCM(11,1) will not be removed.n\n'</span>); |
---|
595 | 0537 <span class="keyword">end</span> |
---|
596 | 0538 <span class="keyword">end</span> |
---|
597 | 0539 <span class="keyword">end</span> |
---|
598 | 0540 <span class="comment">% END ALS only</span> |
---|
599 | 0541 |
---|
600 | 0542 |
---|
601 | 0543 <span class="comment">% Assumes 1 HCM family and it's first???</span> |
---|
602 | 0544 DispDotSmatHCM = DispDotSmatHCM + OCS.BPM{i}.Dispersion'*Smat(1:size(OCS.BPM{i}.DeviceList,1),1:1:size(OCS.CM{i}.DeviceList,1))*HCM.Data; |
---|
603 | 0545 <span class="keyword">else</span> |
---|
604 | 0546 R = [R zeros(1,size(OCS.BPM{i}.DeviceList,1))]; |
---|
605 | 0547 <span class="keyword">end</span> |
---|
606 | 0548 <span class="keyword">end</span> |
---|
607 | 0549 <span class="comment">% [OCS.BPM{1}.Dispersion, Dy] = getdisp(OCS.BPM{1}.FamilyName, OCS.BPM{1}.DeviceList, 'Numeric', OCS.BPM{1}.Units);</span> |
---|
608 | 0550 <span class="comment">% RR = [OCS.BPM{1}.Dispersion(:); zeros(size(OCS.BPM{2}.DeviceList,1),1)]';</span> |
---|
609 | 0551 |
---|
610 | 0552 <span class="comment">%r = 0;</span> |
---|
611 | 0553 r = -1*DispDotSmatHCM; <span class="comment">% Not sure if this should be divided by 2</span> |
---|
612 | 0554 R = [R*Smat(:,1:end-1) 0]; |
---|
613 | 0555 |
---|
614 | 0556 |
---|
615 | 0557 <span class="comment">% Projection onto the columns of Smat*V (or U)</span> |
---|
616 | 0558 <span class="comment">% Since the correctors are V*b, the restriction in corrector strength is R*V</span> |
---|
617 | 0559 X = Smat * V(:,OCS.SVDIndex); |
---|
618 | 0560 R = R * V(:,OCS.SVDIndex); |
---|
619 | 0561 b = inv(X'*X)*X' * (BPMWeight .* (GoalOrbitVec - StartOrbitVec)); |
---|
620 | 0562 br = b - inv(X'*X)*R'*inv(R*inv(X'*X)*R')*(R*b-r); |
---|
621 | 0563 Delcm = V(:,OCS.SVDIndex) * br; |
---|
622 | 0564 |
---|
623 | 0565 <span class="comment">%DelcmNoR = V(:,OCS.SVDIndex) * b;</span> |
---|
624 | 0566 <span class="comment">%fprintf(' dRF=%g dRF(NoR)=%g\n', RFWeight * Delcm(end), RFWeight * DelcmNoR(end));</span> |
---|
625 | 0567 |
---|
626 | 0568 <span class="keyword">elseif</span> OCS.FitRF == 5 |
---|
627 | 0569 <span class="comment">% Constrained L.S. with SVD</span> |
---|
628 | 0570 <span class="comment">% Constraint: dot(HCM(Dispersion), dHCM) = 0</span> |
---|
629 | 0571 R = []; |
---|
630 | 0572 <span class="keyword">for</span> i = 1:length(OCS.CM) |
---|
631 | 0573 <span class="keyword">if</span> <a href="ismemberof.html" class="code" title="function [IsTest, Index] = ismemberof(FamilyName, Field, MemberString)">ismemberof</a>(OCS.CM{i}.FamilyName, <span class="string">'HCM'</span>) |
---|
632 | 0574 <span class="comment">% Assume CM{i} is the same plane as BPM{i}</span> |
---|
633 | 0575 <span class="keyword">if</span> ~isfield(OCS.BPM{i}, <span class="string">'Dispersion'</span>) |
---|
634 | 0576 <span class="comment">% Dispersion at the horizontal BPMs</span> |
---|
635 | 0577 [OCS.BPM{i}.Dispersion, Dy] = <a href="getdisp.html" class="code" title="function [Data, FileName] = getdisp(varargin)">getdisp</a>(OCS.BPM{i}.FamilyName, OCS.BPM{i}.DeviceList, <span class="string">'Numeric'</span>, OCS.BPM{i}.Units); |
---|
636 | 0578 |
---|
637 | 0579 <span class="comment">% Find the corrector pattern of the dispersion orbit</span> |
---|
638 | 0580 <span class="comment">%</span> |
---|
639 | 0581 <span class="comment">% OCS.SVDIndex ????</span> |
---|
640 | 0582 <span class="comment">% Smat??? make an array</span> |
---|
641 | 0583 <span class="comment">% Why is Rhcm so different from Rbpmx?</span> |
---|
642 | 0584 |
---|
643 | 0585 <span class="comment">% Assumes 1 HCM family and it's first???</span> |
---|
644 | 0586 SmatNoEta = Smat(1:size(OCS.BPM{i}.DeviceList,1),1:size(OCS.CM{i}.DeviceList,1)); |
---|
645 | 0587 [UU, SS, VV] = svd(SmatNoEta, <span class="string">'econ'</span>); |
---|
646 | 0588 SS = diag(SS); |
---|
647 | 0589 |
---|
648 | 0590 X = SmatNoEta * VV(:,OCS.SVDIndex); |
---|
649 | 0591 b = inv(X'*X)*X' * OCS.BPM{i}.Dispersion; |
---|
650 | 0592 OCS.BPM{i}.DispersionCorrectors = VV(:,OCS.SVDIndex) * b; |
---|
651 | 0593 <span class="keyword">end</span> |
---|
652 | 0594 R = [R OCS.BPM{i}.DispersionCorrectors(:)']; |
---|
653 | 0595 <span class="keyword">else</span> |
---|
654 | 0596 R = [R zeros(1,size(OCS.CM{i}.DeviceList,1))]; |
---|
655 | 0597 <span class="keyword">end</span> |
---|
656 | 0598 <span class="keyword">end</span> |
---|
657 | 0599 |
---|
658 | 0600 r = 0; <span class="comment">% Should it be an absolute conditional ???</span> |
---|
659 | 0601 R = [R 0]; |
---|
660 | 0602 |
---|
661 | 0603 |
---|
662 | 0604 <span class="comment">% Projection onto the columns of Smat*V (or U)</span> |
---|
663 | 0605 <span class="comment">% Since the correctors are V*b, the restriction in corrector strength is R*V</span> |
---|
664 | 0606 X = Smat * V(:,OCS.SVDIndex); |
---|
665 | 0607 R = R * V(:,OCS.SVDIndex); |
---|
666 | 0608 b = inv(X'*X)*X' * (BPMWeight .* (GoalOrbitVec - StartOrbitVec)); |
---|
667 | 0609 br = b - inv(X'*X)*R'*inv(R*inv(X'*X)*R')*(R*b-r); |
---|
668 | 0610 Delcm = V(:,OCS.SVDIndex) * br; |
---|
669 | 0611 |
---|
670 | 0612 <span class="comment">%DelcmNoR = V(:,OCS.SVDIndex) * b;</span> |
---|
671 | 0613 <span class="comment">%fprintf(' dRF=%g dRF(NoR)=%g\n', RFWeight * Delcm(end), RFWeight * DelcmNoR(end));</span> |
---|
672 | 0614 |
---|
673 | 0615 <span class="keyword">else</span> |
---|
674 | 0616 |
---|
675 | 0617 <span class="comment">% L.S. with SVD algorithm</span> |
---|
676 | 0618 <span class="comment">% The V matrix columns are the singular vectors in the corrector magnet space</span> |
---|
677 | 0619 <span class="comment">% The U matrix columns are the singular vectors in the BPM space</span> |
---|
678 | 0620 <span class="comment">% Smat = U*S*V' where U'*U=I and V*V'=I</span> |
---|
679 | 0621 <span class="comment">%</span> |
---|
680 | 0622 <span class="comment">% b is the coef. of the projection onto the columns of Smat*V(:,Ivec) - the corrector space (same space as spanned by U)</span> |
---|
681 | 0623 <span class="comment">% Sometimes it's interesting to look at the size of these coefficients with singular value number.</span> |
---|
682 | 0624 b = U(:,OCS.SVDIndex)' * (BPMWeight .* (GoalOrbitVec - StartOrbitVec)); |
---|
683 | 0625 b = diag(S(OCS.SVDIndex).^(-1)) * b; |
---|
684 | 0626 |
---|
685 | 0627 <span class="comment">% Convert the b vector back to coefficents of response matrix</span> |
---|
686 | 0628 Delcm = V(:,OCS.SVDIndex) * b; |
---|
687 | 0629 <span class="keyword">end</span> |
---|
688 | 0630 |
---|
689 | 0631 |
---|
690 | 0632 <span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span> |
---|
691 | 0633 <span class="comment">% Remove Weights for the Correction %</span> |
---|
692 | 0634 <span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span> |
---|
693 | 0635 <span class="comment">% Separate the RF from the correctors</span> |
---|
694 | 0636 <span class="keyword">if</span> OCS.FitRF |
---|
695 | 0637 <span class="comment">% RF frequency is the last actuator</span> |
---|
696 | 0638 OCS.DeltaRF = RFWeight * Delcm(end); |
---|
697 | 0639 <span class="keyword">if</span> strcmpi(<a href="getfamilydata.html" class="code" title="function [Data, ErrorFlag] = getfamilydata(Family, Field1, Field2, DeviceList)">getfamilydata</a>(<span class="string">'SubMachine'</span>), <span class="string">'Soleil'</span>) |
---|
698 | 0640 <span class="comment">% Begin Add by Laurent % To be done with variables</span> |
---|
699 | 0641 <span class="comment">% test for avoiding to move too often RF frequency</span> |
---|
700 | 0642 <span class="comment">%fprintf('Prog %e \n',DelRF);</span> |
---|
701 | 0643 <span class="keyword">if</span> abs(OCS.DeltaRF) < DeltaRFmin <span class="comment">% minimum RF is .1 Hz</span> |
---|
702 | 0644 OCS.DeltaRF = 0; |
---|
703 | 0645 <span class="keyword">end</span> |
---|
704 | 0646 <span class="keyword">if</span> abs(OCS.DeltaRF) > DeltaRFmax <span class="comment">% maximum is 100 Hz</span> |
---|
705 | 0647 error(<span class="string">'RF change too large DeltaRF = %f Hz (> %f Hz) \nRF frequency shift has to be fixed first by hand.\nSee expert steprf command'</span>, <span class="keyword">...</span> |
---|
706 | 0648 <a href="hw2physics.html" class="code" title="function S = hw2physics(Family, Field, value, DeviceList, Energy)">hw2physics</a>(<span class="string">'RF'</span>,<span class="string">'Setpoint'</span>,OCS.DeltaRF), <a href="hw2physics.html" class="code" title="function S = hw2physics(Family, Field, value, DeviceList, Energy)">hw2physics</a>(<span class="string">'RF'</span>,<span class="string">'Setpoint'</span>,DeltaRFmax)); |
---|
707 | 0649 <span class="keyword">end</span> |
---|
708 | 0650 <span class="keyword">end</span> |
---|
709 | 0651 Delcm = Delcm(1:end-1); |
---|
710 | 0652 <span class="keyword">end</span> |
---|
711 | 0653 |
---|
712 | 0654 <span class="comment">% Remove column weights from Delcm</span> |
---|
713 | 0655 Delcm = Delcm .* CMWeight; |
---|
714 | 0656 |
---|
715 | 0657 |
---|
716 | 0658 <span class="comment">% Predict Orbit</span> |
---|
717 | 0659 <span class="keyword">if</span> OCS.FitRF |
---|
718 | 0660 OrbitDeltaTotal = [SmatNoWeights OCS.Eta] * [Delcm; OCS.DeltaRF]; |
---|
719 | 0661 <span class="keyword">else</span> |
---|
720 | 0662 OrbitDeltaTotal = SmatNoWeights * Delcm; |
---|
721 | 0663 <span class="keyword">end</span> |
---|
722 | 0664 <span class="keyword">for</span> j = 1:length(OCS.BPM) |
---|
723 | 0665 N = length(OCS.BPM{j}.Data); |
---|
724 | 0666 OCS.BPM{j}.PredictedOrbitDelta = OrbitDeltaTotal(1:N); |
---|
725 | 0667 OrbitDeltaTotal(1:N) = []; |
---|
726 | 0668 <span class="keyword">end</span> |
---|
727 | 0669 |
---|
728 | 0670 |
---|
729 | 0671 <span class="comment">% Don't put the Delcm into OCS.CM so this function can get</span> |
---|
730 | 0672 <span class="comment">% called multiple times before actually correcting the orbit</span> |
---|
731 | 0673 <span class="keyword">for</span> i = 1:length(OCS.CM) |
---|
732 | 0674 N = length(OCS.CM{i}.Data); |
---|
733 | 0675 <span class="comment">%OCS.CM{i}.Data = OCS.CM{i}.Data + Delcm(1:N);</span> |
---|
734 | 0676 OCS.CM{i}.Delta = Delcm(1:N); |
---|
735 | 0677 Delcm(1:N) = []; |
---|
736 | 0678 |
---|
737 | 0679 OCS.CMWeight{i} = CMWeight(1:N); |
---|
738 | 0680 CMWeight(1:N) = []; |
---|
739 | 0681 <span class="keyword">end</span> |
---|
740 | 0682 |
---|
741 | 0683 |
---|
742 | 0684 <span class="comment">% Remove the cell array if the length is one</span> |
---|
743 | 0685 <span class="keyword">if</span> NoCellBPMFlag |
---|
744 | 0686 OCS.BPM = OCS.BPM{1}; |
---|
745 | 0687 OCS.GoalOrbit = OCS.GoalOrbit{1}; |
---|
746 | 0688 OCS.BPMWeight = OCS.BPMWeight{1}; |
---|
747 | 0689 <span class="keyword">end</span> |
---|
748 | 0690 <span class="keyword">if</span> NoCellCMFlag |
---|
749 | 0691 OCS.CM = OCS.CM{1}; |
---|
750 | 0692 OCS.CMWeight = OCS.CMWeight{1}; |
---|
751 | 0693 <span class="keyword">end</span> |
---|
752 | 0694 |
---|
753 | 0695 |
---|
754 | 0696 <span class="comment">% Output statistics???</span> |
---|
755 | 0697 <span class="comment">% 1. Correctable orbit</span> |
---|
756 | 0698 <span class="comment">% 2. Not correctable orbit</span> |
---|
757 | 0699 <span class="comment">% 3. Error propagation</span> |
---|
758 | 0700 |
---|
759 | 0701</pre></div> |
---|
760 | <hr><address>Generated on Mon 21-May-2007 15:29:18 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> © 2003</address> |
---|
761 | </body> |
---|
762 | </html> |
---|