Nonlinear dynamics optimization based on minimizing resonance driving terms along a storage ring