! FIRST FIND PERIODIC SOLUTION IN FODO CELL
!&run_setup
!  lattice="fodo.lte",    
!  use_beamline="fodo1"
!  parameters = "%s.param"
!  p_central= 1000
!  &end

!&run_control
!        n_steps = 1
!&end

!&optimization_setup
!        mode = minimize, method = simplex,
!        target = 1e-10, tolerance = 1e-12,
!        n_passes = 3, n_evaluations = 1500,
!        n_restarts = 2,
!&end

!&optimization_variable name=Q1fdh, item=K1, lower_limit=-80, upper_limit=80, step_size=0.1 &end
!&optimization_variable name=Q2fdh, item=K1, lower_limit=-80, upper_limit=80, step_size=0.1 &end
	
!&optimization_term term = "nux 0.125 .00001 sene" &end
!&optimization_term term = "nuy 0.125 .00001 sene" &end

!&twiss_output
!  matched = 1,            ! periodic
!  concat_order = 1,       ! for speed
!  output_at_each_step = 1,
!  filename="%s.twi" 
!&end

!&run_control
!        n_steps = 1
!&end


!&bunched_beam  &end


!&optimize summarize_setup=1 &end

!&save_lattice filename = %s.new &end

!&track
!&end


! NOW RUN EVERYTHING
&run_setup
!  random_number_seed = 0
  lattice="fodo.new",    
  use_beamline="fodolatt"
  sigma="%s.s",
  final="%s.fin",
  output="%s.out",
  magnets="%s.mag"
  centroid="%s.cen"
 ! p_central= 3.502357e+02
 ! p_central= 4.902357e+02
rootname = <rootname>
p_central = <p_central>
&end

&run_control &end

&twiss_output
        matched = 0,            ! periodic
        alpha_x= 0
	beta_x = 2.652125e+00
	alpha_y= 0
	beta_y = 5.790616e+00
        concat_order = 1,       ! for speed
!        output_at_each_step = 1,
        filename="%s.twi" 
&end

!&alter_elements
!	name=DHINFODO
!	item=LSC
!	value=0
!&end


&bunched_beam 
  bunch                    = %s.bun,
  matched_to_cell = fodo1
  n_particles_per_bunch=100000,
  sigma_s = 8.7735e-6
  sigma_dp = 1e-4
  emit_nx = 1e-7,
  emit_ny = 1e-7, 
  use_twiss_command_values = 1,
  distribution_type[0]     = "gaussian",
  distribution_type[1]     = "gaussian",
  distribution_type[2]     = "gaussian",
  distribution_cutoff[0]   = 3.0,
  distribution_cutoff[1]   = 3.0,
  distribution_cutoff[2]   = 3.0,
  symmetrize               = 1,
  enforce_rms_values[0]    = 1,
  enforce_rms_values[1]    = 1,
  enforce_rms_values[2]    = 1,
&end

!&sdds_beam
! input = "test1.bun"
!   input_type = "elegant"
!&end

&track &end



