Twiss parameters output

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", 'matplotlib')
In [2]:
import warnings
warnings.filterwarnings('ignore')
In [3]:
!pwd
/c/!Work/BINP/c-tau/Injector/damping_ring
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())
! beta_x = 12.81770927 alpha_x = -0.00000017 beta_y = 3.11122611 alpha_y = 0.00000003
! eta_x = -0.00000244 eta_y = -0.00000000 etap_x = 0.00000001 etap_y = -0.00000000

q: charge,total=3.47e-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

MA30: MAXAMP, X_MAX=15.0e-3, Y_MAX=15.0e-3, ELLIPTICAL=1

QFW1: KQUAD, L=0.04858934169278997, K1 = 6.380867488799166
D243: DRIFT, L=0.24294670846394986
WIGHALFPOLEPOS: RBEND, L=0.018221046865203764, ANGLE = 0.00744334
D36: DRIFT, L=0.03644200626959248
WIGPOLENEG: RBEND, L=0.03644234153605016, ANGLE = -0.01488668
WIGPOLEPOS: RBEND, L=0.03644234153605016, ANGLE = 0.01488668
QDW1: KQUAD, L=0.04858934169278997, K1 = -5.327257602543184
D972: DRIFT, L=0.9717868338557994
QDS27: KQUAD, L=0.1700626959247649, K1 = -5.76580494446618
QDS26: KQUAD, L=0.1700626959247649, K1 = 5.6377219271568295
D292: DRIFT, L=0.29153605015673983
QDS25: KQUAD, L=0.1700626959247649, K1 = 1.943702204473911
QDS24: KQUAD, L=0.1700626959247649, K1 = -6.227583201760073
D194: DRIFT, L=0.19435736191222575

DIP1: CSBEND, L=0.31934524153605015, ANGLE = 0.08267349, E1="0.08267349 2 /", E2=0
DIP2: CSBEND, L=0.31934524153605015, ANGLE = 0.08267349, E1=0, E2="0.08267349 2 /"

QDS23: KQUAD, L=0.1700626959247649, K1 = 12.507544803656904
D49: DRIFT, L=0.04858934169278997
SY2: SEXT, L=0.14576802507836992, K2 = 0.002877583106
QDS22: KQUAD, L=0.1700626959247649, K1 = -13.53114362079025
QDS21: KQUAD, L=0.1700626959247649, K1 = 9.292210519225806
D73: DRIFT, L=0.07288401253918496
SX2: KSEXT, L=0.14576802507836992, K2=-0.001201865064
D146: DRIFT, L=0.14576802507836992
SX1: KSEXT, L=0.14576802507836992, K2=+140.0
D78: DRIFT, L=0.07774294670846396
QFA: KQUAD, L=0.13605015673981194, K1 = 10.556848152830382
D155: DRIFT, L=0.15548589341692792
QDA: KQUAD, L=0.13605015673981194, K1 = -8.765279717391106
D102: DRIFT, L=0.10203761755485893
SY1: KSEXT, L=0.14576802507836992, K2=-245.0
QDS11: KQUAD, L=0.1700626959247649, K1 = -2.6480964754119216
QDS12: KQUAD, L=0.1700626959247649, K1 = 12.357911508274118
QDS13: KQUAD, L=0.1700626959247649, K1 = -7.5842513538480745
QDS14: KQUAD, L=0.1700626959247649, K1 = -7.879434676707003
QDS15: KQUAD, L=0.1700626959247649, K1 = 9.04647696121302
QDS16: KQUAD, L=0.1700626959247649, K1 = -0.20539330113631632
D160: DRIFT, L=0.159516670846395
RF: DRIFT, L=4.858934169278997e-6
D0: DRIFT, L=4.615987460815047e-5
D1444: DRIFT, L=1.4438805862068966
QDS17: KQUAD, L=0.1700626959247649, K1 = -4.849284899727069
D1603: DRIFT, L=1.603448275862069
QDS18: KQUAD, L=0.1700626959247649, K1 = 3.46260053525197
D1458: DRIFT, L=1.4576802507836992
QDS19: KQUAD, L=0.1700626959247649, K1 = -3.0509003681938456

