A machine learning approach to shaping magnetic fringe fields for beam dynamics control

Fringe fields at the entrances and exits of multipole magnets can adversely affect the dynamics of particles in an accelerator, but there is also the possibility that fringe fields could be used to enhance accelerator performance. Design work could benefit from computational tools for constructing realistic models of multipole fringe fields at an early stage in the design process; and methods for relating the magnet geometry to the field shape and to the beam dynamics would also be of significant value. We explore novel techniques to produce magnet designs that satisfy specific requirements for the beam dynamics. Machine learning tools are used to link properties of the beam dynamics in the fringe fields to the magnet geometry in an efficient way.


Introduction
In the design process for an accelerator, detailed descriptions of multipole fringe fields are often not available until a late stage, following engineering design of the magnets.Any adverse effects on the beam dynamics then need to be compensated by remedial measures such as shimming, or by adjusting the strengths of other magnets.Fringe effects are of significant importance [1][2][3][4].In this paper, we present techniques currently under development for relating the geometry of the end face of a multipole magnet to the particle dynamics in the fringe field.Using machine learning tools, the particle dynamics can be rapidly estimated from the magnet geometry without the need for (often highly time-consuming) detailed magnet modelling.Machine learning also allows construction of a magnet geometry that will meet specific beam dynamics requirements.Although the techniques that we describe can provide a direct relation between the magnet geometry and the particle dynamics, they are also capable (as an optional intermediate step) of providing detailed information of the fringe field.

Analytical description of multipole fringe fields
To provide training data for neural networks for fringe field computations, we make use of analytical expressions for multipole fringe fields.This avoids the need for numerical magnet modelling, which would be impractical for the generation of large sets of training data.Using the results from [5], the variation of the multipole gradient in the fringe-field region of a multipole of order n (with n = 0 for a dipole, n = 1 for a quadrupole, etc.) can be described by an arbitrary function G n (ζ), where ζ = √ 2z with z the co-ordinate along the axis of the magnet in a Cartesian co-ordinate system.The fields can be expressed as superpositions of functions f n (ζ) and g n (ζ): The field in the body of a pure multipole of order n and unit strength is given by G n (ζ) = 1.The fringe field is described by a smooth variation G n (ζ) → 0 as ζ → ∞.Expressions can be written for the field components satisfying Maxwell's equations in the three-dimensional fringe field in terms of any function G n (ζ) with the appropriate limits.For the present studies, we are interested in relating the fringe field to the geometry of the magnet: a direct connection between the two is provided by the magnetic scalar potential φ, since for an electromagnet with a core of infinite permeability, the end face of the magnet forms an equipotential surface.
In general, the magnetic field B is obtained from the scalar potential by B = ∇φ.Given a function G n (ζ) describing the roll-off of the multipole gradient, the scalar potential is given by: where , and: The transverse behaviour of the fringe field is determined by G n (ζ) and by the n+1 coefficients b j .
In principle, we can now take any given function G n (ζ) (describing the fringe field in a multipole of order n), and, using the above formulae, construct the scalar potential φ.The geometry of the end-face of a magnet corresponding to the fringe field is then found from an equipotential surface φ = constant.However, in practice, constructing the geometry from a given G n (ζ) involves some complicated computation; and finding the fringe field from a given geometry (i.e.reversing the process) still requires detailed magnet modelling.To improve the efficiency of the process, we make use of machine learning tools.By constructing several thousand sets of training data consisting of a range of different functions G n (ζ) and the corresponding scalar potentials φ, we can train a neural network that will provide, almost immediately, the scalar potential for a given roll-off function, or, conversely, the roll-off function (and hence the magnetic field) for a given geometry.
To construct the required training data, we need an efficient way to relate the scalar potential in the three-dimensional fringe region to the roll-off function along the magnetic axis, avoiding the need for numerical integration (in Eq. 3) of the functions f n (ζ) and g n (ζ).This may be achieved as follows.We first express an (arbitrary) roll-off function G n (ζ) as a Fourier series: The scalar potential is then found in terms of the Fourier coefficients a k by making use of the integral [6]: These results allow, given a function G n (ζ) (and coefficients b j ) rapid computation of the scalar potential.The geometry of the end-face of a multipole magnet corresponding to the field derived from the potential is obtained as an equipotential surface.

