Tracking

A. Petrenko (Novosibirsk, 2019)

In [1]:
import numpy as np
import pandas as pd
import holoviews as hv
import os
from IPython.display import Latex
from IPython.display import Image

hv.extension("bokeh")
In [2]:
%opts Curve Spread [width=700 height=300 show_grid=True \
            default_tools=['box_zoom','pan','wheel_zoom','reset']]

Elegant lattice file:

Plot sdds-beam

In [3]:
w = "../p-linac/FODO_and_e-p_sep/results/w3.sdds"
In [4]:
#!plaindata2sdds
In [5]:
png = "results/image.png"
!sddsplot -lay=2,2 -device=lpng -output=$png \
    -col=x,y -graph=dot,type=2 $w -endPanel \
    -col=yp,y -graph=dot,type=2  $w -endPanel \
    -col=x,xp -graph=dot,type=2  $w -endPanel \
    -col=yp,xp -graph=dot,type=2 $w
Image(png) 
Out[5]:
In [6]:
!sddsplot -device=lpng -output=$png -lay=2,2 "-title=" \
    -col=t,p -factor=yMult=0.511,xMult=-3e11 \
        "-xlabel=z (mm)" "-ylabel=p (MeV/c)" -graph=dot,type=2 $w -endPanel \
    -col=xp,p -factor=yMult=0.511,xMult=1e3 \
        "-xlabel=xp (mrad)" "-ylabel=p (MeV/c)" -graph=dot,type=2 $w -endPanel \
    -col=t,x -factor=xMult=-3e11,yMult=1e3 \
        "-xlabel=z (mm)" "-ylabel=x (mm)" -graph=dot,type=2 $w -endPanel \
    -col=xp,x -factor=yMult=1e3,xMult=1e3 \
        "-xlabel=xp (mrad)" "-ylabel=x (mm)" -graph=dot,type=2 $w -endPanel
Image(png)
Out[6]:

Run sddsanalyzebeam to check the beam parameters:

In [7]:
beam_analyzed = 'results/beam_analyzed.sdds'
sdds = "../p-linac/FODO_and_e-p_sep/results/w3.sdds"
!sddsanalyzebeam $sdds $beam_analyzed
In [8]:
!sddsprintout -par=PassLength $sdds
Printout for SDDS file ../p-linac/FODO_and_e-p_sep/results/w3.sdds

PassLength (m) =  8.180000e+01

In [9]:
items = np.array([
    # name in file   symbol                   factor   units
    ['enx',        '\\epsilon_{nx}',         1.0e6,   'mm \\cdot mrad'],
    ['betax',      '\\beta_x',               1.0,     'm'             ],
    ['alphax',     '\\alpha_x',              1.0,     ''              ],
    ['Sx',         '\\sigma_x',              1.0e3,   'mm'            ],
    ['Sxp',        "\\sigma_{x'}",           1.0e3,   'mrad'          ],
    ['eny',        '\\epsilon_{ny}',         1.0e6,   'mm \\cdot mrad'],
    ['betay',      '\\beta_y',               1.0,     'm'             ],
    ['alphay',     '\\alpha_y',              1.0,     ''              ],
#   ['Sy',         '\\sigma_y',              1.0e3,   'mm'            ],
#   ['Syp',        "\\sigma_{y'}",           1.0e3,   'mrad'          ],
    ['St',         '\\sigma_t',              1e12,    'ps'            ],
    ['St',         '\\sigma_z',              3e11,    'mm'            ],
    ['Sdelta',     '\\sigma_{\\Delta p/p}',  100.0,   '\\%'           ],
    ['pAverage',   'p_{average}',            0.511,   'MeV/c'         ],
    ['Charge',     'Q',                      1.0e9,   'nC'            ],
])

