Parsing ImpactT.in¶
Here are some examples of the low level routines to parse the main input file.
Note that the Impact class does this automatically
In [1]:
Copied!
from impact.parsers import (
parse_header,
ix_lattice,
parse_type,
ele_type,
parse_quadrupole,
parse_solrf,
parse_wakefield,
parse_lattice,
)
from impact.parsers import (
parse_header,
ix_lattice,
parse_type,
ele_type,
parse_quadrupole,
parse_solrf,
parse_wakefield,
parse_lattice,
)
In [2]:
Copied!
from impact.lattice import ele_str, ele_line
from impact.lattice import ele_str, ele_line
In [3]:
Copied!
with open("templates/lcls_injector/ImpactT.in", "r") as f:
data = f.read()
LINES = data.split("\n")
with open("templates/lcls_injector/ImpactT.in", "r") as f:
data = f.read()
LINES = data.split("\n")
In [4]:
Copied!
parse_header(LINES)
parse_header(LINES)
Out[4]:
{'Npcol': 2, 'Nprow': 2, 'Dt': 5e-13, 'Ntstep': 1000000, 'Nbunch': 1, 'Dim': 6, 'Np': 10000, 'Flagmap': 1, 'Flagerr': 0, 'Flagdiag': 2, 'Flagimg': 1, 'Zimage': 0.02, 'Nx': 32, 'Ny': 32, 'Nz': 32, 'Flagbc': 1, 'Xrad': 0.015, 'Yrad': 0.015, 'Perdlen': 45.0, 'Flagdist': 16, 'Rstartflg': 0, 'Flagsbstp': 0, 'Nemission': 400, 'Temission': 6.515803466731775e-12, 'sigx(m)': 0.0006, 'sigpx': 0.0, 'muxpx': 0.0, 'xscale': 1.0, 'pxscale': 1.0, 'xmu1(m)': 0.0, 'xmu2': 0.0, 'sigy(m)': 0.0006, 'sigpy': 0.0, 'muxpy': 0.0, 'yscale': 1.0, 'pyscale': 1.0, 'ymu1(m)': 0.0, 'ymu2': 0.0, 'sigz(m)': 1.27e-06, 'sigpz': 0.0, 'muxpz': 0.0, 'zscale': 1.0, 'pzscale': 1.0, 'zmu1(m)': 0.0, 'zmu2': 0.0, 'Bcurr': 0.7140000000000003, 'Bkenergy': 1.0, 'Bmass': 511005.0, 'Bcharge': -1.0, 'Bfreq': 2856000000.0, 'Tini': -3.249141278122655e-12}
In [5]:
Copied!
# Find index of the line where the lattice starts
IX_LATTICE = ix_lattice(LINES)
# Gather lattice lines
LATLINES = LINES[IX_LATTICE:]
# Find index of the line where the lattice starts
IX_LATTICE = ix_lattice(LINES)
# Gather lattice lines
LATLINES = LINES[IX_LATTICE:]
In [6]:
Copied!
# Summary of elements in this lattice
def myf(t):
return [l for l in LATLINES if parse_type(l) == t]
quads = myf("quadrupole")
# drifts = myf('drift')
solrfs = myf("solrf")
wakefields = myf("wakefield")
alltypes = set([parse_type(l) for l in LATLINES])
print(alltypes)
for t in ele_type.values():
l = len(myf(t))
if l > 0:
print(l, t)
# Summary of elements in this lattice
def myf(t):
return [l for l in LATLINES if parse_type(l) == t]
quads = myf("quadrupole")
# drifts = myf('drift')
solrfs = myf("solrf")
wakefields = myf("wakefield")
alltypes = set([parse_type(l) for l in LATLINES])
print(alltypes)
for t in ele_type.values():
l = len(myf(t))
if l > 0:
print(l, t)
{'wakefield', 'quadrupole', 'spacecharge', 'solrf', 'comment', 'change_timestep', 'rotationally_symmetric_to_3d', 'write_beam', 'stop'} 8 quadrupole 10 solrf 4 write_beam 1 change_timestep 1 rotationally_symmetric_to_3d 2 wakefield 1 spacecharge 1 stop
Individual element parsing¶
In [7]:
Copied!
parse_quadrupole(quads[0])
parse_quadrupole(quads[0])
Out[7]:
{'zedge': 0.01601, 'b1_gradient': 0.0, 'L_effective': 0.21, 'radius': 0.0254, 'x_offset': 0.0, 'y_offset': 0.0, 'x_rotation': 0.0, 'y_rotation': 0.0, 'z_rotation': 0.7853981633974483}
In [8]:
Copied!
parse_solrf(solrfs[0])
parse_solrf(solrfs[0])
Out[8]:
{'zedge': 0.0, 'rf_field_scale': 47537665.059089914, 'rf_frequency': 2856000000.0, 'theta0_deg': 303.93723122804266, 'filename': 'rfdata201', 'radius': 0.15, 'x_offset': 0.0, 'y_offset': 0.0, 'x_rotation': 0.0, 'y_rotation': 0.0, 'z_rotation': 0.0, 'solenoid_field_scale': 0.0}
In [9]:
Copied!
parse_wakefield(wakefields[0])
parse_wakefield(wakefields[0])
Out[9]:
{'s_begin': 1.485, 's': 4.527856, 'method': 'analytical', 'iris_radius': 0.0116, 'gap': 0.0292, 'period': 0.035}
Full lattice parsing into element dicts¶
In [10]:
Copied!
# This parses all lines.
eles = parse_lattice(LATLINES)
# This parses all lines.
eles = parse_lattice(LATLINES)
In [11]:
Copied!
# MAD-style printing
for e in eles:
s = ele_str(e)
print(s)
# MAD-style printing
for e in eles:
s = ele_str(e)
print(s)
change_timestep_1: change_timestep, description=name:change_timestep_1, dt=4e-12, s=0.25 SC_2D_to_3D: rotationally_symmetric_to_3d, description=name:SC_2D_to_3D, s=-1000.0 !__________________ 1.5 cell gun __________________ ! Single particle phased and scaled for 6 MeV energy GUN: solrf, description=name:GUN, L=0.15, zedge=0.0, rf_field_scale=47537665.059089914, rf_frequency=2856000000.0, theta0_deg=303.93723122804266, filename=rfdata201, radius=0.15, x_offset=0.0, y_offset=0.0, x_rotation=0.0, y_rotation=0.0, z_rotation=0.0, solenoid_field_scale=0.0, s=0.15 !__________________Solenoid and correctors__________________ SOL1: solrf, description=name:SOL1, L=0.49308, zedge=0.0, rf_field_scale=0.0, rf_frequency=0.0, theta0_deg=0.0, filename=rfdata102, radius=0.15, x_offset=0.0, y_offset=0.0, x_rotation=0.0, y_rotation=0.0, z_rotation=0.0, solenoid_field_scale=0.2457, s=0.49308 ! Corrector quads: skew and regular. ! Overlap SOL1. Effective length, radius estimated from measurements. ! Max field should be 0.00714 T/m SQ01: quadrupole, description=name:SQ01, L=0.36, zedge=0.01601, b1_gradient=0.0, L_effective=0.21, radius=0.0254, x_offset=0.0, y_offset=0.0, x_rotation=0.0, y_rotation=0.0, z_rotation=0.7853981633974483, s=0.37601 CQ01: quadrupole, description=name:CQ01, L=0.36, zedge=0.01601, b1_gradient=0.0, L_effective=0.21, radius=0.0254, x_offset=0.0, y_offset=0.0, x_rotation=0.0, y_rotation=0.0, z_rotation=0.0, s=0.37601 !________________________________________________________________________ !!! Broken: 0 1 101 -2 0.0 0.0 0.61362 /!name:YAG01 YAG02: write_beam, description=name:YAG02, filename=fort.102, sample_frequency=1, s=1.38841 !__________________ L0A begin __________________ wakefield_L0A: wakefield, description=name:wakefield_L0A, s_begin=1.485, s=4.527856, method=analytical, iris_radius=0.0116, gap=0.0292, period=0.035 ! Phased to get to 64 MeV L0A_entrance: solrf, description=name:L0A_entrance, L=0.052464, zedge=1.485, rf_field_scale=26013439.060000002, rf_frequency=2856000000.0, theta0_deg=264.5, filename=rfdata4, radius=0.15, x_offset=0.0, y_offset=0.0, x_rotation=0.0, y_rotation=0.0, z_rotation=0.0, solenoid_field_scale=0.0, s=1.5374640000000002 L0A_body_1: solrf, description=name:L0A_body_1, L=2.937928, zedge=1.537464, rf_field_scale=30048347.1, rf_frequency=2856000000.0, theta0_deg=294.5, filename=rfdata5, radius=0.15, x_offset=0.0, y_offset=0.0, x_rotation=0.0, y_rotation=0.0, z_rotation=0.0, solenoid_field_scale=0.0, s=4.475391999999999 L0A_body_2: solrf, description=name:L0A_body_2, L=2.937928, zedge=1.537464, rf_field_scale=30048347.1, rf_frequency=2856000000.0, theta0_deg=354.5, filename=rfdata6, radius=0.15, x_offset=0.0, y_offset=0.0, x_rotation=0.0, y_rotation=0.0, z_rotation=0.0, solenoid_field_scale=0.0, s=4.475391999999999 L0A_exit: solrf, description=name:L0A_exit, L=0.052464, zedge=4.475392, rf_field_scale=26013439.060000002, rf_frequency=2856000000.0, theta0_deg=264.5, filename=rfdata7, radius=0.15, x_offset=0.0, y_offset=0.0, x_rotation=0.0, y_rotation=0.0, z_rotation=0.0, solenoid_field_scale=0.0, s=4.527856 !__________________ L0A exit __________________ ! Space charge switches !!!0 0 0 -8 0 1 4.527856 / name:SC_ON !!!0 0 0 -8 0 -1 4.527856 / name:SC_OFF !!!0 0 0 -5 0.0 0.0 4.527856 /!name:SC_2D_to_3D YAG03: write_beam, description=name:YAG03, filename=fort.103, sample_frequency=1, s=4.614538605 QA01: quadrupole, description=name:QA01, L=0.204, zedge=4.752933605, b1_gradient=1.8524000101358, L_effective=0.108, radius=0.016, x_offset=0.0, y_offset=0.0, x_rotation=0.0, y_rotation=0.0, z_rotation=0.0, s=4.956933605 QA02: quadrupole, description=name:QA02, L=0.204, zedge=5.081309605, b1_gradient=-1.8524000101358, L_effective=0.108, radius=0.016, x_offset=0.0, y_offset=0.0, x_rotation=0.0, y_rotation=0.0, z_rotation=0.0, s=5.285309605 !__________________ L0B begin __________________ wakefield_L0B: wakefield, description=name:wakefield_L0B, s_begin=5.328756, s=8.371612, method=analytical, iris_radius=0.0116, gap=0.0292, period=0.035 ! Phased to get to 135 MeV L0B_entrance: solrf, description=name:L0B_entrance, L=0.052464, zedge=5.328756, rf_field_scale=31395529.900000002, rf_frequency=2856000000.0, theta0_deg=42.27, filename=rfdata4, radius=0.15, x_offset=0.0, y_offset=0.0, x_rotation=0.0, y_rotation=0.0, z_rotation=0.0, solenoid_field_scale=0.0, s=5.38122 L0B_body_1: solrf, description=name:L0B_body_1, L=2.937928, zedge=5.38122, rf_field_scale=36265246.5, rf_frequency=2856000000.0, theta0_deg=72.27, filename=rfdata5, radius=0.15, x_offset=0.0, y_offset=0.0, x_rotation=0.0, y_rotation=0.0, z_rotation=0.0, solenoid_field_scale=0.0, s=8.319148 L0B_body_2: solrf, description=name:L0B_body_2, L=2.937928, zedge=5.38122, rf_field_scale=36265246.5, rf_frequency=2856000000.0, theta0_deg=132.27, filename=rfdata6, radius=0.15, x_offset=0.0, y_offset=0.0, x_rotation=0.0, y_rotation=0.0, z_rotation=0.0, solenoid_field_scale=0.0, s=8.319148 L0B_exit: solrf, description=name:L0B_exit, L=0.052464, zedge=8.319148, rf_field_scale=31395529.900000002, rf_frequency=2856000000.0, theta0_deg=42.27, filename=rfdata7, radius=0.15, x_offset=0.0, y_offset=0.0, x_rotation=0.0, y_rotation=0.0, z_rotation=0.0, solenoid_field_scale=0.0, s=8.371612 !__________________ L0B exit __________________ QE01: quadrupole, description=name:QE01, L=0.204, zedge=8.392048605, b1_gradient=0.18720000156206, L_effective=0.108, radius=0.016, x_offset=0.0, y_offset=0.0, x_rotation=0.0, y_rotation=0.0, z_rotation=0.0, s=8.596048605 QE02: quadrupole, description=name:QE02, L=0.204, zedge=8.793561605, b1_gradient=0.16609999999321, L_effective=0.108, radius=0.016, x_offset=0.0, y_offset=0.0, x_rotation=0.0, y_rotation=0.0, z_rotation=0.0, s=8.997561605000001 !!! Unmodeled: Laser Heater from 9.076892 m to 10.690580 m QE03: quadrupole, description=name:QE03, L=0.204, zedge=11.469244190867, b1_gradient=-2.6409000012747, L_effective=0.108, radius=0.016, x_offset=0.0, y_offset=0.0, x_rotation=0.0, y_rotation=0.0, z_rotation=0.0, s=11.673244190867 QE04: quadrupole, description=name:QE04, L=0.204, zedge=11.875644190867, b1_gradient=2.9799999853198, L_effective=0.108, radius=0.016, x_offset=0.0, y_offset=0.0, x_rotation=0.0, y_rotation=0.0, z_rotation=0.0, s=12.079644190867 !!! 0 1 104 -2 0.0 0.0 12.175332190867 /!name:WS01 OTR1: write_beam, description=name:OTR1, filename=fort.105, sample_frequency=1, s=12.327300190867 !!! 0 1 106 -2 0.0 0.0 14.089061190867 /!name:WS02 OTR2: write_beam, description=name:OTR2, filename=fort.107, sample_frequency=1, s=14.241029190867 !!! 0 1 108 -2 0.0 0.0 16.002790190867 /!name:WS03 !!! 0 1 109 -2 0.0 0.0 16.154758190867 /!name:OTR3 SC_OFF: spacecharge, description=name:SC_OFF, s=16.5, is_on=False stop_1: stop, description=name:stop_1, s=3.0
In [12]:
Copied!
# Output lines
for e in eles:
print(ele_line(e))
# Output lines
for e in eles:
print(ele_line(e))
0 0 0 -4 0.0 0.0 0.25 4e-12 /!name:change_timestep_1 0 0 0 -5 0.0 0.0 -1000.0 /!name:SC_2D_to_3D !__________________ 1.5 cell gun __________________ ! Single particle phased and scaled for 6 MeV energy 0.15 0 0 105 0.0 47537665.059089914 2856000000.0 303.93723122804266 201 0.15 0.0 0.0 0.0 0.0 0.0 0.0 /!name:GUN !__________________Solenoid and correctors__________________ 0.49308 0 0 105 0.0 0.0 0.0 0.0 102 0.15 0.0 0.0 0.0 0.0 0.0 0.2457 /!name:SOL1 ! Corrector quads: skew and regular. ! Overlap SOL1. Effective length, radius estimated from measurements. ! Max field should be 0.00714 T/m 0.36 0 0 1 0.01601 0.0 0.21 0.0254 0.0 0.0 0.0 0.0 0.7853981633974483 /!name:SQ01 0.36 0 0 1 0.01601 0.0 0.21 0.0254 0.0 0.0 0.0 0.0 0.0 /!name:CQ01 !________________________________________________________________________ !!! Broken: 0 1 101 -2 0.0 0.0 0.61362 /!name:YAG01 0 1 102 -2 0.0 0.0 1.38841 /!name:YAG02 !__________________ L0A begin __________________ 0 -1 0 -6 1 1 1.485 4.527856 0.0116 0.0292 0.035 /!name:wakefield_L0A ! Phased to get to 64 MeV 0.052464 0 0 105 1.485 26013439.060000002 2856000000.0 264.5 4 0.15 0.0 0.0 0.0 0.0 0.0 0.0 /!name:L0A_entrance 2.937928 0 0 105 1.537464 30048347.1 2856000000.0 294.5 5 0.15 0.0 0.0 0.0 0.0 0.0 0.0 /!name:L0A_body_1 2.937928 0 0 105 1.537464 30048347.1 2856000000.0 354.5 6 0.15 0.0 0.0 0.0 0.0 0.0 0.0 /!name:L0A_body_2 0.052464 0 0 105 4.475392 26013439.060000002 2856000000.0 264.5 7 0.15 0.0 0.0 0.0 0.0 0.0 0.0 /!name:L0A_exit !__________________ L0A exit __________________ ! Space charge switches !!!0 0 0 -8 0 1 4.527856 / name:SC_ON !!!0 0 0 -8 0 -1 4.527856 / name:SC_OFF !!!0 0 0 -5 0.0 0.0 4.527856 /!name:SC_2D_to_3D 0 1 103 -2 0.0 0.0 4.614538605 /!name:YAG03 0.204 0 0 1 4.752933605 1.8524000101358 0.108 0.016 0.0 0.0 0.0 0.0 0.0 /!name:QA01 0.204 0 0 1 5.081309605 -1.8524000101358 0.108 0.016 0.0 0.0 0.0 0.0 0.0 /!name:QA02 !__________________ L0B begin __________________ 0 -1 0 -6 1 1 5.328756 8.371612 0.0116 0.0292 0.035 /!name:wakefield_L0B ! Phased to get to 135 MeV 0.052464 0 0 105 5.328756 31395529.900000002 2856000000.0 42.27 4 0.15 0.0 0.0 0.0 0.0 0.0 0.0 /!name:L0B_entrance 2.937928 0 0 105 5.38122 36265246.5 2856000000.0 72.27 5 0.15 0.0 0.0 0.0 0.0 0.0 0.0 /!name:L0B_body_1 2.937928 0 0 105 5.38122 36265246.5 2856000000.0 132.27 6 0.15 0.0 0.0 0.0 0.0 0.0 0.0 /!name:L0B_body_2 0.052464 0 0 105 8.319148 31395529.900000002 2856000000.0 42.27 7 0.15 0.0 0.0 0.0 0.0 0.0 0.0 /!name:L0B_exit !__________________ L0B exit __________________ 0.204 0 0 1 8.392048605 0.18720000156206 0.108 0.016 0.0 0.0 0.0 0.0 0.0 /!name:QE01 0.204 0 0 1 8.793561605 0.16609999999321 0.108 0.016 0.0 0.0 0.0 0.0 0.0 /!name:QE02 !!! Unmodeled: Laser Heater from 9.076892 m to 10.690580 m 0.204 0 0 1 11.469244190867 -2.6409000012747 0.108 0.016 0.0 0.0 0.0 0.0 0.0 /!name:QE03 0.204 0 0 1 11.875644190867 2.9799999853198 0.108 0.016 0.0 0.0 0.0 0.0 0.0 /!name:QE04 !!! 0 1 104 -2 0.0 0.0 12.175332190867 /!name:WS01 0 1 105 -2 0.0 0.0 12.327300190867 /!name:OTR1 !!! 0 1 106 -2 0.0 0.0 14.089061190867 /!name:WS02 0 1 107 -2 0.0 0.0 14.241029190867 /!name:OTR2 !!! 0 1 108 -2 0.0 0.0 16.002790190867 /!name:WS03 !!! 0 1 109 -2 0.0 0.0 16.154758190867 /!name:OTR3 0 0 0 -8 0.0 -1 16.5 /!name:SC_OFF 0 0 0 -99 3.0 0.0 3.0 /!name:stop_1