示例#1
0
def setM(arr):
    n = len(arr)
    m = Matrix(n, n)
    for i in xrange(n):
        for j in xrange(n):
            m.set(i, j, arr[i][j])
    return m
示例#2
0
    def trackOrbit(self, z0):
        """
		Returns the tuple ([(position, x),...], [(position, y),...] ).
		The tracking starts from the values specified as the initial parameters. 
		z0 fulfill: z0 = Mz0 with M as one turn matrix
		"""

        eps_length = 0.00001  # 10^-6 meter

        pos_arr = []
        orbitX_arr = []
        orbitY_arr = []
        orbitXP_arr = []
        orbitYP_arr = []

        position = 0.
        pos_arr.append(position)

        track_o = Matrix(6, 6)

        track_ov = PhaseVector(6)
        track_ov.set(0, z0[0])
        track_ov.set(1, z0[1])
        track_ov.set(2, z0[2])
        track_ov.set(3, z0[3])
        track_ov.set(4, z0[4])
        track_ov.set(5, z0[5])

        orbitX_arr.append(track_ov.get(0))
        orbitY_arr.append(track_ov.get(2))
        orbitXP_arr.append(track_ov.get(1))
        orbitYP_arr.append(track_ov.get(3))

        position_old = position

        for matrixNode in self.getNodes():
            if (isinstance(matrixNode, BaseMATRIX) == True):
                if (abs(position_old - position) > eps_length):
                    pos_arr.append(position)
                    orbitX_arr.append(track_ov.get(0))
                    orbitY_arr.append(track_ov.get(2))
                    orbitXP_arr.append(track_ov.get(1))
                    orbitYP_arr.append(track_ov.get(3))

                mt = matrixNode.getMatrix()

                for i in range(6):
                    for j in range(6):
                        track_o.set(i, j, mt.get(i, j))
                track_ov = track_o.mult(track_ov)

                for i in range(6):
                    tmp = track_ov.get(i)
                    track_ov.set(i, tmp + mt.get(i, 6))

                position_old = position
                position = position + matrixNode.getLength()

        pos_arr.append(position)
        orbitX_arr.append(track_ov.get(0))
        orbitY_arr.append(track_ov.get(2))
        orbitXP_arr.append(track_ov.get(1))
        orbitYP_arr.append(track_ov.get(3))
        #pack the resulting tuple
        graph_orbitX_arr = []
        graph_orbitY_arr = []
        for i in range(len(pos_arr)):
            graph_orbitX_arr.append(
                (pos_arr[i], orbitX_arr[i], orbitXP_arr[i]))
            graph_orbitY_arr.append(
                (pos_arr[i], orbitY_arr[i], orbitYP_arr[i]))

        return (graph_orbitX_arr, graph_orbitY_arr)
