parameter[lattice]                = g_2
parameter[particle]               = antimuon
parameter[geometry] = closed
parameter[p0c]                    = 3.094353005E9
parameter[absolute_time_tracking] = .true.
bmad_com[aperture_limit_on]       = .true.


beginning[beta_a]   = 63.916   ! these values give betax=1.0, betay=9.0 in inflector with Wuzeng inflector map
beginning[beta_b]   = 3.6027
beginning[alpha_a] = 19.567
beginning[alpha_b] = 3.1574

beginning[beta_a]   = 6.3228E+01    ! these values give betax=4.0, betay=16.0 in inflector with Wuzeng inflector map
beginning[beta_b]   = 5.9635E+00  
beginning[alpha_a] = 1.7864E+01  
beginning[alpha_b] = 5.5922E+00 

beginning[eta_x] = 14.262   !these give eta x = 4.0 in inflector
beginning[etap_x] = -3.9241

!parameter[ran_seed]               = 123
parameter[custom_attribute2]      = "sbend::field_index"
parameter[custom_attribute1]      = "sbend::kick_width"

parameter[custom_attribute1]      = "sbend::kick_width"
parameter[custom_attribute2]      = "sbend::field_index"
parameter[custom_attribute3]      = "taylor::x_angle"
parameter[custom_attribute4]      = "sbend::quad_voltage"

!=================================	 
! MAGIC RADIUS	 
!=================================	 
 	 
radius = 7.112	 
x0 = 7.112	 
 	 
!=================================	 
! OPTIONAL ELEMENTS	 
!=================================	 
 	 
! to include the calorimeter markers in the lattice, uncomment the following line	 
!call, file = calorimeter_lattice.
!call, file = fiber_monitor_lattice.

!====================================================================================================
!  INJECTION LINE  ==================================================================================
!====================================================================================================

!call, file = bmad_grid_fringe_inf.bmad

!*********************************************
! ELECTRIC QUADRUPOLES GEOMETRICAL PARAMETERS
!*********************************************

! The following come from the E821 Quad NIM 
! paper and the E821 Design Report, pp. 227.

!==================================
! ELECTRODE SPANNING ANGLES
!==================================
 dThetaQShort = 13.0*pi/180.
 dThetaQGap   =  4.0*pi/180.
 dThetaQLong  = 26.0*pi/180.

!==================================
! ELECTRODE START ANGLES
!==================================
!ThetaQ1Start = 21.900*pi/180. ! design report (old)
 ThetaQ1Start = 22.075*pi/180. ! geometry -- no overlap with kickers
 ThetaQ2Start = ThetaQ1Start + pi/2.
 ThetaQ3Start = ThetaQ1Start + pi
 ThetaQ4Start = ThetaQ2Start + pi

!==================================
! ELECTRODE END ANGLES
!==================================
 ThetaQ1End   = ThetaQ1Start + dThetaQShort + dThetaQGap + dThetaQLong
 ThetaQ2End   = ThetaQ1End   + pi/2.
 ThetaQ3End   = ThetaQ1End   + pi
 ThetaQ4End   = ThetaQ2End   + pi

 width2 = 0.045*0.045

!*********************************************
! KICKER GEOMETRICAL PARAMETERS
!*********************************************

 kickerArrayCenterline = pi/2.


!==================================
! KICKER SPANNING ANGLES
!==================================
 dThetaK1     =  1.27/7.112 !   1.76e0/7.112
 dThetaGapK12 = (0.10 + 1.76-1.27)/7.112
 dThetaK2     =  1.27/7.112 !1.76e0/7.112
 dThetaGapK23 = (0.10+1.76-1.27)/7.112
 dThetaK3     = 1.27/7.112  !1.76e0/7.112

!==================================
! KICKER START ANGLES
!==================================
 ThetaK1Start = kickerArrayCenterline - dThetaK2/2. - dThetaGapK12 - dThetaK1
 ThetaK2Start = kickerArrayCenterline - dThetaK2/2.
 ThetaK3Start = kickerArrayCenterline + dThetaK2/2. + dThetaGapK23

!==================================
! KICKER END ANGLES
!==================================
 ThetaK1End   = kickerArrayCenterline - dThetaK2/2. - dThetaGapK12
 ThetaK2End   = kickerArrayCenterline + dThetaK2/2.
 ThetaK3End   = kickerArrayCenterline + dThetaK2/2. + dThetaGapK23 + dThetaK3


