qempanelset.m 6.49 KB
Newer Older
Laurent Farvacque's avatar
Laurent Farvacque committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function [qemres,semres,qemb]=qempanelset(dirname)
%QEMPANELSET
%
%[QEMB,QEMRES,SEMRES]=QEMPANELSET((DIRNAME)
%
%QEMB:     Table of structures with fields q,cor,bh,bv,eta
%   QEMB(1): with no errors
%   QEMB(2): with errors
%QEMRES:   Empty structure to save results
%

global MACHFS

initname=fullfile(MACHFS,'MDT','respmat','qeminit.mat');
try
    qemres=load(initname);
catch %#ok<*CTCH>
    qemres=struct();
end
qemres.bhscale=1;
qemres.hlist=1:6:96;
qemres.vlist=1:6:96;
23
24
%qemres.qpcode=6;        % 32 quad correctors
qemres.qpcode=106;      % 31 quad correctors (n4/c4 removed)
Laurent Farvacque's avatar
Laurent Farvacque committed
25
%qemres.skcode=7;		% 32 skew correctors
26
27
%qemres.skcode=10;		% 64 skew correctors
qemres.skcode=110;		% 65 skew correctors (s4/c4 added)
Laurent Farvacque's avatar
Laurent Farvacque committed
28
29
30
31
32
33
34
35
36
qemres.datadir=dirname;
qemres.opticsdir=fullfile(dirname,'optics','');

semres=struct();
semres.hlist=1:6:96;
semres.vlist=1:6:96;
semres.iaxemz=NaN(14,1);
semres.wdisp=0;
semres.bvscale=1;
37
38
39
semres.skewkeep=true(1,65);
semres.skewkeep(1)=false;       % Do not touch kicker bump compensation
semres.skewkeep(end)=false;     % Do not touch kicker bump compensation
Laurent Farvacque's avatar
Laurent Farvacque committed
40

41
42
43
44
mach=load_at(qemres.opticsdir);
% mach=atclean(atreadbeta(fullfile(qemres.opticsdir,'betamodel.str'),...
%     'IdentityPass','BndMPoleSymplectic4Pass','QuadMPoleFringePass'));
% mach=sr.checklattice(atreadbeta(fullfile(qemres.opticsdir,'betamodel.str')),...
Laurent Farvacque's avatar
Laurent Farvacque committed
45
%     'reduce',true,'keep','BPM.*|ID.*|CA.*');
46
47
48
49
50
51
52
53
try
    scorhl=load_correction(fullfile(qemres.datadir,'sextcor.dat'),...
        3,qemres.opticsdir);
    mach=setsrmagsextus(mach,fullfile(qemres.datadir,'srmag.dat'),scorhl);
catch
    warning('qem:noinit','No sextupole corrector values in the data directory');
    mach=setsrmagsextus(mach,fullfile(qemres.datadir,'srmag.dat'));
end
Laurent Farvacque's avatar
Laurent Farvacque committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
try
    stgs=load(fullfile(qemres.datadir,'settings.mat'));
    qemres.tunes=stgs.tunes-fix(stgs.tunes);
catch
    qemres.tunes=[0.44 0.39];
end
qemres.at=reshape(atfittune(mach(:),qemres.tunes,'QD6','QF7'),size(mach));	% retune the model
%  qemres.at=mach;															% no retuning
try
    df=stgs.deltafrf;
catch
    df=0;
end
circ=findspos(qemres.at,length(qemres.at)+1);
68
[~,periods,~,harmnumber]=atenergy(qemres.at);
Laurent Farvacque's avatar
Laurent Farvacque committed
69
70
71
72
frf=harmnumber*2.997924e8/circ;
qemres.ct=-circ*df/(frf+df);
syncorb=findsyncorbit(qemres.at,qemres.ct);
qemres.dpp=syncorb(5,1);
73
[~,tunes]=atlinopt(qemres.at,qemres.dpp);
Laurent Farvacque's avatar
Laurent Farvacque committed
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
qemres.fractunes=periods*tunes-fix(periods*tunes);

dipfold=sr.fold(findcells(qemres.at(:),'Class','Bend'));
qemres.dipidx=reshape(dipfold([1 4],:),1,64);

qemres.qpidx=findcells(qemres.at(:),'Class','Quadrupole');
if numel(qemres.qpidx) == 258
    qemres.qdlowidx=qemres.qpidx([153 154]);
    qemres.qpidx([153 154])=[];
else
    qemres.qdlowidx=[];
end
qemres.qpfit=true(size(qemres.qpidx));  % keep all quads
qemres.ql=atgetfieldvalues(qemres.at(qemres.qpidx),'Length');

thinsext=atgetcells(qemres.at(:),'Class',@isthinsext,'ThinMultipole');
thicksext=atgetcells(qemres.at(:),'Class','Sextupole');
qemres.sextidx=reshape(find(thinsext|thicksext),1,[]);
sxl=atgetfieldvalues(qemres.at(qemres.sextidx),'Length');
sxl(~(sxl~=0))=1;   % Handles NaN correctly
qemres.sxl=sxl;

qemres.steerhidx=qemres.sextidx(selcor(8));
if ~isfield(qemres,'khgain'), qemres.khgain=ones(1,length(qemres.steerhidx)); end
if ~isfield(qemres,'khrot'), qemres.khrot=zeros(1,length(qemres.steerhidx)); end
qemres.hsteerl=qemres.sxl(selcor(8));

qemres.steervidx=qemres.sextidx(selcor(9));
if ~isfield(qemres,'kvgain'), qemres.kvgain=ones(1,length(qemres.steervidx)); end
if ~isfield(qemres,'kvrot'), qemres.kvrot=zeros(1,length(qemres.steervidx)); end
qemres.vsteerl=qemres.sxl(selcor(9));

106
elist=sort(selcor(qemres.qpcode));
Laurent Farvacque's avatar
Laurent Farvacque committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
qemres.qcoridx=qemres.sextidx(elist);
qemres.qcorl=qemres.sxl(elist);

qemres.bpmidx=findcells(qemres.at(:),'Class','Monitor');
nbpm=length(qemres.bpmidx);
if ~isfield(qemres,'bhgain'), qemres.bhgain=ones(nbpm,1); end
if ~isfield(qemres,'bvgain'), qemres.bvgain=ones(nbpm,1); end
if ~isfield(qemres,'bhrot'), qemres.bhrot=ones(nbpm,1); end
if ~isfield(qemres,'bvrot'), qemres.bvrot=ones(nbpm,1); end
qemres.brot=0.5*(qemres.bhrot+qemres.bvrot);

elist=sort(selcor(qemres.skcode));
qemres.skewidx=qemres.sextidx(elist);
qemres.skewl=qemres.sxl(elist);

b1idx=findcells(qemres.at(:),'FamName','B1H');
b2idx=findcells(qemres.at(:),'FamName','B2S');
qemres.id25idx=b1idx(22);
qemres.d9idx=b2idx(6);
qemres.d11lensidx=b2idx(8);
127
qemres.qmode=struct;
Laurent Farvacque's avatar
Laurent Farvacque committed
128
129

if nargout >= 3
130
131
132
    qemb1.kn=atgetfieldvalues(qemres.at(qemres.qpidx),'PolynomB',{2});
    qemb1.dipdelta=zeros(length(qemres.dipidx),1);
    qemb1.dxs=zeros(length(qemres.sextidx),1);
Laurent Farvacque's avatar
Laurent Farvacque committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
    qemb1.cor=zeros(length(qemres.qcoridx),1);
    qemb1.skewcor=zeros(length(qemres.skewidx),1);
    qemb1.dzs=zeros(length(qemres.sextidx),1);
    qemb1.diptilt=zeros(length(qemres.dipidx),1);
    qemb1.ks=zeros(length(qemres.qpidx),1);
    qemb1.at=qemat(qemres,qemb1,false);
    qemb(1)=qemoptics(qemres,qemb1);
    
    try
        [kn,qemb(2).dipdelta,qemb(2).dxs]=qemerrload(qemres,fullfile(qemres.opticsdir,'quadinierrors.mat'));
        qemb(2).kn=qemb(1).kn.*(1+srmodulation(kn));
    catch
        warning('qem:noinit','No initial errors in the optics directory');
        qemb(2).kn=qemb(1).kn;
        qemb(2).dipdelta=qemb(1).dipdelta;
        qemb(2).dxs=qemb(1).dxs;
    end
    qemb(2).cor=load_correction(fullfile(qemres.datadir,'magcor.dat'),...
151
        qemres.qpcode,qemres.opticsdir);
Laurent Farvacque's avatar
Laurent Farvacque committed
152
153
154
155
156
157
158
159
    qemb(2).skewcor=zeros(length(qemres.skewidx),1);
    qemb(2).dzs=zeros(length(qemres.sextidx),1);
    qemb(2).diptilt=zeros(length(qemres.dipidx),1);
    qemb(2).ks=zeros(length(qemres.qpidx),1);
    mach=atfittune(qemat(qemres,qemb(2),false),qemres.tunes,'QD6','QF7');      % retune the model
    qemb(2).kn=atgetfieldvalues(mach(qemres.qpidx),'PolynomB',{2});
end

160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
    function at=load_at(path)
        try
            matfile=fullfile(path,'betamodel.mat');
            a=load(matfile);
            keep=atgetcells(a.betamodel,'Class','Monitor|RingParam|RFCavity') |...
                atgetcells(a.betamodel,'FamName','IDMarker$|^ID\d+');
            at=atreduce(a.betamodel,keep);
            fprintf('initializing the optics using %s\n',matfile);
        catch err
            disp(err.message);
            betafile=fullfile(path,'betamodel.str');
            at=sr.checklattice(atreadbeta(betafile),'reduce',true);
            fprintf('initializing the optics using %s\n',betafile);
        end
    end

Laurent Farvacque's avatar
Laurent Farvacque committed
176
177
178
179
180
181
182
183
184
    function ok=isthinsext(elem,fieldname,fieldvalue)
        if isfield(elem,fieldname) && strcmp(elem.(fieldname),fieldvalue)
            pb=find(elem.PolynomB ~= 0,1);
            ok=~isempty(pb) && (pb == 3);
        else
            ok=false;
        end
    end
end