Example-21: Build MADX and ELEGANT style lattice

[1]:
# In this example it is demonstraited how to load MADX and ELEGANT lattice files

# Supported elements (other elemet types will be casted to drifts)

# Drift
# Quadrupole
# Sextupole
# Octupole
# Dipole
# BPM
# Marker
# Line

# Note, only basic options are translated (not translated options are ignored)
# Note, reference orbit slicing is not correct with negative bending
[2]:
from pathlib import Path

from model.command.external import load_lattice
from model.command.build import build
from model.command.layout import Layout

from plotly import graph_objects
[3]:
# Load and build simple MADX lattice

# Set lattice file path

file = Path('../../../config/initial.madx')

# Load lattice to dictionary

table = load_lattice(file)

# Build lattice

FODO = build('FODO', 'MADX', table)
print(FODO)
Marker(name="HEAD")
BPM(name="M", direction=forward)
Quadrupole(name="QD", length=0.5, kn=-0.199999999999999, ks=0.0, dp=0.0, exact=False, ns=1, order=0)
Drift(name="DR", length=2.0, dp=0.0, exact=False, ns=1, order=0)
Dipole(name="BM", length=1.0, angle=0.17453292519943395, e1=0.0, e2=0.0, kn=1e-15, ks=0.0, ms=0.0, mo=0.0, dp=0.0, exact=False, ns=1, order=0)
Drift(name="DR", length=2.0, dp=0.0, exact=False, ns=1, order=0)
Quadrupole(name="QF", length=1.0, kn=0.200000000000001, ks=0.0, dp=0.0, exact=False, ns=1, order=0)
Drift(name="DR", length=2.0, dp=0.0, exact=False, ns=1, order=0)
Dipole(name="BM", length=1.0, angle=0.17453292519943395, e1=0.0, e2=0.0, kn=1e-15, ks=0.0, ms=0.0, mo=0.0, dp=0.0, exact=False, ns=1, order=0)
Drift(name="DR", length=2.0, dp=0.0, exact=False, ns=1, order=0)
Quadrupole(name="QD", length=0.5, kn=-0.199999999999999, ks=0.0, dp=0.0, exact=False, ns=1, order=0)
Marker(name="TAIL")
[4]:
# Load and build simple ELEGANT lattice

# Set lattice file path

file = Path('../../../config/initial.lte')

# Load lattice to dictionary

table = load_lattice(file)

# Build lattice

FODO = build('FODO', 'ELEGANT', table)
print(FODO)
Marker(name="HEAD")
BPM(name="M", direction=forward)
Quadrupole(name="QD", length=0.5, kn=-0.199999999999999, ks=0.0, dp=0.0, exact=False, ns=1, order=0)
Drift(name="DR", length=2, dp=0.0, exact=False, ns=1, order=0)
Dipole(name="BM", length=1.0, angle=0.17453292519943395, e1=0.0, e2=0.0, kn=1e-15, ks=0.0, ms=0.0, mo=0.0, dp=0.0, exact=False, ns=1, order=0)
Drift(name="DR", length=2, dp=0.0, exact=False, ns=1, order=0)
Quadrupole(name="QF", length=1.0, kn=0.200000000000001, ks=0.0, dp=0.0, exact=False, ns=1, order=0)
Drift(name="DR", length=2, dp=0.0, exact=False, ns=1, order=0)
Dipole(name="BM", length=1.0, angle=0.17453292519943395, e1=0.0, e2=0.0, kn=1e-15, ks=0.0, ms=0.0, mo=0.0, dp=0.0, exact=False, ns=1, order=0)
Drift(name="DR", length=2, dp=0.0, exact=False, ns=1, order=0)
Quadrupole(name="QD", length=0.5, kn=-0.199999999999999, ks=0.0, dp=0.0, exact=False, ns=1, order=0)
Marker(name="TAIL")
[5]:
# Build vepp4 lattice

file = Path('../../../config/vepp4m.lte')
data = load_lattice(file)
vepp = build('VEPP4M', 'ELEGANT', data)

# Slice dipoles

vepp.ns = (('Dipole', 0.1), )

# Profile

layout = Layout(vepp)

# Generate reference orbit

x, y, z = layout.orbit(flat=False, step=None, start=(0, 0))

# Generate layout (can be saved as html with write_html method)

blocks = layout.profile_3d(scale=2.5, exclude=['Drift', 'Marker'])

# # Plot

# figure = graph_objects.Figure(
#     data=[
#         graph_objects.Scatter3d(
#             x=x.numpy(),
#             y=y.numpy(),
#             z=z.numpy(),
#             mode='lines',
#             name='Orbit',
#             line=dict(color='black',width=2.0,dash='solid'),
#             opacity=0.75,
#             showlegend=True
#         ),
#         *[graph_objects.Mesh3d(block) for block in blocks]
#     ]
# )
# figure.update_layout(
#     scene=dict(
#         xaxis=dict(visible=False, range=[-100,100]),
#         yaxis=dict(visible=False, range=[-100,100]),
#         zaxis=dict(visible=False, range=[-25,25]),
#         aspectratio=dict(x=1, y=1, z=1/4),
#         annotations=[]
#     ),
#     margin=dict(l=0, r=0, t=0, b=0),
#     legend=dict(orientation='v', x=0., y=1., xanchor='left', yanchor='top'),
#     hoverlabel=dict(font_size=12, font_family="Rockwell", font_color='white'),
#     legend_groupclick='toggleitem'
# )
# figure.show()
[6]:
# Build skif lattice

file = Path('../../../config/skif.lte')
data = load_lattice(file)
skif = build('SKIF', 'ELEGANT', data)

# Slice dipoles

skif.ns = (('Dipole', 0.1), )

# Profile

layout = Layout(skif)

# Generate reference orbit

x, y, z = layout.orbit(flat=False, step=None, start=(0, 0))

# Generate layout (can be saved as html with write_html method)

blocks = layout.profile_3d(scale=2.5, exclude=['Drift', 'Marker'])

# # Plot

# figure = graph_objects.Figure(
#     data=[
#         graph_objects.Scatter3d(
#             x=x.numpy(),
#             y=y.numpy(),
#             z=z.numpy(),
#             mode='lines',
#             name='Orbit',
#             line=dict(color='black',width=2.0,dash='solid'),
#             opacity=0.75,
#             showlegend=True
#         ),
#         *[graph_objects.Mesh3d(block) for block in blocks]
#     ]
# )
# figure.update_layout(
#     scene=dict(
#         xaxis=dict(visible=False, range=[-200,200]),
#         yaxis=dict(visible=False, range=[-200,200]),
#         zaxis=dict(visible=False, range=[-50,50]),
#         aspectratio=dict(x=1, y=1, z=1/4),
#         annotations=[]
#     ),
#     margin=dict(l=0, r=0, t=0, b=0),
#     legend=dict(orientation='v', x=0., y=1., xanchor='left', yanchor='top'),
#     hoverlabel=dict(font_size=12, font_family="Rockwell", font_color='white'),
#     legend_groupclick='toggleitem'
# )
# figure.show()