
! *** OPTICS=AD_ACCEL-17JAN24 ***

! ==============================================================================
! Modification History
! ------------------------------------------------------------------------------
! 04-OCT-2023, M. Woodley
!  * lower Ei (energy after CM01) to 90 MeV per F. Zhou and Y. Ding
! ------------------------------------------------------------------------------
! 16-JAN-2021, M. Woodley
!  * new standard configuration per Y. Ding
! ------------------------------------------------------------------------------

! ------------------------------------------------------------------------------
! constants and global parameters
! ------------------------------------------------------------------------------

! constants

  Cb    : CONSTANT=1.0E10/CLIGHT   !kG-m/GeV
  mc2   : CONSTANT=0.51099906E-3   !GeV
  QELEC : CONSTANT=1.602176462E-19 !C
  in2m  : CONSTANT=0.0254          !m/inch

! energy profile

  E0   := MC2+0.750E-3 !NGLS/APEX gun energy (GeV) ... 750 kV gun
  Ei   :=  0.090       !energy at end of L0B (GeV)
  E1   :=  0.250       !BC1 energy (GeV)
  E2   :=  1.500       !BC2 energy (GeV)
  Ef   :=  4.000       !final beam energy (GeV)
  Eu   :=  4.000       !energy in undulator (GeV)
  Emax := 10.000       !maximum beam energy

  Brho0 := Cb*E0
  Brhoi := Cb*Ei
  Brho1 := Cb*E1
  Brho2 := Cb*E2
  Brhof := Cb*Ef
  Brhou := Cb*Eu

! compression

  R56HTR := 0.0035 !DXmax = 75 cm
  R56BC1 := 0.053
  R56BC2 := 0.045

  R56CCDLU := 9.99E-5
  R56CCDLD := 9.99E-5

! ------------------------------------------------------------------------------
! input beam definition: F. Zhou (100 pC)
! ------------------------------------------------------------------------------

! Twiss at gun

  BX0 := 26.477864273474
  AX0 := 14.164100457582
  BY0 := 26.444176998257
  AY0 := 14.145835881151

! Twiss at S= 15.0 m (ASTRA MARKER)

  BXa := 13.325594260675
  AXa := -2.125811087318
  BYa := 13.315541277701
  AYa := -2.123153832086

! Twiss at BEAM0 MARKER (specified)

  BXi :=  9.3480
  AXi := -1.6946
  BYi :=  9.3429
  AYi := -1.6925

! max beta u/s of WS0H04

  Bmax := 50

! beta at WS0H04 waist

  B0H04w := 5.0

! approximate beam parameters

  QBNCH  := 100.0E-12 !C
  EMITXn := 0.35E-06  !m
  EMITYn := 0.35E-06  !m
  sig_z  := 1.0E-03   !m
  sig_dp := 0.03E-02  !1

! ------------------------------------------------------------------------------
! input beam definition (at BEAM0)
! ------------------------------------------------------------------------------

  EMITX := EMITXn/(Ei/EMASS)
  EMITY := EMITYn/(Ei/EMASS)

  GXi   := (1+AXi*AXi)/BXi
  SIG11 := EMITX*BXi
  SIG21 := -EMITX*AXi
  SIG22 := EMITX*GXi
  C21   := SIG21/SQRT(SIG11*SIG22)
  GYi   := (1+AYi*AYi)/BYi
  SIG33 := EMITY*BYi
  SIG43 := -EMITY*AYi
  SIG44 := EMITY*GYi
  C43   := SIG43/SQRT(SIG33*SIG44)

 !SIGX = SQRT(SIG11)
 !SIGPX= SQRT(SIG22)
 !R21  = C21
 !SIGY = SQRT(SIG33)
 !SIGPY= SQRT(SIG44)
 !R43  = C43
 !SIGT = sig_z
 !SIGPT= sig_dp

