For the free-electron maser design we describe in Prototype free-electron maser design, it is essential to model the magnetic fields in the undulator as well as in the collimating magnets and magnetic lenses. This nearly conducting computational magnetostatics, since the magnetic fields do not have an exact analytical solution for all but the simplest cases. We will give a brief guide for how these simulations are performed.

Introduction to magnetostatics

We will start by giving a basic overview of magnetostatics. Magnetostatics describes the behavior of charges in time-independent magnetic fields. This is only an approximate description of electromagnetism, but works well when the fields vary very gradually (or not at all) in time. The two fundamental equations of magnetostatics are two of the Maxwell equations (Gauss’s law and Ampere’s law) in the time-independent regime, and take the form:

Where is the total current density, is the magnetic field, is the demagnetizing field, and the latter two are related by the constitutive relation of magnetostatics:

Where is the magnetization of the material. Solving these equations for different boundary conditions allows us to find the fields, and therefore the trajectory of particles within these fields. The study of boundary-value problems (BVPs) in magnetostatics, and in particular the specific BVPs that we encounter in modelling free-electron lasers/masers, are discussed within this guide.

Magnetic fields for the undulator

Purely exterior case

To start, we are concerned with finding the magnetic field outside the magnet. It is also possible to find the fields in the interior of the magnet, but this is less useful for our particular applications, so we first consider the external fields only.

Outside a permanent magnet, assuming there are no significant currents present and magnet is surrounded by a non-permeable medium (), then simplifies to . Then it is possible to write in terms of a potential , where is the magnetic scalar potential, and satisfies . By substituting into Gauss’s law () where in the surrounding region, we arrive at:

Therefore, after straightforward simplifications, we arrive at Laplace’s equation for the magnetic scalar potential:

This can be solved in the exterior case to arrive at the following analytical solution1:

For a uniform permanent magnet, the magnitude of the magnetic field when no external field is applied (that is, ) is defined to be equal to its magnetic remanence ; thus from the constitutive relation for magnetic fields, we have2:

Therefore, using this definition, the integral can be evaluated explicitly, though an analytical expression can only be found for simple geometries; the integral must be evaluated numerically (for instance, via Monte-Carlo methods or numerical quadrature) for all other cases. Here, we will take the example of a rectangular bar magnet, for which the surface integral can be done analytically. This corresponds with the magnetic geometry and type used in our undulator design, which uses permanent magnets only (as shown below).

Thus, in our case, where , and the integral becomes:

Let us assume that the magnets have length , width , and depth , where the length is measured along the axis, the width is measured along the axis, and the depth is measured along the axis. Thus, the rectangular cuboid we are integrating over is defined by . To make this integral more tractable, we can assume that the magnets are “thin” magnets and thus set , giving us the slightly “simpler” integral:

Where here we need only integrate over one surface (by symmetry, whether we choose this to be the front or back of the magnet doesn’t matter) as opposed to the six integrals that would otherwise need to be done for . Thus the surface integral reduces to the following double integral:

To solve this integral, we first integrate with respect to with the following substitution:

Now we may use the following identity:

Which allows us to evaluate the integral in as follows:

Now, substituting in the explicit form of , we have:

With the substitution we obtain the two integrals in a slightly simpler form:

Both of the resulting integrals in the sum are integrals of the form:

This is an integral that can be evaluated using integration by parts or (more conveniently) by consulting a computer algebra system; the solution is given (up to a constant) by3:

Where we will not write out explicitly in the rest of the solution for simplicity. Thus we have:

Where we define as:

And thus the complete solution can then be written succinctly as:

While appearing deceptively-simple, this succinct answer expands out to 12 terms(!) in total, making it highly impractical to use for actually compute this solution. A much more numerically-usable solution can be found by not evaluating the integral in , giving us:

Where the function is defined by the following integral:

And thus the magnetic field can be found (via ) to be:

The numerical derivatives here can be found by finite differences, but the analytical derivative can also be found by differentiating under the integral sign; that is:

Note that , and thus for small and (that is, reasonably close to the magnet’s surface) we have:

If we then assume that is small, we can use the approximation once again, giving us:

Substituting this result into our analytical expression for gives us:

From which we may find to be:

A validation test for the solution is that when taking the other limit (for very far distances), the solution should reduce to a simple dipole:

And therefore the magnetic field would fall off by the inverse cube of the distance. This is what allows permanent magnets to be well-approximated by perfect magnetic dipoles.

TODO: Make a 3D plot of the exact solution and the field lines for the and fields.

Solving with a CAS

In theory, the surface integral can also be evaluated with the following Mathematica code:

yprime = Subscript[p, y]
zprime = Subscript[p, z]
Integrate[
	 1/Sqrt[x^2 + (y - yprime)^2 + (z - zprime)^2],
	 {yprime, 0, L}, {zprime, 0, w}
 ]

Or with the following SymPy code:

from sympy import *
x, y, z = symbols("x y z", real=True)
Br, w, L = symbols("B_r w L", constant=True, 
		real=True, positive=True)
mu0 = Symbol(r"\mu_0")
yprime, zprime = symbols("y' z'")
prefactor = Br/(2*pi*mu0)
integrand = 1/sqrt(x**2 + (y-yprime)**2 + (z - zprime)**2)
integral = Integral(integrand, (yprime, 0, L), (zprime, 0, w))
psi = prefactor * integral.doit()
Eq(Symbol(r"\psi_m"), psi)

