1 | function 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 | |
---|
26 | nspec_opt = 0; |
---|
27 | exportfile = 0; |
---|
28 | transferline = 0; |
---|
29 | tdin = []; |
---|
30 | for 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 |
---|
45 | end |
---|
46 | |
---|
47 | args = 0; |
---|
48 | args = args + 1; |
---|
49 | if nargin >= args && ~ischar(varargin{args}) && ~isstruct(varargin{args}) |
---|
50 | line_ring = varargin{args}; |
---|
51 | else |
---|
52 | global THERING |
---|
53 | line_ring = THERING; |
---|
54 | end |
---|
55 | |
---|
56 | args = args + 1; |
---|
57 | if nargin >= args && ~ischar(varargin{args}) && ~isstruct(varargin{args}) |
---|
58 | dp = varargin{args}; |
---|
59 | else |
---|
60 | dp = 0; |
---|
61 | end |
---|
62 | |
---|
63 | args = args + 1; |
---|
64 | if nargin >= args && ~ischar(varargin{args}) && ~isstruct(varargin{args}) |
---|
65 | elements = varargin{args}; |
---|
66 | else |
---|
67 | elements = 1:length(line_ring)+1; |
---|
68 | end |
---|
69 | |
---|
70 | % Check that input struct supplied of 'line' is used |
---|
71 | if 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); |
---|
76 | else |
---|
77 | TD = twissring(line_ring, dp, elements, 'chrom', 1e-6); |
---|
78 | end |
---|
79 | |
---|
80 | % Group element names into a cell array |
---|
81 | for 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 |
---|
90 | end |
---|
91 | optics.elemnames = elemnames; |
---|
92 | optics.elemLeff = elemLeff; |
---|
93 | |
---|
94 | temp = cat(1, TD.beta); |
---|
95 | optics.betax = temp(:,1); |
---|
96 | optics.betay = temp(:,2); |
---|
97 | |
---|
98 | temp = cat(1, TD.alpha); |
---|
99 | optics.alphax = temp(:,1); |
---|
100 | optics.alphay = temp(:,2); |
---|
101 | |
---|
102 | temp = cat(2, TD.Dispersion); |
---|
103 | optics.etax = temp(1,:)'; |
---|
104 | optics.etapx = temp(2,:)'; |
---|
105 | optics.etay = temp(3,:)'; |
---|
106 | optics.etapy = temp(4,:)'; |
---|
107 | |
---|
108 | temp = cat(2, TD.ClosedOrbit); |
---|
109 | optics.x = temp(1,:)'; |
---|
110 | optics.px = temp(2,:)'; |
---|
111 | optics.y = temp(3,:)'; |
---|
112 | optics.py = temp(4,:)'; |
---|
113 | |
---|
114 | temp = cat(1,TD.mu); |
---|
115 | optics.nux = temp(:,1)/(2*pi); |
---|
116 | optics.nuy = temp(:,2)/(2*pi); |
---|
117 | |
---|
118 | optics.spos = cat(1,TD.SPos); |
---|
119 | |
---|
120 | if nspec_opt >= 1 |
---|
121 | varargout{1} = optics.(spec_opt{1}); |
---|
122 | else |
---|
123 | varargout{1} = optics; |
---|
124 | end |
---|
125 | |
---|
126 | if 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'); |
---|
135 | end |
---|