  A continuous system of moment equations is introduced that models the
transverse dynamics of a beam of charged particles as it passes through an
arbitrary lattice of quadrupoles and solenoids in the presence of self-fields.
Then, figures of merit are introduced specifying system characteristics to be
optimized. The resulting model is used to optimize the parameters of the
lattice elements of a flat to round transformer, as could be applied in
relativistic electron cooling. Results are shown for a case of no space-charge
and two cases with space-charge. The optimization is based on a gradient
descent algorithm in which the gradient is calculated using adjoint methods
that prove to be very computationally efficient. In the optimized
configurations for each of the three cases, the beam in the solenoid is round
and with uniform second moments.
