SixTrack / sixtracklib

Library for single charged particle simulations in accelerators
GNU Lesser General Public License v2.1
12 stars 16 forks source link

PySixTrackLib: Missing ksl #54

Closed aoeftiger closed 5 years ago

aoeftiger commented 5 years ago

For PySixTrackLib's madx_helper.dispatch function, if the passed element el is missing the skew values ksl, the dispatcher could instead provide an empty ksl.

This is the case e.g. for cpymad generated thin lattices (mad.command.makethin):

from cpymad.madx import Madx
import pysixtracklib as pyst

madx = Madx()
madx.options.echo = False

madx.call(file="fodo.lat")
madx.command.beam(particle='proton', energy='6')
madx.use(sequence="FODO")
madx.twiss()

assert madx.command.select(
    flag='MAKETHIN',
    CLASS='QUADRUPOLE',
    SLICE='8',
)

assert madx.command.select(
    flag='MAKETHIN',
    CLASS='SBEND',
    SLICE='8',
)

madx.command.makethin(
    makedipedge=False,
    style='simple',
    sequence='FODO',
)

madx.twiss()

fodo = madx.sequence.FODO

elements = pyst.Elements.from_mad(sis18)

which generates

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-48-a65d6f758630> in <module>
----> 1 elements = pyst.Elements.from_mad(fodo)

~/git/sixtracklib/python/pysixtracklib/beam_elements.py in from_mad(cls, seq)
    291     def from_mad(cls, seq):
    292         line = madseq_to_line(seq)
--> 293         return cls.fromline(line)
    294 
    295     # @classmethod

~/git/sixtracklib/python/pysixtracklib/beam_elements.py in fromline(cls, line)
    258     def fromline(cls, line):
    259         self = cls()
--> 260         for label, element_name, element in line:
    261             getattr(self, element_name)(**element._asdict())
    262         return self

~/git/sixtracklib/python/pysixtracklib/mad_helper.py in madseq_to_line(seq, convert)
     51         if length > 0:
     52             yield "drift", "Drift", Drift(length)
---> 53         el = dispatch(el, convert)
     54         oldpos = pos
     55         yield name, el.__class__.__name__, el

~/git/sixtracklib/python/pysixtracklib/mad_helper.py in dispatch(el, classes)
     21         el = Multipole(
     22             knl=el.knl,
---> 23             ksl=el.ksl,
     24             hxl=el.knl[0],
     25             hyl=0,

~/anaconda3/lib/python3.7/site-packages/cpymad/madx.py in __getattr__(self, name)
    742         except KeyError:
    743             pass
--> 744         return _Mapping.__getattr__(self, name)
    745 
    746     def __getitem__(self, name):

~/anaconda3/lib/python3.7/site-packages/cpymad/madx.py in __getattr__(self, key)
    499         except KeyError:
    500             pass
--> 501         return self._missing(key)
    502 
    503     def _missing(self, key):

~/anaconda3/lib/python3.7/site-packages/cpymad/madx.py in _missing(self, key)
    788     def _missing(self, key):
    789         raise AttributeError('Unknown attribute {!r} for {!r} command!'
--> 790                              .format(key, self.name))
    791 
    792     @property

AttributeError: Unknown attribute 'ksl' for 'rb..1' command!

PS: the lattice in fodo.lat is this one:

! Element definitions:
! ------------------- horizontal quadrupole ----------------------------
QS1F: QUADRUPOLE, L=1.04, K1=0.282632;
QS2D: QUADRUPOLE, L=1.04, K1=-0.492;
QS3T: QUADRUPOLE, L = 0.4804, K1 = 0.656;
!---------------------  DRIFT SPACES DEFINITION  ---------------------
DR1: DRIFT, L=0.6450000;
DR2: DRIFT, L=0.9700000;
DR3: DRIFT, L=6.8390117;
DR4: DRIFT, L=0.6000000;
DR5: DRIFT, L=0.7098000;
DR6: DRIFT, L=0.4998000;
!--------------------------  BENDING MAGNET ---------------------------
ALPHA  := 15 * 1/57.2958;
LL  := 150 * 1/57.2958;
RB: SBEND, L=2.6175, ANGLE=ALPHA;

! ---------------------------  LINE DEFINITION --------------------------
! Sections without injection devices, steeres and position monitors
CELLA: LINE=(DR1, RB, DR2, RB, DR3, QS1F, DR4, QS2D, DR5, QS3T, DR6);

FODO: LINE = (12*CELLA);

RETURN;
aoeftiger commented 5 years ago

https://github.com/SixTrack/sixtracklib/pull/55 attempts to fix this

rdemaria commented 5 years ago

Addressed in #56. The new elements created by makethin are not compliant with the madx memory layout, therefore cpymadx fail to find attributes. The fix is to protect against this happening, although the makethin problem will be addressed in future madx releases.

aoeftiger commented 5 years ago

56 works and fixes this issue -- thanks :)