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 |
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 |
66 | | |
67 | | # Define Optics |
68 | | ## Drift |
69 | | L20 = Drift(l=0.2, eid='L20') |
70 | | L50 = Drift(l=0.5, eid='L50') |
71 | | ## Quadrupoles |
72 | | QM1 = Quadrupole(l=0.2, k1=3.9, eid='QM1') |
73 | | QM2 = Quadrupole(l=0.2, k1=-3.25, eid='QM2') |
74 | | ## Lattice |
75 | | 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.0 |
80 | | tws0.beta_y = 30.0 |
81 | | tws0.alpha_x = -10.0 |
82 | | tws0.alpha_y = -5.0 |
83 | | tws0.emit_x = 1.0 |
84 | | tws0.emit_y = 1.0 |
85 | | tws0.E = 1.0 |
86 | | |
87 | | tws = twiss(lat, tws0, nPoints=1000) |
88 | | |