1 | function Amps = gev2bend(varargin) |
---|
2 | %GEV2BEND - Converts beam energy TO BEND current |
---|
3 | % Bend = gev2bend(Family, Field, GeV, DeviceList, BranchFlag) |
---|
4 | % |
---|
5 | % INPUTS |
---|
6 | % 1. Bend - Bend magnet family {Optional} |
---|
7 | % 2. Field - Field {Optional} |
---|
8 | % 3. GeV - Electron beam energy [GeV] |
---|
9 | % 4. DeviceList - Bend magnet device list to reference energy to {Default: BEND(1,1)} |
---|
10 | % 5. BranchFlag - 1 -> Lower branch |
---|
11 | % 2 -> Upper branch {Default} |
---|
12 | % Not working at Spear yet |
---|
13 | % OUTPUTS |
---|
14 | % 1. Amps = Bend magnet current [Amps] |
---|
15 | % |
---|
16 | % ALGORITHM |
---|
17 | % I is the root of the polynom |
---|
18 | % BL = theta*BRHO |
---|
19 | % p = root(-theta*Brho + c0 + c1*I + ... + cn*I^n) |
---|
20 | % I = root(p) closest to linear solution rlin |
---|
21 | % Linear solution is |
---|
22 | % rlin = (THETA*Brho - c0)/c1 |
---|
23 | % |
---|
24 | % See Also bend2gev |
---|
25 | |
---|
26 | % |
---|
27 | % Written by Gregory J. Portmann |
---|
28 | % Modified by Laurent S. Nadolski |
---|
29 | |
---|
30 | % Default |
---|
31 | Family = ''; |
---|
32 | Field = ''; |
---|
33 | GeV = []; |
---|
34 | DeviceList = []; |
---|
35 | BranchFlag = []; |
---|
36 | ModeFlag = ''; % model, online, manual |
---|
37 | UnitsFlag = ''; % hardware, physics |
---|
38 | for i = length(varargin):-1:1 |
---|
39 | if isstruct(varargin{i}) |
---|
40 | % Ignore structures |
---|
41 | elseif iscell(varargin{i}) |
---|
42 | % Ignore cells |
---|
43 | elseif strcmpi(varargin{i},'Struct') |
---|
44 | varargin(i) = []; |
---|
45 | elseif strcmpi(varargin{i},'Numeric') |
---|
46 | varargin(i) = []; |
---|
47 | elseif strcmpi(varargin{i},'Physics') |
---|
48 | UnitsFlag = varargin{i}; |
---|
49 | varargin(i) = []; |
---|
50 | elseif strcmpi(varargin{i},'Hardware') |
---|
51 | UnitsFlag = varargin{i}; |
---|
52 | varargin(i) = []; |
---|
53 | elseif strcmpi(varargin{i},'Simulator') || strcmpi(varargin{i},'Model') |
---|
54 | ModeFlag = varargin{i}; |
---|
55 | varargin(i) = []; |
---|
56 | elseif strcmpi(varargin{i},'Online') |
---|
57 | ModeFlag = varargin{i}; |
---|
58 | varargin(i) = []; |
---|
59 | elseif strcmpi(varargin{i},'Manual') |
---|
60 | ModeFlag = varargin{i}; |
---|
61 | varargin(i) = []; |
---|
62 | end |
---|
63 | end |
---|
64 | |
---|
65 | if length(varargin) >= 1 |
---|
66 | if ischar(varargin{1}) |
---|
67 | Family = varargin{1}; |
---|
68 | varargin(1) = []; |
---|
69 | else |
---|
70 | GeV = varargin{1}; |
---|
71 | varargin(1) = []; |
---|
72 | if length(varargin) >= 1 |
---|
73 | DeviceList = varargin{1}; |
---|
74 | varargin(1) = []; |
---|
75 | end |
---|
76 | if length(varargin) >= 1 |
---|
77 | BranchFlag = varargin{1}; |
---|
78 | varargin(1:end) = []; |
---|
79 | end |
---|
80 | end |
---|
81 | end |
---|
82 | if length(varargin) >= 1 & ischar(varargin{1}) |
---|
83 | Field = varargin{1}; |
---|
84 | varargin(1) = []; |
---|
85 | end |
---|
86 | if length(varargin) >= 1 |
---|
87 | GeV = varargin{1}; |
---|
88 | varargin(1) = []; |
---|
89 | end |
---|
90 | if length(varargin) >= 1 |
---|
91 | DeviceList = varargin{1}; |
---|
92 | varargin(1) = []; |
---|
93 | end |
---|
94 | if length(varargin) >= 1 |
---|
95 | BranchFlag = varargin{1}; |
---|
96 | varargin(1) = []; |
---|
97 | end |
---|
98 | |
---|
99 | |
---|
100 | if isempty(Family) |
---|
101 | Family = 'BEND'; |
---|
102 | end |
---|
103 | if isempty(Field) |
---|
104 | Field = 'Setpoint'; |
---|
105 | end |
---|
106 | |
---|
107 | if isempty(UnitsFlag) |
---|
108 | UnitsFlag = getunits(Family); |
---|
109 | end |
---|
110 | |
---|
111 | if isempty(GeV) |
---|
112 | if isempty(ModeFlag) |
---|
113 | ModeFlag = getmode(Family); |
---|
114 | end |
---|
115 | if strcmpi(ModeFlag,'Simulator') || strcmpi(ModeFlag,'model') |
---|
116 | GeV = getenergymodel; |
---|
117 | else |
---|
118 | error('GeV input required'); |
---|
119 | end |
---|
120 | end |
---|
121 | |
---|
122 | if isempty(DeviceList) |
---|
123 | DeviceList = family2dev(Family); |
---|
124 | if all(size(GeV)==[1 1]) |
---|
125 | DeviceList = DeviceList(1,:); |
---|
126 | end |
---|
127 | end |
---|
128 | if isempty(BranchFlag) |
---|
129 | % Default is upper branch |
---|
130 | BranchFlag = 2; |
---|
131 | end |
---|
132 | |
---|
133 | % End of input checking |
---|
134 | % Machine dependant stuff below |
---|
135 | |
---|
136 | for ii = 1:size(DeviceList,1) |
---|
137 | if length(GeV) == 1 |
---|
138 | GeV1 = GeV; |
---|
139 | else |
---|
140 | GeV1 = GeV(ii); |
---|
141 | end |
---|
142 | |
---|
143 | [C, Leff, MagnetType, A] = magnetcoefficients('BEND'); |
---|
144 | |
---|
145 | % Convert to BEND current |
---|
146 | AcceleratorName = getfamilydata('SubMachine'); |
---|
147 | switch AcceleratorName |
---|
148 | case {'StorageRing'} |
---|
149 | |
---|
150 | % C = C * Leff; |
---|
151 | |
---|
152 | % k = 0.31537858; %% gradient of the dipole |
---|
153 | % bprime = k * brho; |
---|
154 | % b0 = bprime * 0.392348; |
---|
155 | % bl = b0 * Leff; |
---|
156 | % k = -k; |
---|
157 | % brho[Tm] = 3.33620907461447 * E[GeV] |
---|
158 | % brho = GeV1*3.33620907461447; |
---|
159 | % bl = -brho/5.3653*Leff; |
---|
160 | |
---|
161 | % Solve for roots based on polynomial coefficient (coefficients already divided by Leff) |
---|
162 | % p = [C(1) C(2) C(3) C(4) C(5) C(6) C(7) C(8) C(9)-bl]; |
---|
163 | % C(9) = 0 |
---|
164 | |
---|
165 | % p = C; |
---|
166 | % p(9) = bl; |
---|
167 | |
---|
168 | % Closest to the linear line approach |
---|
169 | |
---|
170 | brho = getbrho(GeV1); |
---|
171 | theta = 0.19634954084936; %rad <--> 11.25 degrees |
---|
172 | BL = brho*theta; |
---|
173 | p = A; |
---|
174 | p(end) = A(end) - BL; |
---|
175 | r1inear = (A(end)-BL) / A(end-1); |
---|
176 | |
---|
177 | case { 'LT2'} |
---|
178 | |
---|
179 | brho = getbrho(GeV1); |
---|
180 | theta = 0.193789; %rad <--> 11.25 degrees |
---|
181 | BL = brho*theta; |
---|
182 | p = A; |
---|
183 | p(end) = A(end) - BL; |
---|
184 | r1inear = (A(end)-BL) / A(end-1); |
---|
185 | |
---|
186 | |
---|
187 | case 'LT1' |
---|
188 | brho = getbrho(GeV1); |
---|
189 | theta = 0.26179938779915; %rad <--> 15 degrees |
---|
190 | BL = brho*theta; |
---|
191 | p = A; |
---|
192 | p(end) = A(end) - BL; |
---|
193 | r1inear = (A(end)-BL) / A(end-1); |
---|
194 | |
---|
195 | case 'Booster' |
---|
196 | brho = getbrho(GeV1); |
---|
197 | theta = 0.17453292519943; %rad <--> 10 degrees |
---|
198 | BL = brho*theta; |
---|
199 | p = A; |
---|
200 | p(end) = A(end) - BL; |
---|
201 | r1inear = (A(end)-BL) / A(end-1); |
---|
202 | |
---|
203 | otherwise |
---|
204 | error('Unknown AcceleratorName') |
---|
205 | end |
---|
206 | |
---|
207 | % magnet current is one of the roots |
---|
208 | r = roots(p); |
---|
209 | |
---|
210 | % Choose the closest solution to the linear one |
---|
211 | BEND = inf; |
---|
212 | for i = 1:length(r) |
---|
213 | if isreal(r(i)) |
---|
214 | %if r(i)>0 & abs(r(i)) < BEND(j,1) % Smallest, positive |
---|
215 | if abs(r(i) - r1inear) < abs(BEND - r1inear) % Closest to linear solution |
---|
216 | BEND = r(i); |
---|
217 | end |
---|
218 | end |
---|
219 | end |
---|
220 | |
---|
221 | if isinf(BEND) |
---|
222 | error(sprintf('Solution for GeV=%.3f not found (all roots are complex)', GeV1)); |
---|
223 | end |
---|
224 | |
---|
225 | if size(GeV,2) == 1 |
---|
226 | Amps(ii,1) = BEND; |
---|
227 | else |
---|
228 | Amps(1,ii) = BEND; |
---|
229 | end |
---|
230 | end |
---|
231 | |
---|
232 | if strcmpi(UnitsFlag,'Physics') |
---|
233 | Amps = hw2physics(Family, 'Setpoint', Amps, DeviceList, GeV1); |
---|
234 | end |
---|