Home > lattice > atfittune.m

atfittune

PURPOSE ^

ATFITTUNE fits linear tunes by scaling 2 quadrupole families

SYNOPSIS ^

function newring=atfittune(ring,varargin)

DESCRIPTION ^

ATFITTUNE fits linear tunes by scaling 2 quadrupole families
 NEWRING = ATFITTUNE(RING,NEWTUNES,QUADFAMILY1,QUADFAMILY2)

 NEWRING = ATFITTUNE(RING,DPP,NEWTUNES,QUADFAMILY1,QUADFAMILY2)

RING:          Cell array
DPP:           Optional momentum deviation (default 0)
NEWTUNES:      Desired tune values (fractional part only)
QUADFAMILY1:   1st quadrupole family
QUADFAMILY2:   2nd quadrupole family

QUADFAMILY may be:
   string: Family name
   logical array: mask of selected elements in RING
   Numeric array: list of selected elements in RING
   Cell array: All elements selected by each cell

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function newring=atfittune(ring,varargin)
0002 %ATFITTUNE fits linear tunes by scaling 2 quadrupole families
0003 % NEWRING = ATFITTUNE(RING,NEWTUNES,QUADFAMILY1,QUADFAMILY2)
0004 %
0005 % NEWRING = ATFITTUNE(RING,DPP,NEWTUNES,QUADFAMILY1,QUADFAMILY2)
0006 %
0007 %RING:          Cell array
0008 %DPP:           Optional momentum deviation (default 0)
0009 %NEWTUNES:      Desired tune values (fractional part only)
0010 %QUADFAMILY1:   1st quadrupole family
0011 %QUADFAMILY2:   2nd quadrupole family
0012 %
0013 %QUADFAMILY may be:
0014 %   string: Family name
0015 %   logical array: mask of selected elements in RING
0016 %   Numeric array: list of selected elements in RING
0017 %   Cell array: All elements selected by each cell
0018 
0019 if isscalar(varargin{1}) && isnumeric(varargin{1})
0020     dpp=varargin{1};
0021     [newtunes,famname1,famname2]=deal(varargin{2:end});
0022 else
0023     dpp=0;
0024     [newtunes,famname1,famname2]=deal(varargin{:});
0025 end
0026  
0027 idx1=varelem(ring,famname1);
0028 idx2=varelem(ring,famname2);
0029 newtunes=newtunes-floor(newtunes);
0030 
0031 kl1=atgetfieldvalues(ring(idx1),'PolynomB',{2});
0032 kl2=atgetfieldvalues(ring(idx2),'PolynomB',{2});
0033 if true
0034     delta = 1e-6;
0035 
0036     % Compute initial tunes before fitting
0037     [lindata, tunes] = atlinopt(ring,dpp); %#ok<ASGLU>
0038 
0039     % Take Derivative
0040     [lindata, tunes1] = atlinopt(setqp(ring,idx1,kl1,delta),dpp); %#ok<ASGLU>
0041     [lindata, tunes2] = atlinopt(setqp(ring,idx2,kl2,delta),dpp); %#ok<ASGLU>
0042 
0043     %Construct the Jacobian
0044     J = ([tunes1(:) tunes2(:)] - [tunes(:) tunes(:)])/delta;
0045     dK = J\(newtunes(:)-tunes(:));
0046 else
0047     dK=0.01*fminsearch(@funtune,[0;0],...
0048         optimset(optimset('fminsearch'),'Display','iter','TolX',1.e-5));
0049 end
0050 newring = setqp(ring,idx1,kl1,dK(1));
0051 newring = setqp(newring,idx2,kl2,dK(2));
0052 
0053 %     function c=funtune(dK)
0054 %         ring2=ring;
0055 %         km1=kl1*(1+0.01*dK(1));
0056 %         ring2(idx1)=atsetfieldvalues(atsetfieldvalues(ring2(idx1),'K',km1),'PolynomB',{2},km1);
0057 %         km2=kl2*(1+0.01*dK(2));
0058 %         ring2(idx2)=atsetfieldvalues(atsetfieldvalues(ring2(idx2),'K',km2),'PolynomB',{2},km2);
0059 %         [lindata,tunes]=atlinopt(ring2,dpp); %#ok<SETNU>
0060 %         dt=abs(newtunes(:)-tunes(:));
0061 %         c=sum(dt.*dt);
0062 %     end
0063 
0064     function ring2=setqp(ring,idx,k0,delta)
0065         k=k0*(1+delta);
0066         ring2=ring;
0067         ring2(idx)=atsetfieldvalues(atsetfieldvalues(ring2(idx),'K',k),'PolynomB',{2},k);
0068     end
0069 
0070     function res=varelem(ring,arg)
0071         if islogical(arg)
0072             res=arg;
0073         elseif isnumeric(arg)
0074             res=false(size(ring));
0075             res(arg)=true;
0076         elseif ischar(arg)
0077             res=atgetcells(ring,'FamName',arg);
0078         elseif iscell(arg)
0079             res=false(size(ring));
0080             for i=1:length(arg)
0081                 res=res|varelem(ring,arg{i});
0082             end
0083         else
0084             error('AT:GetElemList:WrongArg','Cannot parse argument');
0085         end
0086     end
0087 end

Generated on Thu 24-Aug-2017 18:47:33 by m2html © 2005