Example-22: Line serialization and deserialization (YAML)

[1]:
# In this example lattice serialization and deserialization is demonstrated
# All elements have serialize property, for all elements but lines, it returns a dictionary that can be used to construct the element
# Element(**Element.serialize)
# Line serialize property also returns a (nested) dictionary with element parameters and kinds
# This dictionary can't be used to construct the original line with constructor
# Instead, model.command.build.load_line can be used for construction (deserialization)
# This function loads and processes YAML fine (can be created with model.command.build.save_line)
[2]:
# Import

import torch

from pathlib import Path
from pprint import pprint

from model.library.drift import Drift
from model.library.quadrupole import Quadrupole
from model.library.sextupole import Sextupole
from model.library.dipole import Dipole
from model.library.bpm import BPM
from model.library.line import Line

from model.command.external import load_lattice
from model.command.build import build
from model.command.build import save_line
from model.command.build import load_line
[3]:
# Define simple FODO based lattice

QF = Quadrupole('QF', 0.5, +0.20)
QD = Quadrupole('QD', 0.5, -0.19)
SF = Sextupole('SF', 0.25)
SD = Sextupole('SD', 0.25)
DR = Drift('DR', 0.25)
BM = Dipole('BM', 3.50, torch.pi/4.0)
BA = BPM('BA', direction='inverse')
BB = BPM('BB', direction='forward')

FODO = Line('FODO',
            [BA, QF, DR, SF, DR, BM, DR, SD, DR, QD, QD, DR, SD, DR, BM, DR, SF, DR, QF, BB],
            propagate=True,
            dp=0.0,
            exact=False,
            output=True,
            matrix = True)
[4]:
# Element serialization and deserialization

pprint(DR.serialize, sort_dicts=False)
print()

print(Drift(**DR.serialize))
print()
{'name': 'DR',
 'length': 0.25,
 'dp': 0.0,
 'dx': 0.0,
 'dy': 0.0,
 'dz': 0.0,
 'wx': 0.0,
 'wy': 0.0,
 'wz': 0.0,
 'ns': 1,
 'order': 0,
 'exact': False,
 'insertion': False,
 'output': True,
 'matrix': True}

Drift(name="DR", length=0.25, dp=0.0, exact=False, ns=1, order=0)

[5]:
# Line serialization

