Twiss parameters output

A. Petrenko (Novosibirsk, 2019)

In [1]:
import numpy as np
import pandas as pd
import holoviews as hv
import os

hv.extension("bokeh", 'matplotlib')
In [2]:
import warnings
warnings.filterwarnings('ignore')
In [3]:
!pwd
/c/!Work/BINP/c-tau/Injector/debuncher
In [4]:
%output backend='bokeh'

%opts Curve Spread [width=700 height=300 show_grid=True \
            default_tools=['box_zoom','pan','wheel_zoom','reset']]

Elegant lattice file:

In [5]:
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 [6]:
with open("twiss.ele", "r") as f:
    print(f.read())
&transmute_elements name=*, type=WATCH, new_type=DRIF &end
&transmute_elements name=*, type=KICKER, new_type=DRIF &end
!&transmute_elements name=*, type=RFCA, new_type=DRIF &end

&divide_elements
    name = *
    maximum_length = 0.05
&end

&run_setup
    lattice = machine.lte
    magnets = results/beamline.mag
    centroid = results/beam.cen
    parameters = results/parameters.sdds
    p_central_mev = 1500.0
    use_beamline = machine
    default_order=2
&end

&twiss_output
    filename = results/twiss.twi
    matched = 0
    beta_x = 1.177900
    alpha_x = 1.204226
    beta_y = 2.237881
    alpha_y = -1.814342
    eta_x = 0
    eta_y = 0
    etap_x = 0
    etap_y = 0
&end

&matrix_output
    SDDS_output = results/R.sdds
&end

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

&run_control &end

&bunched_beam &end

&track &end

Run Elegant

In [7]:
!elegant twiss.ele
Running elegant at Sun Sep 22 00:09:11 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
&transmute_elements
    name = *,
    type = WATCH,
    exclude = {NULL},
    new_type = DRIF,
    disable = 0,
    clear_all = 0,
&end
statistics:    ET:     00:00:00 CP:    0.00 BIO:0 DIO:0 PF:0 MEM:0
&transmute_elements
    name = *,
    type = KICKER,
    exclude = {NULL},
    new_type = DRIF,
    disable = 0,
    clear_all = 0,
&end
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/beam.cen,
    sigma = {NULL},
    final = {NULL},
    acceptance = {NULL},
    losses = {NULL},
    magnets = results/beamline.mag,
    semaphore_file = {NULL},
    parameters = results/parameters.sdds,
    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.01 BIO:0 DIO:0 PF:0 MEM:0
&twiss_output
    filename = results/twiss.twi,
    matched = 0,
    output_at_each_step = 0,
    output_before_tune_correction = 0,
    final_values_only = 0,
    statistics = 0,
    radiation_integrals = 0,
    beta_x = 1.177900000000000e+00,
    alpha_x = 1.204226000000000e+00,
    eta_x = 0.000000000000000e+00,
    etap_x = 0.000000000000000e+00,
    beta_y = 2.237881000000000e+00,
    alpha_y = -1.814342000000000e+00,
    eta_y = 0.000000000000000e+00,
    etap_y = 0.000000000000000e+00,
    reference_file = {NULL},
    reference_element = {NULL},
    reference_element_occurrence = 0,
    reflect_reference_values = 0,
    concat_order = 3,
    higher_order_chromaticity = 0,
    higher_order_chromaticity_points = 5,
    higher_order_chromaticity_range = 4.000000000000000e-04,
    quick_higher_order_chromaticity = 0,
    chromatic_tune_spread_half_range = 0.000000000000000e+00,
    cavities_are_drifts_if_matched = 1,
    compute_driving_terms = 0,
    leading_order_driving_terms_only = 0,
    s_dependent_driving_terms_file = {NULL},
    local_dispersion = 1,
    n_periods = 1,
&end
final Twiss parameters (chromaticity valid for fully second-order calculation only!):
         beta          alpha           nu           eta          eta'       dnu/d(dp/p)   dbeta/(dp/p)     accept.
          m                          1/2pi           m                         1/2pi            m          mm-mrad
