PageRenderTime 61ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/scripts/hibeam.py

https://bitbucket.org/berkeleylab/warp
Python | 209 lines | 183 code | 7 blank | 19 comment | 14 complexity | 99c72e12d7c9b8812dcea57b03eaebba MD5 | raw file
  1. #!/usr/local/python/bin/python
  2. from warp import *
  3. import namelist
  4. import sys
  5. import getopt
  6. import hibeamlattice
  7. import string
  8. from hibeamdefaults import *
  9. hibeam_version = "$Id: hibeam.py,v 1.4 2004/12/03 23:55:41 dave Exp $"
  10. # --- Get the command line options.
  11. runname = 'test'
  12. latticefile = ''
  13. wirefile = ''
  14. inputfile = ''
  15. for arg in sys.argv[1:]:
  16. mr = re.search('r=(\w*)',arg)
  17. ml = re.search('l=(\w*)',arg)
  18. mw = re.search('w=(\w*)',arg)
  19. mi = re.search('i=(\w*)',arg)
  20. if mr: runname = mr.group(1)
  21. if ml: latticefile = ml.group(1)
  22. if mw: wirefile = mw.group(1)
  23. if mi: inputfile = mi.group(1)
  24. mr = re.search('-r\s(\w*)',arg)
  25. ml = re.search('-l\s(\w*)',arg)
  26. mw = re.search('-w\s(\w*)',arg)
  27. mi = re.search('-i\s(\w*)',arg)
  28. if mr: runname = mr.group(1)
  29. if ml: latticefile = ml.group(1)
  30. if mw: wirefile = mw.group(1)
  31. if mi: inputfile = mi.group(1)
  32. if not inputfile: inputfile = 'in'+runname
  33. # --- Read in the namelist file.
  34. namelist = namelist.NameList(inputfile)
  35. try:
  36. hinit = namelist.namelists['hinit']
  37. except KeyError:
  38. hinit = {}
  39. try:
  40. ztime = namelist.namelists['ztime']
  41. except KeyError:
  42. ztime = {}
  43. for k,v in hinit.iteritems():
  44. v = re.sub('\.f\w*','0',v) # Change .false. to 0
  45. v = re.sub('\.t\w*','1',v) # Change .true. to 1
  46. exec(k+'='+v)
  47. for k,v in ztime.iteritems():
  48. v = re.sub('\.f\w*','0',v) # Change .false. to 0
  49. v = re.sub('\.t\w*','1',v) # Change .true. to 1
  50. exec(k+'='+v)
  51. # --- Parse the lattice file.
  52. line = hibeamlattice.hibeamlattice(latticefile)
  53. ##########################################################################
  54. ##########################################################################
  55. # --- Now the standard WARP input deck.
  56. # --- Set four-character run id, comment lines, user's name.
  57. top.runid = runname[:4]
  58. top.pline2 = runname
  59. top.pline1 = 'Hibeam simulation'
  60. top.runmaker = ' '
  61. # --- Invoke setup routine (THIS IS MANDATORY).
  62. #numticks = 4
  63. setup()
  64. # --- Set input parameters describing the beam, 72 to 17.
  65. # --- Parameters calculated with envelope code ignoring focusing effect
  66. # --- of dipoles.
  67. top.a0 = a
  68. top.b0 = b
  69. top.ap0 = ap
  70. top.bp0 = bp
  71. top.x0 = xoffset
  72. top.y0 = yoffset
  73. top.xp0 = xpoffset
  74. top.yp0 = ypoffset
  75. top.ibeam = current0
  76. top.emit = enorm0
  77. top.vbeam = 0.e0
  78. top.ekin = emev0*1.e6
  79. top.aion = amu
  80. top.zion = charge
  81. top.lrelativ = false
  82. derivqty()
  83. top.vthz = dvzth
  84. # +++ Set up arrays describing lattice.
  85. zz = 0.
  86. iq = -1
  87. idrft = -1
  88. for e in line:
  89. if e.type == 'quad':
  90. iq = iq + 1
  91. top.quadzs[iq] = zz
  92. top.quadze[iq] = zz + e.length
  93. top.quadde[iq] = e.gradient
  94. top.quadap[iq] = e.aperture
  95. top.quadrr[iq] = e.r_elem
  96. top.qoffx[iq] = e.offset_x*errordist(e.error_type)
  97. top.qoffy[iq] = e.offset_y*errordist(e.error_type)
  98. zz = zz + e.length
  99. if iq == top.nquad:
  100. top.nquad = top.nquad + 100
  101. gchange("Lattice")
  102. if e.type == 'hyperb':
  103. iq = iq + 1
  104. top.quadzs[iq] = zz
  105. top.quadze[iq] = zz + e.length
  106. top.quadde[iq] = e.gradient
  107. top.quadap[iq] = -e.aperture
  108. top.qoffx[iq] = e.offset_x*errordist(e.error_type)
  109. top.qoffy[iq] = e.offset_y*errordist(e.error_type)
  110. zz = zz + e.length
  111. if iq == top.nquad:
  112. top.nquad = top.nquad + 100
  113. gchange("Lattice")
  114. elif e.type == 'drift':
  115. idrft = idrft + 1
  116. top.drftzs[idrft] = zz
  117. top.drftze[idrft] = zz + e.length
  118. top.drftap[idrft] = e.aperture
  119. top.drftox[idrft] = e.offset_x*errordist(e.error_type)
  120. top.drftoy[idrft] = e.offset_y*errordist(e.error_type)
  121. zz = zz + e.length
  122. if idrft == top.ndrft:
  123. top.ndrft = top.ndrft + 100
  124. gchange("Lattice")
  125. elif e.type == 'box':
  126. idrft = idrft + 1
  127. top.drftzs[idrft] = zz
  128. top.drftze[idrft] = zz + e.length
  129. top.drftap[idrft] = -e.aperture
  130. top.drftox[idrft] = e.offset_x*errordist(e.error_type)
  131. top.drftoy[idrft] = e.offset_y*errordist(e.error_type)
  132. zz = zz + e.length
  133. if idrft == top.ndrft:
  134. top.ndrft = top.ndrft + 100
  135. gchange("Lattice")
  136. elif e.type == 'wire':
  137. idrft = idrft + 1
  138. top.drftzs[idrft] = zz
  139. top.drftze[idrft] = zz + e.length
  140. top.drftap[idrft] = e.aperture
  141. top.drftox[idrft] = e.offset_x*errordist(e.error_type)
  142. top.drftoy[idrft] = e.offset_y*errordist(e.error_type)
  143. zz = zz + e.length
  144. if idrft == top.ndrft:
  145. top.ndrft = top.ndrft + 100
  146. gchange("Lattice")
  147. # --- Set general lattice variables.
  148. top.zlatperi = 2.*zz
  149. top.zlatstrt = 0.
  150. if iq >= 2:
  151. top.tunelen = 0.5*(top.quadzs[2]+top.quadze[2]-top.quadzs[0]-top.quadze[0])
  152. else:
  153. top.tunelen = top.zlatperi
  154. env.zl = zstart
  155. env.zu = max(zz,zmax)
  156. env.dzenv = zstep0
  157. # +++ Set input parameters describing the 3d simulation.
  158. w3d.nx = nx
  159. w3d.ny = ny
  160. top.ibpush = 0
  161. wxy.ds = zstep0
  162. # --- Set grid size
  163. w3d.xmmin = -xdsize
  164. w3d.xmmax = xdsize
  165. w3d.ymmin = -ydsize
  166. w3d.ymmax = ydsize
  167. top.prwall = w3d.xmmax
  168. # --- Load Semi-Gaussian cigar beam.
  169. top.npmax = npart
  170. w3d.distrbtn = "semigaus"
  171. w3d.xrandom = "digitrev"
  172. w3d.vtrandom = "digitrev"
  173. w3d.vzrandom = "digitrev"
  174. w3d.ldprfile = "polar"
  175. # --- Select plot intervals, etc.
  176. top.nhist = max(1,nint(dzhist/wxy.ds))
  177. top.izplfreq[0:3]=[0,100000,dzphaseplot]
  178. top.zzplfreq[3:3+len(zphaseplot)]=zphaseplot
  179. top.itmomnts[0:4]=[0,1000000,top.nhist,0]
  180. # --- Select plots
  181. top.ipxy[:,0] = always
  182. top.iptrace[:,0] = always
  183. top.icrhoxy[:,0] = always
  184. top.icphixy[:,0] = always
  185. # --- open a window
  186. #winon()
  187. # --- Run the envelope solver to provide data used to initialize particles.
  188. #envgen();envexe()
  189. #wxygen()
  190. if l_env:
  191. package("env")
  192. generate()
  193. step()
  194. package("wxy");generate()
  195. step(nint(zmax/zstep0))
  196. dump(runnume+'.dump')