<commit_before>"""
Module. Includes classes for all TEAPOT elements. The approach is based
on the original ORBIT approach developed by <NAME>.
"""

import sys
import os
import math

# import teapot base functions from wrapper around C++ functions
from orbit.teapot_base import TPB

# import the function that creates multidimensional arrays
from orbit.utils import orbitFinalize

# import general accelerator elements and lattice
from orbit.lattice import AccLattice, AccNode, AccActionsContainer, AccNodeBunchTracker

# import the MAD parser to construct lattices of TEAPOT elements.
from orbit.parsers.mad_parser import MAD_Parser, MAD_LattElement

# import the MADX parser to construct lattices of TEAPOT elements.
from orbit.parsers.madx_parser import MADX_Parser, MADX_LattElement

"""
Drift
Bend
Quad
Multipole
Solenoid
Kicker
RingRF
"""

class TEAPOT_Lattice(AccLattice):
	"""
	The subclass of the AccLattice class. Shell class for the TEAPOT nodes collection.
	TEAPOT has the ability to read MAD files.
	"""
	def __init__(self, name = "no name"):
		AccLattice.__init__(self,name)

	def readMAD(self, mad_file_name, lineName):
		"""
		It creates the teapot lattice from MAD file.
		"""
		parser = MAD_Parser()
		parser.parse(mad_file_name)
		accLines = parser.getMAD_LinesDict()
		if(not accLines.has_key(lineName)):
			print "==============================="
			print "MAD file: ", mad_file_name
			print "Can not find accelerator line: ", lineName
			print "STOP."
			sys.exit(1)
		# accelerator lines and elements from mad_parser package
		accMAD_Line = accLines[lineName]
		self.setName(lineName)
		accMADElements = accMAD_Line.getElements()
		# make TEAPOT lattice elements by using TEAPOT
		# element factory
		for madElem in accMADElements:
			elems = _teapotFactory.getElements(madElem)
			for elem in elems:
				self.addNode(elem)
		self.initialize()

	def readMADX(self, madx_file_name, seqName):
		"""
			It creates the teapot lattice from MAD file.
		"""
		parser = MADX_Parser()
		parser.parse(madx_file_name)
		
		if(not seqName == parser.getSequenceName()):
			print "==============================="
			print "MADX file: ", madx_file_name
			print "Can not find accelerator sequence: ", seqName
			print "STOP."
			sys.exit(1)
		   
		self.setName(parser.getSequenceName())
		accMADElements = parser.getSequenceList()
		# make TEAPOT lattice elements by using TEAPOT
		# element factory
		for madElem in accMADElements:
			elems = _teapotFactory.getElements(madElem)
			for elem in elems:
				self.addNode(elem)
		self.initialize()

	def initialize(self):
		AccLattice.initialize(self)
		#set up ring length for RF nodes
		ringRF_Node = RingRFTEAPOT()
		for node in self.getNodes():
			if(node.getType() == ringRF_Node.getType()):
				node.getParamsDict()["ring_length"] = self.getLength()

	def getSubLattice(self, index_start = -1, index_stop = -1,):
		"""
		It returns the new TEAPOT_Lattice with children with indexes 
		between index_start and index_stop inclusive
		"""
		return self._getSubLattice(TEAPOT_Lattice(),index_start,index_stop)

	def trackBunch(self, bunch, paramsDict = {},  index_start = -1, index_stop = -1, actionContainer = None):
		"""
		It tracks the bunch through the lattice.
		"""
		if(actionContainer == None): actionContainer = AccActionsContainer("Bunch Tracking")
		paramsDict["bunch"] = bunch
		
		def track(paramsDict):
			node = paramsDict["node"]
			node.track(paramsDict)
			
		actionContainer.addAction(track, AccActionsContainer.BODY)
		self.trackActions(actionContainer,paramsDict,index_start,index_stop)
		actionContainer.removeAction(track, AccActionsContainer.BODY)