pprint(FODO.serialize, sort_dicts=False)
{'kind': 'Line',
 'name': 'FODO',
 'sequence': [{'kind': 'BPM',
               'name': 'BA',
               'dp': 0.0,
               'dx': 0.0,
               'dy': 0.0,
               'dz': 0.0,
               'wx': 0.0,
               'wy': 0.0,
               'wz': 0.0,
               'output': True,
               'matrix': True,
               'direction': 'inverse'},
              {'kind': 'Quadrupole',
               'name': 'QF',
               'length': 0.5,
               'dp': 0.0,
               'dx': 0.0,
               'dy': 0.0,
               'dz': 0.0,
               'wx': 0.0,
               'wy': 0.0,
               'wz': 0.0,
               'ns': 1,
               'order': 0,
               'exact': False,
               'insertion': False,
               'output': True,
               'matrix': True,
               'kn': 0.200000000000001,
               'ks': 0.0},
              {'kind': 'Drift',
               'name': 'DR',
               'length': 0.25,
               'dp': 0.0,
               'dx': 0.0,
               'dy': 0.0,
               'dz': 0.0,
               'wx': 0.0,
               'wy': 0.0,
               'wz': 0.0,
               'ns': 1,
               'order': 0,
               'exact': False,
               'insertion': False,
               'output': True,
               'matrix': True},
              {'kind': 'Sextupole',
               'name': 'SF',
               'length': 0.25,
               'dp': 0.0,
               'dx': 0.0,
               'dy': 0.0,
               'dz': 0.0,
               'wx': 0.0,
               'wy': 0.0,
               'wz': 0.0,
               'ns': 1,
               'order': 0,
               'exact': False,
               'insertion': False,
               'output': True,
               'matrix': True,
               'ms': 0.0},
              {'kind': 'Drift',
               'name': 'DR',
               'length': 0.25,
               'dp': 0.0,
               'dx': 0.0,
               'dy': 0.0,
               'dz': 0.0,
               'wx': 0.0,
               'wy': 0.0,
               'wz': 0.0,
               'ns': 1,
               'order': 0,
               'exact': False,
               'insertion': False,
               'output': True,
               'matrix': True},
              {'kind': 'Dipole',
               'name': 'BM',
               'length': 3.5,
               'dp': 0.0,
               'dx': 0.0,
               'dy': 0.0,
               'dz': 0.0,
               'wx': 0.0,
               'wy': 0.0,
               'wz': 0.0,
               'ns': 1,
               'order': 0,
               'exact': False,
               'insertion': False,
               'output': True,
               'matrix': True,
               'angle': 0.7853981633974493,
               'e1': 0.0,
               'e2': 0.0,
               'kn': 1e-15,
               'ks': 0.0,
               'ms': 0.0,
               'mo': 0.0,
               'e1_on': True,
               'e2_on': True},
              {'kind': 'Drift',
               'name': 'DR',
               'length': 0.25,
               'dp': 0.0,
               'dx': 0.0,
               'dy': 0.0,
               'dz': 0.0,
               'wx': 0.0,
               'wy': 0.0,
               'wz': 0.0,
               'ns': 1,
               'order': 0,
               'exact': False,
               'insertion': False,
               'output': True,
               'matrix': True},
              {'kind': 'Sextupole',
               'name': 'SD',
               'length': 0.25,
               'dp': 0.0,
               'dx': 0.0,
               'dy': 0.0,
               'dz': 0.0,
               'wx': 0.0,
               'wy': 0.0,
               'wz': 0.0,
               'ns': 1,
               'order': 0,
               'exact': False,
               'insertion': False,
               'output': True,
               'matrix': True,
               'ms': 0.0},
              {'kind': 'Drift',
               'name': 'DR',
               'length': 0.25,
               'dp': 0.0,
               'dx': 0.0,
               'dy': 0.0,
               'dz': 0.0,
               'wx': 0.0,
               'wy': 0.0,
               'wz': 0.0,
               'ns': 1,
               'order': 0,
               'exact': False,
               'insertion': False,
               'output': True,
               'matrix': True},
              {'kind': 'Quadrupole',
               'name': 'QD',
               'length': 0.5,
               'dp': 0.0,
               'dx': 0.0,
               'dy': 0.0,
               'dz': 0.0,
               'wx': 0.0,
               'wy': 0.0,
               'wz': 0.0,
               'ns': 1,
               'order': 0,
               'exact': False,
               'insertion': False,
               'output': True,
               'matrix': True,
               'kn': -0.189999999999999,
               'ks': 0.0},
              {'kind': 'Quadrupole',
               'name': 'QD',
               'length': 0.5,
               'dp': 0.0,
               'dx': 0.0,
               'dy': 0.0,
               'dz': 0.0,
               'wx': 0.0,
               'wy': 0.0,
               'wz': 0.0,
               'ns': 1,
               'order': 0,
               'exact': False,
               'insertion': False,
               'output': True,
               'matrix': True,
               'kn': -0.189999999999999,
               'ks': 0.0},
              {'kind': 'Drift',
               'name': 'DR',
               'length': 0.25,
               'dp': 0.0,
               'dx': 0.0,
               'dy': 0.0,
               'dz': 0.0,
               'wx': 0.0,
               'wy': 0.0,
               'wz': 0.0,
               'ns': 1,
               'order': 0,
               'exact': False,
               'insertion': False,
               'output': True,
               'matrix': True},
              {'kind': 'Sextupole',
               'name': 'SD',
               'length': 0.25,
               'dp': 0.0,
               'dx': 0.0,
               'dy': 0.0,
               'dz': 0.0,
               'wx': 0.0,
               'wy': 0.0,
               'wz': 0.0,
               'ns': 1,
               'order': 0,
               'exact': False,
               'insertion': False,
               'output': True,
               'matrix': True,
               'ms': 0.0},
              {'kind': 'Drift',
               'name': 'DR',
               'length': 0.25,
               'dp': 0.0,
               'dx': 0.0,
               'dy': 0.0,
               'dz': 0.0,
               'wx': 0.0,
               'wy': 0.0,
               'wz': 0.0,
               'ns': 1,
               'order': 0,
               'exact': False,
               'insertion': False,
               'output': True,
               'matrix': True},
              {'kind': 'Dipole',
               'name': 'BM',
               'length': 3.5,
               'dp': 0.0,
               'dx': 0.0,
               'dy': 0.0,
               'dz': 0.0,
               'wx': 0.0,
               'wy': 0.0,
               'wz': 0.0,
               'ns': 1,
               'order': 0,
               'exact': False,
               'insertion': False,
               'output': True,
               'matrix': True,
               'angle': 0.7853981633974493,
               'e1': 0.0,
               'e2': 0.0,
               'kn': 1e-15,
               'ks': 0.0,
               'ms': 0.0,
               'mo': 0.0,
               'e1_on': True,
               'e2_on': True},
              {'kind': 'Drift',
               'name': 'DR',
               'length': 0.25,
               'dp': 0.0,
               'dx': 0.0,
               'dy': 0.0,
               'dz': 0.0,
               'wx': 0.0,
               'wy': 0.0,
               'wz': 0.0,
               'ns': 1,
               'order': 0,
               'exact': False,
               'insertion': False,
               'output': True,
               'matrix': True},
              {'kind': 'Sextupole',
               'name': 'SF',
               'length': 0.25,
               'dp': 0.0,
               'dx': 0.0,
               'dy': 0.0,
               'dz': 0.0,
               'wx': 0.0,
               'wy': 0.0,
               'wz': 0.0,
               'ns': 1,
               'order': 0,
               'exact': False,
               'insertion': False,
               'output': True,
               'matrix': True,
               'ms': 0.0},
              {'kind': 'Drift',
               'name': 'DR',
               'length': 0.25,
               'dp': 0.0,
               'dx': 0.0,
               'dy': 0.0,
               'dz': 0.0,
               'wx': 0.0,
               'wy': 0.0,
               'wz': 0.0,
               'ns': 1,
               'order': 0,
               'exact': False,
               'insertion': False,
               'output': True,
               'matrix': True},
              {'kind': 'Quadrupole',
               'name': 'QF',
               'length': 0.5,
               'dp': 0.0,
               'dx': 0.0,
               'dy': 0.0,
               'dz': 0.0,
               'wx': 0.0,
               'wy': 0.0,
               'wz': 0.0,
               'ns': 1,
               'order': 0,
               'exact': False,
               'insertion': False,
               'output': True,
               'matrix': True,
               'kn': 0.200000000000001,
               'ks': 0.0},
              {'kind': 'BPM',
               'name': 'BB',
               'dp': 0.0,
               'dx': 0.0,
               'dy': 0.0,
               'dz': 0.0,
               'wx': 0.0,
               'wy': 0.0,
               'wz': 0.0,
               'output': True,
               'matrix': True,
               'direction': 'forward'}],
 'propagate': True,
 'dp': 0.0,
 'exact': False,
 'output': True,
 'matrix': True}
[6]:
# Build lattice from ELEGANT

path = Path('ic.lte')
data = load_lattice(path)
ring = build('RING', 'ELEGANT', data)

print(ring.length)
print(ring.describe)
tensor(27.4256, dtype=torch.float64)
{'BPM': 16, 'Drift': 42, 'Quadrupole': 28, 'Dipole': 8, 'Sextupole': 16}
[7]:
# Save lattice

path = Path('ring.yaml')
save_line(ring, path)
[8]:
# Load lattice

path = Path('ring.yaml')
ring = load_line(path)

print(ring.length)
print(ring.describe)
tensor(27.4256, dtype=torch.float64)
{'BPM': 16, 'Drift': 42, 'Quadrupole': 28, 'Dipole': 8, 'Sextupole': 16}