Commit 4ad0261e authored by beamdyn's avatar beamdyn

some modifications from MDTs

parent 5d2ee6f1
function run_by_hand_singlemagnets(MagType,Range,varargin)
%RUN_BY_HAND_SINGLEMAGNET Summary of this function goes here
%
% MagType can be OCT, SEXT, SF, SD
% run_by_hand_singlemagnets(MagType,Range,varargin)
%
% MagType can be OCT, SEXT, SF, SD, SKEW
%
% if Range is a single value then the range is from K0-range to K0+range
% if Range is an arrray of 2 elements:
% if MagType is SEXT then Range(1) is for SF, Range(2) is for SD
% if MagType is OCT then the range is from Range(1) to Range(2)
%
% run_by_hand_singlemagnets(MagType,Range,'Automatic')
% will stop when SI3 is on and will save a magnet file
%
% run_by_hand_singlemagnets(MagType,Range,'IDLosses')
% will use only losses from the first BLM after SS except inj and RF
%
%
[automatic,~]=getflag(varargin,'Automatic');
[IDLosses,~]=getflag(varargin,'IDLosses');
rips=tango.Device('sy/ps-rips/manager');
% rips=tango.Device('sy/ps-rips/manager');
SI3=tango.Device('sr/ps-si/3');
npointscan = 7;
......@@ -22,20 +31,58 @@ switch MagType
case 'OCT'
mags = tango.Attribute('srmag/m-o/all/Strengths');
all_mags = [1:64];
% all_mags = [1];
% all_mags([1 2 35 18 56])=[];
% all_mags([1 2 35 37 47 18 56,... % OFF
% 12,... % already at max
% 13,...
% 15,...
% 16,...
% 19,...
% 21,...
% 26,...
% 29,...
% 32,...
% 36,...
% 39,...
% 40,...
% 44,...
% 48,...
% 51,...
% 55,...
% 60,...
% 61,...
% 62,...
% 64,...
% ])=[];
% all_mags = 2;
case 'SF'
mags = tango.Attribute('srmag/m-o/all/Strengths');
mags = tango.Attribute('srmag/m-s/all/Strengths');
all_mags = sort([2:6:192 5:6:192]);
case 'SD'
mags = tango.Attribute('srmag/m-o/all/Strengths');
mags = tango.Attribute('srmag/m-s/all/Strengths');
all_mags = sort([1:6:192 3:6:192 4:6:192 6:6:192]);
case 'SEXT'
mags = tango.Attribute('srmag/m-s/all/Strengths');
all_mags = [1:192];
case 'SKEW'
mags = tango.Attribute('srmag/sqp/all/CorrectionStrengths');
all_mags = [1:288];
all_mags(5:9:288)=[];
all_mags([1 end])=[];
% all_mags = [5:9:288];
DeltaMaxSkew=1.5e-3;
end
names = mags.device.MagnetNames.read;
f=figure;
f.Position=[93 937 560 420];
if strcmp(MagType,'SKEW')
names = mags.device.CorrectorNames.read;
else
names = mags.device.MagnetNames.read;
end
f1=figure(1);
f1.Position=[93 937 560 420];
plot(0,0);
xlabel(MagType);
ylabel('total losses');
......@@ -46,7 +93,8 @@ grid on;
indmags = all_mags(randperm(length(all_mags))); % 2 = cell 5
dirname=['Optim_' MagType '_' datestr(now,'YYYYmmDD_hhMMss')];
mkdir(dirname);
gains=zeros(1,length(indmags));
residualLosses=zeros(1,length(indmags));
for is = 1:length(indmags)
sel = indmags(is);
name = names{sel};
......@@ -94,12 +142,17 @@ for is = 1:length(indmags)
% check rips running. if running. reset scan to start from zero
% again
magStrAttr=tango.Attribute([name '/Strength']);
if strcmp(MagType,'SKEW')
magStrAttr=tango.Attribute([name '/CorrectionStrength']);
else
magStrAttr=tango.Attribute([name '/Strength']);
end
if strcmp(SI3.State,'On')
disp('Injection ON, set to initial values');
magStrAttr.set=K0;
pause(10);
pause(28);
if ~magsaved
globalfilename=['Settings' datestr(now,'yyyymmmdd_HHMMSS') ];
famsave=tango.Device('sys/settings/m-strengths');famsave.Timeout=10000;
......@@ -109,6 +162,8 @@ for is = 1:length(indmags)
magsaved=true;
ii=0;
jj=0;
figure(2);
saveas(gcf,fullfile(dirname,'ResidualLossesEvolution.fig'));
else
ii=0;
jj=0;
......@@ -122,12 +177,19 @@ for is = 1:length(indmags)
pause(1);
for nn=1:nacq
if nn==1
pause(1);
end
jj=jj+1;
I=BeamCurrent.Current.read;
TL(jj)=getTotalLosses(1)*(I0/I)^2;
if IDLosses
TL(jj)=getIDLosses(1)*(I0/I)^2;
else
TL(jj)=getTotalLosses(1)*(I0/I)^2;
end
DS(jj)=mag_values(ii);
end
figure(1);
plot(ax,DS,TL,'*-','MarkerSize',12,'LineWidth',2);
xlim([range(1)-0.2*K_delta range(2)+0.2*K_delta]);
xlabel([name]);
......@@ -143,11 +205,15 @@ for is = 1:length(indmags)
[fitres,gof] = fit(DS_(~isnan(TL_)),TL_(~isnan(TL_)),'poly2');
rmse=gof.rmse;
x = linspace(range(1),range(2),50);
x = linspace(range(1),range(2),201);
f = polyval([fitres.p1,fitres.p2,fitres.p3],x);
[mf,ind]=min(f);
Mf=max(f);
xin=floor(interp1(x,1:201,DS(1)));
InitialX=x(xin);
InitialY=f(xin);
RelVariation=(Mf-mf)/((Mf+mf)/2);
if RelVariation>0.005 && RelVariation<0.2 && fitres.p1>0
optimal = x(ind);
......@@ -156,21 +222,39 @@ for is = 1:length(indmags)
disp('change too small or too large, I keep the starting value');
end
gains(is)=(InitialY/f(ind)-1);
if is==1
residualLosses(is)=1-gains(is);
else
residualLosses(is)=residualLosses(is-1)*(1-gains(is));
end
figure(1);
plot(ax,DS,TL,'o',...
x,f,'-',...
optimal,f(ind),'x',...
DS(1),TL(1),'s',...
InitialX,InitialY,'s',...
'LineWidth',3,'MarkerSize',10);
xlabel([name]);
ylabel('total losses');
ax.FontSize = 12;
grid on;
legend('measured',['fit - rmse = ' num2str(rmse)],'optimum','initial');
legend('measured',['fit - rmse = ' num2str(rmse)],...
['optimum: - gain = ' num2str(100*gains(is),'%1.2f') ' %' ],...
'initial');
filename=strrep(['Scan_' name '.fig'],'/','_');
filenamemat=strrep(['Scan_' name '.mat'],'/','_');
filename=strrep(filename,'-','_');
saveas(gcf,fullfile(dirname,filename));
save(fullfile(dirname,filenamemat),'DS','TL','fitres','gof','name');
figure(2);
hold off;
bar(0,1)
hold on; grid on;
bar(1:is,residualLosses(1:is))
xlabel('Magnet number')
ylabel('Residual losses')
ylim([(residualLosses(is)-0.1), 1.05]);
if optimal~=0
if ~automatic
prompt = ['Input factor to optimal correction K*' num2str(optimal) '? [0=no correction] K= '];
......@@ -180,6 +264,11 @@ for is = 1:length(indmags)
end
else
scale_factor=1;
if strcmp(MagType,'SKEW')
if abs(optimal-DS(1))>DeltaMaxSkew
optimal=DS(1)+DeltaMaxSkew*sign(optimal-DS(1));
end
end
end
% check rips runnning. if running wait.
if scale_factor~=0
......@@ -205,6 +294,7 @@ famsave=tango.Device('sys/settings/m-strengths');famsave.Timeout=10000;
famsave.GenerateSettingsFile({['FILE:' globalfilename ''],'AUTHOR: sext optimization','COMMENTS: '});
famsave=tango.Device('sys/settings/m-currents');famsave.Timeout=10000;
famsave.GenerateSettingsFile({['FILE:' globalfilename ''],'AUTHOR: sext optimization','COMMENTS: '});
figure(2);
saveas(gcf,fullfile(dirname,'ResidualLossesEvolution.fig'));
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment