hibtc / cpymad Goto Github PK
View Code? Open in Web Editor NEWCython binding to MAD-X
Home Page: http://hibtc.github.io/cpymad/
License: Other
Cython binding to MAD-X
Home Page: http://hibtc.github.io/cpymad/
License: Other
Hi, Thomas
I want to get a desired profile at a specefied place in a transfer line, i.e. betx=3, bety=2, how should I do?
I have made a try, but it doesn't work. I want to plot beta function after matching.
My code is below.
from cpymad.madx import Madx
madx = Madx(command_log="HIMM_log.madx")
print(madx.version)
madx.command.call(file="mebt.txt")
twiss = madx.twiss(sequence='myline',
betx=10.89, alfx=-4.48, dx=1.77, dpx=0.66,
bety=6.98, alfy=-12.4, dy=0, dpy=0)
madx.match(sequence='myline',
constraints=[dict(range=['m1->betx','m1->bety'],betx=3,bety=2)],
vary=['q1->k1','q2->k2'])
q1 = madx.evaluate('q1->K1')
q2 = madx.evaluate('q2->K1')
import matplotlib.pylab as plt
plt.plot(twiss['s'], twiss['betx'])
mebt.txt
TITLE ,"mebt";
BEAM, PARTICLE=proton, PC=1.69602771203775;
D1: DRIFT, L=1;
Q1: QUADRUPOLE, L=0.35, K1:=-3.6013728752;
D2: DRIFT, L=0.35;
Q2: QUADRUPOLE, L=0.35, K1:=4.3532656572;
D3: DRIFT, L=0.35;
M1: MARKER;
Q3: QUADRUPOLE, L=0.35, K1=-2.3119829633;
D4: DRIFT, L=4.2150399;
Q4: QUADRUPOLE, L=0.35, K1=-3.6602446906;
D5: DRIFT, L=0.5;
Q5: QUADRUPOLE, L=0.35, K1=3.4786544844;
D6: DRIFT, L=1;
B1: SBEND, L=0.43022366062, ANGLE=-0.43022366062,
K1=0, E1=-0.21511183031, E2=-0.21511183031,
K2=0, HGAP=0.035, FINT=0;
D7: DRIFT, L=1;
B2: SBEND, L=0.43022366062, ANGLE=-0.43022366062,
K1=0, E1=-0.21511183031, E2=-0.21511183031,
K2=0, HGAP=0.035, FINT=0;
D8: DRIFT, L=1;
Q6: QUADRUPOLE, L=0.4, K1=2.8872328397;
D9: DRIFT, L=1.5;
Q7: QUADRUPOLE, L=0.4, K1=-1.65754164;
D10: DRIFT, L=1.5;
Q8: QUADRUPOLE, L=0.4, K1=1.4539004262;
D11: DRIFT, L=1.5232458;
B3: SBEND, L=0.43022366062, ANGLE=-0.43022366062,
K1=0, E1=-0.21511183031, E2=-0.21511183031,
K2=0, HGAP=0.035, FINT=0;
D12: DRIFT, L=2.5;
myline: LINE=(D1,Q1,D2,Q2,
D3,Q3,D4,Q4,
D5,Q5,D6,B1,
D7,B2,D8,Q6,
D9,Q7,D10,Q8,
D11,B3,D12);
use,sequence=myline;
pip install --user cpymad
creating build/temp.macosx-10.7-x86_64-3.6/src/cpymad
gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/anaconda3/include -arch x86_64 -I/anaconda3/include -arch x86_64 -I/anaconda3/include/python3.6m -c src/cpymad/libmadx.c -o build/temp.macosx-10.7-x86_64-3.6/src/cpymad/libmadx.o -std=gnu99
src/cpymad/libmadx.c:623:10: fatal error: 'madX/mad_types_f.h' file not found
#include "madX/mad_types_f.h"
^~~~~~~~~~~~~~~~~~~~
1 error generated.
error: command 'gcc' failed with exit status 1
----------------------------------------
Command "/anaconda3/bin/python -u -c "import setuptools, tokenize;file='/private/var/folders/vp/fh_kzrj97tx5y23r_4n684hw0000gn/T/pip-install-xe6bvybo/cpymad/setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, file, 'exec'))" install --record /private/var/folders/vp/fh_kzrj97tx5y23r_4n684hw0000gn/T/pip-record-pg51jxxw/install-record.txt --single-version-externally-managed --compile --user --prefix=" failed with error code 1 in /private/var/folders/vp/fh_kzrj97tx5y23r_4n684hw0000gn/T/pip-install-xe6bvybo/cpymad/
my settings:
ia$ uname -a
Darwin eduroam-1032.desy.de 18.7.0 Darwin Kernel Version 18.7.0: Tue Aug 20 16:57:14 PDT 2019; root:xnu-4903.271.2~2/RELEASE_X86_64 x86_64
(base) vpn-116:madx ia$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Currently, only "=" is used.
Hi, Thomas
How to do a match with small step, i.e.1e-6, in cpymad? When I run a small example in cpymad, an error occurs.
python script:
filename:HITFIL.py
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 23 07:42:22 2015
@author: Administrator
"""
from cpymad.madx import Madx
import numpy as np
import matplotlib.pylab as plt
import scipy.interpolate as itp
m = Madx(command_log='MATCH1.log')
print (m.version)
m.call('../MATCH1.madx')
m.survey(sequence='FODO')
m.match(
constraints=[
dict(range='#e', MUX='0.16', MUY='0.25')
],
vary=['QF->K1', 'QD->k1']
)
twiss = m.twiss(sequence='FODO', columns=['name','s','betx','bety'])
twiss['s'][15] = 12.5
x = np.arange(0,twiss['s'][-1],0.1)
#from scipy import interpolate
#tck = interpolate.splrep(bx, by, s=0)
#y = interpolate.splev(x, tck, der=0)
#y1 = interpolate.interp1d(bx, by, kind='cubic')
y = itp.spline(twiss['s'],twiss['betx'],x)
plt.plot(twiss['s'],twiss['betx'],'ro', label='betx')
plt.plot(x,y,'-k')
plt.plot(twiss['s'],twiss['bety'], label='bety')
plt.legend()
plt.show()
madx script:
filename:MATCH1.madx
//
//MADX Example: simple MATCH
//Author: Y. CH. Feng, IMP
//Date: 04122015
TITLE,'Example 3: MATCH1.MADX';
BEAM, PARTICLE = ELECTRON, PC = 3.0;
D: DRIFT, L = 1.0;
QF: QUADRUPOLE, L = 0.5, K1 := 0.2;
QD: QUADRUPOLE, L = 0.5, K1 := -0.2;
FODO: LINE = (QF, 5*(D), QD, QD, 5*(D), QF);
USE, PERIOD = FODO;
//....match phase advance at the end of cell to 60 and 90 deg respectively
//MATCH, SEQUENCE = FODO;
//CONSTRAINT, SEQUENCE = FODO, RANGE = #E, MUX = 0.16666666, MUY = 0.25;
//VARY, NAME = QF->K1, STEP = 1E-6;
//VARY, NAME = QD->K1, STEP = 1E-6;
//LMDIF, CALLS = 500, TOLERANCE = 1E-20;
//ENDMATCH;
//SELECT, FLAG = SECTORMAP, clear;
//SELECT, FLAG = TWISS, coloumn = name, s, betx, alfx, bety, alfy;
//TWISS, file = MATCH1.dat, sectormap;
//PLOT, HAXIS = S, VAXIS = BETX, BETY, COLOUR = 100;
//VALUE, TABLE(SUMM, Q1); //verify result
//VALUE, TABLE(SUMM, Q2);
error information from cpymad:
runfile('E:/cython_madx/test/cpymad/HITFIL.py', wdir='E:/cython_madx/test/cpymad')
MAD-X 5.02.05 (2015.04.06)
Traceback (most recent call last):
File "<ipython-input-12-bd24745bd20b>", line 1, in <module>
runfile('E:/cython_madx/test/cpymad/HITFIL.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/HITFIL.py", line 23, in <module>
vary=['QF->K1', 'QD->k1']
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\madx.py", line 424, in match
sequence = self._use(sequence)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\madx.py", line 388, in _use
or not self._libmadx.is_sequence_expanded(sequence)):
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\_rpc.py", line 93, in DeferredMethod
funcname, args, kwargs)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\rpc_util\client.py", line 80, in _request
self._conn.send((kind, args))
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\rpc_util\connection.py", line 53, in send
return pickle.dump(data, self._send, -1)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\_rpc.py", line 93, in DeferredMethod
funcname, args, kwargs)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\rpc_util\client.py", line 91, in _request
return self._dispatch(response)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\rpc_util\client.py", line 97, in _dispatch
return handler(*args)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\rpc_util\client.py", line 101, in _dispatch_exception
raise exc_info
AttributeError:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\rpc_util\service.py", line 85, in _dispatch
response = handler(*args)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\_rpc.py", line 110, in _dispatch_function_call
AttributeError: 'module' object has no attribute '__getnewargs__'
MAD-X crashes on the slightest problem. In these cases it could be a nice feature to reload the session in a new MAD-X process. The same conditions should be restored. This could be done as follows:
The first option will be slower and yield a state closer to the original.
The second options seems faster and cleaner to me, but imposes the additional burden of tracking all relevant changes and even deciding what is relevant.
It seems that table
and sequence
are mappings and could be renamed tables
and sequences
to make for a more meaningful interface.
A failed twiss computation
from cpymad.madx import Madx
m = Madx()
m.input("""
se: sequence, l=10;
endsequence;
beam;
use, sequence=se;
twiss, sequence=se;
""")
crashes, with the following output:
% python example.py
++++++++++++++++++++++++++++++++++++++++++++
+ MAD-X 5.04.01 (64 bit, Linux) +
+ Support: [email protected], http://cern.ch/mad +
+ Release date: 2018.07.10 +
+ Execution date: 2018.09.28 15:44:11 +
++++++++++++++++++++++++++++++++++++++++++++
enter Twiss module
TWDISP: Unable to compute intial dispersion --- dispersion set to zero.
TWDISP: Unable to compute intial dispersion --- dispersion set to zero.
Singular matrix occurred during closed orbit search.
/home/thomas/.virtualenvs/madgui/bin/python: +++ memory access outside program range, fatal +++
Traceback (most recent call last):
File "/home/thomas/hit/dev/minrpc/minrpc/client.py", line 100, in _request
response = self._communicate((kind, args))
File "/home/thomas/hit/dev/minrpc/minrpc/client.py", line 110, in _communicate
return self._conn.recv()
File "/home/thomas/hit/dev/minrpc/minrpc/connection.py", line 47, in recv
return pickle.load(self._recv)
EOFError: Ran out of input
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/thomas/hit/dev/cpymad/cpymad/madx.py", line 221, in input
self._libmadx.input(text)
File "/home/thomas/hit/dev/minrpc/minrpc/client.py", line 148, in DeferredMethod
funcname, args, kwargs)
File "/home/thomas/hit/dev/minrpc/minrpc/client.py", line 104, in _request
raise RemoteProcessCrashed()
minrpc.client.RemoteProcessCrashed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "example.py", line 10, in <module>
""")
File "/home/thomas/hit/dev/cpymad/cpymad/madx.py", line 224, in input
raise RuntimeError("MAD-X has stopped working!")
RuntimeError: MAD-X has stopped working!
Same command executed in MAD-X:
madx <<EOF
se: sequence, l=10;
endsequence;
beam;
use, sequence=se;
twiss, sequence=se;
EOF
++++++++++++++++++++++++++++++++++++++++++++
+ MAD-X 5.04.01 (64 bit, Linux) +
+ Support: [email protected], http://cern.ch/mad +
+ Release date: 2018.07.10 +
+ Execution date: 2018.09.28 15:43:18 +
++++++++++++++++++++++++++++++++++++++++++++
se: sequence, l=10;
endsequence;
beam;
use, sequence=se;
twiss, sequence=se;
enter Twiss module
TWDISP: Unable to compute intial dispersion --- dispersion set to zero.
TWDISP: Unable to compute intial dispersion --- dispersion set to zero.
Singular matrix occurred during closed orbit search.
++++++ Error: seterrorflag : Errorcode: 1 Reported from pro_twiss:
++++++ Error: seterrorflag : Description: TWISS failed
++++++ warning: Twiss failed: MAD-X continues
Number of warnings: 3
3 in C and 0 in Fortran
++++++++++++++++++++++++++++++++++++++++++++
+ MAD-X finished normally +
++++++++++++++++++++++++++++++++++++++++++++
( exit code 1 )
Note that the +++ Error: seterrorflag
messages don't appear in cpymad, so my first guess is this occurs within seterrorflag_fort
maybe related to some defect runtime initialization.
madseq is a parser and transformation script for MAD-X sequences. Its purpose is somewhat orthogonal to what cpymad currently does. Nonetheless, it is a reasonable question to ask whether it might make sense to integrate it in this repository.
pro:
con:
Hey, Thomas
I'm now back into win7 and get rid of scientific linux because it cracked again.
I have succeeded installing cpymad on my thinkpad with 64bits win7. The process is very successful and no problem has been happened.
First, I install pythonxy 2.7.9, the newest version currently.
Second, I install cpymad via pip according to your instructions.
But I have some troubles when running the Usage.
First, I can't modify the assignment to command_log. I want to store command history to my desired place.
Second, I can't find any MadX file in my disk. I remeber, that there exist amount of MadX examples in linux. What happened to this?
Third, could you give me a complete example to begin cpymad?
Hi, Thomas
I have successfully understood a match process, but, how to save twiss_table to a file, i.e. twiss_table .txt.
Sometimes I want to know the beta function value at a place of sequence, How to do?
Hi, Thomas
Is orbit correction not included in cpymad? I can't find any information about it.
Hey,
I have a problem with initializing the cpymad.madx.Madx() class.
cpymad 0.12.0 for python 3.4.3 installed via pip
Using conda package manager, all my packages are up to date.
I am just going to post the code I execute + the error message. Maybe you have seen this before.
In [1]: import cpymad.madx as mdx
NameError Traceback (most recent call last)
in ()
----> 1 m = madx.Madx()
NameError: name 'madx' is not defined
In [3]: m = mdx.Madx()
Traceback (most recent call last):
File "C:\Anaconda3\lib\runpy.py", line 170, in run_module_as_main
"main", mod_spec)
File "C:\Anaconda3\lib\runpy.py", line 85, in run_code
exec(code, run_globals)
File "C:\Anaconda3\lib\site-packages\cpymad_rpc.py", line 115, in
LibMadxService.stdio_main(sys.argv[1:])
File "C:\Anaconda3\lib\site-packages\cpymad\rpc_util\service.py", line 37, in stdio_main
svc.configure_logging()
File "C:\Anaconda3\lib\site-packages\cpymad\rpc_util\service.py", line 44, in configure_logging
logging.basicConfig(logLevel=logging.INFO)
File "C:\Anaconda3\lib\logging__init.py", line 1760, in basicConfig
raise ValueError('Unrecognised argument(s): %s' % keys)
OSError Traceback (most recent call last)
C:\Anaconda3\lib\site-packages\cpymad\rpc_util\client.py in _request(self, kind, *args)
79 try:
---> 80 self._conn.send((kind, args))
81 except ValueError:
C:\Anaconda3\lib\site-packages\cpymad\rpc_util\connection.py in send(self, data)
52 # improves performance by a factor ~50-100 in my tests:
---> 53 return pickle.dump(data, self._send, -1)
54
OSError: [Errno 22] Invalid argument
During handling of the above exception, another exception occurred:
RemoteProcessCrashed Traceback (most recent call last)
in ()
----> 1 m = mdx.Madx()
C:\Anaconda3\lib\site-packages\cpymad\madx.py in init(self, libmadx, command_log, error_log, **Popen_args)
222 _rpc.LibMadxClient.spawn_subprocess(**Popen_args)
223 libmadx = self._service.libmadx
--> 224 if not libmadx.is_started():
225 libmadx.start()
226 # init instance variables:
C:\Anaconda3\lib\site-packages\cpymad_rpc.py in DeferredMethod(_args, *_kwargs)
91 def DeferredMethod(_args, *_kwargs):
92 return self.__client._request('function_call', self.__module,
---> 93 funcname, args, kwargs)
94 return DeferredMethod
95
C:\Anaconda3\lib\site-packages\cpymad\rpc_util\client.py in _request(self, kind, *args)
84 raise
85 except IOError:
---> 86 raise RemoteProcessCrashed()
87 try:
88 response = self._conn.recv()
RemoteProcessCrashed:
When doing the same in a virtual python 2.7 environment everything works fine. Unfortunately all my tools are written in py3.4, so running cpymad in this virtual environment is not an option for me.
Maybe this issue is related to:
http://bugs.python.org/issue23207 ?
I tried the same on a different machine and got the same behaviour. Oddly enough, cpymad was working just fine for me a couple of weeks ago and it made my life a lot easier!
I really like your project!
Cheers!
Hi,
How to get a survey table using survey
command?
When I run
survey = madx.survey(sequence='myring',columns=['s','l','angle'])
an error occurs.
However, when running ,
survey = madx.command.survey(sequence='myring',columns=['s','l','angle'])
no error occurs, but I can't find anything about survey in variable explorer.
Error displaces.
survey = madx.survey(sequence='myring',columns=['s','l','angle'])
Traceback (most recent call last):
File "<ipython-input-84-7e6faf6b7761>", line 1, in <module>
survey = madx.survey(sequence='myring',columns=['s','l','angle'])
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\madx.py", line 358, in survey
return self.get_table('survey', columns)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\madx.py", line 460, in get_table
return proxy.copy(columns)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\madx.py", line 744, in copy
table.summary = self.summary
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\madx.py", line 730, in summary
return self._libmadx.get_table_summary(self._name)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\_rpc.py", line 93, in DeferredMethod
funcname, args, kwargs)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\rpc_util\client.py", line 91, in _request
return self._dispatch(response)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\rpc_util\client.py", line 97, in _dispatch
return handler(*args)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\rpc_util\client.py", line 101, in _dispatch_exception
raise exc_info
ValueError:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\rpc_util\service.py", line 85, in _dispatch
response = handler(*args)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\_rpc.py", line 111, in _dispatch_function_call
File "cpymad\libmadx.pyx", line 277, in cpymad.libmadx.get_table_summary (cpymad/libmadx.c:2703)
raise ValueError("No summary for table: {!r}".format(table_name))
ValueError: No summary for table: 'survey'
When doing parameter sweeps, the output of madx pollutes the standard output.
I have looked and found no way in the documentation to suppress or redirect that output.
Currently, specifying a range will only forward the parameter to MAD-X but not restrict the output range in the returned table.
Hi, Thomas
When I run my sequence under cpymad, a KeyError
occurs as follows:
elements = madx.sequences['MYRING'].elements
Traceback (most recent call last):
File "<ipython-input-45-c1560b76e813>", line 1, in <module>
elements = madx.sequences['MYRING'].elements
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\madx.py", line 531, in __getitem__
raise KeyError
My python code is below:
from cpymad.madx import Madx
madx = Madx(command_log="HIMM_log.madx")
print(madx.version)
madx.command.call(file="HIMM.txt")
twiss = madx.twiss(sequence='MYRING',columns=['s','x','y','betx','bety','Dx','Dy'])
elements = madx.sequences['MYRING'].elements
My sequence definition is below:
! ring definition----------------------------------------------------
CELL1:Line=(D11,Q11,D12,HB11,D13,PU11,D14,CY11,D15,
SR11,D16,Q12,D17,CX11,D18,SH11,D16,Q13,
D12,HB12,D19,PU12,D1a);
CELL2:Line=(CY22,D21,SV21,D22,HB22,D23,Q23,D24,SR21,
D25,Q22,D26,C21,D27,PU21,D28,HB21,D23,
Q21,D29);
CELL3:Line=(D31,PU31,D32,CX31,D33,Q31,D34,HB31,D35,
PU32,D32,CY31,D36,SR31,D37,Q32,D3c,CX32,D3b,SH31,
D37,Q33,D34,HB32,D39,CY32,D3a,PU33);
CELL4:Line=(D41,SV41,D42,HB42,D43,Q43,D44,SR41,D4b,CX42,D4c,
Q42,D46,CY41,D47,PU42,D48,HB41,D43,Q41,
D44,S41,D49,CX41,D49,PU41,D4a);
MYRING: Line=(CELL1,CELL2,CELL3,CELL4);
BEAM, particle = proton, energy = 150;
USE SEQUENCE = MYRING;
!--------------------------------------------------------------------
However, when I run a sequence with sequence
syntax definition not a line
definition, no KeyError
occurs. I think if the line
definition is the source of this error.
Continuing from #32:
We now have py35 and py36 builds of cpymad produced by manual compilation using mingw from WinPython3.4Qt5 (for a more detailed build description, see below).
While building the C extensions works fine, I found that the MAD-X background process crashes in a significant number of cases (between 10% and 90% depending on my PC's mood). At first, I thought this is a problem due to the new build process and the incompatibility between the different CRTs (ucrt in python3.5+ on the on hand, msvcr100 in winpy34 on the other side), but then I discovered that this problem seems to occur only on 64bit on win10. So far, I neither encountered problems on win7 64bit (using the exact same binary) nor on win7/10 32bit (using the same build process).
Some references on the CRT topic are given in #32 (comment).
While debugging, I found that this happens during startup when invoking cpymad.libmadx.start()
. Attaching a traceback obtained by running gdb on the following mx.py
:
import cpymad.libmadx as l
l.start()
Traceback:
C:\Users\thomas\hit\MAD-X-5.03.07\install\bin>gdb python
GNU gdb (GDB) 7.1.90.20100730-cvs
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-w64-mingw32".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from C:\Users\thomas\hit\python\WinPython-64bit-3.6.3.0\python-3.6.3.amd64/python.exe...(no debugging symbols found)...done.
(gdb) run mx.py
Starting program: C:\Users\thomas\hit\python\WinPython-64bit-3.6.3.0\python-3.6.3.amd64/python.exe mx.py
[New Thread 1968.0x1c54]
[New Thread 1968.0x13f4]
[New Thread 1968.0xb18]
[New Thread 1968.0xfc]
Program received signal SIGSEGV, Segmentation fault.
0x000000006823e2c8 in add_to_var_list ()
from C:\Users\thomas\hit\MAD-X-5.03.07\install\bin\libmadx.dll
(gdb) bt
#0 0x000000006823e2c8 in add_to_var_list ()
from C:\Users\thomas\hit\MAD-X-5.03.07\install\bin\libmadx.dll
#1 0x000000006823eced in enter_variable ()
from C:\Users\thomas\hit\MAD-X-5.03.07\install\bin\libmadx.dll
#2 0x00000000681eec55 in process () from C:\Users\thomas\hit\MAD-X-5.03.07\install\bin\libmadx.dll
#3 0x00000000681f0b74 in pro_input_ ()
from C:\Users\thomas\hit\MAD-X-5.03.07\install\bin\libmadx.dll
#4 0x000000006823d720 in get_defined_constants ()
from C:\Users\thomas\hit\MAD-X-5.03.07\install\bin\libmadx.dll
#5 0x00000000681e62c7 in madx_start ()
from C:\Users\thomas\hit\MAD-X-5.03.07\install\bin\libmadx.dll
#6 0x00000000735307a3 in _ctypes!DllCanUnloadNow ()
from C:\Users\thomas\hit\python\WinPython-64bit-3.6.3.0\python-3.6.3.amd64\DLLs\_ctypes.pyd
#7 0x00000056497eefe0 in ?? ()
#8 0x000000007353075a in _ctypes!DllCanUnloadNow ()
from C:\Users\thomas\hit\python\WinPython-64bit-3.6.3.0\python-3.6.3.amd64\DLLs\_ctypes.pyd
warning: (Internal error: pc 0x2 in read in psymtab, but not in symtab.)
warning: (Internal error: pc 0x2 in read in psymtab, but not in symtab.)
#9 0x0000000000000003 in ?? (warning: (Internal error: pc 0x2 in read in psymtab, but not in symtab.)
)
at C:/Users/thomas/hit/python/WinPython-64bit-3.4.4.4/python-3.4.4.amd64/share/mingwpy/x86_64-w64-mingw32/include/c++/bits/stl_vector.h:423
warning: (Internal error: pc 0x2 in read in psymtab, but not in symtab.)
#10 0x0000000073568ba3 in python36!_PyEval_EvalFrameDefault ()
from C:\Users\thomas\hit\python\WinPython-64bit-3.6.3.0\python-3.6.3.amd64\python36.dll
#11 0x00000000738394d0 in python36!_PyNamespace_Type ()
from C:\Users\thomas\hit\python\WinPython-64bit-3.6.3.0\python-3.6.3.amd64\python36.dll
#12 0x000001caddc0e340 in ?? ()
#13 0x00000056497eee10 in ?? ()
#14 0x0000000073589cb0 in python36!PyUnicode_Partition ()
from C:\Users\thomas\hit\python\WinPython-64bit-3.6.3.0\python-3.6.3.amd64\python36.dll
#15 0x00000056497eee90 in ?? ()
#16 0x000000007352f0b3 in _ctypes!DllCanUnloadNow ()
from C:\Users\thomas\hit\python\WinPython-64bit-3.6.3.0\python-3.6.3.amd64\DLLs\_ctypes.pyd
#17 0x000000007352ed90 in _ctypes!DllCanUnloadNow ()
from C:\Users\thomas\hit\python\WinPython-64bit-3.6.3.0\python-3.6.3.amd64\DLLs\_ctypes.pyd
#18 0x00000056497eee90 in ?? ()
warning: (Internal error: pc 0x27 in read in psymtab, but not in symtab.)
warning: (Internal error: pc 0x27 in read in psymtab, but not in symtab.)
warning: (Internal error: pc 0x27 in read in psymtab, but not in symtab.)
#19 0x0000000000000028 in ?? (warning: (Internal error: pc 0x27 in read in psymtab, but not in symtab.)
)
at C:/Users/thomas/hit/python/WinPython-64bit-3.4.4.4/python-3.4.4.amd64/share/mingwpy/x86_64-w64-mingw32/include/c++/bits/stl_construct.h:102
warning: (Internal error: pc 0x27 in read in psymtab, but not in symtab.)
warning: (Internal error: pc 0x0 in read in psymtab, but not in symtab.)
warning: (Internal error: pc 0x0 in read in psymtab, but not in symtab.)
warning: (Internal error: pc 0x0 in read in psymtab, but not in symtab.)
#20 0x0000000000000001 in ?? (warning: (Internal error: pc 0x0 in read in psymtab, but not in symtab.)
)
at C:/Users/thomas/hit/python/WinPython-64bit-3.4.4.4/python-3.4.4.amd64/share/mingwpy/x86_64-w64-mingw32/include/c++/bits/stl_vector.h:423
warning: (Internal error: pc 0x0 in read in psymtab, but not in symtab.)
#21 0x00000056497ef070 in ?? ()
#22 0x00000000681e5ed0 in update_sequ_constraints ()
from C:\Users\thomas\hit\MAD-X-5.03.07\install\bin\libmadx.dll
#23 0x00000056497eefe0 in ?? ()
#24 0x00000056497ef070 in ?? ()
#25 0x00000056497ef070 in ?? ()
#26 0x0000d93af7e47872 in ?? ()
#27 0x000001caddc5cb60 in ?? ()
#28 0x000001cadffcb240 in ?? ()
#29 0x0000000000000000 in ?? ()
First build MAD-X 5.04.00 as a static library (once on 32bit plus once on 64bit):
mkdir MAD-X-5.04.00\build
cd MAD-X-5.04.00\build
cmake .. -G "MinGW Makefiles" ^
-DCMAKE_INSTALL_PREFIX=..\installStatic ^
-DBUILD_SHARED_LIBS=OFF ^
-DMADX_STATIC=ON ^
-DUSE_GC=ON ^
-DMADX_ONLINE=OFF ^
-DCMAKE_BUILD_TYPE=Release
mingw32-make install
Then build cpymad. Here assuming py35/64bit:
set pythondir=..\WinPython-64bit-3.5.4.1\python-3.5.4.amd64
set madxdir=..\MAD-X-5.04.00\installStatic
set gcc=..\WinPython-64bit-3.4.4.4\python-3.4.4.amd64\Scripts\gcc.exe
set platform=win-amd64-3.5
set pyd_code=cp35-win_amd64
set pymajor=35
set tempdir=build\temp.%platform%\Release\cpymad
set builddir=build\lib.%platform%\cpymad
cython cpymad\libmadx.pyx -I %madxdir%\include
python setup.py build_py
%gcc% -mdll -O -Wall ^
-I%madxdir%\include ^
-I%pythondir%\include ^
-c cpymad/libmadx.c ^
-o %tempdir%\libmadx.obj ^
-std=gnu99
%gcc -shared -s ^
%tempdir%\libmadx.obj ^
-L%madxdir%\lib ^
-L%pythondir%\libs
-lmadx -lptc -lgc-lib -lstdc++ -lgfortran -lquadmath -lpython%pymajor% -lmsvcr100 ^
-o %builddir%\libmadx.%pyd_code%.pyd
python setup.py bdist_wheel
The reason is that our .travis.yml
recipe is designed to test the installed package rather than just the local checkout. On the one hand, this is nice, because detects problems due to modules not being included in the installation. On the other hand, it obviously confuses coveralls which cannot match the absolute path name of the installed file in the virtualenv to the file in the github repository.
Solutions:
Hi.
I have already added alignment and field errors to the magnets and then calculate the twiss parameters, same as we do in madx. The problem is that twiss does not see the errors and 'x' and 'y' columns in twiss table are both zero, even after adding errors. Which command shall I use/change to see the impact of errors on the electron trajectory?
The cpymad code is written below.
p.s. I used while loop to have different seeds, in order to have a better understanding of the effects of errors on electron trajectory.
from cern import cpymad
from matplotlib import pyplot as plt
m = cpymad.start_madx()
m.call('btserror.txt')
m.command.beam(sequence='bts', particle='ELECTRON',energy=3)
m.twiss('bts',fname='twissfile.txt', betx=9.054, alfx=-2.5995, bety=11.301, alfy=1.7948, dx=0.2301, dpx=0.0698)
n=0;
while (n<5):
m.command.eoption(seed='integer')
# Assign alignment errors to quadrupoles
m.command.select(flag="error", clear=True)
m.command.select(flag="error", pattern="qt1")
m.input('ealign, dx:=tgauss(2)*1e-4,dy:=tgauss(2)*1e-4,dpsi:=tgauss(2)*1e-4;')
m.command.esave(file="efield.tab0")
# Assign RELATIVE field errors to quadrupole
m.command.select(flag="error", clear=True)
m.command.select(flag="error", pattern="qt1")
m.input('efcomp, order:=0, radius:=0.013,dknr={tgauss(2)*1e-4,tgauss(2)*1e-3};')
m.command.esave(file="efield.tab2")
table=m.twiss('bts',fname='twissnew.txt', betx=9.054, alfx=-2.5995, bety=11.301, alfy=1.7948, dx=0.2301, dpx=0.0698)
column = table.columns
print column
f=open('data.txt','a')
for item1,item2,item3,item4,item5 in zip(column['s'],column['x'],column['y'],column['betx'],column['bety']):
print>> f,'', item1, '',item2, '', item3, '', item4, '',item5
f.close()
n=n+1;
else:
plt.plot(column['s'], column['x'])
plt.show()
Greetings,
I am having problems with installing cpymad via pip.
This is the log I get. Sorry for not reformatting, but I don't get it...
C:\TEMP>pip install cpymad
Collecting cpymad
Using cached cpymad-0.12.2.tar.gz
Requirement already satisfied (use --upgrade to upgrade): setuptools in c:\anaconda3\lib\site-packages\setuptools-18.4-py3.5.egg (from cpymad)
Requirement already satisfied (use --upgrade to upgrade): numpy in c:\anaconda3\lib\site-packages (from cpymad)
Requirement already satisfied (use --upgrade to upgrade): PyYAML in c:\anaconda3\lib\site-packages (from cpymad)
Building wheels for collected packages: cpymad
Running setup.py bdist_wheel for cpymad
Complete output from command C:\Anaconda3\python.exe -c "import setuptools;file='C:\Users\TKU\AppData\Local\Temp\pip-build-1f1l3aby\cpymad\setup.py';exec(compile(open(file).read().replace('\r\n', '\n'), file, 'exec'))" bdist_w
heel -d C:\Users\TKU\AppData\Local\Temp\tmpoebtrsukpip-wheel-:
running bdist_wheel
running build
running build_py
creating build
creating build\lib.win-amd64-3.5
creating build\lib.win-amd64-3.5\cpymad
copying cpymad\madx.py -> build\lib.win-amd64-3.5\cpymad
copying cpymad\types.py -> build\lib.win-amd64-3.5\cpymad
copying cpymad\util.py -> build\lib.win-amd64-3.5\cpymad
copying cpymad_rpc.py -> build\lib.win-amd64-3.5\cpymad
copying cpymad__init__.py -> build\lib.win-amd64-3.5\cpymad
creating build\lib.win-amd64-3.5\cpymad\rpc_util
copying cpymad\rpc_util\client.py -> build\lib.win-amd64-3.5\cpymad\rpc_util
copying cpymad\rpc_util\connection.py -> build\lib.win-amd64-3.5\cpymad\rpc_util
copying cpymad\rpc_util\file_monitor.py -> build\lib.win-amd64-3.5\cpymad\rpc_util
copying cpymad\rpc_util\ipc.py -> build\lib.win-amd64-3.5\cpymad\rpc_util
copying cpymad\rpc_util\posix.py -> build\lib.win-amd64-3.5\cpymad\rpc_util
copying cpymad\rpc_util\service.py -> build\lib.win-amd64-3.5\cpymad\rpc_util
copying cpymad\rpc_util\windows.py -> build\lib.win-amd64-3.5\cpymad\rpc_util
copying cpymad\rpc_util__init__.py -> build\lib.win-amd64-3.5\cpymad\rpc_util
running egg_info
writing dependency_links to cpymad.egg-info\dependency_links.txt
writing cpymad.egg-info\PKG-INFO
writing top-level names to cpymad.egg-info\top_level.txt
writing requirements to cpymad.egg-info\requires.txt
warning: manifest_maker: standard file '-c' not found
reading manifest file 'cpymad.egg-info\SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'cpymad.egg-info\SOURCES.txt'
copying cpymad\clibmadx.pxd -> build\lib.win-amd64-3.5\cpymad
copying cpymad\libmadx.c -> build\lib.win-amd64-3.5\cpymad
copying cpymad\libmadx.pyx -> build\lib.win-amd64-3.5\cpymad
creating build\lib.win-amd64-3.5\cpymad\COPYING
copying cpymad\COPYING\cpymad.rst -> build\lib.win-amd64-3.5\cpymad\COPYING
copying cpymad\COPYING\madx.rst -> build\lib.win-amd64-3.5\cpymad\COPYING
running build_ext
building 'cpymad.libmadx' extension
creating build\temp.win-amd64-3.5
creating build\temp.win-amd64-3.5\Release
creating build\temp.win-amd64-3.5\Release\cpymad
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\amd64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\Anaconda3\include -IC:\Anaconda3\include "-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE" "-IC:\Program Files (x86)
\Microsoft Visual Studio 14.0\VC\ATLMFC\INCLUDE" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10150.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.6\include\um" "-IC:\Program Files (x86)\Windows Kits\8.1\include\shared" "-IC:\P
rogram Files (x86)\Windows Kits\8.1\include\um" "-IC:\Program Files (x86)\Windows Kits\8.1\include\winrt" /Tccpymad/libmadx.c /Fobuild\temp.win-amd64-3.5\Release\cpymad/libmadx.obj -std=gnu99
cl : Befehlszeile warning D9002 : Unbekannte Option "-std=gnu99" wird ignoriert.
libmadx.c
cpymad/libmadx.c(257): fatal error C1083: Datei (Include) kann nicht geöffnet werden: "madX/mad_def.h": No such file or directory
error: command 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\amd64\cl.exe' failed with exit status 2
Failed building wheel for cpymad
Failed to build cpymad
Installing collected packages: cpymad
Running setup.py install for cpymad
Complete output from command C:\Anaconda3\python.exe -c "import setuptools, tokenize;file='C:\Users\TKU\AppData\Local\Temp\pip-build-1f1l3aby\cpymad\setup.py';exec(compile(getattr(tokenize, 'open', open)(file).read().replace('\r
\n', '\n'), file, 'exec'))" install --record C:\Users\TKU\AppData\Local\Temp\pip-jl3uinjp-record\install-record.txt --single-version-externally-managed --compile:
running install
running build
running build_py
running egg_info
writing requirements to cpymad.egg-info\requires.txt
writing dependency_links to cpymad.egg-info\dependency_links.txt
writing cpymad.egg-info\PKG-INFO
writing top-level names to cpymad.egg-info\top_level.txt
warning: manifest_maker: standard file '-c' not found
reading manifest file 'cpymad.egg-info\SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'cpymad.egg-info\SOURCES.txt'
running build_ext
building 'cpymad.libmadx' extension
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\amd64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\Anaconda3\include -IC:\Anaconda3\include "-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE" "-IC:\Program Files (x8
6)\Microsoft Visual Studio 14.0\VC\ATLMFC\INCLUDE" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10150.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.6\include\um" "-IC:\Program Files (x86)\Windows Kits\8.1\include\shared" "-IC:
\Program Files (x86)\Windows Kits\8.1\include\um" "-IC:\Program Files (x86)\Windows Kits\8.1\include\winrt" /Tccpymad/libmadx.c /Fobuild\temp.win-amd64-3.5\Release\cpymad/libmadx.obj -std=gnu99
cl : Befehlszeile warning D9002 : Unbekannte Option "-std=gnu99" wird ignoriert.
libmadx.c
cpymad/libmadx.c(257): fatal error C1083: Datei (Include) kann nicht geöffnet werden: "madX/mad_def.h": No such file or directory
error: command 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\amd64\cl.exe' failed with exit status 2
----------------------------------------
Command "C:\Anaconda3\python.exe -c "import setuptools, tokenize;file='C:\Users\TKU\AppData\Local\Temp\pip-build-1f1l3aby\cpymad\setup.py';exec(compile(getattr(tokenize, 'open', open)(file).read().replace('\r\n', '\n'), file, 'e
xec'))" install --record C:\Users\TKU\AppData\Local\Temp\pip-jl3uinjp-record\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in C:\Users\TKU\AppData\Local\Temp\pip-build-1f1l3aby\cpymad
C:\TEMP>
Hi, Thomas
I have a question to ask for your help again.
When I run the syntax twiss = madx.twiss(sequence='myline',columns=['s','x','y','betx','bety','Dx','Dy'])
, an error occurs. But, while running in plain madx, there have no any errors.
error information:
runfile('E:/HIMM_commissioning/cpymad/mebt.py', wdir='E:/HIMM_commissioning/cpymad')
MAD-X 5.02.05 (2015.04.06)
Traceback (most recent call last):
File "<ipython-input-7-40ce0ada60f3>", line 1, in <module>
runfile('E:/HIMM_commissioning/cpymad/mebt.py', wdir='E:/HIMM_commissioning/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:/HIMM_commissioning/cpymad/mebt.py", line 18, in <module>
twiss = madx.twiss(sequence='myline',columns=['s','x','y','betx','bety','Dx','Dy'])
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\madx.py", line 340, in twiss
return self.get_table('twiss', columns)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\madx.py", line 460, in get_table
return proxy.copy(columns)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\madx.py", line 744, in copy
table.summary = self.summary
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\madx.py", line 730, in summary
return self._libmadx.get_table_summary(self._name)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\_rpc.py", line 93, in DeferredMethod
funcname, args, kwargs)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\rpc_util\client.py", line 91, in _request
return self._dispatch(response)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\rpc_util\client.py", line 97, in _dispatch
return handler(*args)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\rpc_util\client.py", line 101, in _dispatch_exception
raise exc_info
ValueError:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\rpc_util\service.py", line 85, in _dispatch
response = handler(*args)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\_rpc.py", line 111, in _dispatch_function_call
File "cpymad\libmadx.pyx", line 277, in cpymad.libmadx.get_table_summary (cpymad/libmadx.c:2703)
raise ValueError("No summary for table: {!r}".format(table_name))
ValueError: No summary for table: 'twiss'
python code:
from cpymad.madx import Madx
madx = Madx(command_log="HIMM_log.madx")
print(madx.version)
madx.command.call(file="1.txt")
twiss = madx.twiss(sequence='myline',columns=['s','x','y','betx','bety','Dx','Dy'])
import matplotlib.pylab as plt
plt.plot(twiss['s'], twiss['betx'])
madx file with name '1.txt':
TITLE ,"mebt";
BEAM, PARTICLE=proton, PC=1.69602771203775;
D1: DRIFT, L=1;
Q1: QUADRUPOLE, L=0.35, K1=-3.6013728752;
D2: DRIFT, L=0.35;
Q2: QUADRUPOLE, L=0.35, K1=4.3532656572;
D3: DRIFT, L=0.35;
Q3: QUADRUPOLE, L=0.35, K1=-2.3119829633;
D4: DRIFT, L=4.2150399;
Q4: QUADRUPOLE, L=0.35, K1=-3.6602446906;
D5: DRIFT, L=0.5;
Q5: QUADRUPOLE, L=0.35, K1=3.4786544844;
D6: DRIFT, L=1;
B1: SBEND, L=0.43022366062, ANGLE=-0.43022366062,
K1=0, E1=-0.21511183031, E2=-0.21511183031,
K2=0, HGAP=0.035, FINT=0;
D7: DRIFT, L=1;
B2: SBEND, L=0.43022366062, ANGLE=-0.43022366062,
K1=0, E1=-0.21511183031, E2=-0.21511183031,
K2=0, HGAP=0.035, FINT=0;
D8: DRIFT, L=1;
Q6: QUADRUPOLE, L=0.4, K1=2.8872328397;
D9: DRIFT, L=1.5;
Q7: QUADRUPOLE, L=0.4, K1=-1.65754164;
D10: DRIFT, L=1.5;
Q8: QUADRUPOLE, L=0.4, K1=1.4539004262;
D11: DRIFT, L=1.5232458;
B3: SBEND, L=0.43022366062, ANGLE=-0.43022366062,
K1=0, E1=-0.21511183031, E2=-0.21511183031,
K2=0, HGAP=0.035, FINT=0;
D12: DRIFT, L=2.5;
myline: LINE=(D1,Q1,D2,Q2,
D3,Q3,D4,Q4,
D5,Q5,D6,B1,
D7,B2,D8,Q6,
D9,Q7,D10,Q8,
D11,B3,D12);
use,sequence=myline;
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?
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 10 14:22:58 2015
@author: Administrator
"""
from cpymad.madx import Madx
# create a new interpreter instance:
# the optional 'command_log' parameter can be used to store MAD-X
# command history.
madx = Madx(command_log="log.madx")
# determine the version of MAD-X that is actually loaded:
print(madx.version)
# you execute arbitrary textual MAD-X commands:
#madx.input('call, file="input_file.madx";')
# there is a more convenient syntax available which does the same:
madx.command.call(file="spamatch_global.max")
# And for some commands there exist direct shortcuts:
#madx.call('/path/to/some/input_file.madx')
#Calculate TWISS parameters
twiss = madx.twiss(sequence='x')
#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.show()
//sequence definition
// define the total length
circum=6912.0;
// define number of cells and therefore cell length
ncell = 108;
lcell = circum/ncell;
// forces and other constants;
// element definitions;
// define bending magnets and quadrupoles as multipoles
mbsps: multipole, knl={2.0*pi/(8*ncell)};
kqf = 4.36588E-02;
kqd = -4.36952E-02;
qf: multipole, knl:={0.0, kqf};
qd: multipole, knl:={0.0, kqd};
// define sextupoles for chromaticity correction
ksf = 2.04516700E-02;
ksd = -3.87252263E-02;
lsf: multipole, knl:={0.0,0.0, ksf};
lsd: multipole, knl:={0.0,0.0, ksd};
// define orbit correctors and beam position monitors
bpm: monitor, l=0.00001;
ch: hkicker, l=0.00001;
cv: vkicker, l=0.00001;
// sequence declaration;
cassps: sequence, refer=centre, l=circum;
start_machine: marker, at = 0;
// This defines ONE cell, repeat NCELL times
// to get the full machine
// SPS has 8 bending magnets per cell
n = 1;
while (n <= ncell) {
qf: qf, at=(n-1)*lcell+1.5425;;
lsf: lsf, at=(n-1)*lcell+3.6425;
ch: ch, at=(n-1)*lcell+4.2425;
bpm: bpm, at=(n-1)*lcell+4.3425;
mbsps: mbsps, at=(n-1)*lcell+5.0425;
mbsps: mbsps, at=(n-1)*lcell+11.4425;
mbsps: mbsps, at=(n-1)*lcell+23.6425;
mbsps: mbsps, at=(n-1)*lcell+30.0425;
qd: qd, at=(n-1)*lcell+33.5425;
lsd: lsd, at=(n-1)*lcell+35.6425;
cv: cv, at=(n-1)*lcell+36.2425;
bpm: bpm, at=(n-1)*lcell+36.3425;
mbsps: mbsps, at=(n-1)*lcell+37.0425;
mbsps: mbsps, at=(n-1)*lcell+43.4425;
mbsps: mbsps, at=(n-1)*lcell+55.6425;
mbsps: mbsps, at=(n-1)*lcell+62.0425;
n = n + 1;
}
end_machine: marker at=circum;
endsequence;
//madx definition
TITLE, s='MAD-X test';
call file="sps.seq";
option,-echo,-thin_foc;
// option,debug,-echo;
Beam, particle = proton, sequence=cassps, energy = 450.0,
NPART=1.05E11, sige= 4.5e-4 ;
use, period=cassps;
select,flag=twiss,column=name,s,x,y,mux,betx,muy,bety,dx,dy;
twiss,save,centre,file=twiss.out;
plot, haxis=s, vaxis=x, betx, bety,colour=100,
range=qd[10]/qd[16];
match, sequence=cassps;
vary,name=kqf, step=0.00001;
vary,name=kqd, step=0.00001;
global,sequence=cassps,Q1=26.58;
global,sequence=cassps,Q2=26.62;
Lmdif, calls=10, tolerance=1.0e-21;
endmatch;
match, sequence=cassps;
vary,name=ksf, step=0.00001;
vary,name=ksd, step=0.00001;
global,sequence=cassps,DQ1=0.0;
global,sequence=cassps,DQ2=0.0;
Lmdif, calls=10, tolerance=1.0e-21;
endmatch;
select,flag=twiss,column=name,s,x,y,mux,betx,muy,bety,dx,dy;
twiss,save,centre,file=twiss.out;
plot, haxis=s, vaxis=x, betx, bety,colour=100,
range=qd[10]/qd[16];
stop;
Hi, Thomas
When I run madx.set_value in ipython, an odd output occurs as follows.
In [17]: madx.set_value('R1QS1->K1', '42')
In [18]: madx.evaluate('R1QS1->K1')
Out[18]:
0.0


In the example below, it looks like the "beam;" command is not sent to mad, although it appears in the command log. Splitting the input string in two parts does not show the problem.
from cpymad.madx import Madx
mad=Madx(command_log="log1.madx")
mad.input("""
l1=1; l2=1; l3=1; l4=1;
mb1: sbend, angle:=a1, l:=l1,tilt:=t1;
mb2: sbend, angle:=a2, l:=l2,tilt:=t2;
mb3: sbend, angle:=a3, l:=l3,tilt:=t3;
mb4: sbend, angle:=a4, l:=l4,tilt:=t4;
myseq : line =(mb1,mb2,mb3,mb4);
beam; """)
try:
mad.use(sequence="myseq")
except RuntimeError:
print("beam command seems ignored")
mad=Madx(command_log="log2.madx")
mad.input("""
l1=1; l2=1; l3=1; l4=1;
mb1: sbend, angle:=a1, l:=l1,tilt:=t1;
mb2: sbend, angle:=a2, l:=l2,tilt:=t2;
mb3: sbend, angle:=a3, l:=l3,tilt:=t3;
mb4: sbend, angle:=a4, l:=l4,tilt:=t4;
myseq : line =(mb1,mb2,mb3,mb4);
""")
mad.input("beam;")
mad.use(sequence="myseq")
print("works")
It would be nice to simplify Madx.set_value
by allowing to set values using proxy objects:
# alternative syntax for madx.set_value(NAME, VAL)
madx.globals[NAME] = VAL
# Set properties directly on elements
elem = madx.sequences[SEQU].elements[ELEM]
elem['knl'] = [VAL0, VAL1]
# and even: (using another proxy object for the list)
elem['knl'][0] = VAL0
The simplest way to implement this is by issueing MAD-X commands like
elem, KNL={val0, val1};
It could also be done by setting the property directly on the actual C element. However, this will require to implement a cython-level ElementProxy
object which will need some clever IPC marshaling. Furthermore, I am not even sure yet, whether changes on the element fields are correctly propagated.
Hi, Thomas
How to use MADGUI?
Hi, Thomas
When I add some errors to dipoles, cpymad can't calculate the right "x" values, while in the madx it works well. If cpymad can't recognize the errors anyway I think.
import matplotlib.pylab as plt
from cpymad.madx import Madx
madx = Madx(command_log="HIMM_log.madx")
madx.command.call(file="HIMM_cpymad")
twiss0 = madx.twiss(sequence='himm')
plt.plot(twiss0['s'], twiss0['x'], label='x')
madx output:
Uploading madx.pdf…
HIMM_cpymad:
TITLE,'HIMM Project';
OPTION, INFO, -ECHO,WARN;
BEAM, particle = carbon, energy = 0.007;
! Defining elements
! drift--------------------------------------------------------------
D11:drift,l=1.75;
D12:drift,l=0.40;
D13:drift,l=0.25;
D14:drift,l=0.10;
D15:drift,l=0.13;
D16:drift,l=0.15;
D17:drift,l=1.775;
D18:drift,l=0.125;
D19:drift,l=0.25;
D1a:drift,l=0.53;
D21:drift,l=0.43;
D22:drift,l=0.35;
D23:drift,l=0.40;
D24:drift,l=0.15;
D25:drift,l=2.00;
D26:drift,l=0.44;
D27:drift,l=0.10;
D28:drift,l=0.24;
D29:drift,l=1.75;
D31:drift,l=0.67;
D32:drift,l=0.10;
D33:drift,l=0.63;
D34:drift,l=0.40;
D35:drift,l=0.25;
D36:drift,l=0.13;
D37:drift,l=0.15;
!D38:drift,l=2.00;
D39:drift,l=0.45;
D3a:drift,l=0.33;
D3b:drift,l=0.125;
D3c:drift,l=1.775;
D41:drift,l=0.43;
D42:drift,l=0.35;
D43:drift,l=0.40;
D44:drift,l=0.15;
!D45:drift,l=2.00;
D46:drift,l=0.455;
D47:drift,l=0.075;
D48:drift,l=0.25;
D491:drift,l=0.06;
D492:drift,l=0.07;
D4a:drift,l=0.81;
D4b:drift,l=0.73;
D4c:drift,l=1.17;
! marker-------------------------------------------------------------
m1:marker;
m2:marker;
! constant definition of dipoles-------------------------------------
N=8;
!bero=6.342738
!Bm=1.58568
ro = 4.0; !bero/Bm
leng := ro*(twopi/N);
Rr=0.07; !Reference radius of field measurement
a1=0; !quadrupole component in dipoles
a2=0.0; !end field
a3=0.0; !end field
! dipole without multipole-------------------------------------------
RB: SBEND, L=leng,ANGLE=TWOPI/N,K0=1/ro,K1=a1*(1/ro)*(1/Rr),
E1=PI/8,E2=PI/8,FINT=0.55,FINTX=0.55,HGAP=0.037;
!EB: RBEND, ANGLE=a2*TWOPI/N;
!EA: RBEND, ANGLE=a3*TWOPI/N;
! dipole definition--------------------------------------------------
B11:RB;
B12:RB;
B21:RB;
B22:RB;
B31:RB;
B32:RB;
B41:RB;
B42:RB;
HB11: LINE=(B11);
HB12: LINE=(B12);
HB21: LINE=(B21);
HB22: LINE=(B22);
HB31: LINE=(B31);
HB32: LINE=(B32);
HB41: LINE=(B41);
HB42: LINE=(B42);
! quadrupole without multipole---------------------------------------
QF: QUADRUPOLE, L=0.2, K1=0.799839688766;
QD: QUADRUPOLE, L=0.2, K1=-1.00783349327;
QFF: QUADRUPOLE, L=0.35, K1=1.038161630875;
!FQ: QUADRUPOLE, L=0.15,K1=0.00;
! quadrupole definition----------------------------------------------
Q11:QF;
Q12:QD;
Q13:QFF;
Q21:QF;
Q22:QD;
Q23:QFF;
Q31:QF;
Q32:QD;
Q33:QFF;
Q41:QF;
Q42:QD;
Q43:QFF;
!FQ41: FQ;
! chromaticity sextupole definition----------------------------------
SEXTCH: sextupole,l=0.15, k2=0.0;
SEXTCV: sextupole,l=0.15, k2=0.0;
SH11: SEXTCH;
SH31: SEXTCH;
SV21: SEXTCV;
SV41: SEXTCV;
! resonance sextupole definition-------------------------------------
SEXTR: sextupole,l=0.15, k2=0.0;
SR11: SEXTR;
SR21: SEXTR;
SR31: SEXTR;
SR41: SEXTR;
S41: SEXTR;
! corrector Kicker---------------------------------------------------
CYX: KICKER, L=0.1;
CY: VKICKER, L=0.1;
CX: HKICKER, L=0.1;
CX11:CX;
CX31:CX;
CX32:CX;
CX41:CX;
CX42:CX;
CY11:CY;
CY22:CY;
CY31:CY;
CY32:CY;
CY41:CY;
CY42:CY;
C21:CYX;
! MONITOR------------------------------------------------------------
PKUP: MONITOR, L=0.25;
BPM11: PKUP;
BPM12: PKUP;
BPM21: PKUP;
BPM31: PKUP;
BPM32: PKUP;
BPM33: PKUP;
BPM41: PKUP;
BPM42: PKUP;
PU11: LINE=(BPM11);
PU12: LINE=(BPM12);
PU21: LINE=(BPM21);
PU31: LINE=(BPM31);
PU32: LINE=(BPM32);
PU33: LINE=(BPM33);
PU41: LINE=(BPM41);
PU42: LINE=(BPM42);
! ring definition----------------------------------------------------
CELL1:Line=(D11,Q11,D12,HB11,D13,PU11,D14,CY11,D15,
SR11,D16,Q12,D17,CX11,D18,SH11,D16,Q13,
D12,HB12,D19,PU12,D1a);
CELL2:Line=(CY22,D21,SV21,D22,HB22,D23,Q23,D24,SR21,
D25,Q22,D26,C21,D27,PU21,D28,HB21,D23,
Q21,D29);
CELL3:Line=(D31,PU31,D32,CX31,D33,Q31,D34,HB31,D35,
PU32,D32,CY31,D36,SR31,D37,Q32,D3c,CX32,D3b,SH31,
D37,Q33,D34,HB32,D39,CY32,D3a,PU33);
CELL4:Line=(D41,SV41,D42,HB42,D43,Q43,D44,SR41,D4b,CX42,D4c,
Q42,D46,CY42,D47,PU42,D48,HB41,D43,Q41,
D44,S41,D491,CX41,D492,CY41,D491,PU41,D4a);
himm: Line=(CELL1,CELL2,CELL3,CELL4);
USE SEQUENCE = himm;
//--------------error definition of dipole-------------
SELECT, FLAG = ERROR, CLEAR;
SELECT, FLAG=ERROR, CLASS=B11;
EFCOMP, ORDER=0, RADIUS=0.1,
DKN = {1E-3,0,0},
DKS = {0,0,0};
SELECT,FLAG=TWISS,COLUMN=name,s,x,y,mux,betx,muy,bety,dx,dy;
TWISS, SAVE, FILE="HIMM-TWISS-BEF";
plot, range=#s/#e, haxis=s,vaxis=x,y,colour=100;
Hi, Thomas
When I run a simple example in pythonxy, an error occurs--RuntimeError: MAD-X has stopped working!
example:
from cpymad.madx import Madx
# create a new interpreter instance:
# the optional 'command_log' parameter can be used to store MAD-X
# command history.
madx = Madx(command_log="log.madx")
# determine the version of MAD-X that is actually loaded:
print(madx.version)
# you execute arbitrary textual MAD-X commands:
#madx.input('call, file="input_file.madx";')
# there is a more convenient syntax available which does the same:
madx.command.call(file="HITFIL.madx")
# And for some commands there exist direct shortcuts:
#madx.call('/path/to/some/input_file.madx')
#Calculate TWISS parameters
twiss = madx.twiss(sequence='RING', betx=0.1, bety=0.1, alfx=0.1, alfy=0.1)
#Your own analysis below
import matplotlib.pylab as plt
plt.plot(twiss['s'], twiss['betx'])
plt.show()
error:
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-1-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 21, in <module>
madx.command.call(file="HITFIL.madx")
File "C:\Python27\lib\site-packages\cpymad\madx.py", line 88, in __call__
self.__dispatch(util.mad_command(*args, **kwargs))
File "C:\Python27\lib\site-packages\cpymad\madx.py", line 227, in input
raise RuntimeError("MAD-X has stopped working!")
RuntimeError: MAD-X has stopped working!
There should be a prominent notice which version of MAD-X can be used with the current cpymad version.
Maybe setup.py
can include a check and fail hard on incompatible versions.
Currently, commands are checked only rudimentary. By defining a list of allowed parameters (+types) it should be easy to check any Madx command before execution.
EDIT: This information is already available in mad_dict.c
, i.e. in the const_command_def
variable, or just using the defined_commands
...
So scratch the following:
I have something like this in mind:
_commands = {
'twiss': {
'sequence': str,
'range': Range,
...
},
'match': {
...
}
}
def mad_command(*args, **kwargs):
if len(args) == 1:
command = arg[0]
param_types = _commands.get(command)
if command:
for name, value in kwargs.items():
try:
type = param_types[name]
except KeyError:
raise ValueError("Invalid parameter {!r} for command {}"
.format(name, command))
try:
value = type(value)
except ValueError:
raise ValueError("Invalid type ({}) for parameter {}"
.format(type, name))
...
Easy to implement by adding a parameter to init()
. Useful?
Hi, Thomas
I want to obtain the help information about a madx command via madx.help()
, but I can't obtain anything. How to do?
from cpymad.madx import Madx
madx = Madx()
print madx.help("twiss")
output:
None
Do you know if there is a docker container with cpymad? Thanks!
In the latest master I get instead with pip install -e .
I get
src/cpymad/libmadx.c:618:10: fatal error: madX/mad_types_f.h: No such file or directory
#include "madX/mad_types_f.h"
^~~~~~~~~~~~~~~~~~~~
compilation terminated.
error: command 'gcc' failed with exit status 1
Installing libmadx from sources and setting up MADXDIR works compiles, load, but seg_fault on twiss.
I have installed libmadx with:
cmake .. \
-DMADX_ONLINE=OFF \
-DMADX_INSTALL_DOC=OFF \
-DCMAKE_INSTALL_PREFIX=~/.local \
-DCMAKE_C_FLAGS="-fvisibility=hidden"
Do I understand correctly that a separate installation of libmadx is mandatory now?
Hey, Thomas
I have succeeded installing cpymad 0.10.3 on 64bits Scientific Linux 7. However, there exists an error when I run the Usage.
It is the code
madx = Madx(command_log="log.madx")
that raises an exception, which is as follows.
Traceback (most recent call last):
File "/root/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/cpymad/_rpc.py", line 374, in _dispatch
response = handler(args)
File "/root/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/cpymad/_rpc.py", line 465, in _dispatch_function_call
module = import(modname, None, None, '')
ImportError: /root/Canopy/appdata/canopy-1.5.1.2730.rh5-x86_64/lib/libgfortran.so.3: version `GFORTRAN_1.4' not found (required by /usr/local/lib/libmadx.so.5.02.04)
Building and deploying on windows systems is still not easy, since there is often no compiler available and, currently, only gcc is supported by setup.py
. Switching from a cython to a ctypes binding could remedy this issue. Another possibility might be CFFI.
Implications of using a ctypes binding:
make it possible to wrap all outputs with a physical units type. Or at least expose a mapping of units used in MAD-X.
Hi, Thomas
How to change the value of a magnet in a sequence in cpymad?
I want to change the value (i.e. angle) of some corrector, and to read the closed orbit of each BPM locations.
I would like to start a certain number of madx instances (say 16) to parallelize some studies. I understand that the communication with the Madx object are blocking, although the mad process lives in a separate process. I thought to create a list of Madx object and a thread pool to send the commands, but it does not really work reliably:
from concurrent.futures import ThreadPoolExecutor
from cpymad.madx import Madx
class MadxPool(object):
def __init__(self,mad_istances=1,workers=None):
self.mad_istances=mad_istances
if workers is None:
self.workers=mad_istances
else:
self.workers=workers
self.madout=[ [] for thread in range(self.mad_istances)]
self.mad=[ Madx(stdout=out.append) for out in self.madout]
self.pool=ThreadPoolExecutor(max_workers=self.workers)
def input(self,text):
for mad in self.mad:
self.pool.submit(mad.input,text)
def input_map(self,text_list):
for mad,text in zip(self.mad,text_list):
self.pool.submit(mad.input,text)
mad=MadxPool(16)
mad.input(<heavy script>)
The mad instances run in parallel, and partially work, however I get few of those errors:
Traceback (most recent call last):
File "/home/rdemaria/.pyenv/versions/anaconda3-5.2.0/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/home/rdemaria/.pyenv/versions/anaconda3-5.2.0/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/rdemaria/.pyenv/versions/anaconda3-5.2.0/lib/python3.6/site-packages/minrpc/service.py", line 120, in <module>
Service.stdio_main(sys.argv[1:])
File "/home/rdemaria/.pyenv/versions/anaconda3-5.2.0/lib/python3.6/site-packages/minrpc/service.py", line 38, in stdio_main
svc.run()
File "/home/rdemaria/.pyenv/versions/anaconda3-5.2.0/lib/python3.6/site-packages/minrpc/service.py", line 53, in run
while self._communicate():
File "/home/rdemaria/.pyenv/versions/anaconda3-5.2.0/lib/python3.6/site-packages/minrpc/service.py", line 74, in _communicate
return self._dispatch(request)
File "/home/rdemaria/.pyenv/versions/anaconda3-5.2.0/lib/python3.6/site-packages/minrpc/service.py", line 90, in _dispatch
self._reply_data(response)
File "/home/rdemaria/.pyenv/versions/anaconda3-5.2.0/lib/python3.6/site-packages/minrpc/service.py", line 111, in _reply_data
self._conn.send(('data', (data,)))
File "/home/rdemaria/.pyenv/versions/anaconda3-5.2.0/lib/python3.6/site-packages/minrpc/connection.py", line 53, in send
return pickle.dump(data, self._send, -1)
BrokenPipeError: [Errno 32] Broken pipe
Do you suggest another way? A process pool complains because the Madx object is not pickable.
Hi, Thomas
How to get the BPM readings? I write a small code, but it can't work anyway.
from cpymad.madx import Madx
madx = Madx(command_log="HIMM_log.madx")
madx.command.call(file="HIMM_cpymad")
madx.command.twiss(sequence="himm")
twiss0 = madx.get_table("twiss")
#use madx builtin function `table` to get BPM readings
bpm1 = madx.command.table('twiss','bpm12','x')
Hi, Thomas
When I match a sequence with small step, i.e. 1e-6, an error occurs.
python script:HITFIL.py
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 24 16:10:50 2015
@author: Administrator
"""
from cpymad.madx import Madx
m = Madx(command_log='MATCH1.log')
print (m.version)
m.call('../MATCH1.madx')
m.survey(sequence='FODO')
m.match(
constraints=[
dict(range='#e', MUX='0.16', MUY='0.25')
],
vary=['QF->K1', 'QD->k1']
)
madx file: MATCH1.madx
TITLE,'Example 3: MATCH1.MADX';
BEAM, PARTICLE = ELECTRON, PC = 3.0;
D: DRIFT, L = 1.0;
QF: QUADRUPOLE, L = 0.5, K1 := 0.2;
QD: QUADRUPOLE, L = 0.5, K1 := -0.2;
FODO: LINE = (QF, 5*(D), QD, QD, 5*(D), QF);
USE, PERIOD = FODO;
error information:
runfile('E:/cython_madx/test/cpymad/3.py', wdir='E:/cython_madx/test/cpymad')
MAD-X 5.02.05 (2015.04.06)
Traceback (most recent call last):
File "<ipython-input-14-9bd326b5c062>", line 1, in <module>
runfile('E:/cython_madx/test/cpymad/3.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/3.py", line 21, in <module>
vary=['QF->K1', 'QD->k1']
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\madx.py", line 424, in match
sequence = self._use(sequence)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\madx.py", line 388, in _use
or not self._libmadx.is_sequence_expanded(sequence)):
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\_rpc.py", line 93, in DeferredMethod
funcname, args, kwargs)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\rpc_util\client.py", line 80, in _request
self._conn.send((kind, args))
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\rpc_util\connection.py", line 53, in send
return pickle.dump(data, self._send, -1)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\_rpc.py", line 93, in DeferredMethod
funcname, args, kwargs)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\rpc_util\client.py", line 91, in _request
return self._dispatch(response)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\rpc_util\client.py", line 97, in _dispatch
return handler(*args)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\rpc_util\client.py", line 101, in _dispatch_exception
raise exc_info
AttributeError:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\rpc_util\service.py", line 85, in _dispatch
response = handler(*args)
File "C:\Python27\lib\site-packages\cpymad-0.10.6-py2.7-win32.egg\cpymad\_rpc.py", line 110, in _dispatch_function_call
AttributeError: 'module' object has no attribute '__getnewargs__'
Just a thought. The RPC service could be made asynchronous, so that requests don't block.
If this were ever implemented: it would probably be easy to allow multiple clients to connect to the same libmadx service process and let each receive events.
This allows to share a MAD-X interpreter among multiple client processes.
Hi, Thomas
How to calculate TWISS for a ring. My code can't work as follows. The plain Madx file COD_HIMM.txt can work very well under Madx program.
from cpymad.madx import Madx
madx = Madx(command_log="HIMM_log.madx")
print(madx.version)
madx.command.call(file="COD_HIMM.txt")
#calculate TWISS
twiss0 = madx.twiss(sequence='COD_HIMM.txt')
error occured:
twiss0 = madx.twiss(sequence='COD_HIMM.txt')
Traceback (most recent call last):
File "<ipython-input-89-95502ed45d2c>", line 1, in <module>
twiss0 = madx.twiss(sequence='COD_HIMM.txt')
File "D:\Program Files\lib\site-packages\cpymad\madx.py", line 407, in twiss
return self.get_table('twiss', columns)
File "D:\Program Files\lib\site-packages\cpymad\madx.py", line 560, in get_table
proxy = TableProxy(table, self._libmadx)
File "D:\Program Files\lib\site-packages\cpymad\madx.py", line 870, in __init__
raise ValueError("Invalid table: {!r}".format(name))
ValueError: Invalid table: 'twiss'
COD_HIMM.txt
TITLE "HITFiL Project"
OPTION, INFO, -ECHO, WARN;
!ASSIGN, PRINT="HITFiL-cod-print"
//ASSIGN, ECHO="HITFiL-cod-echo"
//ASSIGN, INFO="HITFiL-cod-info"
BEAM, PARTICLE=carbon, CHARGE=6, energy=0.007;
!---------constant definition-----------
mk1=1.5;
!-----Defining elements----
!----------drift------------------------------
D11:drift,l=1.75;
D12:drift,l=0.40;
D13:drift,l=0.30;
D14:drift,l=0.05;
D15:drift,l=0.13;
D16:drift,l=0.15;
D17:drift,l=1.70;
D18:drift,l=0.53;
D19:drift,l=0.25;
D21:drift,l=1.75;
D22:drift,l=0.40;
D23:drift,l=0.30;
D24:drift,l=0.05;
D25:drift,l=0.43;
D26:drift,l=2.00;
D27:drift,l=0.15;
D28:drift,l=0.35;
D29:drift,l=0.35;
D31:drift,l=1.02;
D32:drift,l=0.63;
D33:drift,l=0.40;
D34:drift,l=0.30;
D35:drift,l=0.05;
D36:drift,l=0.13;
D37:drift,l=0.15;
D38:drift,l=2.00;
D39:drift,l=0.45;
D3a:drift,l=0.33;
D41:drift,l=0.81;
D42:drift,l=0.44;
D43:drift,l=0.15;
D44:drift,l=0.40;
D45:drift,l=0.30;
D46:drift,l=0.05;
D47:drift,l=0.43;
D48:drift,l=2.00;
D49:drift,l=0.35;
D4a:drift,l=0.13;
!---------marker-----
m1:marker;
m2:marker;
!--------------constant difinition of dipoles------------------
N=8;
!bero=6.342738;
!Bm=1.58568;
ro=4.0; !bero/Bm
leng=ro*(twopi/N);
Rr=0.07;! Reference radius of field measurement (Half good-field width)
a1=0.0;!4.5e-4
a2=0.0;!
a3=0.0;!
!---------definition of dipole without multipole-----------
RB: SBEND, L=leng,ANGLE=TWOPI/N,K1=a1*(1/ro)*(1/Rr),HGAP=0.037,FINT=0.55,FINTX=0.55,E1=PI/8,E2=PI/8;
!--------dipole error--------------------------------------
B11:RB;
B12:RB;
B21:RB;
B22:RB;
B31:RB;
B32:RB;
B41:RB;
B42:RB;
KB11: HKICKER;
KB12: HKICKER;
KB21: HKICKER;
KB22: HKICKER;
KB31: HKICKER;
KB32: HKICKER;
KB41: HKICKER;
KB42: HKICKER;
HB11: LINE=(B11,KB11);
HB12: LINE=(B12,KB12);
HB21: LINE=(B21,KB21);
HB22: LINE=(B22,KB22);
HB31: LINE=(B31,KB31);
HB32: LINE=(B32,KB32);
HB41: LINE=(B41,KB41);
HB42: LINE=(B42,KB42);
!--------------quadrupole without multipole------------------
QF: QUADRUPOLE, L=0.2, K1=0.799839688766;
QD: QUADRUPOLE, L=0.2, K1=-1.00783349327;
QFF: QUADRUPOLE, L=0.35, K1=1.038161630875;
FQ: QUADRUPOLE, L=0.15,K1=0.00;
!-------------quadrupole error---------------------
Q11:QF;
Q12:QD;
Q13:QFF;
Q21:QF;
Q22:QD;
Q23:QFF;
Q31:QF;
Q32:QD;
Q33:QFF;
Q41:QF;
Q42:QD;
Q43:QFF;
FQ11: FQ;
FQ41: FQ;
!---------chromaticity sextupole--------------------
SEXTCH: sextupole,l=0.15, k2=0.0;
SEXTCV: sextupole,l=0.15, k2=0.0;
SH11: SEXTCH;
SH31: SEXTCH;
SV21: SEXTCV;
SV41: SEXTCV;
!-------------resonance sextupole--------------------
SEXTR: sextupole,l=0.15, k2=0.0;
SR11: SEXTR;
SR21: SEXTR;
SR31: SEXTR;
SR41: SEXTR;
!-------------CORRECTORS-------------------------
CY: VKICKER, L=0.1;
CY11:CY;
CY21:CY;
CY22:CY;
CY31:CY;
CY32:CY;
CY33:CY;
CY41:CY;
CY42:CY;
!--------------MONITOR---------------------
PKUP: MONITOR, L=0.25;
BPM11: PKUP;
BPM12: PKUP;
BPM21: PKUP;
BPM31: PKUP;
BPM32: PKUP;
BPM41: PKUP;
BPM42: PKUP;
PU11: LINE=(BPM11);
PU12: LINE=(BPM12);
PU21: LINE=(BPM21);
PU31: LINE=(BPM31);
PU32: LINE=(BPM32);
PU41: LINE=(BPM41);
PU42: LINE=(BPM42);
!--------ring definition-------------------------
CELL1:Line=(D11,Q11,D12,HB11,D13,PU11,D14,CY11,D15,&
SR11,D16,Q12,D16,FQ11,D17,SH11,D16,Q13,&
D12,HB12,D19,PU12,D18);
CELL2:Line=(D21,Q21,D22,HB21,D23,PU21,D24,CY21,D25,&
Q22,D26,SR21,D27,Q23,D22,HB22,D28,SV21,&
D25,CY22);
CELL3:Line=(D31,CY31,D32,Q31,D33,HB31,D34,PU31,D35,CY32,&
D36,SR31,D37,Q32,D38,SH31,D37,Q33,D33,&
HB32,D39,CY33,D3a,PU32);
CELL4:Line=(D41,PU41,D42,CY41,D43,Q41,D44,HB41,D45,&
PU42,D46,CY42,D47,Q42,D48,SR41,D43,Q43,&
D44,HB42,D49,SV41,D43,FQ41,D4a);
HIMM: Line=(CELL1,-CELL2,CELL3,-CELL4);
USE, sequence=HIMM;
gvar.h
declares a variable element_list
and some related element lists, that should be usable to expose a global list of elements, i.e. implement madx.elements[ELEM]
to be roughly equivalent to madx.sequences[SEQU].elements[ELEM]
- except when multiple elements have the same name, etc.
Hi, Thomas
Very sorry!
How to calculate TWISS for a ring. My code can't work as follows. The plain Madx file COD_HIMM.txt can work very well under Madx program.
from cpymad.madx import Madx
madx = Madx(command_log="HIMM_log.madx")
print(madx.version)
madx.command.call(file="COD_HIMM.txt")
#calculate TWISS
twiss0 = madx.twiss(sequence='himm')
error occured:
twiss0 = madx.twiss('himm')
Traceback (most recent call last):
File "<ipython-input-105-3fe236d6cf1f>", line 1, in <module>
twiss0 = madx.twiss('himm')
File "D:\Program Files\lib\site-packages\cpymad\madx.py", line 407, in twiss
return self.get_table('twiss', columns)
File "D:\Program Files\lib\site-packages\cpymad\madx.py", line 560, in get_table
proxy = TableProxy(table, self._libmadx)
File "D:\Program Files\lib\site-packages\cpymad\madx.py", line 870, in __init__
raise ValueError("Invalid table: {!r}".format(name))
ValueError: Invalid table: 'twiss'
COD_HIMM.txt
TITLE "HITFiL Project"
OPTION, INFO, -ECHO, WARN;
!ASSIGN, PRINT="HITFiL-cod-print"
//ASSIGN, ECHO="HITFiL-cod-echo"
//ASSIGN, INFO="HITFiL-cod-info"
BEAM, PARTICLE=carbon, CHARGE=6, energy=0.007;
!---------constant definition-----------
mk1=1.5;
!-----Defining elements----
!----------drift------------------------------
D11:drift,l=1.75;
D12:drift,l=0.40;
D13:drift,l=0.30;
D14:drift,l=0.05;
D15:drift,l=0.13;
D16:drift,l=0.15;
D17:drift,l=1.70;
D18:drift,l=0.53;
D19:drift,l=0.25;
D21:drift,l=1.75;
D22:drift,l=0.40;
D23:drift,l=0.30;
D24:drift,l=0.05;
D25:drift,l=0.43;
D26:drift,l=2.00;
D27:drift,l=0.15;
D28:drift,l=0.35;
D29:drift,l=0.35;
D31:drift,l=1.02;
D32:drift,l=0.63;
D33:drift,l=0.40;
D34:drift,l=0.30;
D35:drift,l=0.05;
D36:drift,l=0.13;
D37:drift,l=0.15;
D38:drift,l=2.00;
D39:drift,l=0.45;
D3a:drift,l=0.33;
D41:drift,l=0.81;
D42:drift,l=0.44;
D43:drift,l=0.15;
D44:drift,l=0.40;
D45:drift,l=0.30;
D46:drift,l=0.05;
D47:drift,l=0.43;
D48:drift,l=2.00;
D49:drift,l=0.35;
D4a:drift,l=0.13;
!---------marker-----
m1:marker;
m2:marker;
!--------------constant difinition of dipoles------------------
N=8;
!bero=6.342738;
!Bm=1.58568;
ro=4.0; !bero/Bm
leng=ro*(twopi/N);
Rr=0.07;! Reference radius of field measurement (Half good-field width)
a1=0.0;!4.5e-4
a2=0.0;!
a3=0.0;!
!---------definition of dipole without multipole-----------
RB: SBEND, L=leng,ANGLE=TWOPI/N,K1=a1*(1/ro)*(1/Rr),HGAP=0.037,FINT=0.55,FINTX=0.55,E1=PI/8,E2=PI/8;
!--------dipole error--------------------------------------
B11:RB;
B12:RB;
B21:RB;
B22:RB;
B31:RB;
B32:RB;
B41:RB;
B42:RB;
KB11: HKICKER;
KB12: HKICKER;
KB21: HKICKER;
KB22: HKICKER;
KB31: HKICKER;
KB32: HKICKER;
KB41: HKICKER;
KB42: HKICKER;
HB11: LINE=(B11,KB11);
HB12: LINE=(B12,KB12);
HB21: LINE=(B21,KB21);
HB22: LINE=(B22,KB22);
HB31: LINE=(B31,KB31);
HB32: LINE=(B32,KB32);
HB41: LINE=(B41,KB41);
HB42: LINE=(B42,KB42);
!--------------quadrupole without multipole------------------
QF: QUADRUPOLE, L=0.2, K1=0.799839688766;
QD: QUADRUPOLE, L=0.2, K1=-1.00783349327;
QFF: QUADRUPOLE, L=0.35, K1=1.038161630875;
FQ: QUADRUPOLE, L=0.15,K1=0.00;
!-------------quadrupole error---------------------
Q11:QF;
Q12:QD;
Q13:QFF;
Q21:QF;
Q22:QD;
Q23:QFF;
Q31:QF;
Q32:QD;
Q33:QFF;
Q41:QF;
Q42:QD;
Q43:QFF;
FQ11: FQ;
FQ41: FQ;
!---------chromaticity sextupole--------------------
SEXTCH: sextupole,l=0.15, k2=0.0;
SEXTCV: sextupole,l=0.15, k2=0.0;
SH11: SEXTCH;
SH31: SEXTCH;
SV21: SEXTCV;
SV41: SEXTCV;
!-------------resonance sextupole--------------------
SEXTR: sextupole,l=0.15, k2=0.0;
SR11: SEXTR;
SR21: SEXTR;
SR31: SEXTR;
SR41: SEXTR;
!-------------CORRECTORS-------------------------
CY: VKICKER, L=0.1;
CY11:CY;
CY21:CY;
CY22:CY;
CY31:CY;
CY32:CY;
CY33:CY;
CY41:CY;
CY42:CY;
!--------------MONITOR---------------------
PKUP: MONITOR, L=0.25;
BPM11: PKUP;
BPM12: PKUP;
BPM21: PKUP;
BPM31: PKUP;
BPM32: PKUP;
BPM41: PKUP;
BPM42: PKUP;
PU11: LINE=(BPM11);
PU12: LINE=(BPM12);
PU21: LINE=(BPM21);
PU31: LINE=(BPM31);
PU32: LINE=(BPM32);
PU41: LINE=(BPM41);
PU42: LINE=(BPM42);
!--------ring definition-------------------------
CELL1:Line=(D11,Q11,D12,HB11,D13,PU11,D14,CY11,D15,&
SR11,D16,Q12,D16,FQ11,D17,SH11,D16,Q13,&
D12,HB12,D19,PU12,D18);
CELL2:Line=(D21,Q21,D22,HB21,D23,PU21,D24,CY21,D25,&
Q22,D26,SR21,D27,Q23,D22,HB22,D28,SV21,&
D25,CY22);
CELL3:Line=(D31,CY31,D32,Q31,D33,HB31,D34,PU31,D35,CY32,&
D36,SR31,D37,Q32,D38,SH31,D37,Q33,D33,&
HB32,D39,CY33,D3a,PU32);
CELL4:Line=(D41,PU41,D42,CY41,D43,Q41,D44,HB41,D45,&
PU42,D46,CY42,D47,Q42,D48,SR41,D43,Q43,&
D44,HB42,D49,SV41,D43,FQ41,D4a);
himm: Line=(CELL1,-CELL2,CELL3,-CELL4);
USE, sequence=himm;
Hi, Thomas
When I add some errors to dipoles, cpymad can't calculate the right "x" values,(i.e. the x = 0 always everywhere), while in the madx it works well.
cpymad code:
import matplotlib.pylab as plt
from cpymad.madx import Madx
madx = Madx(command_log="HIMM_log.madx")
madx.command.call(file="HIMM_cpymad")
twiss0 = madx.twiss(sequence='himm')
plt.plot(twiss0['s'], twiss0['x'], label='x')
HIMM_cpymad:
//--------------error definition of dipole-------------
SELECT, FLAG = ERROR, CLEAR;
SELECT, FLAG=ERROR, CLASS=B11;
EFCOMP, ORDER=0, RADIUS=0.1,
DKN = {1E-3,0,0},
DKS = {0,0,0};
//--------------plot twiss-------------
SELECT,FLAG=TWISS,COLUMN=name,s,x,y,mux,betx,muy,bety,dx,dy;
TWISS, SAVE, FILE="HIMM-TWISS-BEF";
plot, range=#s/#e, haxis=s,vaxis=x,y,colour=100;
himm sequence listed in the previous issue.
CPyMAD models should include a version specification, such that cpymad can gracefully fail, whenever incompatible models are loaded.
The version information for models should consist of two parts:
On the other hand, I'm not sure whether this will be too complicated for users that have no insight in cpymad internals.
Hi,
I would like to install cpymad on WIN (10 Enterprise) using the simpleest approach.
I installed Anaconda Python 3.7 and used the instruction
pip install cpymad
All went fine but when I try
from cpymad.madx import Madx
myMad= Madx()
I get the following error:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-727443018fae> in <module>
1 from cpymad.madx import Madx
----> 2 myMad = Madx()
~\AppData\Local\Continuum\anaconda3\lib\site-packages\cpymad\madx.py in __init__(self, libmadx, command_log, stdout, history, **Popen_args)
171 if callable(stdout):
172 self.reader = AsyncReader(self._process.stdout, stdout)
--> 173 if not libmadx.is_started():
174 with self.reader:
175 libmadx.start()
~\AppData\Local\Continuum\anaconda3\lib\site-packages\minrpc\client.py in DeferredMethod(*args, **kwargs)
158 def DeferredMethod(*args, **kwargs):
159 return self.__client._request('function_call', self.__module,
--> 160 funcname, args, kwargs)
161 return DeferredMethod
~\AppData\Local\Continuum\anaconda3\lib\site-packages\minrpc\client.py in _request(self, kind, *args)
110 self._conn.close()
111 raise RemoteProcessCrashed()
--> 112 return self._dispatch(response)
113
114 def _communicate(self, message):
~\AppData\Local\Continuum\anaconda3\lib\site-packages\minrpc\client.py in _dispatch(self, response)
121 kind, args = response
122 handler = getattr(self, '_dispatch_%s' % (kind,))
--> 123 return handler(*args)
124
125 def _dispatch_exception(self, exc_type, message):
~\AppData\Local\Continuum\anaconda3\lib\site-packages\minrpc\client.py in _dispatch_exception(self, exc_type, message)
129 raise type(exc_type.__name__, (exc_type,), {
130 '__str__': lambda *args: message,
--> 131 '__init__': lambda *args: None})
132
133 def _dispatch_data(self, data):
ImportError: Traceback (most recent call last):
File "C:\Users\sterbini\AppData\Local\Continuum\anaconda3\lib\site-packages\minrpc\service.py", line 85, in _dispatch
response = handler(*args)
File "C:\Users\sterbini\AppData\Local\Continuum\anaconda3\lib\site-packages\minrpc\service.py", line 101, in _dispatch_function_call
module = __import__(modname, None, None, '*')
ImportError: DLL load failed: The specified module could not be found.
Can you help me?
Thanks a lot!
Guido
I'd like the interface to be more pythonic and object oriented, like the new model API.
Hi,
I was wondering if the community would find interesting to add a function in cpymad to convert madx code in markdown to ease the documentation process.
For instance I propose something like this
https://codimd.web.cern.ch/s/r1WHKg5iS
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.