A. Petrenko (Novosibirsk, 2019)
import numpy as np
import pandas as pd
import holoviews as hv
import os
from IPython.display import Image
hv.extension("bokeh")
%opts Curve Spread [width=700 height=300 show_grid=True \
default_tools=['box_zoom','pan','wheel_zoom','reset']]
Elegant lattice file:
w = "../Martyshkin/results/beam.sdds"
#!plaindata2sdds
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)
!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)
with open("machine.lte", "r") as f:
print(f.read())
with open("track.ele", "r") as f:
print(f.read())
Run Elegant
#!elegant twiss.ele
#!elegant track.ele
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()
run_cmd("elegant track.ele")
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)
!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)
w = "results/w1.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)
!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)
w = "results/w2.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)
!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)
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)
!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)
os.listdir("results")
Reading beamline.mag
out = !sdds2stream results/beamline.mag -col=ElementName,s,Profile -pipe=out
out[:3]
print("\n".join(out[:3]))
from io import StringIO
DATA = StringIO("\n".join(out))
df = pd.read_csv(DATA, names=['ElementName', 's', 'Profile'], delim_whitespace=True)
df.head(3)
df.tail(3)
dim_s = hv.Dimension('s', unit='m', label="s") # range=(-80,0))
#hv.help(hv.Curve)
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
Reading track.sig file
#!sddsquery results/track.sig
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)
df.tail(3)
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
#df.tail(3)
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)
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')
(Sx*Sy + mag).cols(1)
Reading track.cen file
#!sddsquery results/track.cen
out = !sdds2stream results/track.cen -col=ElementName,s,Particles,pCentral,Cx,Cy -pipe=out
DATA = StringIO("\n".join(out))
df_cen = pd.read_csv(DATA, names=['ElementName','s','Particles','p','Cx','Cy'],
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
df_cen.tail(3)
dim_Np = hv.Dimension('Np', range=(0,None)) # range=(-80,0))
particles = hv.Curve((df_cen.s, df_cen.Particles), kdims=dim_s, vdims=dim_Np)
(particles + mag).cols(1)
Reading twiss.sdds file
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)
df_twi['xAperture'] = 1e3*df_twi['xAperture'] # mm
df_twi['yAperture'] = 1e3*df_twi['yAperture'] # mm
df_twi.tail(3)
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))
%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')
#Ax_pos*Ax_neg
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])
(s_p + Ax_pos*Ax_neg*Cx*Cy + mag).cols(1)
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')
(s_Cx*s_Cy*Ax_pos*Ax_neg + mag).cols(1)
Reading xyz.sdds file
out = !sdds2stream results/xyz.sdds \
-col=ElementName,s,X,Y,Z -pipe=out
DATA = StringIO("\n".join(out))
df_xyz = pd.read_csv(DATA, names=['ElementName','s','X','Y','Z'],
delim_whitespace=True)
df_xyz.tail(3)
df_cen['Cx'] = df_cen['Cx'] + 1e3*df_xyz['X'] # mm
df_twi['xAperture_neg'] = -df_twi['xAperture'] + 1e3*df_xyz['X'] # mm
df_twi['xAperture'] = df_twi['xAperture'] + 1e3*df_xyz['X'] # mm
dim_x = hv.Dimension('x', unit='mm', label="x", range=(-16,+27))
Cx = hv.Spread((df_cen.s, df_cen.Cx, 2*df.sigma_x), label='x', \
kdims=dim_s, vdims=[dim_x,dim_aper])
Ax = hv.Curve((df_twi.s, df_twi.xAperture), label='x Aperture', kdims=dim_s, vdims=dim_sigma, group='Aper')
Ax_pos = Ax
Ax_neg = hv.Curve((df_twi.s, df_twi.xAperture_neg), label='x Aperture', kdims=dim_s, vdims=dim_sigma, group='Aper')
(s_p + Ax_pos*Ax_neg*Cx*Cy + mag).cols(1)