Changes between Version 8 and Version 9 of misc/setup_for_ML/example_beamsize


Ignore:
Timestamp:
09/13/23 23:11:32 (22 months ago)
Author:
Takashi Obina
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • misc/setup_for_ML/example_beamsize

    v8 v9  
    3737from ocelot.gui.accelerator import *
    3838
    39 def func(x):
    40     global cnt, QM1, QM2, tws, lat, tws0
    41     x0=x[:,0][0]
    42     x1=x[:,1][0]
    43 
     39def 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
    4460    # update optics
    45     QM1.k1 = x0
    46     QM2.k1 = x1
    4761    lat.update_transfer_maps()
    4862    tws = twiss(lat, tws0, nPoints=1000)
    4963
    5064    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
     70def 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
     85def 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
    5392
    5493    # Evaluation Function Example
     
    64103
    65104# ==== Main =====
    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 
    89105# GP Optimization
    90106print("==== start ====")
    91107print("# cnt, x0, x1, sx, sy, eval_val")
    92 
    93108cnt = 0
    94109bounds = [{'name':'x0', 'type':'continuous', 'domain':(-20, 20)},
    95110          {'name':'x1', 'type':'continuous', 'domain':(-20, 20)}]
    96111
    97 myBopt = GPyOpt.methods.BayesianOptimization(f=func,domain=bounds,initial_design_numdata=10,acquisition_type='LCB')
     112myBopt = GPyOpt.methods.BayesianOptimization(f=eval_func,domain=bounds,initial_design_numdata=10,acquisition_type='LCB')
    98113
    99114myBopt.run_optimization(max_iter=50)
     
    101116myBopt.plot_convergence()
    102117print("Best     = ", myBopt.x_opt)
    103 print("eval_val = ", func(np.array([myBopt.x_opt])))
    104118
    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
     120x0, x1 = np.array(myBopt.x_opt)
     121show_beta(x0, x1)
    110122
    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()
    123123}}}
    124124