Home > atgui > intlat.m

intlat

PURPOSE ^

INTLAT - interactive AT lattice editor

SYNOPSIS ^

function intlat(varargin)

DESCRIPTION ^

INTLAT - interactive AT lattice editor
 INTLAT(DIRECTION)
  Direction is the initial angle[rad] of the orbit with respect
  to the plot axis

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function intlat(varargin)
0002 %INTLAT - interactive AT lattice editor
0003 % INTLAT(DIRECTION)
0004 %  Direction is the initial angle[rad] of the orbit with respect
0005 %  to the plot axis
0006 
0007 global THERING
0008 if nargin < 1 | isnumeric(varargin{1})
0009 
0010     if nargin == 1
0011         STARTANGLE = varargin{1};
0012     else
0013         STARTANGLE = 0;
0014     end
0015     
0016     % LAUNCH GUI
0017 
0018     fig = openfig(mfilename,'reuse');
0019     
0020     set(fig,'ToolBar','none','HandleVisibility','callback');
0021 
0022     handles = guihandles(fig);
0023     
0024     guidata(fig,handles);
0025     NumElements = length(THERING);
0026     AllFamNames  = getcellstruct(THERING,'FamName',1:NumElements);
0027     NumFamilies = 0;
0028     
0029     FamNames = {};
0030     for i=1:NumElements
0031         if ~any(strcmp(FamNames,THERING{i}.FamName))
0032             NumFamilies = NumFamilies + 1;
0033             FamNames{NumFamilies} = THERING{i}.FamName;
0034         end
0035     end
0036     FamNames = sort(FamNames);
0037     [Families(1:NumFamilies).FamName] = deal(FamNames{:});
0038     
0039     set(handles.FamilyPMenu,'String',FamNames);
0040     set(handles.IconTypePMenu,'String',{'line','rectangle','o','x'});
0041     
0042     [x2d, y2d, a2d] = Survey2D(THERING,STARTANGLE);
0043        XScale=max(x2d)-min(x2d);
0044     YScale=max(y2d)-min(y2d);
0045 
0046     set(handles.Axes,'DataAspectRatioMode','manual', ... 
0047         'DataAspectRatio',[1 1 1],...
0048         'PlotBoxAspectRatioMode','manual', ...
0049         'PlotBoxAspectRatio',[XScale YScale 1]);
0050     
0051     
0052     FamNumbers = zeros(1,NumElements);    
0053     
0054     for i =1:NumFamilies
0055         Families(i).KidsList = find(strcmp(Families(i).FamName, AllFamNames));
0056         Families(i).Display = 1;
0057         Families(i).Color = [0 0 0];
0058         Families(i).IconType = 'line';
0059         Families(i).FieldsList = fieldnames(THERING{Families(i).KidsList(1)});
0060                 
0061         Families(i).SelectedFields = [find(strcmp(Families(i).FieldsList,'FamName')),...
0062                 find(strcmp(Families(i).FieldsList,'Length')),...
0063                 find(strcmp(Families(i).FieldsList,'PassMethod'))];
0064                 
0065         Families(i).IconWidth = 0;
0066         FamNumbers(Families(i).KidsList)=i;
0067         
0068     end
0069     
0070     Elements = struct('FamIndex',num2cell(FamNumbers),'IconHandle',0);
0071     Families = SetDefaultIcons(THERING,Families,Elements);
0072     
0073     setappdata(fig,'Families',Families);
0074     setappdata(fig,'Elements',Elements);
0075     setappdata(fig,'X2D',x2d);
0076     setappdata(fig,'Y2D',y2d);
0077     setappdata(fig,'A2D',a2d);
0078 
0079  
0080         
0081     ShowFamilyDisplayMode(handles.FamilyPMenu, [], handles);
0082     
0083     % Plot all elements
0084     PlotElements(handles,1:NumElements);
0085     
0086         
0087 elseif ischar(varargin{1}) % INVOKE NAMED SUBFUNCTION OR CALLBACK
0088 
0089     try
0090         if (nargout)
0091             [varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard
0092         else
0093             feval(varargin{:}); % FEVAL switchyard
0094         end
0095     catch
0096         disp(lasterr);
0097     end
0098 
0099 end
0100 
0101 
0102 function f = SetDefaultIcons(Lattice,Families,Elements)
0103     L = findspos(Lattice,length(Lattice)+1);
0104     f = Families;
0105     % Make default icons for elements of different physical types
0106     for i=1:length(Families)
0107         Elem = Lattice{Families(i).KidsList(1)};
0108         % make icons for bending magnets
0109         if isfield(Elem,'BendingAngle') & Elem.BendingAngle
0110             f(i).Display = 1;
0111             f(i).Color = [1 1 0];
0112             f(i).IconType = 'rectangle';
0113             f(i).IconWidth = L/300;
0114         % Quadrupoles
0115         elseif isfield(Elem,'K') & Elem.K
0116             if Elem.K > 0 % focusing
0117                 f(i).Display = 1;
0118                 f(i).Color = [1 0 0];
0119                 f(i).IconType = 'rectangle';
0120                 f(i).IconWidth = L/400;
0121             else
0122                 f(i).Display = 1;
0123                 f(i).Color = [0 0 1];
0124                 f(i).IconType = 'rectangle';
0125                 f(i).IconWidth = L/400;
0126             end
0127         elseif isfield(Elem,'PolynomB') & length(Elem.PolynomB)>2 & Elem.PolynomB(3)
0128             if Elem.PolynomB(3)>0 % focusing sextupole
0129                 f(i).Display = 1;
0130                 f(i).Color = [1 0 1];
0131                 f(i).IconType = 'rectangle';
0132                 f(i).IconWidth = L/500;
0133             else
0134                 f(i).Display = 1;
0135                 f(i).Color = [0 1 0];
0136                 f(i).IconType = 'rectangle';
0137                 f(i).IconWidth = L/500;
0138             end
0139         elseif isfield(Elem,'Frequency') & isfield(Elem,'Voltage') % RF cavity
0140             f(i).Display = 1;
0141             f(i).Color = [1 0.5 0];
0142             f(i).IconType = 'o';
0143             f(i).IconWidth = 0;
0144             
0145         end
0146         
0147     end
0148             
0149         
0150 
0151 
0152 % --------------------------------------------------------------------
0153 function PlotElements(GUIhandles,INDEX)
0154     % Retrieve application data
0155     
0156     % Temporarily set handle visibility to 'on' for drawing elements
0157     set(GUIhandles.IntlatMainFigure,'HandleVisibility','on');
0158     
0159     figure(GUIhandles.IntlatMainFigure);
0160     Families = getappdata(GUIhandles.IntlatMainFigure,'Families');
0161     Elements = getappdata(GUIhandles.IntlatMainFigure,'Elements');
0162     x2d = getappdata(GUIhandles.IntlatMainFigure,'X2D');
0163     y2d = getappdata(GUIhandles.IntlatMainFigure,'Y2D');
0164     a2d = getappdata(GUIhandles.IntlatMainFigure,'A2D');
0165 
0166     
0167     xcorners = [-1 -1  1  1];
0168     ycorners = [ 1  1 -1 -1];
0169     
0170     
0171     for i=[INDEX(:)]'
0172         
0173         FamIndex = Elements(i).FamIndex;
0174         
0175         % If Icon already exists
0176         if Elements(i).IconHandle
0177             delete(Elements(i).IconHandle);
0178             Elements(i).IconHandle = 0;
0179         end
0180         
0181         if  Families(FamIndex).Display
0182             switch Families(FamIndex).IconType
0183             case 'rectangle'
0184                 % compute vertex coordinates
0185                 IconWidth = Families(FamIndex).IconWidth;
0186                 vx = [ x2d(i), x2d(i+1), x2d(i+1), x2d(i)] + IconWidth*xcorners*sin((a2d(i)+a2d(i+1))/2);
0187                   vy = [ y2d(i), y2d(i+1), y2d(i+1), y2d(i)] + IconWidth*ycorners*cos((a2d(i)+a2d(i+1))/2);
0188                   Elements(i).IconHandle = patch(vx,vy,Families(FamIndex).Color);
0189             case 'line'
0190                 Elements(i).IconHandle = line([x2d(i) x2d(i+1)],[y2d(i) y2d(i+1)]);
0191                 set(Elements(i).IconHandle,'Color',Families(FamIndex).Color);   
0192             case 'o'
0193                 Elements(i).IconHandle = line([x2d(i) x2d(i+1)],[y2d(i) y2d(i+1)]);
0194                 set(Elements(i).IconHandle,'Color',Families(FamIndex).Color,...
0195                     'Marker','o','MarkerFaceColor',Families(FamIndex).Color);
0196             case 'x'
0197                 Elements(i).IconHandle = line([x2d(i) x2d(i+1)],[y2d(i) y2d(i+1)]);
0198                 set(Elements(i).IconHandle,'Color',Families(FamIndex).Color,...
0199                     'Marker','x');
0200             end
0201             % Assign Callback
0202             set(Elements(i).IconHandle,'UserData',i,...
0203                 'ButtonDownFcn','intlat(''ElementCallback'',gcbo)');
0204         end
0205     end
0206     
0207     setappdata(GUIhandles.IntlatMainFigure,'Elements',Elements);
0208     set(GUIhandles.IntlatMainFigure,'HandleVisibility','callback');
0209     
0210             
0211             
0212             
0213     
0214         
0215 
0216 function ShowFamilyDisplayMode(h, eventdata, handles, varargin)
0217     Families = getappdata(handles.IntlatMainFigure,'Families');
0218     FNum = get(h,'Value');    
0219     
0220     set(handles.DisplayCheckBox,'Value',Families(FNum).Display);
0221     
0222     
0223     
0224     set(handles.ColorSelectionBox,'BackgroundColor',Families(FNum).Color);
0225     
0226     set(handles.FieldListBox,'String',Families(FNum).FieldsList);
0227     set(handles.FieldListBox,'Value',Families(FNum).SelectedFields);
0228     
0229     set(handles.WidthEditBox,'String',num2str(Families(FNum).IconWidth));
0230     
0231     PossibleIconTypes = get(handles.IconTypePMenu,'String');
0232     
0233     set(handles.IconTypePMenu,'value',...
0234         find(strcmp(Families(FNum).IconType,PossibleIconTypes)));
0235     
0236     
0237     if Families(FNum).Display 
0238         Visible = 'on';
0239     else
0240         Visible = 'off';
0241     end
0242     set(handles.IconTypePMenu,'Visible',Visible);
0243     set(handles.ColorSelectionBox,'Visible',Visible);
0244     set(handles.WidthEditBox,'Visible',Visible);
0245     set(handles.FieldListBox,'Visible',Visible);
0246     set(handles.DisplayIconLabel,'Visible',Visible);
0247     set(handles.IconColorLabel,'Visible',Visible);
0248     set(handles.IconWidthLabel,'Visible',Visible);
0249     set(handles.FieldsLabel,'Visible',Visible);
0250 
0251         
0252 function SelectColor(h, eventdata, handles, varargin)
0253     Families = getappdata(handles.IntlatMainFigure,'Families');
0254     FNum = get(handles.FamilyPMenu,'Value');
0255     NewColor = uisetcolor(['Select icon color for ',Families(FNum).FamName,' family']);
0256     
0257     set(handles.ColorSelectionBox,'BackgroundColor',NewColor);
0258     Families(FNum).Color = NewColor;
0259     setappdata(handles.IntlatMainFigure,'Families',Families);
0260     
0261     PlotElements(handles,Families(FNum).KidsList);
0262 
0263 function SetIconType(h, eventdata, handles, varargin)
0264     Families = getappdata(handles.IntlatMainFigure,'Families');
0265     FNum = get(handles.FamilyPMenu,'Value');
0266 
0267     PossibleIconTypes = get(handles.IconTypePMenu,'String');
0268     
0269     NewIconType = PossibleIconTypes{get(h,'Value')};
0270     
0271     Families(FNum).IconType = NewIconType;
0272     setappdata(handles.IntlatMainFigure,'Families',Families);
0273     
0274     
0275     PlotElements(handles,Families(FNum).KidsList);
0276     
0277 function SetDisplay(h, eventdata, handles, varargin)
0278     Families = getappdata(handles.IntlatMainFigure,'Families');
0279     FNum = get(handles.FamilyPMenu,'Value');
0280 
0281     Families(FNum).Display = get(h,'Value');
0282     
0283     
0284     if Families(FNum).Display 
0285         Visible = 'on';
0286     else
0287         Visible = 'off';
0288     end
0289     set(handles.IconTypePMenu,'Visible',Visible);
0290     set(handles.ColorSelectionBox,'Visible',Visible);
0291     set(handles.WidthEditBox,'Visible',Visible);
0292     set(handles.FieldListBox,'Visible',Visible);
0293     set(handles.DisplayIconLabel,'Visible',Visible);
0294     set(handles.IconColorLabel,'Visible',Visible);
0295     set(handles.IconWidthLabel,'Visible',Visible);
0296     set(handles.FieldsLabel,'Visible',Visible);
0297     
0298     setappdata(handles.IntlatMainFigure,'Families',Families);
0299     PlotElements(handles,Families(FNum).KidsList);
0300 % --------------------------------------------------------------------
0301 function SelectFieldsFromList(h, eventdata, handles, varargin)
0302 
0303     Families = getappdata(handles.IntlatMainFigure,'Families');
0304     FNum = get(handles.FamilyPMenu,'Value');
0305     Families(FNum).SelectedFields =  get(h,'Value');
0306     setappdata(handles.IntlatMainFigure,'Families',Families);
0307     
0308     
0309 function SetIconWidth(h, eventdata, handles, varargin)
0310 
0311     Families = getappdata(handles.IntlatMainFigure,'Families');
0312     FNum = get(handles.FamilyPMenu,'Value');
0313     NewIconWidth = str2double(get(h,'String'));
0314     Families(FNum).IconWidth =  NewIconWidth;
0315     setappdata(handles.IntlatMainFigure,'Families',Families);
0316     PlotElements(handles,Families(FNum).KidsList);
0317 
0318     
0319     
0320 
0321 
0322 % --------------------------------------------------------------------
0323 function varargout = ElementCallback(h)
0324 
0325 index = get(h,'UserData');
0326 handles = guidata(gcbo);
0327 Families = getappdata(handles.IntlatMainFigure,'Families');
0328 Elements = getappdata(handles.IntlatMainFigure,'Elements');
0329 
0330 FamIndex= Elements(index).FamIndex;
0331 FieldsList =  Families(FamIndex).FieldsList;
0332 Fields2Edit = FieldsList(Families(FamIndex).SelectedFields);
0333 
0334 intelem(index,Fields2Edit);
0335 
0336 % --------------------------------------------------------------------
0337 function varargout = ColorSelection_Callback(h, eventdata, handles, varargin)
0338 
0339 
0340 
0341 
0342 % --------------------------------------------------------------------
0343 function varargout = listbox1_Callback(h, eventdata, handles, varargin)
0344 
0345 
0346 
0347 
0348 % --------------------------------------------------------------------
0349 function varargout = Untitled_1_Callback(h, eventdata, handles, varargin)
0350 
0351 
0352 
0353 
0354 % --------------------------------------------------------------------
0355 function varargout = ZoomButtonCallback(h, eventdata, handles, varargin)
0356 
0357 
0358 
0359 
0360 % --------------------------------------------------------------------
0361 function [x2d, y2d, a2d] = Survey2D(LATTICE,STARTANGLE)
0362 % Determine 2-d geometry of the LATTICE
0363     NumElements = length(LATTICE);
0364     x2d = zeros(1,NumElements+1);
0365     y2d = zeros(1,NumElements+1);
0366     a2d = zeros(1,NumElements+1); % angle of orbit in radians
0367     a2d(1) = STARTANGLE;
0368     for en = 1:NumElements-1
0369        if isfield(LATTICE{en},'BendingAngle') 
0370           ba = LATTICE{en}.BendingAngle(1); % bending angle in radians
0371           else
0372           ba = 0;
0373           end
0374         
0375        if ba == 0
0376            Lt = LATTICE{en}.Length;
0377              Lp = 0;
0378        else
0379              Lt = LATTICE{en}.Length*sin(ba)/ba;
0380              Lp = -LATTICE{en}.Length*(1-cos(ba))/ba;
0381        end
0382     
0383        x2d(en+1) = x2d(en) + Lt*cos(a2d(en)) - Lp*sin(a2d(en));
0384           y2d(en+1) = y2d(en) + Lt*sin(a2d(en)) + Lp*cos(a2d(en));
0385           a2d(en+1)=a2d(en) - ba;
0386         
0387     end
0388     x2d(NumElements+1) = x2d(1);
0389     y2d(NumElements+1) = y2d(1);
0390     a2d(NumElements+1) = a2d(1);
0391 
0392     X0 = (max(x2d)+min(x2d))/2;
0393     Y0 = (max(y2d)+min(y2d))/2;    
0394     x2d = x2d - X0;
0395     y2d = y2d - Y0;

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