source: MML/trunk/machine/THOMX/common/gev2bend.m

Last change on this file was 27, checked in by zhangj, 10 years ago

(1) Fix the bug to define all sextupole members in one family in AO. (2) Fix the bug to get and define the conversions between the dipole current and beam energy, for the two types dipoles in the transfer line.

  • Property svn:executable set to *
File size: 6.8 KB
Line 
1function Amps = gev2bend(varargin)
2%GEV2BEND - Converts beam energy TO BEND (ring) or BEND1 (TL), or BEND2 currents
3%
4% beam energy/dipole bending angle (BL/Brho) ---> dipole current
5%
6%  Jianfeng Zhang @ LAL, 09/10/2013
7%  Currently used the physics unit, that is 1 Ampere = 1 physics unit.
8%  Need to modify the setting with the measured magnet current.
9%
10%  Customized for ThomX machine by Jianfeng Zhang @ LAL, 21/06/2013
11%
12%
13%  Bend = gev2bend(Family, Field, GeV, DeviceList, BranchFlag)
14%
15%  INPUTS
16%  1. Bend - Bend magnet family {Optional}
17%  2. Field - Field {Optional}
18%  3. GeV - Electron beam energy [GeV]
19%  4. DeviceList - Bend magnet device list to reference energy to {Default: BEND(1,1)}
20%  5. BranchFlag - 1 -> Lower branch
21%                  2 -> Upper branch {Default}
22%                  Not working at Spear yet
23%  OUTPUTS
24%  1. Amps = Bend magnet current [Amps]
25%
26%  ALGORITHM
27%  I is the root of the polynom
28%  BL = theta*BRHO
29%  p = root(-theta*Brho  + c0 + c1*I + ... + cn*I^n)
30%  I = root(p) closest to linear solution rlin
31%  Linear solution is
32%  rlin = (THETA*Brho - c0)/c1
33%
34%  See Also bend2gev
35
36%
37%  Written by Gregory J. Portmann
38%  Modified by Laurent S. Nadolski
39
40% Modified by Jianfeng Zhang @ LAL, 18/09/2013
41% Need to be modified when the magnet is ready...
42%
43%
44% 24/02/2014 Jianfeng Zhang @ LAL
45%      Fix the bugs to turn on/off the dipole current in TL, since
46%      there are two types of dipoles in TL: BEND1, BEND2.
47%
48%
49%
50
51
52%  Default
53Family = '';
54Field = '';
55GeV = [];
56DeviceList = [];
57BranchFlag = [];
58ModeFlag = '';  % model, online, manual
59UnitsFlag = ''; % hardware, physics
60for i = length(varargin):-1:1
61    if isstruct(varargin{i})
62        % Ignore structures
63    elseif iscell(varargin{i})
64        % Ignore cells
65    elseif strcmpi(varargin{i},'Struct')
66        varargin(i) = [];
67    elseif strcmpi(varargin{i},'Numeric')
68        varargin(i) = [];
69    elseif strcmpi(varargin{i},'Physics')
70        UnitsFlag = varargin{i};
71        varargin(i) = [];
72    elseif strcmpi(varargin{i},'Hardware')
73        UnitsFlag = varargin{i};
74        varargin(i) = [];
75    elseif strcmpi(varargin{i},'Simulator') || strcmpi(varargin{i},'Model')
76        ModeFlag = varargin{i};
77        varargin(i) = [];
78    elseif strcmpi(varargin{i},'Online')
79        ModeFlag = varargin{i};
80        varargin(i) = [];
81    elseif strcmpi(varargin{i},'Manual')
82        ModeFlag = varargin{i};
83        varargin(i) = [];
84    end       
85end
86
87if length(varargin) >= 1
88    if ischar(varargin{1})
89        Family = varargin{1};
90        varargin(1) = [];
91    else
92        GeV = varargin{1};
93        varargin(1) = [];
94        if length(varargin) >= 1
95            DeviceList = varargin{1};
96            varargin(1) = [];
97        end
98        if length(varargin) >= 1
99            BranchFlag = varargin{1};
100            varargin(1:end) = [];
101        end
102    end
103end
104if length(varargin) >= 1 & ischar(varargin{1})
105    Field = varargin{1};
106    varargin(1) = [];
107end
108if length(varargin) >= 1
109    GeV = varargin{1};
110    varargin(1) = [];
111end
112if length(varargin) >= 1
113    DeviceList = varargin{1};
114    varargin(1) = [];
115end
116if length(varargin) >= 1
117    BranchFlag = varargin{1};
118    varargin(1) = [];
119end
120
121
122if isempty(Family)
123    Family = 'BEND';
124end
125if isempty(Field)
126    Field = 'Setpoint';
127end
128
129if isempty(UnitsFlag)
130    UnitsFlag = getunits(Family);
131end
132
133if isempty(GeV)
134    if isempty(ModeFlag)
135        ModeFlag = getmode(Family);
136    end
137    if strcmpi(ModeFlag,'Simulator') || strcmpi(ModeFlag,'model')
138        GeV = getenergymodel;
139    else
140        error('GeV input required');
141    end
142end
143
144if isempty(DeviceList)
145    DeviceList = family2dev(Family);
146    if all(size(GeV)==[1 1])
147        DeviceList = DeviceList(1,:);
148    end
149end
150if isempty(BranchFlag)
151    % Default is upper branch
152    BranchFlag = 2;
153end
154
155% End of input checking
156% Machine dependant stuff below
157
158for ii = 1:size(DeviceList,1)
159    if length(GeV) == 1
160        GeV1 = GeV;
161    else
162        GeV1 = GeV(ii);
163    end
164
165      % [C, Leff, MagnetType, A] = magnetcoefficients('BEND');
166    [C, Leff, MagnetType, A] = magnetcoefficients(Family);
167    % Convert to BEND current
168    AcceleratorName = getfamilydata('SubMachine');
169    switch AcceleratorName
170        case {'StorageRing'}
171
172            % C = C * Leff;
173
174            %     k = 0.31537858;  %% gradient of the dipole
175            %     bprime = k * brho;
176            %     b0 = bprime * 0.392348;
177            %     bl = b0 * Leff;
178            %     k = -k;
179            % brho[Tm] = 3.33620907461447 * E[GeV]
180            % brho = GeV1*3.33620907461447;
181            % bl = -brho/5.3653*Leff;
182
183            % Solve for roots based on polynomial coefficient (coefficients already divided by Leff)
184            % p = [C(1) C(2) C(3) C(4) C(5) C(6) C(7) C(8) C(9)-bl];
185            % C(9) = 0
186
187            % p = C;
188            % p(9) = bl;
189
190            % Closest to the linear line approach
191
192            brho = getbrho(GeV1);
193            theta = 0.785398163397448; %rad <--> 45 degrees
194            BL = brho*theta;
195            p  = A;
196            p(end)  = A(end) - BL;
197            r1inear = (A(end)-BL) / A(end-1);
198           
199     case { 'TL'}
200   
201      % BEND1 & BEND2 have different field strength
202      % main dipoles
203      if(strcmp(Family,'BEND1'))
204       
205        brho = getbrho(GeV1);
206        theta = 0.785398163397448; %rad <--> 45 degrees
207        BL = brho*theta;
208        p  = A;
209        p(end)  = A(end) - BL;
210        r1inear = (A(end)-BL) / A(end-1);
211           
212        % injection dipole
213      elseif(strcmp(Family,'BEND2'))
214        brho = getbrho(GeV1);
215        theta = 0.1690000; %rad <--> 9.68 degrees
216        BL = brho*theta;
217        p  = A;
218        p(end)  = A(end) - BL;
219        r1inear = (A(end)-BL) / A(end-1);
220      else
221        error('Error! The dipole in the transfer line is either "BEND1" or "BEND2"');
222            end
223           
224           
225        case 'EL'
226            brho = getbrho(GeV1);
227            theta = 0.785398163397448; %rad <--> 15 degrees
228            BL = brho*theta;
229            p  = A;
230            p(end)  = A(end) - BL;
231            r1inear = (A(end)-BL) / A(end-1);
232
233        otherwise
234            error('Unknown AcceleratorName')
235    end
236
237    % magnet current is one of the roots
238    r = roots(p);
239
240    % Choose the closest solution to the linear one
241    BEND = inf;
242    for i = 1:length(r)
243        if isreal(r(i))
244            %if r(i)>0 & abs(r(i)) < BEND(j,1)  % Smallest, positive
245            if abs(r(i) - r1inear) < abs(BEND - r1inear)  % Closest to linear solution
246                BEND = r(i);
247            end
248        end
249    end
250
251    if isinf(BEND)
252        error(sprintf('Solution for GeV=%.3f not found (all roots are complex)', GeV1));
253    end
254
255    if size(GeV,2) == 1
256        Amps(ii,1) = BEND;
257    else
258        Amps(1,ii) = BEND;
259    end
260end
261   
262if strcmpi(UnitsFlag,'Physics')
263    Amps = hw2physics(Family, 'Setpoint', Amps, DeviceList, GeV1);
264end
Note: See TracBrowser for help on using the repository browser.