Study of fully coupled 3D envelope instability using automatic differentiation Ji Qiang Abstract Auto-differentiation is a powerful tool for computing derivatives of simulation results with respect to given parameters. In this letter, we have applied this tool to investigate the instability of a dynamics system that is governed by 21 21 ordinary differential equations. This second-order instability (named envelope instability) is driven by space-charge effects and has significant impact on the operational regimes of particle accelerators. Our study delves into the three-dimensional envelope instability, incorporating both transverse and longitudinal coupling. Conventionally, analyzing this complex system would necessitate solving 441 441 ordinary differential equations, which is computationally intractable. However, by employing auto-differentiation, we were able to track only 21 21 equations. This approach allowed us to uncover an additional instability stopband, which arises from space-charge-induced coupling and has not been reported in previous studies. This research highlights the significant advantages of auto-differentiation in analyzing complicated dynamical systems involving a large number of ordinary differential equations. Automatic differentiation (AD) is a mathematical technique that enables the efficient computation of derivatives of complex functions with respect to a given set of parameters without numerical approximation or symbolic differentiation. This method has been widely adopted in the artificial intelligence and machine learning (AI/ML) community for training neural network parameters ad ; pytorch ; tensorflow . More recently, AD has found applications in the particle accelerator physics community, where it has been used to evaluate the sensitivity of simulation results to accelerator lattice parameters and to accelerate optimization procedures roussel2022 ; roussel2023a ; roussel2023b ; qiang2023 ; cheetah ; wan . However, to the best of our knowledge, AD has not previously been applied to dynamical systems for stability analysis. Traditionally, stability studies of dynamical systems involve solving the transfer matrix equations for perturbed tangent vectors of the system variables. For highly complex systems, such as three-dimensional (3D) envelope evolutions with coupling induced by space-charge effects, the number of ordinary differential equations (ODEs) in the tangent vector transfer matrix can become prohibitively large—often exceeding 400—making direct computation intractable. In such cases, AD offers an efficient alternative. By solving only the original system of ODEs—typically comprising about 20 equations—AD can automatically generate the corresponding transfer matrix for the tangent vectors. This significantly reduces computational complexity while preserving accuracy, making AD a powerful tool for stability analysis in complex dynamical systems. In the particle accelerator community, envelope instability in a periodic transport channel refers to the instability in the evolution of the second moments of a charged particle distribution as it propagates through the channel. This second-order collective instability, driven by direct space-charge effects, can lead to beam size blow-up, emittance growth, and potential particle losses, thereby significantly constraining the operational regime of high-intensity accelerators. This instability has been recognized as a critical limitation in intense beam transport and has been the subject of extensive investigation since the 1980s ingo1 ; jurgen ; ingo3 ; chernin ; barnard ; okamoto ; fedotov0 ; fedotov ; lund ; tiefenback ; gilson ; groening ; qin ; li0 ; li ; ingo2 ; goswami ; jeon2 ; oliver ; ito ; yuan ; ingo4 ; ingoprab17 ; qiangenv ; qiangenv2 . Most of those studies have focused on two-dimensional coasting charged particle beams. More recently, an investigation of bunched beam instability in a periodic quadrupole RF transport channel, based on an upright 3D envelope model without space-charge induced coupling from initial rotation, revealed a significant influence of longitudinal synchrotron motion on instability stopbands qiangenv . In this study, we examine a 3D bunched beam propagating through a periodic focusing lattice in an accelerator operating without acceleration. This scenario, where no external skew or tilt focusing is present and the only coupling arises from space-charge forces associated with beam rotation, has not been studied before. Following the notation of Ref. ryne , for a charged particle in a periodic focusing lattice without acceleration, the Hamiltonian in terms of the six dimensionless phase-space coordinates \zeta=(\bar{x},\bar{p}_{x},\bar{y},\bar{p}_{y},\bar{z},\bar{p}_{z}) can be expressed as: \displaystyle H(\zeta,s) \displaystyle= \displaystyle\frac{\delta}{lp_{0}}(\frac{\bar{p}_{x}}{2}+\frac{\bar{p}_{y}}{2}+\frac{\bar{p}_{z}}{2})+\frac{qg(s)l}{\delta}(\frac{\bar{x}^{2}}{2}-\frac{\bar{y}^{2}}{2})+ (1) \displaystyle\frac{l}{\delta}k^{2}_{z}(s)\frac{{\bar{z}}^{2}}{2}+\frac{p_{0}}{\delta l}K\phi(\bar{x},\bar{y},\bar{z}) where \bar{x}=x/l , \bar{y}=y/l , and \bar{z}=\Delta z\,\gamma/l are the normalized transverse positions and the relative longitudinal position with respect to the reference particle; \bar{p}_{x}=p_{x}/\delta , \bar{p}_{y}=p_{y}/\delta , and \bar{p}_{z}=\Delta E/(p_{0}c) are the normalized transverse momenta and the normalized longitudinal energy deviation with respect to the reference particle. Here, \delta=mc , where m is the particle rest mass and c is the speed of light in vacuum; l=c/\omega is the scaling length, with \omega the reference angular frequency; \gamma=1/\sqrt{1-\beta^{2}} is the relativistic Lorentz factor, with \beta=v/c and v the speed of the reference particle; p_{0} is the reference particle momentum; q is the particle charge; g is the quadrupole gradient; k_{z} is the longitudinal focusing strength; K=\frac{q}{mc^{2}\gamma^{2}\beta^{2}} is the space-charge factor; and \phi is the space-charge potential from a uniform ellipsoidal density distribution, given by \phi=\frac{Q}{4\pi\epsilon_{0}}\frac{\lambda_{3}}{l}\left[-\frac{1}{2}(\bar{x}^{2},\bar{y}^{2},\bar{z}^{2})\,AGA^{T}\,(\bar{x}^{2},\bar{y}^{2},\bar{z}^{2})^{T}\right], (2) where G is a 3\times 3 diagonal matrix with nonzero diagonal elements (g_{311},g_{131},g_{113}) , obtained from the eigenvalues of the 3\times 3 covariance matrix of the spatial coordinates; A is the eigenvector matrix associated with these eigenvalues; and the superscript T denotes matrix transpose. The constant \lambda_{3} depends weakly on the beam distribution: \lambda_{3}=\frac{1}{5\sqrt{5}} for a uniform distribution, \lambda_{3}=\frac{1.01}{5\sqrt{5}} for a parabolic distribution, and \lambda_{3}=\frac{1.05}{5\sqrt{5}} for a Gaussian distribution sacherer . The diagonal elements g_{lmn} are given by g_{lmn}(x,y,z)=\frac{3}{2}\int_{0}^{\infty}\frac{ds}{(x^{2}+s)^{l/2}(y^{2}+s)^{m/2}(z^{2}+s)^{n/2}}, (3) where x , y , and z are the square roots of the three eigenvalues of the spatial position covariance matrix. Given the above Hamiltonian, the equations of motion for the particle coordinates can be written as \frac{d\zeta}{ds}=F(s)\,\zeta, (4) where F(s) is a 6\times 6 matrix given by \displaystyle F(s) \displaystyle= \displaystyle\begin{pmatrix}0&\frac{\delta}{lp_{0}}&0&0&0&0\\ F_{21}(s)&0&F_{23}(s)&0&F_{25}(s)&0\\ 0&0&0&\frac{\delta}{lp_{0}}&0&0\\ F_{41}(s)&0&F_{43}(s)&0&F_{45}(s)&0\\ 0&0&0&0&0&\frac{\delta}{lp_{0}}\\ F_{61}(s)&0&F_{63}(s)&0&F_{65}(s)&0\\ \end{pmatrix} (5) The elements F_{ij}(s) are given by \displaystyle F_{21}(s) \displaystyle= \displaystyle-\frac{qg(s)l}{\delta}+\bar{K}(g_{311}A_{11}^{2}+g_{131}A_{12}^{2}+g_{113}A_{13}^{2}) (6) \displaystyle F_{23}(s) \displaystyle= \displaystyle\bar{K}(g_{311}A_{11}A_{21}+g_{131}A_{12}A_{32}+g_{113}A_{13}A_{33}) (7) \displaystyle F_{25}(s) \displaystyle= \displaystyle\bar{K}(g_{311}A_{11}A_{31}+g_{131}A_{12}A_{22}+g_{113}A_{13}A_{23}) (8) \displaystyle F_{41}(s) \displaystyle= \displaystyle F_{23}(s) (9) \displaystyle F_{43}(s) \displaystyle= \displaystyle\frac{qg(s)l}{\delta}+\bar{K}(g_{311}A_{21}^{2}+g_{131}A_{22}^{2}+g_{113}A_{23}^{2}) (10) \displaystyle F_{45}(s) \displaystyle= \displaystyle\bar{K}(g_{311}A_{21}A_{31}+g_{131}A_{22}A_{32}+g_{113}A_{23}A_{33}) (11) \displaystyle F_{61}(s) \displaystyle= \displaystyle F_{25}(s) (12) \displaystyle F_{63}(s) \displaystyle= \displaystyle F_{45}(s) (13) \displaystyle F_{65}(s) \displaystyle= \displaystyle-\frac{k_{z}^{2}l}{\delta}+\bar{K}(g_{311}A_{31}^{2}+g_{131}A_{32}^{2}+g_{113}A_{33}^{2}) (14) and \bar{K}=\frac{Q}{4\pi\epsilon_{0}}\frac{\lambda_{3}}{l}\frac{p_{0}}{\delta l}K . From the particle coordinate evolution equation, the evolution of the covariance matrix of the particle distribution, \Sigma_{ij}=\langle\zeta_{i}\zeta_{j}\rangle-\langle\zeta_{i}\rangle\langle\zeta_{j}\rangle , can be expressed as \frac{d\Sigma}{ds}=F\,\Sigma+(F\,\Sigma)^{T}, (15) where \langle\cdot\rangle denotes an average over the particle distribution in six-dimensional phase space. The covariance matrix \Sigma is symmetric and semi-positive definite, with 21 independent components. Therefore, Eq. ( 15 ) represents a coupled system of 21 independent ordinary differential equations. To study the stability of the above equations for a periodic system, the conventional approach is to introduce a perturbation to the original envelope equation, \Sigma(s)=\Sigma_{m}(s)+\tilde{\Sigma}(s), where \Sigma_{m}(s) is the matched solution to the envelope equation, and \tilde{\Sigma}(s) (also called the tangent vector ) denotes the perturbation to the matched solution. The evolution of the tangent vector is governed by another set of 21 ordinary differential equations. To analyze the stability of the evolution, the 21 independent perturbation components can be assembled into a vector \xi=\left(\tilde{\Sigma}_{11},\tilde{\Sigma}_{12},\ldots,\tilde{\Sigma}_{66}\right)^{T} . The equations of motion for the perturbations are then given by \frac{d\xi}{ds}=D(\Sigma_{m},s)\,\xi(s), (16) where D(s) is the 21\times 21 perturbation matrix. Let the solution to the above equation be written as \xi(s)=M(s)\,\xi(0) , where M(s) is the 21\times 21 transfer matrix of the perturbations. Substituting into Eq. ( 16 ) yields \frac{dM(s)}{ds}=D(\Sigma_{m},s)\,M(s), (17) with M(0) equal to the 21\times 21 identity matrix. This system consists of 441 coupled ordinary differential equations, which can be solved numerically using the matched envelope solution \Sigma_{m}(s) . The stability of the envelope perturbations is determined from the eigenvalues of the transfer matrix M(L) over one lattice period L . For the envelope oscillations to remain stable, all 21 eigenvalues of M(L) must lie on the unit circle in the complex plane. The magnitude of each eigenvalue gives the envelope mode growth (or damping) rate per lattice period, while its phase corresponds to the mode oscillation frequency. If the magnitude of any eigenvalue exceeds unity, the corresponding envelope mode becomes unstable. The direct numerical solution of the 441 coupled ODEs in Eq. ( 17 ) can be complicated and computationally intractable. Instead, we utilize the solution of the covariance matrix equation, Eq. ( 15 ), after one lattice period, which can be expressed as a function of the initial condition: \Sigma(L)=f\!\left(\Sigma_{0}\right). (18) To first order in the deviation from the matched solution, this can be written as \Sigma(L)\approx f\!\left(\Sigma_{m0}\right)+\frac{\partial f}{\partial\Sigma_{m0}}\left[\Sigma_{0}-\Sigma_{m0}\right], (19) which leads to M(L)=\frac{\partial\Sigma_{L}}{\partial\Sigma_{m0}}. (20) The above equation shows that the Jacobian matrix, formed from the first-order derivatives of the covariance matrix elements after one lattice period with respect to their initial values, yields the transfer matrix of the perturbation vector. These derivatives can be computed to machine precision using the automatic differentiation (AD) technique. In this study, we adopt a forward-mode method based on the first-order truncated power series algebra (TPSA) qiangipac25 , also known as the dual-number representation adwiki . This method transforms the computation of a function’s derivatives with respect to its variables into the evaluation of the function using a vector-like differentiable variable, according to specific algebraic rules. Here, the differentiable vector variable F is defined as F=\left(f,f_{x_{1}},f_{x_{2}},\ldots,f_{x_{n}}\right) , where the first element is the function value, and the subsequent elements are the partial derivatives with respect to each variable of interest. The algebraic rules for two differentiable vector variables F and G are as follows: Addition rule: F+G=\left(f+g,\;f_{x_{1}}+g_{x_{1}},\;f_{x_{2}}+g_{x_{2}},\;\ldots,\;f_{x_{n}}+g_{x_{n}}\right) (21) Multiplication rule: F\,G=\left(fg,\;gf_{x_{1}}+fg_{x_{1}},\;gf_{x_{2}}+fg_{x_{2}},\;\ldots,\;gf_{x_{n}}+fg_{x_{n}}\right) (22) Division rule: \frac{F}{G}=\left(\frac{f}{g},\;\frac{f_{x_{1}}g-g_{x_{1}}f}{g^{2}},\;\frac{f_{x_{2}}g-g_{x_{2}}f}{g^{2}},\;\ldots,\;\frac{f_{x_{n}}g-g_{x_{n}}f}{g^{2}}\right) (23) For a scalar function h(F) of a differentiable vector variable F , we have: h(F)=\left(h(f),\;h_{f}\,f_{x_{1}},\;h_{f}\,f_{x_{2}},\;\ldots,\;h_{f}\,f_{x_{n}}\right), (24) where h_{f} denotes the partial derivative of h with respect to f , i.e., h_{f}=\partial h/\partial f . Based on the computational rules described above, we define a new data type for the differentiable vector in the Fortran90 programming language, together with its corresponding operators and common special functions (e.g., exponential and trigonometric functions). These differentiable variables are then used to solve Eq. ( 15 ) numerically in the same manner as conventional double-precision variables, employing a fourth-order Runge-Kutta integration scheme. In contrast to a conventional computer program where the \Sigma variables are declared as double precision, the auto-differentiable program declares them as differentiable variables. While the data type is different, the mathematical expressions and the numerical algorithm remain identical. The initial values of the \Sigma variables are defined as a set of independent differentiable variables (x_{1},x_{2},\cdots x_{21}) . As the integration proceeds, these \Sigma variables evolve as functions of their initial conditions. Consequently, upon completion of the integration, the final \Sigma variables contain not only their function values but also their derivatives with respect to those initial values. The transfer matrix is then directly constructed from these derivatives. Figure 1 : Growth rate amplitudes of unstable envelope modes as a function of the horizontal tune depression, shown both without (embedded plot) and with six-dimensional (6D) coupling, for a longitudinal zero-current phase advance of 60^{\circ} . Figure 2 : Phases of unstable envelope modes as a function of the horizontal tune depression, with coupling and a longitudinal phase advance of 60^{\circ} . As an illustration, we consider a periodic lattice with transverse focusing provided by quadrupole magnets and longitudinal focusing from RF bunchers. For a proton beam with a kinetic energy of 150\,\mathrm{MeV} , the zero-current phase advance per period is approximately 60^{\circ} in the horizontal ( x ) dimension and 145^{\circ} in the vertical ( y ) dimension. Fig. 1 shows the envelope mode growth rate amplitude as a function of the horizontal tune depression, for a longitudinal zero-current phase advance of 60^{\circ} . Without coupling, two instability stopbands are observed: one spanning a depressed phase advance from approximately 0.48 to 0.58 , and another centered around 0.65 . When space-charge-induced coupling is included, two additional instability stopbands appear. One corresponds to the skew mode arising from coupling in the transverse ( x – y ) plane oliver ; yuan , and the other corresponds to the tilt mode resulting from coupling between the transverse and longitudinal planes. These additional stopbands extend over a depressed phase advance range from 0.58 to approximately 0.74 , and exhibit larger growth rate amplitudes than those observed in the uncoupled case. To investigate the mechanism responsible for the instability stopbands, we also plot the unstable mode phases as a function of the horizontal tune depression in Fig. 2 . Without coupling, the instability arises when two modes have the same phase and couple with each other. This type of instability is known as confluent instability or coupled-mode instability . When space-charge-induced coupling is present, an additional mechanism emerges: the unstable skew/tilt mode phase locks at 180^{\circ} . In this case, the envelope modes resonate with the lattice structure, leading to instability. This phenomenon is referred to as a half-integer parametric resonance . Figure 3 : Scan of the transverse x - and y -zero-current phase advances showing the maximum envelope mode growth rate amplitudes without coupling (top) and with coupling (bottom). To further explore the effects of coupling among the three dimensions, we performed two-dimensional zero-current phase advance scans in both the transverse ( x – y ) plane and the transverse-to-longitudinal plane, with and without coupling. Figure 3 shows the results for the maximum envelope mode growth rate amplitude from the x – y plane scan, with the longitudinal zero-current phase advance fixed at 60^{\circ} , a proton beam current of 40\,\mathrm{mA} , and an RF bunching frequency of 650\,\mathrm{MHz} . Two strong instability stopbands are observed near a 100^{\circ} zero-current phase advance in both the coupled and uncoupled cases, corresponding to half-integer resonance. However, when rotation-induced coupling is included, larger instability stopbands appear in the range of approximately 140^{\circ} to 160^{\circ} zero-current phase advance, as well as along the diagonal line in the scan. These coupling-induced stopbands are dominated by the half-integer resonance of the skew/tilt envelope modes. Figure 4 : Scan of the transverse-to-longitudinal zero-current phase advances showing the maximum envelope mode growth rate amplitudes without coupling (top) and with coupling (bottom). We also performed a two-dimensional zero-current phase advance scan between the transverse and longitudinal dimensions. In this case, the zero-current phase advances in the horizontal and vertical dimensions were kept equal. Figure 4 shows the maximum envelope mode growth rate amplitude as a function of the transverse and longitudinal zero-current phase advances. Without coupling, a major instability stopband appears around a 100^{\circ} transverse phase advance, corresponding to a half-integer resonance. This stopband is absent in the longitudinal dimension due to the presence of two identical RF focusing bunchers in a single lattice period. For a zero-current phase advance of about 100^{\circ} through the lattice period, the zero-current phase advance through a single RF buncher is less than 90^{\circ} , preventing the occurrence of a half-integer resonance in the longitudinal plane. In the uncoupled case, a weak instability is observed along the diagonal line of the zero-current phase advance plane. With coupling, this instability region along the diagonal becomes broader and stronger, as the skew/tilt envelope modes driven by direct space-charge effects of rotated beam become unstable in this region. In summary, we have shown that automatic differentiation (AD) can be effectively used to study the stability of complex dynamical systems, such as the dynamics of three-dimensional (3D) envelope modes of a charged particle beam. Application of this technique to the study of 3D envelope instability with full coupling has revealed new unstable regimes in the accelerator lattice parameter space that cannot be predicted by the original uncoupled upright 3D model. I would like to thank Dr. R. D. Ryne for the use of his upright 3D envelope code. This research was supported by the U.S. Department of Energy under Contract No. DE-AC02-05CH11231 and used computer resources at the National Energy Research Scientific Computing (NERSC) Center.