示例#3
0
    def trackDispersionData(self, momentum, mass, disp, disp_p, direction="x"):
        """
		Returns the tuple ([(position, disp),...],[(position,disp_p),...] ). 
		The tracking starts from the values specified as the initial parameters. 
		The possible values for direction parameter "x" or "y".
		"""
        if (direction.lower() != "x" and direction.lower() != "y"):
            orbitFinalize(
                "Class orbit.matrix_lattice.MATRIX_Lattice, method trackDispersionData(...): direction should be x or y."
            )
        #track dispersion
        eps_length = 0.00001  # 10^-6 meter
        dir_ind = 0
        if (direction.lower() == "y"):
            dir_ind = 2
        track_m = Matrix(3, 3)
        track_m.unit()
        track_m.set(2, 0, 0.)
        track_m.set(2, 1, 0.)
        track_m.set(2, 2, 1.)
        track_v = PhaseVector(3)
        #kinematics coefficient calculation
        Etotal = math.sqrt(momentum**2 + mass**2)
        beta = momentum / Etotal
        gamma = Etotal / mass
        Ekin = Etotal - mass
        m_coeff = momentum * momentum / (mass + Ekin)
        track_v.set(0, disp)
        track_v.set(1, disp_p)
        track_v.set(2, 1.)
        position = 0.
        pos_arr = []
        disp_arr = []
        disp_p_arr = []
        #put in array the initial dispersions
        #count = 0
        pos_arr.append(position)
        disp_arr.append(track_v.get(0))
        disp_p_arr.append(track_v.get(1))
        for matrixNode in self.getNodes():
            if (isinstance(matrixNode, BaseMATRIX) == True):
                mt = matrixNode.getMatrix()
                ind0 = 0 + dir_ind
                ind1 = 1 + dir_ind
                track_m.set(0, 0, mt.get(ind0, ind0))
                track_m.set(0, 1, mt.get(ind0, ind1))
                track_m.set(1, 0, mt.get(ind1, ind0))
                track_m.set(1, 1, mt.get(ind1, ind1))
                track_m.set(0, 2, mt.get(ind0, 5) * m_coeff)
                track_m.set(1, 2, mt.get(ind1, 5) * m_coeff)
                track_v = track_m.mult(track_v)
                position = position + matrixNode.getLength()
                #only the main nodes are used, the or-cases deal with markers with zero length
                if (isinstance(matrixNode, BaseMATRIX) == True):
                    pos_arr.append(position)
                    disp_arr.append(track_v.get(0))
                    disp_p_arr.append(track_v.get(1))
        #pack the resulting tuple
        graph_disp_arr = []
        graph_disp_p_arr = []
        for i in range(len(pos_arr)):
            graph_disp_arr.append((pos_arr[i], disp_arr[i]))
            graph_disp_p_arr.append((pos_arr[i], disp_p_arr[i]))
        return (graph_disp_arr, graph_disp_p_arr)
示例#4
0
    def trackTwissData(self, alpha, beta, direction="x"):
        """
		Returns the tuple ([(position, phase advance/2/pi),...], [(position, alpha),...],[(position,beta),...] ).
		The tracking starts from the values specified as the initial parameters. 
		The possible values for direction parameter "x" or "y".
		"""
        if (direction.lower() != "x" and direction.lower() != "y"):
            orbitFinalize(
                "Class orbit.matrix_lattice.MATRIX_Lattice, method trackTwissData(...): direction should be x or y."
            )
        #track twiss
        eps_length = 0.00001  # 10^-6 meter
        dir_ind = 0
        if (direction.lower() == "y"):
            dir_ind = 2
        gamma = (1.0 + alpha * alpha) / beta
        track_m = Matrix(3, 3)
        track_m.unit()
        track_v = PhaseVector(3)
        track_v.set(0, alpha)
        track_v.set(1, beta)
        track_v.set(2, gamma)
        position = 0.
        pos_arr = []
        alpha_arr = []
        beta_arr = []
        #phi is for tune accumulation
        phi = 0.
        #phase advance
        mu_arr = []
        #count = 0
        pos_arr.append(position)
        mu_arr.append(phi)
        alpha_arr.append(track_v.get(0))
        beta_arr.append(track_v.get(1))
        for matrixNode in self.getNodes():
            mt = matrixNode.getMatrix()
            ind0 = 0 + dir_ind
            ind1 = 1 + dir_ind
            track_m.set(
                0, 0,
                mt.get(ind0, ind0) * mt.get(ind1, ind1) +
                mt.get(ind0, ind1) * mt.get(ind1, ind0))
            track_m.set(0, 1, -mt.get(ind0, ind0) * mt.get(ind1, ind0))
            track_m.set(0, 2, -mt.get(ind0, ind1) * mt.get(ind1, ind1))
            track_m.set(1, 0, -2 * mt.get(ind0, ind0) * mt.get(ind0, ind1))
            track_m.set(1, 1, mt.get(ind0, ind0) * mt.get(ind0, ind0))
            track_m.set(1, 2, mt.get(ind0, ind1) * mt.get(ind0, ind1))
            track_m.set(2, 0, -2 * mt.get(ind1, ind0) * mt.get(ind1, ind1))
            track_m.set(2, 1, mt.get(ind1, ind0) * mt.get(ind1, ind0))
            track_m.set(2, 2, mt.get(ind1, ind1) * mt.get(ind1, ind1))
            alpha_0 = track_v.get(0)
            beta_0 = track_v.get(1)
            delta_phi = math.atan(
                mt.get(ind0, ind1) /
                (beta_0 * mt.get(ind0, ind0) - alpha_0 * mt.get(ind0, ind1)))
            phi = phi + delta_phi
            track_v = track_m.mult(track_v)
            position = position + matrixNode.getLength()
            if (isinstance(matrixNode, BaseMATRIX) == True):
                #only the main nodes are used, the or-cases deal with markers with zero length
                #print position, matrixNode.getParam("matrix_parent_node_active_index") == 1
                #print position, matrixNode.getName(), track_v.get(1)

                if matrixNode.getLength() > 0:
                    #print position, matrixNode.getName(), track_v.get(1)
                    pos_arr.append(position)
                    alpha_arr.append(track_v.get(0))
                    beta_arr.append(track_v.get(1))
                    mu_arr.append(phi / (2 * math.pi))
            #count = count + 1
        #pack the resulting tuple
        tune = phi / (2 * math.pi)
        graph_alpha_arr = []
        graph_beta_arr = []
        graph_mu_arr = []
        #print count, len(pos_arr)
        for i in range(len(pos_arr)):
            graph_mu_arr.append((pos_arr[i], mu_arr[i]))
            graph_alpha_arr.append((pos_arr[i], alpha_arr[i]))
            graph_beta_arr.append((pos_arr[i], beta_arr[i]))
        return (graph_mu_arr, graph_alpha_arr, graph_beta_arr)
