CreateOpticsFiles.m 3.55 KB
Newer Older
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
function CreateOpticsFiles(r,opticsname,varargin)
% CREATEOPTICSFUILES(r,opticsname,...), 
% given an AT lattice R creates the relevant files for
% operation in APPHOME/ebs/optics/settings/(OPTICSNAME)
% 
% optional input ('NAME',val,'Name2',val2,...)
% 'MeasuredResponse' : folder containing ouput of full RM measurement
% 'errtab' : table of errors
% 'cortab' : table of corrections
%
% WARNING lattice contains QF8D modeled as dipoles. 
% Those are merged to a single quadrupole without bending angle for all 
% computations. This makes any function using dispersion (matching for ex.) 
% incorrect. 
% 
% example:
% load /machfs/liuzzo/esrfupgradelatticefiles-code/S28D/MagneticLengths/AT/errors/WP_021_034__S28D_Z0p67_newErrFunct_1_seed1_.mat
% CreateOpticsFiles(ring_err,'Seed1MeasResp','MeasuredResponse','/machfs/liuzzo/EBS/Simulator/responsematrix/resp2')
% 
%see also: LoadOpticsConfiguration

p=inputParser;
addRequired(p,'r',@iscell);
addRequired(p,'opticsname',@ischar);
addOptional(p,'MeasuredResponse',[]);
addOptional(p,'ErrTab',[],@istable);
addOptional(p,'CorTab',[],@istable);

parse(p,r,opticsname,varargin{:});
r           = p.Results.r;
opticsname  = p.Results.opticsname;
rm          = p.Results.MeasuredResponse;
errtab      = p.Results.ErrTab;
cortab      = p.Results.CorTab;

if isempty(errtab)
    errtab = atcreateerrortable(r);
end

if isempty(cortab)
    cortab = atgetcorrectiontable(r,r); % zeros
end

global APPHOME
ah=getenv('APPHOME');
%if ah~=APPHOME
warning(['Setting APPHOME=' ah]);
APPHOME=ah;
%end

fulfold=fullfile(pwd,'ebs','optics','settings',opticsname);

curdir=pwd;
mkdir(fulfold);
cd(fulfold);

% AT lattice
betamodel=r;
save('betamodel.mat','betamodel');

% AT lattice for simulator with errors and corrections
disp('errors and correction lattice for simulator (provide errtab and cortab or zeros will be set)')
errmodel=atseterrortable(r,errtab);
errmodel=atsetcorrectiontable(errmodel,cortab);
save('errmodel.mat','errmodel','cortab','errtab');


% save image of lattice optics
f=figure;
atplot(r);
saveas(gca,[opticsname '.jpg']);
close(f);

rfull=ebs.model(r);

% compute fast model once for all
% merge DQ
indDQmark=atgetcells(r,'FamName','PINHOLE_D\w*','CellCenter');
r(indDQmark)=[]; % checklattice should merge all splitted elements.

% merge 2PW and transform back to quadrupole
indQF8dip = find(atgetcells(r,'FamName','QF8\w*') & atgetcells(r,'Class','Bend'))';
r=atsetfieldvalues(r,indQF8dip,'BendingAngle',0);
r=atsetfieldvalues(r,indQF8dip,'Class','Quadrupole');
r=atsetfieldvalues(r,indQF8dip,'PassMethod','StrMPoleSymplectic4Pass');

% get fast model (QF8D merged, not ok for optics computations)
rmod=ebs.model(r,'reduce',true,'keep','BPM.*|ID.*');

% % store family magnet strengths to file (or in the format for magnet application, to be loaded)
disp('family magnets')
ebs.magnetsstrengths_model(fullfile(pwd,'DesignStrengths.csv'),rmod);
  
% resonance response
disp('resonances')
ebs.reson_model(rfull);

% % tune response
disp('tune')
ebs.tune_model(rfull); % splitted quadrupoles, lattice is wrong!

% orbit response, theory always,

ebs.autocor_model(rfull);

% measured if required
if ~isempty(rm)
ebs.autocor_model(rfull,'exp',rm);
end

% chromaticity response
disp('chromaticity')
ebs.chrom_model(rfull);

%orbit bumps (takes long time, commented)
disp('orbit bumps')
ebs.orbitbumps_model(rfull);

% injection bump 
disp('injection bump')
ebs.injbump_model(rfull);
mkdir('kicker_bumps');
bumpfile = fullfile(pwd,'kicker_bumps','injbumpmat.cvs');
movefile(fullfile(pwd,'injbumpmat.cvs'),bumpfile);
system(['ln -s ' bumpfile ' kicker_bump'])
  
cd(curdir);

end