source: MML/trunk/machine/THOMX/common/bend2gev.m @ 25

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

(1) Add the beta function measurement of the ThomX ring. (2) Define a new Thomx ring lattice with septum and inj./extr. kickers.

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