source: MML/trunk/machine/SOLEIL/common/amp2k.m

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

To have a stable version on the server.

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