SixTrack / pysixtrack

Reference sixtrack tracking engine in pure python
Apache License 2.0
2 stars 4 forks source link

apply_madx_errors() throws AttributeError if misaligned collimators are present #34

Closed roman-martin closed 4 years ago

roman-martin commented 4 years ago

Example:

import cpymad.madx
import pysixtrack

# code from help(pysixtrack.Line.apply_madx_errors) with minimal madx sequence
madx = cpymad.madx.Madx()
madx.input('''
    !----EXAMPLE----------------------------------------------------------------
    MQ1: Quadrupole, K1:=KQ1, L=1.0;
    MQ2: Quadrupole, K1:=KQ2, L=1.0;
    TSC: Collimator, L=1.0, apertype=circle, aperture=0.2;

    KQ1 = 0.02;
    KQ2 = -0.02;

    testseq: SEQUENCE, l = 20.0;
        MQ1, at = 5;
        TSC, at=10;
        MQ2, at=18;
    ENDSEQUENCE;

    !---the usual stuff
    BEAM, PARTICLE=PROTON, ENERGY=7000.0, EXN=2.2e-6, EYN=2.2e-6;
    USE, SEQUENCE=testseq;

    Select, flag=makethin, class=Collimator, slice=2;
    makethin, sequence=testseq, style=collim;

    use, sequence=testseq;

    !---misalign collimator
    select, flag = error, clear;
    select, flag = error, pattern = "TSC";
    ealign, arex :=  0.05, arey :=0.02;

    !---/EXAMPLE----------------------------------------------------------------
''')
seq = madx.sequence.testseq
# store already applied errors:
madx.command.esave(file='lattice_errors.err')
madx.command.readtable(file='lattice_errors.err', table="errors")
errors = madx.table.errors
pysixtrack_line = pysixtrack.Line.from_madx_sequence(seq)
pysixtrack_line.apply_madx_errors(errors)

results in

AttributeError: 'Drift' object has no attribute 'knl'

The problem is that all errors are in the same madx.esave output file and although knl are all zero, apply_madx_errors() tries to apply them to the collimator marker. An easy fix would be to only call add_multipole_error_to() if any non-zero knl or ksl are present in the list.

aoeftiger commented 4 years ago

I wanted to re-write the apply_madx_errors method anyway to avoid using the detour via esave and readtable.. I'll work on that and include your check :+1:

aoeftiger commented 4 years ago

Fixed by https://github.com/SixTrack/pysixtrack/pull/36 https://github.com/SixTrack/pysixtrack/pull/37 .

However, PySixTrack only supports dx, dy and multipole errors so far. Try to use dx instead of arex (PySixTrack also gives you a warning on that).