|
| 1 | +import time |
| 2 | +import numpy as np |
| 3 | +import pandas as pd |
| 4 | +from pathlib import Path |
| 5 | +import matplotlib.pyplot as plt |
| 6 | +import matplotlib.colors as mcolors |
| 7 | + |
| 8 | +import xobjects as xo |
| 9 | +import xpart as xp |
| 10 | +import xtrack as xt |
| 11 | +import xcoll as xc |
| 12 | + |
| 13 | + |
| 14 | +num_part = 100000 |
| 15 | +_capacity = 2*num_part |
| 16 | +particle_ref = xp.Particles.reference_from_pdg_id(pdg_id='proton', p0c=7e12) |
| 17 | + |
| 18 | +everest_coll = xc.EverestCollimator(length=1.0, material=xc.materials.MolybdenumGraphite, jaw=[0.01, -0.01]) |
| 19 | + |
| 20 | +fluka_coll = xc.FlukaCollimator(length=1.0, jaw=[0.01, -0.01]) |
| 21 | +xc.FlukaEngine.start(elements=fluka_coll, names='tcp.c6l7.b1', debug_level=1, _capacity=_capacity, cwd='temp_scratch') |
| 22 | +xc.FlukaEngine.set_particle_ref(particle_ref=particle_ref) |
| 23 | + |
| 24 | +# g4_coll = xc.Geant4Collimator(length=1.0, material='mogr', jaw=[0.1, -0.1], geant4_id='g4_coll') |
| 25 | + |
| 26 | + |
| 27 | +# Plotting function |
| 28 | +# ----------------- |
| 29 | + |
| 30 | +dri = xt.Drift(length=everest_coll.length) |
| 31 | +def plot_scatters(part_init, part, outfile, show_colorbar=True): |
| 32 | + devmin = 1e-6 |
| 33 | + devmax = 1 |
| 34 | + part_dri = part_init.copy() |
| 35 | + dri.track(part_dri) |
| 36 | + part_dri.sort(interleave_lost_particles=True) |
| 37 | + part.sort(interleave_lost_particles=True) |
| 38 | + mask = part.state > 0 |
| 39 | + dev = 1.e3*np.sqrt((part.x[mask] - part_dri.x[part.parent_particle_id[mask]])**2 |
| 40 | + + (part.y[mask] - part_dri.y[part.parent_particle_id[mask]])**2) |
| 41 | + dev[dev<devmin] = devmin |
| 42 | + if len(dev[dev>devmax]) > 0: |
| 43 | + print(f'Warning: {len(dev[dev>devmax])} particles have deviation > {devmax} mm') |
| 44 | + |
| 45 | + if show_colorbar: |
| 46 | + fig = plt.figure(figsize=(7.11, 3.5)) |
| 47 | + else: |
| 48 | + fig = plt.figure(figsize=(6, 3.5)) |
| 49 | + plt.scatter(1.e3*part_init.x[part.parent_particle_id[mask]], |
| 50 | + 1.e3*part_init.y[part.parent_particle_id[mask]], |
| 51 | + c=dev, s=0.25, cmap='viridis',norm=mcolors.LogNorm(vmin=devmin, vmax=devmax)) |
| 52 | + plt.vlines(10, -5, 5, colors='r') |
| 53 | + plt.xlim(8.5, 11.5) |
| 54 | + plt.ylim(-5, 5) |
| 55 | + plt.xlabel('x [mm]') |
| 56 | + plt.ylabel('y [mm]') |
| 57 | + if show_colorbar: |
| 58 | + plt.colorbar(label='Deviation from drift [mm]') |
| 59 | + plt.tight_layout() |
| 60 | + plt.savefig(outfile, dpi=300) |
| 61 | + |
| 62 | + |
| 63 | +# Generate particles |
| 64 | +# ------------------ |
| 65 | + |
| 66 | +y = np.random.normal(loc=0, scale=1e-3, size=num_part) |
| 67 | +py = np.random.normal(loc=0, scale=1e-3, size=num_part) |
| 68 | +delta = np.random.normal(loc=0, scale=3e-4, size=num_part) |
| 69 | + |
| 70 | +part_init = [] |
| 71 | +x0 = 9.25e-3 |
| 72 | +x = np.random.normal(loc=x0, scale=1e-4, size=num_part) |
| 73 | +part_init.append(xp.build_particles(particle_ref=particle_ref, x=x, px=0, y=y, py=py, delta=delta, _capacity=_capacity)) |
| 74 | + |
| 75 | +x0 = 0.01 |
| 76 | +x = np.random.normal(loc=x0, scale=1e-4, size=num_part) |
| 77 | +part_init.append(xp.build_particles(particle_ref=particle_ref, x=x, px=0, y=y, py=py, delta=delta, _capacity=_capacity)) |
| 78 | + |
| 79 | +x0 = 0.01075 |
| 80 | +x = np.random.normal(loc=x0, scale=1e-4, size=num_part) |
| 81 | +part_init.append(xp.build_particles(particle_ref=particle_ref, x=x, px=0, y=y, py=py, delta=delta, _capacity=_capacity)) |
| 82 | + |
| 83 | + |
| 84 | +# Fluka |
| 85 | +# ----- |
| 86 | + |
| 87 | +part_fluka = [] |
| 88 | +for this_part_init in part_init: |
| 89 | + this_part = this_part_init.copy() |
| 90 | + t_start = time.time() |
| 91 | + fluka_coll.track(this_part) |
| 92 | + t_end = time.time() |
| 93 | + |
| 94 | + print(f"FLUKA execution time: {t_end - t_start} s") |
| 95 | + print(f"Survived in FLUKA: {len(np.unique(this_part.parent_particle_id[this_part.state>0]))}/{num_part}") |
| 96 | + this_part.sort(interleave_lost_particles=True) |
| 97 | + part_fluka.append(this_part) |
| 98 | + |
| 99 | +xc.FlukaEngine.stop() |
| 100 | + |
| 101 | +for idx in range(3): |
| 102 | + plot_scatters(part_init[idx], part_fluka[idx], f'fluka_collimator_{idx}.png', show_colorbar=True) |
| 103 | + |
| 104 | + |
| 105 | +# Everest |
| 106 | +# ------- |
| 107 | + |
| 108 | +# Pre-run for compilation |
| 109 | +this_part = part_init[0].copy() |
| 110 | +everest_coll.track(this_part) |
| 111 | + |
| 112 | +part_everest = [] |
| 113 | +for this_part_init in part_init: |
| 114 | + this_part = this_part_init.copy() |
| 115 | + t_start = time.time() |
| 116 | + everest_coll.track(this_part) |
| 117 | + t_end = time.time() |
| 118 | + |
| 119 | + print(f"Everest execution time: {t_end - t_start} s") |
| 120 | + print(f"Survived in Everest: {len(np.unique(this_part.parent_particle_id[this_part.state>0]))}/{num_part}") |
| 121 | + this_part.sort(interleave_lost_particles=True) |
| 122 | + part_everest.append(this_part) |
| 123 | + |
| 124 | +for idx in range(3): |
| 125 | + plot_scatters(part_init[idx], part_everest[idx], f'everest_collimator_{idx}.png', show_colorbar=True) |
| 126 | + |
| 127 | + |
| 128 | +# # Geant4 |
| 129 | +# # ------ |
| 130 | + |
| 131 | +# xc.Geant4Engine.start(elements=[g4_coll], seed=1993, bdsim_config_file='settings_protons.gmad', particle_ref=particle_ref) |
| 132 | + |
| 133 | +# part_g4 = [] |
| 134 | +# for this_part_init in part_init: |
| 135 | +# this_part = this_part_init.copy() |
| 136 | +# t_start = time.time() |
| 137 | +# g4_coll.track(this_part) |
| 138 | +# t_end = time.time() |
| 139 | + |
| 140 | +# print(f"Geant4 execution time: {t_end - t_start} s") |
| 141 | +# print(f"Survived in Geant4: {len(np.unique(this_part.parent_particle_id[this_part.state>0]))}/{num_part}") |
| 142 | +# this_part.sort(interleave_lost_particles=True) |
| 143 | +# part_g4.append(this_part) |
| 144 | + |
| 145 | +# xc.Geant4Engine.stop() |
| 146 | + |
| 147 | +# for idx in range(3): |
| 148 | +# plot_scatters(part_init[idx], part_g4[idx], f'g4_collimator_{idx}.png', show_colorbar=True) |
0 commit comments