LATTICEDEMO self-running tutorial

Table of Contents
clear

Create elements

An element in Accelerator Toolbox is a 1-by-1 MATLAB STRUCTURE.
Functions are provided to easily create such structures with adequate fields.
The following code creates a structure D1 for a drift space and a structure QF for a quadrupole.
D1 = atdrift('DR01', 3.0);
QF = atquadrupole('QF', 1.0, 0.2);
Use whos, disp or just type variable's name without closing semicolon to print the element's info:
whos D1 QF
Name Size Bytes Class Attributes D1 1x1 716 struct QF 1x1 1646 struct
disp(D1);
FamName: 'DR01' PassMethod: 'DriftPass' Length: 3 Class: 'Drift'
QF
QF = struct with fields:
FamName: 'QF' PassMethod: 'StrMPoleSymplectic4Pass' Length: 1 Class: 'Quadrupole' K: 0.2000 PolynomB: [0 0.2000] PolynomA: [0 0] MaxOrder: 1 NumIntSteps: 10
The next few lines will create another drift structure D2 from the exiting D1 and modify the values of fields 'FamName' and 'Length'
D2 = D1;
D2.FamName = 'DR02';
D2.Length = 2;
Create another quadrupole element structure QD from QF and modify the values of fields 'K' and 'PolynomB' to make it defocusing
QD = QF;
QD.FamName = 'QD';
QD.K = -0.4;
The field 'PolynomB' is a vector of coefficients of the polynomial field expansion.
The second element (quadrupole coefficient) must be consistent with field 'K'
QD.PolynomB(2) = QD.K;
disp(QD);
FamName: 'QD' PassMethod: 'StrMPoleSymplectic4Pass' Length: 1 Class: 'Quadrupole' K: -0.4000 PolynomB: [0 -0.4000] PolynomA: [0 0] MaxOrder: 1 NumIntSteps: 10
We have created four elements:
whos
Name Size Bytes Class Attributes D1 1x1 716 struct D2 1x1 716 struct QD 1x1 1646 struct QF 1x1 1646 struct

Build lattices

We are ultimately interested in sequences of elements to model storage ring lattices or single-pass beam transport lines.
The next section illustrates building such sequences. Accelerator Toolbox represents sequences of elements as MATLAB cell arrays where individual cells are 1-by-1 structures describing elements.
The following command creates a simple FODO cell by copying previously created element structures into a cell array fodocell:
fodocell = {QF D1 QD D2 QF};
whos fodocell
Name Size Bytes Class Attributes fodocell 1x5 6890 cell
length is useful to find the number of elements in a sequence:
L = length(fodocell)
L = 5
Use the {:} cell array syntax to print some or all elements:
fodocell{1}
ans = struct with fields:
FamName: 'QF' PassMethod: 'StrMPoleSymplectic4Pass' Length: 1 Class: 'Quadrupole' K: 0.2000 PolynomB: [0 0.2000] PolynomA: [0 0] MaxOrder: 1 NumIntSteps: 10
fodocell{:}
ans = struct with fields:
FamName: 'QF' PassMethod: 'StrMPoleSymplectic4Pass' Length: 1 Class: 'Quadrupole' K: 0.2000 PolynomB: [0 0.2000] PolynomA: [0 0] MaxOrder: 1 NumIntSteps: 10
ans = struct with fields:
FamName: 'DR01' PassMethod: 'DriftPass' Length: 3 Class: 'Drift'
ans = struct with fields:
FamName: 'QD' PassMethod: 'StrMPoleSymplectic4Pass' Length: 1 Class: 'Quadrupole' K: -0.4000 PolynomB: [0 -0.4000] PolynomA: [0 0] MaxOrder: 1 NumIntSteps: 10
ans = struct with fields:
FamName: 'DR02' PassMethod: 'DriftPass' Length: 2 Class: 'Drift'
ans = struct with fields:
FamName: 'QF' PassMethod: 'StrMPoleSymplectic4Pass' Length: 1 Class: 'Quadrupole' K: 0.2000 PolynomB: [0 0.2000] PolynomA: [0 0] MaxOrder: 1 NumIntSteps: 10
Let's build a cell array fodoring that represents a closed ring with 10 periods of fodocell the same way we would build any other array in MATLAB from the command line:
fodoring = [fodocell fodocell fodocell fodocell fodocell...
fodocell fodocell fodocell fodocell fodocell];
whos fodoring
Name Size Bytes Class Attributes fodoring 1x50 68900 cell
The first element in fodoring is:
fodoring{1}
ans = struct with fields:
FamName: 'QF' PassMethod: 'StrMPoleSymplectic4Pass' Length: 1 Class: 'Quadrupole' K: 0.2000 PolynomB: [0 0.2000] PolynomA: [0 0] MaxOrder: 1 NumIntSteps: 10
To inspect or change the value of a specific field we can use MATLAB syntax for accessing cells in cell arrays and field in structures
oldK = fodoring{1}.K
oldK = 0.2000
fodoring{1}.K = 0.25;
newK = fodoring{1}.K
newK = 0.2500
The lattice fodoring is a variable in MATLAB workspace.
We can use it in accelerator physics functions and scripts.
For example: the function findm44 finds 4-by-4 transverse transfer matrix
M = findm44(fodoring,0)
M = 4×4
-0.6352 11.0305 0 0 -0.0763 -0.2497 0 0 0 0 -0.9706 0.3115 0 0 -0.0471 -1.0151

Summary

  1. Individual elements are represented by 1-by-1 MATLAB structures,
  2. Sequences of elements (lattices) are represented by 1-dimensional MATLAB cell arrays of structures,
  3. MATLAB syntax for handling structures and cell arrays applies.
No special language is required to define a lattice.