Hands-on on Lattice Calculation in Python

A. Latina, A. Poyet, G. Sterbini, V. Ziemann

Introduction to Accelerator Physics, 25 September 8 October 2021, Chavannes de Bogis, Switzerland)



The "Hands-on on Lattice Calculations in Python" aims to help you visualize the concepts of the "Transverse Linear Beam Dynamics" and of "Computational Tools".

In this document we will present the suggested directions to solve the exercises of the "Transverse Linear Beam Dynamics Primer using Python" and will serve as a guidance for the student. Most of the time the result can be achieved using multiple paths and we encourage the student to explore them.

In addition the goal is to stimulate the student's curiosity, so the simple exercise should be challenged to trigger more questions and to dive deeper into the physics of the problem.


You received the instructions on how to prepare your python working environment.

QUESTION: is everyone able to lauch a jupyter lab server from her/his laptop?


From Wolfgang's lecture, you learnt about matrices and particle trajectories.


Let's start with the exercises


Exercise 1

Show that multiplying two such matrices, one with $L_1$ and the other with $L_2$ in the upper right corner, produces a matrix with the sum of the distances in the upper right corner.

SOLUTION

Even we can prove it by hand, we take the opportunity to show some symbolic computation in Python.


Exercise 2

How do you describe a ray that is parallel to the optical axis?

SOLUTION

The phase space vector of a particle parallel ot the optical axis can be represented (1D) by \begin{equation} X=\left( \begin{array}{c} x \\ 0 \end{array} \right), \end{equation} this means that is $x'$ is vanishing.


Exercise 3

How do you describe a ray that is on the optical axis?

SOLUTION

