ocelot-collab / ocelot

OCELOT is a multiphysics simulation toolkit designed for studying FEL and storage ring-based light sources.
GNU General Public License v3.0
86 stars 58 forks source link

Translating MAD lattice to Ocelot #199

Open jonasbjorklundsvensson opened 1 year ago

jonasbjorklundsvensson commented 1 year ago

Hi,

I need to translate a MAD lattice, presumably MAD-X, into Ocelot, but I can't get the built-in converter to work.

I'm trying to use the madx2ocelot() function, where I input the path to a lattice file (if that's what they're called in MAD) as the filename_seq parameter. I then get the error NameError: name 'lattice' is not defined from line 260 in madx.py, pasted below. It looks like the lattice that's being passed to madx_seq2ocelot_seq(), near the bottom of what I've pasted, is never defined.

def madx_seq2ocelot_seq(list_elem_pos, tot_length, exclude_elems = []):
    seq = []
    azimuth = 0.
    for i, term in enumerate(list_elem_pos):
        if term[1] in exclude_elems:
            continue
        element = term[0]
        #print element
        element.id = term[1]
        pos = term[2]
        drift = Drift(l = pos - element.l/2. - azimuth, eid = "drift_" + str(i))
        azimuth = pos + element.l/2.
        seq.append(drift)
        seq.append(element)
        #print elem[0].l, elem[1], elem[2]
    len_last_drift = tot_length - list_elem_pos[-1][-1] - list_elem_pos[-1][0].l/2.
    drift = Drift(l = len_last_drift, eid = "drift_last")
    seq.append(drift)
    return seq

def madx2ocelot(file_seq, exclude_elems):
    lines = lattice_str_from_madx(filename_seq=file_seq)
    #print lines
    file = "\n"
    exec(file.join(lines))
    seq = madx_seq2ocelot_seq(lattice, tot_length = ring.l, exclude_elems=exclude_elems)
    return seq

I guess it's a somewhat niche use case, but I really need to translate this lattice, as other methods we've tried produce a plethora of bugs, requiring lots of manual fixing, and also require the involvement of other people which is usually a bottleneck.

Best regards, Jonas

sergey-tomin commented 7 months ago

hi,

Yes, this is indeed a niche case, and unfortunately, I doubt any of the main developers would dedicate time to translating your specific lattice to Ocelot format. The concept behind Ocelot is that users contribute to its development by creating and sharing useful functions or methods. If you develop something that could benefit other accelerator physicists, for example generalize madx2ocleot adaptor, you can submit a pull request with your contribution.

Cheers, Sergey.

st-walker commented 7 months ago

I wrote tfs adaptor back in 2021 (which is for output from madx). Maybe it will work better for you.

To be honest, translating lattices is generally relatively simple, even if an adaptor does not exist. It is just a one to one mapping of standard components 99% of the time.