! Step 1. Use moments_output to quickly calculate the RMS beam parameters along the beamline, then optimize them

!&run_setup
! lattice = BCopt.new,
! use_beamline = myline,
! final = %s.fin,
! parameters = %s.par,
! magnets = %s.mag,
! combine_bunch_statistics = 0,
! default_order = 2,
! concat_order = 0,
! print_statistics = 0,
! random_number_seed = 9876543210,
! p_central = 3131.083, !at BC2 beg
! tracking_updates = 1
!&end

!&run_control
! n_steps = 1,
! reset_rf_for_each_step = 1
!&end

!&optimization_setup
!        n_evaluations = 500, n_passes = 3, n_restarts = 2,
!        target = 1e-6, tolerance = 1e-12,
!        method = "simplex"
!&end

!&moments_output
!        output_at_each_step = 1
! emit_x = "(3.721128e-007 3131.083 /)",
! emit_y = "(3.623049e-007 3131.083 /)",
! beta_x = 1.102110e+002,
! alpha_x = 4.791224e+000,
! beta_y = 2.797170e+001,
! alpha_y = -1.475479e+000,
! ! ez = Ss*Sdelta
! emit_z = "(3.751812e-003 1.485751e-004 *)",
! ! betaz = Ss/Sdelta
! beta_z = "(1.485751e-004 3.751812e-003 /)",
! ! alphaz = -chirp*betaz
! alpha_z = "(60 1.485751e-004 3.751812e-003 / * chs)",
! equilibrium = 0
! matched = 0
! radiation = 0
!&end

!&optimization_term
!        term = "MEND#1.s55m sqrt 8e-6 - 1e6 * sqr" &end

!&optimization_variable
!        name = BX21, item = ANGLE, lower_limit=-0.1, upper_limit=0, step_size = 1e-3 &end
!&optimization_covariable
!        name = BX21, item=E2, equation=BX21.ANGLE &end
!&optimization_covariable
!        name = BX22, item=ANGLE, equation="BX21.ANGLE chs" &end
!&optimization_covariable
!        name = BX22, item=E1, equation=BX22.ANGLE &end
!&optimization_covariable
!        name = BX23, item=ANGLE, equation="BX21.ANGLE chs" &end
!&optimization_covariable
!        name = BX23, item=E1, equation=BX23.ANGLE &end
!&optimization_covariable
!        name = BX24, item=ANGLE, equation=BX21.ANGLE &end
!&optimization_covariable
!        name = BX24, item=E1, equation=BX24.ANGLE &end
!
!&optimization_variable
!        name = CHIRP, item=VOLT, lower_limit=-1e9, upper_limit=1e6, step_size=1e3 &end
!
!&bunched_beam &end
!
!&optimize &end
!
!&save_lattice filename = %s.new &end

! Step 2. Track to verify

&run_setup
 lattice = BCopt.new,
 use_beamline = myline,
 output = %s.out,
 centroid = %s.cen,
 sigma = %s.sig,
 final = %s.fin,
 parameters = %s.par,
 magnets = %s.mag,
 combine_bunch_statistics = 0,
 default_order = 2,
 concat_order = 0,
 print_statistics = 0,
 random_number_seed = 9876543210,
 p_central = 10, !at BC2 beg
 tracking_updates = 1
&end

&run_control
 n_steps = 1,
 !n_indices = 1,
 reset_rf_for_each_step = 1
&end

&twiss_output
 filename = %s.twi,
 matched = 0,
 beta_x = 1.102110e+002,
 alpha_x = 4.791224e+000,
 beta_y = 2.797170e+001,
 alpha_y = -1.475479e+000,
 concat_order = 3
&end

&bunched_beam
 bunch = %s.bun,
 n_particles_per_bunch = 10000,
 time_start = 0,
 use_twiss_command_values = 1,
 emit_nx = 3.721128e-007,
 emit_ny = 3.623049e-007,
 sigma_dp = 3.751812e-003,
 sigma_s = 1.485751e-004,
 dp_s_coupling = 0,
 distribution_type[0] = "gaussian",
 distribution_type[1] = "gaussian",
 distribution_type[2] = "gaussian",
 distribution_cutoff[0] = 4.0,
 distribution_cutoff[1] = 4.0,
 distribution_cutoff[2] = 2.0,
 enforce_rms_values[0] = 1,
 enforce_rms_values[1] = 1,
 enforce_rms_values[2] = 1,
 one_random_bunch = 1,
 symmetrize = 0,
 centroid[0] = 0,
 centroid[1] = 0,
 centroid[2] = 0,
 centroid[3] = 0,
 centroid[4] = 0,
 centroid[5] = 0,
 !momentum_chirp = 300,
&end

&track &end
