desy-ml / cheetah

Fast and differentiable particle accelerator optics simulation for reinforcement learning and optimisation applications.
https://cheetah-accelerator.readthedocs.io
GNU General Public License v3.0
25 stars 12 forks source link

MAD-X integration #197

Open ansantam opened 5 days ago

ansantam commented 5 days ago

Description

New functionality: be able to directly load MAD-X files in Cheetah

Ways to do this

There are many ways of using MAD-X. If the lattice is small enough, everything can be defined directly within the MAD-X script .madx.

Other people have distinct .beam, .ele, .str, and .seq files, but not every user defines all of those. It is common that people only have a .seq and .madx file.

A way of circumventing this is by loading information from the TFS files produced by the twiss command in MAD-X, but it requires that the user has made sure to print out all the relevant attributes of each element (check here).

With direct TFS Twiss output

Loading with pyoptics

The TFS file needs to be loaded somehow. I am now using the tools from pyoptics, but it might not be a stable solution depending on the maintenance of that repository. Right now I get an error when using python 3.12 due to the deprecated imp module (substituted by importlib in 3.12) which limits the current version to 3.11.

Loading with cpymad

One can also use the cpymad functionalities like this:

from cpymad.madx import Madx

madx = Madx()
madx.call(file='general_tt43.madx')
twiss = madx.table.twiss
print(twiss.name, twiss.s, twiss.l)

With Xsuite objects or JSON output

One could load a line object from Xsuite that was saved as JSON, run the Twiss and get the values from there.

Nice feature to have, but since the majority of users across laboratories have old MAD-X script, the most useful functionality now would be to load from TFS.

Current status

Future steps

Motivation and Context

To be able to use Cheetah directly with MAD-X lattices, and start loading circular accelerators and see if problems arise with very large lattices.

Types of changes

Checklist

Note: We are using a maximum length of 88 characters per line