184 lines
3.5 KiB
Python
184 lines
3.5 KiB
Python
from math import *
|
|
from random import gauss, seed, random
|
|
|
|
from os import system
|
|
|
|
class BeamDynamics:
|
|
|
|
def __init__(self):
|
|
# SLS2 SR parameters (can be changed for the booster or other ring)
|
|
self.Circumference=288.0
|
|
self.BeamEnergy=2.7e9
|
|
self.Vrf=1.44e6
|
|
self.Vrf=1.75e6
|
|
|
|
self.frf=500e6
|
|
self.U0=688e3
|
|
self.alpha=1.05e-4
|
|
|
|
self.EnergySpread=1.16e-3
|
|
|
|
self.c=2.99792458e8
|
|
|
|
|
|
def BoosterParameter(self):
|
|
self.Circumference=270.0
|
|
self.BeamEnergy=2.7e9
|
|
self.Vrf=0.5e6
|
|
self.frf=500e6
|
|
self.U0=373e3
|
|
self.alpha=5e-3
|
|
|
|
self.EnergySpread=0.844e-3
|
|
|
|
def SLSparameter(self):
|
|
|
|
self.Circumference=288.0
|
|
self.BeamEnergy=2.4e9
|
|
self.Vrf=2.4e6
|
|
self.frf=500e6
|
|
self.U0=549e3
|
|
self.alpha=6e-4
|
|
|
|
self.EnergySpread=0.88e-3
|
|
|
|
|
|
|
|
def SynchronousPhase(self, Degree=False):
|
|
|
|
|
|
ph=asin(self.U0/self.Vrf)
|
|
|
|
if Degree:
|
|
ph=ph*180/pi
|
|
|
|
return ph
|
|
|
|
def SynchrotronTune(self):
|
|
|
|
|
|
phis=self.SynchronousPhase()
|
|
|
|
T0=self.Circumference/self.c
|
|
frev=1/T0
|
|
|
|
h=self.frf/frev
|
|
|
|
q2=h*self.alpha*self.Vrf*cos(phis)/2/pi/self.BeamEnergy
|
|
q=sqrt(q2)
|
|
|
|
return q
|
|
|
|
def BucketHeight(self):
|
|
|
|
phis=self.SynchronousPhase()
|
|
|
|
T0=self.Circumference/self.c
|
|
frev=1/T0
|
|
|
|
h=self.frf/frev
|
|
|
|
F2=cos(phis)-(pi-2*phis)/2*sin(phis)
|
|
b2=2*self.Vrf/pi/self.BeamEnergy/h/self.alpha*abs(F2)
|
|
|
|
b=sqrt(b2)
|
|
|
|
return b
|
|
|
|
|
|
def BunchLength(self,mm=True):
|
|
|
|
|
|
q=self.SynchrotronTune()
|
|
T0=self.Circumference/self.c
|
|
|
|
w=2*pi*q/T0
|
|
|
|
if mm:
|
|
bl=self.alpha*self.c*self.EnergySpread/w
|
|
else:
|
|
bl=self.alpha*self.c*self.EnergySpread/w/self.c
|
|
|
|
|
|
|
|
return bl
|
|
|
|
def generateBunch(self,InDic,File=None,Format='plain'):
|
|
|
|
|
|
# InDic keys are
|
|
# bx
|
|
# by
|
|
# ax
|
|
# ay
|
|
# ex: h emittance (m)
|
|
# ey
|
|
# l: bunch length (mm)
|
|
# d: energy spread(fractional)
|
|
# N: number of particle
|
|
|
|
|
|
bx=InDic['bx']
|
|
by=InDic['by']
|
|
ax=InDic['ax']
|
|
ay=InDic['ay']
|
|
|
|
ex=InDic['ex']
|
|
ey=InDic['ey']
|
|
d=InDic['d']
|
|
|
|
|
|
s=InDic['l']
|
|
|
|
Mx11=sqrt(bx)
|
|
Mx12=0.0
|
|
Mx21=-ax/sqrt(bx)
|
|
Mx22=1.0/sqrt(bx)
|
|
|
|
My11=sqrt(by)
|
|
My12=0.0
|
|
My21=-ay/sqrt(by)
|
|
My22=1.0/sqrt(by)
|
|
|
|
|
|
if File:
|
|
fout=open(File,'w')
|
|
else:
|
|
B=[]
|
|
|
|
for i in range(0,InDic['N']):
|
|
ph=2*pi*random()
|
|
amp=2*ex*log(1.0/(1.0-random()))
|
|
xt=sqrt(amp)*cos(ph)
|
|
xpt=sqrt(amp)*sin(ph)
|
|
|
|
x=Mx11*xt+Mx12*xpt
|
|
xp=Mx21*xt+Mx22*xpt
|
|
|
|
ph=2*pi*random()
|
|
amp=2*ey*log(1.0/(1.0-random()))
|
|
yt=sqrt(amp)*cos(ph)
|
|
ypt=sqrt(amp)*sin(ph)
|
|
|
|
y=My11*yt+My12*ypt
|
|
yp=My21*yt+My22*ypt
|
|
|
|
|
|
ph=2*pi*random()
|
|
amp=2*log(1.0/(1.0-random()))
|
|
ss=sqrt(s*1e-12*amp)*cos(ph)
|
|
p=sqrt(d*amp)*sin(ph)
|
|
|
|
if File:
|
|
wline=str(x)+' '+str(xp)+' '+str(y)+' '+str(yp)+' '+str(ss)+' '+str(p)+'\n'
|
|
fout.write(wline)
|
|
else:
|
|
B.append([x,xp,y,yp,ss,p])
|
|
|
|
if File:
|
|
fout.close()
|
|
else:
|
|
return B
|
|
|
|
return
|