Option, ECHO=FALSE;
Option, PSDUMPFREQ=100;
Option, SPTDUMPFREQ = 100;
Option, PSDUMPEACHTURN=FALSE;
Option, PSDUMPLOCALFRAME=FALSE;
Option, ASCIIDUMP=TRUE;

Title,string="3D RF fieldmap test";

Turns = 4;

Edes= 1E-6/1000.0; //GeV
r0  = 8.608;         //mm
pr0 = 0/1000.0; //m_0*c

pxineV=.25;
pyineV=.25;
pzineV=.25;

x0 = 0.083E-3;
y0 = 0.0E-3;
z0 = 0.5E-3;

frequency=72.850;
frequency3=3.0*frequency;
hfphi0=245.2;

ef1=0.8;
ef2=0.8;
ef3=0.8;
ef4=0.5;

gamma=(Edes+PMASS)/PMASS;
beta=sqrt(1-(1/gamma^2));
gambet=gamma*beta;
P0 = gamma*beta*PMASS;
brho = (PMASS*1.0e9*gambet) / CLIGHT;
value,{gamma,brho,Edes,beta,gambet};

COMET2: Cyclotron, TYPE="BANDRF", CYHARMON= 2, PHIINIT= -71.649, PRINIT= pr0, RINIT= r0 , SYMMETRY= 1.0, 
	FMAPFN="fm/BMap_Christian.txt",
	RFPHI=  {hfphi0/180*pi,hfphi0/180*Pi,hfphi0/180*Pi,hfphi0/180*Pi,0.},
        RFFREQ= {frequency,frequency,frequency,frequency,0.},
        RFMAPFN={"fm/ChimneyEB.h5part","fm/PullerEB.h5part","fm/M77EB.h5part",
		 "fm/COMETRF_x850EBc.h5part","fm/VertDef.h5part"},
        ESCALE={ef1,ef2,ef3,ef4,3.0},
	MAXZ=15, MINZ=-15, MINR=0, MAXR= 881.1,
	SUPERPOSE={false,false,false,false,true};

cmphys: SURFACEPHYSICS, TYPE="COLLIMATOR", MATERIAL="Cu";

cma1: CCOLLIMATOR, xstart=8.0200,  xend=11.1382, ystart=22.0300, yend=25.7447, width=1, 
      OUTFN="data/cma1.out"; //, SURFACEPHYSICS=cmphys;
cma2: CCOLLIMATOR, xstart=11.2668, xend=14.4500, ystart=25.8979, yend=29.6900, width=1, 
      OUTFN="data/cma2.out"; //, SURFACEPHYSICS=cmphys;

vc1:  CCollimator,xstart=46.0987, xend=99.7700, ystart=-27.5228, yend=-70.8855, zstart=-15, zend=-11, width=2, 
      OUTFN="data/vc01.dat"; // , SURFACEPHYSICS=cmphys;
vc2:  CCollimator,xstart=46.0987, xend=99.7700, ystart=-27.5228, yend=-70.8855, zstart=11, zend=15, width=2, 
      OUTFN="data/vc02.dat"; // , SURFACEPHYSICS=cmphys;
vc3:  CCollimator,xstart=51.5436, xend=99.7700, ystart=-31.9220, yend=-70.8855, zstart=-11, zend=-3.5, width=2, 
      OUTFN="data/vc03.dat"; // , SURFACEPHYSICS=cmphys;
vc4:  CCollimator,xstart=51.5436, xend=99.7700, ystart=-31.9220, yend=-70.8855, zstart=3.5, zend=11, width=2, 
      OUTFN="data/vc04.dat"; // , SURFACEPHYSICS=cmphys;
vc5:  CCollimator,xstart=51.5436, xend=69.4342, ystart=-31.9220, yend=-46.3764, zstart=-3.5, zend=-3, width=2, 
      OUTFN="data/vc05.dat"; //, SURFACEPHYSICS=cmphys;
vc6:  CCollimator,xstart=51.5436, xend=69.4342, ystart=-31.9220, yend=-46.3764, zstart=3, zend=3.5, width=2, 
      OUTFN="data/vc06.dat"; // , SURFACEPHYSICS=cmphys;
vc7:  CCollimator,xstart=51.5436, xend=60.1000, ystart=-31.9220, yend=-38.8350, zstart=-3, zend=-2.25, width=2, 
      OUTFN="data/vc07.dat"; // , SURFACEPHYSICS=cmphys;
vc8:  CCollimator,xstart=51.5436, xend=60.1000, ystart=-31.9220, yend=-38.8350, zstart=2.25, zend=3, width=2, 
      OUTFN="data/vc08.dat"; // , SURFACEPHYSICS=cmphys;

vcp:  PROBE, xstart=46.0987, xend=99.7700, ystart=-27.5228, yend=-70.8858;

l1:   Line = (COMET2,cma1,cma2,vc1,vc2,vc3,vc4,vc5,vc6,vc7,vc8,vcp);

Dist1:DISTRIBUTION, DISTRIBUTION=gauss,
sigmax=x0, sigmapx=pxineV, corrx=0.0,
sigmay=y0, sigmapy=pyineV, corry=0.0,
sigmaz=z0, sigmapz=pzineV, corrz=0.0;

Fs1:FIELDSOLVER, FSTYPE=NONE, MX=64, MY=64, MT=64, 
		 PARFFTX=true, PARFFTY=true, PARFFTT=false,
		 BCFFTX=open, BCFFTY=open, BCFFTT=open;

beam1: BEAM, PARTICLE=PROTON, pc=P0, SPACECHARGE=false, NPART=200, BCURRENT=1.0E-6,CHARGE=1.0, BFREQ=frequency;

Select, Line=l1;

track,line=l1, beam=beam1, MAXSTEPS=3600*Turns,STEPSPERTURN=3600;
run, method = "CYCLOTRON-T", beam=beam1, fieldsolver=Fs1, distribution=Dist1;
endtrack;

Stop;

