lintrack.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from pytpsa import *
  2. from numpy import sign
  3. quad=polmap(x='x',px='px+kn1l*x',y='y',py='py-kn1l*y')
  4. bend=polmap(x='x+l*px+l*angle*dpp/2.',px='px+angle*dpp',y='y+l*py',py='py')
  5. drift=bend(a=0)
  6. def tmatrix(m):
  7. return m.matrix(vars='x px y py dpp'.split())
  8. def pbeta(r11,r12,r21,r22):
  9. cmu=(r11+r22)/2.
  10. try:
  11. smu=sign(r12.zero()) * sqrt(-r12*r21 - (r11-r22)**2 / 4.)
  12. mu=.5/pi * atan(smu/cmu)
  13. ismu=1/smu
  14. except ZeroDivisionError:
  15. mu=pol('mu')
  16. smu=pol('smu')
  17. ismu=pol('ismu')
  18. bet=r12*ismu
  19. alf=.5*(r11-r22)*ismu
  20. return bet,alf,cmu,mu
  21. def pdisp(r11,r12,r21,r22,r16,r26):
  22. det==r11*r22-r12*r21
  23. d=(-r16*r22+r26*r12) / det
  24. dp=(-r26*r11+r16*r21) / det
  25. return d,dp
  26. def twiss(m):
  27. r=tmatrix(m)
  28. betx,alfx,cmux,mux=pbeta(r[0][0],r[0][1],r[1][0],r[1][1])
  29. bety,alfy,cmuy,muy=pbeta(r[2][2],r[2][3],r[3][2],r[3][3])
  30. return betx,alfx,cmux,mux,bety,alfy,cmuy,muy
  31. def tfquad(l,kn0l,ks0l,kn1l):
  32. kx=(+kn1l/l+(kn0l/l)**2)
  33. skx=sqrt(kx)
  34. cx=cos(skx*l)
  35. sx=sin(skx*l)/skx
  36. dx =(1-cx)/kx
  37. fx =(l-sx)/kx
  38. ky=(-kn1l/l+(ks0l/l)**2)
  39. sky=sqrt(ky)
  40. cy=cos(sky*l)
  41. sy=sin(sky*l)/sky
  42. dy =(1-cy)/ky
  43. m=polmap()
  44. m.x =pol('x+cx*x+sx*px+dx*kn0l/l*dpp')
  45. m.px=pol('px-kx*sx*x+cx*px+sx*kn0l/l*dpp')
  46. m.y =pol('y+cy*y+sy*py')
  47. m.px=pol('py-ky*sy*y+cy*px')