Example of restarting a simulation of a beam abort that involves RFMODE elements.
Instead of using the RFMODE element to supply the generator voltage, we use a separate
RFCA element. This avoids the problem of reinitializing the IIR filters used for
voltage and phase feedback.

Example by M. Borland, 2024/3

===

cleanUp  
         Script to clean up output files for a fresh start

parTest.lte
        Lattice file. For speed, we use a matrix-based lattice for the small PAR ring.

parTest0.ele
        Generates the initial beam, then tracks for 100k turns to reach a quasi-equilibrium
parTest0.bun
        Initial bunch file output (used for full simulation parTest2)
parTest0.out
        Final bunch file output (used for restart simulation parTest1)
parTest0.rfm
        RFMODE output (used by parTest1)
parTest0.w1
        Turn-by-turn parameter output

parTest1.ele
        Loads particles from end of parTest0; loads RFMODE voltage, phase, and time reference;
        varies imposed rf voltage with decaying exponential to simulate shut-off of the rf source.
        The decay time is 12 us, corresponding to the frequency, unloaded Q, and beta value for the rf mode.
        (tau = 2*Q0/(1+beta)/omega)
parTest1.los
        Lost-particle coordinates output
parTest1.out
        Final phase space output
parTest1.rec
        Record of rf cavity modulation
parTest1.rfm
        RFMODE output
parTest1.w1
        Turn-by-turn parameter output

parTest2.ele
        Runs everything in a single simulation
parTest2.los
        Lost-particle coordinates output
parTest2.out
        Final phase space output
parTest2.rec
        Record of rf cavity modulation
parTest2.rfm
        RFMODE output
parTest2.w1
        Turn-by-turn parameter output

===

Sequence (using bash)
$ for run in 0 1 2 ; do mpiexec -np 18 Pelegant parTest$run.ele; done
$ sddsplot -layout=2,2 -separate -col=Pass,'(Sdelta,St,ex,ey)' parTest0.w1  -title="Tracking to quasi-equilibrium"
$ sddsplot -graph=line,vary -column=Ct,Particles parTest1.w1 -legend=spec="Restart" \
  -column=Ct,Particles parTest2.w1 -filter=col,Pass,100000,1e10 -legend=spec="Single run"
$ sddsplot -graph=line,vary -column=Pass,Particles parTest1.w1 -legend=spec="Restart" \
 -column=Pass,Particles parTest2.w1 -legend=spec="Single run"  -filter=col,Pass,1e5,1e10 -offset=xch=-1e5

Note that the results are not identical because the random number sequences used for quantum excitation aren't
identical, and can't be practially.

Also, note that losses occur at x>0 because PAR dipoles bend to the left.