class TEAPOT_Ring(AccLattice):
	"""
		The subclass of the AccLattice class. Shell class for the TEAPOT nodes collection for rings.
		TEAPOT has the ability to read MAD files.
		"""
	def __init__(self, name = "no name"):
		AccLattice.__init__(self,name)
	
	def readMAD(self, mad_file_name, lineName):
		"""
			It creates the teapot lattice from MAD file.
			"""
		parser = MAD_Parser()
		parser.parse(mad_file_name)
		accLines = parser.getMAD_LinesDict()
		if(not accLines.has_key(lineName)):
			print "==============================="
			print "MAD file: ", mad_file_name
			print "Can not find accelerator line: ", lineName
			print "STOP."
			sys.exit(1)
		# accelerator lines and elements from mad_parser package
		accMAD_Line = accLines[lineName]
		self.setName(lineName)
		accMADElements = accMAD_Line.getElements()
		# make TEAPOT lattice elements by using TEAPOT
		# element factory
		for madElem in accMADElements:
			elems = _teapotFactory.getElements(madElem)
			for elem in elems:
				self.addNode(elem)
		self.addChildren()
		self.initialize()

	def readMADX(self, madx_file_name, seqName):
		"""
			It creates the teapot lattice from MAD file.
			"""
		parser = MADX_Parser()
		parser.parse(madx_file_name)
		
		if(not seqName == parser.getSequenceName()):
			print "==============================="
			print "MADX file: ", madx_file_name
			print "Can not find accelerator sequence: ", seqName
			print "STOP."
			sys.exit(1)
		
		self.setName(parser.getSequenceName())
		accMADElements = parser.getSequenceList()
		# make TEAPOT lattice elements by using TEAPOT
		# element factory
		for madElem in accMADElements:
			elems = _teapotFactory.getElements(madElem)
			for elem in elems:
				self.addNode(elem)
		self.addChildren()
		self.initialize()

	
	def addChildren(self):
		AccLattice.initialize(self)
		for node in self.getNodes():
			bunchwrapper = BunchWrapTEAPOT("Bunch Wrap")
			bunchwrapper.getParamsDict()["ring_length"] = self.getLength()
			node.addChildNode(bunchwrapper, AccNode.BODY)
			
	def initialize(self):
		AccLattice.initialize(self)
		#set up ring length for RF nodes
		ringRF_Node = RingRFTEAPOT()
		bunchwrap_Node = BunchWrapTEAPOT()
		for node in self.getNodes():
			if(node.getType() == ringRF_Node.getType()):
				node.getParamsDict()["ring_length"] = self.getLength()
			
		paramsDict = {}
		actions = AccActionsContainer()

		def accSetWrapLengthAction(paramsDict):
			"""
			Nonbound function. Sets lattice length for wrapper nodes
			"""
			node = paramsDict["node"]
			bunchwrap_node = BunchWrapTEAPOT()
			if(node.getType() == bunchwrap_Node.getType()):
				node.getParamsDict()["ring_length"] = self.getLength()

		actions.addAction(accSetWrapLengthAction, AccNode.EXIT)
		self.trackActions(actions, paramsDict)
		actions.removeAction(accSetWrapLengthAction, AccNode.EXIT)


	def getSubLattice(self, index_start = -1, index_stop = -1,):
		"""
			It returns the new TEAPOT_Lattice with children with indexes
			between index_start and index_stop inclusive
			"""
		return self._getSubLattice(TEAPOT_Lattice(),index_start,index_stop)
	
	def trackBunch(self, bunch, paramsDict = {}, index_start = -1, index_stop = -1, actionContainer = None):
		"""
			It tracks the bunch through the lattice.
			"""
		if(actionContainer == None): actionContainer = AccActionsContainer("Bunch Tracking")
		paramsDict["bunch"] = bunch
		
		def track(paramsDict):
			node = paramsDict["node"]
			node.track(paramsDict)
		
		actionContainer.addAction(track, AccActionsContainer.BODY)
		self.trackActions(actionContainer,paramsDict,index_start, index_stop)
		actionContainer.removeAction(track, AccActionsContainer.BODY)


class _teapotFactory:
	"""
	Class. Factory to produce TEAPOT accelerator elements.
	"""
	def __init__(self):
		pass

	def getElements(self, madElem):
		"""
		Method produces TEAPOT accelerator elements. It returns the array of TEAPOT nodes.
		Usually there is only one such element, but sometimes (RF with a non zero length) 
		there could be more than one element in the returned array 
		"""
		# madElem = MAD_LattElement(" "," ")
		params_ini = madElem.getParameters()
		params = {}
		for par_key in params_ini.iterkeys():
			params[par_key.lower()] = params_ini[par_key]
		# Length parameter
		length = 0.
		if(params.has_key("l")): length = params["l"]
		# TILT parameter - if it is there tilt = True,
		# and if it has value tiltAngle = value
		tilt = False
		tiltAngle = None
		if(params.has_key("tilt")):
			tilt = True
			if(params["tilt"] != None):
				tiltAngle = params["tilt"]
		# ============DRIFT Element ==================================
		elem = None
		if(madElem.getType().lower() == "drift"):
			elem = DriftTEAPOT(madElem.getName())
		# ============Dipole Element SBEND or RBEND ===================
		if(madElem.getType().lower()  == "sbend" or \
			 madElem.getType().lower()  == "rbend"):
			elem = BendTEAPOT(madElem.getName())

			theta = 0.
			if(params.has_key("angle")):
				theta = params["angle"]

			ea1 = 0.
			if(params.has_key("e1")):
				ea1 = params["e1"]

			ea2 = 0.
			if(params.has_key("e2")):
				ea2 = params["e2"]

			if(madElem.getType().lower() == "rbend" ):
				length = length*(theta/2.0)/math.sin(theta/2.0)
				ea1 = ea1 + theta/2.0
				ea2 = ea2 + theta/2.0

			elem.addParam("theta",theta)
			elem.addParam("ea1",ea1)
			elem.addParam("ea2",ea2)
			if(tilt):
				if(tiltAngle == None):
					tiltAngle = math.math.pi/2.0
					if(theta < 0.):
						tiltAngle = - tiltAngle

			if(params.has_key("k1")):
				k1 = params["k1"]
				params["k1l"] = k1*length

			if(params.has_key("k2")):
				k2 = params["k2"]
				params["k2l"] = k2*length

			if(params.has_key("k3")):
				k3 = params["k3"]
				params["k3l"] = k3*length
		# ===========QUAD quadrupole element =====================
		if(madElem.getType().lower()  == "quad" or \
			 madElem.getType().lower()  == "quadrupole"):
			elem = QuadTEAPOT(madElem.getName())
			kq = 0.
			if(params.has_key("k1")):
				kq = params["k1"]
			elem.addParam("kq",kq)
			if(tilt):
				if(tiltAngle == None):
					tiltAngle = math.math.pi/4.0
		# ===========Sextupole element =====================
		if(madElem.getType().lower()  == "sextupole"):
			elem = MultipoleTEAPOT(madElem.getName())
			k2 = 0.
			if(params.has_key("k2")):
				k2 = params["k2"]
			params["k2l"] = length*k2
			if(tilt):
				if(tiltAngle == None):
					tiltAngle = math.math.pi/6.0
		# ===========Octupole element =====================
		if(madElem.getType().lower()  == "octupole"):
			elem = MultipoleTEAPOT(madElem.getName())
			k3 = 0.
			if(params.has_key("k3")):
				k3 = params["k3"]
			params["k3l"] = length*k3
			if(tilt):
				if(tiltAngle == None):
					tiltAngle = math.math.pi/8.0
		# ===========Multipole element =====================
		if(madElem.getType().lower()  == "multipole"):
			elem = MultipoleTEAPOT(madElem.getName())
		# ===========Solenoid element ======================
		if(madElem.getType().lower()  == "solenoid"):
			elem = SolenoidTEAPOT(madElem.getName())
			ks = 0.
			if(params.has_key("ks")):
				ks = params["ks"]
			elem.addParam("B",ks)
		# ===========Kicker element ======================
		if(madElem.getType().lower()  == "kicker"):
			elem = KickTEAPOT(madElem.getName())
			hkick = 0.
			if(params.has_key("hkick")):
				hkick = params["hkick"]
			vkick = 0.
			if(params.has_key("vkick")):
				hkick = params["vkick"]
			elem.addParam("kx",hkick)
			elem.addParam("ky",vkick)
		# ===========HKicker element ======================
		if(madElem.getType().lower()  == "hkicker" or \
			 madElem.getType() .lower() == "hkick"):
			elem = KickTEAPOT(madElem.getName())
			hkick = 0.
			if(params.has_key("hkick")):
				hkick = params["hkick"]
			elem.addParam("kx",hkick)
		# ===========VKicker element ======================
		if(madElem.getType().lower()  == "vkicker" or \
			 madElem.getType().lower()  == "vkick"):
			elem = KickTEAPOT(madElem.getName())
			vkick = 0.
			if(params.has_key("vkick")):
				hkick = params["vkick"]
			elem.addParam("ky",vkick)
		# ===========RF Cavity element ======================
		if(madElem.getType().lower() == "rfcavity"):
			elem = RingRFTEAPOT(madElem.getName())
			drft_1 = DriftTEAPOT(madElem.getName()+"_drift")
			drft_2 = DriftTEAPOT(madElem.getName()+"_drift")
			drft_1.setLength(length/2.0)
			drft_2.setLength(length/2.0)
			volt = 0.
			if(params.has_key("volt")):
				volt = params["volt"]
			harmon = 1
			if(params.has_key("harmon")):
				harmon = params["harmon"]
			phase_s = 0.
			if(params.has_key("lag")):
				phase_s = (-1.0) * params["lag"]*2.0*math.pi
			elem.addRF(harmon,volt,phase_s)
			return [drft_1,elem,drft_2]
		# ==========Others elements such as markers,monitor,rcollimator
		if(madElem.getType().lower() == "marker" or \
			 madElem.getType().lower() == "monitor" or \
			 madElem.getType().lower() == "rcolimator"):
			elem = NodeTEAPOT(madElem.getName())
		# ------------------------------------------------
		# ready to finish
		# ------------------------------------------------
		if(elem == None):
			print "======== Can not create elementwith type:",madElem.getType()
			print "You have to fix the _teapotFactory class."
			print "Stop."
			sys.exit(1)
		# set length
		elem.setLength(length)
		# set tilt angle
		if(tilt == True and tiltAngle != None):
			elem.setTiltAngle(tiltAngle)
		# set K1L,K2L,K3L,... and  T1L,T2L,T3L,...
		poles = []
		kls = []
		skews = []
		for i in xrange(20):
			pole = i
			kl_param = None
			skew = 0
			if(params.has_key("k"+str(pole)+"l")):
				kl_param = params["k"+str(pole)+"l"]
			if(params.has_key("t"+str(pole)+"l")):
				skew = 1
			if(kl_param != None):<commit_msg>Fixed bug associated with angle definition syntax for multipoles. <commit_after>"""
Module. Includes classes for all TEAPOT elements. The approach is based
on the original ORBIT approach developed by <NAME>.
"""

