Example of simulation of 24-doublet bunch fill with harmonic cavity in the APS upgrade lattice.
Should work with elegant 27.1 and later.
M. Borland, 2015.

==============
Description of files

lattice.lte
        Lattice file (3PW-V6)

Basic.ele
        Run to create lattice functions for use in setting up other runs.

ring_cdr2014v1.impe1
rw_cdr2014v1.impe1
        Longitudinal impedance files (geometric, RW respectively); from R. Lindberg, APS.

ILMatrixSetup
ILMatrix.xref
        Used to set up the ILMATRIX element for fast tracking.

SREffectsSetup
SREffects.xref
        Used to set up the SREFFECTS element for fast tracking.

makeBunchTrain
makeBunch.ele
        Used to create a bunch train file.
        This is the script to modify if you want a different fill pattern.
        For uniform fill patterns, use the duplication feature of sdds_beam.

AmpFeedbackFilters.sdds
PhaseFeedbackFilters.sdds
        Filters describing the APS RF feedback system; from T. Berenc, APS.

rampTemplate1.ele
        Elegant command file to run the simulation with HHC
        Ramps the charge up over 6000 turns to accommodate the APS rf feedback system.
        A faster ramp may result in beam loss as the feedback fails to keep up.
        The feedback system has a problem at the end of the ramp; the integrator overshoots and
        causes beam oscillations; it might be better to ramp more slowly.

==============
Sequence of operations:

1. Make the Basic.twi file
% elegant Basic.ele

2. Set up the ILMATRIX and SREFFECTS elements
% ILMatrixSetup
% SREffectsSetup

3. Create the beam file (beam-24x2.sdds) and setup file for diagnostics (beam-24x2.param)
% makeBunchTrain

4. Run elegant, e.g., this run does 16k turns for an HHC detuning of 16.5 kHz
% mpiexec -np 96 Pelegant rampTemplate1.ele -macro=rootname=ramp-24x2-16.5kHz,df=16.5e3,turns=16000,beam=beam-24x2.sdds,diagnostics=beam-24x2.param


==============
Output files

Results of Basic.ele run
        Basic.done
        Basic.mag
        Basic.twi

Results of ILMatrixSetup
        ILMatrix.sdds

Results of SREffectsSetup
        SREffects.sdds

Results of makeBunchTrain
        beam-24x2.sdds (beam file)
        beam-24x2.param (diagnostics setup file)

Results of example run
        - Histogram output files
        ramp-24x2-16.5kHz.h00
        ramp-24x2-16.5kHz.h01
        - Turn-by-turn bunch parameter files      
        ramp-24x2-16.5kHz.wp00
        ramp-24x2-16.5kHz.wp01
        - Bunch phase space file (pass>14k)
        ramp-24x2-16.5kHz.wpc
        - rf mode data for harmonic cavity
        ramp-24x2-16.5kHz.rfhm
        - rf mode data for main cavities (1 only)
        ramp-24x2-16.5kHz.rfmm
        - semaphores
        ramp-24x2-16.5kHz.started
        ramp-24x2-16.5kHz.done   <--- Only present if run succeeds
        ramp-24x2-16.5kHz.failed <--- Only present if run fails
        - Twiss data
        ramp-24x2-16.5kHz.twi

==============
Commands for looking at output data. These may be used while the run is in progress; however,
in that case, a command might fail occasionally depending on how your file system works.

Bunch duration for bunches 0 and 1
% sddsplot -column=Pass,St ramp-24x2-16.5kHz.wp0? -graph=line,vary -legend=file,edit=2Z.2d

Bunch momentum spread for bunches 0 and 1
% sddsplot -column=Pass,Sdelta ramp-24x2-16.5kHz.wp0? -graph=line,vary -legend=file,edit=2Z.2d

Bunch time offset for bunches 0 and 1
% sddsplot -column=Pass,dCt -mode=y=offset ramp-24x2-16.5kHz.wp0? -graph=line,vary -legend=file,edit=2Z.2d

Time histograms for bunches 0 and 1 (histograms are centered naively)
% sddsplot -column=dt,dtFrequency ramp-24x2-16.5kHz.h0? -split=page -group=page -separate=page -graph=line,vary \
  -legend=file,edit=2Z.2d -mode=x=meancenter -samescales -title=@Pass,edit="i/Pass:/"

Beam-induced voltage vs time for main cavity (1 of 12), color-coded by bunch number
% sddsplot -column=Pass,V ramp-24x2-16.5kHz.rfmm -graph=sym,vary=subtype,fill,scale=0.5 -split=column=Bunch -order=spectral

Beam-induced voltage vs time for HHC, color-coded by bunch number
% sddsplot -column=Pass,V ramp-24x2-16.5kHz.rfhm -graph=sym,vary=subtype,fill,scale=0.5 -split=column=Bunch -order=spectral
