Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading of MAD and Elegant files #844

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open

Loading of MAD and Elegant files #844

wants to merge 13 commits into from

Conversation

lfarv
Copy link
Contributor

@lfarv lfarv commented Oct 17, 2024

The processing of MAD files is improved:

1. more robustness for malformed files

2. handling of deferred assignments:

deferred assignments are correctly handled when MAD parameters are interactively modified. Example:
assuming an input files containing:

QF:  QUADRUPOLE,  L=0.5,  K1:=KQF
KQF = 0.75

And the following PyAT commands:

>>> parser = MadxParser()
>>> parser.parse_files(input_file)      # Read the input file and fill the database
>>> parser["KQF"]                       # Look at the initial value of KQF
0.75
>>> ring1 = parser.lattice(use="RING")  # Generate a 1st lattice with KQF == 0.75
>>> parser["KQF"] = 0.8                 # Modify the value of KQF
>>> parser["KQF"]                       # check the modified value of KQF
0.8
>>> ring2 = parser.lattice(use="RING")  # Generate another lattice with KQF == 0.8

One can now interactively modify any parameter, and the updated values will be taken into account when generating lattices.
With this, one can automatise the generation of AT lattices with variable conditions. Note however that this is much slower than using AT Variable objects when it is possible.

In the present state, MAD format may be used as a native input format for PyAT.

@lfarv lfarv added enhancement Python For python AT code labels Oct 17, 2024
@lfarv
Copy link
Contributor Author

lfarv commented Oct 17, 2024

Parsing of Elegant files is also rebuilt and should now be fully compliant with the Elegant reference.

@swhite2401
Copy link
Contributor

@lfarv, what you propose is to have an object parser that contains all the lattice information including the so called deferred variables.
However everytime you change a parameter in this parser object you need to instantiate a new ring so in effect it is the same as changing the MADX input file, is that correct?

@lfarv
Copy link
Contributor Author

lfarv commented Oct 29, 2024

@swhite2401 : you are right, the parser is just an image in memory of the MAD-X file, as it is already the case in the present version. So after modifying a variable, you need indeed to instantiate a new lattice (like you need to call the "USE" command in MAD) to take new values into account. But:

  1. This is much faster than parsing again a new MAD file: the interpretation of the MAD syntax is complicated and is done once for ever,
  2. It's much easier to change programmatically a variable in the parser than to modify a text file.

The new point in this version is that the expansion of deferred variables was previously done while parsing the file, while it is now done when the variable is used (as in MAD). So in the previous version, modifying parameters had no interest.

Note that in simple cases, the load_madx function does everything in one command (parsing and instantiating the lattice). Accessing the parser gives more flexibility, like generating several lattices from different SEQUENCES (already possible in the present version), or modifying variables (now possible with this version), without reading the file again.

@simoneliuzzo
Copy link
Contributor

simoneliuzzo commented Oct 29, 2024

I may test the mad files, but I have no elegant files. May be @carmignani has an elegant file we could use as test?

@simoneliuzzo
Copy link
Contributor

Dear @lfarv,

in this branch it is now possible to save mad and elegant files? This would bring a full omni-directional converter feature to pyAT!

I will try the features added in this branch.

best regards
Simone

@lfarv
Copy link
Contributor Author

lfarv commented Nov 13, 2024

@simoneliuzzo: You noticed the new feature even before I mentioned it ! Yes, you are right, both loading and saving are now available. I still need to document the limitations on saving (limited to elements available on both sides, tilt not yet taken into account…), but I tested on a few files that when loading a MAD (or elegant) file, and then saving it back to MAD, the resulting file is of course completely different from the initial one, but MAD gives identical results.

However, this needs to be tried on many files. I have about 10 MAD test files and ~5 elegant files. Every test you can perform is welcome, and is likely to show some problems, which will have to be fixed step by step. The code is simple and flexible enough so that should be easy.

@simoneliuzzo
Copy link
Contributor

Dear @lfarv,

I tested load_madx, load_mad8, save_elegant and load_elegant and they work as expected, producing identical phase space pictures. Assuming it is a complicated case, I used the FCC-ee lattice for test.

I tested also on EBS lattices, that I thought simpler. In fact the lattice I chose has bpms names "BPM_C03-08" and I think those are source of an issue for load_elegant and generate the error:
"SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers"

load_madx and load_mad8 work as expected.

I understood from the output that the save_madx, _mad8 and _elegant functions use of the RingParam element name for the lattice variable. This may be added in the documentation, or in a printed message.

Some additional help in the load / save methods could be nice.

I am unable to test if the elegant files produced by AT are usable in elegant

@lfarv
Copy link
Contributor Author

lfarv commented Nov 13, 2024

@simoneliuzzo : Excellent, thanks a lot ! Can you post the lattice file with problems ?

I understood from the output that the save_madx, _mad8 and _elegant functions use of the RingParam element name for the lattice variable

RingParam in python ? We use the name attribute of the Lattice object, which you can modify at any time. Does'n it make sense ? Anyway, I have to write the documentation…

@simoneliuzzo
Copy link
Contributor

simoneliuzzo commented Nov 13, 2024

Dear @lfarv,

if you have access to ESRF's machfs, those are the lines I am running:


r_at = at.load_lattice('/machfs/liuzzo/EBS/S28F/LATTICE/AT/S28F.mat', mat_key='LOW_EMIT_RING_INJ')
   at.save_madx(r_at, filename='/machfs/liuzzo/accelerator_toolbox_tests/python/load_save_mad_elegant/S28F_save_madx.mdx')
   r_madx = at.load_madx('/machfs/liuzzo/accelerator_toolbox_tests/python/load_save_mad_elegant/S28F_save_madx.mdx', use='EBS', strict=False)
   at.save_mad8(r_at, filename='/machfs/liuzzo/accelerator_toolbox_tests/python/load_save_mad_elegant/S28F_save_madx.md8')
   r_mad8 = at.load_mad8('/machfs/liuzzo/accelerator_toolbox_tests/python/load_save_mad_elegant/S28F_save_madx.md8', use='EBS', strict=False)
   at.save_elegant(r_at, filename='/machfs/liuzzo/accelerator_toolbox_tests/python/load_save_mad_elegant/S28F_save_ele.lte')
   r_elegant = at.load_elegant('/machfs/liuzzo/accelerator_toolbox_tests/python/load_save_mad_elegant/S28F_save_ele.lte', use='EBS', strict=False)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Python For python AT code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants