source: MML/trunk/mml/at/machine_at.m @ 4

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

Initial import--MML version from SOLEIL@2013

File size: 3.7 KB
Line 
1function varargout = machine_at(varargin)
2%MACHINE_AT - Returns the optics function as a structure
3%  optics = machine_at([THERING, DP, ELEMENTS, SPECIFIC_OPTICS])
4%  optics = machine_at(SPECIFIC_OPTICS)
5%
6%  Essentially takes what twissring returns and restructures it so that its easier to use.
7%
8%  Defaults to loading THERING from global, DP = 0, uses all elements and
9%  calculates the dispersion.
10%  SPECIFIC_OPTICS is a string that defines which particular element to
11%  return. Eg. if SPECIFIC_OPTICS = 'betax', then MACHINE_AT will only
12%  return those numbers.
13%
14%  Other options are 'file' which will allow one to export the optical
15%  functions to an xls spreadsheet and 'line' to specify if the THERING used
16%  is actually a transfer line. If using the transfer line then the user
17%  must also provide the initial conditions as specified by TWISSLINE.
18%
19%  Written by Eugene Tan (28/05/04)
20%
21%  Change History
22%  1. Added export of element names as well 14/11/06
23%  2. Added export to xls option. 12/05/05
24
25
26nspec_opt = 0;
27exportfile = 0;
28transferline = 0;
29tdin = [];
30for i=1:nargin
31    if ischar(varargin{i}) && strcmpi(varargin{i},'file')
32        exportfile = 1;
33    elseif ischar(varargin{i}) && strcmpi(varargin{i},'line')
34        transferline = 1;
35    elseif isstruct(varargin{i})
36        % Of all inputs there is only one struct type and that is for
37        % twissline.
38        tdin = varargin{i};
39    else
40        if ischar(varargin{i})
41            nspec_opt = nspec_opt + 1;
42            spec_opt{nspec_opt} = varargin{i};
43        end
44    end
45end
46       
47args = 0;
48args = args + 1;
49if nargin >= args && ~ischar(varargin{args}) && ~isstruct(varargin{args})
50    line_ring = varargin{args};
51else
52    global THERING
53    line_ring = THERING;
54end
55
56args = args + 1;
57if nargin >= args && ~ischar(varargin{args}) && ~isstruct(varargin{args})
58    dp = varargin{args};
59else
60    dp = 0;
61end
62
63args = args + 1;
64if nargin >= args && ~ischar(varargin{args}) && ~isstruct(varargin{args})
65    elements = varargin{args};
66else
67    elements = 1:length(line_ring)+1;
68end
69
70% Check that input struct supplied of 'line' is used
71if transferline
72    if isempty(tdin)
73        error('User must provide the twiss data input structure');
74    end
75    TD = twissline(line_ring, dp, tdin, elements, 'chrom', 1e-6);
76else
77    TD = twissring(line_ring, dp, elements, 'chrom', 1e-6);
78end
79
80% Group element names into a cell array
81for i=1:length(elements)
82    % Circular indexing
83    iind = mod(elements(i)-1,length(line_ring))+1;
84    elemnames{i,1} = line_ring{iind}.FamName;
85    if isfield(line_ring{iind},'Length')
86        elemLeff(i,1) = line_ring{iind}.Length;
87    else
88        elemLeff(i,1) = 0;
89    end
90end
91optics.elemnames = elemnames;
92optics.elemLeff = elemLeff;
93
94temp = cat(1, TD.beta);
95optics.betax = temp(:,1);
96optics.betay = temp(:,2);
97
98temp = cat(1, TD.alpha);
99optics.alphax = temp(:,1);
100optics.alphay = temp(:,2);
101
102temp = cat(2, TD.Dispersion);
103optics.etax = temp(1,:)';
104optics.etapx = temp(2,:)';
105optics.etay = temp(3,:)';
106optics.etapy = temp(4,:)';
107
108temp = cat(2, TD.ClosedOrbit);
109optics.x = temp(1,:)';
110optics.px = temp(2,:)';
111optics.y = temp(3,:)';
112optics.py = temp(4,:)';
113
114temp = cat(1,TD.mu);
115optics.nux = temp(:,1)/(2*pi);
116optics.nuy = temp(:,2)/(2*pi);
117
118optics.spos = cat(1,TD.SPos);
119
120if nspec_opt >= 1
121    varargout{1} = optics.(spec_opt{1});
122else
123    varargout{1} = optics;
124end
125
126if exportfile
127    [filename pathname] = uiputfile('*.xls','Excel spreadsheet');
128    entrystr = fieldnames(optics);
129    temp = optics.(entrystr{1});
130    for i=2:length(entrystr)
131        temp = cat(2,temp,optics.(entrystr{i}));
132    end
133    xlswrite([pathname filename],entrystr','opticalparam','A1');
134    xlswrite([pathname filename],temp,'opticalparam','A2');
135end
Note: See TracBrowser for help on using the repository browser.