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)
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)
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)
res_matrix = Matrix(6,6) res_matrix.unit() n_max = len(teapot_latt.getNodes()) #n_max = 74 length = 0. for i in range(n_max): node = teapot_latt.getNodes()[i] print "i=",i," name =",node.getName()," L=",length m = Matrix(6,6) matrixGenerator.initBunch(matrixTracker.b) node.trackBunch(matrixTracker.b) matrixGenerator.calculateMatrix(matrixTracker.b,m) #m_prt0 = m.copy() #transormToMAD(m_prt0,momentum,beta) #printM(m_prt0) res_matrix = m.mult(res_matrix) length = length + node.getLength() print "====res for TEAPOT n elements=",n_max," L=",length transormToMAD(res_matrix,momentum,beta) printM(res_matrix) m = transp_matrix det_x = m.get(0,0)*m.get(1,1) - m.get(0,1)*m.get(1,0) print "determinant det(Mx)=",det_x det_y = m.get(2,2)*m.get(3,3) - m.get(2,3)*m.get(3,2) print "determinant det(My)=",det_y cos_phi_x = (m.get(0,0)+m.get(1,1))/2.0 cos_phi_y = (m.get(2,2)+m.get(3,3))/2.0
printV(v1) v3 = v.add(1) printV(v3) v3 = v.add(v1) printV(v3) print "v*v1 =", v.mult(v1) print "-----------------------------------------------" #check matrix print "----mult ----" m = Matrix(5, 4) m1 = Matrix(4, 5) setM(m) setM(m1) m3 = m.mult(m1) printM(m3) print "----add -----" m = Matrix(4, 4) m1 = Matrix(4, 4) setM(m) setM(m1) m3 = m.add(m1) printM(m3) m4 = m3.invert() if (m4 == None): print "Cannot invert matrix:" printM(m3) sys.exit(1)
#print "z sigma relative[%] = ",sigma_z_rel*100. print "===================================================================" matrx_W = Matrix(n_cases, n_cases) for ind0 in range(n_cases): for ind1 in range(n_cases): matrx_W.set(ind0, ind1, 0.) if (ind0 == ind1): #matrx_W.set(ind0,ind1,1./(2*z_rms_2_vctr.get(ind0)*sigma_z_rel)**2) matrx_W.set( ind0, ind1, 1. / (2 * math.sqrt(z_rms_2_vctr.get(ind0)) * sigma_z)**2) #---- (MT*W*M)^(-1) matrx_MT = Matrix(matrx_M) matrx_MT.transpose() MTWM_m1 = ((matrx_MT.mult(matrx_W)).mult(matrx_M)).invert() long_corr_results_vector = MTWM_m1.mult( matrx_MT.mult(matrx_W.mult(z_rms_2_vctr))) phase2_rms_res = long_corr_results_vector.get(0) phase_ekin_corr_res = long_corr_results_vector.get(1) ekin2_rms_res = long_corr_results_vector.get(2) phase_rms_res = math.sqrt(phase2_rms_res) ekin_rms_res = math.sqrt(ekin2_rms_res) #---- Error estimation phase_rms_err = math.sqrt(MTWM_m1.get(0, 0)) / (2 * phase_rms_res) phase_ekin_corr_err = math.sqrt(MTWM_m1.get(1, 1)) ekin_rms_err = math.sqrt(MTWM_m1.get(2, 2)) / (2 * ekin_rms_res) print "================Zero Iteration for Long. Twiss at BNCH02 entrance================"
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): (name,matrix) = lattice_arr[i] #print "i=",i," name =",name #printM(mad_matrix) mad_matrix = matrix.mult(mad_matrix) #printM(matrix) #printM(mad_matrix) print "=========One turn MAD matrix======== MAD n_elements=",n_max printM(mad_matrix) print "Stop."
res_matrix = Matrix(6, 6) res_matrix.unit() n_max = len(teapot_latt.getNodes()) #n_max = 74 length = 0. for i in range(n_max): node = teapot_latt.getNodes()[i] print "i=", i, " name =", node.getName(), " L=", length m = Matrix(6, 6) matrixGenerator.initBunch(matrixTracker.b) node.trackBunch(matrixTracker.b) matrixGenerator.calculateMatrix(matrixTracker.b, m) #m_prt0 = m.copy() #transormToMAD(m_prt0,momentum,beta) #printM(m_prt0) res_matrix = m.mult(res_matrix) length = length + node.getLength() print "====res for TEAPOT n elements=", n_max, " L=", length transormToMAD(res_matrix, momentum, beta) printM(res_matrix) m = transp_matrix det_x = m.get(0, 0) * m.get(1, 1) - m.get(0, 1) * m.get(1, 0) print "determinant det(Mx)=", det_x det_y = m.get(2, 2) * m.get(3, 3) - m.get(2, 3) * m.get(3, 2) print "determinant det(My)=", det_y cos_phi_x = (m.get(0, 0) + m.get(1, 1)) / 2.0 cos_phi_y = (m.get(2, 2) + m.get(3, 3)) / 2.0
printV(v1) v3 = v.add(1) printV(v3) v3 = v.add(v1) printV(v3) print "v*v1 =", v.mult(v1) print "-----------------------------------------------" #check matrix print "----mult ----" m = Matrix(5,4) m1 = Matrix(4,5) setM(m) setM(m1) m3 = m.mult(m1) printM(m3) print "----add -----" m = Matrix(4,4) m1 = Matrix(4,4) setM(m) setM(m1) m3 = m.add(m1) printM(m3) m4 = m3.invert() if(m4 == None): print "Cannot invert matrix:"; printM(m3); sys.exit(1) printM(m4.mult(m3)) print "-----------------------------------------------"
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)
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)
twiss_v_ini.set(4,betaY) twiss_v_ini.set(5,gammaY) print "===========================================" print "Initial Twiss parameters:" printV(twiss_v_ini) print "===========================================" fl_out = open("mebt_twiss_matr_distributed_quads.dat","w") st = " ind x[m] alphaX betaX alphaY betaY " fl_out.write(st+"\n") v_in = twiss_v_ini for ind in range(len(matrix_arr)): [z_avg,trMtrx,trTwissMtrx] = matrix_arr[ind] v_out = trTwissMtrx.mult(v_in) alpha_x=v_out.get(0) beta_x= v_out.get(1) gamma_x=v_out.get(2) alpha_y=v_out.get(3) beta_y= v_out.get(4) gamma_y=v_out.get(5) v_in = v_out st = " %4d %9.3f %12.6f %12.6f %12.6f %12.6f "%(ind,z_avg,alpha_x,beta_x,alpha_y,beta_y) if(ind % 100 == 0): print st fl_out.write(st+"\n") fl_out.close()