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.
The typical numerical method used for magnetostatics is **finite-element analysis**. To begin, we start with the basic differential equation of permanent magnets[^1]:
$
\nabla \cdot (\mu(-\nabla \psi_m) + \mathbf{M}) = 0
$
Expanding it gives us:
$
-\mu \nabla^2 \psi_m + \nabla \cdot \mathbf{M} = 0
$
To derive the weak form, we multiply by test function $v$, and integrate both sides (see a more in-depth description of the finite element method in [[RF cavity simulation with aperture]]). This gives us:
$
\begin{align*}
\int_\Omega v (-\mu \nabla^2 \psi_m + \nabla \cdot \mathbf{M}) dV = 0 \\
-\mu \int_\Omega v \nabla^2 \psi_m dV + \int_\Omega v\nabla \cdot \mathbf{M} = 0
\end{align*}
$
We can simplify this by using integration by parts or by simply consulting a list of vector calculus integral identities. This gives us:
$
\mu\int_\Omega \nabla v \cdot \nabla \psi_m~dV
- \mu\int_{\partial \Omega} v \nabla \psi_m \cdot d\vec S - \int_\Omega \mathbf{M} \cdot \nabla v~dV + \int_{\partial \Omega} v \mathbf{M} \cdot d\vec S = 0
$
Combining the volume and surface terms gives us:
$
\mu\int_\Omega (\nabla v \cdot \nabla \psi_m - {M} \cdot \nabla v)~dV
- \mu\int_{\partial \Omega} v (\nabla \psi_m - \mathbf{M}) \cdot d\vec S = 0
$
Note that $\mathbf{M}$ must be pre-provided. In our case for permanent magnets (as detailed in [[Free-electron maser physics]]), this is a piecewise function:
$
\mathbf{M} = \begin{cases}
\dfrac{B_r}{\mu} \hat{\mathbf{x}}, & \text{inside magnet} \\
0, & \text{outside magnet}
\end{cases}
$
Where $B_r$ is the **magnetic remanence** of the magnet, and is a (known) material-dependent constant.
[^1]: Referenced from the Wolfram guide on [magnetostatic modelling for permanent magnets](https://reference.wolfram.com/language/PDEModels/tutorial/Electromagnetics/MagnetostaticsForPermanentMagnets.html).