--------------------------------------------------------------------------------------------------------------------
  x:  6.486202e+00 -2.769782e-01  4.027850e+00 -2.214232e-03  9.932661e-05 -7.474778e+00 -6.967259e+00  1.499575e+01
  y:  2.027833e+00 -2.225418e-02  1.625959e+00  0.000000e+00  0.000000e+00 -6.117949e+00  1.380110e+01  1.762187e+01
x acceptance limited to 1.499575e-05 by W2 ending at 2.374800e+01 m
y acceptance limited to 1.762187e-05 by DBA_Q20 ending at 8.362000e+00 m
statistics:    ET:     00:00:00 CP:    0.31 BIO:0 DIO:0 PF:0 MEM:0
&matrix_output
    printout = {NULL},
    printout_order = 1,
    full_matrix_only = 0,
    SDDS_output = results/R.sdds,
    SDDS_output_order = 1,
    individual_matrices = 0,
    output_at_each_step = 0,
    start_from = {NULL},
    start_from_occurence = 1,
    SDDS_output_match = {NULL},
&end
warning: 35 elements had no matrix
statistics:    ET:     00:00:00 CP:    0.32 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.33 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.33 BIO:0 DIO:0 PF:0 MEM:0
&bunched_beam
    bunch = {NULL},
    n_particles_per_bunch = 1,
    time_start = 0.000000000000000e+00,
    matched_to_cell = {NULL},
    emit_x = 0.000000000000000e+00,
    emit_nx = 0.000000000000000e+00,
    beta_x = 1.000000000000000e+00,
    alpha_x = 0.000000000000000e+00,
    eta_x = 0.000000000000000e+00,
    etap_x = 0.000000000000000e+00,
    emit_y = 0.000000000000000e+00,
    emit_ny = 0.000000000000000e+00,
    beta_y = 1.000000000000000e+00,
    alpha_y = 0.000000000000000e+00,
    eta_y = 0.000000000000000e+00,
    etap_y = 0.000000000000000e+00,
    use_twiss_command_values = 0,
    Po = 2.935426143445352e+03,
    sigma_dp = 0.000000000000000e+00,
    sigma_s = 0.000000000000000e+00,
    dp_s_coupling = 0.000000000000000e+00,
    emit_z = 0.000000000000000e+00,
    beta_z = 0.000000000000000e+00,
    alpha_z = 0.000000000000000e+00,
    momentum_chirp = 0.000000000000000e+00,
    one_random_bunch = 1,
    save_initial_coordinates = 1,
    limit_invariants = 0,
    symmetrize = 0,
    halton_sequence[0] = 0, 0, 0,
    halton_radix[0] = 0, 0, 0, 0, 0, 0,
    optimized_halton = 0,
    randomize_order[0] = 0, 0, 0,
    limit_in_4d = 0,
    enforce_rms_values[0] = 0, 0, 0,
    distribution_cutoff[0] = 2.000000000000000e+00, 2.000000000000000e+00, 2.000000000000000e+00,
    distribution_type[0] = gaussian, gaussian, gaussian,
    centroid[0] = 0.000000000000000e+00, 0.000000000000000e+00, 0.000000000000000e+00, 0.000000000000000e+00, 
        0.000000000000000e+00, 0.000000000000000e+00,
    first_is_fiducial = 0,
&end
statistics:    ET:     00:00:00 CP:    0.33 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
generating bunch 1.0
tracking 1 particles
22 Sep 19 00:09:11: This step establishes energy profile vs s (fiducial beam).
22 Sep 19 00:09:11: Rf phases/references reset.
1 particles transmitted, total effort of 1 particle-turns
16 multipole kicks done

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


