LEP optics database conversion to MADX

John Jowett

DateString[]

This notebook converts the LEP optics database from MAD8 to MADX format.

More specifically, it makes a list of files that seem to be MAD8 input files, ignoring all the others.  Any other files can be treated individually.

Then it makes a copy of each file in MADX format, with a name derived from the original, using a conversion function which also runs each file through MADX to check that it is syntactically correct.

A MAD console file (filetype .mou) for each MADX run is also saved.  It contains the string “deletable” in its file name so that it can be easily removed.

Finally a summary of the conversion results is saved as a Mathematica expression and as a CSV file.

Setup

Access to packages

To get access to the necessary packages in CERN, you need my package folder on the path.  Typically, this can be done by evaluating the following BEFORE the initialisation of this notebook.

PrependTo[$Path,If[$OperatingSystem=="Windows","G:\\Users\\j\\jowett\\Public\\math6","/afs/cern.ch/users/j/jowett/public/math6"]]

MADX program and packages

Needs["Madtomma`RunningMAD`"]

Needs["Madtomma`Mfs`OpticsUtilities`"]

The following package contains the essential function

Needs["Madtomma`MadLanguage`MAD8Conversion`"]

?TranslateLEPMAD8toMADX

LEP optics database

Now using a copy of this available in the lhcions project space on EOS file system

LEPopticsDatabase=FileNameJoin[{If[$OperatingSystem=="Windows","\\\\cernbox-smb.cern.ch\\eos","\eos"],"project","l","lhcions","JMJ","slath","machines"}]

However these UNC file paths will not allow us to run MAD via the runMAD function on Windows.    
So we have to map the folder to network drive Z: in Windows explorer .

If[$OperatingSystem=="Windows",LEPopticsDatabase="Z:"]

It is convenient to work directly in this folder (assuming we have write access).

SetDirectory[LEPopticsDatabase]

FileType[LEPopticsDatabase]

Check how far we need to go in sub-directory depth to find all the files.

Table[{depth,Length[FileNames["*","",depth]]},{depth,1,10}]

So 6 is enough.

dep=6

Directory[]

Choose files to be converted

Get all the file extensions that occur

lepexts=Union[FileExtension/@FileNames["*","",dep]]

This is the usual dog’s breakfast created by Unix users.  Some of these files are not MAD8 files and should not be converted but there is no way to tell programmatically.

There are no .madx files that matter.

FileNames["*.madx","",dep]

Select[lepexts,StringLength[#]==3&]

lepextTestView::usage="lepextTestView[ext] will print the first few lines of the first file found with a given extension.";

lepextTestView[ext_String]:=Module[{fn},
fn=First[FileNames["*."<>ext,"",dep]];
FilePrint[fn,10]]

lepextTestView["tune"]

The most practical thing to do is just edit a copy of the extension list, keeping only those that seem to be MAD8 files.

lepMAD8exts={"2m5","2m_res", "defs", "disl","diss", "hibl","i8","inj","INJBUMP_E",   "lep","lep1","lep13","lep2","lep92","lep96","lobs","mad","mad#", "new","old", "phys","phys2","phys3","phys4", "reversed","rflcell","rfldisl","rfscell","seq","sh","sol","specs","specs05v2","specs06v2","specs07v2","specs08v2","specs09v2","specs108","specs10v2","specs_1m25","specs_1m25_5cm","specs_1m5","specs_1m5_5cm","specs_2m","specs_2m_10cm","specs_2m_5cm","specs_2m_6cm","specs_2m_7cm","specs_2m_8cm","specs_2m_9cm","specsv2","sps",   "tune"}

That gives us the following list of files that should be converted

lepMAD8extsFileNames=Select[FileNames["*","",dep],MemberQ[lepMAD8exts,FileExtension[#]]&]

This version is much slower, avoid it.

(* lepMAD8extsFileNames=Union[Flatten[(FileNames["*."<>#1,"",dep]&)/@lepMAD8exts]]; *)

Length[lepMAD8extsFileNames]

Convert the list of files

Test on first few with timing since each test includes a run of MADX.

Timing[TranslateLEPMAD8toMADX[#,PrintTail->False]&/@Take[lepMAD8extsFileNames,5]//TableForm]

The timing does not include the MADX CPU time, it seems.

Do them all, first clearing away any residues of previous attempts

DeleteFile/@FileNames["*-deletable-check.mou","",dep]

DeleteFile/@FileNames["*-MADX.*","",dep]

lepDBconversion=TranslateLEPMAD8toMADX[#,PrintTail->False]&/@lepMAD8extsFileNames

Save a summary

Save the result as a Mathematica expression for possible later analysis or reference.

Save[FileNameTimeStamped["LEPDatabaseConversionResult.m"],lepDBconversion]

Also save it in CSV format for non-Mathematica users.

Export[FileNameTimeStamped["LEPDatabaseConversionResult.csv"],List@@@lepDBconversion]