The phase space vector of a particle sitting on the optical axis can be represented by \begin{equation} X=\left( \begin{array}{c} 0 \\ x' \end{array} \right). \end{equation} this means that $x$ is vanishing.


Exercise 4

Show by multiplying the respective matrices that a parallel ray, which first passes through a lens with focal length $f$ and then moves on a straight line, actually crosses the optical axis at a distance $L=f$ downstream of the lens. Hint: think a little extra about ordering of the matrices.

SOLUTION

We can still use the symbolic package of python (or do it by hand).

and this is on the beam axis (see Exercise 3), whatever is the initial offset, x. This is the meaning of the focal length.


Exercise 5

Set F=3 and numerically verify what you found in Exercise 4, namely that parallel rays cross the axis after a distance L=f.

SOLUTION

We are going to define some important functions, very useful for the rest of the exercises.

Indeed we found back the expected results.


Exercise 6

Recall that the imaging equation for a lens is $1/b+1/g=1/f,$ which corresponds to a system of one focusing lens with focal length $f,$ sandwiched between drift spaces with length $b=3$ m and $g=2$ m. Write a beamline description that corresponds to this system. We will later return to it and analyze it.

SOLUTION


Exercise 7

Define a FODO beamline and prepare initial coordinates that describe a particle that is on the optical axis, but has an initial angle $x'$ and plot the position $x$ along the beam line.

SOLUTION

The first part of the exercise is to define the FODO cell starting. A FODO cell is one of the most fundamental lattice topologies. In its simpler version, it consists on two thin quadrupoles of opposite focal length, f, interspaced by two drifts of length L/2 (L is the total length of the cell). We can start the FODO from an arbitrary point inside it. We decided to start describing it from the center of drift. For the moment we can define arbitrarly the f and L_2 paramenters: we chose 2.5 m and 1 m, respectively.

Please pay attention that in order to sample more finely the particle trajectory in the drift we split them in pieces of 0.1 m. We will see in the following cell how, starting from the initial position of the particle, we can transport it along the beam line.

The proper tracking will append to the initial beamlist the transformed coordinate in the trace-space and the incremented s-coordinate.


Exercise 8

Plot the angle $x'$ along the beam line.

SOLUTION


Exercise 9

Plot both the position $x$ and the angle $x'$ through five cells.

SOLUTION


Exercise 10

Plot the position $x$ through 100 cells, play with different values of the focal length $F$ and explore whether you can make the oscillations grow.

It it important to observe that when the focal length approaches the stability limit (only for $f>L/4$ the machine is stable), then the oscillation start to grow during the transport in the lattice.


Exercise 11

Use the beam line for the imaging system you prepared in Exercise 6 and launch a particle with $x_0=0$ and an angle of $x'_0=1\,$mrad at one end. Verify that this particle crosses the center of the beam pipe at the exit of the beam line, provided that $b,g,$ and $f$ satisfy the imaging equation that is shown in Exercise 6.

We can do a bit animation showing that, moving the position of the quadruple and keeping fulfilled the imaging equation the particle entering with x=0 will exit with x=0. The animation output is omittend in the pdf version of this document.


Exercise 12

Define an ensemble of 1000 particles with an arbitrary first order ($x0$, $xpo$) and second order momenta( $x_{rms}$ and $x'_{rms}$). Verify the angular divergence of the beam is the one set.

SOLUTION

We did not find exactly back the 0.5 value due to the statistical error coming with the finite numbers of particles. We can explore the dependence of the statistical error with on the beam divergence by a numerical simulation.

Depending on the required precision we need to select the convenient number of particles.

Exercise 13

Transport the beam distribution of Exercise 12 in a drift of length 1 m. Compare the initial and final distribution.

Test of linearity. Scale the input vector by 17 times the month of your birthday (85 if you are born in May) and verify that the output vector from the matrix multiplication has changed by the same factor.

Now launch 3 particles such that they define a triangle of surface A. Verify that this linear transport preserve the area of the triangle.

Area preservation

Now let us consider three points of the distributions and let us verify that the area of the triangle before and after the transoformation is preserved. IMPORTANT: the area preservation is a condition stronger than the linearity. It is possible only for special matrices, the so-called symplectic matrices. A $2\times2$ matrix is symplectic if and only if its determinant is one.

Exercise 14

Using the 5 FODO cells of Exercise 9, trasport the beam of Exercise 13 and plot its rms size and divergence along the line.

SOLUTION

Exercise 15

Starting from Exercise 14, what happens if you (a) increase or (b) reduce by a factor 2 the initial beam size and divergence (sigxp)?

SOLUTIONS

It very important to note that the divergence of CASE A and CASE B are not proportional. So as for the single particle position and angle along the machine is proportional (linearity) to the initial conditions, the statistical quantities describing the beam (beam size and beam divergence) only if you ar varyin the both beam size and divergence by the same factor.


Exercise 16

Using Equation 7, display (a) the average position of the particles along the beam line. Likewise, (b) display the angular divergence. Compare with the result you found in Exercise 14.

SOLUTIONS

It is important to observe that the result of from the particle tracking and the sigma matrix tracking are equivalent. The small deviation is due to the statistical noise of the ensemble (it can be reduced by increasing the numebr of particles). Clearly the sigma matrix tracking is computationally cheaper than the particle tracking (if Npart>>1).


Exercise 17

Can you find an initial beam matrix sigma0 that reproduces itself at the end of the beam line? Hint: proceed by try and error.

SOLUTION

This problem is not so simple. One could proceed with try and error. But there are three parameters to fix so it can be quite cumbersome.


Exercise 18

Explore different initial coordinate and compare the phase-space plots you obtain.

SOLUTIONS

For plotting the trace space evolution of a particle we need to observe it turn-after-turn. The natural way to do that is to compress the beam line (in general composed by more than one element) in a single transformation: the one-turn-matrix. Do to so we introduce a special method.


Exercise 19

Plot the position of the particle vs the number of turns. What do you observe?

SOLUTION

We clearly see a betatronic oscillation. And this is true also for x' (se below, and not the different amplitude and phase).


Exercise 20

In the definition of FODO of the previous exercise, reverse the polarity of both quadrupoles and prepare a phase-space plot. How does it differ from the one in Exercise 18?

SOLUTION

As you can see now the ellipse tilt is inverted.


Exercise 21

From Exercise 20, change the starting point of the lattice at the center of the focusing quadrupole Hint: split the focusing quadrupole in two focusing quadrupole with double focal length and place them at the start and at the end of the lattice.

SOLUTION

The ellipse is not tilted anymore.


Exercise 22

Find the range of focal lengths $f$ for which the FODO cells permit stable oscillations.

SOLUTION

To solve this exercise we need to compute the phase advance and initial optics functions associated to a OTM rotation. To do that we define the following function.

The stability condition is satisfied if (only 1D case), the trace of the matrix is smaller than 2 (for the H adn V case). For a FODO this can be done analytically. But in this exercise we will follow a numerical approach.


Exercise 23

Transport the periodic $\sigma$ matrix along the FODO and convince yourself that the $\sigma$ matrix at the end of the FODO is indeed equal to the one at the start, $\sigma_0$.

SOLUTION

One has to remeber the definition of the $\sigma$ matrix (for the momement we will consired $\epsilon=1$) and how to transport it.


Exercise 24

Write down the numerical values of initial beam matrix $\sigma_0$, then build a beam line made of 15 consecutive cells by changing the definition of the lattice and then, using $\sigma_0$ with the noted-down numbers, prepare a plot of the beam sizes along the $15$ cells. Is it also periodic?

SOLUTION


Exercise 25

Verify that the OTM has determinant equal to 1.

SOLUTION

Exercise 26

Multiply $\sigma_0$ from Exercise 24 by 17 and calculate the emittance. Then propagate the $\sigma$ matrix through the beam line from Exercise 24 and verify that the emittance of the sigma matrix after every element is indeed constant and equal to its initial value.


Exercise 27

Vary $f$ by hand and try to (a) find a value that returns Q= 1/6. (b) Then try to find a value of $f$ that produces a 90 deg phase-advance. What is the corresponding value of Q?

SOLUTION


Exercise 28

A common task when designing accelerators is matching one section of a beam line to another one. Here we will assume that the upstream beam line consists of FODO cells with a $60^o$ phase advance and the downstream beam line of FODO cells with a $90^o$ phase advance. These are the cells with the focal length we calculated in Exercise~27. In between the $60^o$ and $90^o,$ we place a third cell with two quadrupoles that we will use to match the upstream to the downstream beam line. To do so, we need to prepare periodic beam matrices sigma60 and sigma90 for the respective sections. Note that sigma90 only depends on two parameters: the Twiss parameters $\alpha$ and $\beta,$ and therefore we also need two quadrupoles with independently variable focal length to adjust until the final beam matrix equals sigma90.

Implement the procedure described in the previous paragraph and match of a FODO lattice with $\mu$=60 deg and $\mu$=90 deg.

SOLUTION


Exercise 29

Introduce as a new element the thick quadrupoles matrices. Hint: write an external function that returns the corresponding list.

SOLUTION


Exercise 30

Use the beam line from Exercise 27 (60 degrees/cell FODO) and replace the thin quadrupoles by long quadrupoles with a length of 0.2, 0.4, 1.0 m. Make sure the overall length and the phase advance of the FODO cell remains unchanged. By how much does the periodic beta function at the start of the cell change? Express the change in percent.

SOLUTION


Exercise 31

Implement in a Python function the element corresponding to the weak focusing of a sector bend.

SOLUTION


Exercise 32

Insert 1 m long dipoles in the center of the drift spaces of the FODO cells from Exercise 27 while keeping the length of the cell constant. Investigate deflection angles of φ= 5, 10 and 20 degrees. Check by how much the periodic beta functions change. Why do they change? Can you compensate the phase advance $\mu$ by adjusting the strength or focal lengths of the quadrupoles?

SOLUTION


Exercise 33

Upgrade the software to consistently handle $3\times3$ matrices for drift space, quadrupoles, and sector dipoles.

SOLUTION


Exercise 34

Build a beam line of six FODO cells with a phase advance of 60 degrees/cell (thin quadrupoles are OK to use) and add a sector bending magnet with length 1 m and bending angle $\phi=10$ degrees in the center of each drift. You may have to play with the quadrupole values to make the phase advance close to 60 degrees. But you probably already did this in Exercise 32.

SOLUTION


Exercise 35

Use the starting conditions $(x_0,x_0',\delta)=(0,0,0)$ and plot the position along the beam line. Repeat this for $\delta=10^{-3}$ and for $\delta=3\times 10^{-3}$. Plot all three traces in the same graph. Discuss what you observe and explain.

SOLUTION


Exercise 36

Work out the transverse components of the periodic beam matrix $\sigma_0$. Assume that the geometrical emittance is $\epsilon_0=10^{-6}$ m rad. Furthermore, assume that the momentum spread $\sigma_0(3,3)=\sigma_p^2$ is zero and plot the beam size along the beam line.

SOLUTION


Exercise 37

Plot the beam size for for $\sigma_p^2=10^{-3}$ and for $\sigma_p^2=3\times 10^{-3}$. What happens if you change the phase advance of the cell? Try out by slightly changing the focal lengths.

SOLUTION

Exercise 38

Determine the periodic dispersion at the start of the cell. Then plot the dispersion in the cell.

SOLUTION

For that we need to solve the problem


Exercise 39

Convert the code to use $4\times4$ matrices, where the third and fourth columns are associated with the vertical plane.

SOLUTION


Exercise 40

Start from a single FODO cell with 60 degrees/cell you used earlier. Insert sector bending magnets with a bending angle of $\phi=10\,$degrees in the center of the drift spaces. The bending magnets will spoil the phase advance in one plane. Now you have two phase advances and need to adjust both quadrupoles (by hand to 2 significant figures) such that it really is 60 degrees in both planes.

SOLUTION


Exercise 41

Use the result from exercise 40 and adjust the two quadrupoles such that the phase advance in the horizontal plane is 90 degrees, cell, while it remains 60 degrees/cell in the vertical plane.

SOLUTION


Exercise 42

Prepare a beam line with eight FODO cells without bending magnets and with 60 degrees/cell phase advance in both planes. (a) Prepare the periodic beam matrix sigma0 (4x4, uncoupled) as the initial beam and plot both beam sizes along the beam line. (b) Use sigma0 as the starting beam, but change the focal length of the second quadrupole by 10\% and plot the beam sizes once again. Discuss you observations.

SOLUTION


Exercise 43

From the lecture about betatron coupling identify the transfer matrix for a solenoid and write a function that receives the longitudinal magnetic field $B_s$ and the length of the solenoid as input and returns the transfer matrix. Then extend the simulation code to handle solenoids. Finally, define a beam line where you place the solenoid in the middle of a FODO cell and follow a particle with initial condition $(x_0,x'_0,y_0,y'_0)=(10^{-3}\,\mathrm{m},0,0,0).$ What do you observe? Is the motion confined to the horizontal plane?

SOLUTION

One has to remember that the matrix is \begin{equation} M_{\text {SOLENOID}}=\left(\begin{array}{cccc}{C^{2}} & {\frac{1}{K} S C} & {S C} & {\frac{1}{K} S^{2}}\\ {-K S C} & {C^{2}} & {-K S^{2}} & {S C}\\ {-S C} & {-\frac{1}{K} S^{2}} & {C^{2}} & {\frac{1}{K} S C}\\ {K S^{2}} & {-S C} & {-K S C} & {C^{2}} \end{array}\right) \end{equation} where \begin{equation} K=\operatorname{sgn}(\mathrm{q}) \frac{B_{s}}{2(B \rho)} \end{equation} and \begin{equation} \begin{array}{l}{C=\cos K L} \\ {S=\sin K L}\end{array} \end{equation}

One can clearly see that the motion is coupled.