!Provided by Weiming Guo (NSLS-II, Brookhaven National Laboratory) on April 20, 2010
! The user needs to modify the sextupole name and strength limits in the command "optimization_variable"
! And prepare a lattice input file that is compatible to Elegant format, with the file name as infile.lte
! To start optimization, type elegant DA-optimize-example.ele -macro=infr=infile
! Optimized lattice is saved in infile-opt.lte
! One can check the infile-opt.log (text file) file for the optimization result of each step
! The sextupole strength change during the optimization process can be found in infile.fin
! For example, one can plot the variation of the sextupole strength using: sddsplot infr.fin -par=Step,*.K2 -graph=line,vary -leg
! This should give at least good on-momentum dynamic aperture. If not, the working point needs to be adjusted.
! Off-momentum is complicated. One needs to adjust the tune footprint to avoid crossing of strong resonances lines


&run_setup
	lattice = "<infr>.lte"
	use_beamline = "ring"
	p_central_mev = 3e3
	default_order = 3,      ! needed to get the second-order chromaticity right
        final = <infr>.fin
        parameters = <infr>.param
&end

&twiss_output
        concat_order = 3,
        higher_order_chromaticity = 1
        higher_order_chromaticity_range = 0.01
        higher_order_chromaticity_points = 5,
        quick_higher_order_chromaticity = 1
        compute_driving_terms = 1,
        output_at_each_step = 1
&end

&run_control &end

&optimization_setup
        tolerance = 0.001,
        target = 2500,
        n_passes = 3,
        n_restarts = 3,
        n_evaluations = 1500,
        log_file = <infr>-opt.log
        verbose = 1
&end

! Weighting factor is multiplied to make all the constraints of the same order of magnitude
! Users may vary the weights for particular cases

! first order geometric terms
! nux
&optimization_term
        term = "h21000 1.e2 * "
&end

! 3nux
&optimization_term
        term = "h30000 1.e2 * "
&end

! nux
&optimization_term
        term = "h10110 1.e2 * "
&end

! nux-2nuy
&optimization_term
        term = "h10020 1.e2 * "
&end

!nux+2nuy
&optimization_term
        term = "h10200 1.e2 * "
&end

!Momentum dependent terms
! dbetax/dp
&optimization_term
        term = "h20001 1.e2 * "
&end

! dbetay/dp
&optimization_term
        term = "h00201 1.e2 * "
&end

! detax/dp
&optimization_term
        term = "h10002 1.e3 * "
&end


!Linear chromaticity targetting value can be set to the desired number
! Frist order chromaticity
&optimization_term
        term = "dnux/dp 0.0 - abs 2.e-3 0.001 segt" &end
&optimization_term
        term = "dnuy/dp 0.0 - abs 2.e-3 0.001 segt" &end

! second order chromaticity
&optimization_term
        term = "dnux/dp2 abs" &end
&optimization_term
        term = "dnuy/dp2 abs" &end

! amplitude tune dependence
&optimization_term
        term = "dnux/dJx 0 - abs 100 1 segt" &end
&optimization_term
        term = "dnux/dJy 0 - abs 50 1 segt" &end
&optimization_term
        term = "dnuy/dJy 0 - abs 100 1 segt" &end


! Users should change the sextupole name and strength limits according to their own lattice
        
&optimization_variable
        name = SH1 item = K2, lower_limit=-30, upper_limit = 35, step_size = 0.1 &end
&optimization_variable
        name = SH2 item = K2, lower_limit=-30, upper_limit = 30, step_size = 0.1 &end
&optimization_variable
        name = SH3 item = K2, lower_limit=-30, upper_limit = 30, step_size = 0.1 &end
&optimization_variable
        name = SH4 item = K2, lower_limit=-30, upper_limit = 30, step_size = 0.1 &end
&optimization_variable
        name = SM1L item = K2, lower_limit=-30, upper_limit = 30, step_size = 0.1 &end

&optimization_variable
        name = SM1R item = K2, lower_limit=-30, upper_limit = 30, step_size = 0.1 &end
&optimization_variable
        name = SM2H item = K2, lower_limit=-30, upper_limit = 30, step_size = 0.1 &end
&optimization_variable
        name = SL3 item = K2, lower_limit=-30, upper_limit = 30, step_size = 0.1 &end
&optimization_variable
        name = SL2 item = K2, lower_limit=-30, upper_limit = 30, step_size = 0.1 &end
&optimization_variable
        name = SL1 item = K2, lower_limit=-30, upper_limit = 30, step_size = 0.1 &end


&bunched_beam &end

&optimize &end


&run_setup
	lattice = "<infr>.lte"
	use_beamline = "ring"
	p_central_mev = 3e3
	default_order = 2,
        magnets = %s.mag,
&end

&load_parameters
        filename = <infr>.param
        change_defined_values = 1
&end

&tune_shift_with_amplitude
        verbose = 1
        turns = 0
&end

&twiss_output
        filename = "%s.twi",
        concat_order = 3,
        higher_order_chromaticity = 1
        higher_order_chromaticity_range = 0.001
        higher_order_chromaticity_points = 3
        compute_driving_terms = 1,
&end


! the optimized lattice is saved in <infr>-opt.lte
&save_lattice
filename = <infr>-opt.lte
&end