import sys
import os
import math

# import teapot base functions from wrapper around C++ functions
from orbit.teapot_base import TPB

# import the function that creates multidimensional arrays
from orbit.utils import orbitFinalize

# import general accelerator elements and lattice
from orbit.lattice import AccLattice, AccNode, AccActionsContainer, AccNodeBunchTracker

# import the MAD parser to construct lattices of TEAPOT elements.
from orbit.parsers.mad_parser import MAD_Parser, MAD_LattElement

# import the MADX parser to construct lattices of TEAPOT elements.
from orbit.parsers.madx_parser import MADX_Parser, MADX_LattElement

"""
Drift
Bend
Quad
Multipole
Solenoid
Kicker
RingRF
"""

class TEAPOT_Lattice(AccLattice):
	"""
	The subclass of the AccLattice class. Shell class for the TEAPOT nodes collection.
	TEAPOT has the ability to read MAD files.
	"""
	def __init__(self, name = "no name"):
		AccLattice.__init__(self,name)

	def readMAD(self, mad_file_name, lineName):
		"""
		It creates the teapot lattice from MAD file.
		"""
		parser = MAD_Parser()
		parser.parse(mad_file_name)
		accLines = parser.getMAD_LinesDict()
		if(not accLines.has_key(lineName)):
			print "==============================="
			print "MAD file: ", mad_file_name
			print "Can not find accelerator line: ", lineName
			print "STOP."
			sys.exit(1)
		# accelerator lines and elements from mad_parser package
		accMAD_Line = accLines[lineName]
		self.setName(lineName)
		accMADElements = accMAD_Line.getElements()
		# make TEAPOT lattice elements by using TEAPOT
		# element factory
		for madElem in accMADElements:
			elems = _teapotFactory.getElements(madElem)
			for elem in elems:
				self.addNode(elem)
		self.initialize()

	def readMADX(self, madx_file_name, seqName):
		"""
			It creates the teapot lattice from MAD file.
		"""
		parser = MADX_Parser()
		parser.parse(madx_file_name)
		
		if(not seqName == parser.getSequenceName()):
			print "==============================="
			print "MADX file: ", madx_file_name
			print "Can not find accelerator sequence: ", seqName
			print "STOP."
			sys.exit(1)
		   
		self.setName(parser.getSequenceName())
		accMADElements = parser.getSequenceList()
		# make TEAPOT lattice elements by using TEAPOT
		# element factory
		for madElem in accMADElements:
			elems = _teapotFactory.getElements(madElem)
			for elem in elems:
				self.addNode(elem)
		self.initialize()

	def initialize(self):
		AccLattice.initialize(self)
		#set up ring length for RF nodes
		ringRF_Node = RingRFTEAPOT()
		for node in self.getNodes():
			if(node.getType() == ringRF_Node.getType()):
				node.getParamsDict()["ring_length"] = self.getLength()

	def getSubLattice(self, index_start = -1, index_stop = -1,):
		"""
		It returns the new TEAPOT_Lattice with children with indexes 
		between index_start and index_stop inclusive
		"""
		return self._getSubLattice(TEAPOT_Lattice(),index_start,index_stop)

	def trackBunch(self, bunch, paramsDict = {},  index_start = -1, index_stop = -1, actionContainer = None):
		"""
		It tracks the bunch through the lattice.
		"""
		if(actionContainer == None): actionContainer = AccActionsContainer("Bunch Tracking")
		paramsDict["bunch"] = bunch
		
		def track(paramsDict):
			node = paramsDict["node"]
			node.track(paramsDict)
			
		actionContainer.addAction(track, AccActionsContainer.BODY)
		self.trackActions(actionContainer,paramsDict,index_start,index_stop)
		actionContainer.removeAction(track, AccActionsContainer.BODY)


