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)
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 setM(m): for i in xrange(m.size()[0]): for j in xrange(m.size()[1]): if (i == j): m.set(i, j, i + 1.0) def setV(v): for i in xrange(v.size()): v.set(i, i + 1.0) print "Start." #check vector print "-----------------------------------------------" v = PhaseVector(5) v1 = PhaseVector(5) setV(v) setV(v1) printV(v) 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 ----"
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)
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) matrx_M = Matrix(n_cases, 3) z_rms_2_vctr = PhaseVector(n_cases) line_ind = 0 for [E0TL, z_rms_deg, z_rms_exp, mz11, mz12] in res_sizes_trMtrx_arr: matrx_M.set(line_ind, 0, mz11**2) matrx_M.set(line_ind, 1, 2 * mz11 * mz12) matrx_M.set(line_ind, 2, mz12**2) #---------------------------- z_rms_2_vctr.set(line_ind, z_rms_exp**2) #---------------------------- line_ind += 1 #---- error matrix sigma_z in deg and sigma_z_rel - relative sigma_z = 0.1 #sigma_z_rel = 0.05 print "======== Error of longitudinal rms size in deg from DTL scans ===="
def correctTrajectory(self, bunch_initial): """ This method will calculate and applyes fields to dipole correctors to achive minimal beam deviation from the center at all BPMs. LSQM method is used. """ bunch_in = self.makeOneParticleBunch(bunch_initial) #-------------------------- bunch_init = Bunch() bunch_in.copyEmptyBunchTo(bunch_init) #---- add one particle to the bunch_init x0 = 0. xp0 = 0. y0 = 0. yp0 = 0. z0 = 0. dE = 0. bunch_init.addParticle(x0, xp0, y0, yp0, z0, dE) #---------------------------------- n_bpms = len(self.bpm_node_arr) n_corrs_hor = len(self.dch_node_arr) n_corrs_ver = len(self.dcv_node_arr) horResponceMtrx = Matrix(n_bpms, n_corrs_hor) verResponceMtrx = Matrix(n_bpms, n_corrs_ver) self._calculatBPM_Matrix(bunch_init, horResponceMtrx, self.dch_node_arr, axis=0) self._calculatBPM_Matrix(bunch_init, verResponceMtrx, self.dcv_node_arr, axis=1) #printM(horResponceMtrx) horResponceMtrxTr = horResponceMtrx.copy() horResponceMtrxTr.transpose() verResponceMtrxTr = verResponceMtrx.copy() verResponceMtrxTr.transpose() horAmtrx = (horResponceMtrxTr.mult(horResponceMtrx)).invert() verAmtrx = (verResponceMtrxTr.mult(verResponceMtrx)).invert() #printM(horAmtrx) #print "det(horAmtrx) = ",horAmtrx.det() horATmtrx = horAmtrx.mult(horResponceMtrxTr) verATmtrx = verAmtrx.mult(verResponceMtrxTr) #---- matrices for LSQM are ready - now use initial bunch to get #---- the trajectory that should be flattened bunch_init = Bunch() bunch_in.copyBunchTo(bunch_init) (bpm_value_hor_arr, bpm_value_ver_arr) = self.calculateTrajectory(bunch_init) horBPM_V = PhaseVector(len(bpm_value_hor_arr)) for ind in range(len(bpm_value_hor_arr)): horBPM_V.set(ind, bpm_value_hor_arr[ind]) verBPM_V = PhaseVector(len(bpm_value_ver_arr)) for ind in range(len(bpm_value_ver_arr)): verBPM_V.set(ind, bpm_value_ver_arr[ind]) dch_val_V = horATmtrx.mult(horBPM_V) dcv_val_V = verATmtrx.mult(verBPM_V) #print "debug =========== final DCH fields ================" for ind in range(dch_val_V.size()): dc = self.dch_node_arr[ind] delta_field = dch_val_V.get(ind) #print "debug corr =",dc.getName()," init field [T] =",dc.getParam("B")," delta [T] =",delta_field dc.setParam("B", dc.getParam("B") - delta_field) #print "debug =========== final DCV fields ================" for ind in range(dcv_val_V.size()): dc = self.dcv_node_arr[ind] delta_field = dcv_val_V.get(ind) #print "debug corr =",dc.getName()," init field [T] =",dc.getParam("B")," delta [T] =",delta_field dc.setParam("B", dc.getParam("B") - delta_field)
#---------------------------------------------------------- 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( [E0L, E0TL, (x_rms, y_rms, z_rms_deg), mz11, mz12]) print "E0TL[keV] = %5.1f (Sx,Sy,Sz) = %5.3f %5.3f %5.1f det(TrMtrxZ) = %5.4f " % ( E0TL * 1000. * 1000., x_rms, y_rms, z_rms_deg, det_z) #------------------- count += 1 #-------------------------------------------------------------- #----------- LSQ method for Longitudinal Twiss parameters #-------------------------------------------------------------- n_cases = len(res_sizes_trMtrx_arr) matrx_M = Matrix(n_cases, 3) z_rms_2_vctr = PhaseVector(n_cases) line_ind = 0 for [E0L, E0TL, (x_rms, y_rms, z_rms_deg), mz11, mz12] in res_sizes_trMtrx_arr: #print "E0TL[keV] = %5.1f (Sx,Sy,Sz) = %5.3f %5.3f %5.1f "%(E0TL*1000.*1000.,x_rms,y_rms,z_rms_deg) matrx_M.set(line_ind, 0, mz11**2) matrx_M.set(line_ind, 1, 2 * mz11 * mz12) matrx_M.set(line_ind, 2, mz12**2) #---------------------------- z_rms_2_vctr.set(line_ind, z_rms_deg**2) #---------------------------- line_ind += 1 #---- error matrix sigma_z in deg and sigma_z_rel - relative sigma_z = 1.0 sigma_z_rel = 0.05
def setM(m): for i in xrange(m.size()[0]): for j in xrange(m.size()[1]): if(i == j): m.set(i,j,i+1.0) def setV(v): for i in xrange(v.size()): v.set(i,i+1.0) print "Start." #check vector print "-----------------------------------------------" v = PhaseVector(5) v1 = PhaseVector(5) setV(v) setV(v1) printV(v) 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 ----"
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)
matrix_arr.append([z_avg,trMtrx,trTwissMtrx]) #print "debug trMtrx ====" #printM(trMtrx) #print "debug trTwissMtrx ====" #printM(trTwissMtrx) #sys.exit() #---------------------------------------------- (alphaX,betaX,emittX) = (-1.9620, 0.1831, 0.21) (alphaY,betaY,emittY) = ( 1.7681, 0.1620, 0.21) gammaX = (1+alphaX**2)/betaX gammaY = (1+alphaY**2)/betaY twiss_v_ini = PhaseVector(6) twiss_v_ini.set(0,alphaX) twiss_v_ini.set(1,betaX) twiss_v_ini.set(2,gammaX) twiss_v_ini.set(3,alphaY) 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")