source: MML/trunk/at/atdemos/latticedemo.m

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

Initial import--MML version from SOLEIL@2013

File size: 3.9 KB
Line 
1%LATTICEDEMO self-running tutorial
2% demonstrates
3% 1. ELEMENT, family of ELEMENTS, sequence of ELEMENTS
4% 2. Lattice representation
5% 3. Creating a lattice
6% 4. Creating and editing lattice files
7clear all
8clc
9echo on
10% An element in Accelerator Toolbox is a 1-by-1 MATLAB STRUCTURE
11% The folowing code creates a structure D1 for a drift space
12% and a structure QF for a quadrupole.
13
14D1.FamName = 'DR01';
15D1.Length  = 3;
16D1.PassMethod = 'DriftPass';
17
18QF.FamName = 'QF';
19QF.Length = 1;
20QF.K = 0.2;
21QF.MaxOrder = 3;
22QF.NumIntSteps = 1;
23QF.PolynomA= [0 0 0];
24QF.PolynomB= [0 0.2 0];
25QF.R1= eye(6);
26QF.R2= eye(6);
27QF.T1= [0 0 0 0 0 0];
28QF.T2= [0 0 0 0 0 0];
29QF.PassMethod= 'QuadLinearPass';
30
31pause % Press any key to continue
32clc
33% Use WHOS, DISP or just type variable's name without closing semicolon
34% to print the element's info:
35
36whos D1 QF                   
37disp(D1)                     
38QF                           
39
40pause % Press any key to continue
41clc
42% The next few lines will create another drift structure D2 from the exiting D1
43% and modify the values of fields 'FamName' and 'Length'
44
45D2 = D1;
46
47D2.FamName = 'DR02';
48D2.Length = 2;
49
50disp(D2)
51pause % Press any key to continue
52clc
53% Create another quadrupole element structure QD from QF and modify
54% the values of fields 'K' and 'PolynomB' to make it defocusing
55QD = QF;
56QD.FamName = 'QD';
57QD.K = -0.4;
58% Field 'PolynomB is a vector with polynomial field expansion coefficients.
59% The second element (quadrupole coefficient) must be consistent with field 'K'
60QD.PolynomB(2) = QD.K;
61
62disp(QD)
63pause % Press any key to continue
64clc
65% We have declared four elements:
66whos
67
68% They are now independent from each other
69
70% We are ultimately interested in sequences of elements
71% to model storage ring lattices or single-pass beam transport lines.
72% The next section will illustrate building of such sequences
73
74
75
76pause % Press any key to continue
77clc
78% Accelerator Toolbox represents sequences of elements as MATLAB cell arrays
79% where individual cells are 1-by-1 structures containing element data
80% The following commad creates a simple FODO cell by copying previously
81% created element structures for drifts and quadrupole magnets to a cell array FODOCELL:
82
83FODOCELL = {QF D1 QD D2 QF};
84
85whos FODOCELL
86% LENGTH is useful to find the number of elements in a sequence
87
88L = length(FODOCELL)
89pause % Press any key to continue;
90clc
91% Use {:} cell array syntax to print some or all elements
92FODOCELL{1}
93pause % FODOCELL{:} will print a long list of all elements. Press any key to continue
94clc
95FODOCELL{:}
96pause % Press any key to continue;
97clc
98% Let's build a cell array THERING that represents a closed ring
99% with 10 periods of FODOCELL the same way we would build
100% any other array in MATLAB from the comman dline
101
102THERING = [FODOCELL FODOCELL FODOCELL FODOCELL FODOCELL...
103           FODOCELL FODOCELL FODOCELL FODOCELL FODOCELL];
104       
105whos THERING
106pause % Press any key to continue;
107clc
108% The first element in THERING is
109THERING{1}
110
111% To inspect or change the value of a specific field we can use MATLAB syntax
112% for accessing cells in cell arrays and field in structures
113oldK = THERING{1}.K
114
115THERING{1}.K = 0.25;
116
117newK = THERING{1}.K
118
119pause % Press any key to continue;
120clc
121% Lattice THERING is a variable in MATLAB workspace.
122% We can use it in accelerator physics functions and scripts
123%
124% For example: function FindM44 finds 4-by-4 transverse transfer matrix
125M = findm44(THERING,0)
126pause % Press any key to continue;
127clc
128% -----------------------------------------------------------------------
129% SUMMARY
130% 1. Individual elements are represented by 1-by-1  MATLAB structures
131
132% 2. Element sequences (lattices) are represented by 1-dimensional
133%    MATLAB cell arrays of stuctures
134
135% 3. MATLAB syntax for hanling structures and cell arrays applies.
136%    No special language is required to define a lattice.
137
138% --------------------------------------------------------------------------
139
140pause % Press any key to continue;
141echo off
142clc
Note: See TracBrowser for help on using the repository browser.