constr[drift_319] = {'tau':-47.e-4} constr[drift_318] = {'beta_x':['<',50.], 'beta_y':['<',100.]} constr[drift_320] = {'beta_x':['<',50.], 'beta_y':['<',100.]} constr[drift_321] = {'beta_x':['<',50.], 'beta_y':['<',100.]} constr[drift_322] = {'beta_x':['<',50.], 'beta_y':['<',100.]} constr[drift_323] = {'beta_x':['<',50.], 'beta_y':['<',100.]} constr[drift_324] = {'beta_x':['<',50.], 'beta_y':['<',100.], 'Dx':0.0, 'Dxp':0.0} constr[drift_314] = {'beta_x':['<',50.], 'beta_y':['<',100.]} constr[drift_315] = {'beta_x':['<',50.], 'beta_y':['<',100.]} constr[drift_316] = {'beta_x':['<',50.], 'beta_y':['<',100.]} constr[drift_317] = {'beta_x':['<',50.], 'beta_y':['<',100.]} variables = [qms2, qk3nr, q2anr, qk1nr, qfa3_1, qda3_1, qda3_2] match(lat_bdc, constr, variables, tw_end,max_iter=20) tws_bdc=twiss(lat_bdc, tw_end) print 'R56=', tws_bdc[-1].tau*1.e4, 'cm' print 'Dxp=', tws_bdc[-1].Dxp show_optics(tws_bdc, lat_bdc, beam, scale = 0.0) i1 = [] i2 = [] i3 = [] s1 = []
betx = 3.00000000391 alfx = 0.601367998599 bety = 7.78436581638 alfy = -1.52660480872 constr = {} constr['global'] = {'beta_x':['<',150.], 'beta_y':['<',150.]} #constr[arc2_end] = {'beta_x':5.0, 'beta_y':9.75284, 'alpha_x':-0.63097627533, 'alpha_y':1.20340638868, 'Dx':0.0, 'Dxp':0.0} constr[arc2_end] = {'beta_x':betx, 'beta_y':bety, 'alpha_x':alfx, 'alpha_y':alfy, 'Dx':0.0, 'Dxp':0.0} variables = [qfa2, qda2, q1a2, q2a2, q3a2, q4a2, q5a2, q6a2] match(lat_arc2, constr, variables, tw_end,max_iter=50) tw_end.p = 1.e-2 tws_arc2=twiss(lat_arc2, tw_end) plot_opt_func(lat_arc2, tws_arc2) print 'R56=', tws_arc2[-1].tau*1.e4, 'cm' print tws_arc2[-1].beta_x, tws_arc2[-1].alpha_x print tws_arc2[-1].beta_y, tws_arc2[-1].alpha_y print tws_arc2[-1].Dx, tws_arc2[-1].Dxp for v in variables: print v.id, v.k1 plt.show()
tw0.Dx = 0.0 tw0.Dxp = 0.0 tw0.p = 1.e-2 tws = twiss(lat, tw0, nPoints=100) print 'R56=', tws[-1].tau show_optics(tws, lat, beam, scale=0.0) plt.show() constr = {m2_bdc: {'tau': -45.e-4, 'Dx': 0.0}} constr['global'] = {'beta_x': ['<', 40], 'beta_y': ['<', 40]} var = [qf1_bdc, qf2_bdc, qf3_bdc, qf4_bdc, qd1_bdc, qd2_bdc, qd3_bdc, qd4_bdc] res = match(lat, constr, var, tw0, max_iter=5000) tws = twiss(lat, tw0, nPoints=100) print 'diff:', tws[-1].tau * 1.e4, tws[-1].Dx, tws[-1].Dxp show_optics(tws, lat, beam, scale=0.0) plt.show() ''' # knob calculation quad_tab = {} for v in var: quad_tab[v] = [] taus = np.linspace(-5.e-4, 60.e-4, 50) errs = []
constr[drift_321] = {'beta_x': ['<', 50.], 'beta_y': ['<', 100.]} constr[drift_322] = {'beta_x': ['<', 50.], 'beta_y': ['<', 100.]} constr[drift_323] = {'beta_x': ['<', 50.], 'beta_y': ['<', 100.]} constr[drift_324] = { 'beta_x': ['<', 50.], 'beta_y': ['<', 100.], 'Dx': 0.0, 'Dxp': 0.0 } constr[drift_314] = {'beta_x': ['<', 50.], 'beta_y': ['<', 100.]} constr[drift_315] = {'beta_x': ['<', 50.], 'beta_y': ['<', 100.]} constr[drift_316] = {'beta_x': ['<', 50.], 'beta_y': ['<', 100.]} constr[drift_317] = {'beta_x': ['<', 50.], 'beta_y': ['<', 100.]} variables = [qms2, qk3nr, q2anr, qk1nr, qfa3_1, qda3_1, qda3_2] match(lat_bdc, constr, variables, tw_end, max_iter=20) tws_bdc = twiss(lat_bdc, tw_end) print 'R56=', tws_bdc[-1].tau * 1.e4, 'cm' print 'Dxp=', tws_bdc[-1].Dxp show_optics(tws_bdc, lat_bdc, beam, scale=0.0) i1 = [] i2 = [] i3 = [] s1 = [] ang = [] for t in tws_bdc: #print t.beta_x, t.Dx, t.Dxp, t.gamma_x
tw0.p = 1.e-2 tws=twiss(lat, tw0, nPoints = 100) print 'R56=', tws[-1].tau show_optics(tws, lat, beam, scale = 0.0) plt.show() constr = {m2_bdc:{'tau': -45.e-4, 'Dx':0.0} } constr['global'] = {'beta_x':['<',40], 'beta_y':['<',40]} var = [qf1_bdc, qf2_bdc, qf3_bdc, qf4_bdc, qd1_bdc, qd2_bdc, qd3_bdc, qd4_bdc] res = match(lat, constr, var, tw0, max_iter=5000) tws=twiss(lat, tw0, nPoints = 100) print 'diff:', tws[-1].tau * 1.e4, tws[-1].Dx, tws[-1].Dxp show_optics(tws, lat, beam, scale = 0.0) plt.show() ''' # knob calculation quad_tab = {} for v in var: quad_tab[v] = []
def action_matching(self): self.ui.old_results.setText('') self.ui.new_results.setText('') QtWidgets.QApplication.processEvents() m_start = Monitor(eid='matching_start') m_end = Monitor(eid='matching_end') # collect variables string = self.prepare_matching_vars() self.ui.old_results.setText(string) QtWidgets.QApplication.processEvents() if self.matching_vars == []: self.ui.new_results.setText( 'Attention\nSelect matchable variables to do matching') QtWidgets.QApplication.processEvents() return # collect constraints constr = self.collect_constraints(monitors=[m_start, m_end]) # prepare lattice self.lattice.elements[m_start.id] = m_start self.lattice.elements[m_end.id] = m_end self.lattice.cell = (m_start, ) + self.lattice.cell + (m_end, ) self.lattice.init_lattice() # matching string += 'Matching ...' self.ui.old_results.setText(string) QtWidgets.QApplication.processEvents() matching_vars = [line['element'] for line in self.matching_vars] result = match(self.lattice.lattice, constr, matching_vars, self.lattice.tws0) # delete matching monitors self.lattice.cell = self.lattice.cell[1:-1] self.lattice.init_lattice() del self.lattice.elements[m_start.id] del self.lattice.elements[m_end.id] # prerare output results old_lines = 'Old variable values:\n' new_lines = 'New variable values:\n' for line in self.matching_vars: old_lines += str(line['element'].id) + '.' + str( line['type']) + ' = ' + str(line['init_val']) + '\n' new_lines += str(line['element'].id) + '.' + str( line['type']) + ' = ' + str( line['element'].__dict__[line['type']]) + '\n' self.ui.old_results.setText(old_lines) self.ui.new_results.setText(new_lines) # update tune blocks for line in self.matching_vars: self.tune_elements[line['elem_id']][line['type']].set_curval( line['element'].__dict__[line['type']], block_signal=True) # replot twiss functions self.matching_done = True self.update_tws_plot()
tw_end.beta_x = 3.00000000313 tw_end.alpha_x = -0.601367997622 tw_end.beta_y = 7.78559521531 tw_end.alpha_y = 1.5144714708 tw_end.p = 1.e-2 tws_bdc = twiss(lat_bdc, tw_end) show_optics(tws_bdc, lat_bdc, beam, scale=0.0) constr = {m2_bdc: {'tau': -45.e-4, 'Dx': 0.0}} constr['global'] = {'beta_x': ['<', 40], 'beta_y': ['<', 40]} variables = [ qf1_bdc, qf2_bdc, qf3_bdc, qf4_bdc, qd1_bdc, qd2_bdc, qd3_bdc, qd4_bdc ] match(lat_bdc, constr, variables, tw_end, max_iter=20000) tws_bdc = twiss(lat_bdc, tw_end) print 'R56=', tws_bdc[-1].tau * 1.e4, 'cm' print 'Dxp=', tws_bdc[-1].Dxp show_optics(tws_bdc, lat_bdc, beam, scale=0.0) plt.plot(map(lambda x: x.Dxp, tws_bdc)) plt.show() tw_end = deepcopy(tws_bdc[-1]) constr = {} constr['global'] = { 'beta_x': ['<', 80.],
#bc_q1.k1 = 0.0; vars = [bc_q2] match(lat, constr, vars, tw0) tws=twiss(lat, tw0, nPoints = 100) print tws[-1].tau * 1.e4, ' cm' r56.append(tws[-1].tau * 1.e4) plt.plot(cas, r56) plt.grid(True) #plt.show() ''' constr = {m2:{'Dx':0.0, 'Dxp':0.0, 'beta_x':['->',m1], 'beta_y':['->',m1]}, m1:{'Dx':0.0, 'Dxp':0.0}} vars = [bc_q1, bc_q2] #bc_q1.k1 = 0.0; vars = [bc_q2] match(lat, constr, vars, tw0) tws=twiss(lat, tw0, nPoints = 100) f=plt.figure() ax = f.add_subplot(211) ax.set_xlim(0, lat.totalLen) f.canvas.set_window_title('Betas [m]') p1, = ax.plot(map(lambda p: p.s, tws), map(lambda p: p.beta_x, tws), lw=2.0) p2, = ax.plot(map(lambda p: p.s, tws), map(lambda p: p.beta_y, tws), lw=2.0) ax2 = ax.twinx() p3, = ax2.plot(map(lambda p: p.s, tws), map(lambda p: p.Dx, tws), 'r-', lw=2.0) plt.grid(True) plt.legend([p1,p2,p3], [r'$\beta_x$',r'$\beta_y$', r'$D_x$'], fancybox=True, framealpha=0.5)
} #print(constr) p_array = generate_parray(chirp=0.0, charge=5e-9, nparticles=20000, energy=0.0065, tws=tws0) tw = get_envelope(p_array) print(tw) #for i, q in enumerate(vars): # q.k1 = res[i] res = match(lat, constr, vars, tw, verbose=True, max_iter=1000, method='simplex') print(res) tws = twiss(lat, tw) plot_opt_func(lat, tws, top_plot=["E"]) plt.show() for i, q in enumerate(vars): q.k1 = res[i] navi = Navigator(lat) navi.unit_step = 0.05 sc = SpaceCharge() sc.step = 1
tw_end.beta_y = 7.78559521531 tw_end.alpha_y = 1.5144714708 tw_end.p = 1.e-2 tws_bdc=twiss(lat_bdc, tw_end) show_optics(tws_bdc, lat_bdc, beam, scale = 0.0) constr = {m2_bdc:{'tau': -45.e-4, 'Dx':0.0} } constr['global'] = {'beta_x':['<',40], 'beta_y':['<',40]} variables = [qf1_bdc, qf2_bdc, qf3_bdc, qf4_bdc, qd1_bdc, qd2_bdc, qd3_bdc, qd4_bdc] match(lat_bdc, constr, variables, tw_end,max_iter=20000) tws_bdc=twiss(lat_bdc, tw_end) print 'R56=', tws_bdc[-1].tau*1.e4, 'cm' print 'Dxp=', tws_bdc[-1].Dxp show_optics(tws_bdc, lat_bdc, beam, scale = 0.0) plt.plot(map(lambda x: x.Dxp, tws_bdc)) plt.show() tw_end = deepcopy(tws_bdc[-1]) constr = {}
tw_end = tws_arc2[-1] tws_sase=twiss(lat_sase, tw_end) tws_sase_old=twiss(lat_sase_old, tw_end) plot_opt_func(lat_sase, tws_sase) plot_opt_func(lat_sase_old, tws_sase_old) tw_end = tws_sase[-1] tws_arc3=twiss(lat_arc3, tw_end) plot_opt_func(lat_arc3, tws_arc3) tw_end = tws_arc2[-1] #constr = {sase_end:{'Dx':0.0}} constr = {} constr['global'] = {'beta_x':['<',40.], 'beta_y':['<',40.]} #constr[sase_end] = {'beta_x':['->', sase_start], 'beta_y':['->', sase_start], 'Dx':0.44, 'Dxp':0.0} constr[sase_end] = {'beta_x':tws_sase_old[-1].beta_x, 'beta_y':tws_sase_old[-1].beta_y, 'alpha_x':tws_sase_old[-1].alpha_x, 'alpha_y':tws_sase_old[-1].alpha_y} variables = [qms1, qms2, qfs, qds] match(lat_sase, constr, variables, tw_end) tws_sase=twiss(lat_sase, tw_end) plot_opt_func(lat_sase, tws_sase) tw_end = tws_sase_old[-1] tws_arc3=twiss(lat_arc3, tw_end) plot_opt_func(lat_arc3, tws_arc3) plt.show()
constr = {} constr['global'] = {'beta_x': ['<', 170.], 'beta_y': ['<', 170.]} #constr[sase_end] = {'beta_x':['->', sase_start], 'beta_y':['->', sase_start], 'Dx':0.44, 'Dxp':0.0} #constr[qds] = {'alpha_x':0.0, 'alpha_y':0.0, 'beta_x':5.0} constr[qds] = {'beta_x': 3.0} constr[qfs] = {'beta_y': 3.0} constr[m3s] = {'beta_y': ['->', m1s], 'beta_x': ['->', m1s]} constr[m4s] = {'beta_y': ['->', m2s], 'beta_x': ['->', m2s]} constr[m5s] = {'beta_y': ['->', m3s], 'beta_x': ['->', m3s]} constr[m6s] = {'beta_y': ['->', m4s], 'beta_x': ['->', m4s]} variables = [ qfs, qds, [tw_end, 'beta_x'], [tw_end, 'beta_y'], [tw_end, 'alpha_x'], [tw_end, 'alpha_y'] ] match(lat_sase, constr, variables, tw_end, max_iter=10000) tws_sase = twiss(lat_sase, tw_end) plot_opt_func(lat_sase, tws_sase) print 'start twiss' print tws_sase[0].beta_x, tws_sase[0].alpha_x print tws_sase[0].beta_y, tws_sase[0].alpha_y print 'end twiss' print tws_sase[-1].beta_x, tws_sase[-1].alpha_x print tws_sase[-1].beta_y, tws_sase[-1].alpha_y for v in variables: if v.__class__ == Quadrupole: print v.id, v.k1