示例#5
0
 def _makePolynomial(self):
     nPoints = len(self.x_y_err_arr)
     if (nPoints < (self.order + 1)):
         self.order = nPoints - 1
     #check if just one of errors is zero
     infoZeroErr = 1.0
     for [x, y, err] in self.x_y_err_arr:
         infoZeroErr *= err
     for i in range(nPoints):
         [x, y, err] = self.x_y_err_arr[i]
         sigma = 1.0
         if (infoZeroErr != 0.):
             sigma = 1.0 / (err * err)
         self.x_y_err_arr[i][2] = sigma
     #now make A matrix
     aMatr = Matrix(nPoints, self.order + 1)
     for i in range(nPoints):
         for j in range(self.order + 1):
             x = self.x_y_err_arr[i][0]
             aMatr.set(i, j, math.pow(x, j))
     aTCa = Matrix(self.order + 1, self.order + 1)
     for i in range(self.order + 1):
         for j in range(self.order + 1):
             a = 0.
             for k in range(nPoints):
                 sigma = self.x_y_err_arr[k][2]
                 a += aMatr.get(k, i) * sigma * aMatr.get(k, j)
             aTCa.set(i, j, a)
     #now the resuting coefficients and errors
     aTCaI = aTCa.invert()
     e = aTCaI.mult(aTCa)
     if (aTCa == None):
         print "python PolynomialFit: Problem with data."
         for i in range(nPoints):
             x = self.x_y_err_arr[i][0]
             y = self.x_y_err_arr[i][1]
             err = self.x_y_err_arr[i][2]
             print " x,y,err = %12.5g %12.5g %12.5g " % (x, y, err)
         print "Stop."
         sys.exit(1)
     coef_arr = [0.] * (self.order + 1)
     err_arr = [0.] * (self.order + 1)
     for i in range(self.order + 1):
         err_arr[i] = math.sqrt(math.fabs(aTCaI.get(i, i)))
     for i in range(self.order + 1):
         coef_arr[i] = 0.
         for j in range(self.order + 1):
             for k in range(nPoints):
                 sigma = self.x_y_err_arr[k][2]
                 y = self.x_y_err_arr[k][1]
                 coef_arr[i] += aTCaI.get(i, j) * aMatr.get(k,
                                                            j) * sigma * y
     # polinimial coefficients are found
     self.polynomial.order(self.order)
     for i in range(len(coef_arr)):
         self.polynomial.coefficient(i, coef_arr[i])
     # now let's calculate errors
     if (infoZeroErr == 0.):
         total_sigma = 0.
         for k in range(nPoints):
             x = self.x_y_err_arr[k][0]
             y = self.x_y_err_arr[k][1]
             total_sigma += (self.polynomial.value(x) - y)**2
         total_sigma = math.sqrt(total_sigma / (nPoints - 2))
         for i in range(len(err_arr)):
             err_arr[i] *= total_sigma
     # set the resulting coefficients and errors array
     self.coef_err_arr = [coef_arr, err_arr]