names = items[:,0]
cols_str = "-col=" + ",".join(names)
cols_str
Out[9]:
'-col=enx,betax,alphax,Sx,Sxp,eny,betay,alphay,St,St,Sdelta,pAverage,Charge'
In [10]:
out = !sdds2stream $beam_analyzed $cols_str -pipe=out
values = out[0].split()
values = np.array(values)
values = values.astype(np.float)
values = dict(zip(names, values))
In [11]:
latex_str = r"\begin{aligned}"

for name,symbol,factor,units in items:
    value = values[name]*factor.astype(float)
    latex_str = latex_str + r"%s &= %.3f~\rm{%s} \\" % (symbol, value, units)

latex_str = latex_str + r"\end{aligned}"
In [12]:
Latex(latex_str)
Out[12]:
\begin{aligned}\epsilon_{nx} &= 3614.740~\rm{mm \cdot mrad} \\\beta_x &= 1.113~\rm{m} \\\alpha_x &= 1.036~\rm{} \\\sigma_x &= 1.174~\rm{mm} \\\sigma_{x'} &= 1.520~\rm{mrad} \\\epsilon_{ny} &= 3597.825~\rm{mm \cdot mrad} \\\beta_y &= 2.127~\rm{m} \\\alpha_y &= -1.749~\rm{} \\\sigma_t &= 11.257~\rm{ps} \\\sigma_z &= 3.377~\rm{mm} \\\sigma_{\Delta p/p} &= 3.350~\rm{\%} \\p_{average} &= 1490.811~\rm{MeV/c} \\Q &= 3.812~\rm{nC} \\\end{aligned}
In [13]:
with open("machine.lte", "r") as f:
    print(f.read())
q: charge,total=3.81e-9
w0: watch,filename="results/w0.sdds",mode=coord
w1: watch,filename="results/w1.sdds",mode=coord
w2: watch,filename="results/w2.sdds",mode=coord
w3: watch,filename="results/w3.sdds",mode=coord

D100:  DRIFT, L=0.6
Q100:  QUAD, L=0.40, K1 = -2.0408
DQ100: DRIFT, L=0.6
Q110:  QUAD, L=0.40, K1 = +2.6129
DQ110: DRIFT, L=0.6
Q120:  QUAD, L=0.40, K1 = -2.064
DQ120: DRIFT, L=0.6
Q130:  QUAD, L=0.40, K1 = +1.0426
DQ130: DRIFT, L=0.1

MA20:    MAXAMP, X_MAX=10.0e-3, Y_MAX=10.0e-3, ELLIPTICAL=1
MA50x30: MAXAMP, X_MAX=25.0e-3, Y_MAX=15.0e-3, ELLIPTICAL=1
MA30:    MAXAMP, X_MAX=15.0e-3, Y_MAX=15.0e-3, ELLIPTICAL=1

DBA_D10:  DRIFT, L=0.05
DBA_Q10:  QUAD,  L=0.15, K1=+11.0
DBA_DQ10: DRIFT, L=0.10
DBA_Q20:  QUAD,  L=0.15, K1=-11.5
DBA_DQ20: DRIFT, L=0.10

DBA_B10:  SBEND, L=1.531, ANGLE=0.396
DBA_DB10: DRIFT, L=0.10
DBA_Q30:  QUAD,  L=0.15, K1=+7.20
DBA_DQ30: DRIFT, L=0.0

DBA_DOUBLET: LINE=(MA30,DBA_D10,DBA_Q10,DBA_DQ10,DBA_Q20,DBA_DQ20,MA30)
HDBA:        LINE=(MA50x30,DBA_B10,DBA_DB10,DBA_Q30,DBA_DQ30,MA50x30)

!DBA:  LINE=(HDBA,-HDBA)


DBA_NEG_B10:  SBEND, L=1.531, ANGLE=-0.396

HDBA_NEG: LINE=(MA50x30,DBA_NEG_B10,DBA_DB10,DBA_Q30,DBA_DQ30,MA50x30)

!DBA_NEG: LINE=(HDBA_NEG,-HDBA_NEG)


D140:  DRIFT, L=0.05
Q140:  QUAD,  L=0.15, K1=-11.0
DQ140: DRIFT, L=0.20
Q150:  QUAD,  L=0.15, K1=+11.0
DQ150: DRIFT, L=1.00

RF_DEBUNCHER: RFCA, L=3.0, VOLT=-60.0e6, PHASE=0.0, FREQ=2856e6, change_p0=0

D150: DRIFT, L=0.5

Q160:  QUAD,  L=0.15, K1=-6.4
DQ160: DRIFT, L=0.24
Q170:  QUAD,  L=0.15, K1=+8.1
DQ170: DRIFT, L=1.8


QDS19: KQUAD, L=0.1700626959247649, K1 = -3.0509003681938456
D1458: DRIFT, L=1.4576802507836992
QFW1: KQUAD, L=0.04858934169278997, K1 = 6.380867488799166

machine: LINE=(q, w0, MA30, &
      D100,Q100,DQ100,Q110,DQ110,Q120,DQ120,Q130,DQ130, &
      DBA_DOUBLET,HDBA,w1,-HDBA,-DBA_DOUBLET, DBA_DOUBLET,HDBA,-HDBA,-DBA_DOUBLET, &
      DBA_DOUBLET,HDBA_NEG,-HDBA_NEG,-DBA_DOUBLET, DBA_DOUBLET,HDBA_NEG,-HDBA_NEG, &
      MA30, D140,Q140,DQ140,Q150,DQ150, &
      w2, MA20, RF_DEBUNCHER, MA30, D150, &
      Q160,DQ160,Q170,DQ170, &
      QDS19,D1458,QFW1, &
w3)

In [14]:
with open("track.ele", "r") as f:
    print(f.read())
!&transmute_elements name=*, type=*QUAD, new_type=DRIF &end

&divide_elements
    name = *
    maximum_length = 0.05
&end

&run_setup
    lattice = machine.lte
    use_beamline = machine
    p_central_mev = 1500.0
    sigma = results/%s.sig
    centroid = results/%s.cen
    magnets = results/beamline.mag
!    output = results/%s.out
!    final = results/%s.fin
    default_order=2
!    default_order=1
&end

&floor_coordinates
    filename = results/xyz.sdds
    include_vertices = 0
    vertices_only = 0
    magnet_centers = 0
&end

&run_control
    n_steps = 1
&end

&sdds_beam
    input = ../p-linac/FODO_and_e-p_sep/results/w3.sdds
&end

&track &end

Run Elegant

In [15]:
#!elegant track.ele
In [16]:
import subprocess
import sys

def run_cmd(cmd):
    sub_process = subprocess.Popen(cmd, shell=True,
                               stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    while sub_process.poll() is None:
        out = sub_process.stdout.read(10)
        sys.stdout.write(out)
        sys.stdout.flush()
In [17]:
run_cmd("elegant track.ele")
Running elegant at Sun Sep 22 00:11:25 2019

This is elegant 29.1.0, Mar  3 2016, by M. Borland, M. Carla', N. Carmignani, M. Ehrlichman, L. Emery, W. Guo, V. Sajaev, R. Soliday, Y.-P. Sun, C.-X. Wang, Y. Wang, Y. Wu, and A. Xiao.
Link date: Mar  3 2016 21:13:54, SVN revision: 22803M
statistics:    ET:     00:00:00 CP:    0.00 BIO:0 DIO:0 PF:0 MEM:0
&divide_elements
    name = *,
    type = {NULL},
    exclude = {NULL},
    divisions = 0,
    maximum_length = 5.000000000000000e-02,
    clear = 0,
&end
statistics:    ET:     00:00:00 CP:    0.00 BIO:0 DIO:0 PF:0 MEM:0
&run_setup
    lattice = machine.lte,
    use_beamline = machine,
    rootname = {NULL},
    output = {NULL},
    centroid = results/%s.cen,
    sigma = results/%s.sig,
    final = {NULL},
    acceptance = {NULL},
    losses = {NULL},
    magnets = results/beamline.mag,
    semaphore_file = {NULL},
    parameters = {NULL},
    combine_bunch_statistics = 0,
    wrap_around = 1,
    final_pass = 0,
    default_order = 2,
    concat_order = 0,
    print_statistics = 0,
    show_element_timing = 0,
    monitor_memory_usage = 0,
    random_number_seed = 987654321,
    correction_iterations = 1,
    echo_lattice = 0,
    p_central = 0.000000000000000e+00,
    p_central_mev = 1.500000000000000e+03,
    always_change_p0 = 0,
    load_balancing_on = 0,
    random_sequence_No = 1,
    expand_for = {NULL},
    tracking_updates = 1,
    search_path = {NULL},
    element_divisions = 0,
&end
Seeding random number generators
length of beamline MACHINE per pass: 3.126433228840139e+01 m
statistics:    ET:     00:00:00 CP:    0.02 BIO:0 DIO:0 PF:0 MEM:0
&floor_coordinates
    filename = results/xyz.sdds,
    X0 = 0.000000000000000e+00,
    Y0 = 0.000000000000000e+00,
    Z0 = 0.000000000000000e+00,
    theta0 = 0.000000000000000e+00,
    phi0 = 0.000000000000000e+00,
    psi0 = 0.000000000000000e+00,
    include_vertices = 0,
    vertices_only = 0,
    magnet_centers = 0,
    store_vertices = 0,
&end
statistics:    ET:     00:00:00 CP:    0.02 BIO:0 DIO:0 PF:0 MEM:0
&run_control
    n_steps = 1,
    bunch_frequency = 0.000000000000000e+00,
    n_indices = 0,
    n_passes = 1,
    n_passes_fiducial = 0,
    reset_rf_for_each_step = 1,
    first_is_fiducial = 0,
    restrict_fiducialization = 0,
    reset_scattering_seed = 0,
&end
statistics:    ET:     00:00:00 CP:    0.07 BIO:0 DIO:0 PF:0 MEM:0
&sdds_beam
    input = ../p-linac/FODO_and_e-p_sep/results/w3.sdds,
    input_list = {NULL},
    input_type = elegant,
    selection_parameter = {NULL},
    selection_string = {NULL},
    one_random_bunch = 0,
    n_particles_per_ring = 0,
    reuse_bunch = 0,
    prebunched = -1,
    track_pages_separately = 0,
    use_bunched_mode = 0,
    sample_interval = 1,
    n_tables_to_skip = 0,
    center_transversely = 0,
    center_arrival_time = 0,
    reverse_t_sign = 0,
    sample_fraction = 1.000000000000000e+00,
    p_lower = 0.000000000000000e+00,
    p_upper = 0.000000000000000e+00,
    save_initial_coordinates = 1,
    n_duplicates = 0,
    duplicate_stagger[0] = 0.000000000000000e+00, 0.000000000000000e+00, 0.000000000000000e+00, 0.000000000000000e+00, 
        0.000000000000000e+00, 0.000000000000000e+00,
&end
statistics:    ET:     00:00:00 CP:    0.07 BIO:0 DIO:0 PF:0 MEM:0
&track
    center_on_orbit = 0,
    center_momentum_also = 1,
    offset_by_orbit = 0,
    offset_momentum_also = 1,
    soft_failure = 1,
    use_linear_chromatic_matrix = 0,
    longitudinal_ring_only = 0,
    ibs_only = 0,
    stop_tracking_particle_limit = -1,
&end
tracking step 1
File ../p-linac/FODO_and_e-p_sep/results/w3.sdds opened and checked.
Read page 1 from file
0 particle ID slots per bunch
80012 rows in page 1
File ../p-linac/FODO_and_e-p_sep/results/w3.sdds was used up and closed.
a total of 80012 data points were read

tracking 80012 particles
22 Sep 19 00:11:25: This step establishes energy profile vs s (fiducial beam).
22 Sep 19 00:11:25: Rf phases/references reset.
72799 particles transmitted, total effort of 72799 particle-turns
1164784 multipole kicks done

Dumping centroid data...done.
Dumping sigma data...done.
Post-tracking output completed.
Tracking step completed   ET:     00:00:34 CP:   34.03 BIO:0 DIO:0 PF:0 MEM:0


Finished tracking.
End of input data encountered.
statistics:    ET:     00:00:34 CP:   34.06 BIO:0 DIO:0 PF:0 MEM:0
=====================================================================================
Thanks for using elegant.  Please cite the following reference in your publications:
  M. Borland, "elegant: A Flexible SDDS-Compliant Code for Accelerator Simulation,"
  Advanced Photon Source LS-287, September 2000.
If you use a modified version, please indicate this in all publications.
=====================================================================================
In [18]:
w = "results/w0.sdds"

!sddsplot -lay=2,2 -device=lpng -output=$png \
    -col=x,y -graph=dot,type=2 $w -endPanel \
    -col=yp,y -graph=dot,type=2 $w -endPanel \
    -col=x,xp -graph=dot,type=2  $w -endPanel \
    -col=yp,xp -graph=dot,type=2 $w
Image(png)
Out[18]:
In [19]:
!sddsplot -device=lpng -output=$png -lay=2,2 "-title=" \
    -col=dt,p -factor=yMult=0.511,xMult=-3e11 \
        "-xlabel=z (mm)" "-ylabel=p (MeV/c)" -graph=dot,type=2 $w -endPanel \
    -col=xp,p -factor=yMult=0.511,xMult=1e3 \
        "-xlabel=xp (mrad)" "-ylabel=p (MeV/c)" -graph=dot,type=2 $w -endPanel \
    -col=dt,x -factor=xMult=-3e11,yMult=1e3 \
        "-xlabel=z (mm)" "-ylabel=x (mm)" -graph=dot,type=2 $w -endPanel \
    -col=xp,x -factor=yMult=1e3,xMult=1e3 \
        "-xlabel=xp (mrad)" "-ylabel=x (mm)" -graph=dot,type=2 $w -endPanel
Image(png)
Out[19]:
In [20]:
w = "results/w3.sdds"

!sddsplot -lay=2,2 -device=lpng -output=$png \
    -col=x,y -graph=dot,type=2 $w -endPanel \
    -col=yp,y -graph=dot,type=2  $w -endPanel \
    -col=x,xp -graph=dot,type=2  $w -endPanel \
    -col=yp,xp -graph=dot,type=2 $w
Image(png)
Out[20]:
In [21]:
!sddsplot -device=lpng -output=$png -lay=2,2 "-title=" \
    -col=dt,p -factor=yMult=0.511,xMult=-3e11 \
        "-xlabel=z (mm)" "-ylabel=p (MeV/c)" -graph=dot,type=2 $w -endPanel \
    -col=xp,p -factor=yMult=0.511,xMult=1e3 \
        "-xlabel=xp (mrad)" "-ylabel=p (MeV/c)" -graph=dot,type=2 $w -endPanel \
    -col=dt,x -factor=xMult=-3e11,yMult=1e3 \
        "-xlabel=z (mm)" "-ylabel=x (mm)" -graph=dot,type=2 $w -endPanel \
    -col=xp,x -factor=yMult=1e3,xMult=1e3 \
        "-xlabel=xp (mrad)" "-ylabel=x (mm)" -graph=dot,type=2 $w -endPanel
Image(png)
Out[21]:

Now let's plot the results with holoviews

In [22]:
os.listdir("results")
Out[22]:
['beam.cen',
 'beamline.mag',
 'beam_analyzed.sdds',
 'image.png',
 'parameters.sdds',
 'R.sdds',
 'track.cen',
 'track.sig',
 'twiss.twi',
 'w0.sdds',
 'w1.sdds',
 'w2.sdds',
 'w3.sdds',
 'xyz.sdds']

Reading beamline.mag

In [23]:
out = !sdds2stream results/beamline.mag -col=ElementName,s,Profile -pipe=out
In [24]:
out[:3]
Out[24]:
['_BEGIN_ 0.000000000000000e+00 0.000000000000000e+00',
 'Q 0.000000000000000e+00 0.000000000000000e+00',
 'W0 0.000000000000000e+00 0.000000000000000e+00']
In [25]:
print("\n".join(out[:3]))
_BEGIN_ 0.000000000000000e+00 0.000000000000000e+00
Q 0.000000000000000e+00 0.000000000000000e+00
W0 0.000000000000000e+00 0.000000000000000e+00
In [26]:
from io import StringIO
In [27]:
DATA = StringIO("\n".join(out))
In [28]:
df = pd.read_csv(DATA, names=['ElementName', 's', 'Profile'], delim_whitespace=True)
In [29]:
df.head(3)
Out[29]:
ElementName s Profile
0 _BEGIN_ 0.0 0.0
1 Q 0.0 0.0
2 W0 0.0 0.0
In [30]:
df.tail(3)
Out[30]:
ElementName s Profile
3485 QFW1 31.21574 0.0
3486 QFW1 31.26433 0.0
3487 W3 31.26433 0.0
In [31]:
dim_s = hv.Dimension('s', unit='m', label="s") # range=(-80,0))
In [32]:
#hv.help(hv.Curve)
In [33]:
from bokeh.models import HoverTool
#hover = HoverTool(tooltips=[("Name", "@ElementName")])
hover = HoverTool(tooltips="@ElementName")

%opts Curve.mag [height=70 show_frame=False show_title=False \
                 xaxis=None yaxis=None tools=['xbox_zoom, xpan', hover]] (color='black', alpha=0.3)

mag = hv.Curve(df, kdims=dim_s, vdims=['Profile', 'ElementName'], group='mag')
mag
Out[33]:

Reading track.sig file

In [34]:
#!sddsquery results/track.sig
In [35]:
out = !sdds2stream results/track.sig -col=ElementName,s,minimum1,maximum1,minimum3,maximum3,Sx,Sy -pipe=out
DATA = StringIO("\n".join(out))
df = pd.read_csv(DATA, names=['ElementName','s','xmin','xmax','ymin','ymax','sigma_x','sigma_y'],
                   delim_whitespace=True)
In [36]:
df.tail(3)
Out[36]:
ElementName s xmin xmax ymin ymax sigma_x sigma_y
673 D1458 31.215743 -0.007658 0.009890 -0.005167 0.004802 0.002878 0.001551
674 QFW1 31.264332 -0.007726 0.009959 -0.005176 0.004803 0.002905 0.001543
675 W3 31.264332 -0.007726 0.009959 -0.005176 0.004803 0.002905 0.001543
In [37]:
df['xmin'] = df['xmin']*1e3; df['xmax'] = df['xmax']*1e3 # mm
df['ymin'] = df['ymin']*1e3; df['ymax'] = df['ymax']*1e3 # mm
df['sigma_x'] = df['sigma_x']*1e3; df['sigma_y'] = df['sigma_y']*1e3 # mm
In [38]:
#df.tail(3)
In [39]:
dim_x = hv.Dimension('x', unit='mm', label="x", range=(0,None))
dim_y = hv.Dimension('y', unit='mm', label="y", range=(0,None))

%opts Curve.fx (color='red', alpha=0.7, line_width=3)
%opts Curve.fy (color='blue', alpha=0.7, line_width=3)
In [40]:
Sx = hv.Curve((df.s, df.sigma_x), label='σx', kdims=dim_s, vdims=dim_x, group='fx')
Sy = hv.Curve((df.s, df.sigma_y), label='σy', kdims=dim_s, vdims=dim_y, group='fy')
In [41]:
(Sx*Sy + mag).cols(1)
Out[41]:

Reading track.cen file

In [42]:
#!sddsquery results/track.cen
In [43]:
out = !sdds2stream results/track.cen -col=ElementName,s,Particles,pCentral,Cx,Cy,Charge -pipe=out
DATA = StringIO("\n".join(out))
df_cen = pd.read_csv(DATA, names=['ElementName','s','Particles','p','Cx','Cy','Charge'],
                   delim_whitespace=True)

df_cen['p'] = 0.511*df_cen['p'] # MeV/c
df_cen['Cx'] = 1e3*df_cen['Cx'] # mm
df_cen['Cy'] = 1e3*df_cen['Cy'] # mm
In [44]:
df_cen.tail(3)
Out[44]:
ElementName s Particles p Cx Cy Charge
673 D1458 31.215743 72799 1500.002759 0.719874 0.006626 3.466532e-09
674 QFW1 31.264332 72799 1500.002759 0.721919 0.004448 3.466532e-09
675 W3 31.264332 72799 1500.002759 0.721919 0.004448 3.466532e-09
In [45]:
dim_Q = hv.Dimension('Q', range=(0,None), unit='nC') # range=(-80,0))

Charge = hv.Curve((df_cen.s, df_cen.Charge*1e9), kdims=dim_s, vdims=dim_Q)
In [46]:
(Charge + mag).cols(1)
Out[46]:

Reading twiss.sdds file

In [47]:
out = !sdds2stream results/twiss.twi \
    -col=ElementName,s,betax,betay,alphax,alphay,etax,etay,pCentral0,xAperture,yAperture -pipe=out
DATA = StringIO("\n".join(out))
df_twi = pd.read_csv(DATA, names=['ElementName','s','betax','betay','alphax','alphay',
                              'etax','etay','p','xAperture','yAperture'],
                   delim_whitespace=True)
In [48]:
df_twi['xAperture'] = 1e3*df_twi['xAperture'] # mm
df_twi['yAperture'] = 1e3*df_twi['yAperture'] # mm
In [49]:
df_twi.tail(3)
Out[49]:
ElementName s betax betay alphax alphay etax etay p xAperture yAperture
673 D1458 31.215743 6.362722 2.057522 -2.251550 0.636334 -0.002202 0.0 2935.426143 15.0 15.0
674 QFW1 31.264332 6.486202 2.027833 -0.276978 -0.022254 -0.002214 0.0 2935.426143 15.0 15.0
675 W3 31.264332 6.486202 2.027833 -0.276978 -0.022254 -0.002214 0.0 2935.426143 15.0 15.0
In [50]:
dim_x = hv.Dimension('x', unit='mm', label="x", range=(-30,+30))
dim_y = hv.Dimension('y', unit='mm', label="y", range=(-30,+30))
dim_p = hv.Dimension('p', unit='MeV/c', label="p", range=(0,None))
In [51]:
%opts Curve.Aper (color='gray', alpha=0.5, line_width=3)

dim_sigma = hv.Dimension('sigma', unit='mm', label="2σ")#, range=(0,None))
dim_aper  = hv.Dimension('aper', unit='mm', label="Aperture")#, range=(0,None))

Ax =     hv.Curve((df_twi.s,  df_twi.xAperture), label='Aperture', kdims=dim_s, vdims=dim_sigma, group='Aper')
Ax_pos = Ax
Ax_neg = hv.Curve((df_twi.s, -df_twi.xAperture), label='Aperture', kdims=dim_s, vdims=dim_sigma, group='Aper')
In [52]:
#Ax_pos*Ax_neg
In [53]:
#s_p = hv.Curve((df_cen.s, df_cen.p), kdims=dim_s, vdims=dim_p)

Cx  = hv.Spread((df_cen.s, df_cen.Cx, 2*df.sigma_x), label='x', \
               kdims=dim_s, vdims=[dim_x,dim_aper])
Cy  = hv.Spread((df_cen.s, df_cen.Cy, 2*df.sigma_y), label='y', \
               kdims=dim_s, vdims=[dim_y,dim_aper])
In [54]:
(Ax_pos*Ax_neg*Cy*Cx + mag).cols(1)
Out[54]:
In [55]:
s_Cx = hv.Curve((df_cen.s, df_cen.Cx), kdims=dim_s, vdims=dim_x, group='fx', label='x')
s_Cy = hv.Curve((df_cen.s, df_cen.Cy), kdims=dim_s, vdims=dim_x, group='fy', label='y')
In [56]:
(s_Cx*s_Cy*Ax_pos*Ax_neg + mag).cols(1)
Out[56]:

Resulting beam paramaters:

In [57]:
beam_analyzed = 'results/beam_analyzed.sdds'
w = "results/w3.sdds"
!sddsanalyzebeam $w $beam_analyzed
In [58]:
items = np.array([
    # name in file   symbol                   factor   units
    ['enx',        '\\epsilon_{nx}',         1.0e6,   'mm \\cdot mrad'],
    ['betax',      '\\beta_x',               1.0,     'm'             ],
    ['alphax',     '\\alpha_x',              1.0,     ''              ],
    ['Sx',         '\\sigma_x',              1.0e3,   'mm'            ],
    ['Sxp',        "\\sigma_{x'}",           1.0e3,   'mrad'          ],
    ['eny',        '\\epsilon_{ny}',         1.0e6,   'mm \\cdot mrad'],
    ['betay',      '\\beta_y',               1.0,     'm'             ],
    ['alphay',     '\\alpha_y',              1.0,     ''              ],
#   ['Sy',         '\\sigma_y',              1.0e3,   'mm'            ],
#   ['Syp',        "\\sigma_{y'}",           1.0e3,   'mrad'          ],
    ['St',         '\\sigma_t',              1e12,    'ps'            ],
    ['St',         '\\sigma_z',              3e11,    'mm'            ],
    ['Sdelta',     '\\sigma_{\\Delta p/p}',  100.0,   '\\%'           ],
    ['pAverage',   'p_{average}',            0.511,   'MeV/c'         ],
    ['Charge',     'Q',                      1.0e9,   'nC'            ],
])

names = items[:,0]
cols_str = "-col=" + ",".join(names)
cols_str
Out[58]:
'-col=enx,betax,alphax,Sx,Sxp,eny,betay,alphay,St,St,Sdelta,pAverage,Charge'
In [59]:
out = !sdds2stream $beam_analyzed $cols_str -pipe=out
values = out[0].split()
values = np.array(values)
values = values.astype(np.float)
values = dict(zip(names, values))
In [60]:
latex_str = r"\begin{aligned}"

for name,symbol,factor,units in items:
    value = values[name]*factor.astype(float)
    latex_str = latex_str + r"%s &= %.3f~\rm{%s} \\" % (symbol, value, units)

latex_str = latex_str + r"\end{aligned}"
In [61]:
from IPython.display import Latex
In [62]:
Latex(latex_str)
Out[62]:
\begin{aligned}\epsilon_{nx} &= 4547.509~\rm{mm \cdot mrad} \\\beta_x &= 5.460~\rm{m} \\\alpha_x &= -0.200~\rm{} \\\sigma_x &= 2.905~\rm{mm} \\\sigma_{x'} &= 0.543~\rm{mrad} \\\epsilon_{ny} &= 3510.154~\rm{mm \cdot mrad} \\\beta_y &= 1.996~\rm{m} \\\alpha_y &= -0.108~\rm{} \\\sigma_t &= 13.899~\rm{ps} \\\sigma_z &= 4.170~\rm{mm} \\\sigma_{\Delta p/p} &= 0.750~\rm{\%} \\p_{average} &= 1503.852~\rm{MeV/c} \\Q &= 3.467~\rm{nC} \\\end{aligned}
In [ ]: