绘制mad输出(等等)。
madplot的Python项目详细描述
这个项目旨在促进在python中使用madx。它包含以下内容 主要部件:
- madx api:生成、分析和运行madx脚本。
- plot api:以各种格式打印madx输出。
- utilities:例如,将madx输出表转换为pandas数据帧。
MADX API
madx api由三部分组成:building,parsing和runningmadx脚本。
建设者
builder api可用于创建madx脚本。下面的示例代码显示 各种特点。
frommadplot.madx.builderimportScript# At first generate a new script.s=Script()# Labeled or declaration statements can be created via `[]` access.# This produces the following statement in the resulting MADX script:# L = 5;# N = 10;s['L']=5s['N']=10# MADX commands can be created by accessing them through the script instance.# Output: `DP: SBEND, L = L/2, ANGLE = 2*PI/(2*N);`.s['DP']=s.SBEND(L='L/2',ANGLE='2*PI/(2*N)')# Output: `QF: MULTIPOLE, KNL = {0, 1/f};`.s['QF']=s.MULTIPOLE(KNL=[0,'1/f'])# Sequences can be generated using the `Sequence` class.frommadplot.madx.builderimportSequencewithSequence(refer='entry',l='N*L')asseq:forninrange(s.N):# Python loop over number of cells.# Unlabeled statements can be just added the script instance.# Stored element definitions can be reused via attribute access of the script instance.# This produces the following output: `QF, at = 0 * L;`.seq+=s.QF(at=f'{n} * L')# [...] Add more elements.# Adding a sequence to the script will auto-expand it when dumping the script.# This produces the following output:# `LATTICE: sequence, refer = entry, l = N*L;`# ` QF, at = 0 * L;`# ` [...]`# `endsequence;`s['LATTICE']=seq# A script can be dumped by converting to `str`.withopen('example.seq','w')asf:f.write(str(s))
完整的代码示例
下面是一个完整的代码示例。
frommadplot.madx.builderimportSequence,Scripts=Script()s['N_cells']=60s['L_cell']=13.45s['f']=7.570366s['DP']=s.SBEND(L='L_cell/2',ANGLE='2*PI / (2*N_cells)')s['QF']=s.MULTIPOLE(KNL=['0','1/f'])s['QD']=s.MULTIPOLE(KNL=['0','-1/f'])withSequence(refer='entry',l='N_cells*L_cell')asseq:forninrange(s.N_cells):seq+=s.QF(at=f'{n} * L_cell')seq+=s.DP(at=f'{n} * L_cell')seq+=s.QD(at=f'{n} * L_cell + 0.50 * L_cell')seq+=s.DP(at=f'{n} * L_cell + 0.50 * L_cell')s['FODO_LATTICE']=seqwithopen('example.seq','w')asf:f.write(str(s))
高级控制
以下操作允许高级控制语句。
- 注释可以作为字符串放置:s += '// Comment'。
- 重新计算(延迟)的表达式(:=)可以通过E类:from madplot.madx.builder import E; s += s.ealign(dx=E('ranf()'))创建。
- 任何madx命令都可以通过脚本实例来访问:s += s.TWISS(file='optics')。
分析器
parser.Parser类有两个可用的方法:
- Parser.raw_parse:此方法将给定脚本解析为其语句并返回其列表。不同的语句类型可以在Parser._types中找到。将返回命令属性的文本值。
- Parser.parse:也将脚本解析为其语句,但只返回非注释的非变量声明语句并插入任何命令属性值。
例如:
>>>madx=''' ... L = 5; ... QF: QUADRUPOLE, k1 := pi/5, l = L; ... '''>>>Parser.raw_parse(madx)[[Variable]L=5,[Command]QF:QUADRUPOLE{'k1':'pi/5','l':'L'}]>>>Parser.parse(madx)[[Command]QF:QUADRUPOLE{'k1':0.6283185307179586,'l':5}]
发动机
madx引擎api可用于运行madx脚本。类需要一组模板 将用于运行脚本。模板是一个包含未填充部分的madx脚本 以后可以插值。第一个模板被视为入口点(主脚本),并将运行。
以下代码创建引擎:
frommadplot.madx.engineimportMADXEngineengine=MADXEngine(['test.madx','test.seq'],# Template files; `test.madx` is the main script.madx='/opt/madx',# File path to the MADX executable; if not specifed the `MADX` environment variable will be considered.working_directory='/tmp/test'# The directory in which the engine runs the scripts.)
模板可以包含遵循python字符串格式规则的替换。 例如:QF: QUADRUPOLE, KL={kl};。运行脚本时可以插入{kl}部分。
可以调用run方法来运行脚本。它需要一个输出文件名列表(需要 由模板脚本生成)。默认情况下,文件内容将作为^{tt12}返回$ 实例。
twiss,=engine.run(['example.twiss'])
这里需要在运行test.madx时生成文件example.twiss。 如果一个或多个模板脚本需要插值,则可以指定相应的值 使用配置关键字参数:
twiss,=engine.run(['example.twiss'],configuration={'test.madx':{'kl':0.01}})
输出转换的特殊参数可以以dict:
的形式为每个输出指定(twiss,meta),=engine.run([('example.twiss',{'return_meta':True}])
这将沿着主数据帧返回元数据(在tfs输出中前缀为@)。
会话
MADXSession可用于运行交互式madx会话。这有利于避免重新运行 每次运行都是相同的脚本(例如序列结构);它也不需要为 每次跑步。相反,只能发出相关命令(例如,更新光学参数),然后 询问结果(例如twiss文件生成)。例如:
frommadplot.madx.engineimportMADXSessionwithopen('/tmp/log','w')aslog:session=MADXSession(stderr=log,stdout=log)session.run(['a := ranf()'])session.run(['value a']*3)# Running a script at start-up.session=MADXSession(['twiss_script.madx'])twiss,=session.run(results=['example.twiss'])# Update a parameter and regenerate twiss.twiss,=session.run(['some_parameter = 0','twiss, file="example.twiss"'],results=['example.twiss'])
绘图
可以在madplot.plot模块中使用各种绘图功能。请直接参考 更多信息,请参阅本模块。
实用程序
数据格式转换实用程序可在madplot.utils:
- Convert.tfs:将tfs文件转换为pandas数据帧,
- Convert.trackone:将trackone表(由TRACK, onetable = true输出)转换为pandas数据帧。