Comments (8)
Seriously, put more effort into it when reporting an issue!
- Please post minimal examples!! I can't overstate the importance of this. Your posted files contain lots of code which is completely unrelated to the problem. Often, when creating minimal examples, you will figure out the problem on your own.
- The issue title is indescriptive.
- The premise of your first question is wrong, as the output shows (
++++++ warning: unknown sequence skipped: x
;++++++ warning: unknown sequence ignored: x
) - Don't post anything that requires me clicking (annoying madx plots), except when it is essential to the problem.
- State the file names of the posted code. Otherwise, I will have to read your code before executing it in order to just figure out the file names. This is especially annoying since you didn't post minimal examples.
- (About your previous post:) You asked me how to plot multiple lines in matplotlib - don't ask me questions which you will find the answer in 1 minute searching on the internet!
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
- you can use the
range
parameter ofmadx.twiss()
. But it may not work when multiple elements have the same name. - You can slice the array manually after figuring out what the index is. This looks like the following:
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.
from cpymad.
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?
from cpymad.
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'
from cpymad.
Sorry, the offending line must read:
twiss = {k: twiss[k][start:stop] for k in ['s', 'betx', 'bety']}
from cpymad.
Thanks. That works well. Haha
from cpymad.
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()
from cpymad.
Can madx.command
call any code in madx?
from cpymad.
Yes. As stated in the Usage.
from cpymad.
Related Issues (20)
- Element redefinition and twiss HOT 5
- Add support to release Linux AArch64 wheels HOT 7
- EMIT module output is displayed with "delay" HOT 2
- TWISS: no beta0 found HOT 1
- New functions
- Can't run madx instance. TypeError HOT 5
- Build error on Win10 Anaconda 2022.5 HOT 11
- What is the preferred way to retrieve the command (string) with which an Element was created? HOT 3
- libmadx error installing on M1/M2 Apple Silicon HOT 7
- Pitfalls on compiling libmadx/cpymad on Linux HOT 5
- Proposal: specialize tables HOT 8
- Wheel for M1 apple? HOT 2
- Python 3.11 Wheels HOT 4
- Error building cpymad on M1 Apple Silicon - "-march=core2" not supported HOT 5
- call does not work when double slash is present in file path HOT 2
- access default beam HOT 1
- ValueError: TypeError: write() argument must be str HOT 4
- Python 3.12 wheels HOT 2
- Differences in cpymad `table.twiss` and madx `table(twiss, X, X)` when running `use` HOT 7
- Twiss at certain positions HOT 13
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cpymad.