Home > lattice > atfitchrom.m

atfitchrom

PURPOSE ^

ATFITTUNE fits chromaticites by scaling 2 sextupol families

SYNOPSIS ^

function newring=atfitchrom(ring,varargin)

DESCRIPTION ^

ATFITTUNE fits chromaticites by scaling 2 sextupol families
 NEWRING = ATFITCHROM(RING,NEWCHROM,SEXTFAMILY1,SEXTFAMILY2)

RING:          Cell array
NEWCHROM:      Desired non-normalized chromaticities
SEXTFAMILY1:   1st sextupole family
SEXTFAMILY2:   2nd sextupole family

SEXTFAMILY 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=atfitchrom(ring,varargin)
0002 %ATFITTUNE fits chromaticites by scaling 2 sextupol families
0003 % NEWRING = ATFITCHROM(RING,NEWCHROM,SEXTFAMILY1,SEXTFAMILY2)
0004 %
0005 %RING:          Cell array
0006 %NEWCHROM:      Desired non-normalized chromaticities
0007 %SEXTFAMILY1:   1st sextupole family
0008 %SEXTFAMILY2:   2nd sextupole family
0009 %
0010 %SEXTFAMILY may be:
0011 %   string: Family name
0012 %   logical array: mask of selected elements in RING
0013 %   Numeric array: list of selected elements in RING
0014 %   Cell array: All elements selected by each cell
0015 
0016 if isscalar(varargin{1}) && isnumeric(varargin{1})
0017     dpp=varargin{1};
0018     [newchrom,famname1,famname2]=deal(varargin{2:end});
0019 else
0020     dpp=0;
0021     [newchrom,famname1,famname2]=deal(varargin{:});
0022 end
0023 deltaP = 1e-8;
0024 idx1=varelem(ring,famname1);
0025 idx2=varelem(ring,famname2);
0026 kl1=atgetfieldvalues(ring(idx1),'PolynomB',{3});
0027 kl2=atgetfieldvalues(ring(idx2),'PolynomB',{3});
0028 if true
0029     deltaS = 1e-5; % step size in Sextupole strngth
0030     
0031     % Compute initial tunes before fitting
0032     chrom=getchrom(ring,dpp,deltaP);
0033     
0034     % Take Derivative
0035     chrom1 = getchrom(setsx(ring,idx1,kl1,deltaS),dpp,deltaP);
0036     chrom2 = getchrom(setsx(ring,idx2,kl2,deltaS),dpp,deltaP);
0037     
0038     %Construct the Jacobian
0039     J = ([chrom1(:) chrom2(:)] - [chrom(:) chrom(:)])/deltaS;
0040     dK = J\(newchrom(:)-chrom(:));
0041 else
0042     dK=fminsearch(@funchrom,[0;0],...
0043         optimset(optimset('fminsearch'),'Display','iter',...
0044         'TolX',1.e-5,'TolFun',1.e-8));
0045 end
0046 newring=setsx(ring,idx1,kl1,dK(1));
0047 newring=setsx(newring,idx2,kl2,dK(2));
0048 
0049 %     function c=funchrom(dK)
0050 %         ring2=ring;
0051 %         ring2(idx1)=atsetfieldvalues(ring2(idx1),'PolynomB',{3},kl1*(1+dK(1)));
0052 %         ring2(idx2)=atsetfieldvalues(ring2(idx2),'PolynomB',{3},kl2*(1+dK(2)));
0053 %         chrom = getchrom(ring2,dpp,deltaP);
0054 %         dt=abs(newchrom(:)-chrom(:));
0055 %         c=sum(dt.*dt);
0056 %     end
0057 
0058     function ring2=setsx(ring,idx,k0,delta)
0059         ring2=atsetfieldvalues(ring,idx,'PolynomB',{3},k0*(1+delta));
0060     end
0061 
0062     function res=varelem(ring,arg)
0063         if islogical(arg)
0064             res=arg;
0065         elseif isnumeric(arg)
0066             res=false(size(ring));
0067             res(arg)=true;
0068         elseif ischar(arg)
0069             res=atgetcells(ring,'FamName',arg);
0070         elseif iscell(arg)
0071             res=false(size(ring));
0072             for i=1:length(arg)
0073                 res=res|varelem(ring,arg{i});
0074             end
0075         else
0076             error('AT:GetElemList:WrongArg','Cannot parse argument');
0077         end
0078     end
0079 
0080     function chrom=getchrom(ring,dpp,deltaP)
0081         [lindata, tunesa] = atlinopt(ring,dpp); %#ok<ASGLU>
0082         [lindata, tunesb] = atlinopt(ring,dpp+deltaP); %#ok<ASGLU>
0083         chrom = (tunesb-tunesa)/deltaP;
0084     end
0085 end

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