示例#6
0
print "Start."

n = Nll()

k = .01

b = Bunch()

b.addParticle(1., 2., 3., 4., 5., 6.)

b.dumpBunch()

m = Matrix(6, 6)
m.unit()
m.set(0, 0, math.cos(k))
m.set(0, 1, math.sin(k))
m.set(1, 0, -math.sin(k))
m.set(1, 1, math.cos(k))
m.set(2, 2, math.cos(k))
m.set(2, 3, math.sin(k))
m.set(3, 2, -math.sin(k))
m.set(3, 3, math.cos(k))

printM(m)

for i in range(20000):
    m.track(b)
    n.TRACK_EXT(b)
    plotx.append([b.x(0), b.y(0)])
    #ploty.append([b.y(0), b.yp(0)]); ploty.append([b.x(0), b.y(0)])
                             random.random(), random.random(), random.random())
    b_in.addParticle(x, xp, y, yp, z, dE)
    if (b_in_has_ms):
        b_in.partAttrValue("macrosize", i, 0, random.random())
b_in.addParticle(0., 0., 0., 0., 0., 0.)
#----set up ids
ParticleIdNumber.addParticleIdNumbers(b_in)

b_out = Bunch()
b_in.copyEmptyBunchTo(b_out)

#-----set up initial transport matix, later we have to extract it from bunches
mtrxA_init = Matrix(7, 7)
mtrxA_init.zero()
for ix in range(6):
    mtrxA_init.set(ix, 6, 1.0 * ix)
mtrxA_init.set(6, 6, 1.0)
for ix in range(6):
    mtrxA_init.set(ix, ix, 1. * (ix + 1))

mtrxA_init.set(0, 1, 1.5 * .001)
mtrxA_init.set(2, 3, 1.6 * .001)
mtrxA_init.set(4, 5, 1.7 * .001)

mtrxA_init.set(1, 0, 1.4 * .001)
mtrxA_init.set(3, 2, 1.3 * .001)
mtrxA_init.set(5, 4, 1.2 * .001)

printM(mtrxA_init, "Init M ")

#-----------------------------------------------------------------------------
示例#8
0
qv01_pos_start = accLattice.getNodePositionsDict()[accLattice.getNodeForName(
    "QV01")][0]
qh02_pos_start = accLattice.getNodePositionsDict()[accLattice.getNodeForName(
    "QH02")][0]
qv03_pos_start = accLattice.getNodePositionsDict()[accLattice.getNodeForName(
    "QV03")][0]
qh04_pos_start = accLattice.getNodePositionsDict()[accLattice.getNodeForName(
    "QH04")][0]

qv01_pos_center = (qv01_pos_start + qv01_pos_end) / 2.0

#---- qv01_3d    coordinate transformation from the lattice system
transfCoordsMatrix = Matrix(4, 4)
transfCoordsMatrix.unit()
dist_from_qv01_center = 0.
transfCoordsMatrix.set(2, 3, -dist_from_qv01_center)
qv01_3d.transormfMatrix(transfCoordsMatrix)

#---- qh02_3d    coordinate transformation from the lattice system
transfCoordsMatrix = Matrix(4, 4)
transfCoordsMatrix.unit()
dist_from_qv01_center = qh02_pos_end - qv01_pos_end
transfCoordsMatrix.set(2, 3, -dist_from_qv01_center)
qh02_3d.transormfMatrix(transfCoordsMatrix)

