#!/bin/sh  
# \
exec oagwish "$0" "$@"


# set the path to pick up OAG libraries
set auto_path [linsert $auto_path 0  /usr/local/oag/apps/lib/$env(HOST_ARCH)]
set auto_path [linsert $auto_path 0 /usr/local/oag/lib_patch/$env(HOST_ARCH)]

# make the main application frame (.userFrame)
APSApplication . -name sledGain -version 1.0 \
    -overview {This is an example for using oagwish}


# From SLAC-PUB-1453
# M = gamma*Exp(-Ta/Tc)*[1 - (1-g)^(1+nu)]/[g*(1+nu)] - (alpha-1)
# gamma = alpha*(2 - exp(-tau1))
# g = 1- exp(-2*tau1)
# tau1 = T1/Tc (T1 is flip time)
# tau = 0.57
# nu = Ta/Tc/ln(1-g)
# alpha = 2*beta/(1+beta)   (beta is coupling coef)
# Ta = structure filling time
# Tc = cavity filling time

# make a scrolled status widget for communication application
# status and messages.
set status Ready.
APSScrolledStatus .status -parent .userFrame -textVariable status -width 80 -height 4
update

# Accelerator parameters for SLAC constant-gradient sections
set Ta 0.82e-6
set tau 0.57
set f 2856e6
set pi [expr 4*atan(1.0)]

APSFrame .sled -parent .userFrame -label "SLED Cavity Parameters" 
set w .userFrame.sled.frame
set Qo 100000.0
set beta 5.0
APSLabeledEntry .le1 -parent $w -label "Unloaded Q" -textVariable Qo  \
    -contextHelp "Enter the unloaded Q of the SLED cavities"
APSLabeledEntry .le2 -parent $w -label "beta (coupling coef)" -textVariable beta \
    -contextHelp "Enter the coupling coefficient of the SLED cavities"
bind $w.le1.entry <Return> DoCalculations
bind $w.le2.entry <Return> DoCalculations

APSFrame .kly -parent .userFrame -label "Klystron Parameters"
set w .userFrame.kly.frame
set Tk 2.8e-6
set Pk 35e6
APSLabeledEntry .le1 -parent $w -label "Klystron pulse length (s)" -textVariable Tk \
    -contextHelp "Enter the pulse length for the klystron in seconds.  Must be larger than $Ta (structure filling time) to allow for SLED reversal."
APSLabeledEntry .le2 -parent $w -label "Klystron forward power (W)" -textVariable Pk \
    -contextHelp "Enter the forward power from the klystron in watts"
bind $w.le1.entry <Return> DoCalculations
bind $w.le2.entry <Return> DoCalculations

APSFrame .structure -parent .userFrame -label "Structure Parameters"
set w .userFrame.structure.frame
set Ns 4
set powerAttenuation 1.0
APSLabeledEntry .le1 -parent $w -label "Number of structures" -textVariable Ns -contextHelp \
    "Enter the number of linac structures driven by this klystron"
APSLabeledEntry .le2 -parent $w -label "Power attenuation (dB)" -textVariable powerAttenuation \
    -contextHelp "Enter the amount of power attenuation in the transmission waveguide"
bind $w.le1.entry <Return> DoCalculations
bind $w.le2.entry <Return> DoCalculations

APSButton .calc -parent .userFrame -text "Calculate" -command DoCalculations


proc DoCalculations {} {
    global Qo beta f Tk Ta tau pi status Ns Pk powerAttenuation

    if [expr $Tk<$Ta] {
        set status "Error: klystron pulse width must be greater than fill time (0.82 us)"
        return
    }
    if [expr $powerAttenuation<0] {
        set status "Setting negative power attenuation is cheating.  Shame on you."
        return
    }

    set g [expr 1-exp(-2*$tau)]
    set Tc [expr 2*$Qo/(2*$pi*$f*(1+$beta))]
    
    set nu [expr $Ta/$Tc/log(1-$g)]
    set alpha [expr 2*$beta/(1.0+$beta)]

    set T1 [expr ($Tk-$Ta)]
    set tau1 [expr $T1/$Tc]
    set gamma [expr $alpha*(2 - exp(-$tau1))]

    set M [expr $gamma*exp(-$Ta/$Tc)*(1 - pow(1-$g, 1+$nu))/($g*(1+$nu)) - ($alpha-1)]
    set status "Maximum SLED energy gain factor is $M"

    set power [expr $Pk/pow(10.0,$powerAttenuation/10.0)]
    set dE0 [expr $Ns*10.4*sqrt($power/(1e6*$Ns))]
    set status "Maximum energy gain with no SLED is [format %.1f $dE0] MeV"
    set status "Maxmium energy gain with SLED is    [format %.1f [expr $M*$dE0]] MeV"
    update
}




