1 | function varargout = printlattice(varargin) |
---|
2 | %PRINTLATTICE - Simple printout of the elements of the model |
---|
3 | % printlattice(THERING,[format,index,'filename']) |
---|
4 | % |
---|
5 | % Reads THERING and give a simple printout of the elements to standard out |
---|
6 | % (screen). If INDEX is specified only those elements in the INDEX will be |
---|
7 | % printed out and if FILENAME is specified the output will also be printed |
---|
8 | % to file and not printed to standard out (screen). Nothing is returned by |
---|
9 | % PRINTLATTICE. |
---|
10 | % |
---|
11 | % FORMAT determines how the output should look. |
---|
12 | % 'element' - element by element simple output (default) |
---|
13 | % 'input' - list of unique elements followed by element position |
---|
14 | % |
---|
15 | % Written by Eugene Tan |
---|
16 | |
---|
17 | |
---|
18 | % parse headers |
---|
19 | THERING = {}; |
---|
20 | index = []; |
---|
21 | filename = ''; |
---|
22 | element = 1; |
---|
23 | input = 0; |
---|
24 | for i=nargin:-1:1 |
---|
25 | if iscell(varargin{i}) |
---|
26 | THERING = varargin{i}; |
---|
27 | elseif ischar(varargin{i}) |
---|
28 | switch varargin{i} |
---|
29 | case 'element' |
---|
30 | element = 1; |
---|
31 | case 'input' |
---|
32 | input = 1; |
---|
33 | otherwise |
---|
34 | filename = varargin{i}; |
---|
35 | end |
---|
36 | elseif isnumeric(varargin{i}) |
---|
37 | index = varargin{i}; |
---|
38 | else |
---|
39 | fprintf('Input parameter number %d ignored\n',i); |
---|
40 | end |
---|
41 | end |
---|
42 | |
---|
43 | |
---|
44 | % need to specify thering to use |
---|
45 | if isempty(THERING) |
---|
46 | global THERING |
---|
47 | %error('Please specity THERING to use'); |
---|
48 | end |
---|
49 | |
---|
50 | % if index is not specified by the user then print all the elements |
---|
51 | if isempty(index) |
---|
52 | index = 1:length(THERING); |
---|
53 | end |
---|
54 | |
---|
55 | if isempty(filename) |
---|
56 | % standard output |
---|
57 | fid = 1; |
---|
58 | else |
---|
59 | % open file and write to it |
---|
60 | fid = fopen(filename,'w'); |
---|
61 | end |
---|
62 | |
---|
63 | |
---|
64 | if input |
---|
65 | % cycle through index and determine unique elements |
---|
66 | famnames = {}; |
---|
67 | uniqueindex = []; |
---|
68 | elementline = {}; |
---|
69 | for i=index |
---|
70 | elementline{end+1} = THERING{i}.FamName; |
---|
71 | if isempty(strmatch(THERING{i}.FamName,strvcat(famnames))) |
---|
72 | famnames{end+1,1} = THERING{i}.FamName; |
---|
73 | uniqueindex(end+1) = i; |
---|
74 | end |
---|
75 | end |
---|
76 | index = uniqueindex; |
---|
77 | end |
---|
78 | |
---|
79 | % Information header |
---|
80 | fprintf(fid,'=== Element Definitions ===\n\n'); |
---|
81 | fprintf(fid,'DRIFT LENGTH\n'); |
---|
82 | fprintf(fid,'MARKER LENGTH\n'); |
---|
83 | fprintf(fid,'QUAD LENGTH K\n'); |
---|
84 | fprintf(fid,'MULTIPOLE LENGTH NORMAL_POLY\n'); |
---|
85 | fprintf(fid,'BEND LENGTH ANGLE ENTRANCE EXIT NORMAL_POLY\n'); |
---|
86 | fprintf(fid,'\n\n'); |
---|
87 | |
---|
88 | |
---|
89 | for i=index |
---|
90 | elstring = [sprintf('%10s ',THERING{i}.FamName) ' ']; |
---|
91 | switch THERING{i}.PassMethod |
---|
92 | case {'DriftPass'} |
---|
93 | elstring = [elstring sprintf('%10s %10.7f\n','DRIFT',THERING{i}.Length)]; |
---|
94 | case {'IdentityPass'} |
---|
95 | elstring = [elstring sprintf('%10s %10.7f\n','MARKER',THERING{i}.Length)]; |
---|
96 | case {'QuadLinearPass'} |
---|
97 | elstring = [elstring sprintf('%10s %10.7f %10.7f\n',... |
---|
98 | 'QUAD',THERING{i}.Length,THERING{i}.PolynomB(2))]; |
---|
99 | case {'StrMPoleSymplectic4Pass' 'StrMPoleSymplectic4RadPass'} |
---|
100 | elstring = [elstring sprintf('%10s %10.7f ','MULTIPOLE',THERING{i}.Length) ... |
---|
101 | sprintf('%10.7f ',THERING{i}.PolynomB) sprintf('\n')]; |
---|
102 | case {'BendLinearPass' 'BndMPoleSymplectic4Pass','BndMPoleSymplectic4RadPass'} |
---|
103 | elstring = [elstring sprintf('%10s %10.7f %10.7f %10.7f %10.7f ',... |
---|
104 | 'BEND',THERING{i}.Length,THERING{i}.BendingAngle,THERING{i}.EntranceAngle,THERING{i}.ExitAngle) ... |
---|
105 | sprintf('%10.7f ',THERING{i}.PolynomB) sprintf('\n')]; |
---|
106 | otherwise |
---|
107 | disp(['UNKNOWN ELEMENT' i]) |
---|
108 | end |
---|
109 | fprintf(fid,'%s',elstring); |
---|
110 | end |
---|
111 | |
---|
112 | if input |
---|
113 | fprintf(fid,'\n\n === Element Arrangement === \n\n'); |
---|
114 | elstring = ''; |
---|
115 | for i=1:length(elementline) |
---|
116 | elstring = [elstring sprintf('%10s ',elementline{i})]; |
---|
117 | if mod(i,5) == 0 |
---|
118 | fprintf(fid,'%s\n',elstring); |
---|
119 | elstring = ''; |
---|
120 | end |
---|
121 | end |
---|
122 | fprintf(fid,'%s\n',elstring); |
---|
123 | end |
---|
124 | |
---|
125 | if fid ~= 1 |
---|
126 | fclose(fid); |
---|
127 | end |
---|