#---- qv03_3d    coordinate transformation from the lattice system
transfCoordsMatrix = Matrix(4, 4)
transfCoordsMatrix.unit()
dist_from_qv01_center = qv03_pos_end - qv01_pos_end
transfCoordsMatrix.set(2, 3, -dist_from_qv01_center)
qv03_3d.transormfMatrix(transfCoordsMatrix)
示例#9
0
while ( s != ""):
	if(len(s_arr) < stack_size):
		s_arr.append(s)
	else:
		#======= we have the full stack of lines ===========
 		if(s_arr[1].find("Element transfer matrix:") >= 0):
			res_arr = s_arr[0].split()
			name = res_arr[len(res_arr) - 2][1:]
			elem_count = elem_count + 1
			#print "i=",elem_count," name=",name
			matrix = Matrix(6,6)
			for i in range(6):
				res_arr = s_arr[i+2].split()
				for j in range(6):
					matrix.set(i,j, float(res_arr[j]))
			#printM(matrix)
			lattice_arr.append((name,matrix))
		for it in range(stack_size-1):
			s_arr[it] = s_arr[it+1]
		s_arr[stack_size-1] = s	
	#print "debug s=",s
	s = inF.readline()

inF.close()

mad_matrix = Matrix(6,6)
mad_matrix.unit()
n_max = len(lattice_arr)
#n_max = 74
for i in range(n_max):
示例#10
0
	def _makePolynomial(self):
		nPoints = len(self.x_y_err_arr)
		if(nPoints < (self.order+1)):
			self.order = nPoints - 1
		#check if just one of errors is zero
		infoZeroErr = 1.0
		for [x,y,err] in self.x_y_err_arr:
			infoZeroErr *= err
		for i in range(nPoints):
			[x,y,err] = self.x_y_err_arr[i]
			sigma = 1.0
			if(infoZeroErr != 0.):
				sigma = 1.0/(err*err)
			self.x_y_err_arr[i][2] = sigma
		#now make A matrix
		aMatr = Matrix(nPoints,self.order+1)
		for i in range(nPoints):
			for j in range(self.order+1):
				x = self.x_y_err_arr[i][0]
				aMatr.set(i,j,math.pow(x,j))	
		aTCa = Matrix(self.order+1,self.order+1)
		for i in range(self.order+1):
			for j in range(self.order+1):
				a = 0.
				for k in range(nPoints):
					sigma = self.x_y_err_arr[k][2]
					a += aMatr.get(k,i)*sigma*aMatr.get(k,j)
				aTCa.set(i,j,a)
		#now the resuting coefficients and errors		
		aTCaI = aTCa.invert()
		e = aTCaI.mult(aTCa)		
		if(aTCa == None):
			print "python PolynomialFit: Problem with data."
			for i in range(nPoints):
				x = self.x_y_err_arr[i][0]
				y = self.x_y_err_arr[i][1]
				err = self.x_y_err_arr[i][2]
				print " x,y,err = %12.5g %12.5g %12.5g "%(x,y,err)
			print "Stop."
			sys.exit(1)
		coef_arr = [0.]*(self.order+1)			
		err_arr = [0.]*(self.order+1)
		for i in range(self.order+1):
			err_arr[i] = math.sqrt(math.fabs(aTCaI.get(i,i)))
		for i in range(self.order+1):
			coef_arr[i] = 0.
			for j in range(self.order+1):
				for k in range(nPoints):
					sigma = self.x_y_err_arr[k][2]
					y = self.x_y_err_arr[k][1]
					coef_arr[i] += aTCaI.get(i,j)*aMatr.get(k,j)*sigma*y
		# polinimial coefficients are found
		self.polynomial.order(self.order)
		for i in range(len(coef_arr)):
			self.polynomial.coefficient(i,coef_arr[i])	
		# now let's calculate errors
		if(infoZeroErr == 0.):
			total_sigma = 0.
			for k in range(nPoints):
				x = self.x_y_err_arr[k][0]
				y = self.x_y_err_arr[k][1]
				total_sigma += (self.polynomial.value(x)-y)**2
			total_sigma = math.sqrt(total_sigma/(nPoints-2))
			for i in range(len(err_arr)):
				err_arr[i] *= total_sigma
		# set the resulting coefficients and errors array
		self.coef_err_arr = [coef_arr,err_arr]	
