Coder Social home page Coder Social logo

cpymad--twiss problem about cpymad HOT 8 CLOSED

hibtc avatar hibtc commented on July 29, 2024
cpymad--twiss problem

from cpymad.

Comments (8)

coldfix avatar coldfix commented on July 29, 2024

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

  1. you can use the range parameter of madx.twiss(). But it may not work when multiple elements have the same name.
  2. 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.

Landau1908 avatar Landau1908 commented on July 29, 2024

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.

Landau1908 avatar Landau1908 commented on July 29, 2024

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.

coldfix avatar coldfix commented on July 29, 2024

Sorry, the offending line must read:

twiss = {k: twiss[k][start:stop] for k in ['s', 'betx', 'bety']}

from cpymad.

Landau1908 avatar Landau1908 commented on July 29, 2024

Thanks. That works well. Haha

from cpymad.

coldfix avatar coldfix commented on July 29, 2024

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.

Landau1908 avatar Landau1908 commented on July 29, 2024

Can madx.command call any code in madx?

from cpymad.

coldfix avatar coldfix commented on July 29, 2024

Yes. As stated in the Usage.

from cpymad.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.