1 | function [GeV B] = bend2gev(varargin) |
---|
2 | %BEND2GEV - Compute the energy based on the ramp tables |
---|
3 | % GeV = bend2gev(Family, Field, Amps, DeviceList, BranchFlag) |
---|
4 | % |
---|
5 | % INPUTS |
---|
6 | % 1. Bend - Bend magnet family {Optional} |
---|
7 | % 2. Field - Field {Optional} |
---|
8 | % 3. Amps - Bend magnet current |
---|
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, since there isn't any magnet measurements on hysteresis |
---|
13 | % |
---|
14 | % OUTPUTS |
---|
15 | % 1. GeV - Electron beam energy [GeV] |
---|
16 | % 2. B - B-field [T] |
---|
17 | % |
---|
18 | % ALGORYTHM |
---|
19 | % theta = BL/Brho |
---|
20 | % |
---|
21 | % See Also bend2gev |
---|
22 | |
---|
23 | |
---|
24 | % |
---|
25 | % Written by Gregory J. Portmann |
---|
26 | % Modify by Laurent S. Nadolski |
---|
27 | |
---|
28 | % TODO Need BLeff = function of the dipole current |
---|
29 | |
---|
30 | % Default |
---|
31 | Family = ''; |
---|
32 | Field = ''; |
---|
33 | Amps = []; |
---|
34 | DeviceList = []; |
---|
35 | BranchFlag = []; |
---|
36 | |
---|
37 | ModeFlag = ''; % model, online, manual |
---|
38 | UnitsFlag = ''; % hardware, physics |
---|
39 | for i = length(varargin):-1:1 |
---|
40 | if isstruct(varargin{i}) |
---|
41 | % Ignor structures |
---|
42 | elseif iscell(varargin{i}) |
---|
43 | % Ignor cells |
---|
44 | elseif strcmpi(varargin{i},'Struct') |
---|
45 | varargin(i) = []; |
---|
46 | elseif strcmpi(varargin{i},'Numeric') |
---|
47 | varargin(i) = []; |
---|
48 | elseif strcmpi(varargin{i},'Physics') |
---|
49 | UnitsFlag = 'Physics'; |
---|
50 | varargin(i) = []; |
---|
51 | elseif strcmpi(varargin{i},'Hardware') |
---|
52 | UnitsFlag = 'Hardware'; |
---|
53 | varargin(i) = []; |
---|
54 | elseif strcmpi(varargin{i},'Simulator') || strcmpi(varargin{i},'Model') |
---|
55 | ModeFlag = varargin{i}; |
---|
56 | varargin(i) = []; |
---|
57 | elseif strcmpi(varargin{i},'Online') |
---|
58 | ModeFlag = varargin{i}; |
---|
59 | varargin(i) = []; |
---|
60 | elseif strcmpi(varargin{i},'Manual') |
---|
61 | ModeFlag = varargin{i}; |
---|
62 | varargin(i) = []; |
---|
63 | end |
---|
64 | end |
---|
65 | |
---|
66 | |
---|
67 | if length(varargin) >= 1 |
---|
68 | if ischar(varargin{1}) |
---|
69 | Family = varargin{1}; |
---|
70 | varargin(1) = []; |
---|
71 | else |
---|
72 | Amps = varargin{1}; |
---|
73 | varargin(1) = []; |
---|
74 | if length(varargin) >= 1 |
---|
75 | DeviceList = varargin{1}; |
---|
76 | varargin(1) = []; |
---|
77 | end |
---|
78 | if length(varargin) >= 1 |
---|
79 | BranchFlag = varargin{1}; |
---|
80 | varargin(1:end) = []; |
---|
81 | end |
---|
82 | end |
---|
83 | end |
---|
84 | if length(varargin) >= 1 & ischar(varargin{1}) |
---|
85 | Field = varargin{1}; |
---|
86 | varargin(1) = []; |
---|
87 | end |
---|
88 | if length(varargin) >= 1 |
---|
89 | Amps = varargin{1}; |
---|
90 | varargin(1) = []; |
---|
91 | end |
---|
92 | if length(varargin) >= 1 |
---|
93 | DeviceList = varargin{1}; |
---|
94 | varargin(1) = []; |
---|
95 | end |
---|
96 | if length(varargin) >= 1 |
---|
97 | BranchFlag = varargin{1}; |
---|
98 | varargin(1) = []; |
---|
99 | end |
---|
100 | |
---|
101 | |
---|
102 | if isempty(Family) |
---|
103 | Family = 'BEND'; |
---|
104 | end |
---|
105 | if isempty(ModeFlag) |
---|
106 | ModeFlag = getmode(Family); |
---|
107 | end |
---|
108 | |
---|
109 | if isempty(Field) |
---|
110 | Field = 'Setpoint'; |
---|
111 | end |
---|
112 | if isempty(DeviceList) |
---|
113 | DeviceList = family2dev(Family); |
---|
114 | if all(size(Amps)==[1 1]) | isempty(Amps) |
---|
115 | DeviceList = DeviceList(1,:); |
---|
116 | end |
---|
117 | end |
---|
118 | if isempty(BranchFlag) |
---|
119 | % Default is upper branch |
---|
120 | BranchFlag = 2; |
---|
121 | end |
---|
122 | if isempty(Amps) |
---|
123 | if strcmpi(ModeFlag,'simulator') || strcmpi(ModeFlag,'model') |
---|
124 | % The model energy is used only if Amps is empty |
---|
125 | % Otherwise "Maximum recursion limit" |
---|
126 | GeV = getenergymodel; |
---|
127 | return; |
---|
128 | |
---|
129 | %GeVmodel = getenergymodel; |
---|
130 | %kmodel = getpvmodel(Family, Field, DeviceList, 'Physics'); |
---|
131 | %Amps = k2amp(Family, Field, kmodel, DeviceList, [], 1, GeVmodel); |
---|
132 | else |
---|
133 | Amps = getpv(Family, Field, [1 1], 'Hardware', ModeFlag); |
---|
134 | UnitsFlag = 'UnitsFlag'; |
---|
135 | end |
---|
136 | end |
---|
137 | |
---|
138 | % End of input checking |
---|
139 | % Machine dependent stuff below |
---|
140 | |
---|
141 | |
---|
142 | % Amps should be in hardware units |
---|
143 | if strcmpi(UnitsFlag,'Physics') |
---|
144 | Amps = physics2hw(Family, 'Setpoint', Amps, DeviceList); |
---|
145 | end |
---|
146 | |
---|
147 | |
---|
148 | DeviceListTotal = family2dev(Family, 0); |
---|
149 | |
---|
150 | for ii = 1:size(DeviceList,1) |
---|
151 | if length(Amps) == 1 |
---|
152 | BEND = Amps; |
---|
153 | else |
---|
154 | BEND = Amps(ii); |
---|
155 | end |
---|
156 | |
---|
157 | %% Approximation too wrong for low energy like in transfert lines |
---|
158 | % brho[Tm] = 3.33620907461447 * E[GeV] |
---|
159 | % brho = BLeff/theta = 1E9/c*sqrt(E*E-E0*E0) |
---|
160 | % |
---|
161 | |
---|
162 | E0 = .51099906e-3; %electron rest mass in GeV |
---|
163 | e = 1.60217733e-19 ; |
---|
164 | %% celerity times 1 billion |
---|
165 | cb = 2.99792458e-1; |
---|
166 | |
---|
167 | AcceleratorName = getfamilydata('SubMachine'); |
---|
168 | switch AcceleratorName |
---|
169 | case 'StorageRing' |
---|
170 | % Convert to energy |
---|
171 | % |
---|
172 | % |
---|
173 | % |
---|
174 | % Need BLeff = function of the dipole current |
---|
175 | |
---|
176 | % C coefficients have been scaled to convert between AT units and hardware units and also includes a DC term: |
---|
177 | % c8 * I^8+ c7 * I^7+ c6 * I^6 + c5 * I^5 + c4 * I^4 + c3 * I^3 + c2 * I^2+c1*I + c0 = B or B' or B" |
---|
178 | % C = [c8 c7 c6 c5 c4 c3 c2 c1 c0] |
---|
179 | %[C1, Leff, MagnetType, A] = magnetcoefficients('BEND'); |
---|
180 | |
---|
181 | [C Leff Type A] = magnetcoefficients(Family); |
---|
182 | |
---|
183 | % Magnetfield in T |
---|
184 | BL = polyval(A,BEND); |
---|
185 | % k(i,1) = polyval(C, Amps(i)) / brho; |
---|
186 | |
---|
187 | theta = 0.19634954084936; % rad <--> 11.25 degrees |
---|
188 | % la question se pose sur l'utilite de BLeff |
---|
189 | %BLeff = 1.806; |
---|
190 | %BLeff = 1.8014; |
---|
191 | %brho = BLeff/theta; |
---|
192 | %% TODO get BLeff in function of the dipole current |
---|
193 | brho = BL/theta; |
---|
194 | |
---|
195 | case 'LT2' |
---|
196 | |
---|
197 | [C Leff Type A] = magnetcoefficients(Family); |
---|
198 | |
---|
199 | % Magnetfield in T |
---|
200 | BL = polyval(A,BEND); |
---|
201 | % k(i,1) = polyval(C, Amps(i)) / brho; |
---|
202 | |
---|
203 | theta = 0.193789; % rad <--> 11.25 degrees |
---|
204 | BLeff = 1.7780; |
---|
205 | %brho = BLeff/theta; |
---|
206 | %% TODO get BLeff in function of the dipole current |
---|
207 | brho = BL/theta; |
---|
208 | |
---|
209 | |
---|
210 | |
---|
211 | case 'LT1' |
---|
212 | [C Leff Type A] = magnetcoefficients(Family); |
---|
213 | |
---|
214 | % Magnetfield in T |
---|
215 | BL = polyval(A,BEND); |
---|
216 | theta = 0.26179938779915; %rad <--> 15 degrees |
---|
217 | brho = BL/theta; |
---|
218 | case 'Booster' |
---|
219 | [C Leff Type A] = magnetcoefficients(Family); |
---|
220 | |
---|
221 | % Magnetfield in T |
---|
222 | BL = polyval(A,BEND); |
---|
223 | theta = 0.17453292519943; %rad <--> 10 degrees |
---|
224 | brho = BL/theta; |
---|
225 | otherwise |
---|
226 | error('Wrong SubMachine %s', Machine); |
---|
227 | end |
---|
228 | |
---|
229 | % now return energy in GeV |
---|
230 | gev1 = sqrt(E0*E0+cb*cb*brho*brho) - E0; |
---|
231 | |
---|
232 | if size(Amps,2) == 1 |
---|
233 | GeV(ii,1) = gev1; |
---|
234 | else |
---|
235 | GeV(1,ii) = gev1; |
---|
236 | end |
---|
237 | |
---|
238 | end |
---|