class TEAPOT_Ring(AccLattice):
	"""
		The subclass of the AccLattice class. Shell class for the TEAPOT nodes collection for rings.
		TEAPOT has the ability to read MAD files.
		"""
	def __init__(self, name = "no name"):
		AccLattice.__init__(self,name)
	
	def readMAD(self, mad_file_name, lineName):
		"""
			It creates the teapot lattice from MAD file.
			"""
		parser = MAD_Parser()
		parser.parse(mad_file_name)
		accLines = parser.getMAD_LinesDict()
		if(not accLines.has_key(lineName)):
			print "==============================="
			print "MAD file: ", mad_file_name
			print "Can not find accelerator line: ", lineName
			print "STOP."
			sys.exit(1)
		# accelerator lines and elements from mad_parser package
		accMAD_Line = accLines[lineName]
		self.setName(lineName)
		accMADElements = accMAD_Line.getElements()
		# make TEAPOT lattice elements by using TEAPOT
		# element factory
		for madElem in accMADElements:
			elems = _teapotFactory.getElements(madElem)
			for elem in elems:
				self.addNode(elem)
		self.addChildren()
		self.initialize()

	def readMADX(self, madx_file_name, seqName):
		"""
			It creates the teapot lattice from MAD file.
			"""
		parser = MADX_Parser()
		parser.parse(madx_file_name)
		
		if(not seqName == parser.getSequenceName()):
			print "==============================="
			print "MADX file: ", madx_file_name
			print "Can not find accelerator sequence: ", seqName
			print "STOP."
			sys.exit(1)
		
		self.setName(parser.getSequenceName())
		accMADElements = parser.getSequenceList()
		# make TEAPOT lattice elements by using TEAPOT
		# element factory
		for madElem in accMADElements:
			elems = _teapotFactory.getElements(madElem)
			for elem in elems:
				self.addNode(elem)
		self.addChildren()
		self.initialize()

	
	def addChildren(self):
		AccLattice.initialize(self)
		for node in self.getNodes():
			bunchwrapper = BunchWrapTEAPOT("Bunch Wrap")
			bunchwrapper.getParamsDict()["ring_length"] = self.getLength()
			node.addChildNode(bunchwrapper, AccNode.BODY)
			
	def initialize(self):
		AccLattice.initialize(self)
		#set up ring length for RF nodes
		ringRF_Node = RingRFTEAPOT()
		bunchwrap_Node = BunchWrapTEAPOT()
		for node in self.getNodes():
			if(node.getType() == ringRF_Node.getType()):
				node.getParamsDict()["ring_length"] = self.getLength()
			
		paramsDict = {}
		actions = AccActionsContainer()

		def accSetWrapLengthAction(paramsDict):
			"""
			Nonbound function. Sets lattice length for wrapper nodes
			"""
			node = paramsDict["node"]
			bunchwrap_node = BunchWrapTEAPOT()
			if(node.getType() == bunchwrap_Node.getType()):
				node.getParamsDict()["ring_length"] = self.getLength()

		actions.addAction(accSetWrapLengthAction, AccNode.EXIT)
		self.trackActions(actions, paramsDict)
		actions.removeAction(accSetWrapLengthAction, AccNode.EXIT)


	def getSubLattice(self, index_start = -1, index_stop = -1,):
		"""
			It returns the new TEAPOT_Lattice with children with indexes
			between index_start and index_stop inclusive
			"""
		return self._getSubLattice(TEAPOT_Lattice(),index_start,index_stop)
	
	def trackBunch(self, bunch, paramsDict = {}, index_start = -1, index_stop = -1, actionContainer = None):
		"""
			It tracks the bunch through the lattice.
			"""
		if(actionContainer == None): actionContainer = AccActionsContainer("Bunch Tracking")
		paramsDict["bunch"] = bunch
		
		def track(paramsDict):
			node = paramsDict["node"]
			node.track(paramsDict)
		
		actionContainer.addAction(track, AccActionsContainer.BODY)
		self.trackActions(actionContainer,paramsDict,index_start, index_stop)
		actionContainer.removeAction(track, AccActionsContainer.BODY)