b_in = Bunch()
for i in range(nParticles):
	(x,xp,y,yp,z,dE) = (random.random(),random.random(),random.random(),random.random(),random.random(),random.random())
	b_in.addParticle(x,xp,y,yp,z,dE)
	
#----set up ids
ParticleIdNumber.addParticleIdNumbers(b_in)

b_out = Bunch()
b_in.copyEmptyBunchTo(b_out)

#-----set up initial transport matix, later we have to extract it from bunches
mtrxA_init = Matrix(7,7)
mtrxA_init.zero()
for ix in range(6):
	mtrxA_init.set(ix,6,1.0*ix)
mtrxA_init.set(6,6,1.0)
for ix in range(6):
	mtrxA_init.set(ix,ix,1.*(ix+1))
printM(mtrxA_init, "Init M ")
#-----------------------------------------------------------------------------
#------ put particles from b_in bunch to b_out 
noise_level = 0.000001
for i in range(nParticles):
	coord_arr = (b_in.x(i),b_in.xp(i),b_in.y(i),b_in.yp(i),b_in.z(i),b_in.dE(i))
	coord_arr_res = [0.,0.,0.,0.,0.,0.]
	for ix in range(6):
		coord_arr_res[ix] = mtrxA_init.get(ix,6)
		for iy in range(6):
			coord_arr_res[ix] += mtrxA_init.get(ix,iy)*coord_arr[iy]
		coord_arr_res[ix] += noise_level*random.random()
示例#12
0
    bunch = Bunch()
    bunch_in.copyBunchTo(bunch)
    accLattice.trackBunch(bunch, None, None, bnch02_ind)
    transportMtrxFromInitCoords(bunch, trMtrx, use_twiss_weight_x,
                                use_twiss_weight_y, use_twiss_weight_z)
    twiss_analysis.analyzeBunch(bunch)
    x_rms = math.sqrt(
        twiss_analysis.getTwiss(0)[1] * twiss_analysis.getTwiss(0)[3]) * 1000.
    y_rms = math.sqrt(
        twiss_analysis.getTwiss(1)[1] * twiss_analysis.getTwiss(1)[3]) * 1000.
    z_rms = math.sqrt(
        twiss_analysis.getTwiss(2)[1] * twiss_analysis.getTwiss(2)[3]) * 1000.
    z_to_phase_coeff = bunch_gen.getZtoPhaseCoeff(bunch)
    z_rms_deg = z_to_phase_coeff * z_rms / 1000.0
    #----transformation from [m] to [deg] and from [GeV] to [MeV] for z, phi and dE
    trMtrx.set(4, 4 + 1, trMtrx.get(4, 4 + 1) * z_to_phase_coeff / 1000.)
    trMtrx.set(4 + 1, 4, trMtrx.get(4 + 1, 4) * 1000. / z_to_phase_coeff)
    mz11 = trMtrx.get(4, 4)
    mz12 = trMtrx.get(4, 4 + 1)
    #----------------------------------------------------------
    det_z = trMtrx.get(0 + 4, 0 + 4) * trMtrx.get(1 + 4, 1 + 4) - trMtrx.get(
        1 + 4, 0 + 4) * trMtrx.get(0 + 4, 1 + 4)
    res_sizes_trMtrx_arr.append([E0TL, z_rms_deg, z_rms_exp, mz11, mz12])
    print "E0TL[keV] =  %5.1f (z_rms,z_rms_exp) =  %5.3f  %5.1f    det(TrMtrxZ) = %5.4f " % (
        E0TL * 1.0e+6, z_rms_deg, z_rms_exp, det_z)
    #-------------------