!*********************************************
! LATTICE ELEMENTS
!*********************************************


! this is the dipole to inherit for custom quads or custom kickers
FULLRINGA: sbend, rho = 7.112, field_calc = custom, tracking_method = runge_kutta, num_steps = 1000, &
                    mat6_calc_method = tracking, ds_step=0.001 !, angle = twopi

radial_field = 1.45*100.e-6
!radial_field=0.

!call, file=quad_curvilinear/quad_curvilinear_grid.bmad
call, file =quads/quad_cartesian_grid.bmad
!call, file = quads/quad_cartesian_multipole.bmad
!call, file = quad_curvilinear/quad_curvilinear_multipole.bmad

! These elements are the "gaps" between the various quads/kickers
AFREE: FULLRING, angle = ThetaQ1Start, aperture_type=elliptical !, l=radius*ThetaQ1Start Gap from injection point to first electric quadrupole
BFREE: FULLRING, angle = ThetaK1Start - ThetaQ1End,  aperture_type = elliptical !,l=radius*(ThetaK1Start - ThetaQ1End)
CFREE: FULLRING, angle = ThetaQ2Start - ThetaK3End, aperture_type = elliptical !, l=radius*(ThetaQ2Start - ThetaK3End)
DFREE: FULLRING, angle = ThetaQ3Start - ThetaQ2End, aperture_type = elliptical !, l=radius*(ThetaQ3Start - ThetaQ2End)
EFREE: FULLRING, angle = ThetaQ4Start - ThetaQ3End, aperture_type = elliptical !, l=radius*(ThetaQ4Start - ThetaQ3End)
EFREE_HALF: FULLRING, angle = (ThetaQ4Start - ThetaQ3End)/2, aperture_type = elliptical!,l=radius*(ThetaQ4Start - ThetaQ3End)/2
FFREE: FULLRING, angle = twopi        - ThetaQ4End-0.001 !, l=radius*(twopi - ThetaQ4End)


! This element is the small gap between the "short" and "long" sections of each "single" electric quadrupole
QFREE: FULLRING, angle = dThetaQGap
QFREE_half: FULLRING, angle = dThetaQGap/2


! Kickers
KICKER1:         FULLRINGA, angle = dThetaK1, descrip = "KICKER_E989_NoRolledEdges.dat", &
                  kick_width = 100.e-9
GAPK12:          FULLRINGA, angle = dThetaGapK12
KICKER2A:        FULLRINGA, angle = dThetaK2/2,  descrip = "KICKER_E989_NoRolledEdges.dat", kick_width = 100.e-9
MARK_CENTERLINE: MARKER! FULLRING, angle = 0
KICKER2B:        FULLRINGA, angle = dThetaK2/2, descrip = "KICKER_E989_NoRolledEdges.dat", kick_width = 100.e-9
GAPK23:          FULLRINGA, angle = dThetaGapK23
KICKER3:         FULLRINGA, angle = dThetaK3,descrip = "KICKER_E989_NoRolledEdges.dat", kick_width = 100.e-9
KICKERS: LINE = (KICKER1, GAPK12, KICKER2A, MARK_CENTERLINE, KICKER2B, GAPK23, KICKER3)
K_free: FULLRING, angle = dthetak1 + dthetagapk12 + dthetak2 + dthetagapk23 + dthetak3 

!*********************************************
! APERTURE LIMITS
!*********************************************

! Elements just downstream of the injection point should NOT have storage-region aperture limits!
afree[x_limit] := .2
afree[y_limit] := 0.1
afree[aperture_at]:= both_ends

!QUAD1_SHORT[x_limit] := 0.102
!QUAD1_SHORT[y_limit] := 0.05
QUAD1_SHORT[wall]= { superimpose = .true., thickness = 0.00001,       &
                     section = { type = wall_start, s = 0.,  v(1)={0.05025,-0.05025}, v(2)={0.05025, 0.05025},&
                                                             v(3)={-0.05025,0.05025}, v(4)={-0.05025,-0.05025} }, &
                     section = { type = wall_end,   s = dThetaQShort*radius+0.00001, v(1)={0.05025,-0.05025}, v(2)={0.05025, 0.05025},&
                                                                             v(3)={-0.05025,0.05025}, v(4)={-0.05025,-0.05025} } }
QUAD1_SHORT[aperture_at] = wall_transition
QUAD1_SHORT[aperture_type] = wall3d

!QUAD1_LONG[x_limit]  := 0.102
!QUAD1_LONG[y_limit]  := 0.050
QUAD1_LONG[wall]= { superimpose = .true., thickness = 0.00001,       &
                     section = { type = wall_start, s = 0.,                  v(1)={0.05025,-0.05025}, v(2)={0.05025, 0.05025}, &
                                                                             v(3)={-0.05025,0.05025}, v(4)={-0.05025,-0.05025} }, &
                     section = { type = wall_end,   s = dThetaQLong*radius+0.00001, v(1)={0.05025,-0.05025}, v(2)={0.05025, 0.05025}, &
                                                                            v(3)={-0.05025,0.05025}, v(4)={-0.05025,-0.05025} } }
QUAD1_LONG[aperture_at] = wall_transition
!QUAD1_LONG[aperture_at] = continuous
QUAD1_LONG[aperture_type] = wall3d

call, file= walls_free.bmad
call, file = walls_quads2-4.bmad


KICKER1[x_limit]  := 0.05 !0.102 ! vac chmbr. better?
KICKER1[y_limit]  := 0.056
!KICKER1[aperture_at] = wall_transition

KICKER2A[x_limit] := 0.102
KICKER2A[y_limit] := 0.056
!KICKER2A[aperture_at] = wall_transition

KICKER2B[x_limit] := 0.102
KICKER2B[y_limit] := 0.056
!KICKER2B[aperture_at] = wall_transition

KICKER3[x_limit]  := 0.102
KICKER3[y_limit]  := 0.056
!KICKER3[aperture_at] = wall_transition

freelimit=0.05
!bfree[x_limit] := freelimit
!bfree[y_limit] := freelimit

!afree[x_limit] := freelimit
!afree[y_limit] := freelimit

cfree[x_limit] := freelimit
cfree[y_limit] := freelimit

dfree[x_limit] := freelimit
dfree[y_limit] := freelimit

efree[x_limit] := freelimit
efree[y_limit] := freelimit

ffree[x_limit] := freelimit
ffree[y_limit] := freelimit


!call, file = x_injection.lat

!*********************************************
! g-2 STORAGE RING
!*********************************************


collimator4: ecollimator, l=0.00001, x_limit=0.045, y_limit=0.045, x_offset=0.0,  y_offset = 0.0
collimator5: ecollimator, l=0.00001, x_limit=0.045, y_limit=0.045, x_offset = 0.0, y_offset = 0.0
collimator6: ecollimator, l=0.00001, x_limit=0.045, y_limit=0.045, x_offset = 0.0, y_offset = 0.0
collimator7: ecollimator, l=0.00001, x_limit=0.045, y_limit=0.045, x_offset = 0.0, y_offset = 0.0
collimator8: ecollimator, l=0.00001, x_limit=0.045, y_limit=0.045, x_offset = 0.0, y_offset = 0.0

QUAD1_SHORT[x_offset]:= 0.0
QUAD1_LONG[x_offset]:=  0.0
QUAD2_SHORT[y_offset]:= 0.0
QUAD2_LONG[y_offset]:=  0.0
QUAD3_SHORT[y_offset]:= 0.0
QUAD3_LONG[y_offset]:=  0.0
QUAD4_SHORT[x_offset]:= 0.0
QUAD4_LONG[x_offset]:=  0.0

!RING: LINE = (afree, QUAD1_SHORT, qfree, QUAD1_LONG, bfree, KICKERS, cfree,  &
!     QUAD2_SHORT, qfree_half,collimator4,qfree_half, QUAD2_LONG,collimator5, dfree, QUAD3_SHORT, qfree, QUAD3_LONG, collimator6, efree_half,&
!      efree_half, QUAD4_SHORT, qfree_half,collimator7,qfree_half, QUAD4_LONG,collimator8, ffree)

!ring with no kickers
RING: LINE = (afree, QUAD1_SHORT, qfree, QUAD1_LONG, bfree, k_free, cfree,  &
     QUAD2_SHORT, qfree_half,collimator4,qfree_half, QUAD2_LONG,collimator5, dfree, QUAD3_SHORT, qfree, QUAD3_LONG, collimator6, efree_half,&
      efree_half, QUAD4_SHORT, qfree_half,collimator7,qfree_half, QUAD4_LONG,collimator8, ffree)

!inj: line = (injection_line, inj_to_ring, ring_br)
!inj_to_ring: patch, x_offset=-0.077  
!ring_br: fork, to_line = ring

use, RING
!use, inj