! ------------------------------------------------------------------------------
! survey (Z values are w.r.t. start of LI01 where Z=0)
! ------------------------------------------------------------------------------

  Xgun  :=    0.28     !beam axis (w.r.t. original linac axis)
  Ygun  :=   -0.99     !beam axis (w.r.t. original linac axis)
  Zgun  :=  -10.044667 !10.0 m d/s of location of CID gun
  Z0beg :=   -7.992090 !start of L0 (12.0 m d/s of location of CID gun)
  Zi    :=    3.914190 !BEAM0 point (~3.9 m into LI01)
  Za    :=   Zgun+15.0 !ASTRA treaty point (~5.0 m into LI01)
  Z0end :=    7.880000 !end of L0 (~7.9 m into LI01)
  Zhtr  :=   18.202600 !start of laser heater chicane (~18.2 m into LI01)
  Zdg0  :=   32.183970 !start of DIAG0 (~32.2 m into LI01)
  Z1beg :=   75.906000 !start of L1 (~75.9 m into LI01)
  Z1loc :=  104.194330 !center of CAVC011 (should be 104.0 m)
  Z1end :=  119.419000 !end of L1 (~17.8 m into LI02)
  Z2beg :=  177.047000 !start of L2 (~75.4 m into LI02)
  Z2end :=  329.605000 !end of L2 (~24.8 m into LI04)
  Z3beg :=  388.210000 !start of L3 (~83.4 m into LI04)
  Z3end :=  641.320000 !end of L3 (~31.7 m into LI07)
  Zdog  :=  664.310000 !entrance first dogleg bend, BRB1 (~43.1 m into LI10)
  Xbyp  :=   -0.650494 !QBP13 X is |Xbyp| to the south of linac axis
  Ybyp  :=    0.649478 !QBP13 Y is Ybyp above linac axis
  Zbyp  := 1254.531303 !QBP13 centerline (~35.3 m into LI13)

! ------------------------------------------------------------------------------
! twiss parameter definitions (for matching, etc.)
! ------------------------------------------------------------------------------

! "treaty" Twiss at center of laser heater undulator (at element HTRUNDB)

  BXlh := 10.0
  AXlh := 0
  BYlh := 10.0
  AYlh := 0

! matched Twiss at start of coasting linac FODOs
! (see CM.xsif for CMfodo1, CMfodo2, and CMfodo3 definitions)

  BXCMfodo1 := 47.734300019352 !L1: 45 degrees per cell
  BYCMfodo1 := 21.362810104773 !L1: 45 degrees per cell
  BXCMfodo2 := 61.483042301615 !L2: 30 degrees per cell
  BYCMfodo2 := 36.259471142003 !L2: 30 degrees per cell
  BXCMfodo3 := 61.483042301615 !L3: 30 degrees per cell
  BYCMfodo3 := 36.259471142003 !L3: 30 degrees per cell

! matched Twiss at SXR LTU emittance diagnostic wire scanners
! (at elements WS31b, WS32b, WS33b, and WS34b)

  BXEDs := 46.225914318019
  AXEDs := -1.084608326468
  BYEDs := 46.225914318019
  AYEDs :=  1.084608326468

! match into HXR LTU emittance diagnostic section (at element WS31)

  BXEDh := 46.225914290403
  AXEDh := -1.084608326581
  BYEDh := 46.225914290403
  AYEDh :=  1.084608326581

! initial Twiss for SXR dogleg (at element DBMARK34B)
! (found by setting ALFX=ALFY=0 at centers of SXR dogleg quadrupoles QDL12-QDL18)

  MBETXS :=  6.841330859726
  MALFXS := -0.28404741292
  MBETYS := 47.120432391261
  MALFYS :=  2.555848494222

! periodic Twiss in HXR dogleg cell (at element MM1)
! (found by setting ALFX=ALFY=0 at centers of HXR dogleg quadrupoles QDL31,32,33 and QT12,22,32)
! (NOTE: this is an inexact match ... see SUBROUTINE MDL2M2 in LCLS2sc_match.mad8)

  MBETXH := 48.911502413792
  MALFXH :=  3.141736059434
  MBETYH := 92.803659415654
  MALFYH :=  3.519158552438

! Twiss at start of HXR cell #24 (for matching without full complement of undulators)
! (full complement of undulators; Yuri match)

  ENHXRm :=  4.0
  BXHXRm := 12.974739845251
  AXHXRm :=  1.498073135623
  BYHXRm :=  6.123426356819
  AYHXRm := -0.747453073035

