def match_tunes(lat, tw0, quads, nu_x, nu_y, ncells=1, print_proc=0): print("matching start .... ") end = Monitor(eid="end") lat = MagneticLattice(lat.sequence + [end]) # tw0.E = lat.energy tws = twiss(lat, tw0, nPoints=None) nu_x_old = tws[-1].mux / 2 / np.pi * ncells nu_y_old = tws[-1].muy / 2 / np.pi * ncells # print nu_y, nu_y_old strengths1 = [p.k1 for p in quads] constr = { end: { 'mux': 2 * np.pi * nu_x / ncells, 'muy': 2. * np.pi * nu_y / ncells }, 'periodic': True } # print constr vars = quads match(lat, constr, vars, tws[0]) for i, q in enumerate(quads): print(q.id, ".k1: before: ", strengths1[i], " after: ", q.k1) lat = MagneticLattice(lat.sequence[:-1]) tws = twiss(lat, tw0, nPoints=None) print("nu_x: before: ", nu_x_old, "after: ", tws[-1].mux / 2 / np.pi * ncells) print("nu_y: before: ", nu_y_old, "after: ", tws[-1].muy / 2 / np.pi * ncells) print("matching end.") return lat
def __init__(self, lattice, beam, coupling=0.01, nsuperperiod=1, tws0=None): if beam.E == 0: exit("beam.E must be non zero!") self.E = beam.E if tws0 == None: tws = twiss(lattice, Twiss(beam)) self.tws0 = tws[0] else: #tws0.E = lattice.energy self.tws0 = tws0 tws = twiss(lattice, tws0) self.lat = lattice (I1, I2, I3, I4, I5) = radiation_integrals(lattice, self.tws0, nsuperperiod) self.I1 = I1 self.I2 = I2 self.I3 = I3 self.I4 = I4 self.I5 = I5 #print "I2 = ", I2 #print "I3 = ", I3 #print "I4 = ", I4 #print "I5 = ", I5 self.Je = 2 + I4 / I2 self.Jx = 1 - I4 / I2 self.Jy = 1 self.gamma = self.E / m_e_GeV self.sigma_e = self.gamma * sqrt(Cq * self.I3 / (self.Je * I2)) self.emittance = Cq * self.gamma * self.gamma * self.I5 / (self.Jx * self.I2) self.U0 = Cgamma * (beam.E * 1000)**4 * self.I2 / (2 * pi) #print "********* ", twiss_0.Energy self.Tperiod = nsuperperiod * lattice.totalLen / speed_of_light self.Length = nsuperperiod * lattice.totalLen self.tau0 = 2 * self.E * 1000 * self.Tperiod / self.U0 self.tau_e = self.tau0 / self.Je self.tau_x = self.tau0 / self.Jx self.tau_y = self.tau0 / self.Jy self.alpha = self.I1 / (speed_of_light * self.Tperiod) self.coupl = coupling self.emitt_x = self.emittance / (1 + self.coupl) self.emitt_y = self.emittance * self.coupl / (1 + self.coupl) self.sigma_x = sqrt((self.sigma_e * self.tws0.Dx)**2 + self.emitt_x * self.tws0.beta_x) self.sigma_y = sqrt((self.sigma_e * self.tws0.Dy)**2 + self.emitt_y * self.tws0.beta_y) self.sigma_xp = sqrt((self.sigma_e * self.tws0.Dxp)**2 + self.emitt_x * self.tws0.gamma_x) self.sigma_yp = sqrt((self.sigma_e * self.tws0.Dyp)**2 + self.emitt_y * self.tws0.gamma_y)
def compensate_chromaticity(lattice, ksi_x_comp=0, ksi_y_comp=0, nsuperperiod=1): ''' old chromaticity compensation with 2 sextupole families ''' tws0 = Twiss() tws = twiss(lattice, tws0) tws_0 = tws[0] ksi_comp = (ksi_x_comp, ksi_y_comp) ksi = natural_chromaticity(lattice, tws_0, nsuperperiod) print("ksi_x = ", ksi[0]) print("ksi_y = ", ksi[1]) sex_dict_stg = calculate_sex_strength(lattice, tws_0, ksi, ksi_comp, nsuperperiod) print("Chromaticity compensatation: After: ", sex_dict_stg) #print sex_dict_stg sex_name = list(sex_dict_stg.keys()) for element in lattice.sequence: if element.__class__ == Sextupole: if element.id == sex_name[0]: element.k2 = sex_dict_stg[element.id] / element.l #if element.l != 0: element.k2 = element.ms / element.l elif element.id == sex_name[1]: element.k2 = sex_dict_stg[element.id] / element.l #if element.l != 0: element.k2 = element.ms / element.l if element.__class__ == Multipole and element.n == 3: if element.id == sex_name[0]: element.kn[2] = sex_dict_stg[element.id] elif element.id == sex_name[1]: element.kn[2] = sex_dict_stg[element.id] lattice.update_transfer_maps()
def compensate_chromaticity(lattice, ksi_x_comp=0, ksi_y_comp=0, nsuperperiod=1): ''' old chromaticity compensation with 2 sextupole families ''' tws0 = Twiss() tws = twiss(lattice, tws0) tws_0 = tws[0] ksi_comp = (ksi_x_comp, ksi_y_comp) ksi = natural_chromaticity(lattice, tws_0, nsuperperiod) print("ksi_x = ", ksi[0]) print("ksi_y = ", ksi[1]) sex_dict_stg = calculate_sex_strength(lattice, tws_0, ksi, ksi_comp, nsuperperiod) print("Chromaticity compensatation: After: ", sex_dict_stg) #print sex_dict_stg sex_name = list(sex_dict_stg.keys()) for element in lattice.sequence: if element.__class__ == Sextupole: if element.id == sex_name[0]: element.k2 = sex_dict_stg[element.id] / element.l #if element.l != 0: element.k2 = element.ms / element.l elif element.id == sex_name[1]: element.k2 = sex_dict_stg[element.id] / element.l #if element.l != 0: element.k2 = element.ms / element.l if element.__class__ == Multipole and element.n == 3: if element.id == sex_name[0]: element.kn[2] = sex_dict_stg[element.id] elif element.id == sex_name[1]: element.kn[2] = sex_dict_stg[element.id] lattice.update_transfer_maps()
def __init__(self, lattice, beam, coupling = 0.01, nsuperperiod = 1, tws0 = None): if beam.E == 0: exit("beam.E must be non zero!") self.E = beam.E if tws0 == None: tws = twiss(lattice, Twiss(beam)) self.tws0 = tws[0] else: #tws0.E = lattice.energy self.tws0 = tws0 tws = twiss(lattice, tws0) self.lat = lattice (I1,I2,I3, I4, I5) = radiation_integrals(lattice, self.tws0 , nsuperperiod) self.I1 = I1 self.I2 = I2 self.I3 = I3 self.I4 = I4 self.I5 = I5 #print "I2 = ", I2 #print "I3 = ", I3 #print "I4 = ", I4 #print "I5 = ", I5 self.Je = 2 + I4/I2 self.Jx = 1 - I4/I2 self.Jy = 1 self.gamma = self.E/m_e_GeV self.sigma_e = self.gamma*sqrt(Cq * self.I3/(self.Je*I2)) self.emittance = Cq*self.gamma*self.gamma * self.I5/(self.Jx* self.I2) self.U0 = Cgamma*(beam.E*1000)**4*self.I2/(2*pi) #print "********* ", twiss_0.Energy self.Tperiod = nsuperperiod*lattice.totalLen/speed_of_light self.Length = nsuperperiod*lattice.totalLen self.tau0 = 2*self.E*1000*self.Tperiod/self.U0 self.tau_e = self.tau0/self.Je self.tau_x = self.tau0/self.Jx self.tau_y = self.tau0/self.Jy self.alpha = self.I1/(speed_of_light*self.Tperiod) self.coupl = coupling self.emitt_x = self.emittance/(1 + self.coupl) self.emitt_y = self.emittance*self.coupl/(1 + self.coupl) self.sigma_x = sqrt((self.sigma_e*self.tws0.Dx)**2 + self.emitt_x*self.tws0.beta_x) self.sigma_y = sqrt((self.sigma_e*self.tws0.Dy)**2 + self.emitt_y*self.tws0.beta_y) self.sigma_xp = sqrt((self.sigma_e*self.tws0.Dxp)**2 + self.emitt_x*self.tws0.gamma_x) self.sigma_yp = sqrt((self.sigma_e*self.tws0.Dyp)**2 + self.emitt_y*self.tws0.gamma_y)
def __init__(self, lattice, tws0, coupling=0.01, nsuperperiod=1): if tws0.__class__ is not Twiss: _logger.info(" EbeamParams: tws0 is not Twiss class.") return self.tws0 = tws0 self.E = tws0.E if tws0.beta_x == 0 and tws0.beta_y == 0: tws = twiss(lattice, tws0) self.tws0 = tws[0] if self.E == 0.0: _logger.info("tws0.E is 0. Some parameters will be NaN") self.lat = lattice (I1, I2, I3, I4, I5) = radiation_integrals(lattice, self.tws0, nsuperperiod) self.I1 = I1 self.I2 = I2 self.I3 = I3 self.I4 = I4 self.I5 = I5 self.Je = 2.0 + I4 / I2 self.Jx = 1.0 - I4 / I2 self.Jy = 1.0 self.gamma = self.E / m_e_GeV self.sigma_e = self.gamma * np.sqrt(Cq * self.I3 / (self.Je * I2)) self.emittance = Cq * self.gamma * self.gamma * self.I5 / (self.Jx * self.I2) self.U0 = Cgamma * (self.E * 1000.0)**4 * self.I2 / (2.0 * np.pi) self.Tperiod = nsuperperiod * lattice.totalLen / speed_of_light self.Length = nsuperperiod * lattice.totalLen self.tau0 = 2.0 * self.E * 1000.0 * self.Tperiod / self.U0 if self.U0 != 0.0 else np.nan self.tau_e = self.tau0 / self.Je self.tau_x = self.tau0 / self.Jx self.tau_y = self.tau0 / self.Jy self.alpha = self.I1 / (speed_of_light * self.Tperiod) self.coupl = coupling self.emitt_x = self.emittance / (1.0 + self.coupl) self.emitt_y = self.emittance * self.coupl / (1.0 + self.coupl) self.sigma_x = np.sqrt((self.sigma_e * self.tws0.Dx)**2 + self.emitt_x * self.tws0.beta_x) self.sigma_y = np.sqrt((self.sigma_e * self.tws0.Dy)**2 + self.emitt_y * self.tws0.beta_y) self.sigma_xp = np.sqrt((self.sigma_e * self.tws0.Dxp)**2 + self.emitt_x * self.tws0.gamma_x) self.sigma_yp = np.sqrt((self.sigma_e * self.tws0.Dyp)**2 + self.emitt_y * self.tws0.gamma_y)
def DZ(lattice, energy): tws0 = Twiss() tws = twiss(lattice, tws0) R = lattice_transfer_map(lattice, energy) #print np.array(R[:4, :4])- np.eye(4),R[:4, 5] x = dot(R, [1, 1, 1, 1, 0, 0]) x2 = dot(R, [1, 1, 1, 1, 0, 0.001]) #print (x2 - x)/0.001 #print R w, v = eig(R) #print R v1 = dot(R, v[0].real) #print "v0 = ", v[0].real #print "v*R = ", v1 DZ = np.dot(inv(-R[:4, :4] + np.eye(4)), R[:4, 5]) DZ = np.append(DZ, [0, 1]) print("DZ0 = ", DZ) DR_new = np.zeros(6) DZ0 = deepcopy(DZ) DQ1n = 0. DQ2n = 0. R_lat = np.eye(6) for elem in lattice.sequence: R = elem.transfer_map.R(energy) R_lat = dot(R, R_lat) #cosmx = (R[0, 0] + R[1, 1])/2. #cosmy = (R[2, 2] + R[3, 3])/2. #if abs(cosmx) >= 1 or abs(cosmy) >= 1: # logger.warn("************ periodic solution does not exist. return None ***********") # #print("************ periodic solution does not exist. return None ***********") # return None # nsinmx = np.sign(R[0, 1])*sqrt(R[0,1]*R[1, 0] - (R[0, 0]*R[1,1])/4.) # nsinmy = np.sign(R[2, 3])*sqrt(R[2,3]*R[3, 2] - (R[2, 2]*R[3,3])/4.) DZ = dot(R_lat, DZ0) DR = np.zeros((6, 6)) T = elem.transfer_map.t_mat_z_e(elem.l, energy) for i in range(6): for j in range(6): t = 0. for m in range(6): t += T[i, j, m] * DZ[m] DR[i, j] = 2 * t # np.dot(lattice.T[i, j, :], DZ) print(elem.l) print(DR) DR_new = DR + DR_new #DQ1n += (DR[0, 0] + DR[1, 1])/(2*sin(tws[-1].mux))/2/pi #DQ2n += (DR[2, 2] + DR[3, 3])/(2*sin(tws[-1].muy))/2/pi print("DZ = ", DZ) #print "DZ2 = ", dot(R, DZ) print(DQ1n * 6, DQ2n * 6) DR = np.zeros((6, 6)) for i in range(6): for j in range(6): t = 0. for m in range(6): t += lattice.T[i, j, m] * DZ[m] DR[i, j] = 2 * t # np.dot(lattice.T[i, j, :], DZ) #print DR DR = DR_new DQ1 = (DR[0, 0] + DR[1, 1]) / (2 * sin(tws[-1].mux)) / 2 / pi DQ2 = (DR[2, 2] + DR[3, 3]) / (2 * sin(tws[-1].muy)) / 2 / pi print(DQ1 * 6, DQ2 * 6) print(tws[-1].mux / 2 / pi * 6) print(tws[-1].muy / 2 / pi * 6)
def DZ(lattice, energy): tws0 = Twiss() tws = twiss(lattice, tws0) R = lattice_transfer_map(lattice, energy) #print np.array(R[:4, :4])- np.eye(4),R[:4, 5] x = dot(R, [1,1,1,1,0,0]) x2 = dot(R, [1,1,1,1,0,0.001]) #print (x2 - x)/0.001 #print R w, v = eig(R) #print R v1 = dot(R,v[0].real) #print "v0 = ", v[0].real #print "v*R = ", v1 DZ = np.dot(inv(-R[:4, :4] + np.eye(4)), R[:4, 5]) DZ = np.append(DZ, [0, 1]) print("DZ0 = ", DZ) DR_new = np.zeros(6) DZ0 = deepcopy(DZ) DQ1n = 0. DQ2n = 0. R_lat = np.eye(6) for elem in lattice.sequence: R = elem.transfer_map.R(energy) R_lat = dot(R, R_lat) #cosmx = (R[0, 0] + R[1, 1])/2. #cosmy = (R[2, 2] + R[3, 3])/2. #if abs(cosmx) >= 1 or abs(cosmy) >= 1: # logger.warn("************ periodic solution does not exist. return None ***********") # #print("************ periodic solution does not exist. return None ***********") # return None # nsinmx = np.sign(R[0, 1])*sqrt(R[0,1]*R[1, 0] - (R[0, 0]*R[1,1])/4.) # nsinmy = np.sign(R[2, 3])*sqrt(R[2,3]*R[3, 2] - (R[2, 2]*R[3,3])/4.) DZ = dot(R_lat, DZ0) DR = np.zeros((6,6)) T = elem.transfer_map.t_mat_z_e(elem.l, energy) for i in range(6): for j in range(6): t = 0. for m in range(6): t += T[i,j,m]*DZ[m] DR[i,j] = 2*t # np.dot(lattice.T[i, j, :], DZ) print(elem.l) print(DR) DR_new = DR + DR_new #DQ1n += (DR[0, 0] + DR[1, 1])/(2*sin(tws[-1].mux))/2/pi #DQ2n += (DR[2, 2] + DR[3, 3])/(2*sin(tws[-1].muy))/2/pi print ("DZ = ", DZ) #print "DZ2 = ", dot(R, DZ) print(DQ1n*6, DQ2n*6) DR = np.zeros((6,6)) for i in range(6): for j in range(6): t = 0. for m in range(6): t += lattice.T[i,j,m]*DZ[m] DR[i,j] = 2*t # np.dot(lattice.T[i, j, :], DZ) #print DR DR = DR_new DQ1 = (DR[0,0] + DR[1,1])/(2*sin(tws[-1].mux))/2/pi DQ2 = (DR[2,2] + DR[3,3])/(2*sin(tws[-1].muy))/2/pi print(DQ1*6, DQ2*6) print(tws[-1].mux/2/pi*6) print(tws[-1].muy/2/pi*6)