The issue is that both SymPy and Mathematica time out when trying to compute the integral; it is likely that neither can compute it, even if they are given longer times.

Interior and exterior case

We will now consider the more complicated case where we solve for both the interior and exterior magnetic fields. This involves a more complicated boundary-value problem that generally cannot be solved analytically, even in integral form. Instead, numerical methods are necessary for most cases.

To formulate the problem, we must consider the pertinent boundary conditions. First, we impose the boundary condition that the magnetic scalar potential vanish at infinity, that is:

In practice, this boundary condition is only useful analytically; simulating an infinitely-large domain numerically is impractical (though there are some numerical methods that can do so, such as the boundary element method), so we can simply set a cutoff radius (which is large compared to the size of the magnet), giving us:

Next, we consider the boundary conditions at the interface between the interior of the magnet (with permeability ) and the exterior of the magnet (with permeability ). When the relative permeability (which is approximately true for many ferromagnetic materials), then just outside the surface of the magnet4, (the tangential component) becomes zero and (the normal component) becomes the only component of ; this is exactly analogous to the perfect conductor in electrostatics. Therefore, at the surface of the magnet, we obtain the following Neumann boundary condition:

Where denotes the surface of the magnet, denotes the coordinate tangent to the surface, and where the solution is valid for the entirety of the exterior of the magnet. Meanwhile, we may determine using the standard boundary conditions for magnetostatics, given by:

Where is the effective surface current density, is the field just underneath the surface of the magnet, and is the field just outside the magnet (assumed to be vacuum or another non-permeable medium). Then by substitution, one has:

More precisely, the above is only true if are nearly normal to the surface (which occurs in the limit of very high relative permeabilities). In the general case, we have:

Where are the normal components of the fields, respectively. Now, to determine , we use the constitutive relation . Here it is implied that represent the interior fields and magnetizations; that is, , , . We must now impose a constitutive relation between and to solve. In general, this relationship is nonlinear for permanent magnets, and thus . However, for many permanent magnets5 we can make the linear approximation given by6:

Here, is the recoil permeability of the material and is a material constant, where we can often make the assumption (which is true for a perfect magnet). From these boundary conditions, we can then solve for the fields both inside and outside the magnet.

Magnetic fields for the magnetic lens

The field of a sextupole magnet can be written in spherical coordinates (assuming azimuthal symmetry, or in this case, ) via the multipole expansion, which, up to some constant factors, takes the form:

Where are some constant factors, and is a Legendre polynomial. The first three Legendre polynomials are given by:

Thus, we can write in explicit form as:

Note: For the purposes of our discussion, the precise forms of don’t matter, since we’re only interested in the qualitative characteristics of the solution, which are not affected by the values of the constants.

To describe a sextupole magnet, we truncate the series after three terms, hence the name “sextupole” (which comes after the dipole (first) term and the quadrupole (second) term). An interactive Desmos visualization shows the field magnitude as a function of angle. The strong fields near the edge of the sextupole magnet are what cause charges to be repelled towards the center of the magnet, which allows the sextupole magnet to serve the function of a magnetic lens. Borrowing an idea from transmission electron microscopes6, if we use two sextupole magnets of opposite polarities (that is, one flipped so all N-S pairs become S-N pairs), the two lenses can act as a combined converging-diverging lens. This means that rather than diverging the beam (as with a single magnet lens), the two magnets both collimate the beam and restore the beam back to a circular shape.

Numerical methods

The typical numerical method used for magnetostatics is finite-element analysis,. To begin, we start with the basic differential equation of permanent magnets7:

Assuming a constant permeability , one therefore obtains:

To derive the weak form, we multiply by test function , and integrate both sides (see a more in-depth description of the finite element method in RF cavity simulation with aperture). This gives us:

We can simplify this by using integration by parts or by simply consulting a list of vector calculus integral identities. This gives us:

Combining the volume and surface terms gives us:

Note that must be pre-provided. In our case for permanent magnets (as detailed previously), this is a piecewise function:

Where is the remanence, as before. This can then be solved for suitable boundary conditions (as described above). There are dedicated open-source magnetostatics codes such as Radia as well as general-purpose FEM packages (such as FreeFEM and MFEM) that can be used to solve the resulting BVP numerically.

Footnotes

  1. See Jackson, Classical Electrodynamics (3rd. ed.), pg. 207, eq. 5.100. We assume a permanent magnet with homogeneous magnetization, and thus the volume integral term drops out (since ).

  2. Please see Jiles, Introduction to Magnetism and Magnetic Materials (2nd ed.), 1998. Originally from this article.

  3. This was obtained using MathDF but can also be found using a dedicated computer algebra system.

  4. This applies locally, no matter the geometry of the magnet itself; for more information, see Jackson, Classical Electrodynamics (3rd. ed.), pg. 212

  5. Specifically for “hard” magnets like neodymium or ferrite magnets, which keep their magnetization near-permanently, independent of any applied fields.

  6. From Campbell, Permanent Magnet Materials and Their Application, 1994 (p. 94-96). Originally from this Physics SE answer. 2

  7. Referenced from the Wolfram guide on magnetostatic modelling for permanent magnets.