class _teapotFactory:
	"""
	Class. Factory to produce TEAPOT accelerator elements.
	"""
	def __init__(self):
		pass

	def getElements(self, madElem):
		"""
		Method produces TEAPOT accelerator elements. It returns the array of TEAPOT nodes.
		Usually there is only one such element, but sometimes (RF with a non zero length) 
		there could be more than one element in the returned array 
		"""
		# madElem = MAD_LattElement(" "," ")
		params_ini = madElem.getParameters()
		params = {}
		for par_key in params_ini.iterkeys():
			params[par_key.lower()] = params_ini[par_key]
		# Length parameter
		length = 0.
		if(params.has_key("l")): length = params["l"]
		# TILT parameter - if it is there tilt = True,
		# and if it has value tiltAngle = value
		tilt = False
		tiltAngle = None
		if(params.has_key("tilt")):
			tilt = True
			if(params["tilt"] != None):
				tiltAngle = params["tilt"]
		# ============DRIFT Element ==================================
		elem = None
		if(madElem.getType().lower() == "drift"):
			elem = DriftTEAPOT(madElem.getName())
		# ============Dipole Element SBEND or RBEND ===================
		if(madElem.getType().lower()  == "sbend" or \
			 madElem.getType().lower()  == "rbend"):
			elem = BendTEAPOT(madElem.getName())

			theta = 0.
			if(params.has_key("angle")):
				theta = params["angle"]

			ea1 = 0.
			if(params.has_key("e1")):
				ea1 = params["e1"]

			ea2 = 0.
			if(params.has_key("e2")):
				ea2 = params["e2"]

			if(madElem.getType().lower() == "rbend" ):
				length = length*(theta/2.0)/math.sin(theta/2.0)
				ea1 = ea1 + theta/2.0
				ea2 = ea2 + theta/2.0

			elem.addParam("theta",theta)
			elem.addParam("ea1",ea1)
			elem.addParam("ea2",ea2)
			if(tilt):
				if(tiltAngle == None):
					tiltAngle = math.math.pi/2.0
					if(theta < 0.):
						tiltAngle = - tiltAngle

			if(params.has_key("k1")):
				k1 = params["k1"]
				params["k1l"] = k1*length

			if(params.has_key("k2")):
				k2 = params["k2"]
				params["k2l"] = k2*length

			if(params.has_key("k3")):
				k3 = params["k3"]
				params["k3l"] = k3*length
		# ===========QUAD quadrupole element =====================
		if(madElem.getType().lower()  == "quad" or \
			 madElem.getType().lower()  == "quadrupole"):
			elem = QuadTEAPOT(madElem.getName())
			kq = 0.
			if(params.has_key("k1")):
				kq = params["k1"]
			elem.addParam("kq",kq)
			if(tilt):
				if(tiltAngle == None):
					tiltAngle = math.math.pi/4.0
		# ===========Sextupole element =====================
		if(madElem.getType().lower()  == "sextupole"):
			elem = MultipoleTEAPOT(madElem.getName())
			k2 = 0.
			if(params.has_key("k2")):
				k2 = params["k2"]
			params["k2l"] = length*k2
			if(tilt):
				if(tiltAngle == None):
					tiltAngle = math.math.pi/6.0
		# ===========Octupole element =====================
		if(madElem.getType().lower()  == "octupole"):
			elem = MultipoleTEAPOT(madElem.getName())
			k3 = 0.
			if(params.has_key("k3")):
				k3 = params["k3"]
			params["k3l"] = length*k3
			if(tilt):
				if(tiltAngle == None):
					tiltAngle = math.math.pi/8.0
		# ===========Multipole element =====================
		if(madElem.getType().lower()  == "multipole"):
			elem = MultipoleTEAPOT(madElem.getName())
		# ===========Solenoid element ======================
		if(madElem.getType().lower()  == "solenoid"):
			elem = SolenoidTEAPOT(madElem.getName())
			ks = 0.
			if(params.has_key("ks")):
				ks = params["ks"]
			elem.addParam("B",ks)
		# ===========Kicker element ======================
		if(madElem.getType().lower()  == "kicker"):
			elem = KickTEAPOT(madElem.getName())
			hkick = 0.
			if(params.has_key("hkick")):
				hkick = params["hkick"]
			vkick = 0.
			if(params.has_key("vkick")):
				hkick = params["vkick"]
			elem.addParam("kx",hkick)
			elem.addParam("ky",vkick)
		# ===========HKicker element ======================
		if(madElem.getType().lower()  == "hkicker" or \
			 madElem.getType() .lower() == "hkick"):
			elem = KickTEAPOT(madElem.getName())
			hkick = 0.
			if(params.has_key("hkick")):
				hkick = params["hkick"]
			elem.addParam("kx",hkick)
		# ===========VKicker element ======================
		if(madElem.getType().lower()  == "vkicker" or \
			 madElem.getType().lower()  == "vkick"):
			elem = KickTEAPOT(madElem.getName())
			vkick = 0.
			if(params.has_key("vkick")):
				hkick = params["vkick"]
			elem.addParam("ky",vkick)
		# ===========RF Cavity element ======================
		if(madElem.getType().lower() == "rfcavity"):
			elem = RingRFTEAPOT(madElem.getName())
			drft_1 = DriftTEAPOT(madElem.getName()+"_drift")
			drft_2 = DriftTEAPOT(madElem.getName()+"_drift")
			drft_1.setLength(length/2.0)
			drft_2.setLength(length/2.0)
			volt = 0.
			if(params.has_key("volt")):
				volt = params["volt"]
			harmon = 1
			if(params.has_key("harmon")):
				harmon = params["harmon"]
			phase_s = 0.
			if(params.has_key("lag")):
				phase_s = (-1.0) * params["lag"]*2.0*math.pi
			elem.addRF(harmon,volt,phase_s)
			return [drft_1,elem,drft_2]
		# ==========Others elements such as markers,monitor,rcollimator
		if(madElem.getType().lower() == "marker" or \
			 madElem.getType().lower() == "monitor" or \
			 madElem.getType().lower() == "rcolimator"):
			elem = NodeTEAPOT(madElem.getName())
		# ------------------------------------------------
		# ready to finish
		# ------------------------------------------------
		if(elem == None):
			print "======== Can not create elementwith type:",madElem.getType()
			print "You have to fix the _teapotFactory class."
			print "Stop."
			sys.exit(1)
		# set length
		elem.setLength(length)
		# set tilt angle
		if(tilt == True and tiltAngle != None):
			elem.setTiltAngle(tiltAngle)
		# set K1L,K2L,K3L,... and  T1L,T2L,T3L,...
		poles = []
		kls = []
		skews = []
		for i in xrange(20):
			pole = i
			kl_param = None
			skew = 0
			if(params.has_key("k"+str(pole)+"l")):
				kl_param = params["k"+str(pole)+"l"]
			if(params.has_key("t"+str(pole))):
				skew = 1
			if(kl_param != None):
{'fraction_non_alphanumeric': 0.17332781892694665, 'fraction_numerical': 0.008572395382937926, 'mean_word_length': 3.9669467787114847, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4119039', 'n_tokens_mistral': 10494, 'n_tokens_neox': 9057, 'n_words': 2018}