Finished tracking.
End of input data encountered.
statistics:    ET:     00:00:00 CP:    0.33 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 [8]:
os.listdir("results")
Out[8]:
['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 [9]:
out = !sdds2stream results/beamline.mag -col=ElementName,s,Profile -pipe=out
In [10]:
out[:3]
Out[10]:
['_BEGIN_ 0.000000000000000e+00 0.000000000000000e+00',
 'Q 0.000000000000000e+00 0.000000000000000e+00',
 'W0 0.000000000000000e+00 0.000000000000000e+00']
In [11]:
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 [12]:
from io import StringIO
In [13]:
DATA = StringIO("\n".join(out))
In [14]:
df_mag = pd.read_csv(DATA, names=['ElementName', 's', 'Profile'], delim_whitespace=True)
In [15]:
df_mag.head(3)
Out[15]:
ElementName s Profile
0 _BEGIN_ 0.0 0.0
1 Q 0.0 0.0
2 W0 0.0 0.0
In [16]:
df_mag.tail(3)
Out[16]:
ElementName s Profile
3489 QFW1 31.26433 0.0
3490 W3 31.26433 0.0
3491 W3 31.26433 0.0
In [17]:
dim_s = hv.Dimension('s', unit='m', label="s") # range=(-80,0))
In [18]:
#hv.help(hv.Curve)
In [19]:
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_mag, kdims=dim_s, vdims=['Profile', 'ElementName'], group='mag')
mag
Out[19]:

Reading Twiss parameters file

In [20]:
#!sddsquery results/twiss.twi
In [21]:
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 = pd.read_csv(DATA, names=['ElementName','s','betax','betay','alphax','alphay',
                              'etax','etay','p','xAperture','yAperture'],
                   delim_whitespace=True)
In [22]:
df.tail(3)
Out[22]:
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 0.015 0.015
674 QFW1 31.264332 6.486202 2.027833 -0.276978 -0.022254 -0.002214 0.0 2935.426143 0.015 0.015
675 W3 31.264332 6.486202 2.027833 -0.276978 -0.022254 -0.002214 0.0 2935.426143 0.015 0.015
In [23]:
mc = 0.511 # MeV/c
In [24]:
df['xAperture'] = 1e3*df['xAperture'] # mm
df['yAperture'] = 1e3*df['yAperture'] # mm

Reading $R_{56}$:

In [25]:
out = !sdds2stream results/R.sdds \
    -col=ElementName,s,R56 -pipe=out
DATA = StringIO("\n".join(out))
df_R = pd.read_csv(DATA, names=['ElementName','s','R56'], delim_whitespace=True)
In [26]:
df_R.tail(3)
Out[26]:
ElementName s R56
673 D1458 31.215743 0.317273
674 QFW1 31.264332 0.317273
675 W3 31.264332 0.317273
In [27]:
dim_R56  = hv.Dimension('R56', unit='m', label="R56")

R56 = hv.Curve((df_R.s, df_R.R56), label='R56', kdims=dim_s, vdims=dim_R56)
In [28]:
%opts Curve.fx (color='red', alpha=0.7, line_width=3)
%opts Curve.fy (color='blue', alpha=0.7, line_width=3)
dim_beta = hv.Dimension('beta', unit='m', label="β", range=(0,15))
dim_eta  = hv.Dimension('eta', unit='m', label="D", range=(-0.40,+0.40))

beta_x = hv.Curve((df.s, df.betax), label='βx', kdims=dim_s, vdims=dim_beta, group='fx')
beta_y = hv.Curve((df.s, df.betay), label='βy', kdims=dim_s, vdims=dim_beta, group='fy')

eta_x = hv.Curve((df.s, df.etax), label='Dx', kdims=dim_s, vdims=dim_eta, group='fx')
In [29]:
(beta_x*beta_y + eta_x*R56 + mag).cols(1)
Out[29]:
In [30]:
#!sddsprintout results/twiss.twi -par=nux -par=nuy
In [31]:
#Dx = hv.Curve((df.s, df.etax), label='Dx', kdims='s (m)', vdims='D (m)', group='fx')
#Dy = hv.Curve((df.s, df.etay), label='Dy', group='fy')

#(Dx*Dy + mag).cols(1)
In [32]:
e_nx = 5300.0 # normalized emittance [mm*mrad]
e_ny = 5300.0 # normalized emittance [mm*mrad]

#e_x = e_nx * 1e-6 / gamma # geometric emittance [m]