! ------------------------------------------------------------------------------
! load lattice definitions
! ------------------------------------------------------------------------------

  CALL, FILENAME="common.xsif"
  CALL, FILENAME="CM.xsif"
  CALL, FILENAME="DIAG0.xsif" !must come before INJ.xsif
  CALL, FILENAME="INJ.xsif"
  CALL, FILENAME="BC1.xsif"
  CALL, FILENAME="BC2.xsif"
  CALL, FILENAME="EXT.xsif"
  CALL, FILENAME="DLBM.xsif"
  CALL, FILENAME="SPRD.xsif"
  CALL, FILENAME="CUSXR.xsif"
  CALL, FILENAME="ALINE.xsif"
  CALL, FILENAME="BYP.xsif"
  CALL, FILENAME="LTU.xsif"
  CALL, FILENAME="UND.xsif"
  CALL, FILENAME="SFT.xsif"
  CALL, FILENAME="SXTES.xsif"
  CALL, FILENAME="DASEL.xsif"

  LCLS2scI : LINE=(BEAM0,DCM4b,QCM01_full,DCM5,CM01END,&
    DCMCM1,HOMCM,DCAP0da,ASTRA,DCAP0db,FC1,DMSC0da,BLF1,DMSC0db,&
    VGL0Bd,DMSC0dc,VPL0Bd,DMSC0dd,MSC0d,VVL0Bd,ENDL0B)
  LCLS2scC : LINE=(HTR,COL0,L1,BC1,COL1,L2,BC2,EMIT2,L3,EXT,DLBM)

! new BSY area definitions

  CALL, FILENAME="BSYsc.xsif"

! beam path definitions

 !SC_SXR   : superconducting linac to e- SXR dump
 !SC_SFTS  : superconducting linac to e- SXR safety dump
 !SC_S2_X  : superconducting linac to SXR XTES "2.X" line
 !SC_STXI  : superconducting linac to SXR XTES TXI line
 !SC_STMO  : superconducting linac to SXR XTES TMO line
 !SC_HXR   : superconducting linac to e- HXR dump
 !SC_BSYD  : superconducting linac to BSY dump
 !SC_DIAG0 : superconducting linac to DIAG0
 !SC_DASEL : superconducting linac to End Station A (DASEL)
 !SC_EIC   : superconducting linac Early Injector Commissioning

  SC_SXR   : LINE=(GUN,L0,LCLS2scC,LCLS2scS)
  SC_SFTS  : LINE=(GUN,L0,LCLS2scC,LCLS2scSS)
  SC_S2_X  : LINE=(GUN,L0,LCLS2scC,LCLS2scS2_X)
  SC_STXI  : LINE=(GUN,L0,LCLS2scC,LCLS2scSTXI)
  SC_STMO  : LINE=(GUN,L0,LCLS2scC,LCLS2scSTMO)
  SC_HXR   : LINE=(GUN,L0,LCLS2scC,LCLS2scH)
  SC_BSYD  : LINE=(GUN,L0,LCLS2scC,LCLS2scD)
  SC_DIAG0 : LINE=(GUN,L0,HTR,DIAG0)
  SC_DASEL : LINE=(GUN,L0,LCLS2scC,LCLS2scDA)
  SC_EIC   : LINE=(EIC)

! for Twiss starting at BEAM0

  SC_SXRi   : LINE=(LCLS2scI,LCLS2scC,LCLS2scS)
  SC_SFTSi  : LINE=(LCLS2scI,LCLS2scC,LCLS2scSS)
  SC_S2_Xi  : LINE=(LCLS2scI,LCLS2scC,LCLS2scS2_X)
  SC_STXIi  : LINE=(LCLS2scI,LCLS2scC,LCLS2scSTXI)
  SC_STMOi  : LINE=(LCLS2scI,LCLS2scC,LCLS2scSTMO)
  SC_HXRi   : LINE=(LCLS2scI,LCLS2scC,LCLS2scH)
  SC_BSYDi  : LINE=(LCLS2scI,LCLS2scC,LCLS2scD)
  SC_DIAG0i : LINE=(LCLS2scI,HTR,DIAG0)
  SC_DASELi : LINE=(LCLS2scI,LCLS2scC,LCLS2scDA)

! ------------------------------------------------------------------------------
! initial BSY coordinates at BEGSP (LTUSPLIT)
! ------------------------------------------------------------------------------

  Xf     :=  XOFF
  Yf     :=  1.899339708719 !1.897939159495 -0.3*SIN(2*AVB)
  Zf     := -267.7180471432 !-267.4180504124 -0.3*COS(2*AVB)
  THETAf :=  0
  PHIf   :=  2*AVB
  PSIf   :=  0

! ------------------------------------------------------------------------------

  RETURN