#--------------------------------------------------------------
#----------- LSQ method for Longitudinal Twiss parameters
#--------------------------------------------------------------
n_cases = len(res_sizes_trMtrx_arr)
示例#13
0
	def trackDispersionData(self,momentum,mass, disp, disp_p, direction = "x"):
		"""
		Returns the tuple ([(position, disp),...],[(position,disp_p),...] ). 
		The tracking starts from the values specified as the initial parameters. 
		The possible values for direction parameter "x" or "y".
		"""
		if(direction.lower() != "x" and direction.lower() != "y"):
			orbitFinalize("Class orbit.matrix_lattice.MATRIX_Lattice, method trackDispersionData(...): direction should be x or y.")
		#track dispersion
		eps_length = 0.00001 # 10^-6 meter
		dir_ind = 0
		if(direction.lower() == "y"):
			dir_ind = 2
		track_m = Matrix(3,3)
		track_m.unit()
		track_m.set(2,0,0.)
		track_m.set(2,1,0.)			
		track_m.set(2,2,1.)			
		track_v = PhaseVector(3)
		#kinematics coefficient calculation
		Etotal = math.sqrt(momentum**2 + mass**2)
		beta = momentum/Etotal
		gamma = Etotal/mass
		Ekin = Etotal - mass		
		m_coeff =  momentum*momentum/(mass + Ekin)		
		track_v.set(0,disp)
		track_v.set(1,disp_p)
		track_v.set(2,1.)
		position = 0.
		pos_arr = []
		disp_arr = []
		disp_p_arr = []
		#put in array the initial dispersions
		pos_arr.append(position)
		disp_arr.append(track_v.get(0))
		disp_p_arr.append(track_v.get(1))
		position_old = position
		disp_old = disp
		#count = 0
		for matrixNode in self.getNodes():
			if(isinstance(matrixNode,BaseMATRIX) == True):
				disp = track_v.get(0)				
				if(abs(position_old-position) > eps_length or abs(disp_old - disp) > eps_length):
					pos_arr.append(position)
					disp_arr.append(track_v.get(0))
					disp_p_arr.append(track_v.get(1))
				disp_old = disp
				position_old = position
				mt = matrixNode.getMatrix()
				ind0 = 0+dir_ind
				ind1 = 1+dir_ind
				track_m.set(0,0,mt.get(ind0,ind0))
				track_m.set(0,1,mt.get(ind0,ind1))
				track_m.set(1,0,mt.get(ind1,ind0))
				track_m.set(1,1,mt.get(ind1,ind1))		
				track_m.set(0,2,mt.get(ind0,5)*m_coeff)
				track_m.set(1,2,mt.get(ind1,5)*m_coeff)
				track_v = track_m.mult(track_v)	
				position = position + matrixNode.getLength()
		pos_arr.append(position)
		disp_arr.append(track_v.get(0))
		disp_p_arr.append(track_v.get(1))
		#pack the resulting tuple
		graph_disp_arr = []
		graph_disp_p_arr = []
		for i in range(len(pos_arr)):
			graph_disp_arr.append((pos_arr[i],disp_arr[i]))
			graph_disp_p_arr.append((pos_arr[i],disp_p_arr[i]))
		return (graph_disp_arr,graph_disp_p_arr)