df['e_x'] = e_nx * 1e-6 / (df['p']/mc) # geometric emittance [m]
df['e_y'] = e_ny * 1e-6 / (df['p']/mc) # geometric emittance [m]
df['dp/p'] = 0.0335
In [33]:
df['sigma_x'] = 1e3*np.sqrt(df['betax']*df['e_x'] + df['etax']*df['etax']*df['dp/p']*df['dp/p']) # mm
df['sigma_y'] = 1e3*np.sqrt(df['betay']*df['e_y'] + df['etay']*df['etay']*df['dp/p']*df['dp/p']) # mm
df.tail(3)
Out[33]:
ElementName s betax betay alphax alphay etax etay p xAperture yAperture e_x e_y dp/p sigma_x sigma_y
673 D1458 31.215743 6.362722 2.057522 -2.251550 0.636334 -0.002202 0.0 2935.426143 15.0 15.0 9.226258e-07 9.226258e-07 0.0335 2.424016 1.377796
674 QFW1 31.264332 6.486202 2.027833 -0.276978 -0.022254 -0.002214 0.0 2935.426143 15.0 15.0 9.226258e-07 9.226258e-07 0.0335 2.447415 1.367820
675 W3 31.264332 6.486202 2.027833 -0.276978 -0.022254 -0.002214 0.0 2935.426143 15.0 15.0 9.226258e-07 9.226258e-07 0.0335 2.447415 1.367820
In [34]:
%opts Curve.Aper (color='gray', alpha=0.5, line_width=3)

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

Ax =     hv.Curve((df.s,  df.xAperture), label='Aperture', kdims=dim_s, vdims=dim_sigma, group='Aper')
Ax_pos = Ax
Ax_neg = hv.Curve((df.s, -df.xAperture), label='Aperture', kdims=dim_s, vdims=dim_sigma, group='Aper')

Sx = hv.Curve((df.s, 2*df.sigma_x), label='2σx', kdims=dim_s, vdims=dim_sigma, group='fx')
Sy = hv.Curve((df.s, 2*df.sigma_y), label='2σy', kdims=dim_s, vdims=dim_sigma, group='fy')
In [35]:
#(Sx*Sy*Ax + mag).cols(1)
In [36]:
(Sx*Sy + mag).cols(1)
Out[36]:

Beam centroid:

In [37]:
!sddsquery results/beam.cen
file results/beam.cen is in SDDS protocol version 1
description: centroid output--input: twiss.ele  lattice: machine.lte
contents: centroid output
data is little-endian binary

13 columns of data:
NAME            UNITS           SYMBOL          FORMAT          TYPE    FIELD  DESCRIPTION
                                                                        LENGTH
s               m               NULL            NULL            double  0       Distance
ElementName     NULL            NULL            %10s            string  0       Element name
ElementOccurence NULL            NULL            %6ld            long    0       Occurence of element
ElementType     NULL            NULL            %10s            string  0       Element-type name
Cx              m               <x>             NULL            double  0       x centroid
Cxp             NULL            <x'>            NULL            double  0       x' centroid
Cy              m               <y>             NULL            double  0       y centroid
Cyp             NULL            <y'>            NULL            double  0       y' centroid
Cs              m               <s>             NULL            double  0       mean distance traveled
Cdelta          NULL            <$gd$r>         NULL            double  0       delta centroid
Particles       NULL            NULL            NULL            long    0       Number of particles
pCentral        m$be$nc         p$bcen$n        NULL            double  0       Reference beta*gamma
Charge          C               NULL            NULL            double  0       Charge in the beam

2 parameters:
NAME                UNITS               SYMBOL              TYPE                DESCRIPTION
Step                NULL                NULL                long                Simulation step
SVNVersion          NULL                NULL                string              SVN version number
In [38]:
out = !sdds2stream results/beam.cen -col=ElementName,s,Cx,Cy,pCentral -pipe=out
DATA = StringIO("\n".join(out))
df_cen = pd.read_csv(DATA, names=['ElementName','s','Cx','Cy','p'],
                   delim_whitespace=True)
In [39]:
df_cen.tail(3)
Out[39]:
ElementName s Cx Cy p
673 D1458 31.215743 0.0 0.0 2935.426143
674 QFW1 31.264332 0.0 0.0 2935.426143
675 W3 31.264332 0.0 0.0 2935.426143
In [40]:
df_cen['Cx'] = df_cen['Cx']*1e3 # m -> mm
df_cen['Cy'] = df_cen['Cy']*1e3 # m -> mm
df_cen['p']  = df_cen['p']*0.511 # gamma*beta -> MeV/c
In [41]:
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 [42]:
#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 [43]:
(Ax_pos*Ax_neg*Cy*Cx + mag).cols(1)
Out[43]:

