Commit 0ef507fe authored by ncarmignani's avatar ncarmignani

function to optimize sextupoles or octupoles

parent ec5d30ce
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
%
%
[automatic,~]=getflag(varargin,'Automatic');
rips = tango.Device('sy/ps-rips/manager');
npointscan = 7;
nacq=2;
switch MagType
case 'OCT'
mags = tango.Attribute('srmag/m-o/all/Strengths');
all_mags = [1:64];
case 'SF'
mags = tango.Attribute('srmag/m-o/all/Strengths');
all_mags = sort([2:6:192 5:6:192]);
case 'SD'
mags = tango.Attribute('srmag/m-o/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];
end
names = mags.device.MagnetNames.read;
figure;
plot(0,0);
xlabel(MagType);
ylabel('total losses');
ax=gca;
ax.FontSize = 12;
grid on;
indmags = all_mags(randperm(length(all_mags))); % 2 = cell 5
dirname=['Optim_' MagType '_' datestr(now,'YYYYmmDD_hhMMss')];
mkdir(dirname);
for is = 1:length(indmags)
sel = indmags(is);
name = names(sel);
disp(['Magnet ' num2str(is) '/' num2str(length(indmags(is)))]);
pause(1);
K0 = tango.Attribute([name '/Strength']).set;
if length(Range)==2
range=Range;
else
range=[K0-abs(Range) K0+abs(Range)];
end
K_mid = mean(range);%-175;
K_delta = abs(diff(range));
% define variation steps
mag_values=[linspace(K_mid,range(1),ceil(npointscan/2)),...
linspace(range(1),range(2),npointscan),...
linspace(range(2),0,ceil(npointscan/2))];
% % sort to start at closest to present setpoint
[~,imin]=min((abs(mag_values-K0)));
mag_values=circshift(mag_values,-(imin-1));
mag_values = [mag_values,mag_values(1)];
TL = NaN(1,length(mag_values) * nacq);
DS = nan(1,length(mag_values) * nacq);
% loop points for a given magnet
ii=0;
jj=0;
while ii < length(mag_values)+1
% check rips running. if running. reset scan to start from zero
% again
if rips.State==10
disp('RIPS running set to initial values');
tango.Attribute([name '/Strength']).set=K0;
pause(10);
ii=0;
jj=0;
else
ii=ii+1;
tango.Attribute([name '/Strength']).set=mag_values(ii);
pause(1);
for nn=1:nacq
jj=jj+1;
TL(jj)=getTotalLosses(1);
DS(jj)=mag_values(ii);
end
plot(ax,DS,TL,'*-','MarkerSize',12,'LineWidth',2);
xlim([range(1)-0.2*K_delta + range(2)+0.2*K_delta]);
xlabel([name]);
ylabel('total losses');
ax.FontSize = 12;
grid on;
end
end
% find minimum and set it.
DS_=DS(:);
TL_=TL(:);
[fitres,gof] = fit(DS_(~isnan(TL_)),TL_(~isnan(TL_)),'poly2');
rmse=gof.rmse;
x = linspace(range(1),range(2),50);
f = polyval([fitres.p1,fitres.p2,fitres.p3],x);
[mf,ind]=min(f);
Mf=max(f);
RelVariation=(Mf-mf)/((Mf+mf)/2);
if RelVariation>0.005 && RelVariation<0.1 && fitres.p1>0
optimal = x(ind);
else
optimal=0;
disp('change too small or too large, I keep the starting value');
end
plot(ax,DS,TL,'o',...
x,f,'-',...
optimal,f(ind),'x',...
DS(1),TL(1),'s',...
'LineWidth',3,'MarkerSize',10);
xlabel([names{1}]);
ylabel('total losses');
ax.FontSize = 12;
grid on;
legend('measured',['fit - rmse = ' num2str(rmse)],'optimum','initial');
filename=strrep(['Scan_' names{1} '.fig'],'/','_');
filename=strrep(filename,'-','_');
saveas(gcf,fullfile(dirname,filename));
if optimal~=0
if ~automatic
prompt = ['Input factor to optimal correction K*' num2str(optimal) '? [0=no correction] K= '];
scale_factor=-1e-4;
while scale_factor>1 | scale_factor<0
scale_factor = input(prompt);
end
else
scale_factor=1;
end
% check rips runnning. if running wait.
if scale_factor~=0
% set skews
tango.Attribute([name '/Strength']).set=K0+(optimal-K0)*scale_factor;
disp('optimum has been set');
pause(2);
else
% set skews
tango.Attribute([name '/Strength']).set=K0;
disp('initial has been set');
pause(2);
end
else
tango.Attribute([name '/Strength']).set=K0;
disp('initial values has been set back');
end
end
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