Technical Articles and Newsletters

MATLAB Based Toolboxes to Simulate and Control High Energy Particle Accelerators

By Jeff Corbett, SLAC National Accelerator Laboratory

The SPEAR3 particle accelerator at the SLAC National Accelerator Laboratory in Menlo Park, California, produces extremely intense x-ray beams that enable researchers to study phenomena at the atomic and molecular level. Each year more than 1700 scientists conduct experiments using the accelerator at the Stanford Synchrotron Radiation Lightsource (SSRL), resulting in approximately 15,000 scientific publications since 1974 covering advances in nanotechnology, solar power, biology, medicine, and many other fields.

Particle accelerators are complex scientific facilities; the SPEAR3 synchrotron, for example, is 234 m in circumference, has 17 high-brightness x-ray "microscope" beam lines, and requires hundreds of large electromagnets, including dipoles, quadrupoles, and sextupoles, to operate (Figures 1 and 2).

Figure 1. Section of the SPEAR3 synchrotron at the SLAC National Accelerator Laboratory.

Figure 2. Plan view diagram of the SPEAR3 synchrotron showing layout of x-ray of beam lines. 

To make it easier for scientists to automate and conduct experiments at SPEAR3 and similar accelerators worldwide, SLAC, in collaboration with the Lawrence Berkeley National Laboratory (LBNL), created three MATLAB® toolboxes: MATLAB Middle Layer, LabCA, and the Accelerator Toolbox (Figure 3). 

Figure 3. MATLAB toolbox architectural diagram for accelerator control and operations.

Using these toolboxes, researchers can design experiments and run them repeatedly on a simulated accelerator model to fine-tune the procedure, and then run equivalent experiments on the actual accelerator. After analyzing the results in MATLAB, researchers can share code with colleagues at other facilities, who can then run the same or similar experiments on an entirely different accelerator using the same MATLAB Middle Layer software.

Before developing the suite of MATLAB toolboxes, we considered a variety of interpretive and compile/link style programming languages. At both SLAC and LBNL we were already using MATLAB for machine control and data analysis. Implementing accelerator simulation control capabilities with MATLAB graphical capabilities enabled us to bring the experimental workflow into one environment, saving both time and money. One of the main goals in the software development was to maintain “machine independence” so that research scientists could easily transfer accelerator software programs from one accelerator facility to another. The code transfers require only minor changes to the source code and the one-time creation of a centralized database file specifying key parameters particular to each machine.

Connecting MATLAB to Particle Accelerator Hardware with LabCA

In order to establish an interface between MATLAB and the EPICS hardware control software (Experimental Physics and Industrial Control System), we developed a MATLAB “wrapper” around EPICS known as LabCA. (The LabCA toolbox works well not only with EPICS but also with many other communication protocols). LabCA allows us to control particle accelerators or any other facilities, such as deep space telescopes, directly from MATLAB. This relatively simple LabCA toolbox enables us to conduct experiments on SPEAR3 by scripting MATLAB commands to read and write online control system variables; store, recall, and manipulate the data; and generate plots of the results.

Accelerator Simulation in MATLAB with the Accelerator Toolbox

The Accelerator Toolbox (AT) is a collection of tools for modeling and simulating circular accelerators and beam transport lines. At the time we developed AT, most accelerator simulators were compiled or “canned” programs that were difficult to modify or extend. Unlike canned programs, AT enables the researcher to interactively test a variety of operational scenarios using sophisticated simulation algorithms (Figure 4). The results can be directly processed in MATLAB for visualization and analysis. The MATLAB code in AT is open and extensible, with the computationally intensive routines written in C/C++ and compiled into MEX files to improve speed and performance. 

Figure 4. Section of a synchrotron light source model displayed in an Accelerator Toolbox utility for interactive editing of accelerator element properties.

Originally developed for use with the SPEAR3 accelerator, AT is currently in use at dozens of laboratories worldwide for accelerator simulation studies. At its core, AT functions propagate or transport individual charge particles through the accelerator magnets tracking particle positions and arrival times. This tracking facility is then applied to calculate the equilibrium beam position (orbit) and beam size (envelope) and to simulate when, where, and at what rate charged particles are lost.

With the advent of fast personal computers, calculations that previously took tens of minutes or even hours now take only milliseconds. This speedup is important for optimization problems where the search space can include large multiparameter, nonlinear domains. Modern accelerators with small “high brightness” beams have very nonlinear operational conditions and, consequently, depend heavily on both particle tracking simulations and online machine control experiments.

Accelerator Calibration with LOCO

One operational breakthrough that occurred in the early 1990s was the application of system identification (SI) tools to particle accelerators. Originally written in Fortran, LOCO (Linear Optics from Closed Orbits) works by applying perturbations to the particle accelerator to generate a “response matrix” using LabCA (Figure 5). It then applies the same perturbations to a baseline AT model of the accelerator to generate a second, simulated response matrix for comparison. Finally, LOCO performs SI using a Levenberg–Marquardt algorithm implemented in MATLAB to tune the parameters of the AT model so that it more accurately represents the real accelerator. The algorithm quickly gained popularity, as the new “calibrated” accelerator model enabled researchers to run accurate simulations of the accelerator behavior under a wide range of operating conditions.

Figure 5. The LOCO interface showing a 60x85 measured response matrix. 

Automating Common Tasks with the MATLAB Middle Layer

MATLAB Middle Layer (MML) provides a library of more than 100 functions that automate a variety of common tasks, including fast and efficient access to individual accelerator component properties. High-level functions and applications include global orbit correction, local photon beam steering, insertion device compensation, beam-based alignment, tune correction, and response matrix measurements for LOCO.

The same MML syntax is used for both online and simulation applications. MML uses the LabCA toolbox for the online accelerator, with the option to use either the EPICS protocol or different communication protocols available at other laboratories. In general, the MML acts as a layer of abstraction between high-level control scripts and either AT or the actual particle accelerator. This make it possible for physicists to run simulation experiments repeatedly with AT until they are satisfied with the results and then issue a single “switch” command to run the experiment on the operational accelerator.

Ongoing Research

The suite of MATLAB toolboxes for accelerator science is currently in use around the world, including Australia, Brazil, Canada, China, several European countries, Japan, Korea, and the United States. In addition to enabling accelerator scientists to model, simulate, and control existing machines, the toolboxes make possible optimization and automation studies and help physicists commission new accelerators on a common platform. The complex tuning processes required to commission a new accelerator previously required six months or more. Researchers can now complete these processes in about two weeks by downloading publicly available, machine-independent MATLAB toolboxes and collaborating with scientists already familiar with the software.


The toolboxes described in this article are the products of a long-running collaborative effort by many individuals. In particular, the author acknowledges the substantial contributions of Andrei Terebilo (AT), James Safranek (LOCO), and Till Straumann (LabCA) at SLAC and Greg Portmann (LOCO, MML) at Lawrence Berkeley National Laboratory.

About the Author

Jeff Corbett is a senior staff scientist at SLAC National Accelerator Laboratory. In his 30 years at Stanford University, he has made major contributions to numerous initiatives, including construction of the SPEAR3 synchrotron light source.

Published 2021


View Articles for Related Capabilities