TESLA 9-cell Cavity¶
In [1]:
Copied!
# Useful for debugging
%load_ext autoreload
%autoreload 2
# Useful for debugging
%load_ext autoreload
%autoreload 2
In [2]:
Copied!
from astra import Astra
from pmd_beamphysics import single_particle
from distgen import Generator
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['figure.figsize'] = (13,8)
%config InlineBackend.figure_format = 'retina'
import os
from astra import Astra
from pmd_beamphysics import single_particle
from distgen import Generator
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['figure.figsize'] = (13,8)
%config InlineBackend.figure_format = 'retina'
import os
In [3]:
Copied!
# Input template file
ASTRA_IN = '../templates/tesla_9cell_cavity/astra.in'
DISTGEN_IN = '../templates/tesla_9cell_cavity/distgen.yaml'
# Input template file
ASTRA_IN = '../templates/tesla_9cell_cavity/astra.in'
DISTGEN_IN = '../templates/tesla_9cell_cavity/distgen.yaml'
In [4]:
Copied!
G = Generator(DISTGEN_IN)
G.run()
P0 = G.particles
G = Generator(DISTGEN_IN)
G.run()
P0 = G.particles
In [5]:
Copied!
# Make an Astra object
A = Astra(input_file=ASTRA_IN, initial_particles=P0, verbose=True)
# Make an Astra object
A = Astra(input_file=ASTRA_IN, initial_particles=P0, verbose=True)
In [6]:
Copied!
# Run
A.input['charge']['lspch'] = True
A.input['charge']['lspch3d'] = False
A.run()
# Run
A.input['charge']['lspch'] = True
A.input['charge']['lspch3d'] = False
A.run()
Initial particles written to /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn/T/tmp9yes1459/astra.particles -------------------------------------------------------------------------- Astra- A space charge tracking algorithm Version 3.2 - macOS 64bit - Apple Silicon DESY, Hamburg 2011 Tue Oct 4 12:55:13 Parameter file is: astra.in TESLA 9-cell cavity example Initialize element settings: -------------------------------------------------------------------------- Cavity: Reading cavity field data from: tesla_9cell_cavity_Ez.dat field smoothing is applied Cavity Frequency f = 1.300 GHz maximum gradient 32.00 MV/m at 1.136 m estimated average gradient 12.44 MV/m nominal phase 0.000 deg -------------------------------------------------------------------------- 10001 particles from file astra.particles Particles taken into account N = 10001 total charge Q = -1.000 nC horizontal beam position x = 6.0718E-18 mm vertical beam position y = 1.8565E-17 mm longitudinal beam position z = 1.5199E-19 m horizontal beam size sig x = 1.0000E-02 mm vertical beam size sig y = 1.0000E-02 mm longitudinal beam size sig z = 0.1000 mm average kinetic energy E = 99.49 MeV energy spread dE = 1.6514E-08 keV average momentum P = 100.0 MeV/c transverse beam emittance eps x = 0.000 pi mrad mm correlated divergence cor x = 0.000 mrad transverse beam emittance eps y = 0.000 pi mrad mm correlated divergence cor y = 0.000 mrad longitudinal beam emittance eps z = 1.6513E-09 pi keV mm correlated energy spread cor z = 1.4196E-25 keV emittance ratio eps y/eps x = 0.000 -------------------------------------------------------------------------- Start auto phasing: WARNING: Particle starts inside cavity number: 1 Scan cavity number : 1 0 unstable trajectories out of 50 -------------------------------------------------------------------------- Start phase scan: WARNING: Particle starts inside cavity number: 1 Cavity number : 1 from : 0.000 m to: 1.347 m 0 unstable trajectories out of 360 Cavity phasing completed: Cavity number Energy gain [MeV] at Phase [deg] 1 17.34 299.38 -------------------------------------------------------------------------- on axis tracking of the reference particle: initial position z = 2.0892E-21 m x = 0.000 mm y = -1.9722E-19 mm initial momentum p = 100.0 MeV/c global phase shift phi = 0.000 deg time step for integration dt = 3.000 ps -------------------------------------------------------------------------- Online element settings: -------------------------------------------------------------------------- particle reaches position z = 1.500 m time of flight is t = 5.004 ns final momentum p = 117.3 MeV/c final phase (cavity 1) phi_end = 299.4 deg -------------------------------------------------------------------------- off axis tracking of the reference particle: initial position z = 2.0892E-21 m x = 1.0000E-02 mm y = 1.0000E-02 mm final position x = 9.1605E-03 mm y = 9.1605E-03 mm divergence px/pz = -8.3798E-05 mrad py/pz = -8.3798E-05 mrad -------------------------------------------------------------------------- tracking of 10001 particles : tracking will stop at z = 1.500 m space charge forces are INCLUDED cylindrical symmetric algorithm final checkpoint at z = 1.500 m total number of iteration steps: 1828 ********************************************************************** Particles taken into account N = 10001 total charge Q = -1.000 nC horizontal beam position x = 2.3730E-05 mm vertical beam position y = 7.8206E-06 mm longitudinal beam position z = 1.500 m horizontal beam size sig x = 0.1443 mm vertical beam size sig y = 0.1443 mm longitudinal beam size sig z = 0.1000 mm average kinetic energy E = 116.9 MeV energy spread dE = 58.64 keV average momentum P = 117.4 MeV/c transverse beam emittance eps x = 4.7268E-02 pi mrad mm correlated divergence cor x = 0.1175 mrad transverse beam emittance eps y = 4.7712E-02 pi mrad mm correlated divergence cor y = 0.1174 mrad longitudinal beam emittance eps z = 5.261 pi keV mm correlated energy spread cor z = 25.91 keV emittance ratio eps y/eps x = 0.9907 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 Tracking ended with 2 warning(s) ********************************************************************** 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 Core emittance data saved to file : astra.Cemit.001 ********************************************************************** finished simulation Tue Oct 4 12:55:59 2022 elapsed time : 45.8 seconds execution time : 45.6 seconds system time : 0.2 seconds Goodbye. -------------------------------------------------------------------------- loading 1 particle files [150.0] {'start_time': 1664902513.90176, 'run_script': '/Users/chrisonian/Code/Astra/bin/Astra astra.in', 'run_time': 45.97193694114685, 'run_error': False}
Note: The following floating-point exceptions are signalling: IEEE_DIVIDE_BY_ZERO
In [7]:
Copied!
# Compare these.
key1 = 'mean_z'
key2 = 'sigma_x'
units1 = str(A.units(key1))
units2 = str(A.units(key2))
plt.xlabel(key1+f' ({units1})')
plt.ylabel(key2+f' ({units2})')
plt.plot(A.stat(key1), A.stat(key2))
plt.scatter(A.particle_stat(key1), A.particle_stat(key2), color='red')
#plt.scatter(A.particle_stat(key1, alive_only=False), A.particle_stat(key2, alive_only=False), color='green')
# Compare these.
key1 = 'mean_z'
key2 = 'sigma_x'
units1 = str(A.units(key1))
units2 = str(A.units(key2))
plt.xlabel(key1+f' ({units1})')
plt.ylabel(key2+f' ({units2})')
plt.plot(A.stat(key1), A.stat(key2))
plt.scatter(A.particle_stat(key1), A.particle_stat(key2), color='red')
#plt.scatter(A.particle_stat(key1, alive_only=False), A.particle_stat(key2, alive_only=False), color='green')
Out[7]:
<matplotlib.collections.PathCollection at 0x13f164b20>
In [8]:
Copied!
P = A.particles[-1]
#P.plot('x', 'px')
P.plot('z', 'pz', bins=100)
P = A.particles[-1]
#P.plot('x', 'px')
P.plot('z', 'pz', bins=100)
In [9]:
Copied!
# Compare these.
key1 = 'mean_z'
key2 = 'norm_emit_x'
units1 = str(A.units(key1))
units2 = str(A.units(key2))
plt.xlabel(key1+f' ({units1})')
plt.ylabel(key2+f' ({units2})')
plt.plot(A.stat(key1), A.stat(key2), label='Astra', color='red')
#plt.plot(I.stat(key1), I.stat(key2), '--', label='Impact-T', color='blue')
plt.scatter(A.particle_stat(key1), A.particle_stat(key2), color='red')
plt.legend()
#plt.scatter(A.particle_stat(key1, alive_only=False), A.particle_stat(key2, alive_only=False), color='green')
# Compare these.
key1 = 'mean_z'
key2 = 'norm_emit_x'
units1 = str(A.units(key1))
units2 = str(A.units(key2))
plt.xlabel(key1+f' ({units1})')
plt.ylabel(key2+f' ({units2})')
plt.plot(A.stat(key1), A.stat(key2), label='Astra', color='red')
#plt.plot(I.stat(key1), I.stat(key2), '--', label='Impact-T', color='blue')
plt.scatter(A.particle_stat(key1), A.particle_stat(key2), color='red')
plt.legend()
#plt.scatter(A.particle_stat(key1, alive_only=False), A.particle_stat(key2, alive_only=False), color='green')
Out[9]:
<matplotlib.legend.Legend at 0x14e781280>
In [10]:
Copied!
#A.archive('astra_cavity9_SC_2D.h5')
#A.archive('astra_cavity9_SC_2D.h5')
In [11]:
Copied!
#PI1 = I.particles['initial_particles']
#PI2 = I.particles['final_particles']
#
#PA1 = P0
#PA2 = A.particles[-1]
#PI1 = I.particles['initial_particles']
#PI2 = I.particles['final_particles']
#
#PA1 = P0
#PA2 = A.particles[-1]
In [ ]:
Copied!
P0['sigma_x'], PI1['sigma_x']
P0['sigma_x'], PI1['sigma_x']
In [ ]:
Copied!
k1 = 'x'
k2 = 'px'
skip = 1
plt.scatter(PA1[k1][::skip], PA1[k2][::skip], label='Astra', marker='x', alpha=0.5)
plt.scatter(PI1[k1][::skip], PI1[k2][::skip], label='Impact', marker='.', alpha=0.5)
plt.legend()
k1 = 'x'
k2 = 'px'
skip = 1
plt.scatter(PA1[k1][::skip], PA1[k2][::skip], label='Astra', marker='x', alpha=0.5)
plt.scatter(PI1[k1][::skip], PI1[k2][::skip], label='Impact', marker='.', alpha=0.5)
plt.legend()
In [ ]:
Copied!
k1 = 'z'
k2 = 'x'
skip = 1
plt.scatter(PA2[k1][::skip], PA2[k2][::skip], label='Astra', marker='x', alpha=0.5)
plt.scatter(PI2[k1][::skip], PI2[k2][::skip], label='Impact', marker='.', alpha=0.5)
plt.legend()
k1 = 'z'
k2 = 'x'
skip = 1
plt.scatter(PA2[k1][::skip], PA2[k2][::skip], label='Astra', marker='x', alpha=0.5)
plt.scatter(PI2[k1][::skip], PI2[k2][::skip], label='Impact', marker='.', alpha=0.5)
plt.legend()
In [ ]:
Copied!
PA2['sigma_x'], PI2['sigma_x']
PA2['sigma_x'], PI2['sigma_x']
In [ ]:
Copied!
plt.hist(PA2['x'], bins=50, alpha=0.5, label='Astra')
plt.hist(PI2['x'], bins=50, alpha=0.5, label='Impact')
plt.legend()
plt.hist(PA2['x'], bins=50, alpha=0.5, label='Astra')
plt.hist(PI2['x'], bins=50, alpha=0.5, label='Impact')
plt.legend()
3D¶
In [ ]:
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'] = 64
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'] = 64
A2.input['charge']['nx0'] = 4
A2.input['charge']['ny0'] = 4
A2.input['charge']['nz0'] = 4
A2.configure()
A2.run()
In [ ]:
Copied!
P2 = A2.particles[-1]
P2.plot('x', 'px')
P2 = A2.particles[-1]
P2.plot('x', 'px')
In [ ]:
Copied!
A2.archive('astra_cavity9_SC_3D.h5')
A2.archive('astra_cavity9_SC_3D.h5')
Compare¶
In [ ]:
Copied!
# Compare these.
key1 = 'mean_z'
key2 = 'norm_emit_x'
units1 = str(A.units(key1))
units2 = str(A.units(key2))
plt.xlabel(key1+f' ({units1})')
plt.ylabel(key2+f' ({units2})')
plt.plot(A.stat(key1), A.stat(key2), label='Astra 2D', color='red')
plt.plot(A2.stat(key1), A2.stat(key2), label='Astra 3D', color='blue')
plt.plot(I.stat(key1), I.stat(key2), '--', label='Impact-T', color='green')
plt.scatter(A.particle_stat(key1), A.particle_stat(key2), color='red')
plt.legend()
#plt.scatter(A.particle_stat(key1, alive_only=False), A.particle_stat(key2, alive_only=False), color='green')
# Compare these.
key1 = 'mean_z'
key2 = 'norm_emit_x'
units1 = str(A.units(key1))
units2 = str(A.units(key2))
plt.xlabel(key1+f' ({units1})')
plt.ylabel(key2+f' ({units2})')
plt.plot(A.stat(key1), A.stat(key2), label='Astra 2D', color='red')
plt.plot(A2.stat(key1), A2.stat(key2), label='Astra 3D', color='blue')
plt.plot(I.stat(key1), I.stat(key2), '--', label='Impact-T', color='green')
plt.scatter(A.particle_stat(key1), A.particle_stat(key2), color='red')
plt.legend()
#plt.scatter(A.particle_stat(key1, alive_only=False), A.particle_stat(key2, alive_only=False), color='green')
In [ ]:
Copied!
k1 = 'x'
k2 = 'px'
skip = 10
plt.title('Astra')
plt.xlabel(k1)
plt.ylabel(k2)
plt.scatter(P[k1][::skip], P[k2][::skip], label='2d', marker='x', alpha=0.5)
plt.scatter(P2[k1][::skip], P2[k2][::skip], label='3d', marker='.', alpha=0.5)
plt.scatter(PI2[k1][::skip], PI2[k2][::skip], label='Impact', marker='.', alpha=0.5)
plt.legend()
k1 = 'x'
k2 = 'px'
skip = 10
plt.title('Astra')
plt.xlabel(k1)
plt.ylabel(k2)
plt.scatter(P[k1][::skip], P[k2][::skip], label='2d', marker='x', alpha=0.5)
plt.scatter(P2[k1][::skip], P2[k2][::skip], label='3d', marker='.', alpha=0.5)
plt.scatter(PI2[k1][::skip], PI2[k2][::skip], label='Impact', marker='.', alpha=0.5)
plt.legend()
In [ ]:
Copied!
k1 = 'x'
k2 = 'px'
skip = 1
plt.scatter(P[k1][::skip], P[k2][::skip], label='2d', marker='x', alpha=0.5)
plt.scatter(P2[k1][::skip], P2[k2][::skip], label='3d', marker='.', alpha=0.5)
plt.legend()
k1 = 'x'
k2 = 'px'
skip = 1
plt.scatter(P[k1][::skip], P[k2][::skip], label='2d', marker='x', alpha=0.5)
plt.scatter(P2[k1][::skip], P2[k2][::skip], label='3d', marker='.', alpha=0.5)
plt.legend()
In [ ]:
Copied!
P['max_pz']/ P2['max_pz']
P['max_pz']/ P2['max_pz']
Compare with Impact-T¶
In [ ]:
Copied!
from impact import Impact
I = Impact()
I.load_archive('impact_cavity9.h5')
from impact import Impact
I = Impact()
I.load_archive('impact_cavity9.h5')