Closed Landau1908 closed 9 years ago
Seriously, put more effort into it when reporting an issue!
++++++ warning: unknown sequence skipped: x
; ++++++ warning: unknown sequence ignored: x
)Question 1
Short version: It doesn't work. Always make sure to use the correct sequence name! Read the MAD-X output carefully, it tells you that it doesn't work.
If you had created a minimal example you would have noticed this on your own (as soon as you remove the TWISS command from your madx file)!
Explanation: There is a TWISS command in your .madx file. MAD-X then stores the calculated values in a memory table named "twiss". When executing madx.twiss(sequence='x')
from your python code, cpymad executes a command TWISS, sequence=x, ...
. For some reason MAD-X thinks that this is not an error and ignores the attempt. It does not signal the error so there is no way to infer this from python, and so the the python code just continues. It next loads values from the "twiss" table assuming its filled with happy new values. Since you previously calculated TWISS in some other way, there are indeed values, so this doesn't crash either. But it is an error!
Question 2
range
parameter of madx.twiss()
. But it may not work when multiple elements have the same name.twiss = madx.twiss(...)
elements = madx.sequences['cassps'].elements
start = elements.index('qd:10')
stop = elements.index('qd:16')
twiss = {twiss[k][start:stop] for k in ['s', 'betx', 'bety']}
plt.plot(twiss['s'], twiss['betx'])
...
And by the way: why do you intermix the native madx twiss+plotting and cpymad? The whole purpose of cpymad is to access the madx in data in python, so you can use more sophisticated tools for data analysis, such as matplotlib.
Very sorry for that.
Native madx twiss+plotting is just a model from madx guide, so I directly copy from that.
Could you give me an example about matching?
Hi, Thomas
Your code has an error.
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 10 14:22:58 2015
@author: Administrator
"""
from cpymad.madx import Madx
madx = Madx(command_log="log.madx")
# determine the version of MAD-X that is actually loaded:
print(madx.version)
# there is a more convenient syntax available which does the same:
madx.command.call(file="spamatch_global.max")
#m = Madx(ElementList = 'cassps')
#Calculate TWISS parameters
twiss = madx.twiss(sequence='cassps')
elements = madx.sequences['cassps'].elements
start = elements.index('qd:10')
stop = elements.index('qd:16')
twiss = {twiss[k][start:stop] for k in ['s', 'betx', 'bety']}
#Your own analysis below
import matplotlib.pylab as plt
plt.plot(twiss['s'], twiss['betx'])
plt.plot(twiss['s'], twiss['bety'])
plt.plot(twiss['s'], twiss['Dx'])
plt.plot(twiss['s'], twiss['x'], 'g')
plt.plot(twiss['s'], twiss['betx'])
plt.show()
error information:
runfile('E:/cython_madx/test/cpymad/1.py', wdir='E:/cython_madx/test/cpymad')
MAD-X 5.02.04 (2014.11.14)
Traceback (most recent call last):
File "<ipython-input-50-f4c790c83cb7>", line 1, in <module>
runfile('E:/cython_madx/test/cpymad/1.py', wdir='E:/cython_madx/test/cpymad')
File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 601, in runfile
execfile(filename, namespace)
File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 66, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "E:/cython_madx/test/cpymad/1.py", line 23, in <module>
twiss = {twiss[k][start:stop] for k in ['s', 'betx', 'bety']}
File "E:/cython_madx/test/cpymad/1.py", line 23, in <setcomp>
twiss = {twiss[k][start:stop] for k in ['s', 'betx', 'bety']}
TypeError: unhashable type: 'numpy.ndarray'
Sorry, the offending line must read:
twiss = {k: twiss[k][start:stop] for k in ['s', 'betx', 'bety']}
Thanks. That works well. Haha
There is a match
function. Its docstring also contains a small example code, which can be accessed as follows:
help(madx.match)
However, its functionality is currently somewhat restricted. In particular, it doesn't support the global
commands. You can either (a) contribute a patch to fix that, OR (b) write your match code like this
madx.command.match(sequence='cassps')
madx.command.vary(name='kqf', step=0.00001)
...
# 'global' is python keyword and therefore needs to be specified as string:
madx.command('global', sequence='cassps', Q1=26.58)
...
madx.command.endmatch()
Can madx.command
call any code in madx?
Yes. As stated in the Usage.
Hi, Thomas,
I have two problems. 1/ The TWISS parameters can be calculated from a sequence named any names, for example 'x' as showed in the below. Why?
2/ If I want to plot a piece of lattice not the whole, such as
range=qd[10]/qd[16]
, how I should do?