/scripts/attic/hibeamlattice.py

https://bitbucket.org/berkeleylab/warp
Python | 179 lines | 143 code | 11 blank | 25 comment | 24 complexity | 4b54043ac3ff029aa54f6446629337a0 MD5 | raw file
  1. # Setup classes to mirror lattice element type in hibeam.
  2. # DPG 7/22/99
  3. import re
  4. import string
  5. class LINE:
  6. def __init__(self,*elems):
  7. self.type = 'LINE'
  8. # --- Unravel any imbedded lists.
  9. i = 0
  10. elems = list(elems)
  11. while i < len(elems):
  12. if isinstance(elems[i],list):
  13. elems[i:i+1] = elems[i]
  14. else:
  15. i = i + 1
  16. # --- save list of elements
  17. self.elems = elems
  18. # --- initialize expanded list as a blank
  19. self.elemslist = []
  20. def expand(self):
  21. if self.elemslist: return self.elemslist
  22. for e in self.elems:
  23. self.elemslist.append(e.expand())
  24. # --- Unravel any imbedded lists.
  25. i = 0
  26. self.elemslist = list(self.elemslist)
  27. while i < len(self.elemslist):
  28. if isinstance(self.elemslist[i],list):
  29. self.elemslist[i:i+1] = self.elemslist[i]
  30. else:
  31. i = i + 1
  32. return self.elemslist
  33. def __mul__(self,other):
  34. # --- This allows multiplication of elements
  35. return LINE(other*[self])
  36. def __rmul__(self,other):
  37. # --- This allows multiplication of elements
  38. return LINE(other*[self])
  39. class Elem:
  40. def __init__(self,l=0,length=0):
  41. self.type = ''
  42. self.length = max(l,length)
  43. #def isin(self,z=top.zbeam):
  44. #if self._zs < z and z < self._ze: return 1
  45. #return 0
  46. def length(self):
  47. return self._length
  48. def expand(self):
  49. return self
  50. def __mul__(self,other):
  51. return LINE(other*[self])
  52. def __rmul__(self,other):
  53. return LINE(other*[self])
  54. class drift(Elem):
  55. def __init__(self,l=0,length=0,aperture=0,error_type='',
  56. offset_x=0,offset_y=0,i_cap_pointer=0,n_cap_nodes=0):
  57. self.type = 'drift'
  58. Elem.__init__(self,l=l,length=length)
  59. self.aperture = aperture
  60. self.error_type = error_type
  61. self.offset_x = offset_x
  62. self.offset_y = offset_y
  63. self.i_cap_pointer = i_cap_pointer
  64. self.n_cap_nodes = n_cap_nodes
  65. class box(Elem):
  66. def __init__(self,l=0,length=0,width_x=0,width_y=0,error_type='',
  67. offset_x=0,offset_y=0,i_cap_pointer=0,n_cap_nodes=0):
  68. self.type = 'box'
  69. Elem.__init__(self,l=l,length=length)
  70. self.width_x = width_x
  71. self.width_y = width_y
  72. self.error_type = error_type
  73. self.offset_x = offset_x
  74. self.offset_y = offset_y
  75. self.i_cap_pointer = i_cap_pointer
  76. self.n_cap_nodes = n_cap_nodes
  77. class quad(Elem):
  78. def __init__(self,l=0,length=0,aperture=0,voltage=0,gradient=0,r_elem=0,
  79. width_x=0,width_y=0,error_type='',
  80. offset_x=0,offset_y=0,i_cap_pointer=0,n_cap_nodes=0):
  81. self.type = 'quad'
  82. Elem.__init__(self,l=l,length=length)
  83. self.aperture = aperture
  84. self.voltage = voltage
  85. self.gradient = gradient
  86. self.r_elem = r_elem
  87. self.error_type = error_type
  88. self.offset_x = offset_x
  89. self.offset_y = offset_y
  90. self.i_cap_pointer = i_cap_pointer
  91. self.n_cap_nodes = n_cap_nodes
  92. if gradient != 0:
  93. self.voltage = gradient*aperture**2
  94. else:
  95. self.gradient = voltage/aperture**2
  96. class hyperb(Elem):
  97. def __init__(self,l=0,length=0,aperture=0,voltage=0,r_elem=0,error_type='',
  98. i_cap_pointer=0,n_cap_nodes=0):
  99. self.type = 'hyperb'
  100. Elem.__init__(self,l=l,length=length)
  101. self.aperture = aperture
  102. self.voltage = voltage
  103. self.r_elem = r_elem
  104. self.error_type = error_type
  105. self.i_cap_pointer = i_cap_pointer
  106. self.n_cap_nodes = n_cap_nodes
  107. class wire(Elem):
  108. def __init__(self,l=0,length=0,aperture=0,):
  109. self.type = 'wire'
  110. Elem.__init__(self,l=l,length=length)
  111. self.aperture = aperture
  112. class child:
  113. def __init__(self,parent,**changes):
  114. self.parent = eval(parent,globals())
  115. self.__dict__.update(changes)
  116. if 'l' in self.__dict__:
  117. self.__dict__['length'] = self.__dict__['l']
  118. def __getattr__(self,name):
  119. return eval('self.parent.'+name,locals())
  120. #########################################################################
  121. # --- Now, using above classes, read in and parse a hibeam lattice file.
  122. def hibeamlattice(file):
  123. with open(file,'r') as ff:
  124. data = ff.readlines()
  125. # --- Massage the data removing carriage returns, comments and blank lines
  126. i = 0
  127. while i < len(data):
  128. # --- Remove carriage return at end of line
  129. data[i] = data[i][:-1]
  130. # --- Remove all white space
  131. data[i] = re.sub('\s','',data[i])
  132. # --- Remove comments
  133. data[i] = re.sub('!.*','',data[i])
  134. # --- Delete empty lines
  135. if data[i]:
  136. i = i + 1
  137. else:
  138. del data[i]
  139. # --- Massage the data removing line continuations.
  140. i = 0
  141. while i < len(data):
  142. mextend = re.search('&',data[i])
  143. while mextend:
  144. data[i] = re.sub('&',data[i+1],data[i])
  145. del data[i+1]
  146. mextend = re.search('&',data[i])
  147. i = i + 1
  148. # --- Massage the data into python syntax
  149. for i in range(len(data)):
  150. # --- Replace '=' with '(' in LINE command
  151. # --- Replace first ',' with '(' in all except LINE commands
  152. mline = re.search('LINE',data[i])
  153. if mline:
  154. data[i] = re.sub('=','(',data[i])
  155. else:
  156. data[i] = re.sub(',','(',data[i],1)
  157. # --- Replace ':' with '=' in all commands
  158. data[i] = re.sub(':','=',data[i])
  159. # --- Append ')' to the end of the line
  160. data[i] = data[i] + ')'
  161. # --- Now the data is ready to be processed
  162. for d in data:
  163. mfinish = re.search(r'(?P<u>USE\((?P<use>\w*)\))|(?P<end>END)',d)
  164. if mfinish:
  165. if mfinish.group('u'):
  166. return eval(mfinish.group('use'),globals())
  167. else:
  168. exec(d,globals())