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 |
---|
7 | clear all |
---|
8 | clc |
---|
9 | echo 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 | |
---|
14 | D1.FamName = 'DR01'; |
---|
15 | D1.Length = 3; |
---|
16 | D1.PassMethod = 'DriftPass'; |
---|
17 | |
---|
18 | QF.FamName = 'QF'; |
---|
19 | QF.Length = 1; |
---|
20 | QF.K = 0.2; |
---|
21 | QF.MaxOrder = 3; |
---|
22 | QF.NumIntSteps = 1; |
---|
23 | QF.PolynomA= [0 0 0]; |
---|
24 | QF.PolynomB= [0 0.2 0]; |
---|
25 | QF.R1= eye(6); |
---|
26 | QF.R2= eye(6); |
---|
27 | QF.T1= [0 0 0 0 0 0]; |
---|
28 | QF.T2= [0 0 0 0 0 0]; |
---|
29 | QF.PassMethod= 'QuadLinearPass'; |
---|
30 | |
---|
31 | pause % Press any key to continue |
---|
32 | clc |
---|
33 | % Use WHOS, DISP or just type variable's name without closing semicolon |
---|
34 | % to print the element's info: |
---|
35 | |
---|
36 | whos D1 QF |
---|
37 | disp(D1) |
---|
38 | QF |
---|
39 | |
---|
40 | pause % Press any key to continue |
---|
41 | clc |
---|
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 | |
---|
45 | D2 = D1; |
---|
46 | |
---|
47 | D2.FamName = 'DR02'; |
---|
48 | D2.Length = 2; |
---|
49 | |
---|
50 | disp(D2) |
---|
51 | pause % Press any key to continue |
---|
52 | clc |
---|
53 | % Create another quadrupole element structure QD from QF and modify |
---|
54 | % the values of fields 'K' and 'PolynomB' to make it defocusing |
---|
55 | QD = QF; |
---|
56 | QD.FamName = 'QD'; |
---|
57 | QD.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' |
---|
60 | QD.PolynomB(2) = QD.K; |
---|
61 | |
---|
62 | disp(QD) |
---|
63 | pause % Press any key to continue |
---|
64 | clc |
---|
65 | % We have declared four elements: |
---|
66 | whos |
---|
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 | |
---|
76 | pause % Press any key to continue |
---|
77 | clc |
---|
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 | |
---|
83 | FODOCELL = {QF D1 QD D2 QF}; |
---|
84 | |
---|
85 | whos FODOCELL |
---|
86 | % LENGTH is useful to find the number of elements in a sequence |
---|
87 | |
---|
88 | L = length(FODOCELL) |
---|
89 | pause % Press any key to continue; |
---|
90 | clc |
---|
91 | % Use {:} cell array syntax to print some or all elements |
---|
92 | FODOCELL{1} |
---|
93 | pause % FODOCELL{:} will print a long list of all elements. Press any key to continue |
---|
94 | clc |
---|
95 | FODOCELL{:} |
---|
96 | pause % Press any key to continue; |
---|
97 | clc |
---|
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 | |
---|
102 | THERING = [FODOCELL FODOCELL FODOCELL FODOCELL FODOCELL... |
---|
103 | FODOCELL FODOCELL FODOCELL FODOCELL FODOCELL]; |
---|
104 | |
---|
105 | whos THERING |
---|
106 | pause % Press any key to continue; |
---|
107 | clc |
---|
108 | % The first element in THERING is |
---|
109 | THERING{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 |
---|
113 | oldK = THERING{1}.K |
---|
114 | |
---|
115 | THERING{1}.K = 0.25; |
---|
116 | |
---|
117 | newK = THERING{1}.K |
---|
118 | |
---|
119 | pause % Press any key to continue; |
---|
120 | clc |
---|
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 |
---|
125 | M = findm44(THERING,0) |
---|
126 | pause % Press any key to continue; |
---|
127 | clc |
---|
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 | |
---|
140 | pause % Press any key to continue; |
---|
141 | echo off |
---|
142 | clc |
---|