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

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

Initial import--MML version from SOLEIL@2013

File size: 3.8 KB
Line 
1function 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
19THERING = {};
20index = [];
21filename = '';
22element = 1;
23input = 0;
24for 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
41end
42
43
44% need to specify thering to use
45if isempty(THERING)
46    global THERING
47    %error('Please specity THERING to use');
48end
49
50% if index is not specified by the user then print all the elements
51if isempty(index)
52    index = 1:length(THERING);
53end
54
55if isempty(filename)
56    % standard output
57    fid = 1;
58else
59    % open file and write to it
60    fid = fopen(filename,'w');
61end
62
63
64if 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;
77end
78
79% Information header
80fprintf(fid,'=== Element Definitions ===\n\n');
81fprintf(fid,'DRIFT      LENGTH\n');
82fprintf(fid,'MARKER     LENGTH\n');
83fprintf(fid,'QUAD       LENGTH  K\n');
84fprintf(fid,'MULTIPOLE  LENGTH  NORMAL_POLY\n');
85fprintf(fid,'BEND       LENGTH  ANGLE  ENTRANCE  EXIT  NORMAL_POLY\n');
86fprintf(fid,'\n\n');
87
88
89for 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);
110end
111
112if 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);
123end
124
125if fid ~= 1
126    fclose(fid);
127end
Note: See TracBrowser for help on using the repository browser.