forked from ThorstenHellert/SC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCsetCMs2SetPoints.m
127 lines (114 loc) · 3.58 KB
/
SCsetCMs2SetPoints.m
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
function [SC,setpoints] = SCsetCMs2SetPoints(SC,CMords,setpoints,nDim,varargin)
% SCsetCMs2SetPoints
% ==================
%
% NAME
% ----
% SCsetCMs2SetPoints - Sets dipole corrector magnets to different setpoints
%
% SYNOPSIS
% --------
% `SC = SCsetCMs2SetPoints(SC, CMords, setpoints, nDim [, mode])`
%
%
% DESCRIPTION
% -----------
% Sets horizontal or vertical CMs as specified in `CMords` and `nDim`, respectively, to `setpoints`
% [rad] and updates the magnetic fields. If the corresponding setpoint exceeds the CM limit
% specified in the corresponding lattice field `CMlimit`, the CM is clipped to that value
% and a warning is being printed (to switch off, use `warning('off','SC:CM1'))`. Positive setpoints
% will results in kicks in the positive horizontal or vertical direction.
%
%
% INPUTS
% ------
% `SC`:: SC base structure
% `CMords`:: Array of CM ordinates in the lattice structure
% `setpoints`:: CM setpoints (array or single value for all CMs) [rad]
% `nDim`:: Integer specifying CM dimension ([1|2] -> [hor|ver])
%
%
% RETURN VALUES
% -------------
% `SC`::
% The lattice structure with modified and applied setpoints
% `setpoints`::
% The list of acutal setpoints applied to the magnets after possible clipping [rad]
%
%
% MODE
% ----
% `'abs'` (default)::
% Use absolute setpoints
% `'rel'`::
% Use setpoints relative to current value
% `'add'`::
% Add setpoints to current value
%
%
% EXAMPLES
% --------
% Set all registered horizontal CMs to zero.
% ------------------------------------------------------------------
% SC = SCsetCMs2SetPoints(SC,SC.ORD.CM{1},0,1);
% ------------------------------------------------------------------
%
% Add 10urad to the fourth registered vertical CM.
% ------------------------------------------------------------------
% SC = SCsetCMs2SetPoints(SC,SC.ORD.CM{2}(4),1E-5, 2,'add');
% ------------------------------------------------------------------
%
%
% SEE ALSO
% --------
% *SCregisterMagnets*, *SCsetMags2SetPoints*, *SCupdateMagnets*, *SCgetCMSetPoints*
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Input check
% If only single setpoint is given, use setpoint for all CMs
if length(setpoints)==1
setpoints = repmat(setpoints,size(CMords));
end
% Set setpoint flag
if isempty(varargin)
method = 'abs';
else
method = varargin{1};
end
i=1;
for ord=CMords(:)'
% Get current fields
curAB = [SC.RING{ord}.SetPointB' ,SC.RING{ord}.SetPointA' ];
% Get setpoint normalization factor (field <-> kick)
if strcmp(SC.RING{ord}.PassMethod,'CorrectorPass')
normBy = [1 1];
else
normBy = [-1 1]*SC.RING{ord}.Length; % positive setpoint -> positive kick -> negative horizontal field
end
% Select actual setpoint based on setpoint flag
switch method
case 'abs'
setpoints(i) = setpoints(i);
case 'rel'
setpoints(i) = setpoints(i) * curAB(1,nDim) * normBy(nDim);
case 'add'
setpoints(i) = setpoints(i) + curAB(1,nDim) * normBy(nDim);
otherwise
error('Unsupported method: ''%s''. Allowed are ''abs'',''rel'' and ''add''.',method)
end
% Check clipping
if isfield(SC.RING{ord},'CMlimit') && abs(setpoints(i))>abs(SC.RING{ord}.CMlimit(nDim))
warning('SC:CM1','CM (ord: %d / dim: %d) is clipping',ord,nDim);
setpoints(i) = sign(setpoints(i)) * SC.RING{ord}.CMlimit(nDim);
end
% Apply setpoint.
if nDim==1
SC.RING{ord}.SetPointB(1) = setpoints(i) / normBy(nDim);
else
SC.RING{ord}.SetPointA(1) = setpoints(i) / normBy(nDim);
end
% Update loop index
i = i + 1;
end
% Update magnets
SC = SCupdateMagnets(SC,CMords);
end