forked from ThorstenHellert/SC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSCsetMultipoles.m
More file actions
174 lines (150 loc) · 5.26 KB
/
SCsetMultipoles.m
File metadata and controls
174 lines (150 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
function RING = SCsetMultipoles(RING,ords,AB,varargin)
% SCsetMultipoles
% ===============
%
% NAME
% ----
% SCsetMultipoles - sets multipole errors in lattice elements
%
% SYNOPSIS
% --------
% `RING = SCsetMultipoles(RING, ords, AB [, options])`
%
%
% DESCRIPTION
% -----------
% Applies multipole errors specified in `AB` in the lattice elements `ords` of `RING` depending on
% the specified options.
%
%
% INPUTS
% ------
% `RING`:: Lattice cell structure.
% `ords`:: Ordinates of the considered magnets.
% `AB`:: [N x 2] array of PolynomA/B multipole errors.
%
%
% OPTIONS
% -------
% The following options can be given as name/value-pairs:
%
% `'method'` (`'rnd'`)::
% Specifies which multipole error method should be applied. Possible are
% * `'sys'`: This option is intended for normalized systematic multipole error tables. It sets
% the systematic multipoles of the field component defined by option `'order'`
% and `'type'`. It is required that the `AB` entries are normalized by that component,
% e.g. `AB(2,1)=1` for skew-quadrupole systematic multipoles. The systematic
% multipoles are from now on scaled with the current magnet excitation and added to the
% PolynomA/B fields.
% * `'rnd'`: This option is intended for random multipole error tables. It randomly generates
% multipole components with a 2-sigma truncated Gaussian distribution from each of
% the `AB` entries. The final multipole errors are stored in the PolynomA/BOffset of
% the lattice elements.
% `'order'` ([])::
% Numeric value defining the order of the considered magnet: [1,2,3,...] => [dip,quad,sext,...]
% `'type'` ([])::
% Numeric value defining the type of the considered magnet: [1,2] => [skew/normal]
%
%
% EXAMPLES
% --------
% Defines random multipole components for the 'QF' magnet and adds it to the field offsets of all
% magnets named 'QF'.
% ------------------------------------------------------------------
% ords = SCgetOrds(SC.RING,'QF');
% AB = [0 1E-5;...
% 0 1E-4;...
% 0 0;...
% 0 1E-2];
% RING = SCsetMultipoles(RING,ords,AB,'method','rnd');
% ------------------------------------------------------------------
%
% Reads the normalized systematic multipole components for the skew quadrupole excitation of the 'SF'
% magnet from table 'SF_skew_quad_AT_norm.tsv' and assigns it to all magnets named 'SF'. Note that
% the data table in 'SF_skew_quad_AT_norm.tsv' must be 1.0 for the skew quadrupole component.
% ------------------------------------------------------------------
% ords = SCgetOrds(SC.RING,'SF');
% [AB,order,type] = SCmultipolesRead('SF_skew_quad_AT_norm.tsv');
% RING = SCsetMultipoles(RING,ords,AB,'method','sys','order',order,'type',type);
% ------------------------------------------------------------------
%
%
% SEE ALSO
% --------
% *SCmultipolesRead*, *SCupdateMagnets*
p = inputParser;
addParameter(p,'method','rnd');
addOptional(p,'order',[]);
addOptional(p,'type',[]);
parse(p,varargin{:});
par = p.Results;
inputCheck(par);
% Loop over magnets
for ord=ords
% Check which multipole error method should be applied
switch par.method
case 'sys'
% Apply multipole errros to lattice
RING = applySysMultipoles(RING,ord,AB,par.order,par.type);
case 'rnd'
% Generate random multipoles
addAB = SCrandnc(2,size(AB)) .* AB;
% Apply multipole errros to lattice
RING = applyRndMultipoles(RING,ord,addAB);
end
end
end
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Auxiliary functions
% Applies the systematic multipole to the lattice element
function RING = applySysMultipoles(RING,ord,AB,order,type)
% Set nominal coefficent to zero (we dont want to add to it)
AB(order,type) = 0;
if type==1
% Add systmatic PolynomA entries for coil A('order')
RING{ord}.SysPolAFromA{order} = AB(:,1)';
% Add systmatic PolynomB entries for coil A('order')
RING{ord}.SysPolBFromA{order} = AB(:,2)';
else
% Add systmatic PolynomA entries for coil B('order')
RING{ord}.SysPolAFromB{order} = AB(:,1)';
% Add systmatic PolynomB entries for coil B('order')
RING{ord}.SysPolBFromB{order} = AB(:,2)';
end
end
% Applies the random multipole to the lattice element
function RING = applyRndMultipoles(RING,ord,AB)
if isfield(RING{ord},'PolynomAOffset')
RING{ord}.PolynomAOffset = addPadded(RING{ord}.PolynomAOffset, AB(:,1)');
else
RING{ord}.PolynomAOffset = AB(:,1)';
end
if isfield(RING{ord},'PolynomBOffset')
RING{ord}.PolynomBOffset = addPadded(RING{ord}.PolynomBOffset, AB(:,2)');
else
RING{ord}.PolynomBOffset = AB(:,2)';
end
end
% Auxiliary function to add unequally long arrays (zero padding)
function v = addPadded(v1,v2)
if ~((iscolumn(v1)&&iscolumn(v2))||(isrow(v1)&&isrow(v2)))
error('Wrong dimensions.');
end
l1=length(v1);
l2=length(v2);
if l2>l1; v1(l2)=0; end
if l1>l2; v2(l1)=0; end
v=v1+v2;
end
% Check if input looks reasonable
function inputCheck(par)
switch par.method
case 'sys'
if isempty(par.order) || isempty(par.type)
error('Options ''order'' and ''type'' must be specified for method %s.',par.method)
end
case 'rnd'
otherwise
error('Unsupported multipole method. Allowed are ''sys'' or ''rnd''.')
end
end