示例#14
0
	def trackTwissData(self, alpha, beta, direction = "x"):
		"""
		Returns the tuple ([(position, phase advance/2/pi),...], [(position, alpha),...],[(position,beta),...] ).
		The tracking starts from the values specified as the initial parameters. 
		The possible values for direction parameter "x" or "y".
		"""
		if(direction.lower() != "x" and direction.lower() != "y"):
			orbitFinalize("Class orbit.matrix_lattice.MATRIX_Lattice, method trackTwissData(...): direction should be x or y.")
		#track twiss 
		eps_length = 0.00001 # 10^-6 meter
		dir_ind = 0
		if(direction.lower() == "y"):
			dir_ind = 2
		gamma = (1.0+alpha*alpha)/beta
		track_m = Matrix(3,3)
		track_m.unit()
		track_v = PhaseVector(3)
		track_v.set(0,alpha)
		track_v.set(1,beta)
		track_v.set(2,gamma)
		position = 0.
		pos_arr = []
		alpha_arr = []
		beta_arr = []
		#phi is for tune accumulation
		phi = 0.
		#phase advance 
		mu_arr = []
		#put in array the initial twiss
		pos_arr.append(position)
		mu_arr.append(phi)
		alpha_arr.append(track_v.get(0))
		beta_arr.append(track_v.get(1))
		position_old = position
		beta_old = beta
		#count = 0
		for matrixNode in self.getNodes():
			if(isinstance(matrixNode,BaseMATRIX) == True):
				beta = track_v.get(1)
				if(abs(position_old-position) > eps_length or abs(beta_old - beta) > eps_length):
					pos_arr.append(position)
					alpha_arr.append(track_v.get(0))
					beta_arr.append(track_v.get(1))
					mu_arr.append(phi/(2*math.pi))
				mt = matrixNode.getMatrix()
				ind0 = 0+dir_ind
				ind1 = 1+dir_ind
				track_m.set(0,0,mt.get(ind0,ind0)*mt.get(ind1,ind1)+mt.get(ind0,ind1)*mt.get(ind1,ind0))
				track_m.set(0,1,-mt.get(ind0,ind0)*mt.get(ind1,ind0))
				track_m.set(0,2,-mt.get(ind0,ind1)*mt.get(ind1,ind1))
				track_m.set(1,0,-2*mt.get(ind0,ind0)*mt.get(ind0,ind1))
				track_m.set(1,1,mt.get(ind0,ind0)*mt.get(ind0,ind0))
				track_m.set(1,2,mt.get(ind0,ind1)*mt.get(ind0,ind1))
				track_m.set(2,0,-2*mt.get(ind1,ind0)*mt.get(ind1,ind1))
				track_m.set(2,1,mt.get(ind1,ind0)*mt.get(ind1,ind0))
				track_m.set(2,2,mt.get(ind1,ind1)*mt.get(ind1,ind1))
				alpha_0 = track_v.get(0)
				beta_0 = track_v.get(1)
				delta_phi = math.atan(	mt.get(ind0,ind1)/(	beta_0*mt.get(ind0,ind0) - alpha_0*mt.get(ind0,ind1)))
				phi = phi + delta_phi
				track_v = track_m.mult(track_v)	
				position_old = position
				beta_old = beta_0
				position = position + matrixNode.getLength()
				#count = count + 1
		pos_arr.append(position)
		alpha_arr.append(track_v.get(0))
		beta_arr.append(track_v.get(1))
		mu_arr.append(phi/(2*math.pi))
		#pack the resulting tuple
		tune = phi/(2*math.pi)	
		graph_alpha_arr = []
		graph_beta_arr = []
		graph_mu_arr = []
		for i in range(len(pos_arr)):
			graph_mu_arr.append((pos_arr[i], mu_arr[i]))
			graph_alpha_arr.append((pos_arr[i],alpha_arr[i]))
			graph_beta_arr.append((pos_arr[i],beta_arr[i]))
		return (graph_mu_arr,graph_alpha_arr,graph_beta_arr)
示例#15
0
	g1 = g_func.y(ind)
	z_avg = (z1+z0)/2
	g_avg = (g0+g1)/2
	g_avg *= charge
	dL = z1-z0
	#print "debug dL=",dL, " y= %12.3f "%y_avg
	#---- this is a transport 4x4 matrix for coordinates x,x',y,y'
	trMtrx = Matrix(4,4)
	k2 = abs(g_avg/Brho)
	kq = math.sqrt(k2)
	sin_kL = math.sin(kq*dL)
	cos_kL = math.cos(kq*dL)
	sinh_kL = math.sinh(kq*dL)
	cosh_kL = math.cosh(kq*dL)
	if(g_avg == 0.):
		trMtrx.set(0,0,1.)
		trMtrx.set(1,1,1.)
		trMtrx.set(2,2,1.)
		trMtrx.set(3,3,1.)
		trMtrx.set(0,1,dL)
		trMtrx.set(2,3,dL)
	else:
		if(g_avg > 0.):
			trMtrx.set(0,0,cos_kL)
			trMtrx.set(1,1,cos_kL)
			trMtrx.set(0,1,sin_kL/kq)	
			trMtrx.set(1,0,-kq*sin_kL)
			#-------------------------
			trMtrx.set(2,2,cosh_kL)
			trMtrx.set(3,3,cosh_kL)
			trMtrx.set(2,3,sinh_kL/kq)