WIGGLE: LINE=(WIGPOLENEG,D36,WIGPOLEPOS,D36)

WIGGLER: LINE=(WIGHALFPOLEPOS,D36,9*WIGGLE,WIGPOLENEG,D36,WIGHALFPOLEPOS)
WIGGLER_FODO: LINE=(QFW1,D243,WIGGLER,D243,QDW1,QDW1,D243,WIGGLER,D243,QFW1)

TME_CELL: LINE=(DIP2,D146,SX1,D78,QFA,D155,QDA,D102,SY1,D102,QDA,D155,QFA,D78,SX1,D146,DIP1)

machine: LINE=(q,w0,MA30,2*WIGGLER_FODO,QFW1,&
D972,QDS27,D972,QDS26,D292,QDS25,D292,QDS24,D194,&
DIP1,DIP2,D243,QDS23,D49,SY2,D49,QDS22,D292,QDS21,D73,SX2,D73,DIP1,&
16*TME_CELL,&
DIP2,D73,SX2,D73,QDS11,D292,QDS12,D49,SY2,D49,QDS13,D243,DIP1,DIP2,&
D194,QDS14,D292,QDS15,D292,QDS16,D160,RF,D0,D1444,QDS17,D1603,QDS18,D1458,QDS19,D1458,&
w1,QFW1,2*WIGGLER_FODO,QFW1,&
D972,QDS27,D972,QDS26,D292,QDS25,D292,QDS24,D194,&
DIP1,DIP2,D243,QDS23,D49,SY2,D49,QDS22,D292,QDS21,D73,SX2,D73,DIP1,&
16*TME_CELL,&
DIP2,D73,SX2,D73,QDS11,D292,QDS12,D49,SY2,D49,QDS13,D243,DIP1,DIP2,&
D194,QDS14,D292,QDS15,D292,QDS16,D1603,QDS17,D1603,QDS18,D1458,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.1
&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 = 1
    beta_x = 5.0
    alpha_x = 0.5
    beta_y = 5.0
    alpha_y = 0.5
    eta_x = 0
    eta_y = 0
    etap_x = 0
    etap_y = 0
    radiation_integrals = 1
&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 Wed Sep 25 01:01:00 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
&divide_elements
    name = *,
    type = {NULL},
    exclude = {NULL},
    divisions = 0,
    maximum_length = 1.000000000000000e-01,
    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
computed value for DIP1.E1 is 4.133674500000000e-02
computed value for DIP2.E2 is 4.133674500000000e-02
length of beamline MACHINE per pass: 1.319443230107195e+02 m
statistics:    ET:     00:00:00 CP:    0.02 BIO:0 DIO:0 PF:0 MEM:0
&twiss_output
    filename = results/twiss.twi,
    matched = 1,
    output_at_each_step = 0,
    output_before_tune_correction = 0,
    final_values_only = 0,
    statistics = 0,
    radiation_integrals = 1,
    beta_x = 5.000000000000000e+00,
    alpha_x = 5.000000000000000e-01,
    eta_x = 0.000000000000000e+00,
    etap_x = 0.000000000000000e+00,
    beta_y = 5.000000000000000e+00,
    alpha_y = 5.000000000000000e-01,
    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
1461 matrices recomputed for periodic Twiss parameter computation
statistics:    ET:     00:00:00 CP:    0.07 BIO:0 DIO:0 PF:0 MEM:0
periodic 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.228034e+00 -1.342539e-06  1.321203e+01 -1.246039e-06 -8.351295e-10  5.153120e+00 -3.035110e+01  1.743496e+01
  y:  1.511723e+00 -8.805023e-07  9.061203e+00  0.000000e+00  0.000000e+00  5.476313e+00 -4.945983e+01  1.802140e+01
x acceptance limited to 1.743496e-05 by QDS15 ending at 1.248017e+02 m
y acceptance limited to 1.802140e-05 by QDS17 ending at 1.269518e+02 m
statistics:    ET:     00:00:01 CP:    0.50 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:01 CP:    0.51 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:01 CP:    0.51 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:01 CP:    0.51 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
25 Sep 19 01:01:01: This step establishes energy profile vs s (fiducial beam).
25 Sep 19 01:01:01: Rf phases/references reset.
1 particles transmitted, total effort of 1 particle-turns
3072 multipole kicks done

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


Finished tracking.
End of input data encountered.
statistics:    ET:     00:00:01 CP:    0.55 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',
 'resdiag.sdds',
 'track.cen',
 'track.sig',
 'twiss.twi',
 'w0.sdds',
 'w1.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
6166 QFW1 131.9443 0.0
6167 W3 131.9443 0.0
6168 W3 131.9443 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]:
twi = "results/twiss.twi"
In [21]:
#!sddsquery $twi
In [22]:
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 [23]:
df.tail(3)
Out[23]:
ElementName s betax betay alphax alphay etax etay p xAperture yAperture
1461 D1458 131.895734 6.135057 1.536181 -1.903896 5.058840e-01 -0.000001 0.0 2935.426143 0.015 0.015
1462 QFW1 131.944323 6.228034 1.511723 -0.000001 -8.805023e-07 -0.000001 0.0 2935.426143 0.015 0.015
1463 W3 131.944323 6.228034 1.511723 -0.000001 -8.805023e-07 -0.000001 0.0 2935.426143 0.015 0.015
In [24]:
mc = 0.511 # MeV/c
In [25]:
df['xAperture'] = 1e3*df['xAperture'] # mm
df['yAperture'] = 1e3*df['yAperture'] # mm
In [26]:
%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,+0.25))

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 [27]:
(beta_x*beta_y + eta_x + mag).cols(1)
Out[27]:

Betatron tune and chromaticity

In [28]:
def sddspar(file_name, par_name):
    s = !sdds2stream $file_name -par=$par_name
    s = s[0]
    try:
        val = float(s)
        return val
    except (ValueError, TypeError):
        return s
In [29]:
nux=sddspar(twi,'nux')
nuy=sddspar(twi,'nuy')

Latex(r'$\nu_x=%.3f,\ \nu_y=%.3f$' % (nux, nuy))
Out[29]:
$\nu_x=13.212,\ \nu_y=9.061$
In [30]:
diag = 'results/resdiag.sdds'
nux_int = np.floor(nux)
nuy_int = np.floor(nuy)
!sddsresdiag $diag -order=8 -integerTunes=$nux_int,$nuy_int -superperiodicity=1
In [31]:
png = "results/image.png"
!sddsplot -device=lpng -output=$png -split=page \
    -col=nux,nuy -graph=line,type=15 -filter=par,Order,5,5 $diag \
    -par=nux,nuy -graph=sym,type=1,subtype=1,scale=3,thick=3 $twi
Image(png)
Warning: not all x quantities have the same units
Warning: not all y quantities have the same units
Out[31]:

Chromaticity:

In [32]:
Latex(r'$\xi_x=%.1f,\ \xi_y=%.1f$' % (sddspar(twi,'dnux/dp'), sddspar(twi,'dnuy/dp')))
Out[32]:
$\xi_x=5.2,\ \xi_y=5.5$

Radiation damping parameters

In [33]:
!sddsprintout results/twiss.twi \
    -par=taudelta -par=taux -par=tauy -par=U0 -par=enx0 -par=Sdelta0
Printout for SDDS file results/twiss.twi

taudelta (s) =           3.548133e-03  taux (s) =               7.185875e-03  tauy (s) =               7.125886e-03
U0 (MeV) =               1.852902e-01  enx0 (m$be$nc $gp$rm) =  1.768015e-05  Sdelta0 =                7.195060e-04

Beam envelope:

In [34]:
#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 [35]:
e_nx = 4537.6 # normalized emittance [mm*mrad]
e_ny = 3512.5 # 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.01
In [36]:
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[36]:
ElementName s betax betay alphax alphay etax etay p xAperture yAperture e_x e_y dp/p sigma_x sigma_y
1461 D1458 131.895734 6.135057 1.536181 -1.903896 5.058840e-01 -0.000001 0.0 2935.426143 15.0 15.0 7.899070e-07 6.114572e-07 0.01 2.201392 0.969180
1462 QFW1 131.944323 6.228034 1.511723 -0.000001 -8.805023e-07 -0.000001 0.0 2935.426143 15.0 15.0 7.899070e-07 6.114572e-07 0.01 2.218010 0.961433
1463 W3 131.944323 6.228034 1.511723 -0.000001 -8.805023e-07 -0.000001 0.0 2935.426143 15.0 15.0 7.899070e-07 6.114572e-07 0.01 2.218010 0.961433
In [37]:
%opts Curve.Aper (color='gray', alpha=0.5, line_width=3)

dim_sigma = hv.Dimension('sigma', unit='mm', label="2σ", range=(0,16))
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 [38]:
#(Sx*Sy*Ax + mag).cols(1)
In [39]:
(Sx*Sy + mag).cols(1)
Out[39]:
In [40]:
!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 [41]:
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 [42]:
df_cen.tail(3)
Out[42]:
ElementName s Cx Cy p
1461 D1458 131.895734 -7.205575e-12 0.0 2935.426143
1462 QFW1 131.944323 -7.160631e-12 0.0 2935.426143
1463 W3 131.944323 -7.160631e-12 0.0 2935.426143
In [43]:
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 [44]:
dim_x = hv.Dimension('x', unit='mm', label="x", range=(-16,+16))
dim_y = hv.Dimension('y', unit='mm', label="y", range=(-16,+16))
dim_p = hv.Dimension('p', unit='MeV/c', label="p", range=(0,None))
In [45]:
#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 [46]:
(Ax_pos*Ax_neg*Cy*Cx + mag).cols(1)
Out[46]:

2D Layout

Tunnel walls:

In [47]:
df_wall = pd.read_csv('../../drawings_and_maps/results/tunnel.csv')
In [48]:
%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 [49]:
dim_X = hv.Dimension('X', label='X', unit='m') #,  range=(z_min,z_max))
dim_Z = hv.Dimension('Z', label='Z', unit='m', range=(-32,+32)) #,  range=(z_min,z_max))

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

Reading xyz.sdds file

In [51]:
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 [52]:
df_xyz['X'] = df_xyz['X'] - 12.5
df_xyz['Z'] = df_xyz['Z'] - 1.85
In [53]:
df_xyz.tail(3)
Out[53]:
ElementName s X Y Z theta phi psi
1461 D1458 131.895734 -12.5 0.0 -1.898590 6.717959e-08 0.0 0.0
1462 QFW1 131.944323 -12.5 0.0 -1.850001 6.717959e-08 0.0 0.0
1463 W3 131.944323 -12.5 0.0 -1.850001 6.717959e-08 0.0 0.0
In [54]:
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 [55]:
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 [56]:
df_mag.tail(6)
Out[56]:
ElementName s Profile X Z
6163 QFW1 131.9443 1.0 -12.0 -1.850024
6164 QFW1 131.9443 0.0 -12.5 -1.850024
6165 QFW1 131.8957 0.0 -12.5 -1.898624
6166 QFW1 131.9443 0.0 -12.5 -1.850024
6167 W3 131.9443 0.0 -12.5 -1.850024
6168 W3 131.9443 0.0 -12.5 -1.850024
In [57]:
#XY_traj = hv.Curve((df_xyz.Z,df_xyz.X), kdims=dim_Z, vdims=dim_X)
#XY_traj*Tunnel
In [58]:
XY_traj = hv.Curve((df_mag.Z,df_mag.X), kdims=dim_Z, vdims=dim_X)
XY_traj*Tunnel
Out[58]:
In [59]:
%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[59]:

Technical info

In [60]:
%load_ext watermark
In [61]:
%watermark --python --date --iversions --machine
numpy     1.16.4
holoviews 1.12.5
pandas    0.25.1
scipy     1.3.1
2019-09-25 

CPython 3.6.7
IPython 7.8.0

compiler   : MSC v.1900 64 bit (AMD64)
system     : Windows
release    : 10
machine    : AMD64
processor  : Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
CPU cores  : 4
interpreter: 64bit
In [ ]: