source: MML/trunk/at/atphysics/fitchrom2.m @ 5

Last change on this file since 5 was 5, checked in by zhangj, 10 years ago

ThomX MML version on the LAL server @ 17/12/2013

File size: 3.7 KB
Line 
1function [FinalS1,FinalS2] =  fitchrom2(newchrom, sextfam1, sextfam2, varargin)
2%function varargout =  fitchrom2(newchrom, sextfam1, sextfam2, varargin)
3%FITCHROM2 fits chromaticity  of THERING using 2 sextupole families
4%  FITCHROM2(NEWCHROM,SEXTUPOLEFAMILY1,SEXTUPOLEFAMILY2)
5%
6%  INPUTS
7%  1. newchrom - 2D chromaticity vector to fit to
8%  2. sextfam1 - Family name for the first sextupole family
9%  3. sextfam1 - Family name for the second sextupole family
10%  4. Display  - Displays fitting results {default}
11%     NoDisplay- Do not displays fitting results
12%
13%  OUTPUTS
14%  1. changes in sextupoles strength
15%
16%  EXAMPLES
17%  1. fitchrom2([2 2],'S9','S10')
18%
19%  See Also fittune2
20
21%
22%  Written by Andrei Terebilo
23%  Modified by Laurent S. Nadolski
24%  MARCH 25, 2005 - Take into account thin sextupoles
25%                 - Display Flag
26%
27%
28%   Modified by Jianfeng Zhang 30/09/2013 @ LAL
29%   Display the sextupole strengths after correction.
30%
31
32
33DisplayFlag = 1;
34
35%% Optional Input data parser
36for i = length(varargin):-1:1
37    if strcmpi(varargin{i},'Display')
38        DisplayFlag = 1;
39        varargin(i) = [];
40    elseif strcmpi(varargin{i},'NoDisplay')
41        DisplayFlag = 0;
42        varargin(i) = [];
43    end
44end
45
46% Must declare THERING as global in order for the function to modify sextupole values
47if ~isempty(whos('global','THERING'))
48    global THERING
49end
50
51%make a column vector
52newchrom = newchrom(:);
53% Thick sextupole
54deltaS = 1e-5; % step size in Sextupole strength
55deltaP = 1e-8;
56
57% find indexes of the 2 sextupole families use for fitting
58S1I = findcells(THERING,'FamName',sextfam1);
59S2I = findcells(THERING,'FamName',sextfam2);
60InitialS1 = getcellstruct(THERING,'PolynomB',S1I,3);
61InitialS2 = getcellstruct(THERING,'PolynomB',S2I,3);
62
63% Thin sextupoles
64if THERING{S1I(1)}.Length < 1e-3
65  deltaS = 1e-2*1e8; % step size in Sextupole strength
66end
67
68% Compute initial tunes and chromaticities before fitting
69
70[ LD, InitialTunes] = linopt(THERING,0);
71[ LDdP, ITdP] =linopt(THERING,deltaP);
72
73InitialChrom = (ITdP-InitialTunes)/deltaP;
74
75TempTunes = InitialTunes;
76TempChrom = InitialChrom;
77TempS1 = InitialS1;
78TempS2 = InitialS2;
79
80%% loop over n times to assure convergence
81for i=1:3
82               
83        % Take Derivative
84        THERING = setcellstruct(THERING,'PolynomB',S1I,TempS1+deltaS,3);
85        [LD , Tunes_dS1 ] = linopt(THERING,0);
86        [LD , Tunes_dS1dP ] = linopt(THERING,deltaP);
87
88        THERING = setcellstruct(THERING,'PolynomB',S1I,TempS1,3);
89        THERING = setcellstruct(THERING,'PolynomB',S2I,TempS2+deltaS,3);
90        [LD , Tunes_dS2 ] = linopt(THERING,0);
91        [LD , Tunes_dS2dP ] = linopt(THERING,deltaP);
92        THERING = setcellstruct(THERING,'PolynomB',S2I,TempS2,3);
93
94        %Construct the Jacobian
95        Chrom_dS1 = (Tunes_dS1dP-Tunes_dS1)/deltaP;
96        Chrom_dS2 = (Tunes_dS2dP-Tunes_dS2)/deltaP;
97
98        J = ([Chrom_dS1(:) Chrom_dS2(:)] - [TempChrom(:) TempChrom(:)])/deltaS;
99        Jinv = inv(J);
100
101        dchrom = (newchrom(:) - TempChrom(:));
102        dS = Jinv*dchrom;
103
104        TempS1 = TempS1 + dS(1);
105        TempS2 = TempS2 + dS(2);
106
107        THERING = setcellstruct(THERING,'PolynomB',S1I,TempS1,3);
108        THERING = setcellstruct(THERING,'PolynomB',S2I,TempS2,3);
109
110        [ LD, TempTunes] = linopt(THERING,0);
111        [ LD, TempTunesdP] = linopt(THERING,deltaP);
112        TempChrom = (TempTunesdP-TempTunes)/deltaP;
113
114end
115
116%% Get the final value of the correction sextupole
117
118   FinalS1 = getcellstruct(THERING,'PolynomB',S1I,3);
119   FinalS2 = getcellstruct(THERING,'PolynomB',S2I,3);
120 
121%% Display how good is the fit
122if DisplayFlag
123    fprintf('Desired chromaticities xix=%f xiz=%f\n',newchrom);
124    [tune xi] = tunechrom(THERING,0,'chrom');
125    fprintf('Reached chromaticities xix=%f xiz=%f\n',xi);
126   
127    fprintf('before correction: %s = %f, %s = %f\n', sextfam1,InitialS1(1),sextfam2,InitialS2(1));
128    fprintf('after correction:  %s = %f, %s = %f\n', sextfam1,FinalS1(1),sextfam2,FinalS2(1));
129end
Note: See TracBrowser for help on using the repository browser.