APEX GunĀ¶
InĀ [1]:
Copied!
# Useful for debugging
%load_ext autoreload
%autoreload 2
# Useful for debugging
%load_ext autoreload
%autoreload 2
InĀ [2]:
Copied!
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['figure.figsize'] = (13,8)
%config InlineBackend.figure_format = 'retina'
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['figure.figsize'] = (13,8)
%config InlineBackend.figure_format = 'retina'
InĀ [3]:
Copied!
from astra import Astra
from astra import recommended_spacecharge_mesh
from distgen import Generator
import matplotlib.pyplot as plt
import os
from astra import Astra
from astra import recommended_spacecharge_mesh
from distgen import Generator
import matplotlib.pyplot as plt
import os
InĀ [4]:
Copied!
# Input template file
ASTRA_IN = '../templates/apex_gun/astra.in'
DISTGEN_IN = '../templates/apex_gun/distgen.yaml'
# Input template file
ASTRA_IN = '../templates/apex_gun/astra.in'
DISTGEN_IN = '../templates/apex_gun/distgen.yaml'
InĀ [5]:
Copied!
recommended_spacecharge_mesh(10000)
recommended_spacecharge_mesh(10000)
Out[5]:
{'nrad': 20, 'nlong_in': 43}
InĀ [6]:
Copied!
G = Generator(DISTGEN_IN)
G['n_particle'] = 10000
G.run()
P0 = G.particles
P0.status[:] = -1 # Cathode start
#factor = 2
#P0.x *= factor
#P0.y *= 1/factor
P0.plot('x', 'y')
G = Generator(DISTGEN_IN)
G['n_particle'] = 10000
G.run()
P0 = G.particles
P0.status[:] = -1 # Cathode start
#factor = 2
#P0.x *= factor
#P0.y *= 1/factor
P0.plot('x', 'y')
InĀ [7]:
Copied!
# Make an Astra object
A = Astra(input_file=ASTRA_IN, initial_particles=P0, verbose=True)
sc_settings = recommended_spacecharge_mesh(P0['n_particle'])
A.input['charge'].update()
A.input['output']['zstop'] = 0.15
A.input['charge']['lspch'] = True
A.input['charge']['lspch3d'] = False
# Make an Astra object
A = Astra(input_file=ASTRA_IN, initial_particles=P0, verbose=True)
sc_settings = recommended_spacecharge_mesh(P0['n_particle'])
A.input['charge'].update()
A.input['output']['zstop'] = 0.15
A.input['charge']['lspch'] = True
A.input['charge']['lspch3d'] = False
Configured to run in: /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn/T/tmpkvgt3vpc
InĀ [8]:
Copied!
%%time
# Run
A.run()
%%time
# Run
A.run()
Initial particles written to /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn/T/tmpkvgt3vpc/astra.particles -------------------------------------------------------------------------- Astra- A space charge tracking algorithm Version 4.0 - macOS 64bit - Apple Silicon DESY, Hamburg 2022 Wed Aug 2 08:49:09 Parameter file is: astra.in APEX Gun Initialize element settings: -------------------------------------------------------------------------- Cavity: Reading cavity field data from: 187MHz_HighDef_March2010.dat field smoothing is applied Cavity Frequency f = 0.1870 GHz maximum gradient 20.00 MV/m at 0.000 m estimated average gradient 2.667 MV/m nominal phase 0.000 deg -------------------------------------------------------------------------- Solenoid: Reading solenoid field data from: realbucking.dat field smoothing is applied maximum |Bz| field 0.000 T at -6.2050E-02 m integral Bz squared 0.000 T^2m Reading solenoid field data from: newSOL.dat field smoothing is applied maximum |Bz| field 5.7000E-02 T at 0.2465 m integral Bz squared 2.8019E-04 T^2m solenoids number: 1 and 2 overlap -------------------------------------------------------------------------- 10001 particles from file astra.particles Cathode located at: z = 0.000 m Particles taken into account N = 10001 total charge Q = -0.1000 nC horizontal beam position x = -1.3665E-16 mm vertical beam position y = -1.6267E-16 mm longitudinal beam position z = 0.000 m horizontal beam size sig x = 0.2500 mm vertical beam size sig y = 0.2500 mm longitudinal beam size sig z = 0.000 mm total emission time t = 5.0643E-02 ns rms emission time sig t = 8.3859E-03 ns average kinetic energy E = 3.7500E-07 MeV energy spread dE = 3.0451E-04 keV average momentum P = 6.1907E-04 MeV/c transverse beam emittance eps x = 0.1749 pi mrad mm correlated divergence cor x = -0.5362 mrad transverse beam emittance eps y = 0.1749 pi mrad mm correlated divergence cor y = -0.6932 mrad longitudinal beam emittance eps z = 0.000 pi keV mm correlated energy spread cor z = 0.000 keV emittance ratio eps y/eps x = 1.000 -------------------------------------------------------------------------- Start auto phasing: Scan cavity number : 1 6 unstable trajectories out of 50 -------------------------------------------------------------------------- Start phase scan: Cavity number : 1 from : 0.000 m to: 0.1990 m 197 unstable trajectories out of 360 Cavity phasing completed: Cavity number Energy gain [MeV] at Phase [deg] 1 0.7586 261.45 -------------------------------------------------------------------------- on axis tracking of the reference particle: initial position z = 0.000 m x = -7.8886E-18 mm y = -3.9443E-18 mm initial momentum p = 2.8534E-04 MeV/c global phase shift phi = 0.000 deg time step for integration dt = 7.500 ps -------------------------------------------------------------------------- Online element settings: -------------------------------------------------------------------------- particle reaches position z = 0.1500 m time of flight is t = 0.6043 ns final momentum p = 1.162 MeV/c final phase (cavity 1) phi_end = 268.4 deg -------------------------------------------------------------------------- off axis tracking of the reference particle: initial position z = 0.000 m x = 0.2500 mm y = 0.2500 mm final position x = 0.5436 mm y = 0.5978 mm divergence px/pz = 1.865 mrad py/pz = 3.587 mrad -------------------------------------------------------------------------- tracking of 10001 particles: tracking will stop at z = 0.1500 m space charge forces are INCLUDED cylindrical symmetric algorithm mirror charges at the cathode are taken into account mirror charge switched off at: 0.1138 m final checkpoint at z = 0.1500 m total number of iteration steps: 1651 ********************************************************************** Particles taken into account N = 10001 total charge Q = -0.1000 nC horizontal beam position x = 2.3682E-04 mm vertical beam position y = -7.5225E-06 mm longitudinal beam position z = 0.1500 m horizontal beam size sig x = 2.413 mm vertical beam size sig y = 2.413 mm longitudinal beam size sig z = 2.654 mm average kinetic energy E = 0.7585 MeV energy spread dE = 3.257 keV average momentum P = 1.162 MeV/c transverse beam emittance eps x = 16.97 pi mrad mm correlated divergence cor x = 15.16 mrad transverse beam emittance eps y = 16.97 pi mrad mm correlated divergence cor y = 15.16 mrad longitudinal beam emittance eps z = 2.417 pi keV mm correlated energy spread cor z = 3.127 keV emittance ratio eps y/eps x = 1.000 Particle Statistics: Total number of particles on stack = 10001 Electrons (total) = 10001 particles at the cathode = 0 active particles = 10001 passive particles (lost out of bunch) = 0 probe particles = 0 backward traveling particles = 0 particles lost with z<Zmin = 0 particles lost due to cathode field = 0 particles lost on aperture = 0 ********************************************************************** Emittance information saved to file : astra.Xemit.001 Emittance information saved to file : astra.Yemit.001 Emittance information saved to file : astra.Zemit.001 Lost & found saved to file : astra.LandF.001 Ref. part. information saved to file : astra.ref.001 Phase-space distributions logged in : astra.Log.001 Phase scan saved to file : astra.PScan.001 Cathode sp. charge field saved to file : astra.Cathode.001 Core emittance data saved to file : astra.Cemit.001 ********************************************************************** finished simulation Wed Aug 2 08:49:31 2023 elapsed time : 22.0 seconds execution time : 21.9 seconds system time : 0.1 seconds Goodbye. -------------------------------------------------------------------------- loading 1 particle files [15.0] {'start_time': 1690991349.348098, 'run_script': '/Users/chrisonian/Code/Astra/bin/Astra astra.in', 'run_time': 22.16491198539734} CPU times: user 59.4 ms, sys: 10.1 ms, total: 69.5 ms Wall time: 22.2 s
InĀ [9]:
Copied!
P1 = A.particles[-1]
P1.plot('z', 'pz')
P1 = A.particles[-1]
P1.plot('z', 'pz')
3D Space Charge methodĀ¶
This does not properly take the image charge force at the cathode.
InĀ [10]:
Copied!
A2 = A.copy()
A2.input['charge']['lspch'] = True
A2.input['charge']['lspch3d'] = True
A2.input['charge']['l2d_3d'] = False
A2.input['charge']['nxf'] = 32
A2.input['charge']['nyf'] = 32
A2.input['charge']['nzf'] = 32
A2.input['charge']['nx0'] = 4
A2.input['charge']['ny0'] = 4
A2.input['charge']['nz0'] = 4
A2.configure()
A2.run()
A2 = A.copy()
A2.input['charge']['lspch'] = True
A2.input['charge']['lspch3d'] = True
A2.input['charge']['l2d_3d'] = False
A2.input['charge']['nxf'] = 32
A2.input['charge']['nyf'] = 32
A2.input['charge']['nzf'] = 32
A2.input['charge']['nx0'] = 4
A2.input['charge']['ny0'] = 4
A2.input['charge']['nz0'] = 4
A2.configure()
A2.run()
Configured to run in: /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn/T/tmpd93dk5wn Initial particles written to /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn/T/tmpd93dk5wn/astra.particles -------------------------------------------------------------------------- Astra- A space charge tracking algorithm Version 4.0 - macOS 64bit - Apple Silicon DESY, Hamburg 2022 Wed Aug 2 08:49:32 Parameter file is: astra.in APEX Gun Initialize element settings: -------------------------------------------------------------------------- Cavity: Reading cavity field data from: 187MHz_HighDef_March2010.dat field smoothing is applied Cavity Frequency f = 0.1870 GHz maximum gradient 20.00 MV/m at 0.000 m estimated average gradient 2.667 MV/m nominal phase 0.000 deg -------------------------------------------------------------------------- Solenoid: Reading solenoid field data from: realbucking.dat field smoothing is applied maximum |Bz| field 0.000 T at -6.2050E-02 m integral Bz squared 0.000 T^2m Reading solenoid field data from: newSOL.dat field smoothing is applied maximum |Bz| field 5.7000E-02 T at 0.2465 m integral Bz squared 2.8019E-04 T^2m solenoids number: 1 and 2 overlap -------------------------------------------------------------------------- 10001 particles from file astra.particles Particles taken into account N = 10001 total charge Q = -0.1000 nC horizontal beam position x = -1.3665E-16 mm vertical beam position y = -1.6267E-16 mm longitudinal beam position z = 0.000 m horizontal beam size sig x = 0.2500 mm vertical beam size sig y = 0.2500 mm longitudinal beam size sig z = 0.000 mm total emission time t = 5.0643E-02 ns rms emission time sig t = 8.3859E-03 ns average kinetic energy E = 3.7500E-07 MeV energy spread dE = 3.0451E-04 keV average momentum P = 6.1907E-04 MeV/c transverse beam emittance eps x = 0.1749 pi mrad mm correlated divergence cor x = -0.5362 mrad transverse beam emittance eps y = 0.1749 pi mrad mm correlated divergence cor y = -0.6932 mrad longitudinal beam emittance eps z = 0.000 pi keV mm correlated energy spread cor z = 0.000 keV emittance ratio eps y/eps x = 1.000 -------------------------------------------------------------------------- Start auto phasing: Scan cavity number : 1 6 unstable trajectories out of 50 -------------------------------------------------------------------------- Start phase scan: Cavity number : 1 from : 0.000 m to: 0.1990 m 197 unstable trajectories out of 360 Cavity phasing completed: Cavity number Energy gain [MeV] at Phase [deg] 1 0.7586 261.45 -------------------------------------------------------------------------- on axis tracking of the reference particle: initial position z = 0.000 m x = -7.8886E-18 mm y = -3.9443E-18 mm initial momentum p = 2.8534E-04 MeV/c global phase shift phi = 0.000 deg time step for integration dt = 7.500 ps -------------------------------------------------------------------------- Online element settings: -------------------------------------------------------------------------- particle reaches position z = 0.1500 m time of flight is t = 0.6043 ns final momentum p = 1.162 MeV/c final phase (cavity 1) phi_end = 268.4 deg -------------------------------------------------------------------------- off axis tracking of the reference particle: initial position z = 0.000 m x = 0.2500 mm y = 0.2500 mm final position x = 0.5436 mm y = 0.5978 mm divergence px/pz = 1.865 mrad py/pz = 3.587 mrad -------------------------------------------------------------------------- tracking of 10001 particles: tracking will stop at z = 0.1500 m space charge forces are INCLUDED 3D FFT algorithm WARNING: particle outside grid WARNING: particle outside grid WARNING: particle outside grid WARNING: particle outside grid final checkpoint at z = 0.1500 m total number of iteration steps: 1651 ********************************************************************** Particles taken into account N = 10001 total charge Q = -0.1000 nC horizontal beam position x = -1.8560E-04 mm vertical beam position y = -1.5354E-04 mm longitudinal beam position z = 0.1500 m horizontal beam size sig x = 2.972 mm vertical beam size sig y = 2.973 mm longitudinal beam size sig z = 2.571 mm average kinetic energy E = 0.7596 MeV energy spread dE = 3.174 keV average momentum P = 1.163 MeV/c transverse beam emittance eps x = 25.77 pi mrad mm correlated divergence cor x = 18.53 mrad transverse beam emittance eps y = 25.77 pi mrad mm correlated divergence cor y = 18.54 mrad longitudinal beam emittance eps z = 2.345 pi keV mm correlated energy spread cor z = 3.040 keV emittance ratio eps y/eps x = 0.9999 Particle Statistics: Total number of particles on stack = 10001 Electrons (total) = 10001 particles at the cathode = 0 active particles = 10001 passive particles (lost out of bunch) = 0 probe particles = 0 backward traveling particles = 0 particles lost with z<Zmin = 0 particles lost due to cathode field = 0 particles lost on aperture = 0 ********************************************************************** Emittance information saved to file : astra.Xemit.001 Emittance information saved to file : astra.Yemit.001 Emittance information saved to file : astra.Zemit.001 Lost & found saved to file : astra.LandF.001 Ref. part. information saved to file : astra.ref.001 Phase-space distributions logged in : astra.Log.001 Phase scan saved to file : astra.PScan.001 Cathode sp. charge field saved to file : astra.Cathode.001 Core emittance data saved to file : astra.Cemit.001 ********************************************************************** finished simulation Wed Aug 2 08:50:07 2023 elapsed time : 35.6 seconds execution time : 35.4 seconds system time : 0.2 seconds Goodbye. -------------------------------------------------------------------------- loading 1 particle files [15.0] {'start_time': 1690991372.058728, 'run_script': '/Users/chrisonian/Code/Astra/bin/Astra astra.in', 'run_time': 35.72111988067627}
InĀ [11]:
Copied!
P2 = A2.particles[-1]
P2.plot('x', 'px')
P2 = A2.particles[-1]
P2.plot('x', 'px')
InĀ [12]:
Copied!
k1 = 'z'
k2 = 'x'
plt.scatter(P2[k1], P2[k2], label='3d', marker='.')
plt.scatter(P1[k1], P1[k2], label='2d', marker='x')
plt.legend()
k1 = 'z'
k2 = 'x'
plt.scatter(P2[k1], P2[k2], label='3d', marker='.')
plt.scatter(P1[k1], P1[k2], label='2d', marker='x')
plt.legend()
Out[12]:
<matplotlib.legend.Legend at 0x15c80cf70>
InĀ [13]:
Copied!
k1 = 'x'
k2 = 'y'
units1 = str(A.units(k1))
units2 = str(A.units(k2))
plt.xlabel(k1+f' ({units1})')
plt.ylabel(k2+f' ({units2})')
plt.scatter(P2[k1], P2[k2], label='Astra 3d', marker='.')
plt.scatter(P1[k1], P1[k2], label='Astra 2d', marker='.')
#plt.scatter(P3[k1], P3[k2], label='Impact-T 3d', marker='.')
#plt.scatter(P4[k1], P4[k2], label='GPT 3D', marker='.')
plt.legend()
k1 = 'x'
k2 = 'y'
units1 = str(A.units(k1))
units2 = str(A.units(k2))
plt.xlabel(k1+f' ({units1})')
plt.ylabel(k2+f' ({units2})')
plt.scatter(P2[k1], P2[k2], label='Astra 3d', marker='.')
plt.scatter(P1[k1], P1[k2], label='Astra 2d', marker='.')
#plt.scatter(P3[k1], P3[k2], label='Impact-T 3d', marker='.')
#plt.scatter(P4[k1], P4[k2], label='GPT 3D', marker='.')
plt.legend()
Out[13]:
<matplotlib.legend.Legend at 0x15d596580>
InĀ [14]:
Copied!
k1 = 'z'
k2 = 'pz'
units1 = str(A.units(k1))
units2 = str(A.units(k2))
plt.xlabel(k1+f' ({units1})')
plt.ylabel(k2+f' ({units2})')
plt.scatter(P2[k1], P2[k2], label='Astra 3d', marker='.')
plt.scatter(P1[k1], P1[k2], label='Astra 2d', marker='.')
#plt.scatter(P3[k1], P3[k2], label='Impact-T 3d', marker='.')
#plt.scatter(P4[k1], P4[k2], label='GPT 3D', marker='.')
plt.legend()
k1 = 'z'
k2 = 'pz'
units1 = str(A.units(k1))
units2 = str(A.units(k2))
plt.xlabel(k1+f' ({units1})')
plt.ylabel(k2+f' ({units2})')
plt.scatter(P2[k1], P2[k2], label='Astra 3d', marker='.')
plt.scatter(P1[k1], P1[k2], label='Astra 2d', marker='.')
#plt.scatter(P3[k1], P3[k2], label='Impact-T 3d', marker='.')
#plt.scatter(P4[k1], P4[k2], label='GPT 3D', marker='.')
plt.legend()
Out[14]:
<matplotlib.legend.Legend at 0x15f0fa7f0>
InĀ [15]:
Copied!
P1.plot('z', 'pz')
P1.plot('z', 'pz')
InĀ [16]:
Copied!
P2.plot('z', 'pz')
P2.plot('z', 'pz')
InĀ [17]:
Copied!
A.input['output']
A.input['output']
Out[17]:
{'zstart': 0, 'zstop': 0.15, 'zemit': 1000, 'zphase': 1, 'screen(1)': 1.490314, 'lmagnetized': True, 'lsub_rot': False, 'lproject_emit': False, 'larmors': False, 'emits': True, 'c_emits': True, 'landfs': True, 'phases': True, 'tracks': True, 'refs': True, 'tchecks': False, 'cathodes': True, 'high_res': True}