|
| 1 | +import logging |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import matplotlib.pyplot as plt |
| 5 | + |
| 6 | +import xline as xl |
| 7 | +import xtrack as xt |
| 8 | +import xobjects as xo |
| 9 | +import xpart as xp |
| 10 | +from scipy.spatial import ConvexHull |
| 11 | + |
| 12 | + |
| 13 | +plt.close('all') |
| 14 | + |
| 15 | +n_part=10000 |
| 16 | +shift_x = 0.3e-2 |
| 17 | +shift_y = 0.5e-2 |
| 18 | + |
| 19 | +ctx = xo.context_default |
| 20 | +buf = ctx.new_buffer() |
| 21 | + |
| 22 | +logger = logging.getLogger('xtrack') |
| 23 | +logger.setLevel(logging.DEBUG) |
| 24 | + |
| 25 | +# Define aper_0 |
| 26 | +aper_0 = xt.LimitEllipse(_buffer=buf, a=2e-2, b=2e-2) |
| 27 | +shift_aper_0 = (shift_x, shift_y) |
| 28 | +rot_deg_aper_0 = 10. |
| 29 | + |
| 30 | +# Define aper_1 |
| 31 | +aper_1 = xt.LimitEllipse(_buffer=buf, a=1e-2, b=1e-2) |
| 32 | +shift_aper_1 = (shift_x, shift_y) |
| 33 | +rot_deg_aper_1 = 10. |
| 34 | + |
| 35 | +# aper_0_sandwitch |
| 36 | +trk_aper_0 = xt.Tracker(_buffer=buf, sequence=xl.Line( |
| 37 | + elements=[xt.XYShift(_buffer=buf, dx=shift_aper_0[0], dy=shift_aper_0[1]), |
| 38 | + xt.SRotation(_buffer=buf, angle=rot_deg_aper_0), |
| 39 | + aper_0, |
| 40 | + xt.Multipole(_buffer=buf, knl=[0.00]), |
| 41 | + xt.SRotation(_buffer=buf, angle=-rot_deg_aper_0), |
| 42 | + xt.XYShift(_buffer=buf, dx=-shift_aper_0[0], dy=-shift_aper_0[1])])) |
| 43 | + |
| 44 | +# aper_1_sandwitch |
| 45 | +trk_aper_1 = xt.Tracker(_buffer=buf, sequence=xl.Line( |
| 46 | + elements=[xt.XYShift(_buffer=buf, dx=shift_aper_1[0], dy=shift_aper_1[1]), |
| 47 | + xt.SRotation(_buffer=buf, angle=rot_deg_aper_1), |
| 48 | + aper_1, |
| 49 | + xt.Multipole(_buffer=buf, knl=[0.00]), |
| 50 | + xt.SRotation(_buffer=buf, angle=-rot_deg_aper_1), |
| 51 | + xt.XYShift(_buffer=buf, dx=-shift_aper_1[0], dy=-shift_aper_1[1])])) |
| 52 | + |
| 53 | +# Build example line |
| 54 | +tracker = xt.Tracker(_buffer=buf, sequence=xl.Line( |
| 55 | + elements = ((xt.Drift(_buffer=buf, length=0.5),) |
| 56 | + + trk_aper_0.line.elements |
| 57 | + + (xt.Drift(_buffer=buf, length=1), |
| 58 | + xt.Multipole(_buffer=buf, knl=[0.]), |
| 59 | + xt.Drift(_buffer=buf, length=1), |
| 60 | + xt.Cavity(_buffer=buf, voltage=3e6, frequency=400e6), |
| 61 | + xt.Drift(_buffer=buf, length=1.),) |
| 62 | + + trk_aper_1.line.elements))) |
| 63 | +num_elements = len(tracker.line.elements) |
| 64 | + |
| 65 | +# Test on full line |
| 66 | +r = np.linspace(0, 0.018, n_part) |
| 67 | +theta = np.linspace(0, 8*np.pi, n_part) |
| 68 | +particles = xt.Particles(_context=ctx, |
| 69 | + p0c=6500e9, |
| 70 | + x=r*np.cos(theta)+shift_x, |
| 71 | + y=r*np.sin(theta)+shift_y) |
| 72 | + |
| 73 | +tracker.track(particles) |
| 74 | + |
| 75 | + |
| 76 | +loss_loc_refinement = xt.LossLocationRefinement(tracker, |
| 77 | + n_theta = 360, |
| 78 | + r_max = 0.5, # m |
| 79 | + dr = 50e-6, |
| 80 | + ds = 0.1, |
| 81 | + save_refine_trackers=True) |
| 82 | + |
| 83 | +import time |
| 84 | +t0 = time.time() |
| 85 | + |
| 86 | +loss_loc_refinement.refine_loss_location(particles) |
| 87 | + |
| 88 | +t1 = time.time() |
| 89 | +print(f'Took\t{(t1-t0)*1e3:.2f} ms') |
| 90 | + |
| 91 | + |
| 92 | +# Automatic checks |
| 93 | +mask_lost = particles.state == 0 |
| 94 | +r_calc = np.sqrt((particles.x-shift_x)**2 + (particles.y-shift_y)**2) |
| 95 | +assert np.all(r_calc[~mask_lost]<1e-2) |
| 96 | +assert np.all(r_calc[mask_lost]>1e-2) |
| 97 | +i_aper_1 = tracker.line.elements.index(aper_1) |
| 98 | +assert np.all(particles.at_element[mask_lost]==i_aper_1) |
| 99 | +assert np.all(particles.at_element[~mask_lost]==0) |
| 100 | +s0 = tracker.line.element_s_locations[tracker.line.elements.index(aper_0)] |
| 101 | +s1 = tracker.line.element_s_locations[tracker.line.elements.index(aper_1)] |
| 102 | +r0 = np.sqrt(aper_0.a_squ) |
| 103 | +r1 = np.sqrt(aper_1.a_squ) |
| 104 | +s_expected = s0 + (r_calc-r0)/(r1 - r0)*(s1 - s0) |
| 105 | +# TODO This threshold is a bit large |
| 106 | +assert np.allclose(particles.s[mask_lost], s_expected[mask_lost], atol=0.11) |
| 107 | + |
| 108 | +# Visualize apertures |
| 109 | +interp_tracker = loss_loc_refinement.refine_trackers[ |
| 110 | + loss_loc_refinement.i_apertures[1]] |
| 111 | +s0 = interp_tracker.s0 |
| 112 | +s1 = interp_tracker.s1 |
| 113 | +polygon_0 = interp_tracker.line.elements[0] |
| 114 | +polygon_1 = interp_tracker.line.elements[-1] |
| 115 | +for ii, (trkr, poly) in enumerate( |
| 116 | + zip([trk_aper_0, trk_aper_1], |
| 117 | + [polygon_0, polygon_1])): |
| 118 | + part_gen_range = 0.05 |
| 119 | + pp = xt.Particles( |
| 120 | + p0c=6500e9, |
| 121 | + x=np.random.uniform(-part_gen_range, part_gen_range, n_part), |
| 122 | + y=np.random.uniform(-part_gen_range, part_gen_range, n_part)) |
| 123 | + x0 = pp.x.copy() |
| 124 | + y0 = pp.y.copy() |
| 125 | + |
| 126 | + trkr.track(pp) |
| 127 | + ids = pp.particle_id |
| 128 | + |
| 129 | + |
| 130 | + fig = plt.figure(ii+1) |
| 131 | + ax = fig.add_subplot(111) |
| 132 | + plt.plot(x0, y0, '.', color='red') |
| 133 | + plt.axis('equal') |
| 134 | + plt.grid(linestyle=':') |
| 135 | + plt.plot(x0[ids][pp.state>0], y0[ids][pp.state>0], '.', color='green') |
| 136 | + plt.plot(poly.x_closed, poly.y_closed, '-k', linewidth=1) |
| 137 | + |
| 138 | +plt.figure() |
| 139 | +ax = plt.axes(projection='3d') |
| 140 | +ax.plot3D( |
| 141 | + polygon_0.x_closed, |
| 142 | + polygon_0.y_closed, |
| 143 | + s0+polygon_0.x_closed*0, |
| 144 | + color='k', linewidth=3) |
| 145 | +ax.plot3D( |
| 146 | + polygon_1.x_closed, |
| 147 | + polygon_1.y_closed, |
| 148 | + s1+polygon_1.x_closed*0, |
| 149 | + color='k', linewidth=3) |
| 150 | +s_check = [] |
| 151 | +r_check = [] |
| 152 | +for ee, ss in zip(interp_tracker.line.elements, |
| 153 | + interp_tracker.line.element_s_locations): |
| 154 | + if ee.__class__ is xt.LimitPolygon: |
| 155 | + ax.plot3D( |
| 156 | + ee.x_closed, |
| 157 | + ee.y_closed, |
| 158 | + s0+ss+0*ee.x_closed, |
| 159 | + alpha=0.9, |
| 160 | + ) |
| 161 | + s_check.append(ss+s0) |
| 162 | + r_vertices = np.sqrt( |
| 163 | + (ee.x_vertices-shift_x)**2 + (ee.y_vertices-shift_y)**2) |
| 164 | + r_check.append(np.mean(r_vertices)) |
| 165 | +mask = particles.at_element == loss_loc_refinement.i_apertures[1] |
| 166 | +ax.plot3D(particles.x[mask], particles.y[mask], particles.s[mask], |
| 167 | + '.r', markersize=.5) |
| 168 | +ax.view_init(65, 62); plt.draw() |
| 169 | +plt.show() |
0 commit comments