Machine learning tools for computing fringe fields
As an intermediate step towards the goal of directly relating the magnet geometry to the particle dynamics in the fringe field, we can create a machine learning model (a neural network) to construct the scalar potential from a given roll-off function G n (ζ).For purposes of illustration, and as a convenient way to generate training data for a neural network, we consider fringe fields in quadrupole magnets, with roll-off functions constructed from the three-parameter Enge function: where the parameters A, B and C can be chosen arbitrarily.The effect of the higher-order parameters on the shape of the Enge function is illustrated in Fig. 1.
For any given values of the parameters A, B and C, the resulting function G n (ζ) is sampled at a number of points through the fringe field, and the Fourier coefficients a k obtained by a discrete Fourier transform.Because of the freedom in the transverse behaviour of the fringe field provided by the parameters b j , the function G n (ζ) on its own is not sufficient fully to determine the scalar potential.We therefore include in the training data values of the transverse field components along lines through the fringe field parallel to the magnetic axis, but with some transverse offset as for a quadrupole, ⃗ B = 0 when x = y = 0. Using the analytical formulae presented above, the scalar potential is then computed on a three-dimensional Cartesian grid in the fringe-field region.Several thousand such cases (each case consisting of the value of G n (ζ) sampled at points through the fringe field, and the corresponding scalar potential on a three-dimensional Cartesian grid) can then be used as training data for a neural network.
An example from testing the trained network is given in Fig. 2, which shows the residuals between the scalar potential calculated analytically from a given three-parameter Enge function, and the scalar potential obtained from a neural network trained as described above.
The results in Fig. 2 demonstrate that a neural network can be trained to compute the scalar potential in the fringe field of a quadrupole magnet, given the magnetic field.This calculation can of course be readily performed using conventional techniques, not requiring machine learning.A more challenging computation is to find the magnetic field given the shape of the end face of a multipole magnet (i.e. an equipotential surface).This requires solution of Poisson's equation, and would normally be performed using a finite element analysis code.However, the training data already constructed (for the neural network used to generate the results in Fig. 2) can also be used for training a neural network for the "reverse" calculation, i.e. to find the full (three-dimensional) fringe field from a given equipotential surface.Some results are illustrated in Fig. 3.The plots compare the known roll-off function G n (ζ) with the results from a trained neural network, given the equipotential surface as input.

Fringe field particle dynamics
The results presented in the previous section demonstrate that machine learning tools can provide a rapid way to compute the geometry of a multipole magnet given properties of the fringe field, and vice-versa.This in itself can be of value in optimising the design of a magnet in an accelerator; however, the main goal is to be able to relate the magnet geometry directly to the particle dynamics in the fringe field.Here again, machine learning tools can be of use, and in this section we present the results from studies (still in progress) of the relationships between the geometry of a multipole magnet and the dynamics of particles moving through the fringe fields.
In general, the trajectories of particles in three-dimensional magnetic fields need to be computed using numerical integration techniques.For the present studies, we use a magnetic velocity-Verlet algorithm [7].The dynamics can be conveniently characterised using the transfer functions, i.e. representing the final phase-space co-ordinates of a particle after travelling some distance through a given field, as functions of the initial phase-space co-ordinates.Some examples of transfer functions in a quadrupole fringe field are shown in Fig. 4: in this figure, we show only the final transverse momentum p x as a function of the initial transverse co-ordinate x.Other combinations of final and initial variables are also computed, but are not shown here.The properties of the field in this case (for a normal quadrupole) mean that  transfer functions can be fitted to good accuracy using polynomials of fifth-order, including only odd-order terms, i.e. for the horizontal motion we write the transfer function as: where x is the initial value of the x co-ordinate of a particle entering the fringe field, and c x,n are coefficients determined by fitting the tracking results with a fifth-order polynomial.
A machine learning model for the dynamics can be constructed by training a neural network using a set of roll-off functions G n (ζ), and the corresponding polynomial coefficients describing the transfer functions.The results of such a model, with the roll-off function provided as input, and the transfer function polynomial coefficients obtained as output, are shown as red lines in Fig. 4: the black points show the results of tracking through the known fringe field (described analytically by the roll-off function).
Finally, the same data relating the roll-off function in the fringe field to the transfer functions can be used to train a neural network to generate a roll-off function for given transfer functions.Some results from such a neural network are shown in Fig. 5: the black points show the known roll-off function used to generate the fringe field from which the transfer functions are computed, and the red lines show the roll-off functions obtained from the neural network given the transfer functions as input.We see that there is good agreement, even in the detailed features of the roll-off functions.Since the magnet geometry is readily obtained from the roll-off function (as described in the previous section), the use of machine learning in this way opens the possibility for rapid optimisation of magnet design to achieve specific properties of the beam dynamics.

Figure 1 .
Figure 1.Enge functions (Eq.6) for describing the roll-off of the field in the fringe region of a multipole magnet.The different colour lines show shapes obtained with a single non-zero coefficient (B = C = 0 in Eq. 6), with two nonzero coefficients (C = 0) and with three non-zero coefficients.

Figure 2 .
Figure 2. Scalar potential computed from fringe-field data.Left: equipotential curves in a typical case at z = 0 in a quadrupole fringe field.Because the end face of the magnet is an equipotential surface, the plot also indicates the geometry of the end face (i.e.z co-ordinate for different transverse offsets).Right: distribution of residuals, defined as the difference between the known values of the scalar potential and the values obtained from a neural network (given field data as input), divided by the rms value of the scalar potential.

Figure 3 .
Figure 3. Gradient roll-off along the magnetic axis in the fringe field of a quadrupole magnet.Black points show known values from a three-parameter Enge function; red lines show the rolloff computed by a neural network, using the scalar potential as input.

14thFigure 4 .
Figure 4. Transfer functions in a quadrupole fringe field.The linear component is subtracted, to show more clearly the nonlinear behaviour.Black points are from particle tracking through the known fringe field; the red lines show the transfer functions predicted by a neural network, given the fringe field roll-off function as input.

Figure 5 .
Figure 5. Roll-off functions computed using machine learning from given transfer functions (red lines) compared with the roll-off functions originally used (black points) to construct the fringe fields from which the transfer functions are obtained by particle tracking.