Shimming Magnets in a Particle Accelerator

Advance Photon Source Electron Storage Ring

Introduction

The Advanced Photon Source (APS) is a scientific facility at Argonne National Laboratory, used by over 5,500 scientists annually. It generates ultrabright X-rays, which can be used in the development of robust materials, the advancement of efficient battery technologies, and the research contributing to vaccines and treatments for infectious diseases. The comprehensive upgrade to the facility's electron storage ring, scheduled to begin in April 2024, promises a substantial enhancement in X-ray brightness, reaching up to 500 times its current intensity. This upgrade is poised to unlock novel opportunities for scientific exploration and discoveries. Replacing the existing electron storage ring, the new storage ring is preassembled into 200 modules, each of them containing an array of magnets.

Magnets play the crucial role of steering and focusing the circulating electron beam. Hence, the positioning and alignment of the magnets around the storage ring is of upmost importance. If the magnets in the storage ring are not properly aligned, it can lead to deviations in the trajectory of the circulating electron beam, resulting in irregularities in the emitted X-ray photon beams. The consequences include reduced beam quality, diminished experimental precision, and potential safety violations, as harmful X-rays could be directed towards sections that are inadequately shielded from radiation. One of my responsibilities as an intern at Argonne was to develop a time-efficient algorithm for computing the number of micron-sized shims each magnet in an array (or module) should be fitted with such that the alignment error is minimized.

Problem Statement

 

Module Assembly

 

For each module, magnets are assembled onto a metal girder with high precision. There is still, however, measurable error in the positions of these magnets. This error is on the level of microns. Due to misalignment during the assembly stage, the positions of the magnets look more like the figure drawn below, where the scale of the y-axis is disproportionately larger than that of the x-axis to exaggerate the positional errors. The y-axis represents the error (the difference between where the magnets are and where the magnets should be), not the position. The x-axis runs along the length of the module, roughly in the direction tangent to the storage ring. The positional errors are measured using a laser displacement sensor and recorded for each of the magnets.

Initial Magnet Positional Errors

The lab has access to accurately machined, 25 micron shims that can be added to each of the magnets to adjust their position on the girder. The shims can only be added in one direction with respect to the y-axis. In the figure below, the red lines mark the position of the magnets after $N$ number of shims have been added. The dark blue dots mark the initial magnet error and the light blue dots mark the optimal magnet error after shimming. Note that the residuals of the light blue line of best fit are smaller than that of the dark blue line of best fit, indicating that the shimmed magnets are better aligned to the curve they are supposed to lie on. The absolute error (measured from a fixed point on the girder), may have increased, but that doesn’t matter since the girders are assembled in a later stage that undergoes another alignment process. What we care about is the direction of the magnetic fields relative to one another in a single module or, more informally, that electrons’ paths follow a specific curvature.

 

Shimmed Magnet Positional Errors

 

I used the standard error as the quantitative metric that assesses how aligned the magnets are. The shims, themselves, have tight, albeit non-zero, tolerances. Hence, it is preferable to use a minimal number of shims to avoid accumulating errors from the shims. All together, the objective is to minimize the root mean squared error as well as the number of shims used to align magnets in a single module. The weighing of the two goals is left undecided as a parameter to be tuned experimentally. The objective function is defined as

$$f=w_1 \cdot \text{SE} + w_2 \cdot \sum{N_i}$$

where $w_1$ and $w_2$ denote the weights and $N_i$ denotes the number of shims fitted to the ith magnet.


Approach

My solution was a brute force algorithm. Let’s represent a shim configuration with an array of integers, where the first element is the number of shims fitted onto the first magnet, the second element is the number of shims fitted onto the second, and so on. I realized that the solution space was limited to the subset of shim configurations in which at least one element in the array was equal to 0. This is due to the fact that the magnets should only be shimmed in one direction and that adding the same number of shims to every magnet in the module did not affect the RMSE. If all the magnet were fitted with at least one shim, then the same SE can be achieved by a solution that uses 1 less shim for each magnet in the module. This constraint limits the search for the optimal shim configuration to a space centered around a zero-slope line that intersects one of the magnets initial errors. Subsequently, the algorithm for computing the optimal shim configuration is as follows.

  1. Determine the error of the magnet with the highest value in the y-axis.

  2. Add shims to all the magnets with y-values less than that of the highest magnet. The shimmed error of the magnets should lie below or at the error of the highest magnet, such that adding one more shim causes the magnets’ y-values to exceed that of the highest magnet.

  3. Search all permutations of shim configurations in proximity to the baseline established in the previous step. Select the shim configuration that minimizes the objective function.

