Autophase and Autophase and Scale examples¶
This includes general (slow) autophasing and scaling, as well as fast autophasing.
%load_ext autoreload
%autoreload 2
from impact import Impact
import numpy as np
import os
# Nicer plotting
import matplotlib.pyplot as plt
import matplotlib
%config InlineBackend.figure_format = 'retina'
matplotlib.rcParams['figure.figsize'] = (8,4)
NUMPROCS = 8
Make Impact object from the LCLS injector model:
ifile ='templates/lcls_injector/ImpactT.in'
I = Impact(ifile, verbose=True)
I.numprocs=1
Configured to run in: /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn/T/tmpfs9aorq4 Setting Npcol, Nprow = 1, 1
Phase and Scale the LCLS gun¶
from impact.autophase import autophase_and_scale
from pmd_beamphysics import single_particle
P0 = single_particle(pz=1e-15, z=1e-15)
autophase_and_scale(I, phase_ele_name='GUN', target=6e6,
phase_range=(270, 360),
scale_range=(10e6, 100e6), initial_particles=P0, verbose=True)
Copied initial Impact object. Phasing GUN by changing theta0_deg Scaling GUN by changing rf_field_scale Bounds: 0.0, 0.15 m Disabling SOL1 Disabling L0A_entrance Disabling L0A_body_1 Disabling L0A_body_2 Disabling L0A_exit Disabling L0B_entrance Disabling L0B_body_1 Disabling L0B_body_2 Disabling L0B_exit Default brent2 algorithm Phase: 270, Scale: 10000000.0, 1.3066482616283404 MeV Phase: 0, Scale: 10000000.0, 0.889899440680668 MeV Exception with Phase: 124.37693999999999, Scale: 10000000.0,'final_particles' Exception with Phase: 214.37694226404, Scale: 10000000.0,'final_particles' Phase: 304.37694, Scale: 10000000.0, 1.1065852470188486 MeV Phase: 278.2536823231907, Scale: 10000000.0, 1.2957644153200447 MeV
/Users/chrisonian/Code/GitHub/lume-impact/impact/parsers.py:1997: UserWarning: Empty file: /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn/T/tmpwbm_2kmy/fort.50 warnings.warn(f'Empty file: {filePath}') /Users/chrisonian/Code/mambaforge/envs/lume-impact-dev/lib/python3.12/site-packages/scipy/interpolate/_interpolate.py:712: RuntimeWarning: invalid value encountered in divide slope = (y_hi - y_lo) / (x_hi - x_lo)[:, None]
Phase: 270.30017170510285, Scale: 10000000.0, 1.3066336280987214 MeV Exception with Phase: 269.72999999999, Scale: 10000000.0,'final_particles' Step 1 phasing found: 270 Phase: 270, Scale: 10000000.0, 1.3066482616283404 MeV Phase: 270, Scale: 100000000.0, 10.753457854685582 MeV Phase: 270, Scale: 54713683.73550004, 6.47888084960828 MeV Phase: 270, Scale: 50573783.510182634, 6.066701924203923 MeV Phase: 270, Scale: 49913221.32431346, 6.000425786826622 MeV Phase: 270, Scale: 49888264.71365131, 5.997918912975785 MeV Step 2 scale found: 49913221.32431346 Exception with Phase: 269, Scale: 49913221.32431346,'final_particles' Phase: 271, Scale: 49913221.32431346, 6.001231586796401 MeV Phase: 274.236068, Scale: 49913221.32431346, 6.0137277403129685 MeV Phase: 272.62140751668596, Scale: 49913221.32431346, 6.005739244654294 MeV Phase: 279.472136050312, Scale: 49913221.32431346, 6.057790211736788 MeV Phase: 287.9442721820305, Scale: 49913221.32431346, 6.156595248444007 MeV Phase: 301.6524764957795, Scale: 49913221.32431346, 6.27304328823136 MeV Phase: 324.5398067966829, Scale: 49913221.32431346, 6.025084364930247 MeV Phase: 310.3946585014944, Scale: 49913221.32431346, 6.260332330669786 MeV Phase: 296.4164085268741, Scale: 49913221.32431346, 6.243248695832717 MeV Phase: 304.6011563478669, Scale: 49913221.32431346, 6.278416665851761 MeV Phase: 304.73801839479944, Scale: 49913221.32431346, 6.278441711205727 MeV Phase: 304.8418085397704, Scale: 49913221.32431346, 6.278446922666081 MeV Phase: 304.83541356204114, Scale: 49913221.32431346, 6.278446946094327 MeV Phase: 304.8351087266176, Scale: 49913221.32431346, 6.278446946094327 MeV Phase: 304.83480389149884, Scale: 49913221.32431346, 6.2784469459924654 MeV Step 3 phase found: 304.8351087266176 Phase: 304.8351087266176, Scale: 10000000.0, 1.1007965915646973 MeV Phase: 304.8351087266176, Scale: 100000000.0, 12.026576906369518 MeV Phase: 304.8351087266176, Scale: 50356687.94856727, 6.3303966696578655 MeV Phase: 304.8351087266176, Scale: 47807025.43183231, 6.031237530820886 MeV Phase: 304.8351087266176, Scale: 47542482.09155658, 6.0001286756823 MeV Phase: 304.8351087266176, Scale: 47541388.545513116, 6.000000052316555 MeV Phase: 304.8351087266176, Scale: 47541364.774818845, 5.999997256379129 MeV Step 4 scale found: 47541388.545513116 Set Phase: 304.8351087266176, Scale: 47541388.545513116
(304.8351087266176, 47541388.545513116)
Check the energy:
I.verbose=False
PF = I.track(P0, s=0.15)
PF['mean_energy']
6000000.052316555
Examine this process using the debug flag. This will return the function used for phasing and scaling.
ps_f, Itest = autophase_and_scale(I, phase_ele_name='GUN', target=6e6, initial_particles=P0, verbose=False,
debug = True)
Plot various phases and scales:
ptry = np.linspace(-100, 50, 30)
for sc in np.linspace(10e6, 100e6, 5):
res = np.array([ps_f(p, sc)/1e6 for p in ptry])
plt.plot(ptry, res, label=f'{sc/1e6:0.2f} MV')
plt.title('Final energy for various phases and scales')
plt.ylabel('Final energy (MeV)')
plt.xlabel('phase (deg)')
plt.legend()
/Users/chrisonian/Code/GitHub/lume-impact/impact/parsers.py:1997: UserWarning: Empty file: /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn/T/tmpd6liy9er/fort.50 warnings.warn(f'Empty file: {filePath}')
<matplotlib.legend.Legend at 0x13de0c500>
Make a 3D data and plot the surface
X = np.linspace(-100, 50, 10)
Y = np.linspace(10e6, 100e6, 10)
X, Y = np.meshgrid(X, Y)
@np.vectorize
def f(phase, scale):
return ps_f(phase, scale)
Z = f(X, Y)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(X, Y/1e6, Z/1e6, cmap=matplotlib.cm.coolwarm,
linewidth=0, antialiased=True)
# Add a color bar which maps values to colors.
#fig.colorbar(surf, shrink=0.5, aspect=5)
ax.set_xlabel('phase (deg)')
ax.set_ylabel('scale (MV)')
ax.set_zlabel('Final energy (MeV)')
plt.show()
Phase and scale LCLS linac sections¶
Linacs L0A and L0B are special, because they require 4 fieldmaps each to model the travelling wave structure. To tune these together, we need to add control groups.
These will control overall phases:
I.add_group('L0A', ele_names=['L0A_entrance', 'L0A_body_1', 'L0A_body_2', 'L0A_exit'], var_name='theta0_deg', attributes='theta0_deg')
I.add_group('L0B', ele_names=['L0B_entrance', 'L0B_body_1', 'L0B_body_2', 'L0B_exit'], var_name='theta0_deg', attributes='theta0_deg')
ControlGroup(**{"ele_names": ["L0B_entrance", "L0B_body_1", "L0B_body_2", "L0B_exit"], "var_name": "theta0_deg", "attributes": ["theta0_deg", "theta0_deg", "theta0_deg", "theta0_deg"], "factors": [1.0, 1.0, 1.0, 1.0], "reference_values": [42.27, 72.27, 132.27, 42.27], "absolute": false, "value": 0.0, "name": "L0B"})
These will control overall scaling, respecting the special factors:
I.add_group('L0A_scale', ele_names=['L0A_entrance', 'L0A_body_1', 'L0A_body_2', 'L0A_exit'],
var_name = 'rf_field_scale',
factors = [0.86571945106805, 1, 1, 0.86571945106805], # sin(k*d) with d = 3.5e-2 m
absolute=True)
I.add_group('L0B_scale', ele_names=['L0B_entrance', 'L0B_body_1', 'L0B_body_2', 'L0B_exit'],
var_name = 'rf_field_scale',
factors = [0.86571945106805, 1, 1, 0.86571945106805], # sin(k*d) with d = 3.5e-2 m
absolute=True)
I['L0A_scale']['rf_field_scale'] = 30e6
#I['L0A_scale'].__dict__
Now phase and scale L0A to 64 MeV:
res_L0A = autophase_and_scale(I, phase_ele_name='L0A', scale_ele_name='L0A_scale', target=64e6, scale_range=(10e6, 100e6), initial_particles=P0, verbose=True)
Copied initial Impact object. Phasing L0A by changing theta0_deg Scaling L0A_scale by changing rf_field_scale Bounds: 1.485, 4.527856 m Tracking initial particles to s = 1.485 Initial particle: 1.48542826464 6000000.052316555 Disabling GUN Disabling SOL1 Disabling L0B_entrance Disabling L0B_body_1 Disabling L0B_body_2 Disabling L0B_exit Default brent2 algorithm Phase: 180, Scale: 10000000.0, 14.167255568451399 MeV Phase: 180, Scale: 10000000.0, 14.167255568451399 MeV Phase: 42.49224000000004, Scale: 10000000.0, 19.900599221695774 MeV Phase: 264.9844890561601, Scale: 10000000.0, 8.713842837178289 MeV Phase: 42.49223440298533, Scale: 10000000.0, 19.90060061459368 MeV Phase: 264.9844709438398, Scale: 10000000.0, 8.713840068114541 MeV Phase: 222.492259613844, Scale: 10000000.0, 10.405713584184868 MeV Phase: 127.47670402554945, Scale: 10000000.0, 8.198791876959703 MeV Phase: 333.73836403267615, Scale: 10000000.0, 23.419924482386943 MeV Phase: 291.2461345122325, Scale: 10000000.0, 14.431300814474834 MeV Phase: 4.882541588813183e-06, Scale: 10000000.0, 25.29364711479038 MeV Phase: 358.92000487764903, Scale: 10000000.0, 25.292053052340982 MeV Phase: 1.0800048874341428, Scale: 10000000.0, 25.288424614826123 MeV Step 1 phasing found: 4.882541588813183e-06 Phase: 4.882541588813183e-06, Scale: 10000000.0, 25.29364711479038 MeV Phase: 4.882541588813183e-06, Scale: 100000000.0, 198.2786027693992 MeV Phase: 4.882541588813183e-06, Scale: 30138004.177798875, 64.08285768443744 MeV Phase: 4.882541588813183e-06, Scale: 30094987.360213272, 64.0001343260896 MeV Phase: 4.882541588813183e-06, Scale: 30079939.866533164, 63.97119721819594 MeV Step 2 scale found: 30094987.360213272 Phase: 359.0000048825416, Scale: 30094987.360213272, 63.9352460536336 MeV Phase: 1.0000048825415888, Scale: 30094987.360213272, 64.04753882304797 MeV Phase: 4.236072882541588, Scale: 30094987.360213272, 64.08051762064106 MeV Phase: 3.1986109512544125, Scale: 30094987.360213272, 64.090035507204 MeV Phase: 2.35881818561245, Scale: 30094987.360213272, 64.08383986409696 MeV Phase: 3.5948861353004498, Scale: 30094987.360213272, 64.08864219671567 MeV Phase: 3.1972720031408612, Scale: 30094987.360213272, 64.09003551997871 MeV Phase: 3.197400538538031, Scale: 30094987.360213272, 64.0900355204897 MeV Phase: 3.1975142776476075, Scale: 30094987.360213272, 64.09003551997871 MeV Phase: 3.1973931403942344, Scale: 30094987.360213272, 64.0900355204897 MeV Phase: 3.197389942991094, Scale: 30094987.360213272, 64.0900355204897 MeV Phase: 3.19734489397826, Scale: 30094987.360213272, 64.0900355204897 MeV Phase: 3.197317052156662, Scale: 30094987.360213272, 64.0900355204897 MeV Phase: 3.197299844964293, Scale: 30094987.360213272, 64.0900355204897 MeV Phase: 3.197289210334364, Scale: 30094987.360213272, 64.0900355204897 MeV Phase: 3.1972826377714907, Scale: 30094987.360213272, 64.0900355204897 MeV Phase: 3.1972785757041677, Scale: 30094987.360213272, 64.0900355204897 MeV Phase: 3.197275378415592, Scale: 30094987.360213272, 64.09003551997871 MeV Step 3 phase found: 3.1972785757041677 Phase: 3.1972785757041677, Scale: 10000000.0, 25.258312531615665 MeV Phase: 3.1972785757041677, Scale: 100000000.0, 198.92614175914477 MeV Phase: 3.1972785757041677, Scale: 30077131.657967918, 64.05556740911076 MeV Phase: 3.1972785757041677, Scale: 30048376.16503081, 64.00005859296365 MeV Phase: 3.1972785757041677, Scale: 30048345.811888352, 63.999999999932065 MeV Phase: 3.1972785757041677, Scale: 30048360.836061258, 64.00002900211835 MeV Step 4 scale found: 30048345.811888352 Set Phase: 3.1972785757041677, Scale: 30048345.811888352
Do the same for L0B:
autophase_and_scale(I, phase_ele_name='L0B', scale_ele_name='L0B_scale', target=135e6, scale_range=(10e6, 100e6), initial_particles=P0, verbose=True)
Copied initial Impact object. Phasing L0B by changing theta0_deg Scaling L0B_scale by changing rf_field_scale Bounds: 5.328756, 8.371612 m Tracking initial particles to s = 5.328756 Initial particle: 5.32879920224 63999921.09254425 Disabling GUN Disabling SOL1 Disabling L0A_entrance Disabling L0A_body_1 Disabling L0A_body_2 Disabling L0A_exit Default brent2 algorithm Phase: 180, Scale: 10000000.0, 44.70994104184851 MeV Phase: 180, Scale: 10000000.0, 44.70994104184851 MeV Phase: 42.49224000000004, Scale: 10000000.0, 78.71763465275679 MeV Phase: 264.9844890561601, Scale: 10000000.0, 61.59522101051607 MeV Phase: 42.49223440298533, Scale: 10000000.0, 78.71763587301102 MeV Phase: 264.9844709438398, Scale: 10000000.0, 61.595214959403094 MeV Phase: 222.4922500294541, Scale: 10000000.0, 49.294788977865146 MeV Phase: 127.47670402554945, Scale: 10000000.0, 52.83159870312501 MeV Phase: 333.7383603717651, Scale: 10000000.0, 80.97585983215453 MeV Phase: 291.2461285887539, Scale: 10000000.0, 70.31374590232957 MeV Phase: 2.61997411143966e-06, Scale: 10000000.0, 83.29039104829663 MeV Phase: 2.3470891902397852, Scale: 10000000.0, 83.30418962580039 MeV Phase: 1.2647421010394737, Scale: 10000000.0, 83.30185037119493 MeV Phase: 3.4294362794400968, Scale: 10000000.0, 83.29964055018992 MeV Step 1 phasing found: 2.3470891902397852 Phase: 2.3470891902397852, Scale: 10000000.0, 83.30418962580039 MeV Phase: 2.3470891902397852, Scale: 100000000.0, 257.0347067338642 MeV Phase: 2.3470891902397852, Scale: 36780688.914797515, 135.0001104481728 MeV Phase: 2.3470891902397852, Scale: 36762298.57034012, 134.96461083261016 MeV Step 2 scale found: 36780688.914797515 Phase: 1.3470891902397852, Scale: 36780688.914797515, 134.99218801678353 MeV Phase: 3.3470891902397852, Scale: 36780688.914797515, 134.98640557724622 MeV Phase: 358.1110211902398, Scale: 36780688.914797515, 134.81839744826186 MeV Phase: 0.1110212405517852, Scale: 36780688.914797515, 134.95251244313832 MeV Phase: 2.1110211902397853, Scale: 36780688.914797515, 135.0001903391442 MeV Phase: 2.2135003506046598, Scale: 36780688.914797515, 135.00030370121428 MeV Phase: 2.2134093929020993, Scale: 36780688.914797515, 135.00030370121428 MeV Phase: 2.2134548717533793, Scale: 36780688.914797515, 135.00030370121428 MeV Phase: 2.2134375003784714, Scale: 36780688.914797515, 135.00030370121428 MeV Phase: 2.2134267642781515, Scale: 36780688.914797515, 135.00030370121428 MeV Phase: 2.2134201290031266, Scale: 36780688.914797515, 135.00030370121428 MeV Phase: 2.2134160281775617, Scale: 36780688.914797515, 135.00030370121428 MeV Phase: 2.2134134937279346, Scale: 36780688.914797515, 135.00030370121428 MeV Step 3 phase found: 2.2134134937279346 Phase: 2.2134134937279346, Scale: 10000000.0, 83.30427355577054 MeV Phase: 2.2134134937279346, Scale: 100000000.0, 257.0349352824428 MeV Phase: 2.2134134937279346, Scale: 36780623.14239347, 135.00017673782156 MeV Phase: 2.2134134937279346, Scale: 36780531.58486758, 135.00000000034336 MeV Phase: 2.2134134937279346, Scale: 36780513.19460179, 134.9999645010803 MeV Step 4 scale found: 36780531.58486758 Set Phase: 2.2134134937279346, Scale: 36780531.58486758
(2.2134134937279346, 36780531.58486758)
Check the final energy and plot:
I.track(P0, s=8.371612)['mean_energy']
134999248.954592
plt.plot(I.stat('mean_z'), I.stat('mean_kinetic_energy')/1e6 + 0.511)
[<matplotlib.lines.Line2D at 0x13d9e7800>]
Fast autophase¶
This is a faster method that can find and set all relative phases by tracking the fields externally.
%%time
I.autophase()
CPU times: user 2.73 s, sys: 4.76 ms, total: 2.74 s Wall time: 2.74 s
{'GUN': 0.952227592096051, 'L0A': 0.0126678830614253, 'L0B': 0.009126886461046979}
Sending in a dict will set these phases as it goes:
I.verbose=True
I.autophase({'GUN': 1,
'L0A': 2,
'L0B': 3})
Setting GUN relative phase = 1 deg Found GUN relative phase = 1.00 deg Setting L0A relative phase = 2 deg Found L0A relative phase = 2.00 deg Setting L0B relative phase = 3 deg Found L0B relative phase = 3.00 deg
{'GUN': 1.0, 'L0A': 2.0, 'L0B': 3.0}
Autophase without scaling¶
Just phasing is simpler.
from impact.autophase import autophase
ifile2 = 'templates/apex_gun/ImpactT.in'
I2 = Impact(ifile2, verbose=False)
autophase(I2, ele_name='APEX_GUN', initial_particles=P0, metric='mean_kinetic_energy', verbose=True)
Copied initial Impact object. Phasing APEX_GUN by changing theta0_deg Bounds: 0.0, 0.199 m Disabling SOL1 Default brent2 algorithm Phase: 180, 0.7501400260547707 MeV Phase: 180, 0.7501400260547707 MeV Exception with Phase: 42.49224000000004, 'final_particles' Exception with Phase: 42.49223094384001, 'final_particles' Exception with Phase: 42.49223999999998, 'final_particles' Phase: 222.49223547192, 0.46487921649929437 MeV Phase: 151.59398241178104, 0.714711059468071 MeV
/Users/chrisonian/Code/GitHub/lume-impact/impact/parsers.py:1997: UserWarning: Empty file: /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn/T/tmp9apb_fdy/fort.50 warnings.warn(f'Empty file: {filePath}')
Phase: 171.3511003792883, 0.7585459504017038 MeV Phase: 171.34908462106745, 0.7585459029872261 MeV Phase: 171.4523027473299, 0.7585471358806966 MeV Phase: 171.45289970740637, 0.7585471359181192 MeV Phase: 171.45272825449666, 0.7585471359181192 MeV Phase: 171.45255680175842, 0.7585471359087637 MeV Set Phase: 171.45272825449666
171.45272825449666
phase_f, Itest = autophase(I2, ele_name='APEX_GUN', metric='mean_kinetic_energy', initial_particles=P0, debug = True)
# Phases to try
ptry = np.linspace(0, 360, 60)
energies = np.array([phase_f(p)/1e3 for p in ptry])
plt.plot(ptry, energies)
plt.ylim(0, 800)
plt.title('Final energy for various phases in the APEX gun')
plt.ylabel('Final kinetic energy (keV)')
plt.xlabel('phase (deg)')
/Users/chrisonian/Code/GitHub/lume-impact/impact/parsers.py:1997: UserWarning: Empty file: /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn/T/tmpt5_35hzt/fort.50 warnings.warn(f'Empty file: {filePath}')
Text(0.5, 0, 'phase (deg)')
Autophase with alternative metric, and bunch tracking with space charge.¶
The above uses mean_energy
as the metric to maximize. Alternatively, one might want to minimize energy spread. This is accomplished by passing maximize=False
and metric='sigma_pz'
or similar.
from distgen import Generator
ifile = 'templates/lcls_injector/ImpactT.in'
gfile = 'templates/lcls_injector/distgen.yaml'
G = Generator(gfile)
G['n_particle'] = 2000
G.run()
P0 = G.particles
%%time
I = Impact(ifile, initial_particles=P0, verbose=False)
I.stop = 0.16
I.numprocs=NUMPROCS
I.run()
CPU times: user 135 ms, sys: 35.9 ms, total: 171 ms Wall time: 1min 22s
phase_f, Itest = autophase(I, ele_name='GUN', metric='sigma_pz', maximize=False, initial_particles=P0, debug = True, verbose=True)
Copied initial Impact object. Phasing GUN by changing theta0_deg Bounds: 0.0, 0.15 m Disabling SOL1 Disabling L0A_entrance Disabling L0A_body_1 Disabling L0A_body_2 Disabling L0A_exit Disabling L0B_entrance Disabling L0B_body_1 Disabling L0B_body_2 Disabling L0B_exit
I.particles['final_particles'].plot('z', 'pz')
# Phases to try
ptry = np.linspace(290, 310, 20)
sigma_pzs = np.array([phase_f(p) for p in ptry])
plt.plot(ptry, sigma_pzs)
#plt.ylim(0, 800)
#plt.title('Final energy for various phases in the APEX gun')
#plt.ylabel('Final kinetic energy (keV)')
plt.xlabel('phase (deg)')
Phase: 290.0, 0.009085754178422183 MeV/c Phase: 291.05263157894734, 0.006578656765260174 MeV/c Phase: 292.10526315789474, 0.004207059004763233 MeV/c Phase: 293.1578947368421, 0.0022541274747156013 MeV/c Phase: 294.2105263157895, 0.0021860255994458106 MeV/c Phase: 295.2631578947368, 0.004043289659012823 MeV/c Phase: 296.3157894736842, 0.006282846678883406 MeV/c Phase: 297.36842105263156, 0.00859764434304115 MeV/c Phase: 298.42105263157896, 0.010935630516750535 MeV/c Phase: 299.4736842105263, 0.013276394683263821 MeV/c Phase: 300.5263157894737, 0.015618784622734914 MeV/c Phase: 301.57894736842104, 0.01796569656136093 MeV/c Phase: 302.63157894736844, 0.02031237225334309 MeV/c Phase: 303.6842105263158, 0.02266589802217114 MeV/c Phase: 304.7368421052632, 0.025021139456963045 MeV/c Phase: 305.7894736842105, 0.027384699678097663 MeV/c Phase: 306.8421052631579, 0.029751313075304067 MeV/c Phase: 307.89473684210526, 0.03212866206883085 MeV/c Phase: 308.9473684210526, 0.03451123243888133 MeV/c Phase: 310.0, 0.03690615751485246 MeV/c
Text(0.5, 0, 'phase (deg)')
phase_f(293.5)
Phase: 293.5, 0.0019129915380495803 MeV/c
1912.9915380495804
Itest.particles['final_particles'].plot('z', 'pz')
phase_f, Itest = autophase(I, ele_name='GUN', metric='sigma_pz',
maximize=False, initial_particles=P0, debug = True,
s_stop = 1.45,
verbose=True)
# Phases to try
ptry = np.linspace(270, 290, 30)
sigma_pzs = np.array([phase_f(p) for p in ptry])
plt.plot(ptry, sigma_pzs)
#plt.ylim(0, 800)
#plt.title('Final energy for various phases in the APEX gun')
#plt.ylabel('Final kinetic energy (keV)')
plt.xlabel('phase (deg)')
phase_f(280.0)
Phase: 280.0, 0.01898577134704411 MeV/c
18985.771347044112
Itest.particles['final_particles'].plot('z', 'pz')