1 | function intlat(varargin) |
---|
2 | %INTLAT - interactive AT lattice editor |
---|
3 | % INTLAT(DIRECTION) |
---|
4 | % Direction is the initial angle[rad] of the orbit with respect |
---|
5 | % to the plot axis |
---|
6 | |
---|
7 | global THERING |
---|
8 | if nargin < 1 | isnumeric(varargin{1}) |
---|
9 | |
---|
10 | if nargin == 1 |
---|
11 | STARTANGLE = varargin{1}; |
---|
12 | else |
---|
13 | STARTANGLE = 0; |
---|
14 | end |
---|
15 | |
---|
16 | % LAUNCH GUI |
---|
17 | |
---|
18 | fig = openfig(mfilename,'reuse'); |
---|
19 | |
---|
20 | set(fig,'ToolBar','none','HandleVisibility','callback'); |
---|
21 | |
---|
22 | handles = guihandles(fig); |
---|
23 | |
---|
24 | guidata(fig,handles); |
---|
25 | NumElements = length(THERING); |
---|
26 | AllFamNames = getcellstruct(THERING,'FamName',1:NumElements); |
---|
27 | NumFamilies = 0; |
---|
28 | |
---|
29 | FamNames = {}; |
---|
30 | for i=1:NumElements |
---|
31 | if ~any(strcmp(FamNames,THERING{i}.FamName)) |
---|
32 | NumFamilies = NumFamilies + 1; |
---|
33 | FamNames{NumFamilies} = THERING{i}.FamName; |
---|
34 | end |
---|
35 | end |
---|
36 | FamNames = sort(FamNames); |
---|
37 | [Families(1:NumFamilies).FamName] = deal(FamNames{:}); |
---|
38 | |
---|
39 | set(handles.FamilyPMenu,'String',FamNames); |
---|
40 | set(handles.IconTypePMenu,'String',{'line','rectangle','o','x'}); |
---|
41 | |
---|
42 | [x2d, y2d, a2d] = Survey2D(THERING,STARTANGLE); |
---|
43 | XScale=max(x2d)-min(x2d); |
---|
44 | YScale=max(y2d)-min(y2d); |
---|
45 | |
---|
46 | set(handles.Axes,'DataAspectRatioMode','manual', ... |
---|
47 | 'DataAspectRatio',[1 1 1],... |
---|
48 | 'PlotBoxAspectRatioMode','manual', ... |
---|
49 | 'PlotBoxAspectRatio',[XScale YScale 1]); |
---|
50 | |
---|
51 | |
---|
52 | FamNumbers = zeros(1,NumElements); |
---|
53 | |
---|
54 | for i =1:NumFamilies |
---|
55 | Families(i).KidsList = find(strcmp(Families(i).FamName, AllFamNames)); |
---|
56 | Families(i).Display = 1; |
---|
57 | Families(i).Color = [0 0 0]; |
---|
58 | Families(i).IconType = 'line'; |
---|
59 | Families(i).FieldsList = fieldnames(THERING{Families(i).KidsList(1)}); |
---|
60 | |
---|
61 | Families(i).SelectedFields = [find(strcmp(Families(i).FieldsList,'FamName')),... |
---|
62 | find(strcmp(Families(i).FieldsList,'Length')),... |
---|
63 | find(strcmp(Families(i).FieldsList,'PassMethod'))]; |
---|
64 | |
---|
65 | Families(i).IconWidth = 0; |
---|
66 | FamNumbers(Families(i).KidsList)=i; |
---|
67 | |
---|
68 | end |
---|
69 | |
---|
70 | Elements = struct('FamIndex',num2cell(FamNumbers),'IconHandle',0); |
---|
71 | Families = SetDefaultIcons(THERING,Families,Elements); |
---|
72 | |
---|
73 | setappdata(fig,'Families',Families); |
---|
74 | setappdata(fig,'Elements',Elements); |
---|
75 | setappdata(fig,'X2D',x2d); |
---|
76 | setappdata(fig,'Y2D',y2d); |
---|
77 | setappdata(fig,'A2D',a2d); |
---|
78 | |
---|
79 | |
---|
80 | |
---|
81 | ShowFamilyDisplayMode(handles.FamilyPMenu, [], handles); |
---|
82 | |
---|
83 | % Plot all elements |
---|
84 | PlotElements(handles,1:NumElements); |
---|
85 | |
---|
86 | |
---|
87 | elseif ischar(varargin{1}) % INVOKE NAMED SUBFUNCTION OR CALLBACK |
---|
88 | |
---|
89 | try |
---|
90 | if (nargout) |
---|
91 | [varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard |
---|
92 | else |
---|
93 | feval(varargin{:}); % FEVAL switchyard |
---|
94 | end |
---|
95 | catch err |
---|
96 | disp(lasterr); |
---|
97 | end |
---|
98 | |
---|
99 | end |
---|
100 | |
---|
101 | |
---|
102 | function f = SetDefaultIcons(Lattice,Families,Elements) |
---|
103 | L = findspos(Lattice,length(Lattice)+1); |
---|
104 | f = Families; |
---|
105 | % Make default icons for elements of different physical types |
---|
106 | for i=1:length(Families) |
---|
107 | Elem = Lattice{Families(i).KidsList(1)}; |
---|
108 | % make icons for bending magnets |
---|
109 | if isfield(Elem,'BendingAngle') & Elem.BendingAngle |
---|
110 | f(i).Display = 1; |
---|
111 | f(i).Color = [1 1 0]; |
---|
112 | f(i).IconType = 'rectangle'; |
---|
113 | f(i).IconWidth = L/300; |
---|
114 | % Quadrupoles |
---|
115 | elseif isfield(Elem,'K') & Elem.K |
---|
116 | if Elem.K > 0 % focusing |
---|
117 | f(i).Display = 1; |
---|
118 | f(i).Color = [1 0 0]; |
---|
119 | f(i).IconType = 'rectangle'; |
---|
120 | f(i).IconWidth = L/400; |
---|
121 | else |
---|
122 | f(i).Display = 1; |
---|
123 | f(i).Color = [0 0 1]; |
---|
124 | f(i).IconType = 'rectangle'; |
---|
125 | f(i).IconWidth = L/400; |
---|
126 | end |
---|
127 | elseif isfield(Elem,'PolynomB') & length(Elem.PolynomB)>2 & Elem.PolynomB(3) |
---|
128 | if Elem.PolynomB(3)>0 % focusing sextupole |
---|
129 | f(i).Display = 1; |
---|
130 | f(i).Color = [1 0 1]; |
---|
131 | f(i).IconType = 'rectangle'; |
---|
132 | f(i).IconWidth = L/500; |
---|
133 | else |
---|
134 | f(i).Display = 1; |
---|
135 | f(i).Color = [0 1 0]; |
---|
136 | f(i).IconType = 'rectangle'; |
---|
137 | f(i).IconWidth = L/500; |
---|
138 | end |
---|
139 | elseif isfield(Elem,'Frequency') & isfield(Elem,'Voltage') % RF cavity |
---|
140 | f(i).Display = 1; |
---|
141 | f(i).Color = [1 0.5 0]; |
---|
142 | f(i).IconType = 'o'; |
---|
143 | f(i).IconWidth = 0; |
---|
144 | |
---|
145 | end |
---|
146 | |
---|
147 | end |
---|
148 | |
---|
149 | |
---|
150 | |
---|
151 | |
---|
152 | % -------------------------------------------------------------------- |
---|
153 | function PlotElements(GUIhandles,INDEX) |
---|
154 | % Retrieve application data |
---|
155 | |
---|
156 | % Temporarily set handle visibility to 'on' for drawing elements |
---|
157 | set(GUIhandles.IntlatMainFigure,'HandleVisibility','on'); |
---|
158 | |
---|
159 | figure(GUIhandles.IntlatMainFigure); |
---|
160 | Families = getappdata(GUIhandles.IntlatMainFigure,'Families'); |
---|
161 | Elements = getappdata(GUIhandles.IntlatMainFigure,'Elements'); |
---|
162 | x2d = getappdata(GUIhandles.IntlatMainFigure,'X2D'); |
---|
163 | y2d = getappdata(GUIhandles.IntlatMainFigure,'Y2D'); |
---|
164 | a2d = getappdata(GUIhandles.IntlatMainFigure,'A2D'); |
---|
165 | |
---|
166 | |
---|
167 | xcorners = [-1 -1 1 1]; |
---|
168 | ycorners = [ 1 1 -1 -1]; |
---|
169 | |
---|
170 | |
---|
171 | for i=[INDEX(:)]' |
---|
172 | |
---|
173 | FamIndex = Elements(i).FamIndex; |
---|
174 | |
---|
175 | % If Icon already exists |
---|
176 | if Elements(i).IconHandle |
---|
177 | delete(Elements(i).IconHandle); |
---|
178 | Elements(i).IconHandle = 0; |
---|
179 | end |
---|
180 | |
---|
181 | if Families(FamIndex).Display |
---|
182 | switch Families(FamIndex).IconType |
---|
183 | case 'rectangle' |
---|
184 | % compute vertex coordinates |
---|
185 | IconWidth = Families(FamIndex).IconWidth; |
---|
186 | vx = [ x2d(i), x2d(i+1), x2d(i+1), x2d(i)] + IconWidth*xcorners*sin((a2d(i)+a2d(i+1))/2); |
---|
187 | vy = [ y2d(i), y2d(i+1), y2d(i+1), y2d(i)] + IconWidth*ycorners*cos((a2d(i)+a2d(i+1))/2); |
---|
188 | Elements(i).IconHandle = patch(vx,vy,Families(FamIndex).Color); |
---|
189 | case 'line' |
---|
190 | Elements(i).IconHandle = line([x2d(i) x2d(i+1)],[y2d(i) y2d(i+1)]); |
---|
191 | set(Elements(i).IconHandle,'Color',Families(FamIndex).Color); |
---|
192 | case 'o' |
---|
193 | Elements(i).IconHandle = line([x2d(i) x2d(i+1)],[y2d(i) y2d(i+1)]); |
---|
194 | set(Elements(i).IconHandle,'Color',Families(FamIndex).Color,... |
---|
195 | 'Marker','o','MarkerFaceColor',Families(FamIndex).Color); |
---|
196 | case 'x' |
---|
197 | Elements(i).IconHandle = line([x2d(i) x2d(i+1)],[y2d(i) y2d(i+1)]); |
---|
198 | set(Elements(i).IconHandle,'Color',Families(FamIndex).Color,... |
---|
199 | 'Marker','x'); |
---|
200 | end |
---|
201 | % Assign Callback |
---|
202 | set(Elements(i).IconHandle,'UserData',i,... |
---|
203 | 'ButtonDownFcn','intlat(''ElementCallback'',gcbo)'); |
---|
204 | end |
---|
205 | end |
---|
206 | |
---|
207 | setappdata(GUIhandles.IntlatMainFigure,'Elements',Elements); |
---|
208 | set(GUIhandles.IntlatMainFigure,'HandleVisibility','callback'); |
---|
209 | |
---|
210 | |
---|
211 | |
---|
212 | |
---|
213 | |
---|
214 | |
---|
215 | |
---|
216 | function ShowFamilyDisplayMode(h, eventdata, handles, varargin) |
---|
217 | Families = getappdata(handles.IntlatMainFigure,'Families'); |
---|
218 | FNum = get(h,'Value'); |
---|
219 | |
---|
220 | set(handles.DisplayCheckBox,'Value',Families(FNum).Display); |
---|
221 | |
---|
222 | |
---|
223 | |
---|
224 | set(handles.ColorSelectionBox,'BackgroundColor',Families(FNum).Color); |
---|
225 | |
---|
226 | set(handles.FieldListBox,'String',Families(FNum).FieldsList); |
---|
227 | set(handles.FieldListBox,'Value',Families(FNum).SelectedFields); |
---|
228 | |
---|
229 | set(handles.WidthEditBox,'String',num2str(Families(FNum).IconWidth)); |
---|
230 | |
---|
231 | PossibleIconTypes = get(handles.IconTypePMenu,'String'); |
---|
232 | |
---|
233 | set(handles.IconTypePMenu,'value',... |
---|
234 | find(strcmp(Families(FNum).IconType,PossibleIconTypes))); |
---|
235 | |
---|
236 | |
---|
237 | if Families(FNum).Display |
---|
238 | Visible = 'on'; |
---|
239 | else |
---|
240 | Visible = 'off'; |
---|
241 | end |
---|
242 | set(handles.IconTypePMenu,'Visible',Visible); |
---|
243 | set(handles.ColorSelectionBox,'Visible',Visible); |
---|
244 | set(handles.WidthEditBox,'Visible',Visible); |
---|
245 | set(handles.FieldListBox,'Visible',Visible); |
---|
246 | set(handles.DisplayIconLabel,'Visible',Visible); |
---|
247 | set(handles.IconColorLabel,'Visible',Visible); |
---|
248 | set(handles.IconWidthLabel,'Visible',Visible); |
---|
249 | set(handles.FieldsLabel,'Visible',Visible); |
---|
250 | |
---|
251 | |
---|
252 | function SelectColor(h, eventdata, handles, varargin) |
---|
253 | Families = getappdata(handles.IntlatMainFigure,'Families'); |
---|
254 | FNum = get(handles.FamilyPMenu,'Value'); |
---|
255 | NewColor = uisetcolor(['Select icon color for ',Families(FNum).FamName,' family']); |
---|
256 | |
---|
257 | set(handles.ColorSelectionBox,'BackgroundColor',NewColor); |
---|
258 | Families(FNum).Color = NewColor; |
---|
259 | setappdata(handles.IntlatMainFigure,'Families',Families); |
---|
260 | |
---|
261 | PlotElements(handles,Families(FNum).KidsList); |
---|
262 | |
---|
263 | function SetIconType(h, eventdata, handles, varargin) |
---|
264 | Families = getappdata(handles.IntlatMainFigure,'Families'); |
---|
265 | FNum = get(handles.FamilyPMenu,'Value'); |
---|
266 | |
---|
267 | PossibleIconTypes = get(handles.IconTypePMenu,'String'); |
---|
268 | |
---|
269 | NewIconType = PossibleIconTypes{get(h,'Value')}; |
---|
270 | |
---|
271 | Families(FNum).IconType = NewIconType; |
---|
272 | setappdata(handles.IntlatMainFigure,'Families',Families); |
---|
273 | |
---|
274 | |
---|
275 | PlotElements(handles,Families(FNum).KidsList); |
---|
276 | |
---|
277 | function SetDisplay(h, eventdata, handles, varargin) |
---|
278 | Families = getappdata(handles.IntlatMainFigure,'Families'); |
---|
279 | FNum = get(handles.FamilyPMenu,'Value'); |
---|
280 | |
---|
281 | Families(FNum).Display = get(h,'Value'); |
---|
282 | |
---|
283 | |
---|
284 | if Families(FNum).Display |
---|
285 | Visible = 'on'; |
---|
286 | else |
---|
287 | Visible = 'off'; |
---|
288 | end |
---|
289 | set(handles.IconTypePMenu,'Visible',Visible); |
---|
290 | set(handles.ColorSelectionBox,'Visible',Visible); |
---|
291 | set(handles.WidthEditBox,'Visible',Visible); |
---|
292 | set(handles.FieldListBox,'Visible',Visible); |
---|
293 | set(handles.DisplayIconLabel,'Visible',Visible); |
---|
294 | set(handles.IconColorLabel,'Visible',Visible); |
---|
295 | set(handles.IconWidthLabel,'Visible',Visible); |
---|
296 | set(handles.FieldsLabel,'Visible',Visible); |
---|
297 | |
---|
298 | setappdata(handles.IntlatMainFigure,'Families',Families); |
---|
299 | PlotElements(handles,Families(FNum).KidsList); |
---|
300 | % -------------------------------------------------------------------- |
---|
301 | function SelectFieldsFromList(h, eventdata, handles, varargin) |
---|
302 | |
---|
303 | Families = getappdata(handles.IntlatMainFigure,'Families'); |
---|
304 | FNum = get(handles.FamilyPMenu,'Value'); |
---|
305 | Families(FNum).SelectedFields = get(h,'Value'); |
---|
306 | setappdata(handles.IntlatMainFigure,'Families',Families); |
---|
307 | |
---|
308 | |
---|
309 | function SetIconWidth(h, eventdata, handles, varargin) |
---|
310 | |
---|
311 | Families = getappdata(handles.IntlatMainFigure,'Families'); |
---|
312 | FNum = get(handles.FamilyPMenu,'Value'); |
---|
313 | NewIconWidth = str2double(get(h,'String')); |
---|
314 | Families(FNum).IconWidth = NewIconWidth; |
---|
315 | setappdata(handles.IntlatMainFigure,'Families',Families); |
---|
316 | PlotElements(handles,Families(FNum).KidsList); |
---|
317 | |
---|
318 | |
---|
319 | |
---|
320 | |
---|
321 | |
---|
322 | % -------------------------------------------------------------------- |
---|
323 | function varargout = ElementCallback(h) |
---|
324 | |
---|
325 | index = get(h,'UserData'); |
---|
326 | handles = guidata(gcbo); |
---|
327 | Families = getappdata(handles.IntlatMainFigure,'Families'); |
---|
328 | Elements = getappdata(handles.IntlatMainFigure,'Elements'); |
---|
329 | |
---|
330 | FamIndex= Elements(index).FamIndex; |
---|
331 | FieldsList = Families(FamIndex).FieldsList; |
---|
332 | Fields2Edit = FieldsList(Families(FamIndex).SelectedFields); |
---|
333 | |
---|
334 | intelem(index,Fields2Edit); |
---|
335 | |
---|
336 | % -------------------------------------------------------------------- |
---|
337 | function varargout = ColorSelection_Callback(h, eventdata, handles, varargin) |
---|
338 | |
---|
339 | |
---|
340 | |
---|
341 | |
---|
342 | % -------------------------------------------------------------------- |
---|
343 | function varargout = listbox1_Callback(h, eventdata, handles, varargin) |
---|
344 | |
---|
345 | |
---|
346 | |
---|
347 | |
---|
348 | % -------------------------------------------------------------------- |
---|
349 | function varargout = Untitled_1_Callback(h, eventdata, handles, varargin) |
---|
350 | |
---|
351 | |
---|
352 | |
---|
353 | |
---|
354 | % -------------------------------------------------------------------- |
---|
355 | function varargout = ZoomButtonCallback(h, eventdata, handles, varargin) |
---|
356 | |
---|
357 | |
---|
358 | |
---|
359 | |
---|
360 | % -------------------------------------------------------------------- |
---|
361 | function [x2d, y2d, a2d] = Survey2D(LATTICE,STARTANGLE) |
---|
362 | % Determine 2-d geometry of the LATTICE |
---|
363 | NumElements = length(LATTICE); |
---|
364 | x2d = zeros(1,NumElements+1); |
---|
365 | y2d = zeros(1,NumElements+1); |
---|
366 | a2d = zeros(1,NumElements+1); % angle of orbit in radians |
---|
367 | a2d(1) = STARTANGLE; |
---|
368 | for en = 1:NumElements-1 |
---|
369 | if isfield(LATTICE{en},'BendingAngle') |
---|
370 | ba = LATTICE{en}.BendingAngle(1); % bending angle in radians |
---|
371 | else |
---|
372 | ba = 0; |
---|
373 | end |
---|
374 | |
---|
375 | if ba == 0 |
---|
376 | Lt = LATTICE{en}.Length; |
---|
377 | Lp = 0; |
---|
378 | else |
---|
379 | Lt = LATTICE{en}.Length*sin(ba)/ba; |
---|
380 | Lp = -LATTICE{en}.Length*(1-cos(ba))/ba; |
---|
381 | end |
---|
382 | |
---|
383 | x2d(en+1) = x2d(en) + Lt*cos(a2d(en)) - Lp*sin(a2d(en)); |
---|
384 | y2d(en+1) = y2d(en) + Lt*sin(a2d(en)) + Lp*cos(a2d(en)); |
---|
385 | a2d(en+1)=a2d(en) - ba; |
---|
386 | |
---|
387 | end |
---|
388 | x2d(NumElements+1) = x2d(1); |
---|
389 | y2d(NumElements+1) = y2d(1); |
---|
390 | a2d(NumElements+1) = a2d(1); |
---|
391 | |
---|
392 | X0 = (max(x2d)+min(x2d))/2; |
---|
393 | Y0 = (max(y2d)+min(y2d))/2; |
---|
394 | x2d = x2d - X0; |
---|
395 | y2d = y2d - Y0; |
---|