source: MML/trunk/machine/THOMX/common/amp2k.m @ 5

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

ThomX MML version on the LAL server @ 17/12/2013

  • Property svn:executable set to *
File size: 4.6 KB
Line 
1function k = amp2k(Family, Field, Amps, DeviceList, Energy, C, K2AmpScaleFactor)
2%AMP2K - Converts amperes to simulator values
3%
4% Customized for ThomX by Jianfeng Zhang @ LAL, 21/06/2013
5%
6%
7%  k = amp2k(Family, Field, Amps, DeviceList, Energy, Coefficients, K2AmpScaleFactor)
8%    or
9%  k = amp2k(Family, Field, Amps, DeviceList, Energy, MagnetCoreType, K2AmpScaleFactor)
10%
11%  Calculates the "K-value" from the coefficients (or MagnetCoreType), current [amps], energy, and linear scale factor
12%
13%  A Coefficients vector or a MagnetCoreType string (coefficient found from magnetcoefficents.m) can be used
14%  Amps and Coefficients must have equal number of rows or one must only have one row
15% INPUTS
16% 1. Family - Family Name
17% 2. Field  - Family Field (like 'Setpoint')
18% 3. Amps   - Ampere vector to convert
19% 4. DeviceList - Device list (Amps and DeviceList must have the same
20% number of rows)
21% 5. Energy - Energy in GeV {Default: getenergy}
22%              If Energy is a vector, each output column will correspond to that energy.
23%              Energy can be anything getenergy accepts, like 'Model' or 'Online'.
24%              (Note: If Energy is a vector, then Amps can only have 1
25%              column)Family, Field, value(:,i), DeviceList, Energy, ParamsList{:}
26% 6. C - %  A Coefficients vector or a MagnetCoreType string (coefficient found from magnetcoefficents.m) can be used
27%  Amps and Coefficients must have equal number of rows or one must only
28%  have one row
29% 7. K2AmpScaleFactor - linearly scales the input current:  Amps = Amps ./ K2AmpScaleFactor
30%    This can be used to account for linear calibration errors of the power supply and magnet
31%
32% OUTPUT
33% 1. K and B - K-value and field in AT convention
34%    For dipole:      k = B / Brho
35%    For quadrupole:  k = B'/ Brho
36%    For sextupole:   k = B"/ Brho / 2  (to be compatible with AT)
37%
38%  NOTES
39%  1. If energy is not an input or empty, then the energy is obtained from getenergy.
40%  2. Family and Field inputs are not used but there automatically part of the hw2physics call.
41%
42% See Also k2amp, magnetcoefficients
43
44% ALGORITHM
45%
46% k = (c0 + c1*I + ... + cn*I^n)/Brho
47
48%
49% Written by M. Yoon 4/8/03
50% Adapted by Laurent S. Nadolski
51
52if nargin < 4
53    error('At least 4 inputs required');
54end
55
56if nargin < 6
57    C = [];
58end
59if isempty(C)
60    %[C, Leff, MagnetName] = magnetcoefficients(Family);
61    C = getfamilydata(Family, Field, 'HW2PhysicsParams', DeviceList);
62    C = C{1};
63    %Leff = getleff(Family, DeviceList(ii,:));
64end
65
66if nargin < 5
67    Energy = [];
68end
69if isempty(Energy)
70    Energy = getenergy; % like getenergy('Production')
71elseif ischar(Energy)
72    Energy = getenergy(Energy);
73end
74
75
76% If Amps is a row vector make it a column vector
77Amps = Amps(:);
78
79% Scale solution if required
80if nargin >= 7
81    Amps = Amps ./ K2AmpScaleFactor;
82end
83
84brho = getbrho(Energy);
85
86% Compute polynomial expansion:  polynom = c0 + c1*I ...
87% For dipole:      polynom = B  * Leff / I
88% For quadrupole:  polynom = B' * Leff / I
89% For sextupole:   polynom = B" * Leff / I  (use AT unit /2 from MAD units)
90
91% polynom = (C(8)+C(7)*Amps+C(6)*Amps^2+C(5)*Amps^3+C(4)*Amps^4+C(3)*Amps^5+C(2)*Amps^6+C(1)*Amps^7)
92% polynom = (c0+c1*Amps+c2*Amps^2+c3*Amps^3+c4*Amps^4+c5*Amps^5+c6*Amps^6+c7*Amps^7)
93
94if isstr(C)
95    TableLookUpFlag = 1;
96    MagnetCoreType = C;
97    [C, Leff, MagnetName] = magnetcoefficients(MagnetCoreType, Amps(1));
98    Amps0 = Amps(1);
99else
100    TableLookUpFlag = 0;
101end
102
103if any(size(C,1) ~= length(Amps))
104    if length(Amps) == 1
105        Amps = ones(size(C,1),1) * Amps;
106    elseif size(C,1) == 1
107        %C = ones(size(Amps,1),1) * C;
108    else
109        error('Amps and Coefficients must have equal number of rows or one must only have one row');
110    end
111end
112
113% B, B', or B" scaled by energy
114for i = 1:length(Amps)
115    % For piecewise tables, get a new polynomial
116    if TableLookUpFlag
117        if Amps(i) ~= Amps0;
118            [C, Leff, MagnetName] = magnetcoefficients(MagnetCoreType, Amps(i));
119            Amps0 = Amps(i);
120        end
121    end
122   
123    if size(C,1) == 1
124        k(i,1) = polyval(C, Amps(i)) / brho;
125    else
126        k(i,1) = polyval(C(i,:), Amps(i)) / brho;
127    end
128end
129
130
131% switch upper(deblank(MagnetName))
132%     case 'BEND'
133%         k = (Amps/brho)*polynom/Leff;   % B scaled by energy
134%         %BLeff = Amps*polynom;
135%         %B = BLeff/Leff;
136%         
137%     case 'QUAD'
138%         k = (Amps/brho)*polynom/Leff;   % B' scaled by energy
139%         %BPrimeLeff = Amps*polynom;
140%         %Bprime = BPrimeLeff/Leff;
141%         
142%     case 'SEXT'
143%         k = (Amps/brho)*polynom/Leff;   % B" scaled by energy
144%         %BPrimePrimeLeff = Amps*polynom;
145%         %BPrimePrime = BPrimePrimeLeff/Leff;       
146% end
Note: See TracBrowser for help on using the repository browser.