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

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

Initial import--MML version from SOLEIL@2013

File size: 4.3 KB
Line 
1function R = tunespaceplot(XTUNE,YTUNE,RESORDER,varargin);
2%TUNESPACEPLOT draws a tune diagram
3%  resonance lines: m*nu_x + n*nu_y = p
4%
5%  TUNESPACEPLOT(XTUNE, YTUNE, ORDER)
6%
7%  XTUNE = [XTUNEMIN,XTUNEMAX]
8%  YTUNE = [YTUNEMIN,YTUNEMAX] - plotting range in tune space
9%  RESORDER - resonance order: |m| + |n|
10%
11%  EXAMPLES
12%  1. tunespaceplot([18 19], [10 11] ,4)
13
14% TUNESPACEPLOT(XTUNE, YTUNE, ORDER, FIGHANDLE)
15% TUNESPACEPLOT(XTUNE, YTUNE, ORDER, FIGHANDLE)
16
17%
18% Modified by Laurent S. Nadolski
19
20if nargin>3
21    if ishandle(varargin{1}) & strcmp(get(varargin{1},'Type'),'figure')
22        % Plot tune space plot
23        figure(varargin{1});
24    else % create new figure
25        figure
26        axes;   
27    end
28end
29if nargin>4
30    LINEARGS = varargin(2:end);
31else
32    LINEARGS = {};
33end
34
35
36axis([XTUNE,YTUNE]);
37axis square
38       
39
40R = zeros(8*length(RESORDER),6);
41NLMAX = 0;
42for r = RESORDER
43    for m = 0:r
44        n = r-m;
45       
46        % Lower
47        p1 = ceil(m*XTUNE(1)+n*YTUNE(1));
48        p2 = floor(m*XTUNE(2)+n*YTUNE(1));
49       
50           
51        for p =p1:p2
52            if m % lines with m=0 do not cross upper and lower sides
53                NLMAX = NLMAX+1;
54                R(NLMAX,:) = [abs(m)+abs(n),m,n,p,(p-n*YTUNE(1))/m,YTUNE(1)];
55            end
56        end
57       
58        % Left
59        p1 = ceil(m*XTUNE(1)+n*YTUNE(1));
60        p2 = floor(m*XTUNE(1)+n*YTUNE(2));
61       
62       
63        for p =p1:p2
64            if n % lines with n=0 do not cross left and right sides
65                NLMAX = NLMAX+1;
66                R(NLMAX,:) = [abs(m)+abs(n),m,n,p,XTUNE(1),(p-m*XTUNE(1))/n];
67            end
68        end
69       
70        % Upper
71        p1 = ceil(m*XTUNE(1)+n*YTUNE(2));
72        p2 = floor(m*XTUNE(2)+n*YTUNE(2));
73       
74        for p=p1:p2
75            if m
76                NLMAX = NLMAX+1;
77                R(NLMAX,:) = [abs(m)+abs(n),m,n,p,(p-n*YTUNE(2))/m,YTUNE(2)];
78            end
79        end
80       
81        % Right
82        p1 = ceil(m*XTUNE(2)+n*YTUNE(1));
83        p2 = floor(m*XTUNE(2)+n*YTUNE(2));
84       
85        for p=p1:p2
86            if n
87                NLMAX = NLMAX+1;
88                R(NLMAX,:) = [abs(m)+abs(n),m,n,p,XTUNE(2),(p-m*XTUNE(2))/n];
89            end
90        end
91       
92        % ========================
93        n = -r+m;
94       
95        % Lower
96        p1 = ceil(m*XTUNE(1)+n*YTUNE(1));
97        p2 = floor(m*XTUNE(2)+n*YTUNE(1));
98       
99        for p =p1:p2
100            if m % lines with m=0 do not cross upper and lower sides
101                NLMAX = NLMAX+1;
102                R(NLMAX,:) = [abs(m)+abs(n),m,n,p,(p-n*YTUNE(1))/m,YTUNE(1)];
103            end
104        end
105       
106        % Left
107        % Note: negative n
108        p1 = floor(m*XTUNE(1)+n*YTUNE(1));
109        p2 = ceil(m*XTUNE(1)+n*YTUNE(2));
110       
111        for p =p2:p1
112            if n % lines with n=0 do not cross left and right sides
113                NLMAX = NLMAX+1;
114                R(NLMAX,:) = [abs(m)+abs(n),m,n,p,XTUNE(1),(p-m*XTUNE(1))/n];
115            end
116        end
117       
118        % Upper
119        p1 = ceil(m*XTUNE(1)+n*YTUNE(2));
120        p2 = floor(m*XTUNE(2)+n*YTUNE(2));
121       
122        for p=p1:p2
123            if m
124                NLMAX = NLMAX+1;
125                R(NLMAX,:) = [abs(m)+abs(n),m,n,p,(p-n*YTUNE(2))/m,YTUNE(2)];
126            end
127        end
128       
129        % Right
130        % Note: negative n
131       
132        p1 = floor(m*XTUNE(2)+n*YTUNE(1));
133        p2 = ceil(m*XTUNE(2)+n*YTUNE(2));
134        for p=p2:p1
135            if n
136                NLMAX = NLMAX+1;
137                R(NLMAX,:) = [abs(m)+abs(n),m,n,p,XTUNE(2),(p-m*XTUNE(2))/n];
138            end
139        end
140    end
141end
142%R = sortrows(R(1:NLMAX,:));
143R = unique(R(1:NLMAX,:),'rows');
144[temp,I,J] = unique(R(:,1:4),'rows');
145K = I(find(diff([0;I])==2))-1;
146
147RESNUM = [R(K,1:4)]; % [o, m, n, p] O = |m| + |n|
148X1 = R(K,5);
149X2 = R(K+1,5);
150Y1 = R(K,6);
151Y2 = R(K+1,6);
152
153
154% Remove accidental lines that are on the box edge
155K1 = (X1==X2) & (X1==XTUNE(1));
156K2 = (X1==X2) & (X1==XTUNE(2));
157K3 = (Y1==Y2) & (Y1==YTUNE(1));
158K4 = (Y1==Y2) & (Y1==YTUNE(2));
159
160K = find(~(K1 | K2 | K3 | K4));
161
162
163RESNUM = RESNUM(K,:);
164X1 = X1(K);
165X2 = X2(K);
166Y1 = Y1(K);
167Y2 = Y2(K);
168
169
170R = [RESNUM,X1,Y1,X2,Y2];
171
172
173
174
175
176
177NL = size(RESNUM,1);
178for i = 1:NL
179    hl = line([X1(i) X2(i)],[Y1(i) Y2(i)]);
180    if ~isempty(LINEARGS)
181        set(hl,LINEARGS{:});
182    end
183end
184
185
186   
Note: See TracBrowser for help on using the repository browser.