To specify the “proximity” of shim configuration, we can say that the number of shims for each magnet is allowed to deviate by $k$ number of shims. For example, [0, 1, 2] is in proximity of [0, 0, 0] if $k=2$, but [0, 1, 3] is not because the last magnet is 3 (greater than 2) shims away. To gauge the computational intensity of our algorithm, let’s approximate the size of the search space. It varies per module, but the average number of magnets in a module is around 7. The highest magnet starts with 0 shims and since the shimming is unidirectional, that magnet can be fitted with 0 to $k$ shims ($k+1$ possible integer values). The rest of the magnets can be fitted with $n-k$ to $n+k$ shims ($2k+1$ possible integer values), where $n$ is the number of shims added in step 2. $n-k$ is lower bounded by 0. Hence, the upper bound of the size of the search space, is equal to $(k+1)(2k+1)^6$. If $k=3$, then the number of linear regression performed will be 470596. This rough calculation motivates a need for a time-efficient algorithm. I chose to parallelize the regression of the shimmed magnet errors and computation of the shim configuration’s score (output of objective function).

def lin_regress(index_shim_numbers):
    shim_numbers = var_dict["permutations"][index_shim_numbers]
    X_tofit = var_dict["X_np"] + np.multiply(shim_numbers, var_dict["MagShim_np"])
    regression_result = scistats.linregress(var_dict["Z_np"], X_tofit)
    stderr = float(regression_result.stderr)
    weighted_score = var_dict["stderr_weight"] * stderr / 5e-6 + (
        1 - var_dict["stderr_weight"]
    ) * np.sum(shim_numbers) / (len(shim_numbers) * 5)

    return weighted_score

The code shown above is the parallelized portion of the program. var_dict is a hashtable (Python dictionary) of shared memory between the multiple processors. index_shim_numbers is the index of the shim configuration in the array containing all the configurations in the search space. The shimmed magnet errors are calculated, and then a linear regression is performed on those errors. I use the standard error of that regression and the total number of shims used to calculate the value of the objective function, or the weighted_score.

 

Plot of Initial and Shimmed Magnet Errors

 

The plot shows the result of this algorithm run on the module, DLMA-1010. The blue crosses, “+”, mark the initial magnet errors and the orange dots mark the shimmed magnet errors. The standard error of the orange regression line is less than that of the blue, meaning our algorithm was successful.

Backend and Frontend Integration

The magnet errors were extracted from Argonne’s Component Database (CDB) API. A basic graphical user interface (GUI) was created for magnet alignment technicians to run the algorithm. Inputs into the program included the name of the module, which iteration of shimming was being run, the name of the file containing where the magnets are located along the length of the module, the axis being shimmed, the value of $k$ (although, the option to choose an asymmetrical search area around the baseline is permitted), the weight of the standard error (the weight of the total number of shims is the complement), and the number of the highest-scoring shim configurations to display. Here’s a brief explanation of why we need some of those inputs:

  1. The modules usually undergo one iteration of shimming. However, due to the tolerances of the shims themselves and imperfect installation of the shims, the modules may need to undergo a second iteration to, again, correct the curvature of the magnets’ positions.

  2. The magnets can also be shimmed in two axes, X or Y. Positional errors of the magnets along the Z axis, or the length of the module, are considered to be negligible since those errors are magnitudes smaller than the distance between magnets.

  3. I created the option to display a number of the best shim configurations instead of just one. This becomes useful when users are tuning the weights in the objective function. If they see a shim configuration that achieves nearly the same standard error with much fewer shims ranked below first, then they can increase the weight of the total shims used. Similarly, if they see a shim configuration with a much smaller standard error with a greater number of shims ranked below first, then they can increase the weight of the standard error.

Magnet Shimming User Interface


Results

After the magnets were shimmed, measurements of the positional errors were taken. After at most two iterations of shimming, the errors (residuals in the regressions) dropped below 5 microns, achieved successfully for all 200 modules. I received high praise for the work I had performed.

The program does exactly what we need it to do. It is neatly packaged in an intuitive GUI, and everyone on the APS-U Assembly team is thoroughly impressed with Charles’ work. Charles is extremely quick to understand requirements, hardworking, and an extremely capable engineer.
— Jeremy Nudell (Mechanical Engineer on APS Upgrade Project)
Previous
Previous

Angry Birds Space: A Custom Physics Engine