Tunnel walls:

In [44]:
df_wall = pd.read_csv('../../drawings_and_maps/results/tunnel.csv')
In [45]:
%output backend='matplotlib' fig='svg' size=200
#%output backend='matplotlib' fig='png' size=160
%opts Layout [tight=True]
%opts Curve [show_grid=True aspect='equal']
In [46]:
dim_X = hv.Dimension('X', label='X', unit='m') #,  range=(z_min,z_max))
dim_Z = hv.Dimension('Z', label='Z', unit='m', range=(-28,+28)) #,  range=(z_min,z_max))

%opts Curve.Tunnel (color='gray', alpha=0.5, line_width=3)
In [47]:
Tunnel = hv.Curve((df_wall.Z,df_wall.X), kdims=dim_Z, vdims=dim_X, group='Tunnel')
Tunnel
Out[47]:

Reading xyz.sdds file

In [48]:
out = !sdds2stream results/xyz.sdds \
    -col=ElementName,s,X,Y,Z,theta,phi,psi -pipe=out
DATA = StringIO("\n".join(out))
df_xyz = pd.read_csv(DATA, names=['ElementName','s','X','Y','Z','theta','phi','psi'],
                   delim_whitespace=True)
In [49]:
df_xyz['X'] = df_xyz['X']
df_xyz['Z'] = df_xyz['Z'] - 20.0
In [50]:
df_xyz.tail(3)
Out[50]:
ElementName s X Y Z theta phi psi
673 D1458 31.215743 -11.813545 0.0 4.226327 3.313322e-16 0.0 0.0
674 QFW1 31.264332 -11.813545 0.0 4.274916 3.313322e-16 0.0 0.0
675 W3 31.264332 -11.813545 0.0 4.274916 3.313322e-16 0.0 0.0
In [51]:
from scipy import interpolate

theta = interpolate.interp1d(
    df_xyz.s.values, df_xyz.theta.values,
    fill_value=(0, 0), bounds_error=False
)

X0 = interpolate.interp1d(
    df_xyz.s.values, df_xyz.X.values,
    fill_value=(0, 0), bounds_error=False
)

Z0 = interpolate.interp1d(
    df_xyz.s.values, df_xyz.Z.values,
    fill_value=(0, 0), bounds_error=False
)
In [52]:
s = df_mag.s.values
nx =  np.cos(theta(s))
nz = -np.sin(theta(s))

Element_width = 0.5 # m

df_mag['X'] = X0(s) + Element_width*df_mag['Profile']*nx
df_mag['Z'] = Z0(s) + Element_width*df_mag['Profile']*nz
In [53]:
df_mag.tail(6)
Out[53]:
ElementName s Profile X Z
3486 QFW1 31.26433 1.0 -11.313545 4.274914
3487 QFW1 31.26433 0.0 -11.813545 4.274914
3488 QFW1 31.21574 0.0 -11.813545 4.226324
3489 QFW1 31.26433 0.0 -11.813545 4.274914
3490 W3 31.26433 0.0 -11.813545 4.274914
3491 W3 31.26433 0.0 -11.813545 4.274914
In [54]:
#XY_traj = hv.Curve((df_xyz.Z,df_xyz.X), kdims=dim_Z, vdims=dim_X)
#XY_traj*Tunnel
In [55]:
XY_traj = hv.Curve((df_mag.Z,df_mag.X), kdims=dim_Z, vdims=dim_X)
XY_traj*Tunnel
Out[55]:
In [56]:
%output backend='bokeh'

%opts Curve.mag2D [width=800 height=700 show_grid=True tools=[hover]] (color='black', alpha=0.5)

mag = hv.Curve(df_mag, kdims=dim_Z, vdims=[dim_X, 'ElementName'], group='mag2D')
mag*Tunnel
Out[56]:

Misc tests

In [ ]: