Changes between Version 8 and Version 9 of misc/setup_for_ML/example_beamsize
- Timestamp:
- 09/13/23 23:11:32 (10 months ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
misc/setup_for_ML/example_beamsize
v8 v9 37 37 from ocelot.gui.accelerator import * 38 38 39 def func(x): 40 global cnt, QM1, QM2, tws, lat, tws0 41 x0=x[:,0][0] 42 x1=x[:,1][0] 43 39 def calc_resp(x0, x1): 40 # Define Optics 41 ## Drift 42 L20 = Drift(l=0.2, eid='L20') 43 L50 = Drift(l=0.5, eid='L50') 44 ## Quadrupoles 45 QM1 = Quadrupole(l=0.2, k1=x0, eid='QM1') 46 QM2 = Quadrupole(l=0.2, k1=x1, eid='QM2') 47 ## Lattice 48 cell = (L50, QM1, L20, QM2, L50, L50) 49 lat = MagneticLattice(cell, stop=None) 50 # print("length of the cell: ", lat.totalLen, " m") 51 # Initial condition 52 tws0 = Twiss() 53 tws0.beta_x = 20.0 54 tws0.beta_y = 30.0 55 tws0.alpha_x = -10.0 56 tws0.alpha_y = -5.0 57 tws0.emit_x = 1.0 58 tws0.emit_y = 1.0 59 tws0.E = 1.0 44 60 # update optics 45 QM1.k1 = x046 QM2.k1 = x147 61 lat.update_transfer_maps() 48 62 tws = twiss(lat, tws0, nPoints=1000) 49 63 50 64 idx = -1 # last point of the lattice 51 sx = np.sqrt(tws[idx].beta_x) 52 sy = np.sqrt(tws[idx].beta_y) 65 sx = np.sqrt(tws0.emit_x * tws[idx].beta_x) 66 sy = np.sqrt(tws0.emit_y * tws[idx].beta_y) 67 68 return sx, sy, tws 69 70 def show_beta(x0, x1): 71 sx, sy, tws = calc_resp(x0, x1) 72 73 s = [p.s for p in tws] 74 beta_x = [p.beta_x for p in tws] 75 beta_y = [p.beta_y for p in tws] 76 77 plt.plot(s, beta_x, 'b-'); 78 plt.plot(s, beta_y, 'r-'); 79 plt.legend(['Horiz', 'Vert']) 80 plt.xlabel('s [m]') 81 plt.ylabel('beta [m]') 82 plt.grid(True) 83 plt.show() 84 85 def eval_func(x): 86 global cnt 87 88 x0 = x[0][0] 89 x1 = x[0][1] 90 91 sx, sy, tws = calc_resp(x0, x1) # calc optics for 2 QM 53 92 54 93 # Evaluation Function Example … … 64 103 65 104 # ==== Main ===== 66 67 # Define Optics68 ## Drift69 L20 = Drift(l=0.2, eid='L20')70 L50 = Drift(l=0.5, eid='L50')71 ## Quadrupoles72 QM1 = Quadrupole(l=0.2, k1=3.9, eid='QM1')73 QM2 = Quadrupole(l=0.2, k1=-3.25, eid='QM2')74 ## Lattice75 cell = (L50, QM1, L20, QM2, L50, L50)76 lat = MagneticLattice(cell, stop=None)77 print("length of the cell: ", lat.totalLen, " m")78 tws0 = Twiss()79 tws0.beta_x = 20.080 tws0.beta_y = 30.081 tws0.alpha_x = -10.082 tws0.alpha_y = -5.083 tws0.emit_x = 1.084 tws0.emit_y = 1.085 tws0.E = 1.086 87 tws = twiss(lat, tws0, nPoints=1000)88 89 105 # GP Optimization 90 106 print("==== start ====") 91 107 print("# cnt, x0, x1, sx, sy, eval_val") 92 93 108 cnt = 0 94 109 bounds = [{'name':'x0', 'type':'continuous', 'domain':(-20, 20)}, 95 110 {'name':'x1', 'type':'continuous', 'domain':(-20, 20)}] 96 111 97 myBopt = GPyOpt.methods.BayesianOptimization(f= func,domain=bounds,initial_design_numdata=10,acquisition_type='LCB')112 myBopt = GPyOpt.methods.BayesianOptimization(f=eval_func,domain=bounds,initial_design_numdata=10,acquisition_type='LCB') 98 113 99 114 myBopt.run_optimization(max_iter=50) … … 101 116 myBopt.plot_convergence() 102 117 print("Best = ", myBopt.x_opt) 103 print("eval_val = ", func(np.array([myBopt.x_opt])))104 118 105 # Plot results 106 QM1.k1 = myBopt.x_opt[0] 107 QM2.k1 = myBopt.x_opt[1] 108 lat.update_transfer_maps() 109 tws = twiss(lat, tws0, nPoints=1000) 119 # plot betatron function 120 x0, x1 = np.array(myBopt.x_opt) 121 show_beta(x0, x1) 110 122 111 s = [p.s for p in tws]112 beta_x = [p.beta_x for p in tws]113 beta_y = [p.beta_y for p in tws]114 115 plt.plot(s, beta_x, 'b-');116 plt.plot(s, beta_y, 'r-');117 118 plt.legend(['Horiz', 'Vert'])119 plt.xlabel('s [m]')120 plt.ylabel('beta [m]')121 plt.grid(True)122 plt.show()123 123 }}} 124 124